Question
Could you check my code below and correct it? If you think it is completely wrong, could you give a right version based on the
Could you check my code below and correct it? If you think it is completely wrong, could you give a right version based on the test code?
int mdadm_read(uint32_t addr, uint32_t len, uint8_t *buf) { int bound=JBOD_NUM_DISKS*JBOD_DISK_SIZE; if(MountStatus!=2||len>1024||(addr+len)>bound||(len>0&&buf==NULL)){ return -1; } int curr_addr=addr; while(curr_addr
test code
int test_read_within_block() {
printf("running %s: ", __func__);
mdadm_mount();
/* Set the contents of JBOD drives to a specific pattern. */
jbod_initialize_drives_contents();
bool success = false;
uint8_t out[SIZE];
if (mdadm_read(0, SIZE, out) != SIZE) {
printf("failed: read failed ");
return 0;
}
uint8_t expected[SIZE] = {
0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa,
};
if (memcmp(out, expected, SIZE) != 0) {
char *out_s = stringify(out, SIZE);
char *expected_s = stringify(expected, SIZE);
printf("failed: got: %s expected: %s ", out_s, expected_s);
free(out_s);
free(expected_s);
goto out;
}
success = true;
out:
mdadm_unmount();
if (!success)
return 0;
printf("passed ");
return 1;
}
int test_read_across_blocks() {
printf("running %s: ", __func__);
mdadm_mount();
jbod_initialize_drives_contents();
bool success = false;
uint8_t out[SIZE];
if (mdadm_read(248, SIZE, out) != SIZE) {
printf("failed: read failed ");
goto out;
uint8_t expected[SIZE] = {
0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa,
0xbb, 0xbb, 0xbb, 0xbb,
0xbb, 0xbb, 0xbb, 0xbb,
};
if (memcmp(out, expected, SIZE) != 0) {
char *out_s = stringify(out, SIZE);
char *expected_s = stringify(expected, SIZE);
printf("failed: got: %s expected: %s ", out_s, expected_s);
free(out_s);
free(expected_s);
goto out;
}
success = true;
out:
mdadm_unmount();
if (!success)
return 0;
printf("passed ");
return 1;
}
int test_read_three_blocks() {
printf("running %s: ", __func__);
mdadm_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, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xb\
b, 0xbb, 0xbb, 0xbb,
0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xb\
b, 0xbb, 0xbb, 0xbb,
0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xb\
b, 0xbb, 0xbb, 0xbb,
0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xb\
b, 0xbb, 0xbb, 0xbb,
0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xb\
b, 0xbb, 0xbb, 0xbb,
0xbb, 0xcc,
};
if (memcmp(out, expected, TEST3_SIZE) != 0) {
char *out_s = stringify(out, TEST3_SIZE);
char *expected_s = stringify(expected, TEST3_SIZE);
printf("failed: got: %s expected: %s ", out_s, expected_s);
free(out_s);
free(expected_s);
goto out;
}
success = true;
out:
mdadm_unmount();
if (!success)
return 0;
printf("passed ");
return 1;
-UU-:----F1 tester.c
int test_read_across_disks() {
printf("running %s: ", __func__);
int rc = mdadm_mount();
if (rc != 1) {
printf("failed: mount should succeed on an unmounted system but it failed.\\
n");
return 0;
}
jbod_initialize_drives_contents();
bool success = false;
uint8_t out[SIZE];
if (mdadm_read(983032, SIZE, out) != SIZE) {
printf("failed: read failed ");
goto out;
uint8_t expected[SIZE] = {
0xee, 0xee, 0xee, 0xee,
0xee, 0xee, 0xee, 0xee,
0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff,
};
if (memcmp(out, expected, SIZE) != 0) {
char *out_s = stringify(out, SIZE);
char *expected_s = stringify(expected, SIZE);
printf("failed: got: %s expected: %s ", out_s, expected_s);
free(out_s);
free(expected_s);
goto out;
}
success = true;
mdadm_unmount();
if (!success)
return 0;
printf("passed ");
return 2;
}
char *stringify(uint8_t buf[], int length) {
char *p = (char *)malloc(length * 6);
for (int i = 0, n = 0; i
if (i && i % 16 == 0)
n += sprintf(p + n, " ");
n += sprintf(p + n, "0x%02x ", buf[i]);
}
return p;
}
technologies. Each of the disks in front of you consists of 256 blocks, and each block has 256 bytes, coming to a total of 256 x 256 = 65,536 bytes per disk. Since you bought 16 disks, the combined capacity is 16 x 65,536 = 1,048,576 bytes = 1 MB. We provide you with a device driver with a single function that you can use to control the disks. int jbod_operation (uint32_t op, uint8_t *block); This function returns 0 on success and -1 on failure. It accepts an operation through the op parameter, the format of which is described in Table 1, and a pointer to a buffer. The command field can be one of the following commands, which are declared as a C enum type in the header that we have provide to you: 1. JBOD_MOUNT: mount all disks in the JBOD and make them ready to serve commands. This is the first command that should be called on the JBOD before issuing any other commands; all commands before it will fail. When the command field of op is set to this command, all other fields in op are ignored by the JBOD driver. Similarly, the block argument passed to jbod_operation can be NULL. 2. JBOD_UNMOUNT: unmount all disks in the JBOD. This is the last command that should be called on the JBOD; all commands after it will fail. When the command field of op is set to this command, all other fields in op are ignored by the JBOD driver. Similarly, the block argument passed to jbod_- operation can be NULL. 1 3. JBOD_SEEK_TO_DISK: seeks to a specific disk. JBOD internally maintains an 1/0 position, a tuple consisting of {CurrentDiskID, Current BlockID), which determines where the next I/O operation will happen. This command seeks to the beginning of disk specified DiskID field in op. In other words, it modifies I/O position: it sets CurrentDiskID to DiskID specified in op and it sets Current BlockID to 0. When the command field of op is set to this command, the BlockID field in op is ignored by the JBOD driver. Similarly, the block argument passed to jbod_operation can be NULL. 4. JBOD_SEEK_TO_BLOCK: seeks to a specific block in current disk. This command sets the Current- BlockID in 1/0 position to the block specified in BlockID field in op. When the command field of op is set to this command, the DiskID field in op is ignored by the JBOD driver. Similarly, the block BlockID in 170 position to the block specified in BlockId field in op. When the command field of op is set to this command, the DiskID field in op is ignored by the JBOD driver. Similarly, the block argument passed to jbod_operation can be NULL. 5. JBOD_READ_BLOCK: reads the block in current I/O position into the buffer specified by the block argument to jbod_operation. The buffer pointed by block must be of block size, that is 256 bytes. More importantly, after this operation completes, the Current BlockID in I/O position is incre- mented by 1; that is, the next 1/0 operation will happen on the next block of the current disk. When the command field of op is set to this command, all other fields in op are ignored by the JBOD driver. 6. JBOD_WRITE_BLOCK: writes the data in the block buffer into the block in the current I/O position. The buffer pointed by block must be of block size, that is 256 bytes. More importantly, after this operation completes, the CurrentBlockID in 1/0 position is incremented by 1; that is, the next I/O operation will happen on the next block of the current disk. When the command field of op is set to this command, all other fields in op are ignored by the JBOD driver. After you finished your onboarding session with HR and enjoyed the free lunch with your new colleagues, you received an email from the manager of the team. Welcome, to the team! Here's your task for the next two weeks. You will be working on integrating JBOD into our existing storage system. Specifically, you will implement one of the functionalities of the mdadm utility in Linux. Mdadm stands for multiple disk and device administration, and it is a tool for doing cool tricks with multiple disks. You will implement one of such tricks supported by mdadm, called linear device. A linear device makes multiple disks appear as a one large disk to the operating system. In our case, we will use your program to configure 16 disks of size 64 KB as a single 1 MB disk. Below are the functions you need to implement. int mdadm_mount (void): Mount the linear device; now mdadm user can run read and oper- ations on the linear address space that combines all disks. It should return 1 on success and -1 on failure. Calling this function the second time without calling mdadm_unmount in between, should fail. int mdadm_unmount (void): Unmount the linear device; now all commands to the linear device should fail. It should return 1 on success and - 1 on failure. Calling this function the second time without calling mdadm_mount in between, should fail. int mda dm_read(uint32_t addr, uint32_t len, uint8_t *buf): Read len bytes into buf starting at addr. Read from an out-of-bound linear address should fail. A read larger than 1,024 bytes should fail; in other words, len can be 1,024 at most. There are a few more restrictions that you will find out as you try to pass the tests. technologies. Each of the disks in front of you consists of 256 blocks, and each block has 256 bytes, coming to a total of 256 x 256 = 65,536 bytes per disk. Since you bought 16 disks, the combined capacity is 16 x 65,536 = 1,048,576 bytes = 1 MB. We provide you with a device driver with a single function that you can use to control the disks. int jbod_operation (uint32_t op, uint8_t *block); This function returns 0 on success and -1 on failure. It accepts an operation through the op parameter, the format of which is described in Table 1, and a pointer to a buffer. The command field can be one of the following commands, which are declared as a C enum type in the header that we have provide to you: 1. JBOD_MOUNT: mount all disks in the JBOD and make them ready to serve commands. This is the first command that should be called on the JBOD before issuing any other commands; all commands before it will fail. When the command field of op is set to this command, all other fields in op are ignored by the JBOD driver. Similarly, the block argument passed to jbod_operation can be NULL. 2. JBOD_UNMOUNT: unmount all disks in the JBOD. This is the last command that should be called on the JBOD; all commands after it will fail. When the command field of op is set to this command, all other fields in op are ignored by the JBOD driver. Similarly, the block argument passed to jbod_- operation can be NULL. 1 3. JBOD_SEEK_TO_DISK: seeks to a specific disk. JBOD internally maintains an 1/0 position, a tuple consisting of {CurrentDiskID, Current BlockID), which determines where the next I/O operation will happen. This command seeks to the beginning of disk specified DiskID field in op. In other words, it modifies I/O position: it sets CurrentDiskID to DiskID specified in op and it sets Current BlockID to 0. When the command field of op is set to this command, the BlockID field in op is ignored by the JBOD driver. Similarly, the block argument passed to jbod_operation can be NULL. 4. JBOD_SEEK_TO_BLOCK: seeks to a specific block in current disk. This command sets the Current- BlockID in 1/0 position to the block specified in BlockID field in op. When the command field of op is set to this command, the DiskID field in op is ignored by the JBOD driver. Similarly, the block BlockID in 170 position to the block specified in BlockId field in op. When the command field of op is set to this command, the DiskID field in op is ignored by the JBOD driver. Similarly, the block argument passed to jbod_operation can be NULL. 5. JBOD_READ_BLOCK: reads the block in current I/O position into the buffer specified by the block argument to jbod_operation. The buffer pointed by block must be of block size, that is 256 bytes. More importantly, after this operation completes, the Current BlockID in I/O position is incre- mented by 1; that is, the next 1/0 operation will happen on the next block of the current disk. When the command field of op is set to this command, all other fields in op are ignored by the JBOD driver. 6. JBOD_WRITE_BLOCK: writes the data in the block buffer into the block in the current I/O position. The buffer pointed by block must be of block size, that is 256 bytes. More importantly, after this operation completes, the CurrentBlockID in 1/0 position is incremented by 1; that is, the next I/O operation will happen on the next block of the current disk. When the command field of op is set to this command, all other fields in op are ignored by the JBOD driver. After you finished your onboarding session with HR and enjoyed the free lunch with your new colleagues, you received an email from the manager of the team. Welcome, to the team! Here's your task for the next two weeks. You will be working on integrating JBOD into our existing storage system. Specifically, you will implement one of the functionalities of the mdadm utility in Linux. Mdadm stands for multiple disk and device administration, and it is a tool for doing cool tricks with multiple disks. You will implement one of such tricks supported by mdadm, called linear device. A linear device makes multiple disks appear as a one large disk to the operating system. In our case, we will use your program to configure 16 disks of size 64 KB as a single 1 MB disk. Below are the functions you need to implement. int mdadm_mount (void): Mount the linear device; now mdadm user can run read and oper- ations on the linear address space that combines all disks. It should return 1 on success and -1 on failure. Calling this function the second time without calling mdadm_unmount in between, should fail. int mdadm_unmount (void): Unmount the linear device; now all commands to the linear device should fail. It should return 1 on success and - 1 on failure. Calling this function the second time without calling mdadm_mount in between, should fail. int mda dm_read(uint32_t addr, uint32_t len, uint8_t *buf): Read len bytes into buf starting at addr. Read from an out-of-bound linear address should fail. A read larger than 1,024 bytes should fail; in other words, len can be 1,024 at most. There are a few more restrictions that you will find out as you try to pass the testsStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started