Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * fs/kernfs/mount.c - kernfs mount implementation
0004  *
0005  * Copyright (c) 2001-3 Patrick Mochel
0006  * Copyright (c) 2007 SUSE Linux Products GmbH
0007  * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org>
0008  */
0009 
0010 #include <linux/fs.h>
0011 #include <linux/mount.h>
0012 #include <linux/init.h>
0013 #include <linux/magic.h>
0014 #include <linux/slab.h>
0015 #include <linux/pagemap.h>
0016 #include <linux/namei.h>
0017 #include <linux/seq_file.h>
0018 #include <linux/exportfs.h>
0019 
0020 #include "kernfs-internal.h"
0021 
0022 struct kmem_cache *kernfs_node_cache, *kernfs_iattrs_cache;
0023 struct kernfs_global_locks *kernfs_locks;
0024 
0025 static int kernfs_sop_show_options(struct seq_file *sf, struct dentry *dentry)
0026 {
0027     struct kernfs_root *root = kernfs_root(kernfs_dentry_node(dentry));
0028     struct kernfs_syscall_ops *scops = root->syscall_ops;
0029 
0030     if (scops && scops->show_options)
0031         return scops->show_options(sf, root);
0032     return 0;
0033 }
0034 
0035 static int kernfs_sop_show_path(struct seq_file *sf, struct dentry *dentry)
0036 {
0037     struct kernfs_node *node = kernfs_dentry_node(dentry);
0038     struct kernfs_root *root = kernfs_root(node);
0039     struct kernfs_syscall_ops *scops = root->syscall_ops;
0040 
0041     if (scops && scops->show_path)
0042         return scops->show_path(sf, node, root);
0043 
0044     seq_dentry(sf, dentry, " \t\n\\");
0045     return 0;
0046 }
0047 
0048 const struct super_operations kernfs_sops = {
0049     .statfs     = simple_statfs,
0050     .drop_inode = generic_delete_inode,
0051     .evict_inode    = kernfs_evict_inode,
0052 
0053     .show_options   = kernfs_sop_show_options,
0054     .show_path  = kernfs_sop_show_path,
0055 };
0056 
0057 static int kernfs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
0058                 struct inode *parent)
0059 {
0060     struct kernfs_node *kn = inode->i_private;
0061 
0062     if (*max_len < 2) {
0063         *max_len = 2;
0064         return FILEID_INVALID;
0065     }
0066 
0067     *max_len = 2;
0068     *(u64 *)fh = kn->id;
0069     return FILEID_KERNFS;
0070 }
0071 
0072 static struct dentry *__kernfs_fh_to_dentry(struct super_block *sb,
0073                         struct fid *fid, int fh_len,
0074                         int fh_type, bool get_parent)
0075 {
0076     struct kernfs_super_info *info = kernfs_info(sb);
0077     struct kernfs_node *kn;
0078     struct inode *inode;
0079     u64 id;
0080 
0081     if (fh_len < 2)
0082         return NULL;
0083 
0084     switch (fh_type) {
0085     case FILEID_KERNFS:
0086         id = *(u64 *)fid;
0087         break;
0088     case FILEID_INO32_GEN:
0089     case FILEID_INO32_GEN_PARENT:
0090         /*
0091          * blk_log_action() exposes "LOW32,HIGH32" pair without
0092          * type and userland can call us with generic fid
0093          * constructed from them.  Combine it back to ID.  See
0094          * blk_log_action().
0095          */
0096         id = ((u64)fid->i32.gen << 32) | fid->i32.ino;
0097         break;
0098     default:
0099         return NULL;
0100     }
0101 
0102     kn = kernfs_find_and_get_node_by_id(info->root, id);
0103     if (!kn)
0104         return ERR_PTR(-ESTALE);
0105 
0106     if (get_parent) {
0107         struct kernfs_node *parent;
0108 
0109         parent = kernfs_get_parent(kn);
0110         kernfs_put(kn);
0111         kn = parent;
0112         if (!kn)
0113             return ERR_PTR(-ESTALE);
0114     }
0115 
0116     inode = kernfs_get_inode(sb, kn);
0117     kernfs_put(kn);
0118     if (!inode)
0119         return ERR_PTR(-ESTALE);
0120 
0121     return d_obtain_alias(inode);
0122 }
0123 
0124 static struct dentry *kernfs_fh_to_dentry(struct super_block *sb,
0125                       struct fid *fid, int fh_len,
0126                       int fh_type)
0127 {
0128     return __kernfs_fh_to_dentry(sb, fid, fh_len, fh_type, false);
0129 }
0130 
0131 static struct dentry *kernfs_fh_to_parent(struct super_block *sb,
0132                       struct fid *fid, int fh_len,
0133                       int fh_type)
0134 {
0135     return __kernfs_fh_to_dentry(sb, fid, fh_len, fh_type, true);
0136 }
0137 
0138 static struct dentry *kernfs_get_parent_dentry(struct dentry *child)
0139 {
0140     struct kernfs_node *kn = kernfs_dentry_node(child);
0141 
0142     return d_obtain_alias(kernfs_get_inode(child->d_sb, kn->parent));
0143 }
0144 
0145 static const struct export_operations kernfs_export_ops = {
0146     .encode_fh  = kernfs_encode_fh,
0147     .fh_to_dentry   = kernfs_fh_to_dentry,
0148     .fh_to_parent   = kernfs_fh_to_parent,
0149     .get_parent = kernfs_get_parent_dentry,
0150 };
0151 
0152 /**
0153  * kernfs_root_from_sb - determine kernfs_root associated with a super_block
0154  * @sb: the super_block in question
0155  *
0156  * Return the kernfs_root associated with @sb.  If @sb is not a kernfs one,
0157  * %NULL is returned.
0158  */
0159 struct kernfs_root *kernfs_root_from_sb(struct super_block *sb)
0160 {
0161     if (sb->s_op == &kernfs_sops)
0162         return kernfs_info(sb)->root;
0163     return NULL;
0164 }
0165 
0166 /*
0167  * find the next ancestor in the path down to @child, where @parent was the
0168  * ancestor whose descendant we want to find.
0169  *
0170  * Say the path is /a/b/c/d.  @child is d, @parent is NULL.  We return the root
0171  * node.  If @parent is b, then we return the node for c.
0172  * Passing in d as @parent is not ok.
0173  */
0174 static struct kernfs_node *find_next_ancestor(struct kernfs_node *child,
0175                           struct kernfs_node *parent)
0176 {
0177     if (child == parent) {
0178         pr_crit_once("BUG in find_next_ancestor: called with parent == child");
0179         return NULL;
0180     }
0181 
0182     while (child->parent != parent) {
0183         if (!child->parent)
0184             return NULL;
0185         child = child->parent;
0186     }
0187 
0188     return child;
0189 }
0190 
0191 /**
0192  * kernfs_node_dentry - get a dentry for the given kernfs_node
0193  * @kn: kernfs_node for which a dentry is needed
0194  * @sb: the kernfs super_block
0195  */
0196 struct dentry *kernfs_node_dentry(struct kernfs_node *kn,
0197                   struct super_block *sb)
0198 {
0199     struct dentry *dentry;
0200     struct kernfs_node *knparent = NULL;
0201 
0202     BUG_ON(sb->s_op != &kernfs_sops);
0203 
0204     dentry = dget(sb->s_root);
0205 
0206     /* Check if this is the root kernfs_node */
0207     if (!kn->parent)
0208         return dentry;
0209 
0210     knparent = find_next_ancestor(kn, NULL);
0211     if (WARN_ON(!knparent)) {
0212         dput(dentry);
0213         return ERR_PTR(-EINVAL);
0214     }
0215 
0216     do {
0217         struct dentry *dtmp;
0218         struct kernfs_node *kntmp;
0219 
0220         if (kn == knparent)
0221             return dentry;
0222         kntmp = find_next_ancestor(kn, knparent);
0223         if (WARN_ON(!kntmp)) {
0224             dput(dentry);
0225             return ERR_PTR(-EINVAL);
0226         }
0227         dtmp = lookup_positive_unlocked(kntmp->name, dentry,
0228                            strlen(kntmp->name));
0229         dput(dentry);
0230         if (IS_ERR(dtmp))
0231             return dtmp;
0232         knparent = kntmp;
0233         dentry = dtmp;
0234     } while (true);
0235 }
0236 
0237 static int kernfs_fill_super(struct super_block *sb, struct kernfs_fs_context *kfc)
0238 {
0239     struct kernfs_super_info *info = kernfs_info(sb);
0240     struct kernfs_root *kf_root = kfc->root;
0241     struct inode *inode;
0242     struct dentry *root;
0243 
0244     info->sb = sb;
0245     /* Userspace would break if executables or devices appear on sysfs */
0246     sb->s_iflags |= SB_I_NOEXEC | SB_I_NODEV;
0247     sb->s_blocksize = PAGE_SIZE;
0248     sb->s_blocksize_bits = PAGE_SHIFT;
0249     sb->s_magic = kfc->magic;
0250     sb->s_op = &kernfs_sops;
0251     sb->s_xattr = kernfs_xattr_handlers;
0252     if (info->root->flags & KERNFS_ROOT_SUPPORT_EXPORTOP)
0253         sb->s_export_op = &kernfs_export_ops;
0254     sb->s_time_gran = 1;
0255 
0256     /* sysfs dentries and inodes don't require IO to create */
0257     sb->s_shrink.seeks = 0;
0258 
0259     /* get root inode, initialize and unlock it */
0260     down_read(&kf_root->kernfs_rwsem);
0261     inode = kernfs_get_inode(sb, info->root->kn);
0262     up_read(&kf_root->kernfs_rwsem);
0263     if (!inode) {
0264         pr_debug("kernfs: could not get root inode\n");
0265         return -ENOMEM;
0266     }
0267 
0268     /* instantiate and link root dentry */
0269     root = d_make_root(inode);
0270     if (!root) {
0271         pr_debug("%s: could not get root dentry!\n", __func__);
0272         return -ENOMEM;
0273     }
0274     sb->s_root = root;
0275     sb->s_d_op = &kernfs_dops;
0276     return 0;
0277 }
0278 
0279 static int kernfs_test_super(struct super_block *sb, struct fs_context *fc)
0280 {
0281     struct kernfs_super_info *sb_info = kernfs_info(sb);
0282     struct kernfs_super_info *info = fc->s_fs_info;
0283 
0284     return sb_info->root == info->root && sb_info->ns == info->ns;
0285 }
0286 
0287 static int kernfs_set_super(struct super_block *sb, struct fs_context *fc)
0288 {
0289     struct kernfs_fs_context *kfc = fc->fs_private;
0290 
0291     kfc->ns_tag = NULL;
0292     return set_anon_super_fc(sb, fc);
0293 }
0294 
0295 /**
0296  * kernfs_super_ns - determine the namespace tag of a kernfs super_block
0297  * @sb: super_block of interest
0298  *
0299  * Return the namespace tag associated with kernfs super_block @sb.
0300  */
0301 const void *kernfs_super_ns(struct super_block *sb)
0302 {
0303     struct kernfs_super_info *info = kernfs_info(sb);
0304 
0305     return info->ns;
0306 }
0307 
0308 /**
0309  * kernfs_get_tree - kernfs filesystem access/retrieval helper
0310  * @fc: The filesystem context.
0311  *
0312  * This is to be called from each kernfs user's fs_context->ops->get_tree()
0313  * implementation, which should set the specified ->@fs_type and ->@flags, and
0314  * specify the hierarchy and namespace tag to mount via ->@root and ->@ns,
0315  * respectively.
0316  */
0317 int kernfs_get_tree(struct fs_context *fc)
0318 {
0319     struct kernfs_fs_context *kfc = fc->fs_private;
0320     struct super_block *sb;
0321     struct kernfs_super_info *info;
0322     int error;
0323 
0324     info = kzalloc(sizeof(*info), GFP_KERNEL);
0325     if (!info)
0326         return -ENOMEM;
0327 
0328     info->root = kfc->root;
0329     info->ns = kfc->ns_tag;
0330     INIT_LIST_HEAD(&info->node);
0331 
0332     fc->s_fs_info = info;
0333     sb = sget_fc(fc, kernfs_test_super, kernfs_set_super);
0334     if (IS_ERR(sb))
0335         return PTR_ERR(sb);
0336 
0337     if (!sb->s_root) {
0338         struct kernfs_super_info *info = kernfs_info(sb);
0339         struct kernfs_root *root = kfc->root;
0340 
0341         kfc->new_sb_created = true;
0342 
0343         error = kernfs_fill_super(sb, kfc);
0344         if (error) {
0345             deactivate_locked_super(sb);
0346             return error;
0347         }
0348         sb->s_flags |= SB_ACTIVE;
0349 
0350         down_write(&root->kernfs_rwsem);
0351         list_add(&info->node, &info->root->supers);
0352         up_write(&root->kernfs_rwsem);
0353     }
0354 
0355     fc->root = dget(sb->s_root);
0356     return 0;
0357 }
0358 
0359 void kernfs_free_fs_context(struct fs_context *fc)
0360 {
0361     /* Note that we don't deal with kfc->ns_tag here. */
0362     kfree(fc->s_fs_info);
0363     fc->s_fs_info = NULL;
0364 }
0365 
0366 /**
0367  * kernfs_kill_sb - kill_sb for kernfs
0368  * @sb: super_block being killed
0369  *
0370  * This can be used directly for file_system_type->kill_sb().  If a kernfs
0371  * user needs extra cleanup, it can implement its own kill_sb() and call
0372  * this function at the end.
0373  */
0374 void kernfs_kill_sb(struct super_block *sb)
0375 {
0376     struct kernfs_super_info *info = kernfs_info(sb);
0377     struct kernfs_root *root = info->root;
0378 
0379     down_write(&root->kernfs_rwsem);
0380     list_del(&info->node);
0381     up_write(&root->kernfs_rwsem);
0382 
0383     /*
0384      * Remove the superblock from fs_supers/s_instances
0385      * so we can't find it, before freeing kernfs_super_info.
0386      */
0387     kill_anon_super(sb);
0388     kfree(info);
0389 }
0390 
0391 static void __init kernfs_mutex_init(void)
0392 {
0393     int count;
0394 
0395     for (count = 0; count < NR_KERNFS_LOCKS; count++)
0396         mutex_init(&kernfs_locks->open_file_mutex[count]);
0397 }
0398 
0399 static void __init kernfs_lock_init(void)
0400 {
0401     kernfs_locks = kmalloc(sizeof(struct kernfs_global_locks), GFP_KERNEL);
0402     WARN_ON(!kernfs_locks);
0403 
0404     kernfs_mutex_init();
0405 }
0406 
0407 void __init kernfs_init(void)
0408 {
0409     kernfs_node_cache = kmem_cache_create("kernfs_node_cache",
0410                           sizeof(struct kernfs_node),
0411                           0, SLAB_PANIC, NULL);
0412 
0413     /* Creates slab cache for kernfs inode attributes */
0414     kernfs_iattrs_cache  = kmem_cache_create("kernfs_iattrs_cache",
0415                           sizeof(struct kernfs_iattrs),
0416                           0, SLAB_PANIC, NULL);
0417 
0418     kernfs_lock_init();
0419 }