Thomas Gleixner | 55716d2 | 2019-06-01 10:08:42 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Tejun Heo | b8441ed | 2013-11-24 09:54:58 -0500 | [diff] [blame] | 2 | /* |
| 3 | * fs/kernfs/inode.c - kernfs inode implementation |
| 4 | * |
| 5 | * Copyright (c) 2001-3 Patrick Mochel |
| 6 | * Copyright (c) 2007 SUSE Linux Products GmbH |
| 7 | * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org> |
Tejun Heo | b8441ed | 2013-11-24 09:54:58 -0500 | [diff] [blame] | 8 | */ |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 9 | |
| 10 | #include <linux/pagemap.h> |
| 11 | #include <linux/backing-dev.h> |
| 12 | #include <linux/capability.h> |
| 13 | #include <linux/errno.h> |
| 14 | #include <linux/slab.h> |
| 15 | #include <linux/xattr.h> |
| 16 | #include <linux/security.h> |
| 17 | |
| 18 | #include "kernfs-internal.h" |
| 19 | |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 20 | static const struct inode_operations kernfs_iops = { |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 21 | .permission = kernfs_iop_permission, |
| 22 | .setattr = kernfs_iop_setattr, |
| 23 | .getattr = kernfs_iop_getattr, |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 24 | .listxattr = kernfs_iop_listxattr, |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 25 | }; |
| 26 | |
Ondrej Mosnacek | d0c9c15 | 2019-02-22 15:57:13 +0100 | [diff] [blame] | 27 | static struct kernfs_iattrs *__kernfs_iattrs(struct kernfs_node *kn, int alloc) |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 28 | { |
Tejun Heo | 4afddd6 | 2014-04-02 16:40:52 -0400 | [diff] [blame] | 29 | static DEFINE_MUTEX(iattr_mutex); |
| 30 | struct kernfs_iattrs *ret; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 31 | |
Tejun Heo | 4afddd6 | 2014-04-02 16:40:52 -0400 | [diff] [blame] | 32 | mutex_lock(&iattr_mutex); |
| 33 | |
Ondrej Mosnacek | d0c9c15 | 2019-02-22 15:57:13 +0100 | [diff] [blame] | 34 | if (kn->iattr || !alloc) |
Tejun Heo | 4afddd6 | 2014-04-02 16:40:52 -0400 | [diff] [blame] | 35 | goto out_unlock; |
Tejun Heo | 9a8049a | 2013-11-23 17:40:01 -0500 | [diff] [blame] | 36 | |
Ayush Mittal | 26e28d6 | 2019-02-06 10:25:42 +0530 | [diff] [blame] | 37 | kn->iattr = kmem_cache_zalloc(kernfs_iattrs_cache, GFP_KERNEL); |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 38 | if (!kn->iattr) |
Tejun Heo | 4afddd6 | 2014-04-02 16:40:52 -0400 | [diff] [blame] | 39 | goto out_unlock; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 40 | |
| 41 | /* assign default attributes */ |
Ondrej Mosnacek | 0589521 | 2019-02-22 15:57:12 +0100 | [diff] [blame] | 42 | kn->iattr->ia_uid = GLOBAL_ROOT_UID; |
| 43 | kn->iattr->ia_gid = GLOBAL_ROOT_GID; |
Deepa Dinamani | 3a3a5fe | 2016-02-22 07:17:53 -0800 | [diff] [blame] | 44 | |
Ondrej Mosnacek | 0589521 | 2019-02-22 15:57:12 +0100 | [diff] [blame] | 45 | ktime_get_real_ts64(&kn->iattr->ia_atime); |
| 46 | kn->iattr->ia_mtime = kn->iattr->ia_atime; |
| 47 | kn->iattr->ia_ctime = kn->iattr->ia_atime; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 48 | |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 49 | simple_xattrs_init(&kn->iattr->xattrs); |
Daniel Xu | 0c47383 | 2020-03-12 13:03:16 -0700 | [diff] [blame] | 50 | atomic_set(&kn->iattr->nr_user_xattrs, 0); |
| 51 | atomic_set(&kn->iattr->user_xattr_size, 0); |
Tejun Heo | 4afddd6 | 2014-04-02 16:40:52 -0400 | [diff] [blame] | 52 | out_unlock: |
| 53 | ret = kn->iattr; |
| 54 | mutex_unlock(&iattr_mutex); |
| 55 | return ret; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 56 | } |
| 57 | |
Ondrej Mosnacek | d0c9c15 | 2019-02-22 15:57:13 +0100 | [diff] [blame] | 58 | static struct kernfs_iattrs *kernfs_iattrs(struct kernfs_node *kn) |
| 59 | { |
| 60 | return __kernfs_iattrs(kn, 1); |
| 61 | } |
| 62 | |
| 63 | static struct kernfs_iattrs *kernfs_iattrs_noalloc(struct kernfs_node *kn) |
| 64 | { |
| 65 | return __kernfs_iattrs(kn, 0); |
| 66 | } |
| 67 | |
Dmitry Torokhov | 488dee9 | 2018-07-20 21:56:47 +0000 | [diff] [blame] | 68 | int __kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr) |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 69 | { |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 70 | struct kernfs_iattrs *attrs; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 71 | unsigned int ia_valid = iattr->ia_valid; |
| 72 | |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 73 | attrs = kernfs_iattrs(kn); |
Tejun Heo | 9a8049a | 2013-11-23 17:40:01 -0500 | [diff] [blame] | 74 | if (!attrs) |
| 75 | return -ENOMEM; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 76 | |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 77 | if (ia_valid & ATTR_UID) |
Ondrej Mosnacek | 0589521 | 2019-02-22 15:57:12 +0100 | [diff] [blame] | 78 | attrs->ia_uid = iattr->ia_uid; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 79 | if (ia_valid & ATTR_GID) |
Ondrej Mosnacek | 0589521 | 2019-02-22 15:57:12 +0100 | [diff] [blame] | 80 | attrs->ia_gid = iattr->ia_gid; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 81 | if (ia_valid & ATTR_ATIME) |
Ondrej Mosnacek | 0589521 | 2019-02-22 15:57:12 +0100 | [diff] [blame] | 82 | attrs->ia_atime = iattr->ia_atime; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 83 | if (ia_valid & ATTR_MTIME) |
Ondrej Mosnacek | 0589521 | 2019-02-22 15:57:12 +0100 | [diff] [blame] | 84 | attrs->ia_mtime = iattr->ia_mtime; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 85 | if (ia_valid & ATTR_CTIME) |
Ondrej Mosnacek | 0589521 | 2019-02-22 15:57:12 +0100 | [diff] [blame] | 86 | attrs->ia_ctime = iattr->ia_ctime; |
| 87 | if (ia_valid & ATTR_MODE) |
| 88 | kn->mode = iattr->ia_mode; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * kernfs_setattr - set iattr on a node |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 94 | * @kn: target node |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 95 | * @iattr: iattr to set |
| 96 | * |
| 97 | * Returns 0 on success, -errno on failure. |
| 98 | */ |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 99 | int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr) |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 100 | { |
| 101 | int ret; |
Minchan Kim | 393c371 | 2021-11-18 15:00:08 -0800 | [diff] [blame] | 102 | struct kernfs_root *root = kernfs_root(kn); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 103 | |
Minchan Kim | 393c371 | 2021-11-18 15:00:08 -0800 | [diff] [blame] | 104 | down_write(&root->kernfs_rwsem); |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 105 | ret = __kernfs_setattr(kn, iattr); |
Minchan Kim | 393c371 | 2021-11-18 15:00:08 -0800 | [diff] [blame] | 106 | up_write(&root->kernfs_rwsem); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 107 | return ret; |
| 108 | } |
| 109 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 110 | int kernfs_iop_setattr(struct user_namespace *mnt_userns, struct dentry *dentry, |
| 111 | struct iattr *iattr) |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 112 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 113 | struct inode *inode = d_inode(dentry); |
Shaohua Li | 319ba91 | 2017-07-12 11:49:49 -0700 | [diff] [blame] | 114 | struct kernfs_node *kn = inode->i_private; |
Minchan Kim | 393c371 | 2021-11-18 15:00:08 -0800 | [diff] [blame] | 115 | struct kernfs_root *root; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 116 | int error; |
| 117 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 118 | if (!kn) |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 119 | return -EINVAL; |
| 120 | |
Minchan Kim | 393c371 | 2021-11-18 15:00:08 -0800 | [diff] [blame] | 121 | root = kernfs_root(kn); |
| 122 | down_write(&root->kernfs_rwsem); |
Christian Brauner | 2f221d6 | 2021-01-21 14:19:26 +0100 | [diff] [blame] | 123 | error = setattr_prepare(&init_user_ns, dentry, iattr); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 124 | if (error) |
| 125 | goto out; |
| 126 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 127 | error = __kernfs_setattr(kn, iattr); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 128 | if (error) |
| 129 | goto out; |
| 130 | |
| 131 | /* this ignores size changes */ |
Christian Brauner | 2f221d6 | 2021-01-21 14:19:26 +0100 | [diff] [blame] | 132 | setattr_copy(&init_user_ns, inode, iattr); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 133 | |
| 134 | out: |
Minchan Kim | 393c371 | 2021-11-18 15:00:08 -0800 | [diff] [blame] | 135 | up_write(&root->kernfs_rwsem); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 136 | return error; |
| 137 | } |
| 138 | |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 139 | ssize_t kernfs_iop_listxattr(struct dentry *dentry, char *buf, size_t size) |
Tejun Heo | 2322392 | 2013-11-23 17:40:02 -0500 | [diff] [blame] | 140 | { |
Shaohua Li | 319ba91 | 2017-07-12 11:49:49 -0700 | [diff] [blame] | 141 | struct kernfs_node *kn = kernfs_dentry_node(dentry); |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 142 | struct kernfs_iattrs *attrs; |
Tejun Heo | 2322392 | 2013-11-23 17:40:02 -0500 | [diff] [blame] | 143 | |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 144 | attrs = kernfs_iattrs(kn); |
Tejun Heo | 2322392 | 2013-11-23 17:40:02 -0500 | [diff] [blame] | 145 | if (!attrs) |
| 146 | return -ENOMEM; |
| 147 | |
Andreas Gruenbacher | 786534b | 2015-12-02 14:44:39 +0100 | [diff] [blame] | 148 | return simple_xattr_list(d_inode(dentry), &attrs->xattrs, buf, size); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | static inline void set_default_inode_attr(struct inode *inode, umode_t mode) |
| 152 | { |
| 153 | inode->i_mode = mode; |
Deepa Dinamani | 3a3a5fe | 2016-02-22 07:17:53 -0800 | [diff] [blame] | 154 | inode->i_atime = inode->i_mtime = |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 155 | inode->i_ctime = current_time(inode); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 156 | } |
| 157 | |
Ondrej Mosnacek | 0589521 | 2019-02-22 15:57:12 +0100 | [diff] [blame] | 158 | static inline void set_inode_attr(struct inode *inode, |
| 159 | struct kernfs_iattrs *attrs) |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 160 | { |
Ondrej Mosnacek | 0589521 | 2019-02-22 15:57:12 +0100 | [diff] [blame] | 161 | inode->i_uid = attrs->ia_uid; |
| 162 | inode->i_gid = attrs->ia_gid; |
Al Viro | f0f3588 | 2019-12-07 21:51:31 -0500 | [diff] [blame] | 163 | inode->i_atime = attrs->ia_atime; |
| 164 | inode->i_mtime = attrs->ia_mtime; |
| 165 | inode->i_ctime = attrs->ia_ctime; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 166 | } |
| 167 | |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 168 | static void kernfs_refresh_inode(struct kernfs_node *kn, struct inode *inode) |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 169 | { |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 170 | struct kernfs_iattrs *attrs = kn->iattr; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 171 | |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 172 | inode->i_mode = kn->mode; |
Ondrej Mosnacek | 0ac6075 | 2019-02-22 15:57:15 +0100 | [diff] [blame] | 173 | if (attrs) |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 174 | /* |
| 175 | * kernfs_node has non-default attributes get them from |
| 176 | * persistent copy in kernfs_node. |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 177 | */ |
Ondrej Mosnacek | 0589521 | 2019-02-22 15:57:12 +0100 | [diff] [blame] | 178 | set_inode_attr(inode, attrs); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 179 | |
Tejun Heo | df23fc3 | 2013-12-11 14:11:56 -0500 | [diff] [blame] | 180 | if (kernfs_type(kn) == KERNFS_DIR) |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 181 | set_nlink(inode, kn->dir.subdirs + 2); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 182 | } |
| 183 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 184 | int kernfs_iop_getattr(struct user_namespace *mnt_userns, |
| 185 | const struct path *path, struct kstat *stat, |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 186 | u32 request_mask, unsigned int query_flags) |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 187 | { |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 188 | struct inode *inode = d_inode(path->dentry); |
Shaohua Li | 319ba91 | 2017-07-12 11:49:49 -0700 | [diff] [blame] | 189 | struct kernfs_node *kn = inode->i_private; |
Minchan Kim | 393c371 | 2021-11-18 15:00:08 -0800 | [diff] [blame] | 190 | struct kernfs_root *root = kernfs_root(kn); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 191 | |
Minchan Kim | 393c371 | 2021-11-18 15:00:08 -0800 | [diff] [blame] | 192 | down_read(&root->kernfs_rwsem); |
Ian Kent | 47b5c64 | 2021-07-16 17:28:34 +0800 | [diff] [blame] | 193 | spin_lock(&inode->i_lock); |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 194 | kernfs_refresh_inode(kn, inode); |
Christian Brauner | 0d56a45 | 2021-01-21 14:19:30 +0100 | [diff] [blame] | 195 | generic_fillattr(&init_user_ns, inode, stat); |
Ian Kent | 47b5c64 | 2021-07-16 17:28:34 +0800 | [diff] [blame] | 196 | spin_unlock(&inode->i_lock); |
Minchan Kim | 393c371 | 2021-11-18 15:00:08 -0800 | [diff] [blame] | 197 | up_read(&root->kernfs_rwsem); |
Ian Kent | 47b5c64 | 2021-07-16 17:28:34 +0800 | [diff] [blame] | 198 | |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 199 | return 0; |
| 200 | } |
| 201 | |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 202 | static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode) |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 203 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 204 | kernfs_get(kn); |
| 205 | inode->i_private = kn; |
Christoph Hellwig | c1e3dbe | 2021-06-28 19:36:09 -0700 | [diff] [blame] | 206 | inode->i_mapping->a_ops = &ram_aops; |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 207 | inode->i_op = &kernfs_iops; |
Tejun Heo | 67c0496 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 208 | inode->i_generation = kernfs_gen(kn); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 209 | |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 210 | set_default_inode_attr(inode, kn->mode); |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 211 | kernfs_refresh_inode(kn, inode); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 212 | |
| 213 | /* initialize inode according to type */ |
Tejun Heo | df23fc3 | 2013-12-11 14:11:56 -0500 | [diff] [blame] | 214 | switch (kernfs_type(kn)) { |
| 215 | case KERNFS_DIR: |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 216 | inode->i_op = &kernfs_dir_iops; |
| 217 | inode->i_fop = &kernfs_dir_fops; |
Eric W. Biederman | ea01521 | 2015-05-13 16:09:29 -0500 | [diff] [blame] | 218 | if (kn->flags & KERNFS_EMPTY_DIR) |
| 219 | make_empty_dir_inode(inode); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 220 | break; |
Tejun Heo | df23fc3 | 2013-12-11 14:11:56 -0500 | [diff] [blame] | 221 | case KERNFS_FILE: |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 222 | inode->i_size = kn->attr.size; |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 223 | inode->i_fop = &kernfs_file_fops; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 224 | break; |
Tejun Heo | df23fc3 | 2013-12-11 14:11:56 -0500 | [diff] [blame] | 225 | case KERNFS_LINK: |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 226 | inode->i_op = &kernfs_symlink_iops; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 227 | break; |
| 228 | default: |
| 229 | BUG(); |
| 230 | } |
| 231 | |
| 232 | unlock_new_inode(inode); |
| 233 | } |
| 234 | |
| 235 | /** |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 236 | * kernfs_get_inode - get inode for kernfs_node |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 237 | * @sb: super block |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 238 | * @kn: kernfs_node to allocate inode for |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 239 | * |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 240 | * Get inode for @kn. If such inode doesn't exist, a new inode is |
| 241 | * allocated and basics are initialized. New inode is returned |
| 242 | * locked. |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 243 | * |
| 244 | * LOCKING: |
| 245 | * Kernel thread context (may sleep). |
| 246 | * |
| 247 | * RETURNS: |
| 248 | * Pointer to allocated inode on success, NULL on failure. |
| 249 | */ |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 250 | struct inode *kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn) |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 251 | { |
| 252 | struct inode *inode; |
| 253 | |
Tejun Heo | 67c0496 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 254 | inode = iget_locked(sb, kernfs_ino(kn)); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 255 | if (inode && (inode->i_state & I_NEW)) |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 256 | kernfs_init_inode(kn, inode); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 257 | |
| 258 | return inode; |
| 259 | } |
| 260 | |
| 261 | /* |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 262 | * The kernfs_node serves as both an inode and a directory entry for |
| 263 | * kernfs. To prevent the kernfs inode numbers from being freed |
| 264 | * prematurely we take a reference to kernfs_node from the kernfs inode. A |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 265 | * super_operations.evict_inode() implementation is needed to drop that |
| 266 | * reference upon inode destruction. |
| 267 | */ |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 268 | void kernfs_evict_inode(struct inode *inode) |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 269 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 270 | struct kernfs_node *kn = inode->i_private; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 271 | |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 272 | truncate_inode_pages_final(&inode->i_data); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 273 | clear_inode(inode); |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 274 | kernfs_put(kn); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 275 | } |
| 276 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 277 | int kernfs_iop_permission(struct user_namespace *mnt_userns, |
| 278 | struct inode *inode, int mask) |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 279 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 280 | struct kernfs_node *kn; |
Minchan Kim | 393c371 | 2021-11-18 15:00:08 -0800 | [diff] [blame] | 281 | struct kernfs_root *root; |
Ian Kent | 47b5c64 | 2021-07-16 17:28:34 +0800 | [diff] [blame] | 282 | int ret; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 283 | |
| 284 | if (mask & MAY_NOT_BLOCK) |
| 285 | return -ECHILD; |
| 286 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 287 | kn = inode->i_private; |
Minchan Kim | 393c371 | 2021-11-18 15:00:08 -0800 | [diff] [blame] | 288 | root = kernfs_root(kn); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 289 | |
Minchan Kim | 393c371 | 2021-11-18 15:00:08 -0800 | [diff] [blame] | 290 | down_read(&root->kernfs_rwsem); |
Ian Kent | 47b5c64 | 2021-07-16 17:28:34 +0800 | [diff] [blame] | 291 | spin_lock(&inode->i_lock); |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 292 | kernfs_refresh_inode(kn, inode); |
Ian Kent | 47b5c64 | 2021-07-16 17:28:34 +0800 | [diff] [blame] | 293 | ret = generic_permission(&init_user_ns, inode, mask); |
| 294 | spin_unlock(&inode->i_lock); |
Minchan Kim | 393c371 | 2021-11-18 15:00:08 -0800 | [diff] [blame] | 295 | up_read(&root->kernfs_rwsem); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 296 | |
Ian Kent | 47b5c64 | 2021-07-16 17:28:34 +0800 | [diff] [blame] | 297 | return ret; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 298 | } |
Andreas Gruenbacher | e72a1a8 | 2016-09-29 17:48:33 +0200 | [diff] [blame] | 299 | |
Ondrej Mosnacek | 1537ad1 | 2019-04-03 09:29:41 +0200 | [diff] [blame] | 300 | int kernfs_xattr_get(struct kernfs_node *kn, const char *name, |
| 301 | void *value, size_t size) |
Andreas Gruenbacher | e72a1a8 | 2016-09-29 17:48:33 +0200 | [diff] [blame] | 302 | { |
Ondrej Mosnacek | 1537ad1 | 2019-04-03 09:29:41 +0200 | [diff] [blame] | 303 | struct kernfs_iattrs *attrs = kernfs_iattrs_noalloc(kn); |
Andreas Gruenbacher | e72a1a8 | 2016-09-29 17:48:33 +0200 | [diff] [blame] | 304 | if (!attrs) |
Ondrej Mosnacek | d0c9c15 | 2019-02-22 15:57:13 +0100 | [diff] [blame] | 305 | return -ENODATA; |
Andreas Gruenbacher | e72a1a8 | 2016-09-29 17:48:33 +0200 | [diff] [blame] | 306 | |
| 307 | return simple_xattr_get(&attrs->xattrs, name, value, size); |
| 308 | } |
| 309 | |
Ondrej Mosnacek | 1537ad1 | 2019-04-03 09:29:41 +0200 | [diff] [blame] | 310 | int kernfs_xattr_set(struct kernfs_node *kn, const char *name, |
| 311 | const void *value, size_t size, int flags) |
Andreas Gruenbacher | e72a1a8 | 2016-09-29 17:48:33 +0200 | [diff] [blame] | 312 | { |
Ondrej Mosnacek | 1537ad1 | 2019-04-03 09:29:41 +0200 | [diff] [blame] | 313 | struct kernfs_iattrs *attrs = kernfs_iattrs(kn); |
Andreas Gruenbacher | e72a1a8 | 2016-09-29 17:48:33 +0200 | [diff] [blame] | 314 | if (!attrs) |
| 315 | return -ENOMEM; |
| 316 | |
Daniel Xu | a46a229 | 2020-03-12 13:03:15 -0700 | [diff] [blame] | 317 | return simple_xattr_set(&attrs->xattrs, name, value, size, flags, NULL); |
Andreas Gruenbacher | e72a1a8 | 2016-09-29 17:48:33 +0200 | [diff] [blame] | 318 | } |
| 319 | |
Ondrej Mosnacek | 1537ad1 | 2019-04-03 09:29:41 +0200 | [diff] [blame] | 320 | static int kernfs_vfs_xattr_get(const struct xattr_handler *handler, |
| 321 | struct dentry *unused, struct inode *inode, |
| 322 | const char *suffix, void *value, size_t size) |
Ondrej Mosnacek | b230d5a | 2019-02-22 15:57:16 +0100 | [diff] [blame] | 323 | { |
Ondrej Mosnacek | 1537ad1 | 2019-04-03 09:29:41 +0200 | [diff] [blame] | 324 | const char *name = xattr_full_name(handler, suffix); |
Ondrej Mosnacek | b230d5a | 2019-02-22 15:57:16 +0100 | [diff] [blame] | 325 | struct kernfs_node *kn = inode->i_private; |
| 326 | |
Ondrej Mosnacek | 1537ad1 | 2019-04-03 09:29:41 +0200 | [diff] [blame] | 327 | return kernfs_xattr_get(kn, name, value, size); |
Ondrej Mosnacek | b230d5a | 2019-02-22 15:57:16 +0100 | [diff] [blame] | 328 | } |
| 329 | |
Ondrej Mosnacek | 1537ad1 | 2019-04-03 09:29:41 +0200 | [diff] [blame] | 330 | static int kernfs_vfs_xattr_set(const struct xattr_handler *handler, |
Christian Brauner | e65ce2a | 2021-01-21 14:19:27 +0100 | [diff] [blame] | 331 | struct user_namespace *mnt_userns, |
Ondrej Mosnacek | 1537ad1 | 2019-04-03 09:29:41 +0200 | [diff] [blame] | 332 | struct dentry *unused, struct inode *inode, |
| 333 | const char *suffix, const void *value, |
| 334 | size_t size, int flags) |
Ondrej Mosnacek | b230d5a | 2019-02-22 15:57:16 +0100 | [diff] [blame] | 335 | { |
Ondrej Mosnacek | 1537ad1 | 2019-04-03 09:29:41 +0200 | [diff] [blame] | 336 | const char *name = xattr_full_name(handler, suffix); |
Ondrej Mosnacek | b230d5a | 2019-02-22 15:57:16 +0100 | [diff] [blame] | 337 | struct kernfs_node *kn = inode->i_private; |
| 338 | |
Ondrej Mosnacek | 1537ad1 | 2019-04-03 09:29:41 +0200 | [diff] [blame] | 339 | return kernfs_xattr_set(kn, name, value, size, flags); |
Ondrej Mosnacek | b230d5a | 2019-02-22 15:57:16 +0100 | [diff] [blame] | 340 | } |
| 341 | |
Daniel Xu | 0c47383 | 2020-03-12 13:03:16 -0700 | [diff] [blame] | 342 | static int kernfs_vfs_user_xattr_add(struct kernfs_node *kn, |
| 343 | const char *full_name, |
| 344 | struct simple_xattrs *xattrs, |
| 345 | const void *value, size_t size, int flags) |
| 346 | { |
| 347 | atomic_t *sz = &kn->iattr->user_xattr_size; |
| 348 | atomic_t *nr = &kn->iattr->nr_user_xattrs; |
| 349 | ssize_t removed_size; |
| 350 | int ret; |
| 351 | |
| 352 | if (atomic_inc_return(nr) > KERNFS_MAX_USER_XATTRS) { |
| 353 | ret = -ENOSPC; |
| 354 | goto dec_count_out; |
| 355 | } |
| 356 | |
| 357 | if (atomic_add_return(size, sz) > KERNFS_USER_XATTR_SIZE_LIMIT) { |
| 358 | ret = -ENOSPC; |
| 359 | goto dec_size_out; |
| 360 | } |
| 361 | |
| 362 | ret = simple_xattr_set(xattrs, full_name, value, size, flags, |
| 363 | &removed_size); |
| 364 | |
| 365 | if (!ret && removed_size >= 0) |
| 366 | size = removed_size; |
| 367 | else if (!ret) |
| 368 | return 0; |
| 369 | dec_size_out: |
| 370 | atomic_sub(size, sz); |
| 371 | dec_count_out: |
| 372 | atomic_dec(nr); |
| 373 | return ret; |
| 374 | } |
| 375 | |
| 376 | static int kernfs_vfs_user_xattr_rm(struct kernfs_node *kn, |
| 377 | const char *full_name, |
| 378 | struct simple_xattrs *xattrs, |
| 379 | const void *value, size_t size, int flags) |
| 380 | { |
| 381 | atomic_t *sz = &kn->iattr->user_xattr_size; |
| 382 | atomic_t *nr = &kn->iattr->nr_user_xattrs; |
| 383 | ssize_t removed_size; |
| 384 | int ret; |
| 385 | |
| 386 | ret = simple_xattr_set(xattrs, full_name, value, size, flags, |
| 387 | &removed_size); |
| 388 | |
| 389 | if (removed_size >= 0) { |
| 390 | atomic_sub(removed_size, sz); |
| 391 | atomic_dec(nr); |
| 392 | } |
| 393 | |
| 394 | return ret; |
| 395 | } |
| 396 | |
| 397 | static int kernfs_vfs_user_xattr_set(const struct xattr_handler *handler, |
Christian Brauner | e65ce2a | 2021-01-21 14:19:27 +0100 | [diff] [blame] | 398 | struct user_namespace *mnt_userns, |
Daniel Xu | 0c47383 | 2020-03-12 13:03:16 -0700 | [diff] [blame] | 399 | struct dentry *unused, struct inode *inode, |
| 400 | const char *suffix, const void *value, |
| 401 | size_t size, int flags) |
| 402 | { |
| 403 | const char *full_name = xattr_full_name(handler, suffix); |
| 404 | struct kernfs_node *kn = inode->i_private; |
| 405 | struct kernfs_iattrs *attrs; |
| 406 | |
| 407 | if (!(kernfs_root(kn)->flags & KERNFS_ROOT_SUPPORT_USER_XATTR)) |
| 408 | return -EOPNOTSUPP; |
| 409 | |
| 410 | attrs = kernfs_iattrs(kn); |
| 411 | if (!attrs) |
| 412 | return -ENOMEM; |
| 413 | |
| 414 | if (value) |
| 415 | return kernfs_vfs_user_xattr_add(kn, full_name, &attrs->xattrs, |
| 416 | value, size, flags); |
| 417 | else |
| 418 | return kernfs_vfs_user_xattr_rm(kn, full_name, &attrs->xattrs, |
| 419 | value, size, flags); |
| 420 | |
| 421 | } |
| 422 | |
Bart Van Assche | b5a0623 | 2016-11-11 16:45:58 -0800 | [diff] [blame] | 423 | static const struct xattr_handler kernfs_trusted_xattr_handler = { |
Andreas Gruenbacher | e72a1a8 | 2016-09-29 17:48:33 +0200 | [diff] [blame] | 424 | .prefix = XATTR_TRUSTED_PREFIX, |
Ondrej Mosnacek | 1537ad1 | 2019-04-03 09:29:41 +0200 | [diff] [blame] | 425 | .get = kernfs_vfs_xattr_get, |
| 426 | .set = kernfs_vfs_xattr_set, |
Andreas Gruenbacher | e72a1a8 | 2016-09-29 17:48:33 +0200 | [diff] [blame] | 427 | }; |
| 428 | |
Bart Van Assche | b5a0623 | 2016-11-11 16:45:58 -0800 | [diff] [blame] | 429 | static const struct xattr_handler kernfs_security_xattr_handler = { |
Andreas Gruenbacher | e72a1a8 | 2016-09-29 17:48:33 +0200 | [diff] [blame] | 430 | .prefix = XATTR_SECURITY_PREFIX, |
Ondrej Mosnacek | 1537ad1 | 2019-04-03 09:29:41 +0200 | [diff] [blame] | 431 | .get = kernfs_vfs_xattr_get, |
| 432 | .set = kernfs_vfs_xattr_set, |
Andreas Gruenbacher | e72a1a8 | 2016-09-29 17:48:33 +0200 | [diff] [blame] | 433 | }; |
| 434 | |
Daniel Xu | 0c47383 | 2020-03-12 13:03:16 -0700 | [diff] [blame] | 435 | static const struct xattr_handler kernfs_user_xattr_handler = { |
| 436 | .prefix = XATTR_USER_PREFIX, |
| 437 | .get = kernfs_vfs_xattr_get, |
| 438 | .set = kernfs_vfs_user_xattr_set, |
| 439 | }; |
| 440 | |
Andreas Gruenbacher | e72a1a8 | 2016-09-29 17:48:33 +0200 | [diff] [blame] | 441 | const struct xattr_handler *kernfs_xattr_handlers[] = { |
| 442 | &kernfs_trusted_xattr_handler, |
| 443 | &kernfs_security_xattr_handler, |
Daniel Xu | 0c47383 | 2020-03-12 13:03:16 -0700 | [diff] [blame] | 444 | &kernfs_user_xattr_handler, |
Andreas Gruenbacher | e72a1a8 | 2016-09-29 17:48:33 +0200 | [diff] [blame] | 445 | NULL |
| 446 | }; |