/** * Write to the 'block'th block within an inode 'inode' from the buffer 'buf'. * In other words, this function writes to the actual file content. * @return the actual block number read from. */ uint32_t ext2_disk_inode_write_block(ext2_inodetable_t *inode, uint32_t no, uint32_t block, uint8_t *buf) { /* We must allocate blocks up to this point to account for unused space in the middle. */ while (block >= inode->blocks) { kprintf("[kernel/ext2] Need to allocate blocks, have %d, want to write to #%d.\n", inode->blocks, block); ext2_disk_inode_alloc_block(inode, no, inode->blocks); if (block != inode->blocks - 1) { /* Clear the block */ uint32_t real_block = ext2_get_real_block(inode, inode->blocks - 1); uint8_t empty[1024] = {0}; memset(empty, 0x00, 1024); ext2_disk_write_block(real_block, empty); } } // The real work to write to a block of an inode. uint32_t real_block = ext2_get_real_block(inode, block); ext2_disk_write_block(real_block, buf); return real_block; }
/** * Write to the 'block'th block within an inode 'inode' from the buffer 'buf'. * In other words, this function writes to the actual file content. * @return the actual block number read from. */ uint32_t ext2_disk_inode_write_block(ext2_inodetable_t *inode, uint32_t inode_no, uint32_t block, uint8_t *buf) { /* We must allocate blocks up to this point to account for unused space in the middle. */ while (block >= inode->blocks) { ext2_disk_inode_alloc_block(inode, inode_no, inode->blocks); if (block != inode->blocks - 1) { /* Clear the block */ uint32_t real_block = ext2_get_real_block(inode, inode->blocks - 1); uint8_t * empty = malloc(BLOCKSIZE); memset(empty, 0x00, BLOCKSIZE); ext2_disk_write_block(real_block, empty); free(empty); } } // The real work to write to a block of an inode. uint32_t real_block = ext2_get_real_block(inode, block); debug_print(INFO, "virtual block %d maps to real block %d", block, real_block); ext2_disk_write_block(real_block, buf); return real_block; }
/** * Read the 'block'th block within an inode 'inode', and put it into * the buffer 'buf'. In other words, this function reads the actual file * content. 'no' is the inode number of 'inode'. * @return the actual block number read from. */ uint32_t ext2_disk_inode_read_block(ext2_inodetable_t *inode, uint32_t no, uint32_t block, uint8_t *buf) { // if the memory for 'block'th block has not been allocated to this inode, we need to // allocate the memory first using block bitmap. if (block >= inode->blocks) { /* Invalid block requested, return 0s */ memset(buf, 0x00, BLOCKSIZE); kprintf("[kernel/ext2] An invalid inode block [%d] was requested [have %d]\n", block, inode->blocks); return 0; } // The real work to read a block from an inode. uint32_t real_block = ext2_get_real_block(inode, block); ext2_disk_read_block(real_block, buf); return real_block; }