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