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; |
| 57 | iattrs->ia_atime = iattrs->ia_mtime = iattrs->ia_ctime = CURRENT_TIME; |
| 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 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 66 | static 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 | { |
| 114 | struct inode *inode = dentry->d_inode; |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 115 | struct kernfs_node *kn = dentry->d_fsdata; |
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); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 122 | error = inode_change_ok(inode, iattr); |
| 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 | |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 138 | static int kernfs_node_setsecdata(struct kernfs_node *kn, void **secdata, |
| 139 | u32 *secdata_len) |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 140 | { |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 141 | struct kernfs_iattrs *attrs; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 142 | void *old_secdata; |
| 143 | size_t old_secdata_len; |
| 144 | |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 145 | attrs = kernfs_iattrs(kn); |
Tejun Heo | 9a8049a | 2013-11-23 17:40:01 -0500 | [diff] [blame] | 146 | if (!attrs) |
| 147 | return -ENOMEM; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 148 | |
Tejun Heo | 9a8049a | 2013-11-23 17:40:01 -0500 | [diff] [blame] | 149 | old_secdata = attrs->ia_secdata; |
| 150 | old_secdata_len = attrs->ia_secdata_len; |
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 | attrs->ia_secdata = *secdata; |
| 153 | attrs->ia_secdata_len = *secdata_len; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 154 | |
| 155 | *secdata = old_secdata; |
| 156 | *secdata_len = old_secdata_len; |
| 157 | return 0; |
| 158 | } |
| 159 | |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 160 | int kernfs_iop_setxattr(struct dentry *dentry, const char *name, |
| 161 | const void *value, size_t size, int flags) |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 162 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 163 | struct kernfs_node *kn = dentry->d_fsdata; |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 164 | struct kernfs_iattrs *attrs; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 165 | void *secdata; |
| 166 | int error; |
| 167 | u32 secdata_len = 0; |
| 168 | |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 169 | attrs = kernfs_iattrs(kn); |
Tejun Heo | 2322392 | 2013-11-23 17:40:02 -0500 | [diff] [blame] | 170 | if (!attrs) |
| 171 | return -ENOMEM; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 172 | |
| 173 | if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)) { |
| 174 | const char *suffix = name + XATTR_SECURITY_PREFIX_LEN; |
| 175 | error = security_inode_setsecurity(dentry->d_inode, suffix, |
| 176 | value, size, flags); |
| 177 | if (error) |
Tejun Heo | 2322392 | 2013-11-23 17:40:02 -0500 | [diff] [blame] | 178 | return error; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 179 | error = security_inode_getsecctx(dentry->d_inode, |
| 180 | &secdata, &secdata_len); |
| 181 | if (error) |
Tejun Heo | 2322392 | 2013-11-23 17:40:02 -0500 | [diff] [blame] | 182 | return error; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 183 | |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 184 | mutex_lock(&kernfs_mutex); |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 185 | error = kernfs_node_setsecdata(kn, &secdata, &secdata_len); |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 186 | mutex_unlock(&kernfs_mutex); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 187 | |
| 188 | if (secdata) |
| 189 | security_release_secctx(secdata, secdata_len); |
Tejun Heo | 2322392 | 2013-11-23 17:40:02 -0500 | [diff] [blame] | 190 | return error; |
| 191 | } else if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN)) { |
| 192 | return simple_xattr_set(&attrs->xattrs, name, value, size, |
| 193 | flags); |
| 194 | } |
| 195 | |
| 196 | return -EINVAL; |
| 197 | } |
| 198 | |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 199 | int kernfs_iop_removexattr(struct dentry *dentry, const char *name) |
Tejun Heo | 2322392 | 2013-11-23 17:40:02 -0500 | [diff] [blame] | 200 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 201 | struct kernfs_node *kn = dentry->d_fsdata; |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 202 | struct kernfs_iattrs *attrs; |
Tejun Heo | 2322392 | 2013-11-23 17:40:02 -0500 | [diff] [blame] | 203 | |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 204 | attrs = kernfs_iattrs(kn); |
Tejun Heo | 2322392 | 2013-11-23 17:40:02 -0500 | [diff] [blame] | 205 | if (!attrs) |
| 206 | return -ENOMEM; |
| 207 | |
| 208 | return simple_xattr_remove(&attrs->xattrs, name); |
| 209 | } |
| 210 | |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 211 | ssize_t kernfs_iop_getxattr(struct dentry *dentry, const char *name, void *buf, |
| 212 | size_t size) |
Tejun Heo | 2322392 | 2013-11-23 17:40:02 -0500 | [diff] [blame] | 213 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 214 | struct kernfs_node *kn = dentry->d_fsdata; |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 215 | struct kernfs_iattrs *attrs; |
Tejun Heo | 2322392 | 2013-11-23 17:40:02 -0500 | [diff] [blame] | 216 | |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 217 | attrs = kernfs_iattrs(kn); |
Tejun Heo | 2322392 | 2013-11-23 17:40:02 -0500 | [diff] [blame] | 218 | if (!attrs) |
| 219 | return -ENOMEM; |
| 220 | |
| 221 | return simple_xattr_get(&attrs->xattrs, name, buf, size); |
| 222 | } |
| 223 | |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 224 | 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] | 225 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 226 | struct kernfs_node *kn = dentry->d_fsdata; |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 227 | struct kernfs_iattrs *attrs; |
Tejun Heo | 2322392 | 2013-11-23 17:40:02 -0500 | [diff] [blame] | 228 | |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 229 | attrs = kernfs_iattrs(kn); |
Tejun Heo | 2322392 | 2013-11-23 17:40:02 -0500 | [diff] [blame] | 230 | if (!attrs) |
| 231 | return -ENOMEM; |
| 232 | |
| 233 | return simple_xattr_list(&attrs->xattrs, buf, size); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | static inline void set_default_inode_attr(struct inode *inode, umode_t mode) |
| 237 | { |
| 238 | inode->i_mode = mode; |
| 239 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 240 | } |
| 241 | |
| 242 | static inline void set_inode_attr(struct inode *inode, struct iattr *iattr) |
| 243 | { |
| 244 | inode->i_uid = iattr->ia_uid; |
| 245 | inode->i_gid = iattr->ia_gid; |
| 246 | inode->i_atime = iattr->ia_atime; |
| 247 | inode->i_mtime = iattr->ia_mtime; |
| 248 | inode->i_ctime = iattr->ia_ctime; |
| 249 | } |
| 250 | |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 251 | static void kernfs_refresh_inode(struct kernfs_node *kn, struct inode *inode) |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 252 | { |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 253 | struct kernfs_iattrs *attrs = kn->iattr; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 254 | |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 255 | inode->i_mode = kn->mode; |
Tejun Heo | 9a8049a | 2013-11-23 17:40:01 -0500 | [diff] [blame] | 256 | if (attrs) { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 257 | /* |
| 258 | * kernfs_node has non-default attributes get them from |
| 259 | * persistent copy in kernfs_node. |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 260 | */ |
Tejun Heo | 9a8049a | 2013-11-23 17:40:01 -0500 | [diff] [blame] | 261 | set_inode_attr(inode, &attrs->ia_iattr); |
| 262 | security_inode_notifysecctx(inode, attrs->ia_secdata, |
| 263 | attrs->ia_secdata_len); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 264 | } |
| 265 | |
Tejun Heo | df23fc3 | 2013-12-11 14:11:56 -0500 | [diff] [blame] | 266 | if (kernfs_type(kn) == KERNFS_DIR) |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 267 | set_nlink(inode, kn->dir.subdirs + 2); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 268 | } |
| 269 | |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 270 | int kernfs_iop_getattr(struct vfsmount *mnt, struct dentry *dentry, |
| 271 | struct kstat *stat) |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 272 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 273 | struct kernfs_node *kn = dentry->d_fsdata; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 274 | struct inode *inode = dentry->d_inode; |
| 275 | |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 276 | mutex_lock(&kernfs_mutex); |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 277 | kernfs_refresh_inode(kn, inode); |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 278 | mutex_unlock(&kernfs_mutex); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 279 | |
| 280 | generic_fillattr(inode, stat); |
| 281 | return 0; |
| 282 | } |
| 283 | |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 284 | static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode) |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 285 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 286 | kernfs_get(kn); |
| 287 | inode->i_private = kn; |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 288 | inode->i_mapping->a_ops = &kernfs_aops; |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 289 | inode->i_op = &kernfs_iops; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 290 | |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 291 | set_default_inode_attr(inode, kn->mode); |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 292 | kernfs_refresh_inode(kn, inode); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 293 | |
| 294 | /* initialize inode according to type */ |
Tejun Heo | df23fc3 | 2013-12-11 14:11:56 -0500 | [diff] [blame] | 295 | switch (kernfs_type(kn)) { |
| 296 | case KERNFS_DIR: |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 297 | inode->i_op = &kernfs_dir_iops; |
| 298 | inode->i_fop = &kernfs_dir_fops; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 299 | break; |
Tejun Heo | df23fc3 | 2013-12-11 14:11:56 -0500 | [diff] [blame] | 300 | case KERNFS_FILE: |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 301 | inode->i_size = kn->attr.size; |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 302 | inode->i_fop = &kernfs_file_fops; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 303 | break; |
Tejun Heo | df23fc3 | 2013-12-11 14:11:56 -0500 | [diff] [blame] | 304 | case KERNFS_LINK: |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 305 | inode->i_op = &kernfs_symlink_iops; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 306 | break; |
| 307 | default: |
| 308 | BUG(); |
| 309 | } |
| 310 | |
| 311 | unlock_new_inode(inode); |
| 312 | } |
| 313 | |
| 314 | /** |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 315 | * kernfs_get_inode - get inode for kernfs_node |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 316 | * @sb: super block |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 317 | * @kn: kernfs_node to allocate inode for |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 318 | * |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 319 | * Get inode for @kn. If such inode doesn't exist, a new inode is |
| 320 | * allocated and basics are initialized. New inode is returned |
| 321 | * locked. |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 322 | * |
| 323 | * LOCKING: |
| 324 | * Kernel thread context (may sleep). |
| 325 | * |
| 326 | * RETURNS: |
| 327 | * Pointer to allocated inode on success, NULL on failure. |
| 328 | */ |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 329 | 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] | 330 | { |
| 331 | struct inode *inode; |
| 332 | |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 333 | inode = iget_locked(sb, kn->ino); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 334 | if (inode && (inode->i_state & I_NEW)) |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 335 | kernfs_init_inode(kn, inode); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 336 | |
| 337 | return inode; |
| 338 | } |
| 339 | |
| 340 | /* |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 341 | * The kernfs_node serves as both an inode and a directory entry for |
| 342 | * kernfs. To prevent the kernfs inode numbers from being freed |
| 343 | * 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] | 344 | * super_operations.evict_inode() implementation is needed to drop that |
| 345 | * reference upon inode destruction. |
| 346 | */ |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 347 | void kernfs_evict_inode(struct inode *inode) |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 348 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 349 | struct kernfs_node *kn = inode->i_private; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 350 | |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 351 | truncate_inode_pages_final(&inode->i_data); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 352 | clear_inode(inode); |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 353 | kernfs_put(kn); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 354 | } |
| 355 | |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 356 | int kernfs_iop_permission(struct inode *inode, int mask) |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 357 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 358 | struct kernfs_node *kn; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 359 | |
| 360 | if (mask & MAY_NOT_BLOCK) |
| 361 | return -ECHILD; |
| 362 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 363 | kn = inode->i_private; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 364 | |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 365 | mutex_lock(&kernfs_mutex); |
Tejun Heo | c637b8a | 2013-12-11 14:11:58 -0500 | [diff] [blame] | 366 | kernfs_refresh_inode(kn, inode); |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame] | 367 | mutex_unlock(&kernfs_mutex); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 368 | |
| 369 | return generic_permission(inode, mask); |
| 370 | } |