static int sfs_dirent_read_nolock(struct sfs_fs *sfs, struct sfs_inode *sin, int slot, struct sfs_disk_entry *entry) { assert(_SFS_INODE_GET_TYPE(sin->din) == SFS_TYPE_DIR && (slot >= 0 && slot < sin->din->blocks)); int ret; uint32_t ino; if ((ret = sfs_bmap_load_nolock(sfs, sin, slot, &ino)) != 0) { return ret; } assert(sfs_block_inuse(sfs, ino)); if ((ret = sfs_rbuf(sfs, entry, sizeof(struct sfs_disk_entry), ino, 0)) != 0) { return ret; } entry->name[SFS_MAX_FNAME_LEN] = '\0'; return 0; }
/* * sfs_dirent_read_nolock - read the file entry from disk block which contains this entry * @sfs: sfs file system * @sin: sfs inode in memory * @slot: the index of file entry * @entry: file entry */ static int sfs_dirent_read_nolock(struct sfs_fs *sfs, struct sfs_inode *sin, int slot, struct sfs_disk_entry *entry) { assert(sin->din->type == SFS_TYPE_DIR && (slot >= 0 && slot < sin->din->blocks)); int ret; uint32_t ino; // according to the DIR's inode and the slot of file entry, find the index of disk block which contains this file entry if ((ret = sfs_bmap_load_nolock(sfs, sin, slot, &ino)) != 0) { return ret; } assert(sfs_block_inuse(sfs, ino)); // read the content of file entry in the disk block if ((ret = sfs_rbuf(sfs, entry, sizeof(struct sfs_disk_entry), ino, 0)) != 0) { return ret; } entry->name[SFS_MAX_FNAME_LEN] = '\0'; return 0; }
/* * sfs_bmap_free_sub_nolock - set the entry item to 0 (free) in the indirect block */ static int sfs_bmap_free_sub_nolock(struct sfs_fs *sfs, uint32_t ent, uint32_t index) { assert(sfs_block_inuse(sfs, ent) && index < SFS_BLK_NENTRY); int ret; uint32_t ino, zero = 0; off_t offset = index * sizeof(uint32_t); if ((ret = sfs_rbuf(sfs, &ino, sizeof(uint32_t), ent, offset)) != 0) { return ret; } if (ino != 0) { if ((ret = sfs_wbuf(sfs, &zero, sizeof(uint32_t), ent, offset)) != 0) { return ret; } sfs_block_free(sfs, ino); } return 0; }
/* * sfs_bmap_get_sub_nolock - according entry pointer entp and index, find the index of indrect disk block * return the index of indrect disk block to ino_store. no lock protect * @sfs: sfs file system * @entp: the pointer of index of entry disk block * @index: the index of block in indrect block * @create: BOOL, if the block isn't allocated, if create = 1 the alloc a block, otherwise just do nothing * @ino_store: 0 OR the index of already inused block or new allocated block. */ static int sfs_bmap_get_sub_nolock(struct sfs_fs *sfs, uint32_t *entp, uint32_t index, bool create, uint32_t *ino_store) { assert(index < SFS_BLK_NENTRY); int ret; uint32_t ent, ino = 0; off_t offset = index * sizeof(uint32_t); // the offset of entry in entry block // if entry block is existd, read the content of entry block into sfs->sfs_buffer if ((ent = *entp) != 0) { if ((ret = sfs_rbuf(sfs, &ino, sizeof(uint32_t), ent, offset)) != 0) { return ret; } if (ino != 0 || !create) { goto out; } } else { if (!create) { goto out; } //if entry block isn't existd, allocated a entry block (for indrect block) if ((ret = sfs_block_alloc(sfs, &ent)) != 0) { return ret; } } if ((ret = sfs_block_alloc(sfs, &ino)) != 0) { goto failed_cleanup; } if ((ret = sfs_wbuf(sfs, &ino, sizeof(uint32_t), ent, offset)) != 0) { sfs_block_free(sfs, ino); goto failed_cleanup; } out: if (ent != *entp) { *entp = ent; } *ino_store = ino; return 0; failed_cleanup: if (ent != *entp) { sfs_block_free(sfs, ent); } return ret; }
static int sfs_bmap_get_sub_nolock(struct sfs_fs *sfs, uint32_t *entp, uint32_t index, bool create, uint32_t *ino_store) { assert(index < SFS_BLK_NENTRY); int ret; uint32_t ent, ino = 0; off_t offset = index * sizeof(uint32_t); if ((ent = *entp) != 0) { if ((ret = sfs_rbuf(sfs, &ino, sizeof(uint32_t), ent, offset)) != 0) { return ret; } if (ino != 0 || !create) { goto out; } } else { if (!create) { goto out; } if ((ret = sfs_block_alloc(sfs, &ent)) != 0) { return ret; } } if ((ret = sfs_block_alloc(sfs, &ino)) != 0) { goto failed_cleanup; } if ((ret = sfs_wbuf(sfs, &ino, sizeof(uint32_t), ent, offset)) != 0) { sfs_block_free(sfs, ino); goto failed_cleanup; } out: if (ent != *entp) { *entp = ent; } *ino_store = ino; return 0; failed_cleanup: if (ent != *entp) { sfs_block_free(sfs, ent); } return ret; }
int sfs_load_inode(struct sfs_fs *sfs, struct inode **node_store, uint32_t ino) { // while (1); lock_sfs_fs(sfs); struct inode *node; if ((node = lookup_sfs_nolock(sfs, ino)) != NULL) { goto out_unlock; } int ret = -E_NO_MEM; struct sfs_disk_inode *din; if ((din = kmalloc(sizeof(struct sfs_disk_inode))) == NULL) { goto failed_unlock; } // kprintf("in %s: ino is %d\n", __func__, ino); // while (1); // kprintf("%s\n", __func__); assert(sfs_block_inuse(sfs, ino)); if ((ret = sfs_rbuf(sfs, din, sizeof(struct sfs_disk_inode), ino, 0)) != 0) { goto failed_cleanup_din; } assert(_SFS_INODE_GET_NLINKS(din) != 0); if ((ret = sfs_create_inode(sfs, din, ino, &node)) != 0) { goto failed_cleanup_din; } sfs_set_links(sfs, vop_info(node, sfs_inode)); out_unlock: unlock_sfs_fs(sfs); *node_store = node; return 0; failed_cleanup_din: kfree(din); failed_unlock: unlock_sfs_fs(sfs); return ret; }
static int sfs_dirent_read_nolock(struct sfs_fs *sfs, struct sfs_inode *sin, int slot, struct sfs_disk_entry *entry) { assert(_SFS_INODE_GET_TYPE(sin->din) == SFS_TYPE_DIR && (slot >= 0 && slot < sin->din->blocks)); // kprintf("%s\n", __func__); int ret; uint32_t ino; if ((ret = sfs_bmap_load_nolock(sfs, sin, slot, &ino)) != 0) { return ret; } // kprintf("%s and ino is %d\n", __func__, ino); // if (ino == -1) { // kprintf("%d\n", __LINE__); // } assert(sfs_block_inuse(sfs, ino)); if ((ret = sfs_rbuf(sfs, entry, sizeof(struct sfs_disk_entry), ino, 0)) != 0) { return ret; } entry->name[SFS_MAX_FNAME_LEN] = '\0'; // kprintf("%s finish inner\n", __func__); return 0; }
/* * sfs_load_inode - If the inode isn't existed, load inode related ino disk block data into a new created inode. * If the inode is in memory alreadily, then do nothing */ int sfs_load_inode(struct sfs_fs *sfs, struct inode **node_store, uint32_t ino) { lock_sfs_fs(sfs); struct inode *node; if ((node = lookup_sfs_nolock(sfs, ino)) != NULL) { goto out_unlock; } int ret = -E_NO_MEM; struct sfs_disk_inode *din; if ((din = kmalloc(sizeof(struct sfs_disk_inode))) == NULL) { goto failed_unlock; } assert(sfs_block_inuse(sfs, ino)); if ((ret = sfs_rbuf(sfs, din, sizeof(struct sfs_disk_inode), ino, 0)) != 0) { goto failed_cleanup_din; } assert(din->nlinks != 0); if ((ret = sfs_create_inode(sfs, din, ino, &node)) != 0) { goto failed_cleanup_din; } sfs_set_links(sfs, vop_info(node, sfs_inode)); out_unlock: unlock_sfs_fs(sfs); *node_store = node; return 0; failed_cleanup_din: kfree(din); failed_unlock: unlock_sfs_fs(sfs); return ret; }