^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * fs/kernfs/mount.c - kernfs mount implementation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2001-3 Patrick Mochel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (c) 2007 SUSE Linux Products GmbH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/mount.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/magic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/pagemap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/namei.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/exportfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include "kernfs-internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct kmem_cache *kernfs_node_cache, *kernfs_iattrs_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) static int kernfs_sop_show_options(struct seq_file *sf, struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct kernfs_root *root = kernfs_root(kernfs_dentry_node(dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct kernfs_syscall_ops *scops = root->syscall_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) if (scops && scops->show_options)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) return scops->show_options(sf, root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static int kernfs_sop_show_path(struct seq_file *sf, struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct kernfs_node *node = kernfs_dentry_node(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct kernfs_root *root = kernfs_root(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct kernfs_syscall_ops *scops = root->syscall_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) if (scops && scops->show_path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) return scops->show_path(sf, node, root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) seq_dentry(sf, dentry, " \t\n\\");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) const struct super_operations kernfs_sops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) .statfs = simple_statfs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) .drop_inode = generic_delete_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) .evict_inode = kernfs_evict_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) .show_options = kernfs_sop_show_options,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) .show_path = kernfs_sop_show_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static int kernfs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) struct inode *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) struct kernfs_node *kn = inode->i_private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (*max_len < 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) *max_len = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) return FILEID_INVALID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) *max_len = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) *(u64 *)fh = kn->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) return FILEID_KERNFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static struct dentry *__kernfs_fh_to_dentry(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) struct fid *fid, int fh_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) int fh_type, bool get_parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) struct kernfs_super_info *info = kernfs_info(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) struct kernfs_node *kn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) u64 id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) if (fh_len < 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) switch (fh_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) case FILEID_KERNFS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) id = *(u64 *)fid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) case FILEID_INO32_GEN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) case FILEID_INO32_GEN_PARENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * blk_log_action() exposes "LOW32,HIGH32" pair without
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * type and userland can call us with generic fid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) * constructed from them. Combine it back to ID. See
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * blk_log_action().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) id = ((u64)fid->i32.gen << 32) | fid->i32.ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) kn = kernfs_find_and_get_node_by_id(info->root, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (!kn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return ERR_PTR(-ESTALE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) if (get_parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) struct kernfs_node *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) parent = kernfs_get_parent(kn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) kernfs_put(kn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) kn = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (!kn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) return ERR_PTR(-ESTALE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) inode = kernfs_get_inode(sb, kn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) kernfs_put(kn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (!inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) return ERR_PTR(-ESTALE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) return d_obtain_alias(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) static struct dentry *kernfs_fh_to_dentry(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) struct fid *fid, int fh_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) int fh_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) return __kernfs_fh_to_dentry(sb, fid, fh_len, fh_type, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) static struct dentry *kernfs_fh_to_parent(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) struct fid *fid, int fh_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) int fh_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) return __kernfs_fh_to_dentry(sb, fid, fh_len, fh_type, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) static struct dentry *kernfs_get_parent_dentry(struct dentry *child)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) struct kernfs_node *kn = kernfs_dentry_node(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return d_obtain_alias(kernfs_get_inode(child->d_sb, kn->parent));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) static const struct export_operations kernfs_export_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) .encode_fh = kernfs_encode_fh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) .fh_to_dentry = kernfs_fh_to_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) .fh_to_parent = kernfs_fh_to_parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) .get_parent = kernfs_get_parent_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) * kernfs_root_from_sb - determine kernfs_root associated with a super_block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) * @sb: the super_block in question
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) * Return the kernfs_root associated with @sb. If @sb is not a kernfs one,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) * %NULL is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) struct kernfs_root *kernfs_root_from_sb(struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (sb->s_op == &kernfs_sops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return kernfs_info(sb)->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) * find the next ancestor in the path down to @child, where @parent was the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) * ancestor whose descendant we want to find.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) * Say the path is /a/b/c/d. @child is d, @parent is NULL. We return the root
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) * node. If @parent is b, then we return the node for c.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) * Passing in d as @parent is not ok.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) static struct kernfs_node *find_next_ancestor(struct kernfs_node *child,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) struct kernfs_node *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) if (child == parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) pr_crit_once("BUG in find_next_ancestor: called with parent == child");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) while (child->parent != parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if (!child->parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) child = child->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) return child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) * kernfs_node_dentry - get a dentry for the given kernfs_node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) * @kn: kernfs_node for which a dentry is needed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) * @sb: the kernfs super_block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) struct dentry *kernfs_node_dentry(struct kernfs_node *kn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) struct dentry *dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) struct kernfs_node *knparent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) BUG_ON(sb->s_op != &kernfs_sops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) dentry = dget(sb->s_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) /* Check if this is the root kernfs_node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) if (!kn->parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) return dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) knparent = find_next_ancestor(kn, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) if (WARN_ON(!knparent)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) dput(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) struct dentry *dtmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) struct kernfs_node *kntmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) if (kn == knparent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) return dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) kntmp = find_next_ancestor(kn, knparent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) if (WARN_ON(!kntmp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) dput(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) dtmp = lookup_positive_unlocked(kntmp->name, dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) strlen(kntmp->name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) dput(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) if (IS_ERR(dtmp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) return dtmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) knparent = kntmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) dentry = dtmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) } while (true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) static int kernfs_fill_super(struct super_block *sb, struct kernfs_fs_context *kfc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) struct kernfs_super_info *info = kernfs_info(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) struct dentry *root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) info->sb = sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) /* Userspace would break if executables or devices appear on sysfs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) sb->s_iflags |= SB_I_NOEXEC | SB_I_NODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) sb->s_blocksize = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) sb->s_blocksize_bits = PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) sb->s_magic = kfc->magic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) sb->s_op = &kernfs_sops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) sb->s_xattr = kernfs_xattr_handlers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) if (info->root->flags & KERNFS_ROOT_SUPPORT_EXPORTOP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) sb->s_export_op = &kernfs_export_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) sb->s_time_gran = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) /* sysfs dentries and inodes don't require IO to create */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) sb->s_shrink.seeks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) /* get root inode, initialize and unlock it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) mutex_lock(&kernfs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) inode = kernfs_get_inode(sb, info->root->kn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) mutex_unlock(&kernfs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) if (!inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) pr_debug("kernfs: could not get root inode\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) /* instantiate and link root dentry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) root = d_make_root(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) if (!root) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) pr_debug("%s: could not get root dentry!\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) sb->s_root = root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) sb->s_d_op = &kernfs_dops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) static int kernfs_test_super(struct super_block *sb, struct fs_context *fc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) struct kernfs_super_info *sb_info = kernfs_info(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) struct kernfs_super_info *info = fc->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) return sb_info->root == info->root && sb_info->ns == info->ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) static int kernfs_set_super(struct super_block *sb, struct fs_context *fc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) struct kernfs_fs_context *kfc = fc->fs_private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) kfc->ns_tag = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) return set_anon_super_fc(sb, fc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) * kernfs_super_ns - determine the namespace tag of a kernfs super_block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) * @sb: super_block of interest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) * Return the namespace tag associated with kernfs super_block @sb.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) const void *kernfs_super_ns(struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) struct kernfs_super_info *info = kernfs_info(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) return info->ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) * kernfs_get_tree - kernfs filesystem access/retrieval helper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) * @fc: The filesystem context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) * This is to be called from each kernfs user's fs_context->ops->get_tree()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) * implementation, which should set the specified ->@fs_type and ->@flags, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) * specify the hierarchy and namespace tag to mount via ->@root and ->@ns,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) * respectively.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) int kernfs_get_tree(struct fs_context *fc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) struct kernfs_fs_context *kfc = fc->fs_private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) struct super_block *sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) struct kernfs_super_info *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) info = kzalloc(sizeof(*info), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) if (!info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) info->root = kfc->root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) info->ns = kfc->ns_tag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) INIT_LIST_HEAD(&info->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) fc->s_fs_info = info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) sb = sget_fc(fc, kernfs_test_super, kernfs_set_super);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) if (IS_ERR(sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) return PTR_ERR(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) if (!sb->s_root) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) struct kernfs_super_info *info = kernfs_info(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) kfc->new_sb_created = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) error = kernfs_fill_super(sb, kfc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) deactivate_locked_super(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) sb->s_flags |= SB_ACTIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) mutex_lock(&kernfs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) list_add(&info->node, &info->root->supers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) mutex_unlock(&kernfs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) fc->root = dget(sb->s_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) void kernfs_free_fs_context(struct fs_context *fc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) /* Note that we don't deal with kfc->ns_tag here. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) kfree(fc->s_fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) fc->s_fs_info = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) * kernfs_kill_sb - kill_sb for kernfs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) * @sb: super_block being killed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) * This can be used directly for file_system_type->kill_sb(). If a kernfs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) * user needs extra cleanup, it can implement its own kill_sb() and call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) * this function at the end.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) void kernfs_kill_sb(struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) struct kernfs_super_info *info = kernfs_info(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) mutex_lock(&kernfs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) list_del(&info->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) mutex_unlock(&kernfs_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) * Remove the superblock from fs_supers/s_instances
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) * so we can't find it, before freeing kernfs_super_info.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) kill_anon_super(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) kfree(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) void __init kernfs_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) kernfs_node_cache = kmem_cache_create("kernfs_node_cache",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) sizeof(struct kernfs_node),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 0, SLAB_PANIC, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) /* Creates slab cache for kernfs inode attributes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) kernfs_iattrs_cache = kmem_cache_create("kernfs_iattrs_cache",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) sizeof(struct kernfs_iattrs),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 0, SLAB_PANIC, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) }