blob: f9cc912c31e1b9ea91fd837e4e430e4d59ddec83 [file] [log] [blame]
Thomas Gleixner55716d22019-06-01 10:08:42 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Tejun Heoae6621b2013-11-28 14:54:31 -05002/*
3 * fs/kernfs/kernfs-internal.h - kernfs internal header file
4 *
5 * Copyright (c) 2001-3 Patrick Mochel
6 * Copyright (c) 2007 SUSE Linux Products GmbH
7 * Copyright (c) 2007, 2013 Tejun Heo <teheo@suse.de>
Tejun Heoae6621b2013-11-28 14:54:31 -05008 */
9
10#ifndef __KERNFS_INTERNAL_H
11#define __KERNFS_INTERNAL_H
12
13#include <linux/lockdep.h>
14#include <linux/fs.h>
Tejun Heofd7b9f72013-11-28 14:54:33 -050015#include <linux/mutex.h>
Ian Kent7ba02732021-07-16 17:28:29 +080016#include <linux/rwsem.h>
Tejun Heo23223922013-11-23 17:40:02 -050017#include <linux/xattr.h>
Tejun Heoae6621b2013-11-28 14:54:31 -050018
19#include <linux/kernfs.h>
David Howells23bf1b62018-11-01 23:07:26 +000020#include <linux/fs_context.h>
Tejun Heoae6621b2013-11-28 14:54:31 -050021
Tejun Heoc525aad2013-12-11 14:11:55 -050022struct kernfs_iattrs {
Ondrej Mosnacek05895212019-02-22 15:57:12 +010023 kuid_t ia_uid;
24 kgid_t ia_gid;
25 struct timespec64 ia_atime;
26 struct timespec64 ia_mtime;
27 struct timespec64 ia_ctime;
Tejun Heo23223922013-11-23 17:40:02 -050028
29 struct simple_xattrs xattrs;
Daniel Xu0c473832020-03-12 13:03:16 -070030 atomic_t nr_user_xattrs;
31 atomic_t user_xattr_size;
Tejun Heoae6621b2013-11-28 14:54:31 -050032};
33
Tejun Heo81c173c2014-02-03 14:03:00 -050034/* +1 to avoid triggering overflow warning when negating it */
35#define KN_DEACTIVATED_BIAS (INT_MIN + 1)
Tejun Heoae6621b2013-11-28 14:54:31 -050036
Tejun Heodf23fc32013-12-11 14:11:56 -050037/* KERNFS_TYPE_MASK and types are defined in include/linux/kernfs.h */
Tejun Heoae6621b2013-11-28 14:54:31 -050038
Tejun Heoba7443b2013-11-28 14:54:40 -050039/**
Tejun Heo324a56e2013-12-11 14:11:53 -050040 * kernfs_root - find out the kernfs_root a kernfs_node belongs to
41 * @kn: kernfs_node of interest
Tejun Heoba7443b2013-11-28 14:54:40 -050042 *
Tejun Heo324a56e2013-12-11 14:11:53 -050043 * Return the kernfs_root @kn belongs to.
Tejun Heoba7443b2013-11-28 14:54:40 -050044 */
Tejun Heo324a56e2013-12-11 14:11:53 -050045static inline struct kernfs_root *kernfs_root(struct kernfs_node *kn)
Tejun Heoba7443b2013-11-28 14:54:40 -050046{
47 /* if parent exists, it's always a dir; otherwise, @sd is a dir */
Tejun Heoadc5e8b2013-12-11 14:11:54 -050048 if (kn->parent)
49 kn = kn->parent;
50 return kn->dir.root;
Tejun Heoba7443b2013-11-28 14:54:40 -050051}
52
Tejun Heoae6621b2013-11-28 14:54:31 -050053/*
Tejun Heofa736a92013-11-28 14:54:44 -050054 * mount.c
55 */
Tejun Heoc525aad2013-12-11 14:11:55 -050056struct kernfs_super_info {
Tejun Heo7d568a82014-04-09 11:07:30 -040057 struct super_block *sb;
58
Tejun Heofa736a92013-11-28 14:54:44 -050059 /*
60 * The root associated with this super_block. Each super_block is
61 * identified by the root and ns it's associated with.
62 */
63 struct kernfs_root *root;
64
65 /*
Tejun Heo324a56e2013-12-11 14:11:53 -050066 * Each sb is associated with one namespace tag, currently the
Tejun Heoc525aad2013-12-11 14:11:55 -050067 * network namespace of the task which mounted this kernfs
68 * instance. If multiple tags become necessary, make the following
69 * an array and compare kernfs_node tag against every entry.
Tejun Heofa736a92013-11-28 14:54:44 -050070 */
71 const void *ns;
Tejun Heo7d568a82014-04-09 11:07:30 -040072
Ian Kent7ba02732021-07-16 17:28:29 +080073 /* anchored at kernfs_root->supers, protected by kernfs_rwsem */
Tejun Heo7d568a82014-04-09 11:07:30 -040074 struct list_head node;
Tejun Heofa736a92013-11-28 14:54:44 -050075};
Tejun Heoc525aad2013-12-11 14:11:55 -050076#define kernfs_info(SB) ((struct kernfs_super_info *)(SB->s_fs_info))
Tejun Heofa736a92013-11-28 14:54:44 -050077
Shaohua Li319ba912017-07-12 11:49:49 -070078static inline struct kernfs_node *kernfs_dentry_node(struct dentry *dentry)
79{
80 if (d_really_is_negative(dentry))
81 return NULL;
82 return d_inode(dentry)->i_private;
83}
84
Ian Kent895adbe2021-07-16 17:28:18 +080085static inline void kernfs_set_rev(struct kernfs_node *parent,
86 struct dentry *dentry)
87{
88 dentry->d_time = parent->dir.rev;
89}
90
91static inline void kernfs_inc_rev(struct kernfs_node *parent)
92{
93 parent->dir.rev++;
94}
95
96static inline bool kernfs_dir_changed(struct kernfs_node *parent,
97 struct dentry *dentry)
98{
99 if (parent->dir.rev != dentry->d_time)
100 return true;
101 return false;
102}
103
Li Zefanf41c5932014-02-14 16:57:27 +0800104extern const struct super_operations kernfs_sops;
Ayush Mittal26e28d62019-02-06 10:25:42 +0530105extern struct kmem_cache *kernfs_node_cache, *kernfs_iattrs_cache;
Tejun Heofa736a92013-11-28 14:54:44 -0500106
107/*
Tejun Heoffed24e2013-11-28 14:54:32 -0500108 * inode.c
109 */
Andreas Gruenbachere72a1a82016-09-29 17:48:33 +0200110extern const struct xattr_handler *kernfs_xattr_handlers[];
Tejun Heoc637b8a2013-12-11 14:11:58 -0500111void kernfs_evict_inode(struct inode *inode);
Christian Brauner549c7292021-01-21 14:19:43 +0100112int kernfs_iop_permission(struct user_namespace *mnt_userns,
113 struct inode *inode, int mask);
114int kernfs_iop_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
115 struct iattr *iattr);
116int kernfs_iop_getattr(struct user_namespace *mnt_userns,
117 const struct path *path, struct kstat *stat,
David Howellsa528d352017-01-31 16:46:22 +0000118 u32 request_mask, unsigned int query_flags);
Tejun Heoc637b8a2013-12-11 14:11:58 -0500119ssize_t kernfs_iop_listxattr(struct dentry *dentry, char *buf, size_t size);
Dmitry Torokhov488dee92018-07-20 21:56:47 +0000120int __kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr);
Tejun Heoffed24e2013-11-28 14:54:32 -0500121
Tejun Heofd7b9f72013-11-28 14:54:33 -0500122/*
123 * dir.c
124 */
Ian Kent7ba02732021-07-16 17:28:29 +0800125extern struct rw_semaphore kernfs_rwsem;
Tejun Heoa797bfc2013-12-11 14:11:57 -0500126extern const struct dentry_operations kernfs_dops;
127extern const struct file_operations kernfs_dir_fops;
128extern const struct inode_operations kernfs_dir_iops;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500129
Tejun Heoc637b8a2013-12-11 14:11:58 -0500130struct kernfs_node *kernfs_get_active(struct kernfs_node *kn);
131void kernfs_put_active(struct kernfs_node *kn);
Tejun Heo988cd7a2014-02-03 14:02:58 -0500132int kernfs_add_one(struct kernfs_node *kn);
Tejun Heodb4aad22014-01-17 09:58:25 -0500133struct kernfs_node *kernfs_new_node(struct kernfs_node *parent,
134 const char *name, umode_t mode,
Dmitry Torokhov488dee92018-07-20 21:56:47 +0000135 kuid_t uid, kgid_t gid,
Tejun Heodb4aad22014-01-17 09:58:25 -0500136 unsigned flags);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500137
Tejun Heo414985a2013-11-28 14:54:34 -0500138/*
139 * file.c
140 */
Tejun Heoa797bfc2013-12-11 14:11:57 -0500141extern const struct file_operations kernfs_file_fops;
Tejun Heo414985a2013-11-28 14:54:34 -0500142
Tejun Heo0e67db22016-12-27 14:49:03 -0500143void kernfs_drain_open_files(struct kernfs_node *kn);
Tejun Heo414985a2013-11-28 14:54:34 -0500144
Tejun Heo2072f1a2013-11-28 14:54:35 -0500145/*
146 * symlink.c
147 */
Tejun Heoa797bfc2013-12-11 14:11:57 -0500148extern const struct inode_operations kernfs_symlink_iops;
Tejun Heo2072f1a2013-11-28 14:54:35 -0500149
Tejun Heoae6621b2013-11-28 14:54:31 -0500150#endif /* __KERNFS_INTERNAL_H */