Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * linux/fs/befs/linuxvfs.c
0004  *
0005  * Copyright (C) 2001 Will Dyson <will_dyson@pobox.com
0006  *
0007  */
0008 
0009 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0010 
0011 #include <linux/module.h>
0012 #include <linux/slab.h>
0013 #include <linux/fs.h>
0014 #include <linux/errno.h>
0015 #include <linux/stat.h>
0016 #include <linux/nls.h>
0017 #include <linux/buffer_head.h>
0018 #include <linux/vfs.h>
0019 #include <linux/parser.h>
0020 #include <linux/namei.h>
0021 #include <linux/sched.h>
0022 #include <linux/cred.h>
0023 #include <linux/exportfs.h>
0024 #include <linux/seq_file.h>
0025 #include <linux/blkdev.h>
0026 
0027 #include "befs.h"
0028 #include "btree.h"
0029 #include "inode.h"
0030 #include "datastream.h"
0031 #include "super.h"
0032 #include "io.h"
0033 
0034 MODULE_DESCRIPTION("BeOS File System (BeFS) driver");
0035 MODULE_AUTHOR("Will Dyson");
0036 MODULE_LICENSE("GPL");
0037 
0038 /* The units the vfs expects inode->i_blocks to be in */
0039 #define VFS_BLOCK_SIZE 512
0040 
0041 static int befs_readdir(struct file *, struct dir_context *);
0042 static int befs_get_block(struct inode *, sector_t, struct buffer_head *, int);
0043 static int befs_read_folio(struct file *file, struct folio *folio);
0044 static sector_t befs_bmap(struct address_space *mapping, sector_t block);
0045 static struct dentry *befs_lookup(struct inode *, struct dentry *,
0046                   unsigned int);
0047 static struct inode *befs_iget(struct super_block *, unsigned long);
0048 static struct inode *befs_alloc_inode(struct super_block *sb);
0049 static void befs_free_inode(struct inode *inode);
0050 static void befs_destroy_inodecache(void);
0051 static int befs_symlink_read_folio(struct file *, struct folio *);
0052 static int befs_utf2nls(struct super_block *sb, const char *in, int in_len,
0053             char **out, int *out_len);
0054 static int befs_nls2utf(struct super_block *sb, const char *in, int in_len,
0055             char **out, int *out_len);
0056 static void befs_put_super(struct super_block *);
0057 static int befs_remount(struct super_block *, int *, char *);
0058 static int befs_statfs(struct dentry *, struct kstatfs *);
0059 static int befs_show_options(struct seq_file *, struct dentry *);
0060 static int parse_options(char *, struct befs_mount_options *);
0061 static struct dentry *befs_fh_to_dentry(struct super_block *sb,
0062                 struct fid *fid, int fh_len, int fh_type);
0063 static struct dentry *befs_fh_to_parent(struct super_block *sb,
0064                 struct fid *fid, int fh_len, int fh_type);
0065 static struct dentry *befs_get_parent(struct dentry *child);
0066 
0067 static const struct super_operations befs_sops = {
0068     .alloc_inode    = befs_alloc_inode, /* allocate a new inode */
0069     .free_inode = befs_free_inode, /* deallocate an inode */
0070     .put_super  = befs_put_super,   /* uninit super */
0071     .statfs     = befs_statfs,  /* statfs */
0072     .remount_fs = befs_remount,
0073     .show_options   = befs_show_options,
0074 };
0075 
0076 /* slab cache for befs_inode_info objects */
0077 static struct kmem_cache *befs_inode_cachep;
0078 
0079 static const struct file_operations befs_dir_operations = {
0080     .read       = generic_read_dir,
0081     .iterate_shared = befs_readdir,
0082     .llseek     = generic_file_llseek,
0083 };
0084 
0085 static const struct inode_operations befs_dir_inode_operations = {
0086     .lookup     = befs_lookup,
0087 };
0088 
0089 static const struct address_space_operations befs_aops = {
0090     .read_folio = befs_read_folio,
0091     .bmap       = befs_bmap,
0092 };
0093 
0094 static const struct address_space_operations befs_symlink_aops = {
0095     .read_folio = befs_symlink_read_folio,
0096 };
0097 
0098 static const struct export_operations befs_export_operations = {
0099     .fh_to_dentry   = befs_fh_to_dentry,
0100     .fh_to_parent   = befs_fh_to_parent,
0101     .get_parent = befs_get_parent,
0102 };
0103 
0104 /*
0105  * Called by generic_file_read() to read a folio of data
0106  *
0107  * In turn, simply calls a generic block read function and
0108  * passes it the address of befs_get_block, for mapping file
0109  * positions to disk blocks.
0110  */
0111 static int befs_read_folio(struct file *file, struct folio *folio)
0112 {
0113     return block_read_full_folio(folio, befs_get_block);
0114 }
0115 
0116 static sector_t
0117 befs_bmap(struct address_space *mapping, sector_t block)
0118 {
0119     return generic_block_bmap(mapping, block, befs_get_block);
0120 }
0121 
0122 /*
0123  * Generic function to map a file position (block) to a
0124  * disk offset (passed back in bh_result).
0125  *
0126  * Used by many higher level functions.
0127  *
0128  * Calls befs_fblock2brun() in datastream.c to do the real work.
0129  */
0130 
0131 static int
0132 befs_get_block(struct inode *inode, sector_t block,
0133            struct buffer_head *bh_result, int create)
0134 {
0135     struct super_block *sb = inode->i_sb;
0136     befs_data_stream *ds = &BEFS_I(inode)->i_data.ds;
0137     befs_block_run run = BAD_IADDR;
0138     int res;
0139     ulong disk_off;
0140 
0141     befs_debug(sb, "---> befs_get_block() for inode %lu, block %ld",
0142            (unsigned long)inode->i_ino, (long)block);
0143     if (create) {
0144         befs_error(sb, "befs_get_block() was asked to write to "
0145                "block %ld in inode %lu", (long)block,
0146                (unsigned long)inode->i_ino);
0147         return -EPERM;
0148     }
0149 
0150     res = befs_fblock2brun(sb, ds, block, &run);
0151     if (res != BEFS_OK) {
0152         befs_error(sb,
0153                "<--- %s for inode %lu, block %ld ERROR",
0154                __func__, (unsigned long)inode->i_ino,
0155                (long)block);
0156         return -EFBIG;
0157     }
0158 
0159     disk_off = (ulong) iaddr2blockno(sb, &run);
0160 
0161     map_bh(bh_result, inode->i_sb, disk_off);
0162 
0163     befs_debug(sb, "<--- %s for inode %lu, block %ld, disk address %lu",
0164           __func__, (unsigned long)inode->i_ino, (long)block,
0165           (unsigned long)disk_off);
0166 
0167     return 0;
0168 }
0169 
0170 static struct dentry *
0171 befs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
0172 {
0173     struct inode *inode;
0174     struct super_block *sb = dir->i_sb;
0175     const befs_data_stream *ds = &BEFS_I(dir)->i_data.ds;
0176     befs_off_t offset;
0177     int ret;
0178     int utfnamelen;
0179     char *utfname;
0180     const char *name = dentry->d_name.name;
0181 
0182     befs_debug(sb, "---> %s name %pd inode %ld", __func__,
0183            dentry, dir->i_ino);
0184 
0185     /* Convert to UTF-8 */
0186     if (BEFS_SB(sb)->nls) {
0187         ret =
0188             befs_nls2utf(sb, name, strlen(name), &utfname, &utfnamelen);
0189         if (ret < 0) {
0190             befs_debug(sb, "<--- %s ERROR", __func__);
0191             return ERR_PTR(ret);
0192         }
0193         ret = befs_btree_find(sb, ds, utfname, &offset);
0194         kfree(utfname);
0195 
0196     } else {
0197         ret = befs_btree_find(sb, ds, name, &offset);
0198     }
0199 
0200     if (ret == BEFS_BT_NOT_FOUND) {
0201         befs_debug(sb, "<--- %s %pd not found", __func__, dentry);
0202         inode = NULL;
0203     } else if (ret != BEFS_OK || offset == 0) {
0204         befs_error(sb, "<--- %s Error", __func__);
0205         inode = ERR_PTR(-ENODATA);
0206     } else {
0207         inode = befs_iget(dir->i_sb, (ino_t) offset);
0208     }
0209     befs_debug(sb, "<--- %s", __func__);
0210 
0211     return d_splice_alias(inode, dentry);
0212 }
0213 
0214 static int
0215 befs_readdir(struct file *file, struct dir_context *ctx)
0216 {
0217     struct inode *inode = file_inode(file);
0218     struct super_block *sb = inode->i_sb;
0219     const befs_data_stream *ds = &BEFS_I(inode)->i_data.ds;
0220     befs_off_t value;
0221     int result;
0222     size_t keysize;
0223     char keybuf[BEFS_NAME_LEN + 1];
0224 
0225     befs_debug(sb, "---> %s name %pD, inode %ld, ctx->pos %lld",
0226           __func__, file, inode->i_ino, ctx->pos);
0227 
0228     while (1) {
0229         result = befs_btree_read(sb, ds, ctx->pos, BEFS_NAME_LEN + 1,
0230                      keybuf, &keysize, &value);
0231 
0232         if (result == BEFS_ERR) {
0233             befs_debug(sb, "<--- %s ERROR", __func__);
0234             befs_error(sb, "IO error reading %pD (inode %lu)",
0235                    file, inode->i_ino);
0236             return -EIO;
0237 
0238         } else if (result == BEFS_BT_END) {
0239             befs_debug(sb, "<--- %s END", __func__);
0240             return 0;
0241 
0242         } else if (result == BEFS_BT_EMPTY) {
0243             befs_debug(sb, "<--- %s Empty directory", __func__);
0244             return 0;
0245         }
0246 
0247         /* Convert to NLS */
0248         if (BEFS_SB(sb)->nls) {
0249             char *nlsname;
0250             int nlsnamelen;
0251 
0252             result =
0253                 befs_utf2nls(sb, keybuf, keysize, &nlsname,
0254                      &nlsnamelen);
0255             if (result < 0) {
0256                 befs_debug(sb, "<--- %s ERROR", __func__);
0257                 return result;
0258             }
0259             if (!dir_emit(ctx, nlsname, nlsnamelen,
0260                       (ino_t) value, DT_UNKNOWN)) {
0261                 kfree(nlsname);
0262                 return 0;
0263             }
0264             kfree(nlsname);
0265         } else {
0266             if (!dir_emit(ctx, keybuf, keysize,
0267                       (ino_t) value, DT_UNKNOWN))
0268                 return 0;
0269         }
0270         ctx->pos++;
0271     }
0272 }
0273 
0274 static struct inode *
0275 befs_alloc_inode(struct super_block *sb)
0276 {
0277     struct befs_inode_info *bi;
0278 
0279     bi = alloc_inode_sb(sb, befs_inode_cachep, GFP_KERNEL);
0280     if (!bi)
0281         return NULL;
0282     return &bi->vfs_inode;
0283 }
0284 
0285 static void befs_free_inode(struct inode *inode)
0286 {
0287     kmem_cache_free(befs_inode_cachep, BEFS_I(inode));
0288 }
0289 
0290 static void init_once(void *foo)
0291 {
0292     struct befs_inode_info *bi = (struct befs_inode_info *) foo;
0293 
0294     inode_init_once(&bi->vfs_inode);
0295 }
0296 
0297 static struct inode *befs_iget(struct super_block *sb, unsigned long ino)
0298 {
0299     struct buffer_head *bh;
0300     befs_inode *raw_inode;
0301     struct befs_sb_info *befs_sb = BEFS_SB(sb);
0302     struct befs_inode_info *befs_ino;
0303     struct inode *inode;
0304 
0305     befs_debug(sb, "---> %s inode = %lu", __func__, ino);
0306 
0307     inode = iget_locked(sb, ino);
0308     if (!inode)
0309         return ERR_PTR(-ENOMEM);
0310     if (!(inode->i_state & I_NEW))
0311         return inode;
0312 
0313     befs_ino = BEFS_I(inode);
0314 
0315     /* convert from vfs's inode number to befs's inode number */
0316     befs_ino->i_inode_num = blockno2iaddr(sb, inode->i_ino);
0317 
0318     befs_debug(sb, "  real inode number [%u, %hu, %hu]",
0319            befs_ino->i_inode_num.allocation_group,
0320            befs_ino->i_inode_num.start, befs_ino->i_inode_num.len);
0321 
0322     bh = sb_bread(sb, inode->i_ino);
0323     if (!bh) {
0324         befs_error(sb, "unable to read inode block - "
0325                "inode = %lu", inode->i_ino);
0326         goto unacquire_none;
0327     }
0328 
0329     raw_inode = (befs_inode *) bh->b_data;
0330 
0331     befs_dump_inode(sb, raw_inode);
0332 
0333     if (befs_check_inode(sb, raw_inode, inode->i_ino) != BEFS_OK) {
0334         befs_error(sb, "Bad inode: %lu", inode->i_ino);
0335         goto unacquire_bh;
0336     }
0337 
0338     inode->i_mode = (umode_t) fs32_to_cpu(sb, raw_inode->mode);
0339 
0340     /*
0341      * set uid and gid.  But since current BeOS is single user OS, so
0342      * you can change by "uid" or "gid" options.
0343      */
0344 
0345     inode->i_uid = befs_sb->mount_opts.use_uid ?
0346         befs_sb->mount_opts.uid :
0347         make_kuid(&init_user_ns, fs32_to_cpu(sb, raw_inode->uid));
0348     inode->i_gid = befs_sb->mount_opts.use_gid ?
0349         befs_sb->mount_opts.gid :
0350         make_kgid(&init_user_ns, fs32_to_cpu(sb, raw_inode->gid));
0351 
0352     set_nlink(inode, 1);
0353 
0354     /*
0355      * BEFS's time is 64 bits, but current VFS is 32 bits...
0356      * BEFS don't have access time. Nor inode change time. VFS
0357      * doesn't have creation time.
0358      * Also, the lower 16 bits of the last_modified_time and
0359      * create_time are just a counter to help ensure uniqueness
0360      * for indexing purposes. (PFD, page 54)
0361      */
0362 
0363     inode->i_mtime.tv_sec =
0364         fs64_to_cpu(sb, raw_inode->last_modified_time) >> 16;
0365     inode->i_mtime.tv_nsec = 0;   /* lower 16 bits are not a time */
0366     inode->i_ctime = inode->i_mtime;
0367     inode->i_atime = inode->i_mtime;
0368 
0369     befs_ino->i_inode_num = fsrun_to_cpu(sb, raw_inode->inode_num);
0370     befs_ino->i_parent = fsrun_to_cpu(sb, raw_inode->parent);
0371     befs_ino->i_attribute = fsrun_to_cpu(sb, raw_inode->attributes);
0372     befs_ino->i_flags = fs32_to_cpu(sb, raw_inode->flags);
0373 
0374     if (S_ISLNK(inode->i_mode) && !(befs_ino->i_flags & BEFS_LONG_SYMLINK)){
0375         inode->i_size = 0;
0376         inode->i_blocks = befs_sb->block_size / VFS_BLOCK_SIZE;
0377         strlcpy(befs_ino->i_data.symlink, raw_inode->data.symlink,
0378             BEFS_SYMLINK_LEN);
0379     } else {
0380         int num_blks;
0381 
0382         befs_ino->i_data.ds =
0383             fsds_to_cpu(sb, &raw_inode->data.datastream);
0384 
0385         num_blks = befs_count_blocks(sb, &befs_ino->i_data.ds);
0386         inode->i_blocks =
0387             num_blks * (befs_sb->block_size / VFS_BLOCK_SIZE);
0388         inode->i_size = befs_ino->i_data.ds.size;
0389     }
0390 
0391     inode->i_mapping->a_ops = &befs_aops;
0392 
0393     if (S_ISREG(inode->i_mode)) {
0394         inode->i_fop = &generic_ro_fops;
0395     } else if (S_ISDIR(inode->i_mode)) {
0396         inode->i_op = &befs_dir_inode_operations;
0397         inode->i_fop = &befs_dir_operations;
0398     } else if (S_ISLNK(inode->i_mode)) {
0399         if (befs_ino->i_flags & BEFS_LONG_SYMLINK) {
0400             inode->i_op = &page_symlink_inode_operations;
0401             inode_nohighmem(inode);
0402             inode->i_mapping->a_ops = &befs_symlink_aops;
0403         } else {
0404             inode->i_link = befs_ino->i_data.symlink;
0405             inode->i_op = &simple_symlink_inode_operations;
0406         }
0407     } else {
0408         befs_error(sb, "Inode %lu is not a regular file, "
0409                "directory or symlink. THAT IS WRONG! BeFS has no "
0410                "on disk special files", inode->i_ino);
0411         goto unacquire_bh;
0412     }
0413 
0414     brelse(bh);
0415     befs_debug(sb, "<--- %s", __func__);
0416     unlock_new_inode(inode);
0417     return inode;
0418 
0419 unacquire_bh:
0420     brelse(bh);
0421 
0422 unacquire_none:
0423     iget_failed(inode);
0424     befs_debug(sb, "<--- %s - Bad inode", __func__);
0425     return ERR_PTR(-EIO);
0426 }
0427 
0428 /* Initialize the inode cache. Called at fs setup.
0429  *
0430  * Taken from NFS implementation by Al Viro.
0431  */
0432 static int __init
0433 befs_init_inodecache(void)
0434 {
0435     befs_inode_cachep = kmem_cache_create_usercopy("befs_inode_cache",
0436                 sizeof(struct befs_inode_info), 0,
0437                 (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD|
0438                     SLAB_ACCOUNT),
0439                 offsetof(struct befs_inode_info,
0440                     i_data.symlink),
0441                 sizeof_field(struct befs_inode_info,
0442                     i_data.symlink),
0443                 init_once);
0444     if (befs_inode_cachep == NULL)
0445         return -ENOMEM;
0446 
0447     return 0;
0448 }
0449 
0450 /* Called at fs teardown.
0451  *
0452  * Taken from NFS implementation by Al Viro.
0453  */
0454 static void
0455 befs_destroy_inodecache(void)
0456 {
0457     /*
0458      * Make sure all delayed rcu free inodes are flushed before we
0459      * destroy cache.
0460      */
0461     rcu_barrier();
0462     kmem_cache_destroy(befs_inode_cachep);
0463 }
0464 
0465 /*
0466  * The inode of symbolic link is different to data stream.
0467  * The data stream become link name. Unless the LONG_SYMLINK
0468  * flag is set.
0469  */
0470 static int befs_symlink_read_folio(struct file *unused, struct folio *folio)
0471 {
0472     struct inode *inode = folio->mapping->host;
0473     struct super_block *sb = inode->i_sb;
0474     struct befs_inode_info *befs_ino = BEFS_I(inode);
0475     befs_data_stream *data = &befs_ino->i_data.ds;
0476     befs_off_t len = data->size;
0477     char *link = folio_address(folio);
0478 
0479     if (len == 0 || len > PAGE_SIZE) {
0480         befs_error(sb, "Long symlink with illegal length");
0481         goto fail;
0482     }
0483     befs_debug(sb, "Follow long symlink");
0484 
0485     if (befs_read_lsymlink(sb, data, link, len) != len) {
0486         befs_error(sb, "Failed to read entire long symlink");
0487         goto fail;
0488     }
0489     link[len - 1] = '\0';
0490     folio_mark_uptodate(folio);
0491     folio_unlock(folio);
0492     return 0;
0493 fail:
0494     folio_set_error(folio);
0495     folio_unlock(folio);
0496     return -EIO;
0497 }
0498 
0499 /*
0500  * UTF-8 to NLS charset convert routine
0501  *
0502  * Uses uni2char() / char2uni() rather than the nls tables directly
0503  */
0504 static int
0505 befs_utf2nls(struct super_block *sb, const char *in,
0506          int in_len, char **out, int *out_len)
0507 {
0508     struct nls_table *nls = BEFS_SB(sb)->nls;
0509     int i, o;
0510     unicode_t uni;
0511     int unilen, utflen;
0512     char *result;
0513     /* The utf8->nls conversion won't make the final nls string bigger
0514      * than the utf one, but if the string is pure ascii they'll have the
0515      * same width and an extra char is needed to save the additional \0
0516      */
0517     int maxlen = in_len + 1;
0518 
0519     befs_debug(sb, "---> %s", __func__);
0520 
0521     if (!nls) {
0522         befs_error(sb, "%s called with no NLS table loaded", __func__);
0523         return -EINVAL;
0524     }
0525 
0526     *out = result = kmalloc(maxlen, GFP_NOFS);
0527     if (!*out)
0528         return -ENOMEM;
0529 
0530     for (i = o = 0; i < in_len; i += utflen, o += unilen) {
0531 
0532         /* convert from UTF-8 to Unicode */
0533         utflen = utf8_to_utf32(&in[i], in_len - i, &uni);
0534         if (utflen < 0)
0535             goto conv_err;
0536 
0537         /* convert from Unicode to nls */
0538         if (uni > MAX_WCHAR_T)
0539             goto conv_err;
0540         unilen = nls->uni2char(uni, &result[o], in_len - o);
0541         if (unilen < 0)
0542             goto conv_err;
0543     }
0544     result[o] = '\0';
0545     *out_len = o;
0546 
0547     befs_debug(sb, "<--- %s", __func__);
0548 
0549     return o;
0550 
0551 conv_err:
0552     befs_error(sb, "Name using character set %s contains a character that "
0553            "cannot be converted to unicode.", nls->charset);
0554     befs_debug(sb, "<--- %s", __func__);
0555     kfree(result);
0556     return -EILSEQ;
0557 }
0558 
0559 /**
0560  * befs_nls2utf - Convert NLS string to utf8 encodeing
0561  * @sb: Superblock
0562  * @in: Input string buffer in NLS format
0563  * @in_len: Length of input string in bytes
0564  * @out: The output string in UTF-8 format
0565  * @out_len: Length of the output buffer
0566  *
0567  * Converts input string @in, which is in the format of the loaded NLS map,
0568  * into a utf8 string.
0569  *
0570  * The destination string @out is allocated by this function and the caller is
0571  * responsible for freeing it with kfree()
0572  *
0573  * On return, *@out_len is the length of @out in bytes.
0574  *
0575  * On success, the return value is the number of utf8 characters written to
0576  * the output buffer @out.
0577  *
0578  * On Failure, a negative number coresponding to the error code is returned.
0579  */
0580 
0581 static int
0582 befs_nls2utf(struct super_block *sb, const char *in,
0583          int in_len, char **out, int *out_len)
0584 {
0585     struct nls_table *nls = BEFS_SB(sb)->nls;
0586     int i, o;
0587     wchar_t uni;
0588     int unilen, utflen;
0589     char *result;
0590     /*
0591      * There are nls characters that will translate to 3-chars-wide UTF-8
0592      * characters, an additional byte is needed to save the final \0
0593      * in special cases
0594      */
0595     int maxlen = (3 * in_len) + 1;
0596 
0597     befs_debug(sb, "---> %s\n", __func__);
0598 
0599     if (!nls) {
0600         befs_error(sb, "%s called with no NLS table loaded.",
0601                __func__);
0602         return -EINVAL;
0603     }
0604 
0605     *out = result = kmalloc(maxlen, GFP_NOFS);
0606     if (!*out) {
0607         *out_len = 0;
0608         return -ENOMEM;
0609     }
0610 
0611     for (i = o = 0; i < in_len; i += unilen, o += utflen) {
0612 
0613         /* convert from nls to unicode */
0614         unilen = nls->char2uni(&in[i], in_len - i, &uni);
0615         if (unilen < 0)
0616             goto conv_err;
0617 
0618         /* convert from unicode to UTF-8 */
0619         utflen = utf32_to_utf8(uni, &result[o], 3);
0620         if (utflen <= 0)
0621             goto conv_err;
0622     }
0623 
0624     result[o] = '\0';
0625     *out_len = o;
0626 
0627     befs_debug(sb, "<--- %s", __func__);
0628 
0629     return i;
0630 
0631 conv_err:
0632     befs_error(sb, "Name using character set %s contains a character that "
0633            "cannot be converted to unicode.", nls->charset);
0634     befs_debug(sb, "<--- %s", __func__);
0635     kfree(result);
0636     return -EILSEQ;
0637 }
0638 
0639 static struct inode *befs_nfs_get_inode(struct super_block *sb, uint64_t ino,
0640                      uint32_t generation)
0641 {
0642     /* No need to handle i_generation */
0643     return befs_iget(sb, ino);
0644 }
0645 
0646 /*
0647  * Map a NFS file handle to a corresponding dentry
0648  */
0649 static struct dentry *befs_fh_to_dentry(struct super_block *sb,
0650                 struct fid *fid, int fh_len, int fh_type)
0651 {
0652     return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
0653                     befs_nfs_get_inode);
0654 }
0655 
0656 /*
0657  * Find the parent for a file specified by NFS handle
0658  */
0659 static struct dentry *befs_fh_to_parent(struct super_block *sb,
0660                 struct fid *fid, int fh_len, int fh_type)
0661 {
0662     return generic_fh_to_parent(sb, fid, fh_len, fh_type,
0663                     befs_nfs_get_inode);
0664 }
0665 
0666 static struct dentry *befs_get_parent(struct dentry *child)
0667 {
0668     struct inode *parent;
0669     struct befs_inode_info *befs_ino = BEFS_I(d_inode(child));
0670 
0671     parent = befs_iget(child->d_sb,
0672                (unsigned long)befs_ino->i_parent.start);
0673     if (IS_ERR(parent))
0674         return ERR_CAST(parent);
0675 
0676     return d_obtain_alias(parent);
0677 }
0678 
0679 enum {
0680     Opt_uid, Opt_gid, Opt_charset, Opt_debug, Opt_err,
0681 };
0682 
0683 static const match_table_t befs_tokens = {
0684     {Opt_uid, "uid=%d"},
0685     {Opt_gid, "gid=%d"},
0686     {Opt_charset, "iocharset=%s"},
0687     {Opt_debug, "debug"},
0688     {Opt_err, NULL}
0689 };
0690 
0691 static int
0692 parse_options(char *options, struct befs_mount_options *opts)
0693 {
0694     char *p;
0695     substring_t args[MAX_OPT_ARGS];
0696     int option;
0697     kuid_t uid;
0698     kgid_t gid;
0699 
0700     /* Initialize options */
0701     opts->uid = GLOBAL_ROOT_UID;
0702     opts->gid = GLOBAL_ROOT_GID;
0703     opts->use_uid = 0;
0704     opts->use_gid = 0;
0705     opts->iocharset = NULL;
0706     opts->debug = 0;
0707 
0708     if (!options)
0709         return 1;
0710 
0711     while ((p = strsep(&options, ",")) != NULL) {
0712         int token;
0713 
0714         if (!*p)
0715             continue;
0716 
0717         token = match_token(p, befs_tokens, args);
0718         switch (token) {
0719         case Opt_uid:
0720             if (match_int(&args[0], &option))
0721                 return 0;
0722             uid = INVALID_UID;
0723             if (option >= 0)
0724                 uid = make_kuid(current_user_ns(), option);
0725             if (!uid_valid(uid)) {
0726                 pr_err("Invalid uid %d, "
0727                        "using default\n", option);
0728                 break;
0729             }
0730             opts->uid = uid;
0731             opts->use_uid = 1;
0732             break;
0733         case Opt_gid:
0734             if (match_int(&args[0], &option))
0735                 return 0;
0736             gid = INVALID_GID;
0737             if (option >= 0)
0738                 gid = make_kgid(current_user_ns(), option);
0739             if (!gid_valid(gid)) {
0740                 pr_err("Invalid gid %d, "
0741                        "using default\n", option);
0742                 break;
0743             }
0744             opts->gid = gid;
0745             opts->use_gid = 1;
0746             break;
0747         case Opt_charset:
0748             kfree(opts->iocharset);
0749             opts->iocharset = match_strdup(&args[0]);
0750             if (!opts->iocharset) {
0751                 pr_err("allocation failure for "
0752                        "iocharset string\n");
0753                 return 0;
0754             }
0755             break;
0756         case Opt_debug:
0757             opts->debug = 1;
0758             break;
0759         default:
0760             pr_err("Unrecognized mount option \"%s\" "
0761                    "or missing value\n", p);
0762             return 0;
0763         }
0764     }
0765     return 1;
0766 }
0767 
0768 static int befs_show_options(struct seq_file *m, struct dentry *root)
0769 {
0770     struct befs_sb_info *befs_sb = BEFS_SB(root->d_sb);
0771     struct befs_mount_options *opts = &befs_sb->mount_opts;
0772 
0773     if (!uid_eq(opts->uid, GLOBAL_ROOT_UID))
0774         seq_printf(m, ",uid=%u",
0775                from_kuid_munged(&init_user_ns, opts->uid));
0776     if (!gid_eq(opts->gid, GLOBAL_ROOT_GID))
0777         seq_printf(m, ",gid=%u",
0778                from_kgid_munged(&init_user_ns, opts->gid));
0779     if (opts->iocharset)
0780         seq_printf(m, ",charset=%s", opts->iocharset);
0781     if (opts->debug)
0782         seq_puts(m, ",debug");
0783     return 0;
0784 }
0785 
0786 /* This function has the responsibiltiy of getting the
0787  * filesystem ready for unmounting.
0788  * Basically, we free everything that we allocated in
0789  * befs_read_inode
0790  */
0791 static void
0792 befs_put_super(struct super_block *sb)
0793 {
0794     kfree(BEFS_SB(sb)->mount_opts.iocharset);
0795     BEFS_SB(sb)->mount_opts.iocharset = NULL;
0796     unload_nls(BEFS_SB(sb)->nls);
0797     kfree(sb->s_fs_info);
0798     sb->s_fs_info = NULL;
0799 }
0800 
0801 /* Allocate private field of the superblock, fill it.
0802  *
0803  * Finish filling the public superblock fields
0804  * Make the root directory
0805  * Load a set of NLS translations if needed.
0806  */
0807 static int
0808 befs_fill_super(struct super_block *sb, void *data, int silent)
0809 {
0810     struct buffer_head *bh;
0811     struct befs_sb_info *befs_sb;
0812     befs_super_block *disk_sb;
0813     struct inode *root;
0814     long ret = -EINVAL;
0815     const unsigned long sb_block = 0;
0816     const off_t x86_sb_off = 512;
0817     int blocksize;
0818 
0819     sb->s_fs_info = kzalloc(sizeof(*befs_sb), GFP_KERNEL);
0820     if (sb->s_fs_info == NULL)
0821         goto unacquire_none;
0822 
0823     befs_sb = BEFS_SB(sb);
0824 
0825     if (!parse_options((char *) data, &befs_sb->mount_opts)) {
0826         if (!silent)
0827             befs_error(sb, "cannot parse mount options");
0828         goto unacquire_priv_sbp;
0829     }
0830 
0831     befs_debug(sb, "---> %s", __func__);
0832 
0833     if (!sb_rdonly(sb)) {
0834         befs_warning(sb,
0835                  "No write support. Marking filesystem read-only");
0836         sb->s_flags |= SB_RDONLY;
0837     }
0838 
0839     /*
0840      * Set dummy blocksize to read super block.
0841      * Will be set to real fs blocksize later.
0842      *
0843      * Linux 2.4.10 and later refuse to read blocks smaller than
0844      * the logical block size for the device. But we also need to read at
0845      * least 1k to get the second 512 bytes of the volume.
0846      */
0847     blocksize = sb_min_blocksize(sb, 1024);
0848     if (!blocksize) {
0849         if (!silent)
0850             befs_error(sb, "unable to set blocksize");
0851         goto unacquire_priv_sbp;
0852     }
0853 
0854     bh = sb_bread(sb, sb_block);
0855     if (!bh) {
0856         if (!silent)
0857             befs_error(sb, "unable to read superblock");
0858         goto unacquire_priv_sbp;
0859     }
0860 
0861     /* account for offset of super block on x86 */
0862     disk_sb = (befs_super_block *) bh->b_data;
0863     if ((disk_sb->magic1 == BEFS_SUPER_MAGIC1_LE) ||
0864         (disk_sb->magic1 == BEFS_SUPER_MAGIC1_BE)) {
0865         befs_debug(sb, "Using PPC superblock location");
0866     } else {
0867         befs_debug(sb, "Using x86 superblock location");
0868         disk_sb =
0869             (befs_super_block *) ((void *) bh->b_data + x86_sb_off);
0870     }
0871 
0872     if ((befs_load_sb(sb, disk_sb) != BEFS_OK) ||
0873         (befs_check_sb(sb) != BEFS_OK))
0874         goto unacquire_bh;
0875 
0876     befs_dump_super_block(sb, disk_sb);
0877 
0878     brelse(bh);
0879 
0880     if (befs_sb->num_blocks > ~((sector_t)0)) {
0881         if (!silent)
0882             befs_error(sb, "blocks count: %llu is larger than the host can use",
0883                     befs_sb->num_blocks);
0884         goto unacquire_priv_sbp;
0885     }
0886 
0887     /*
0888      * set up enough so that it can read an inode
0889      * Fill in kernel superblock fields from private sb
0890      */
0891     sb->s_magic = BEFS_SUPER_MAGIC;
0892     /* Set real blocksize of fs */
0893     sb_set_blocksize(sb, (ulong) befs_sb->block_size);
0894     sb->s_op = &befs_sops;
0895     sb->s_export_op = &befs_export_operations;
0896     sb->s_time_min = 0;
0897     sb->s_time_max = 0xffffffffffffll;
0898     root = befs_iget(sb, iaddr2blockno(sb, &(befs_sb->root_dir)));
0899     if (IS_ERR(root)) {
0900         ret = PTR_ERR(root);
0901         goto unacquire_priv_sbp;
0902     }
0903     sb->s_root = d_make_root(root);
0904     if (!sb->s_root) {
0905         if (!silent)
0906             befs_error(sb, "get root inode failed");
0907         goto unacquire_priv_sbp;
0908     }
0909 
0910     /* load nls library */
0911     if (befs_sb->mount_opts.iocharset) {
0912         befs_debug(sb, "Loading nls: %s",
0913                befs_sb->mount_opts.iocharset);
0914         befs_sb->nls = load_nls(befs_sb->mount_opts.iocharset);
0915         if (!befs_sb->nls) {
0916             befs_warning(sb, "Cannot load nls %s"
0917                     " loading default nls",
0918                     befs_sb->mount_opts.iocharset);
0919             befs_sb->nls = load_nls_default();
0920         }
0921     /* load default nls if none is specified  in mount options */
0922     } else {
0923         befs_debug(sb, "Loading default nls");
0924         befs_sb->nls = load_nls_default();
0925     }
0926 
0927     return 0;
0928 
0929 unacquire_bh:
0930     brelse(bh);
0931 
0932 unacquire_priv_sbp:
0933     kfree(befs_sb->mount_opts.iocharset);
0934     kfree(sb->s_fs_info);
0935     sb->s_fs_info = NULL;
0936 
0937 unacquire_none:
0938     return ret;
0939 }
0940 
0941 static int
0942 befs_remount(struct super_block *sb, int *flags, char *data)
0943 {
0944     sync_filesystem(sb);
0945     if (!(*flags & SB_RDONLY))
0946         return -EINVAL;
0947     return 0;
0948 }
0949 
0950 static int
0951 befs_statfs(struct dentry *dentry, struct kstatfs *buf)
0952 {
0953     struct super_block *sb = dentry->d_sb;
0954     u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
0955 
0956     befs_debug(sb, "---> %s", __func__);
0957 
0958     buf->f_type = BEFS_SUPER_MAGIC;
0959     buf->f_bsize = sb->s_blocksize;
0960     buf->f_blocks = BEFS_SB(sb)->num_blocks;
0961     buf->f_bfree = BEFS_SB(sb)->num_blocks - BEFS_SB(sb)->used_blocks;
0962     buf->f_bavail = buf->f_bfree;
0963     buf->f_files = 0;   /* UNKNOWN */
0964     buf->f_ffree = 0;   /* UNKNOWN */
0965     buf->f_fsid = u64_to_fsid(id);
0966     buf->f_namelen = BEFS_NAME_LEN;
0967 
0968     befs_debug(sb, "<--- %s", __func__);
0969 
0970     return 0;
0971 }
0972 
0973 static struct dentry *
0974 befs_mount(struct file_system_type *fs_type, int flags, const char *dev_name,
0975         void *data)
0976 {
0977     return mount_bdev(fs_type, flags, dev_name, data, befs_fill_super);
0978 }
0979 
0980 static struct file_system_type befs_fs_type = {
0981     .owner      = THIS_MODULE,
0982     .name       = "befs",
0983     .mount      = befs_mount,
0984     .kill_sb    = kill_block_super,
0985     .fs_flags   = FS_REQUIRES_DEV,
0986 };
0987 MODULE_ALIAS_FS("befs");
0988 
0989 static int __init
0990 init_befs_fs(void)
0991 {
0992     int err;
0993 
0994     pr_info("version: %s\n", BEFS_VERSION);
0995 
0996     err = befs_init_inodecache();
0997     if (err)
0998         goto unacquire_none;
0999 
1000     err = register_filesystem(&befs_fs_type);
1001     if (err)
1002         goto unacquire_inodecache;
1003 
1004     return 0;
1005 
1006 unacquire_inodecache:
1007     befs_destroy_inodecache();
1008 
1009 unacquire_none:
1010     return err;
1011 }
1012 
1013 static void __exit
1014 exit_befs_fs(void)
1015 {
1016     befs_destroy_inodecache();
1017 
1018     unregister_filesystem(&befs_fs_type);
1019 }
1020 
1021 /*
1022  * Macros that typecheck the init and exit functions,
1023  * ensures that they are called at init and cleanup,
1024  * and eliminates warnings about unused functions.
1025  */
1026 module_init(init_befs_fs)
1027 module_exit(exit_befs_fs)