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 address_space_operations kernfs_aops = { |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 21 | .readpage = simple_readpage, |
| 22 | .write_begin = simple_write_begin, |
| 23 | .write_end = simple_write_end, |
| 24 | }; |
| 25 | |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 26 | static const struct inode_operations kernfs_iops = { |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 27 | .permission = kernfs_iop_permission, |
| 28 | .setattr = kernfs_iop_setattr, |
| 29 | .getattr = kernfs_iop_getattr, |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 30 | .listxattr = kernfs_iop_listxattr, |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 31 | }; |
| 32 | |
Ondrej Mosnacek | d0c9c15 | 2019-02-22 15:57:13 +0100 | [diff] [blame] | 33 | static struct kernfs_iattrs *__kernfs_iattrs(struct kernfs_node *kn, int alloc) |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 34 | { |
Tejun Heo | 4afddd6 | 2014-04-02 16:40:52 -0400 | [diff] [blame] | 35 | static DEFINE_MUTEX(iattr_mutex); |
| 36 | struct kernfs_iattrs *ret; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 37 | |
Tejun Heo | 4afddd6 | 2014-04-02 16:40:52 -0400 | [diff] [blame] | 38 | mutex_lock(&iattr_mutex); |
| 39 | |
Ondrej Mosnacek | d0c9c15 | 2019-02-22 15:57:13 +0100 | [diff] [blame] | 40 | if (kn->iattr || !alloc) |
Tejun Heo | 4afddd6 | 2014-04-02 16:40:52 -0400 | [diff] [blame] | 41 | goto out_unlock; |
Tejun Heo | 9a8049a | 2013-11-23 17:40:01 -0500 | [diff] [blame] | 42 | |
Ayush Mittal | 26e28d6 | 2019-02-06 10:25:42 +0530 | [diff] [blame] | 43 | kn->iattr = kmem_cache_zalloc(kernfs_iattrs_cache, GFP_KERNEL); |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 44 | if (!kn->iattr) |
Tejun Heo | 4afddd6 | 2014-04-02 16:40:52 -0400 | [diff] [blame] | 45 | goto out_unlock; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 46 | |
| 47 | /* assign default attributes */ |
Ondrej Mosnacek | 0589521 | 2019-02-22 15:57:12 +0100 | [diff] [blame] | 48 | kn->iattr->ia_uid = GLOBAL_ROOT_UID; |
| 49 | kn->iattr->ia_gid = GLOBAL_ROOT_GID; |
Deepa Dinamani | 3a3a5fe | 2016-02-22 07:17:53 -0800 | [diff] [blame] | 50 | |
Ondrej Mosnacek | 0589521 | 2019-02-22 15:57:12 +0100 | [diff] [blame] | 51 | ktime_get_real_ts64(&kn->iattr->ia_atime); |
| 52 | kn->iattr->ia_mtime = kn->iattr->ia_atime; |
| 53 | kn->iattr->ia_ctime = kn->iattr->ia_atime; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 54 | |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 55 | simple_xattrs_init(&kn->iattr->xattrs); |
Tejun Heo | 4afddd6 | 2014-04-02 16:40:52 -0400 | [diff] [blame] | 56 | out_unlock: |
| 57 | ret = kn->iattr; |
| 58 | mutex_unlock(&iattr_mutex); |
| 59 | return ret; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 60 | } |
| 61 | |
Ondrej Mosnacek | d0c9c15 | 2019-02-22 15:57:13 +0100 | [diff] [blame] | 62 | static struct kernfs_iattrs *kernfs_iattrs(struct kernfs_node *kn) |
| 63 | { |
| 64 | return __kernfs_iattrs(kn, 1); |
| 65 | } |
| 66 | |
| 67 | static struct kernfs_iattrs *kernfs_iattrs_noalloc(struct kernfs_node *kn) |
| 68 | { |
| 69 | return __kernfs_iattrs(kn, 0); |
| 70 | } |
| 71 | |
Dmitry Torokhov | 488dee9 | 2018-07-20 21:56:47 +0000 | [diff] [blame] | 72 | int __kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr) |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 73 | { |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 74 | struct kernfs_iattrs *attrs; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 75 | unsigned int ia_valid = iattr->ia_valid; |
| 76 | |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 77 | attrs = kernfs_iattrs(kn); |
Tejun Heo | 9a8049a | 2013-11-23 17:40:01 -0500 | [diff] [blame] | 78 | if (!attrs) |
| 79 | return -ENOMEM; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 80 | |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 81 | if (ia_valid & ATTR_UID) |
Ondrej Mosnacek | 0589521 | 2019-02-22 15:57:12 +0100 | [diff] [blame] | 82 | attrs->ia_uid = iattr->ia_uid; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 83 | if (ia_valid & ATTR_GID) |
Ondrej Mosnacek | 0589521 | 2019-02-22 15:57:12 +0100 | [diff] [blame] | 84 | attrs->ia_gid = iattr->ia_gid; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 85 | if (ia_valid & ATTR_ATIME) |
Ondrej Mosnacek | 0589521 | 2019-02-22 15:57:12 +0100 | [diff] [blame] | 86 | attrs->ia_atime = iattr->ia_atime; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 87 | if (ia_valid & ATTR_MTIME) |
Ondrej Mosnacek | 0589521 | 2019-02-22 15:57:12 +0100 | [diff] [blame] | 88 | attrs->ia_mtime = iattr->ia_mtime; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 89 | if (ia_valid & ATTR_CTIME) |
Ondrej Mosnacek | 0589521 | 2019-02-22 15:57:12 +0100 | [diff] [blame] | 90 | attrs->ia_ctime = iattr->ia_ctime; |
| 91 | if (ia_valid & ATTR_MODE) |
| 92 | kn->mode = iattr->ia_mode; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * kernfs_setattr - set iattr on a node |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 98 | * @kn: target node |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 99 | * @iattr: iattr to set |
| 100 | * |
| 101 | * Returns 0 on success, -errno on failure. |
| 102 | */ |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 103 | int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr) |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 104 | { |
| 105 | int ret; |
| 106 | |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 107 | mutex_lock(&kernfs_mutex); |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 108 | ret = __kernfs_setattr(kn, iattr); |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 109 | mutex_unlock(&kernfs_mutex); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 110 | return ret; |
| 111 | } |
| 112 | |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 113 | int kernfs_iop_setattr(struct dentry *dentry, struct iattr *iattr) |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 114 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 115 | struct inode *inode = d_inode(dentry); |
Shaohua Li | 319ba91 | 2017-07-12 11:49:49 -0700 | [diff] [blame] | 116 | struct kernfs_node *kn = inode->i_private; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 117 | int error; |
| 118 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 119 | if (!kn) |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 120 | return -EINVAL; |
| 121 | |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 122 | mutex_lock(&kernfs_mutex); |
Jan Kara | 31051c8 | 2016-05-26 16:55:18 +0200 | [diff] [blame] | 123 | error = setattr_prepare(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 */ |
| 132 | setattr_copy(inode, iattr); |
| 133 | |
| 134 | out: |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 135 | mutex_unlock(&kernfs_mutex); |
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; |
Deepa Dinamani | 3818c19 | 2019-06-15 09:00:05 -0700 | [diff] [blame] | 163 | inode->i_atime = timestamp_truncate(attrs->ia_atime, inode); |
| 164 | inode->i_mtime = timestamp_truncate(attrs->ia_mtime, inode); |
| 165 | inode->i_ctime = timestamp_truncate(attrs->ia_ctime, inode); |
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 | |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 184 | int kernfs_iop_getattr(const struct path *path, struct kstat *stat, |
| 185 | u32 request_mask, unsigned int query_flags) |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 186 | { |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 187 | struct inode *inode = d_inode(path->dentry); |
Shaohua Li | 319ba91 | 2017-07-12 11:49:49 -0700 | [diff] [blame] | 188 | struct kernfs_node *kn = inode->i_private; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 189 | |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 190 | mutex_lock(&kernfs_mutex); |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 191 | kernfs_refresh_inode(kn, inode); |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 192 | mutex_unlock(&kernfs_mutex); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 193 | |
| 194 | generic_fillattr(inode, stat); |
| 195 | return 0; |
| 196 | } |
| 197 | |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 198 | static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode) |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 199 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 200 | kernfs_get(kn); |
| 201 | inode->i_private = kn; |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 202 | inode->i_mapping->a_ops = &kernfs_aops; |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 203 | inode->i_op = &kernfs_iops; |
Shaohua Li | c53cd49 | 2017-07-12 11:49:50 -0700 | [diff] [blame] | 204 | inode->i_generation = kn->id.generation; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 205 | |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 206 | set_default_inode_attr(inode, kn->mode); |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 207 | kernfs_refresh_inode(kn, inode); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 208 | |
| 209 | /* initialize inode according to type */ |
Tejun Heo | df23fc3 | 2013-12-11 14:11:56 -0500 | [diff] [blame] | 210 | switch (kernfs_type(kn)) { |
| 211 | case KERNFS_DIR: |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 212 | inode->i_op = &kernfs_dir_iops; |
| 213 | inode->i_fop = &kernfs_dir_fops; |
Eric W. Biederman | ea01521 | 2015-05-13 16:09:29 -0500 | [diff] [blame] | 214 | if (kn->flags & KERNFS_EMPTY_DIR) |
| 215 | make_empty_dir_inode(inode); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 216 | break; |
Tejun Heo | df23fc3 | 2013-12-11 14:11:56 -0500 | [diff] [blame] | 217 | case KERNFS_FILE: |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 218 | inode->i_size = kn->attr.size; |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 219 | inode->i_fop = &kernfs_file_fops; |
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_LINK: |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 222 | inode->i_op = &kernfs_symlink_iops; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 223 | break; |
| 224 | default: |
| 225 | BUG(); |
| 226 | } |
| 227 | |
| 228 | unlock_new_inode(inode); |
| 229 | } |
| 230 | |
| 231 | /** |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 232 | * kernfs_get_inode - get inode for kernfs_node |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 233 | * @sb: super block |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 234 | * @kn: kernfs_node to allocate inode for |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 235 | * |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 236 | * Get inode for @kn. If such inode doesn't exist, a new inode is |
| 237 | * allocated and basics are initialized. New inode is returned |
| 238 | * locked. |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 239 | * |
| 240 | * LOCKING: |
| 241 | * Kernel thread context (may sleep). |
| 242 | * |
| 243 | * RETURNS: |
| 244 | * Pointer to allocated inode on success, NULL on failure. |
| 245 | */ |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 246 | 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] | 247 | { |
| 248 | struct inode *inode; |
| 249 | |
Shaohua Li | c53cd49 | 2017-07-12 11:49:50 -0700 | [diff] [blame] | 250 | inode = iget_locked(sb, kn->id.ino); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 251 | if (inode && (inode->i_state & I_NEW)) |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 252 | kernfs_init_inode(kn, inode); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 253 | |
| 254 | return inode; |
| 255 | } |
| 256 | |
| 257 | /* |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 258 | * The kernfs_node serves as both an inode and a directory entry for |
| 259 | * kernfs. To prevent the kernfs inode numbers from being freed |
| 260 | * 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] | 261 | * super_operations.evict_inode() implementation is needed to drop that |
| 262 | * reference upon inode destruction. |
| 263 | */ |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 264 | void kernfs_evict_inode(struct inode *inode) |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 265 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 266 | struct kernfs_node *kn = inode->i_private; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 267 | |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 268 | truncate_inode_pages_final(&inode->i_data); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 269 | clear_inode(inode); |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 270 | kernfs_put(kn); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 271 | } |
| 272 | |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 273 | int kernfs_iop_permission(struct inode *inode, int mask) |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 274 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 275 | struct kernfs_node *kn; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 276 | |
| 277 | if (mask & MAY_NOT_BLOCK) |
| 278 | return -ECHILD; |
| 279 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 280 | kn = inode->i_private; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 281 | |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 282 | mutex_lock(&kernfs_mutex); |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 283 | kernfs_refresh_inode(kn, inode); |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 284 | mutex_unlock(&kernfs_mutex); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 285 | |
| 286 | return generic_permission(inode, mask); |
| 287 | } |
Andreas Gruenbacher | e72a1a8 | 2016-09-29 17:48:33 +0200 | [diff] [blame] | 288 | |
Ondrej Mosnacek | 1537ad1 | 2019-04-03 09:29:41 +0200 | [diff] [blame] | 289 | int kernfs_xattr_get(struct kernfs_node *kn, const char *name, |
| 290 | void *value, size_t size) |
Andreas Gruenbacher | e72a1a8 | 2016-09-29 17:48:33 +0200 | [diff] [blame] | 291 | { |
Ondrej Mosnacek | 1537ad1 | 2019-04-03 09:29:41 +0200 | [diff] [blame] | 292 | struct kernfs_iattrs *attrs = kernfs_iattrs_noalloc(kn); |
Andreas Gruenbacher | e72a1a8 | 2016-09-29 17:48:33 +0200 | [diff] [blame] | 293 | if (!attrs) |
Ondrej Mosnacek | d0c9c15 | 2019-02-22 15:57:13 +0100 | [diff] [blame] | 294 | return -ENODATA; |
Andreas Gruenbacher | e72a1a8 | 2016-09-29 17:48:33 +0200 | [diff] [blame] | 295 | |
| 296 | return simple_xattr_get(&attrs->xattrs, name, value, size); |
| 297 | } |
| 298 | |
Ondrej Mosnacek | 1537ad1 | 2019-04-03 09:29:41 +0200 | [diff] [blame] | 299 | int kernfs_xattr_set(struct kernfs_node *kn, const char *name, |
| 300 | const void *value, size_t size, int flags) |
Andreas Gruenbacher | e72a1a8 | 2016-09-29 17:48:33 +0200 | [diff] [blame] | 301 | { |
Ondrej Mosnacek | 1537ad1 | 2019-04-03 09:29:41 +0200 | [diff] [blame] | 302 | struct kernfs_iattrs *attrs = kernfs_iattrs(kn); |
Andreas Gruenbacher | e72a1a8 | 2016-09-29 17:48:33 +0200 | [diff] [blame] | 303 | if (!attrs) |
| 304 | return -ENOMEM; |
| 305 | |
| 306 | return simple_xattr_set(&attrs->xattrs, name, value, size, flags); |
| 307 | } |
| 308 | |
Ondrej Mosnacek | 1537ad1 | 2019-04-03 09:29:41 +0200 | [diff] [blame] | 309 | static int kernfs_vfs_xattr_get(const struct xattr_handler *handler, |
| 310 | struct dentry *unused, struct inode *inode, |
| 311 | const char *suffix, void *value, size_t size) |
Ondrej Mosnacek | b230d5a | 2019-02-22 15:57:16 +0100 | [diff] [blame] | 312 | { |
Ondrej Mosnacek | 1537ad1 | 2019-04-03 09:29:41 +0200 | [diff] [blame] | 313 | const char *name = xattr_full_name(handler, suffix); |
Ondrej Mosnacek | b230d5a | 2019-02-22 15:57:16 +0100 | [diff] [blame] | 314 | struct kernfs_node *kn = inode->i_private; |
| 315 | |
Ondrej Mosnacek | 1537ad1 | 2019-04-03 09:29:41 +0200 | [diff] [blame] | 316 | return kernfs_xattr_get(kn, name, value, size); |
Ondrej Mosnacek | b230d5a | 2019-02-22 15:57:16 +0100 | [diff] [blame] | 317 | } |
| 318 | |
Ondrej Mosnacek | 1537ad1 | 2019-04-03 09:29:41 +0200 | [diff] [blame] | 319 | static int kernfs_vfs_xattr_set(const struct xattr_handler *handler, |
| 320 | struct dentry *unused, struct inode *inode, |
| 321 | const char *suffix, const void *value, |
| 322 | size_t size, int flags) |
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_set(kn, name, value, size, flags); |
Ondrej Mosnacek | b230d5a | 2019-02-22 15:57:16 +0100 | [diff] [blame] | 328 | } |
| 329 | |
Bart Van Assche | b5a0623 | 2016-11-11 16:45:58 -0800 | [diff] [blame] | 330 | static const struct xattr_handler kernfs_trusted_xattr_handler = { |
Andreas Gruenbacher | e72a1a8 | 2016-09-29 17:48:33 +0200 | [diff] [blame] | 331 | .prefix = XATTR_TRUSTED_PREFIX, |
Ondrej Mosnacek | 1537ad1 | 2019-04-03 09:29:41 +0200 | [diff] [blame] | 332 | .get = kernfs_vfs_xattr_get, |
| 333 | .set = kernfs_vfs_xattr_set, |
Andreas Gruenbacher | e72a1a8 | 2016-09-29 17:48:33 +0200 | [diff] [blame] | 334 | }; |
| 335 | |
Bart Van Assche | b5a0623 | 2016-11-11 16:45:58 -0800 | [diff] [blame] | 336 | static const struct xattr_handler kernfs_security_xattr_handler = { |
Andreas Gruenbacher | e72a1a8 | 2016-09-29 17:48:33 +0200 | [diff] [blame] | 337 | .prefix = XATTR_SECURITY_PREFIX, |
Ondrej Mosnacek | 1537ad1 | 2019-04-03 09:29:41 +0200 | [diff] [blame] | 338 | .get = kernfs_vfs_xattr_get, |
| 339 | .set = kernfs_vfs_xattr_set, |
Andreas Gruenbacher | e72a1a8 | 2016-09-29 17:48:33 +0200 | [diff] [blame] | 340 | }; |
| 341 | |
| 342 | const struct xattr_handler *kernfs_xattr_handlers[] = { |
| 343 | &kernfs_trusted_xattr_handler, |
| 344 | &kernfs_security_xattr_handler, |
| 345 | NULL |
| 346 | }; |