Tejun Heo | b8441ed | 2013-11-24 09:54:58 -0500 | [diff] [blame] | 1 | /* |
| 2 | * fs/kernfs/inode.c - kernfs inode implementation |
| 3 | * |
| 4 | * Copyright (c) 2001-3 Patrick Mochel |
| 5 | * Copyright (c) 2007 SUSE Linux Products GmbH |
| 6 | * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org> |
| 7 | * |
| 8 | * This file is released under the GPLv2. |
| 9 | */ |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 10 | |
| 11 | #include <linux/pagemap.h> |
| 12 | #include <linux/backing-dev.h> |
| 13 | #include <linux/capability.h> |
| 14 | #include <linux/errno.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/xattr.h> |
| 17 | #include <linux/security.h> |
| 18 | |
| 19 | #include "kernfs-internal.h" |
| 20 | |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 21 | static const struct address_space_operations kernfs_aops = { |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 22 | .readpage = simple_readpage, |
| 23 | .write_begin = simple_write_begin, |
| 24 | .write_end = simple_write_end, |
| 25 | }; |
| 26 | |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 27 | static const struct inode_operations kernfs_iops = { |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 28 | .permission = kernfs_iop_permission, |
| 29 | .setattr = kernfs_iop_setattr, |
| 30 | .getattr = kernfs_iop_getattr, |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 31 | .listxattr = kernfs_iop_listxattr, |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 32 | }; |
| 33 | |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 34 | static struct kernfs_iattrs *kernfs_iattrs(struct kernfs_node *kn) |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 35 | { |
Tejun Heo | 4afddd6 | 2014-04-02 16:40:52 -0400 | [diff] [blame] | 36 | static DEFINE_MUTEX(iattr_mutex); |
| 37 | struct kernfs_iattrs *ret; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 38 | struct iattr *iattrs; |
| 39 | |
Tejun Heo | 4afddd6 | 2014-04-02 16:40:52 -0400 | [diff] [blame] | 40 | mutex_lock(&iattr_mutex); |
| 41 | |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 42 | if (kn->iattr) |
Tejun Heo | 4afddd6 | 2014-04-02 16:40:52 -0400 | [diff] [blame] | 43 | goto out_unlock; |
Tejun Heo | 9a8049a | 2013-11-23 17:40:01 -0500 | [diff] [blame] | 44 | |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 45 | kn->iattr = kzalloc(sizeof(struct kernfs_iattrs), GFP_KERNEL); |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 46 | if (!kn->iattr) |
Tejun Heo | 4afddd6 | 2014-04-02 16:40:52 -0400 | [diff] [blame] | 47 | goto out_unlock; |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 48 | iattrs = &kn->iattr->ia_iattr; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 49 | |
| 50 | /* assign default attributes */ |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 51 | iattrs->ia_mode = kn->mode; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 52 | iattrs->ia_uid = GLOBAL_ROOT_UID; |
| 53 | iattrs->ia_gid = GLOBAL_ROOT_GID; |
Deepa Dinamani | 3a3a5fe | 2016-02-22 07:17:53 -0800 | [diff] [blame] | 54 | |
Deepa Dinamani | 95582b0 | 2018-05-08 19:36:02 -0700 | [diff] [blame] | 55 | ktime_get_real_ts64(&iattrs->ia_atime); |
Deepa Dinamani | 3a3a5fe | 2016-02-22 07:17:53 -0800 | [diff] [blame] | 56 | iattrs->ia_mtime = iattrs->ia_atime; |
| 57 | iattrs->ia_ctime = iattrs->ia_atime; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 58 | |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 59 | simple_xattrs_init(&kn->iattr->xattrs); |
Tejun Heo | 4afddd6 | 2014-04-02 16:40:52 -0400 | [diff] [blame] | 60 | out_unlock: |
| 61 | ret = kn->iattr; |
| 62 | mutex_unlock(&iattr_mutex); |
| 63 | return ret; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 64 | } |
| 65 | |
Dmitry Torokhov | 488dee9 | 2018-07-20 21:56:47 +0000 | [diff] [blame^] | 66 | int __kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr) |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 67 | { |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 68 | struct kernfs_iattrs *attrs; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 69 | struct iattr *iattrs; |
| 70 | unsigned int ia_valid = iattr->ia_valid; |
| 71 | |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 72 | attrs = kernfs_iattrs(kn); |
Tejun Heo | 9a8049a | 2013-11-23 17:40:01 -0500 | [diff] [blame] | 73 | if (!attrs) |
| 74 | return -ENOMEM; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 75 | |
Tejun Heo | 9a8049a | 2013-11-23 17:40:01 -0500 | [diff] [blame] | 76 | iattrs = &attrs->ia_iattr; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 77 | |
| 78 | if (ia_valid & ATTR_UID) |
| 79 | iattrs->ia_uid = iattr->ia_uid; |
| 80 | if (ia_valid & ATTR_GID) |
| 81 | iattrs->ia_gid = iattr->ia_gid; |
| 82 | if (ia_valid & ATTR_ATIME) |
| 83 | iattrs->ia_atime = iattr->ia_atime; |
| 84 | if (ia_valid & ATTR_MTIME) |
| 85 | iattrs->ia_mtime = iattr->ia_mtime; |
| 86 | if (ia_valid & ATTR_CTIME) |
| 87 | iattrs->ia_ctime = iattr->ia_ctime; |
| 88 | if (ia_valid & ATTR_MODE) { |
| 89 | umode_t mode = iattr->ia_mode; |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 90 | iattrs->ia_mode = kn->mode = mode; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 91 | } |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * kernfs_setattr - set iattr on a node |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 97 | * @kn: target node |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 98 | * @iattr: iattr to set |
| 99 | * |
| 100 | * Returns 0 on success, -errno on failure. |
| 101 | */ |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 102 | int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr) |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 103 | { |
| 104 | int ret; |
| 105 | |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 106 | mutex_lock(&kernfs_mutex); |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 107 | ret = __kernfs_setattr(kn, iattr); |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 108 | mutex_unlock(&kernfs_mutex); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 109 | return ret; |
| 110 | } |
| 111 | |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 112 | int kernfs_iop_setattr(struct dentry *dentry, struct iattr *iattr) |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 113 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 114 | struct inode *inode = d_inode(dentry); |
Shaohua Li | 319ba91 | 2017-07-12 11:49:49 -0700 | [diff] [blame] | 115 | struct kernfs_node *kn = inode->i_private; |
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 | |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 121 | mutex_lock(&kernfs_mutex); |
Jan Kara | 31051c8 | 2016-05-26 16:55:18 +0200 | [diff] [blame] | 122 | error = setattr_prepare(dentry, iattr); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 123 | if (error) |
| 124 | goto out; |
| 125 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 126 | error = __kernfs_setattr(kn, iattr); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 127 | if (error) |
| 128 | goto out; |
| 129 | |
| 130 | /* this ignores size changes */ |
| 131 | setattr_copy(inode, iattr); |
| 132 | |
| 133 | out: |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 134 | mutex_unlock(&kernfs_mutex); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 135 | return error; |
| 136 | } |
| 137 | |
Andreas Gruenbacher | e72a1a8 | 2016-09-29 17:48:33 +0200 | [diff] [blame] | 138 | static int kernfs_node_setsecdata(struct kernfs_iattrs *attrs, void **secdata, |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 139 | u32 *secdata_len) |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 140 | { |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 141 | void *old_secdata; |
| 142 | size_t old_secdata_len; |
| 143 | |
Tejun Heo | 9a8049a | 2013-11-23 17:40:01 -0500 | [diff] [blame] | 144 | old_secdata = attrs->ia_secdata; |
| 145 | old_secdata_len = attrs->ia_secdata_len; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 146 | |
Tejun Heo | 9a8049a | 2013-11-23 17:40:01 -0500 | [diff] [blame] | 147 | attrs->ia_secdata = *secdata; |
| 148 | attrs->ia_secdata_len = *secdata_len; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 149 | |
| 150 | *secdata = old_secdata; |
| 151 | *secdata_len = old_secdata_len; |
| 152 | return 0; |
| 153 | } |
| 154 | |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 155 | 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] | 156 | { |
Shaohua Li | 319ba91 | 2017-07-12 11:49:49 -0700 | [diff] [blame] | 157 | struct kernfs_node *kn = kernfs_dentry_node(dentry); |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 158 | struct kernfs_iattrs *attrs; |
Tejun Heo | 2322392 | 2013-11-23 17:40:02 -0500 | [diff] [blame] | 159 | |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 160 | attrs = kernfs_iattrs(kn); |
Tejun Heo | 2322392 | 2013-11-23 17:40:02 -0500 | [diff] [blame] | 161 | if (!attrs) |
| 162 | return -ENOMEM; |
| 163 | |
Andreas Gruenbacher | 786534b | 2015-12-02 14:44:39 +0100 | [diff] [blame] | 164 | return simple_xattr_list(d_inode(dentry), &attrs->xattrs, buf, size); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | static inline void set_default_inode_attr(struct inode *inode, umode_t mode) |
| 168 | { |
| 169 | inode->i_mode = mode; |
Deepa Dinamani | 3a3a5fe | 2016-02-22 07:17:53 -0800 | [diff] [blame] | 170 | inode->i_atime = inode->i_mtime = |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 171 | inode->i_ctime = current_time(inode); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | static inline void set_inode_attr(struct inode *inode, struct iattr *iattr) |
| 175 | { |
Deepa Dinamani | 3a3a5fe | 2016-02-22 07:17:53 -0800 | [diff] [blame] | 176 | struct super_block *sb = inode->i_sb; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 177 | inode->i_uid = iattr->ia_uid; |
| 178 | inode->i_gid = iattr->ia_gid; |
Deepa Dinamani | 95582b0 | 2018-05-08 19:36:02 -0700 | [diff] [blame] | 179 | inode->i_atime = timespec64_trunc(iattr->ia_atime, sb->s_time_gran); |
| 180 | inode->i_mtime = timespec64_trunc(iattr->ia_mtime, sb->s_time_gran); |
| 181 | inode->i_ctime = timespec64_trunc(iattr->ia_ctime, sb->s_time_gran); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 182 | } |
| 183 | |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 184 | static void kernfs_refresh_inode(struct kernfs_node *kn, struct inode *inode) |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 185 | { |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 186 | struct kernfs_iattrs *attrs = kn->iattr; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 187 | |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 188 | inode->i_mode = kn->mode; |
Tejun Heo | 9a8049a | 2013-11-23 17:40:01 -0500 | [diff] [blame] | 189 | if (attrs) { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 190 | /* |
| 191 | * kernfs_node has non-default attributes get them from |
| 192 | * persistent copy in kernfs_node. |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 193 | */ |
Tejun Heo | 9a8049a | 2013-11-23 17:40:01 -0500 | [diff] [blame] | 194 | set_inode_attr(inode, &attrs->ia_iattr); |
| 195 | security_inode_notifysecctx(inode, attrs->ia_secdata, |
| 196 | attrs->ia_secdata_len); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 197 | } |
| 198 | |
Tejun Heo | df23fc3 | 2013-12-11 14:11:56 -0500 | [diff] [blame] | 199 | if (kernfs_type(kn) == KERNFS_DIR) |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 200 | set_nlink(inode, kn->dir.subdirs + 2); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 201 | } |
| 202 | |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 203 | int kernfs_iop_getattr(const struct path *path, struct kstat *stat, |
| 204 | u32 request_mask, unsigned int query_flags) |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 205 | { |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 206 | struct inode *inode = d_inode(path->dentry); |
Shaohua Li | 319ba91 | 2017-07-12 11:49:49 -0700 | [diff] [blame] | 207 | struct kernfs_node *kn = inode->i_private; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 208 | |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 209 | mutex_lock(&kernfs_mutex); |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 210 | kernfs_refresh_inode(kn, inode); |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 211 | mutex_unlock(&kernfs_mutex); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 212 | |
| 213 | generic_fillattr(inode, stat); |
| 214 | return 0; |
| 215 | } |
| 216 | |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 217 | static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode) |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 218 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 219 | kernfs_get(kn); |
| 220 | inode->i_private = kn; |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 221 | inode->i_mapping->a_ops = &kernfs_aops; |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 222 | inode->i_op = &kernfs_iops; |
Shaohua Li | c53cd49 | 2017-07-12 11:49:50 -0700 | [diff] [blame] | 223 | inode->i_generation = kn->id.generation; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 224 | |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 225 | set_default_inode_attr(inode, kn->mode); |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 226 | kernfs_refresh_inode(kn, inode); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 227 | |
| 228 | /* initialize inode according to type */ |
Tejun Heo | df23fc3 | 2013-12-11 14:11:56 -0500 | [diff] [blame] | 229 | switch (kernfs_type(kn)) { |
| 230 | case KERNFS_DIR: |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 231 | inode->i_op = &kernfs_dir_iops; |
| 232 | inode->i_fop = &kernfs_dir_fops; |
Eric W. Biederman | ea01521 | 2015-05-13 16:09:29 -0500 | [diff] [blame] | 233 | if (kn->flags & KERNFS_EMPTY_DIR) |
| 234 | make_empty_dir_inode(inode); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 235 | break; |
Tejun Heo | df23fc3 | 2013-12-11 14:11:56 -0500 | [diff] [blame] | 236 | case KERNFS_FILE: |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 237 | inode->i_size = kn->attr.size; |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 238 | inode->i_fop = &kernfs_file_fops; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 239 | break; |
Tejun Heo | df23fc3 | 2013-12-11 14:11:56 -0500 | [diff] [blame] | 240 | case KERNFS_LINK: |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 241 | inode->i_op = &kernfs_symlink_iops; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 242 | break; |
| 243 | default: |
| 244 | BUG(); |
| 245 | } |
| 246 | |
| 247 | unlock_new_inode(inode); |
| 248 | } |
| 249 | |
| 250 | /** |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 251 | * kernfs_get_inode - get inode for kernfs_node |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 252 | * @sb: super block |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 253 | * @kn: kernfs_node to allocate inode for |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 254 | * |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 255 | * Get inode for @kn. If such inode doesn't exist, a new inode is |
| 256 | * allocated and basics are initialized. New inode is returned |
| 257 | * locked. |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 258 | * |
| 259 | * LOCKING: |
| 260 | * Kernel thread context (may sleep). |
| 261 | * |
| 262 | * RETURNS: |
| 263 | * Pointer to allocated inode on success, NULL on failure. |
| 264 | */ |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 265 | 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] | 266 | { |
| 267 | struct inode *inode; |
| 268 | |
Shaohua Li | c53cd49 | 2017-07-12 11:49:50 -0700 | [diff] [blame] | 269 | inode = iget_locked(sb, kn->id.ino); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 270 | if (inode && (inode->i_state & I_NEW)) |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 271 | kernfs_init_inode(kn, inode); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 272 | |
| 273 | return inode; |
| 274 | } |
| 275 | |
| 276 | /* |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 277 | * The kernfs_node serves as both an inode and a directory entry for |
| 278 | * kernfs. To prevent the kernfs inode numbers from being freed |
| 279 | * 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] | 280 | * super_operations.evict_inode() implementation is needed to drop that |
| 281 | * reference upon inode destruction. |
| 282 | */ |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 283 | void kernfs_evict_inode(struct inode *inode) |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 284 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 285 | struct kernfs_node *kn = inode->i_private; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 286 | |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 287 | truncate_inode_pages_final(&inode->i_data); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 288 | clear_inode(inode); |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 289 | kernfs_put(kn); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 290 | } |
| 291 | |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 292 | int kernfs_iop_permission(struct inode *inode, int mask) |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 293 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 294 | struct kernfs_node *kn; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 295 | |
| 296 | if (mask & MAY_NOT_BLOCK) |
| 297 | return -ECHILD; |
| 298 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 299 | kn = inode->i_private; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 300 | |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 301 | mutex_lock(&kernfs_mutex); |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 302 | kernfs_refresh_inode(kn, inode); |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 303 | mutex_unlock(&kernfs_mutex); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 304 | |
| 305 | return generic_permission(inode, mask); |
| 306 | } |
Andreas Gruenbacher | e72a1a8 | 2016-09-29 17:48:33 +0200 | [diff] [blame] | 307 | |
| 308 | static int kernfs_xattr_get(const struct xattr_handler *handler, |
| 309 | struct dentry *unused, struct inode *inode, |
| 310 | const char *suffix, void *value, size_t size) |
| 311 | { |
| 312 | const char *name = xattr_full_name(handler, suffix); |
| 313 | struct kernfs_node *kn = inode->i_private; |
| 314 | struct kernfs_iattrs *attrs; |
| 315 | |
| 316 | attrs = kernfs_iattrs(kn); |
| 317 | if (!attrs) |
| 318 | return -ENOMEM; |
| 319 | |
| 320 | return simple_xattr_get(&attrs->xattrs, name, value, size); |
| 321 | } |
| 322 | |
| 323 | static int kernfs_xattr_set(const struct xattr_handler *handler, |
| 324 | struct dentry *unused, struct inode *inode, |
| 325 | const char *suffix, const void *value, |
| 326 | size_t size, int flags) |
| 327 | { |
| 328 | const char *name = xattr_full_name(handler, suffix); |
| 329 | struct kernfs_node *kn = inode->i_private; |
| 330 | struct kernfs_iattrs *attrs; |
| 331 | |
| 332 | attrs = kernfs_iattrs(kn); |
| 333 | if (!attrs) |
| 334 | return -ENOMEM; |
| 335 | |
| 336 | return simple_xattr_set(&attrs->xattrs, name, value, size, flags); |
| 337 | } |
| 338 | |
Bart Van Assche | b5a0623 | 2016-11-11 16:45:58 -0800 | [diff] [blame] | 339 | static const struct xattr_handler kernfs_trusted_xattr_handler = { |
Andreas Gruenbacher | e72a1a8 | 2016-09-29 17:48:33 +0200 | [diff] [blame] | 340 | .prefix = XATTR_TRUSTED_PREFIX, |
| 341 | .get = kernfs_xattr_get, |
| 342 | .set = kernfs_xattr_set, |
| 343 | }; |
| 344 | |
| 345 | static int kernfs_security_xattr_set(const struct xattr_handler *handler, |
| 346 | struct dentry *unused, struct inode *inode, |
| 347 | const char *suffix, const void *value, |
| 348 | size_t size, int flags) |
| 349 | { |
| 350 | struct kernfs_node *kn = inode->i_private; |
| 351 | struct kernfs_iattrs *attrs; |
| 352 | void *secdata; |
| 353 | u32 secdata_len = 0; |
| 354 | int error; |
| 355 | |
| 356 | attrs = kernfs_iattrs(kn); |
| 357 | if (!attrs) |
| 358 | return -ENOMEM; |
| 359 | |
| 360 | error = security_inode_setsecurity(inode, suffix, value, size, flags); |
| 361 | if (error) |
| 362 | return error; |
| 363 | error = security_inode_getsecctx(inode, &secdata, &secdata_len); |
| 364 | if (error) |
| 365 | return error; |
| 366 | |
| 367 | mutex_lock(&kernfs_mutex); |
| 368 | error = kernfs_node_setsecdata(attrs, &secdata, &secdata_len); |
| 369 | mutex_unlock(&kernfs_mutex); |
| 370 | |
| 371 | if (secdata) |
| 372 | security_release_secctx(secdata, secdata_len); |
| 373 | return error; |
| 374 | } |
| 375 | |
Bart Van Assche | b5a0623 | 2016-11-11 16:45:58 -0800 | [diff] [blame] | 376 | static const struct xattr_handler kernfs_security_xattr_handler = { |
Andreas Gruenbacher | e72a1a8 | 2016-09-29 17:48:33 +0200 | [diff] [blame] | 377 | .prefix = XATTR_SECURITY_PREFIX, |
| 378 | .get = kernfs_xattr_get, |
| 379 | .set = kernfs_security_xattr_set, |
| 380 | }; |
| 381 | |
| 382 | const struct xattr_handler *kernfs_xattr_handlers[] = { |
| 383 | &kernfs_trusted_xattr_handler, |
| 384 | &kernfs_security_xattr_handler, |
| 385 | NULL |
| 386 | }; |