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