Question: #include #include #include #include mdadm.h #include jbod.h static int mounted = 0 ; / / Helper function to create JBOD operation command static

#include
#include
#include
#include "mdadm.h"
#include "jbod.h"
static int mounted =0;
// Helper function to create JBOD operation command
static uint32_t create_operation(jbod_cmd_t cmd, int disk_num, int block_num){
uint32_t op =0.
op |=(disk_num <<28); // Disk ID in bits 31-28
op |=(block_num <<20); // Block ID in bits 27-20
op |=(cmd <<12); // Command in bits 19-12
return op;
}
int mdadm_mount(void){
// If already mounted, return error
if (mounted){
return -1; // System is already mounted
}
// Create the mount operation command
// Since the operation is to mount, we don't care about disk_num or block_num.
uint32_t op = JBOD_MOUNT <<12; // Command is stored in bits 19-12, so shift JBOD_MOUNT by 12 bits.
// Call jbod_operation to execute the mount operation
if (jbod_operation(op, NULL)==0){
mounted =1; // Mark system as mounted
return 1; // Return success
} else {
return -1; // Mount operation failed
}
}
extern int mounted;
int mdadm_unmount(void){
// If not mounted, return error
if (!mounted){
return -1; // System is not mounted, cannot unmount
}
// Create the unmount operation command
// Since the operation is to unmount, we don't care about disk_num or block_num.
uint32_t op = JBOD_UNMOUNT <<12; // Command is stored in bits 19-12, so shift JBOD_UNMOUNT by 12 bits.
// Call jbod_operation to execute the unmount operation
if (jbod_operation(op, NULL)==0){
mounted =0; // Mark system as unmounted
return 1; // Return success
} else {
return -1; // Unmount operation failed
}
}
int mdadm_read(uint32_t start_addr, uint32_t read_len, uint8_t *read_buf){
// Basic validation checks
if (!mounted) return -3; // System is unmounted
if (read_len >1024) return -2; // Exceeds max read length
if (read_len >0 and read_buf == NULL) return -4; // Null buffer
if (read_len ==0) return 0; // Nothing to read
if (start_addr >= JBOD_NUM_DISKS * JBOD_DISK_SIZE) return -1; // Out of bounds
if (start_addr + read_len > JBOD_NUM_DISKS * JBOD_DISK_SIZE) return -1; // Out of bounds
uint32_t bytes_read =0.
uint32_t curr_addr = start_addr;
// Calculate the initial disk and block based on the start address
uint32_t disk_id = curr_addr / JBOD_DISK_SIZE;
uint32_t disk_offset = curr_addr % JBOD_DISK_SIZE;
uint32_t block_id = disk_offset / JBOD_BLOCK_SIZE;
uint32_t block_offset = disk_offset % JBOD_BLOCK_SIZE;
// Seek to the initial disk and block
uint32_t seek_disk_op = create_operation(JBOD_SEEK_TO_DISK, disk_id,0);
if (jbod_operation(seek_disk_op, NULL)!=0) return -4;
uint32_t seek_block_op = create_operation(JBOD_SEEK_TO_BLOCK, 0, block_id);
if (jbod_operation(seek_block_op, NULL)!=0) return -4;
while (bytes_read < read_len){
// Read the block at the current position
uint8_t block[JBOD_BLOCK_SIZE];
uint32_t read_op = create_operation(JBOD_READ_BLOCK, 0,0);
if (jbod_operation(read_op, block)!=0) return -4;
// Calculate how many bytes to copy from this block
uint32_t remaining_in_block = JBOD_BLOCK_SIZE - block_offset;
uint32_t remaining_to_read = read_len - bytes_read;
uint32_t bytes_to_copy =(remaining_in_block < remaining_to_read)? remaining_in_block : remaining_to_read;
// Copy the required bytes from the block to the output buffer
memcpy(read_buf + bytes_read, block + block_offset, bytes_to_copy);
// Update counters
bytes_read += bytes_to_copy;
curr_addr += bytes_to_copy;
if (curr_addr % JBOD_BLOCK_SIZE ==0){
block_id++; // Move to the next block
block_offset =0; // Reset block offset to 0 for the new block
}
// Reset block_offset since we start reading from the beginning of the next block
block_offset =0;
// Move to the next disk if the current address exceeds the disk boundary
if (curr_addr % JBOD_DISK_SIZE ==0){
disk_id++;
block_id =0;
seek_disk_op = create_operation(JBOD_SEEK_TO_DISK, disk_id,0);
if (jbod_operation(seek_disk_op, NULL)!=0) return -4;
}
}
return bytes_read; // Return the number of bytes read
}
This is my code for a JBOD program. Disk Operation Format:
| Bits | Width | Field | Description |
|0-3|4| DiskID | ID of the disk for the operation |
|4-11|8| BlockID | ID of the block within the disk |
|12-19|8| Command | The command to be executed |
|20-31|12| Reserved | Unused |
Each of these hard disks consists of `i` blocks, with each block holding `256` bytes. Since youve bought `j` disks, the total storage capacity is:
25616256=1,048,576 bytes =1 MB`
The following function can be used to control the disks:
`int jbod_operation(uint32_t op, uint8_t *block);
Only 8/10 of my test cases are
running .The mdadm_read function correctly handles reading within a single disk, but when it tries to read across disk boundaries, the data read does not match the expected result.
The test case is expecting the last 8 bytes of disk 14 to be 0xee and the first 8 bytes of disk 15 to be 0xff. However, the mdadm_read function is returning 0xaa for both disks,indicating that the disk switch (from disk 14 to disk 15). PLEASE CORRECT MY CODE TO HANDLE THESE EDGE CASES. you dont need more lib funcs

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!