/** * befs_read_lsmylink - read long symlink from datastream. * @sb: Filesystem superblock * @ds: Datastrem to read from * @buf: Buffer in which to place long symlink data * @len: Length of the long symlink in bytes * * Returns the number of bytes read */ size_t befs_read_lsymlink(struct super_block * sb, befs_data_stream * ds, void *buff, befs_off_t len) { befs_off_t bytes_read = 0; /* bytes readed */ u16 plen; struct buffer_head *bh = NULL; befs_debug(sb, "---> befs_read_lsymlink() length: %Lu", len); while (bytes_read < len) { bh = befs_read_datastream(sb, ds, bytes_read, NULL); if (!bh) { befs_error(sb, "BeFS: Error reading datastream block " "starting from %Lu", bytes_read); befs_debug(sb, "<--- befs_read_lsymlink() ERROR"); return bytes_read; } plen = ((bytes_read + BEFS_SB(sb)->block_size) < len) ? BEFS_SB(sb)->block_size : len - bytes_read; memcpy(buff + bytes_read, bh->b_data, plen); brelse(bh); bytes_read += plen; } befs_debug(sb, "<--- befs_read_lsymlink() read %u bytes", bytes_read); return bytes_read; }
/* * The inode of symbolic link is different to data stream. * The data stream become link name. Unless the LONG_SYMLINK * flag is set. */ static void * befs_follow_link(struct dentry *dentry, struct nameidata *nd) { befs_inode_info *befs_ino = BEFS_I(dentry->d_inode); char *link; if (befs_ino->i_flags & BEFS_LONG_SYMLINK) { struct super_block *sb = dentry->d_sb; befs_data_stream *data = &befs_ino->i_data.ds; befs_off_t len = data->size; befs_debug(sb, "Follow long symlink"); link = kmalloc(len, GFP_NOFS); if (!link) { link = ERR_PTR(-ENOMEM); } else if (befs_read_lsymlink(sb, data, link, len) != len) { kfree(link); befs_error(sb, "Failed to read entire long symlink"); link = ERR_PTR(-EIO); } } else { link = befs_ino->i_data.symlink; } nd_set_link(nd, link); return NULL; }
/* Finds the block run that starts at file block number blockno in the file represented by the datastream data, if that blockno is in the direct region of the datastream. sb: the superblock data: the datastream blockno: the blocknumber to find run: The found run is passed back through this pointer Return value is BEFS_OK if the blockrun is found, BEFS_ERR otherwise. Algorithm: Linear search. Checks each element of array[] to see if it contains the blockno-th filesystem block. This is necessary because the block runs map variable amounts of data. Simply keeps a count of the number of blocks searched so far (sum), incrementing this by the length of each block run as we come across it. Adds sum to *count before returning (this is so you can search multiple arrays that are logicaly one array, as in the indirect region code). When/if blockno is found, if blockno is inside of a block run as stored on disk, we offset the start and lenght members of the block run, so that blockno is the start and len is still valid (the run ends in the same place). 2001-11-15 Will Dyson */ static int befs_find_brun_direct(struct super_block *sb, befs_data_stream * data, befs_blocknr_t blockno, befs_block_run * run) { int i; befs_block_run *array = data->direct; befs_blocknr_t sum; befs_blocknr_t max_block = data->max_direct_range >> BEFS_SB(sb)->block_shift; befs_debug(sb, "---> befs_find_brun_direct(), find %lu", blockno); if (blockno > max_block) { befs_error(sb, "befs_find_brun_direct() passed block outside of" "direct region"); return BEFS_ERR; } for (i = 0, sum = 0; i < BEFS_NUM_DIRECT_BLOCKS; sum += array[i].len, i++) { if (blockno >= sum && blockno < sum + (array[i].len)) { int offset = blockno - sum; run->allocation_group = array[i].allocation_group; run->start = array[i].start + offset; run->len = array[i].len - offset; befs_debug(sb, "---> befs_find_brun_direct(), " "found %lu at direct[%d]", blockno, i); return BEFS_OK; } } befs_debug(sb, "---> befs_find_brun_direct() ERROR"); return BEFS_ERR; }
/** * befs_find_key - Search for a key within a node * @sb: Filesystem superblock * @node: Node to find the key within * @findkey: Keystring to search for * @value: If key is found, the value stored with the key is put here * * Finds exact match if one exists, and returns BEFS_BT_MATCH. * If there is no match and node's value array is too small for key, return * BEFS_BT_OVERFLOW. * If no match and node should countain this key, return BEFS_BT_NOT_FOUND. * * Uses binary search instead of a linear. */ static int befs_find_key(struct super_block *sb, struct befs_btree_node *node, const char *findkey, befs_off_t * value) { int first, last, mid; int eq; u16 keylen; int findkey_len; char *thiskey; fs64 *valarray; befs_debug(sb, "---> %s %s", __func__, findkey); findkey_len = strlen(findkey); /* if node can not contain key, just skip this node */ last = node->head.all_key_count - 1; thiskey = befs_bt_get_key(sb, node, last, &keylen); eq = befs_compare_strings(thiskey, keylen, findkey, findkey_len); if (eq < 0) { befs_debug(sb, "<--- node can't contain %s", findkey); return BEFS_BT_OVERFLOW; } valarray = befs_bt_valarray(node); /* simple binary search */ first = 0; mid = 0; while (last >= first) { mid = (last + first) / 2; befs_debug(sb, "first: %d, last: %d, mid: %d", first, last, mid); thiskey = befs_bt_get_key(sb, node, mid, &keylen); eq = befs_compare_strings(thiskey, keylen, findkey, findkey_len); if (eq == 0) { befs_debug(sb, "<--- %s found %s at %d", __func__, thiskey, mid); *value = fs64_to_cpu(sb, valarray[mid]); return BEFS_BT_MATCH; } if (eq > 0) last = mid - 1; else first = mid + 1; } /* return an existing value so caller can arrive to a leaf node */ if (eq < 0) *value = fs64_to_cpu(sb, valarray[mid + 1]); else *value = fs64_to_cpu(sb, valarray[mid]); befs_error(sb, "<--- %s %s not found", __func__, findkey); befs_debug(sb, "<--- %s ERROR", __func__); return BEFS_BT_NOT_FOUND; }
static int befs_bt_read_node(struct super_block *sb, befs_data_stream * ds, befs_btree_node * node, befs_off_t node_off) { uint off = 0; befs_debug(sb, "---> befs_bt_read_node()"); if (node->bh) brelse(node->bh); node->bh = befs_read_datastream(sb, ds, node_off, &off); if (!node->bh) { befs_error(sb, "befs_bt_read_node() failed to read " "node at %Lu", node_off); befs_debug(sb, "<--- befs_bt_read_node() ERROR"); return BEFS_ERR; } node->od_node = (befs_btree_nodehead *) ((void *) node->bh->b_data + off); befs_dump_index_node(sb, node->od_node); node->head.left = fs64_to_cpu(sb, node->od_node->left); node->head.right = fs64_to_cpu(sb, node->od_node->right); node->head.overflow = fs64_to_cpu(sb, node->od_node->overflow); node->head.all_key_count = fs16_to_cpu(sb, node->od_node->all_key_count); node->head.all_key_length = fs16_to_cpu(sb, node->od_node->all_key_length); befs_debug(sb, "<--- befs_btree_read_node()"); return BEFS_OK; }
static int befs_readdir(struct file *file, struct dir_context *ctx) { struct inode *inode = file_inode(file); struct super_block *sb = inode->i_sb; befs_data_stream *ds = &BEFS_I(inode)->i_data.ds; befs_off_t value; int result; size_t keysize; unsigned char d_type; char keybuf[BEFS_NAME_LEN + 1]; befs_debug(sb, "---> %s name %pD, inode %ld, ctx->pos %lld", __func__, file, inode->i_ino, ctx->pos); more: result = befs_btree_read(sb, ds, ctx->pos, BEFS_NAME_LEN + 1, keybuf, &keysize, &value); if (result == BEFS_ERR) { befs_debug(sb, "<--- %s ERROR", __func__); befs_error(sb, "IO error reading %pD (inode %lu)", file, inode->i_ino); return -EIO; } else if (result == BEFS_BT_END) { befs_debug(sb, "<--- %s END", __func__); return 0; } else if (result == BEFS_BT_EMPTY) { befs_debug(sb, "<--- %s Empty directory", __func__); return 0; } d_type = DT_UNKNOWN; /* Convert to NLS */ if (BEFS_SB(sb)->nls) { char *nlsname; int nlsnamelen; result = befs_utf2nls(sb, keybuf, keysize, &nlsname, &nlsnamelen); if (result < 0) { befs_debug(sb, "<--- %s ERROR", __func__); return result; } if (!dir_emit(ctx, nlsname, nlsnamelen, (ino_t) value, d_type)) { kfree(nlsname); return 0; } kfree(nlsname); } else { if (!dir_emit(ctx, keybuf, keysize, (ino_t) value, d_type)) return 0; } ctx->pos++; goto more; }
static int befs_readlink(struct dentry *dentry, char __user *buffer, int buflen) { struct super_block *sb = dentry->d_sb; befs_inode_info *befs_ino = BEFS_I(dentry->d_inode); char *link; int res; if (befs_ino->i_flags & BEFS_LONG_SYMLINK) { befs_data_stream *data = &befs_ino->i_data.ds; befs_off_t linklen = data->size; befs_debug(sb, "Read long symlink"); link = kmalloc(linklen, GFP_NOFS); if (link == NULL) return -ENOMEM; if (befs_read_lsymlink(sb, data, link, linklen) != linklen) { kfree(link); befs_error(sb, "Failed to read entire long symlink"); return -EIO; } res = vfs_readlink(dentry, buffer, buflen, link); kfree(link); } else { link = befs_ino->i_data.symlink; res = vfs_readlink(dentry, buffer, buflen, link); } return res; }
static int befs_get_block(struct inode *inode, sector_t block, struct buffer_head *bh_result, int create) { struct super_block *sb = inode->i_sb; befs_data_stream *ds = &BEFS_I(inode)->i_data.ds; befs_block_run run = BAD_IADDR; int res = 0; ulong disk_off; befs_debug(sb, "---> befs_get_block() for inode %lu, block %ld", inode->i_ino, block); if (block < 0) { befs_error(sb, "befs_get_block() was asked for a block " "number less than zero: block %ld in inode %lu", block, inode->i_ino); return -EIO; } if (create) { befs_error(sb, "befs_get_block() was asked to write to " "block %ld in inode %lu", block, inode->i_ino); return -EPERM; } res = befs_fblock2brun(sb, ds, block, &run); if (res != BEFS_OK) { befs_error(sb, "<--- befs_get_block() for inode %lu, block " "%ld ERROR", inode->i_ino, block); return -EFBIG; } disk_off = (ulong) iaddr2blockno(sb, &run); map_bh(bh_result, inode->i_sb, disk_off); befs_debug(sb, "<--- befs_get_block() for inode %lu, block %ld, " "disk address %lu", inode->i_ino, block, disk_off); return 0; }
/** * befs_bt_read_super - read in btree superblock convert to cpu byteorder * @sb: Filesystem superblock * @ds: Datastream to read from * @sup: Buffer in which to place the btree superblock * * Calls befs_read_datastream to read in the btree superblock and * makes sure it is in cpu byteorder, byteswapping if necessary. * * On success, returns BEFS_OK and *@sup contains the btree superblock, * in cpu byte order. * * On failure, BEFS_ERR is returned. */ static int befs_bt_read_super(struct super_block *sb, befs_data_stream * ds, befs_btree_super * sup) { struct buffer_head *bh = NULL; befs_disk_btree_super *od_sup = NULL; befs_debug(sb, "---> befs_btree_read_super()"); bh = befs_read_datastream(sb, ds, 0, NULL); if (!bh) { befs_error(sb, "Couldn't read index header."); goto error; } od_sup = (befs_disk_btree_super *) bh->b_data; befs_dump_index_entry(sb, od_sup); sup->magic = fs32_to_cpu(sb, od_sup->magic); sup->node_size = fs32_to_cpu(sb, od_sup->node_size); sup->max_depth = fs32_to_cpu(sb, od_sup->max_depth); sup->data_type = fs32_to_cpu(sb, od_sup->data_type); sup->root_node_ptr = fs64_to_cpu(sb, od_sup->root_node_ptr); sup->free_node_ptr = fs64_to_cpu(sb, od_sup->free_node_ptr); sup->max_size = fs64_to_cpu(sb, od_sup->max_size); brelse(bh); if (sup->magic != BEFS_BTREE_MAGIC) { befs_error(sb, "Index header has bad magic."); goto error; } befs_debug(sb, "<--- befs_btree_read_super()"); return BEFS_OK; error: befs_debug(sb, "<--- befs_btree_read_super() ERROR"); return BEFS_ERR; }
/* Validates the correctness of the befs inode Returns BEFS_OK if the inode should be used, otherwise returns BEFS_BAD_INODE */ int befs_check_inode(struct super_block *sb, befs_inode * raw_inode, befs_blocknr_t inode) { u32 magic1 = fs32_to_cpu(sb, raw_inode->magic1); befs_inode_addr ino_num = fsrun_to_cpu(sb, raw_inode->inode_num); u32 flags = fs32_to_cpu(sb, raw_inode->flags); /* check magic header. */ if (magic1 != BEFS_INODE_MAGIC1) { befs_error(sb, "Inode has a bad magic header - inode = %lu", (unsigned long)inode); return BEFS_BAD_INODE; } /* * Sanity check2: inodes store their own block address. Check it. */ if (inode != iaddr2blockno(sb, &ino_num)) { befs_error(sb, "inode blocknr field disagrees with vfs " "VFS: %lu, Inode %lu", (unsigned long) inode, (unsigned long)iaddr2blockno(sb, &ino_num)); return BEFS_BAD_INODE; } /* * check flag */ if (!(flags & BEFS_INODE_IN_USE)) { befs_error(sb, "inode is not used - inode = %lu", (unsigned long)inode); return BEFS_BAD_INODE; } return BEFS_OK; }
static int befs_get_block(struct inode *inode, sector_t block, struct buffer_head *bh_result, int create) { struct super_block *sb = inode->i_sb; befs_data_stream *ds = &BEFS_I(inode)->i_data.ds; befs_block_run run = BAD_IADDR; int res; ulong disk_off; befs_debug(sb, "---> befs_get_block() for inode %lu, block %ld", (unsigned long)inode->i_ino, (long)block); if (create) { befs_error(sb, "befs_get_block() was asked to write to " "block %ld in inode %lu", (long)block, (unsigned long)inode->i_ino); return -EPERM; } res = befs_fblock2brun(sb, ds, block, &run); if (res != BEFS_OK) { befs_error(sb, "<--- %s for inode %lu, block %ld ERROR", __func__, (unsigned long)inode->i_ino, (long)block); return -EFBIG; } disk_off = (ulong) iaddr2blockno(sb, &run); map_bh(bh_result, inode->i_sb, disk_off); befs_debug(sb, "<--- %s for inode %lu, block %ld, disk address %lu", __func__, (unsigned long)inode->i_ino, (long)block, (unsigned long)disk_off); return 0; }
static struct dentry * befs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) { struct inode *inode; struct super_block *sb = dir->i_sb; const befs_data_stream *ds = &BEFS_I(dir)->i_data.ds; befs_off_t offset; int ret; int utfnamelen; char *utfname; const char *name = dentry->d_name.name; befs_debug(sb, "---> %s name %pd inode %ld", __func__, dentry, dir->i_ino); /* Convert to UTF-8 */ if (BEFS_SB(sb)->nls) { ret = befs_nls2utf(sb, name, strlen(name), &utfname, &utfnamelen); if (ret < 0) { befs_debug(sb, "<--- %s ERROR", __func__); return ERR_PTR(ret); } ret = befs_btree_find(sb, ds, utfname, &offset); kfree(utfname); } else { ret = befs_btree_find(sb, ds, name, &offset); } if (ret == BEFS_BT_NOT_FOUND) { befs_debug(sb, "<--- %s %pd not found", __func__, dentry); d_add(dentry, NULL); return ERR_PTR(-ENOENT); } else if (ret != BEFS_OK || offset == 0) { befs_error(sb, "<--- %s Error", __func__); return ERR_PTR(-ENODATA); } inode = befs_iget(dir->i_sb, (ino_t) offset); if (IS_ERR(inode)) return ERR_CAST(inode); d_add(dentry, inode); befs_debug(sb, "<--- %s", __func__); return NULL; }
struct buffer_head * befs_bread_iaddr(struct super_block *sb, befs_inode_addr iaddr) { struct buffer_head *bh = NULL; befs_blocknr_t block = 0; struct befs_sb_info *befs_sb = BEFS_SB(sb); befs_debug(sb, "---> Enter %s " "[%u, %hu, %hu]", __func__, iaddr.allocation_group, iaddr.start, iaddr.len); if (iaddr.allocation_group > befs_sb->num_ags) { befs_error(sb, "BEFS: Invalid allocation group %u, max is %u", iaddr.allocation_group, befs_sb->num_ags); goto error; } block = iaddr2blockno(sb, &iaddr); befs_debug(sb, "%s: offset = %lu", __func__, (unsigned long)block); bh = sb_bread(sb, block); if (bh == NULL) { befs_error(sb, "Failed to read block %lu", (unsigned long)block); goto error; } befs_debug(sb, "<--- %s", __func__); return bh; error: befs_debug(sb, "<--- %s ERROR", __func__); return NULL; }
struct buffer_head * befs_bread(struct super_block *sb, befs_blocknr_t block) { struct buffer_head *bh = NULL; befs_debug(sb, "---> Enter befs_read() %Lu", block); bh = sb_bread(sb, block); if (bh == NULL) { befs_error(sb, "Failed to read block %lu", block); goto error; } befs_debug(sb, "<--- befs_read()"); return bh; error: befs_debug(sb, "<--- befs_read() ERROR"); return NULL; }
struct buffer_head * befs_bread(struct super_block *sb, befs_blocknr_t block) { struct buffer_head *bh = NULL; befs_debug(sb, "---> Enter %s %lu", __func__, (unsigned long)block); bh = sb_bread(sb, block); if (bh == NULL) { befs_error(sb, "Failed to read block %lu", (unsigned long)block); goto error; } befs_debug(sb, "<--- %s", __func__); return bh; error: befs_debug(sb, "<--- %s ERROR", __func__); return NULL; }
/* * Takes a file position and gives back a brun who's starting block * is block number fblock of the file. * * Returns BEFS_OK or BEFS_ERR. * * Calls specialized functions for each of the three possible * datastream regions. * * 2001-11-15 Will Dyson */ int befs_fblock2brun(struct super_block *sb, befs_data_stream * data, befs_blocknr_t fblock, befs_block_run * run) { int err; befs_off_t pos = fblock << BEFS_SB(sb)->block_shift; if (pos < data->max_direct_range) { err = befs_find_brun_direct(sb, data, fblock, run); } else if (pos < data->max_indirect_range) { err = befs_find_brun_indirect(sb, data, fblock, run); } else if (pos < data->max_double_indirect_range) { err = befs_find_brun_dblindirect(sb, data, fblock, run); } else { befs_error(sb, "befs_fblock2brun() was asked to find block %lu, " "which is not mapped by the datastream\n", fblock); err = BEFS_ERR; } return err; }
/** * befs_btree_read - Traverse leafnodes of a btree * @sb: Filesystem superblock * @ds: Datastream containing btree * @key_no: Key number (alphabetical order) of key to read * @bufsize: Size of the buffer to return key in * @keybuf: Pointer to a buffer to put the key in * @keysize: Length of the returned key * @value: Value stored with the returned key * * Heres how it works: Key_no is the index of the key/value pair to * return in keybuf/value. * Bufsize is the size of keybuf (BEFS_NAME_LEN+1 is a good size). Keysize is * the number of charecters in the key (just a convenience). * * Algorithm: * Get the first leafnode of the tree. See if the requested key is in that * node. If not, follow the node->right link to the next leafnode. Repeat * until the (key_no)th key is found or the tree is out of keys. */ int befs_btree_read(struct super_block *sb, befs_data_stream * ds, loff_t key_no, size_t bufsize, char *keybuf, size_t * keysize, befs_off_t * value) { befs_btree_node *this_node; befs_btree_super bt_super; befs_off_t node_off = 0; int cur_key; fs64 *valarray; char *keystart; u16 keylen; int res; uint key_sum = 0; befs_debug(sb, "---> befs_btree_read()"); if (befs_bt_read_super(sb, ds, &bt_super) != BEFS_OK) { befs_error(sb, "befs_btree_read() failed to read index superblock"); goto error; } if ((this_node = kmalloc(sizeof (befs_btree_node), GFP_NOFS)) == NULL) { befs_error(sb, "befs_btree_read() failed to allocate %u " "bytes of memory", sizeof (befs_btree_node)); goto error; } node_off = bt_super.root_node_ptr; this_node->bh = NULL; /* seeks down to first leafnode, reads it into this_node */ res = befs_btree_seekleaf(sb, ds, &bt_super, this_node, &node_off); if (res == BEFS_BT_EMPTY) { brelse(this_node->bh); kfree(this_node); *value = 0; *keysize = 0; befs_debug(sb, "<--- befs_btree_read() Tree is EMPTY"); return BEFS_BT_EMPTY; } else if (res == BEFS_ERR) { goto error_alloc; } /* find the leaf node containing the key_no key */ while (key_sum + this_node->head.all_key_count <= key_no) { /* no more nodes to look in: key_no is too large */ if (this_node->head.right == befs_bt_inval) { *keysize = 0; *value = 0; befs_debug(sb, "<--- befs_btree_read() END of keys at %Lu", key_sum + this_node->head.all_key_count); brelse(this_node->bh); kfree(this_node); return BEFS_BT_END; } key_sum += this_node->head.all_key_count; node_off = this_node->head.right; if (befs_bt_read_node(sb, ds, this_node, node_off) != BEFS_OK) { befs_error(sb, "befs_btree_read() failed to read " "node at %Lu", node_off); goto error_alloc; } } /* how many keys into this_node is key_no */ cur_key = key_no - key_sum; /* get pointers to datastructures within the node body */ valarray = befs_bt_valarray(this_node); keystart = befs_bt_get_key(sb, this_node, cur_key, &keylen); befs_debug(sb, "Read [%Lu,%d]: keysize %d", node_off, cur_key, keylen); if (bufsize < keylen + 1) { befs_error(sb, "befs_btree_read() keybuf too small (%u) " "for key of size %d", bufsize, keylen); brelse(this_node->bh); goto error_alloc; }; strncpy(keybuf, keystart, keylen); *value = fs64_to_cpu(sb, valarray[cur_key]); *keysize = keylen; keybuf[keylen] = '\0'; befs_debug(sb, "Read [%Lu,%d]: Key \"%.*s\", Value %Lu", node_off, cur_key, keylen, keybuf, *value); brelse(this_node->bh); kfree(this_node); befs_debug(sb, "<--- befs_btree_read()"); return BEFS_OK; error_alloc: kfree(this_node); error: *keysize = 0; *value = 0; befs_debug(sb, "<--- befs_btree_read() ERROR"); return BEFS_ERR; }
/** * befs_btree_find - Find a key in a befs B+tree * @sb: Filesystem superblock * @ds: Datastream containing btree * @key: Key string to lookup in btree * @value: Value stored with @key * * On success, returns BEFS_OK and sets *@value to the value stored * with @key (usually the disk block number of an inode). * * On failure, returns BEFS_ERR or BEFS_BT_NOT_FOUND. * * Algorithm: * Read the superblock and rootnode of the b+tree. * Drill down through the interior nodes using befs_find_key(). * Once at the correct leaf node, use befs_find_key() again to get the * actuall value stored with the key. */ int befs_btree_find(struct super_block *sb, befs_data_stream * ds, const char *key, befs_off_t * value) { befs_btree_node *this_node = NULL; befs_btree_super bt_super; befs_off_t node_off; int res; befs_debug(sb, "---> befs_btree_find() Key: %s", key); if (befs_bt_read_super(sb, ds, &bt_super) != BEFS_OK) { befs_error(sb, "befs_btree_find() failed to read index superblock"); goto error; } this_node = kmalloc(sizeof (befs_btree_node), GFP_NOFS); if (!this_node) { befs_error(sb, "befs_btree_find() failed to allocate %u " "bytes of memory", sizeof (befs_btree_node)); goto error; } this_node->bh = NULL; /* read in root node */ node_off = bt_super.root_node_ptr; if (befs_bt_read_node(sb, ds, this_node, node_off) != BEFS_OK) { befs_error(sb, "befs_btree_find() failed to read " "node at %Lu", node_off); goto error_alloc; } while (!befs_leafnode(this_node)) { res = befs_find_key(sb, this_node, key, &node_off); if (res == BEFS_BT_NOT_FOUND) node_off = this_node->head.overflow; /* if no match, go to overflow node */ if (befs_bt_read_node(sb, ds, this_node, node_off) != BEFS_OK) { befs_error(sb, "befs_btree_find() failed to read " "node at %Lu", node_off); goto error_alloc; } } /* at the correct leaf node now */ res = befs_find_key(sb, this_node, key, value); brelse(this_node->bh); kfree(this_node); if (res != BEFS_BT_MATCH) { befs_debug(sb, "<--- befs_btree_find() Key %s not found", key); *value = 0; return BEFS_BT_NOT_FOUND; } befs_debug(sb, "<--- befs_btree_find() Found key %s, value %Lu", key, *value); return BEFS_OK; error_alloc: kfree(this_node); error: *value = 0; befs_debug(sb, "<--- befs_btree_find() ERROR"); return BEFS_ERR; }
int befs_check_sb(struct super_block *sb) { struct befs_sb_info *befs_sb = BEFS_SB(sb); /* Check magic headers of super block */ if ((befs_sb->magic1 != BEFS_SUPER_MAGIC1) || (befs_sb->magic2 != BEFS_SUPER_MAGIC2) || (befs_sb->magic3 != BEFS_SUPER_MAGIC3)) { befs_error(sb, "invalid magic header"); return BEFS_ERR; } /* * Check blocksize of BEFS. * * Blocksize of BEFS is 1024, 2048, 4096 or 8192. */ if ((befs_sb->block_size != 1024) && (befs_sb->block_size != 2048) && (befs_sb->block_size != 4096) && (befs_sb->block_size != 8192)) { befs_error(sb, "invalid blocksize: %u", befs_sb->block_size); return BEFS_ERR; } if (befs_sb->block_size > PAGE_SIZE) { befs_error(sb, "blocksize(%u) cannot be larger " "than system pagesize(%lu)", befs_sb->block_size, PAGE_SIZE); return BEFS_ERR; } /* * block_shift and block_size encode the same information * in different ways as a consistency check. */ if ((1 << befs_sb->block_shift) != befs_sb->block_size) { befs_error(sb, "block_shift disagrees with block_size. " "Corruption likely."); return BEFS_ERR; } /* ag_shift also encodes the same information as blocks_per_ag in a * different way, non-fatal consistency check */ if ((1 << befs_sb->ag_shift) != befs_sb->blocks_per_ag) befs_error(sb, "ag_shift disagrees with blocks_per_ag."); if (befs_sb->log_start != befs_sb->log_end || befs_sb->flags == BEFS_DIRTY) { befs_error(sb, "Filesystem not clean! There are blocks in the " "journal. You must boot into BeOS and mount this " "volume to make it clean."); return BEFS_ERR; } return BEFS_OK; }
/* Allocate private field of the superblock, fill it. * * Finish filling the public superblock fields * Make the root directory * Load a set of NLS translations if needed. */ static int befs_fill_super(struct super_block *sb, void *data, int silent) { struct buffer_head *bh; befs_sb_info *befs_sb; befs_super_block *disk_sb; struct inode *root; long ret = -EINVAL; const unsigned long sb_block = 0; const off_t x86_sb_off = 512; save_mount_options(sb, data); sb->s_fs_info = kmalloc(sizeof (*befs_sb), GFP_KERNEL); if (sb->s_fs_info == NULL) { printk(KERN_ERR "BeFS(%s): Unable to allocate memory for private " "portion of superblock. Bailing.\n", sb->s_id); goto unacquire_none; } befs_sb = BEFS_SB(sb); memset(befs_sb, 0, sizeof(befs_sb_info)); if (!parse_options((char *) data, &befs_sb->mount_opts)) { befs_error(sb, "cannot parse mount options"); goto unacquire_priv_sbp; } befs_debug(sb, "---> befs_fill_super()"); #ifndef CONFIG_BEFS_RW if (!(sb->s_flags & MS_RDONLY)) { befs_warning(sb, "No write support. Marking filesystem read-only"); sb->s_flags |= MS_RDONLY; } #endif /* CONFIG_BEFS_RW */ /* * Set dummy blocksize to read super block. * Will be set to real fs blocksize later. * * Linux 2.4.10 and later refuse to read blocks smaller than * the hardsect size for the device. But we also need to read at * least 1k to get the second 512 bytes of the volume. * -WD 10-26-01 */ sb_min_blocksize(sb, 1024); if (!(bh = sb_bread(sb, sb_block))) { befs_error(sb, "unable to read superblock"); goto unacquire_priv_sbp; } /* account for offset of super block on x86 */ disk_sb = (befs_super_block *) bh->b_data; if ((disk_sb->magic1 == BEFS_SUPER_MAGIC1_LE) || (disk_sb->magic1 == BEFS_SUPER_MAGIC1_BE)) { befs_debug(sb, "Using PPC superblock location"); } else { befs_debug(sb, "Using x86 superblock location"); disk_sb = (befs_super_block *) ((void *) bh->b_data + x86_sb_off); } if (befs_load_sb(sb, disk_sb) != BEFS_OK) goto unacquire_bh; befs_dump_super_block(sb, disk_sb); brelse(bh); if (befs_check_sb(sb) != BEFS_OK) goto unacquire_priv_sbp; if( befs_sb->num_blocks > ~((sector_t)0) ) { befs_error(sb, "blocks count: %Lu " "is larger than the host can use", befs_sb->num_blocks); goto unacquire_priv_sbp; } /* * set up enough so that it can read an inode * Fill in kernel superblock fields from private sb */ sb->s_magic = BEFS_SUPER_MAGIC; /* Set real blocksize of fs */ sb_set_blocksize(sb, (ulong) befs_sb->block_size); sb->s_op = &befs_sops; root = befs_iget(sb, iaddr2blockno(sb, &(befs_sb->root_dir))); if (IS_ERR(root)) { ret = PTR_ERR(root); goto unacquire_priv_sbp; } sb->s_root = d_make_root(root); if (!sb->s_root) { befs_error(sb, "get root inode failed"); goto unacquire_priv_sbp; } /* load nls library */ if (befs_sb->mount_opts.iocharset) { befs_debug(sb, "Loading nls: %s", befs_sb->mount_opts.iocharset); befs_sb->nls = load_nls(befs_sb->mount_opts.iocharset); if (!befs_sb->nls) { befs_warning(sb, "Cannot load nls %s" " loading default nls", befs_sb->mount_opts.iocharset); befs_sb->nls = load_nls_default(); } /* load default nls if none is specified in mount options */ } else { befs_debug(sb, "Loading default nls"); befs_sb->nls = load_nls_default(); } return 0; /*****************/ unacquire_bh: brelse(bh); unacquire_priv_sbp: kfree(befs_sb->mount_opts.iocharset); kfree(sb->s_fs_info); unacquire_none: sb->s_fs_info = NULL; return ret; }
static struct inode *befs_iget(struct super_block *sb, unsigned long ino) { struct buffer_head *bh = NULL; befs_inode *raw_inode = NULL; befs_sb_info *befs_sb = BEFS_SB(sb); befs_inode_info *befs_ino = NULL; struct inode *inode; long ret = -EIO; befs_debug(sb, "---> befs_read_inode() " "inode = %lu", ino); inode = iget_locked(sb, ino); if (!inode) return ERR_PTR(-ENOMEM); if (!(inode->i_state & I_NEW)) return inode; befs_ino = BEFS_I(inode); /* convert from vfs's inode number to befs's inode number */ befs_ino->i_inode_num = blockno2iaddr(sb, inode->i_ino); befs_debug(sb, " real inode number [%u, %hu, %hu]", befs_ino->i_inode_num.allocation_group, befs_ino->i_inode_num.start, befs_ino->i_inode_num.len); bh = befs_bread(sb, inode->i_ino); if (!bh) { befs_error(sb, "unable to read inode block - " "inode = %lu", inode->i_ino); goto unacquire_none; } raw_inode = (befs_inode *) bh->b_data; befs_dump_inode(sb, raw_inode); if (befs_check_inode(sb, raw_inode, inode->i_ino) != BEFS_OK) { befs_error(sb, "Bad inode: %lu", inode->i_ino); goto unacquire_bh; } inode->i_mode = (umode_t) fs32_to_cpu(sb, raw_inode->mode); /* * set uid and gid. But since current BeOS is single user OS, so * you can change by "uid" or "gid" options. */ inode->i_uid = befs_sb->mount_opts.use_uid ? befs_sb->mount_opts.uid : make_kuid(&init_user_ns, fs32_to_cpu(sb, raw_inode->uid)); inode->i_gid = befs_sb->mount_opts.use_gid ? befs_sb->mount_opts.gid : make_kgid(&init_user_ns, fs32_to_cpu(sb, raw_inode->gid)); set_nlink(inode, 1); /* * BEFS's time is 64 bits, but current VFS is 32 bits... * BEFS don't have access time. Nor inode change time. VFS * doesn't have creation time. * Also, the lower 16 bits of the last_modified_time and * create_time are just a counter to help ensure uniqueness * for indexing purposes. (PFD, page 54) */ inode->i_mtime.tv_sec = fs64_to_cpu(sb, raw_inode->last_modified_time) >> 16; inode->i_mtime.tv_nsec = 0; /* lower 16 bits are not a time */ inode->i_ctime = inode->i_mtime; inode->i_atime = inode->i_mtime; befs_ino->i_inode_num = fsrun_to_cpu(sb, raw_inode->inode_num); befs_ino->i_parent = fsrun_to_cpu(sb, raw_inode->parent); befs_ino->i_attribute = fsrun_to_cpu(sb, raw_inode->attributes); befs_ino->i_flags = fs32_to_cpu(sb, raw_inode->flags); if (S_ISLNK(inode->i_mode) && !(befs_ino->i_flags & BEFS_LONG_SYMLINK)){ inode->i_size = 0; inode->i_blocks = befs_sb->block_size / VFS_BLOCK_SIZE; strncpy(befs_ino->i_data.symlink, raw_inode->data.symlink, BEFS_SYMLINK_LEN - 1); befs_ino->i_data.symlink[BEFS_SYMLINK_LEN - 1] = '\0'; } else { int num_blks; befs_ino->i_data.ds = fsds_to_cpu(sb, &raw_inode->data.datastream); num_blks = befs_count_blocks(sb, &befs_ino->i_data.ds); inode->i_blocks = num_blks * (befs_sb->block_size / VFS_BLOCK_SIZE); inode->i_size = befs_ino->i_data.ds.size; } inode->i_mapping->a_ops = &befs_aops; if (S_ISREG(inode->i_mode)) { inode->i_fop = &generic_ro_fops; } else if (S_ISDIR(inode->i_mode)) { inode->i_op = &befs_dir_inode_operations; inode->i_fop = &befs_dir_operations; } else if (S_ISLNK(inode->i_mode)) { if (befs_ino->i_flags & BEFS_LONG_SYMLINK) inode->i_op = &befs_symlink_inode_operations; else inode->i_op = &befs_fast_symlink_inode_operations; } else { befs_error(sb, "Inode %lu is not a regular file, " "directory or symlink. THAT IS WRONG! BeFS has no " "on disk special files", inode->i_ino); goto unacquire_bh; } brelse(bh); befs_debug(sb, "<--- befs_read_inode()"); unlock_new_inode(inode); return inode; unacquire_bh: brelse(bh); unacquire_none: iget_failed(inode); befs_debug(sb, "<--- befs_read_inode() - Bad inode"); return ERR_PTR(ret); }
/* Finds the block run that starts at file block number blockno in the file represented by the datastream data, if that blockno is in the indirect region of the datastream. sb: the superblock data: the datastream blockno: the blocknumber to find run: The found run is passed back through this pointer Return value is BEFS_OK if the blockrun is found, BEFS_ERR otherwise. Algorithm: For each block in the indirect run of the datastream, read it in and search through it for search_blk. XXX: Really should check to make sure blockno is inside indirect region. 2001-11-15 Will Dyson */ static int befs_find_brun_indirect(struct super_block *sb, befs_data_stream * data, befs_blocknr_t blockno, befs_block_run * run) { int i, j; befs_blocknr_t sum = 0; befs_blocknr_t indir_start_blk; befs_blocknr_t search_blk; struct buffer_head *indirblock; befs_disk_block_run *array; befs_block_run indirect = data->indirect; befs_blocknr_t indirblockno = iaddr2blockno(sb, &indirect); int arraylen = befs_iaddrs_per_block(sb); befs_debug(sb, "---> befs_find_brun_indirect(), find %lu", blockno); indir_start_blk = data->max_direct_range >> BEFS_SB(sb)->block_shift; search_blk = blockno - indir_start_blk; /* Examine blocks of the indirect run one at a time */ for (i = 0; i < indirect.len; i++) { indirblock = befs_bread(sb, indirblockno + i); if (indirblock == NULL) { befs_debug(sb, "---> befs_find_brun_indirect() failed to " "read disk block %lu from the indirect brun", indirblockno + i); return BEFS_ERR; } array = (befs_disk_block_run *) indirblock->b_data; for (j = 0; j < arraylen; ++j) { int len = fs16_to_cpu(sb, array[j].len); if (search_blk >= sum && search_blk < sum + len) { int offset = search_blk - sum; run->allocation_group = fs32_to_cpu(sb, array[j].allocation_group); run->start = fs16_to_cpu(sb, array[j].start) + offset; run->len = fs16_to_cpu(sb, array[j].len) - offset; brelse(indirblock); befs_debug(sb, "<--- befs_find_brun_indirect() found " "file block %lu at indirect[%d]", blockno, j + (i * arraylen)); return BEFS_OK; } sum += len; } brelse(indirblock); } /* Only fallthrough is an error */ befs_error(sb, "BeFS: befs_find_brun_indirect() failed to find " "file block %lu", blockno); befs_debug(sb, "<--- befs_find_brun_indirect() ERROR"); return BEFS_ERR; }
/* Finds the block run that starts at file block number blockno in the file represented by the datastream data, if that blockno is in the double-indirect region of the datastream. sb: the superblock data: the datastream blockno: the blocknumber to find run: The found run is passed back through this pointer Return value is BEFS_OK if the blockrun is found, BEFS_ERR otherwise. Algorithm: The block runs in the double-indirect region are different. They are always allocated 4 fs blocks at a time, so each block run maps a constant amount of file data. This means that we can directly calculate how many block runs into the double-indirect region we need to go to get to the one that maps a particular filesystem block. We do this in two stages. First we calculate which of the inode addresses in the double-indirect block will point us to the indirect block that contains the mapping for the data, then we calculate which of the inode addresses in that indirect block maps the data block we are after. Oh, and once we've done that, we actually read in the blocks that contain the inode addresses we calculated above. Even though the double-indirect run may be several blocks long, we can calculate which of those blocks will contain the index we are after and only read that one. We then follow it to the indirect block and perform a similar process to find the actual block run that maps the data block we are interested in. Then we offset the run as in befs_find_brun_array() and we are done. 2001-11-15 Will Dyson */ static int befs_find_brun_dblindirect(struct super_block *sb, befs_data_stream * data, befs_blocknr_t blockno, befs_block_run * run) { int dblindir_indx; int indir_indx; int offset; int dbl_which_block; int which_block; int dbl_block_indx; int block_indx; off_t dblindir_leftover; befs_blocknr_t blockno_at_run_start; struct buffer_head *dbl_indir_block; struct buffer_head *indir_block; befs_block_run indir_run; befs_disk_inode_addr *iaddr_array = NULL; befs_sb_info *befs_sb = BEFS_SB(sb); befs_blocknr_t indir_start_blk = data->max_indirect_range >> befs_sb->block_shift; off_t dbl_indir_off = blockno - indir_start_blk; /* number of data blocks mapped by each of the iaddrs in * the indirect block pointed to by the double indirect block */ size_t iblklen = BEFS_DBLINDIR_BRUN_LEN; /* number of data blocks mapped by each of the iaddrs in * the double indirect block */ size_t diblklen = iblklen * befs_iaddrs_per_block(sb) * BEFS_DBLINDIR_BRUN_LEN; befs_debug(sb, "---> befs_find_brun_dblindirect() find %lu", blockno); /* First, discover which of the double_indir->indir blocks * contains pos. Then figure out how much of pos that * accounted for. Then discover which of the iaddrs in * the indirect block contains pos. */ dblindir_indx = dbl_indir_off / diblklen; dblindir_leftover = dbl_indir_off % diblklen; indir_indx = dblindir_leftover / diblklen; /* Read double indirect block */ dbl_which_block = dblindir_indx / befs_iaddrs_per_block(sb); if (dbl_which_block > data->double_indirect.len) { befs_error(sb, "The double-indirect index calculated by " "befs_read_brun_dblindirect(), %d, is outside the range " "of the double-indirect block", dblindir_indx); return BEFS_ERR; } dbl_indir_block = befs_bread(sb, iaddr2blockno(sb, &data->double_indirect) + dbl_which_block); if (dbl_indir_block == NULL) { befs_error(sb, "befs_read_brun_dblindirect() couldn't read the " "double-indirect block at blockno %lu", iaddr2blockno(sb, &data->double_indirect) + dbl_which_block); brelse(dbl_indir_block); return BEFS_ERR; } dbl_block_indx = dblindir_indx - (dbl_which_block * befs_iaddrs_per_block(sb)); iaddr_array = (befs_disk_inode_addr *) dbl_indir_block->b_data; indir_run = fsrun_to_cpu(sb, iaddr_array[dbl_block_indx]); brelse(dbl_indir_block); iaddr_array = NULL; /* Read indirect block */ which_block = indir_indx / befs_iaddrs_per_block(sb); if (which_block > indir_run.len) { befs_error(sb, "The indirect index calculated by " "befs_read_brun_dblindirect(), %d, is outside the range " "of the indirect block", indir_indx); return BEFS_ERR; } indir_block = befs_bread(sb, iaddr2blockno(sb, &indir_run) + which_block); if (indir_block == NULL) { befs_error(sb, "befs_read_brun_dblindirect() couldn't read the " "indirect block at blockno %lu", iaddr2blockno(sb, &indir_run) + which_block); brelse(indir_block); return BEFS_ERR; } block_indx = indir_indx - (which_block * befs_iaddrs_per_block(sb)); iaddr_array = (befs_disk_inode_addr *) indir_block->b_data; *run = fsrun_to_cpu(sb, iaddr_array[block_indx]); brelse(indir_block); iaddr_array = NULL; blockno_at_run_start = indir_start_blk; blockno_at_run_start += diblklen * dblindir_indx; blockno_at_run_start += iblklen * indir_indx; offset = blockno - blockno_at_run_start; run->start += offset; run->len -= offset; befs_debug(sb, "Found file block %lu in double_indirect[%d][%d]," " double_indirect_leftover = %lu", blockno, dblindir_indx, indir_indx, dblindir_leftover); return BEFS_OK; }
static int befs_readdir(struct file *filp, void *dirent, filldir_t filldir) { struct inode *inode = filp->f_dentry->d_inode; struct super_block *sb = inode->i_sb; befs_data_stream *ds = &BEFS_I(inode)->i_data.ds; befs_off_t value; int result; size_t keysize; unsigned char d_type; char keybuf[BEFS_NAME_LEN + 1]; char *nlsname; int nlsnamelen; const char *dirname = filp->f_dentry->d_name.name; befs_debug(sb, "---> befs_readdir() " "name %s, inode %ld, filp->f_pos %Ld", dirname, inode->i_ino, filp->f_pos); result = befs_btree_read(sb, ds, filp->f_pos, BEFS_NAME_LEN + 1, keybuf, &keysize, &value); if (result == BEFS_ERR) { befs_debug(sb, "<--- befs_readdir() ERROR"); befs_error(sb, "IO error reading %s (inode %lu)", dirname, inode->i_ino); return -EIO; } else if (result == BEFS_BT_END) { befs_debug(sb, "<--- befs_readdir() END"); return 0; } else if (result == BEFS_BT_EMPTY) { befs_debug(sb, "<--- befs_readdir() Empty directory"); return 0; } d_type = DT_UNKNOWN; /* Convert to NLS */ if (BEFS_SB(sb)->nls) { result = befs_utf2nls(sb, keybuf, keysize, &nlsname, &nlsnamelen); if (result < 0) { befs_debug(sb, "<--- befs_readdir() ERROR"); return result; } result = filldir(dirent, nlsname, nlsnamelen, filp->f_pos, (ino_t) value, d_type); kfree(nlsname); } else { result = filldir(dirent, keybuf, keysize, filp->f_pos, (ino_t) value, d_type); } filp->f_pos++; befs_debug(sb, "<--- befs_readdir() filp->f_pos %Ld", filp->f_pos); return 0; }