/** * Allocate memory for a block in an inode whose inode number is 'no'. */ void ext2_disk_inode_alloc_block(ext2_inodetable_t *inode, uint32_t no, uint32_t block) { uint32_t block_no = 0, block_offset = 0, group = 0; char *bg_buffer = malloc(BLOCKSIZE); for (uint32_t i = 0; i < BGDS; ++i) { if (BGD[i].free_blocks_count > 0) { ext2_disk_read_block(BGD[i].block_bitmap, (uint8_t *)bg_buffer); while (BLOCKBIT(block_offset)) ++block_offset; block_no = block_offset + SB->blocks_per_group * i + 1; group = i; break; } } if (!block_no) { kprintf("[kernel/ext2] No available blocks!\n"); free(bg_buffer); return; } // Found a block (block_no), we need to mark it as in-use uint8_t b = BLOCKBYTE(block_offset); b |= SETBIT(block_offset); BLOCKBYTE(block_offset) = b; ext2_disk_write_block(BGD[group].block_bitmap, (uint8_t *)bg_buffer); free(bg_buffer); ext2_set_real_block(inode, block, block_no); // Now update available blocks count BGD[group].free_blocks_count -= 1; ext2_disk_write_block(2, (uint8_t *)BGD); inode->blocks++; ext2_disk_write_inode(inode, no); }
uint32_t write_ext2_disk(fs_node_t *node, uint32_t offset, uint32_t size, uint8_t *buffer) { ext2_inodetable_t *inode = ext2_disk_inode(node->inode); uint32_t end = offset + size; uint32_t start_block = offset / BLOCKSIZE; uint32_t end_block = end / BLOCKSIZE; uint32_t end_size = end - end_block * BLOCKSIZE; uint32_t size_to_write = end - offset; kprintf("[kernel/ext2] Write at node 0x%x, offset %d, size %d, buffer=0x%x\n", node, offset, size, buffer); if (end_size == 0) { end_block--; } // need to update if size has increased. if (inode->size < end) { inode->size = end; ext2_disk_write_inode(inode, node->inode); } if (start_block == end_block) { void *buf = malloc(BLOCKSIZE); ext2_disk_inode_read_block(inode, node->inode, start_block, buf); memcpy((uint8_t *)((uintptr_t)buf + (offset % BLOCKSIZE)), buffer, size_to_write); kprintf("[kernel/ext2] Single-block write.\n"); ext2_disk_inode_write_block(inode, node->inode, start_block, buf); free(buf); free(inode); return size_to_write; } else { uint32_t block_offset; uint32_t blocks_read = 0; for (block_offset = start_block; block_offset < end_block; block_offset++, blocks_read++) { if (block_offset == start_block) { void *buf = malloc(BLOCKSIZE); ext2_disk_inode_read_block(inode, node->inode, block_offset, buf); memcpy((uint8_t *)((uint32_t)buf + (offset % BLOCKSIZE)), buffer, BLOCKSIZE - (offset % BLOCKSIZE)); kprintf("[kernel/ext2] Writing block [loop...]...\n"); ext2_disk_inode_write_block(inode, node->inode, start_block, buf); free(buf); } else { kprintf("[kernel/ext2] Writing block [buffer...?]...\n"); ext2_disk_inode_write_block(inode, node->inode, block_offset, buffer + BLOCKSIZE * blocks_read - (block_offset % BLOCKSIZE)); } } void *buf = malloc(BLOCKSIZE); ext2_disk_inode_read_block(inode, node->inode, end_block, buf); memcpy(buf, buffer + BLOCKSIZE * blocks_read - (block_offset % BLOCKSIZE), end_size); kprintf("[kernel/ext2] Writing block [tail]...\n"); ext2_disk_inode_write_block(inode, node->inode, end_block, buf); free(buf); } free(inode); return size_to_write; }
/** * Insert an entry named 'name' with type 'type' into a directory 'p_node' * at the end. * This function assumes that parent directory 'p_node' does not contain * any entry with same name as 'name'. Caller should ensure this. */ void insertdir_ext2_disk(ext2_inodetable_t *p_node, uint32_t no, uint32_t inode, char *name, uint8_t type) { /* XXX HACK This needs to be seriously fixed up. */ kprintf("[kernel/ext2] Request to insert new directory entry at 0x%x#%d->%d '%s' type %d\n", p_node, no, inode, name, type); assert(p_node->mode & EXT2_S_IFDIR); void *block = malloc(BLOCKSIZE); uint32_t block_nr = 0; ext2_disk_inode_read_block(p_node, no, block_nr, block); uint32_t dir_offset = 0; uint32_t total_offset = 0; // first, iterate pass the last entry in the parent directory. while (total_offset < p_node->size) { ext2_dir_t *d_ent = (ext2_dir_t *)((uintptr_t)block + dir_offset); if (d_ent->rec_len + total_offset == p_node->size) { d_ent->rec_len = d_ent->name_len + sizeof(ext2_dir_t); while (d_ent->rec_len % 4 > 0) { d_ent->rec_len++; } dir_offset += d_ent->rec_len; total_offset += d_ent->rec_len; break; } dir_offset += d_ent->rec_len; total_offset += d_ent->rec_len; // move on to the next block of this directory if needed. if (dir_offset >= BLOCKSIZE) { block_nr++; dir_offset -= BLOCKSIZE; ext2_disk_inode_read_block(p_node, no, block_nr, block); kprintf("[kernel/ext2] Advancing to next block...\n"); } } kprintf("[kernel/ext2] Total Offset = %d; block = %d; offset within block = %d\n", total_offset, block_nr, dir_offset); // Put the new directory entry at 'dir_offset' in block 'block_nr'. uint32_t size = p_node->size - total_offset; if (dir_offset + size > BLOCKSIZE) { kprintf("\033[1;31m[kernel/ext2] Just a warning: You probably just f****d everything.\033[0m\n"); } ext2_dir_t *new_entry = malloc(size); // Initialize the new entry. new_entry->inode = inode; new_entry->rec_len = size; new_entry->name_len = (uint8_t)strlen(name); new_entry->file_type = type; memcpy(&new_entry->name, name, strlen(name)); // Write back to block. memcpy(((uint8_t *)block) + dir_offset, new_entry, size); memset(((uint8_t *)block) + dir_offset + new_entry->rec_len, 0x00, 4); ext2_disk_inode_write_block(p_node, no, block_nr, block); // Update parent node size //p_node->size += size; ext2_disk_write_inode(p_node, no); free(block); }
/** * Allocate a new inode with parent as the parent directory node and name as the filename * within that parent directory. Returns a pointer to a memory-copy of the node which * the client can (and should) free. * 'ftype' is file type, used when adding the entry to the parent dir. 1 for regular file, * 2 for directory, etc... 'no' is the inode number of 'parent'. * Upon return, the inode number of the newly allocated inode will be stored in 'inode_no'. * * This function assumes that parent directory 'parent' does not contain any entry with * same name as 'name'. Caller shuold ensure this. * Note that inode just created using this function has size of 0, which means no data * blocks have been allocated to the inode. */ ext2_inodetable_t *ext2_disk_alloc_inode ( ext2_inodetable_t *parent, uint32_t no, char *name, uint16_t mode, uint32_t *inode_no ) { if ((parent->mode & EXT2_S_IFDIR) == 0 || name == NULL) { kprintf("[kernel/ext2] No name or bad parent.\n"); return NULL; } ext2_inodetable_t *inode; uint32_t node_no = 0, node_offset = 0, group = 0; char *bg_buffer = malloc(BLOCKSIZE); /* Locate a block with an available inode. Will probably be the first block group. */ for (uint32_t i = 0; i < BGDS; ++i) { if (BGD[i].free_inodes_count > 0) { #if EXT2_DEBUG_BLOCK_DESCRIPTORS kprintf("Group %d has %d free inodes!\n", i, BGD[i].free_inodes_count); #endif ext2_disk_read_block(BGD[i].inode_bitmap, (uint8_t *)bg_buffer); while (BLOCKBIT(node_offset)) ++node_offset; node_no = node_offset + ext2_disk_inodes_per_group * i + 1; group = i; break; } } if (!node_no) { kprintf("[kernel/ext2] Failure: No free inodes in block descriptors!\n"); free(bg_buffer); return NULL; } /* Alright, we found an inode (node_no), we need to mark it as in-use... */ uint8_t b = BLOCKBYTE(node_offset); #if EXT2_DEBUG_BLOCK_DESCRIPTORS kprintf("Located an inode at #%d (%d), the byte for this block is currently set to %x\n", node_no, node_offset, (uint32_t)b); #endif b |= SETBIT(node_offset); #if EXT2_DEBUG_BLOCK_DESCRIPTORS kprintf("We would want to set it to %x\n", (uint32_t)b); kprintf("Setting it in our temporary buffer...\n"); #endif BLOCKBYTE(node_offset) = b; #if EXT2_DEBUG_BLOCK_DESCRIPTORS kprintf("\nWriting back out.\n"); #endif ext2_disk_write_block(BGD[group].inode_bitmap, (uint8_t *)bg_buffer); free(bg_buffer); #if EXT2_DEBUG_BLOCK_DESCRIPTORS kprintf("Okay, now we need to update the available inodes count...\n"); kprintf("it is %d, it should be %d\n", BGD[group].free_inodes_count, BGD[group].free_inodes_count - 1); kprintf("\n"); kprintf("%d\n", BGD[group].free_inodes_count); #endif BGD[group].free_inodes_count -= 1; #if EXT2_DEBUG_BLOCK_DESCRIPTORS kprintf("%d\n", BGD[group].free_inodes_count); kprintf("\nOkay, writing the block descriptors back to disk.\n"); #endif ext2_disk_write_block(2, (uint8_t *)BGD); #if EXT2_DEBUG_BLOCK_DESCRIPTORS kprintf("Alright, we have an inode (%d), time to write it out to disk and make the file in the directory.\n", node_no); #endif // Get the inode struct from the disk and init it inode = ext2_disk_inode(node_no); inode->size = 0; inode->blocks = 0; inode->mode = mode; ext2_disk_write_inode(inode, node_no); *inode_no = node_no; // Create an entry in the parent directory uint8_t ftype = mode_to_filetype(mode); kprintf("[kernel/ext2] Allocated inode, inserting directory entry [%d]...\n", node_no); insertdir_ext2_disk(parent, no, node_no, name, ftype); return inode; }
/** * Insert an entry named 'name' with type 'type' into a directory 'p_node' * at the end. * This function assumes that parent directory 'p_node' does not contain * any entry with same name as 'name'. Caller should ensure this. */ void insertdir_ext2_disk(ext2_inodetable_t *p_node, uint32_t no, uint32_t inode, char *name, uint8_t type) { /* XXX HACK This needs to be seriously fixed up. */ debug_print(NOTICE, "Request to insert new directory entry at 0x%x#%d->%d '%s' type %d", p_node, no, inode, name, type); assert(p_node->mode & EXT2_S_IFDIR); void *block = malloc(BLOCKSIZE); uint32_t block_nr = 0; ext2_disk_inode_read_block(p_node, no, block_nr, block); uint32_t dir_offset = 0; uint32_t total_offset = 0; // first, iterate pass the last entry in the parent directory. while (total_offset < p_node->size) { ext2_dir_t *d_ent = (ext2_dir_t *)((uintptr_t)block + dir_offset); if (d_ent->rec_len + total_offset == p_node->size) { d_ent->rec_len = d_ent->name_len + sizeof(ext2_dir_t); while (d_ent->rec_len % 4 > 0) { d_ent->rec_len++; } dir_offset += d_ent->rec_len; total_offset += d_ent->rec_len; break; } dir_offset += d_ent->rec_len; total_offset += d_ent->rec_len; // move on to the next block of this directory if needed. if (dir_offset >= BLOCKSIZE) { block_nr++; dir_offset -= BLOCKSIZE; ext2_disk_inode_read_block(p_node, no, block_nr, block); debug_print(NOTICE, "Advancing to next block..."); } } debug_print(NOTICE, "total offset = %d; block = %d; offset within block = %d", total_offset, block_nr, dir_offset); // Put the new directory entry at 'dir_offset' in block 'block_nr'. uint32_t size = p_node->size - total_offset; if (dir_offset + size > BLOCKSIZE) { debug_print(WARNING, "Directory entry is beyond first block of directory. This might be bad."); } ext2_dir_t *new_entry = malloc(size); // Initialize the new entry. new_entry->inode = inode; new_entry->rec_len = size; new_entry->name_len = (uint8_t)strlen(name); new_entry->file_type = type; memcpy(&new_entry->name, name, strlen(name)); // Write back to block. memcpy(((uint8_t *)block) + dir_offset, new_entry, size); memset(((uint8_t *)block) + dir_offset + new_entry->rec_len, 0x00, 4); ext2_disk_inode_write_block(p_node, no, block_nr, block); free(new_entry); // Update parent node size //p_node->size += size; ext2_disk_write_inode(p_node, no); free(block); }