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 | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 34 | .permission = sysfs_permission, |
| 35 | .setattr = sysfs_setattr, |
| 36 | .getattr = sysfs_getattr, |
| 37 | .setxattr = sysfs_setxattr, |
Tejun Heo | 2322392 | 2013-11-23 17:40:02 -0500 | [diff] [blame] | 38 | .removexattr = sysfs_removexattr, |
| 39 | .getxattr = sysfs_getxattr, |
| 40 | .listxattr = sysfs_listxattr, |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 41 | }; |
| 42 | |
Tejun Heo | 4b93dc9 | 2013-11-28 14:54:43 -0500 | [diff] [blame] | 43 | void __init sysfs_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 | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 51 | struct iattr *iattrs; |
| 52 | |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 53 | if (kn->iattr) |
| 54 | return kn->iattr; |
Tejun Heo | 9a8049a | 2013-11-23 17:40:01 -0500 | [diff] [blame] | 55 | |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 56 | kn->iattr = kzalloc(sizeof(struct kernfs_iattrs), GFP_KERNEL); |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 57 | if (!kn->iattr) |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 58 | return NULL; |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 59 | iattrs = &kn->iattr->ia_iattr; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 60 | |
| 61 | /* assign default attributes */ |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 62 | iattrs->ia_mode = kn->mode; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 63 | iattrs->ia_uid = GLOBAL_ROOT_UID; |
| 64 | iattrs->ia_gid = GLOBAL_ROOT_GID; |
| 65 | iattrs->ia_atime = iattrs->ia_mtime = iattrs->ia_ctime = CURRENT_TIME; |
| 66 | |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 67 | simple_xattrs_init(&kn->iattr->xattrs); |
Tejun Heo | 2322392 | 2013-11-23 17:40:02 -0500 | [diff] [blame] | 68 | |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 69 | return kn->iattr; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 70 | } |
| 71 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 72 | static int __kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr) |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 73 | { |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 74 | struct kernfs_iattrs *attrs; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 75 | struct iattr *iattrs; |
| 76 | unsigned int ia_valid = iattr->ia_valid; |
| 77 | |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 78 | attrs = kernfs_iattrs(kn); |
Tejun Heo | 9a8049a | 2013-11-23 17:40:01 -0500 | [diff] [blame] | 79 | if (!attrs) |
| 80 | return -ENOMEM; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 81 | |
Tejun Heo | 9a8049a | 2013-11-23 17:40:01 -0500 | [diff] [blame] | 82 | iattrs = &attrs->ia_iattr; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 83 | |
| 84 | if (ia_valid & ATTR_UID) |
| 85 | iattrs->ia_uid = iattr->ia_uid; |
| 86 | if (ia_valid & ATTR_GID) |
| 87 | iattrs->ia_gid = iattr->ia_gid; |
| 88 | if (ia_valid & ATTR_ATIME) |
| 89 | iattrs->ia_atime = iattr->ia_atime; |
| 90 | if (ia_valid & ATTR_MTIME) |
| 91 | iattrs->ia_mtime = iattr->ia_mtime; |
| 92 | if (ia_valid & ATTR_CTIME) |
| 93 | iattrs->ia_ctime = iattr->ia_ctime; |
| 94 | if (ia_valid & ATTR_MODE) { |
| 95 | umode_t mode = iattr->ia_mode; |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 96 | iattrs->ia_mode = kn->mode = mode; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 97 | } |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * kernfs_setattr - set iattr on a node |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 103 | * @kn: target node |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 104 | * @iattr: iattr to set |
| 105 | * |
| 106 | * Returns 0 on success, -errno on failure. |
| 107 | */ |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 108 | int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr) |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 109 | { |
| 110 | int ret; |
| 111 | |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame^] | 112 | mutex_lock(&kernfs_mutex); |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 113 | ret = __kernfs_setattr(kn, iattr); |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame^] | 114 | mutex_unlock(&kernfs_mutex); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 115 | return ret; |
| 116 | } |
| 117 | |
| 118 | int sysfs_setattr(struct dentry *dentry, struct iattr *iattr) |
| 119 | { |
| 120 | struct inode *inode = dentry->d_inode; |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 121 | struct kernfs_node *kn = dentry->d_fsdata; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 122 | int error; |
| 123 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 124 | if (!kn) |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 125 | return -EINVAL; |
| 126 | |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame^] | 127 | mutex_lock(&kernfs_mutex); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 128 | error = inode_change_ok(inode, iattr); |
| 129 | if (error) |
| 130 | goto out; |
| 131 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 132 | error = __kernfs_setattr(kn, iattr); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 133 | if (error) |
| 134 | goto out; |
| 135 | |
| 136 | /* this ignores size changes */ |
| 137 | setattr_copy(inode, iattr); |
| 138 | |
| 139 | out: |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame^] | 140 | mutex_unlock(&kernfs_mutex); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 141 | return error; |
| 142 | } |
| 143 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 144 | static int sysfs_sd_setsecdata(struct kernfs_node *kn, void **secdata, |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 145 | u32 *secdata_len) |
| 146 | { |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 147 | struct kernfs_iattrs *attrs; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 148 | void *old_secdata; |
| 149 | size_t old_secdata_len; |
| 150 | |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 151 | attrs = kernfs_iattrs(kn); |
Tejun Heo | 9a8049a | 2013-11-23 17:40:01 -0500 | [diff] [blame] | 152 | if (!attrs) |
| 153 | return -ENOMEM; |
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 | old_secdata = attrs->ia_secdata; |
| 156 | old_secdata_len = attrs->ia_secdata_len; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 157 | |
Tejun Heo | 9a8049a | 2013-11-23 17:40:01 -0500 | [diff] [blame] | 158 | attrs->ia_secdata = *secdata; |
| 159 | attrs->ia_secdata_len = *secdata_len; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 160 | |
| 161 | *secdata = old_secdata; |
| 162 | *secdata_len = old_secdata_len; |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | int sysfs_setxattr(struct dentry *dentry, const char *name, const void *value, |
| 167 | size_t size, int flags) |
| 168 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 169 | struct kernfs_node *kn = dentry->d_fsdata; |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 170 | struct kernfs_iattrs *attrs; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 171 | void *secdata; |
| 172 | int error; |
| 173 | u32 secdata_len = 0; |
| 174 | |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 175 | attrs = kernfs_iattrs(kn); |
Tejun Heo | 2322392 | 2013-11-23 17:40:02 -0500 | [diff] [blame] | 176 | if (!attrs) |
| 177 | return -ENOMEM; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 178 | |
| 179 | if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)) { |
| 180 | const char *suffix = name + XATTR_SECURITY_PREFIX_LEN; |
| 181 | error = security_inode_setsecurity(dentry->d_inode, suffix, |
| 182 | value, size, flags); |
| 183 | if (error) |
Tejun Heo | 2322392 | 2013-11-23 17:40:02 -0500 | [diff] [blame] | 184 | return error; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 185 | error = security_inode_getsecctx(dentry->d_inode, |
| 186 | &secdata, &secdata_len); |
| 187 | if (error) |
Tejun Heo | 2322392 | 2013-11-23 17:40:02 -0500 | [diff] [blame] | 188 | return error; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 189 | |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame^] | 190 | mutex_lock(&kernfs_mutex); |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 191 | error = sysfs_sd_setsecdata(kn, &secdata, &secdata_len); |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame^] | 192 | mutex_unlock(&kernfs_mutex); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 193 | |
| 194 | if (secdata) |
| 195 | security_release_secctx(secdata, secdata_len); |
Tejun Heo | 2322392 | 2013-11-23 17:40:02 -0500 | [diff] [blame] | 196 | return error; |
| 197 | } else if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN)) { |
| 198 | return simple_xattr_set(&attrs->xattrs, name, value, size, |
| 199 | flags); |
| 200 | } |
| 201 | |
| 202 | return -EINVAL; |
| 203 | } |
| 204 | |
| 205 | int sysfs_removexattr(struct dentry *dentry, const char *name) |
| 206 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 207 | struct kernfs_node *kn = dentry->d_fsdata; |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 208 | struct kernfs_iattrs *attrs; |
Tejun Heo | 2322392 | 2013-11-23 17:40:02 -0500 | [diff] [blame] | 209 | |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 210 | attrs = kernfs_iattrs(kn); |
Tejun Heo | 2322392 | 2013-11-23 17:40:02 -0500 | [diff] [blame] | 211 | if (!attrs) |
| 212 | return -ENOMEM; |
| 213 | |
| 214 | return simple_xattr_remove(&attrs->xattrs, name); |
| 215 | } |
| 216 | |
| 217 | ssize_t sysfs_getxattr(struct dentry *dentry, const char *name, void *buf, |
| 218 | size_t size) |
| 219 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 220 | struct kernfs_node *kn = dentry->d_fsdata; |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 221 | struct kernfs_iattrs *attrs; |
Tejun Heo | 2322392 | 2013-11-23 17:40:02 -0500 | [diff] [blame] | 222 | |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 223 | attrs = kernfs_iattrs(kn); |
Tejun Heo | 2322392 | 2013-11-23 17:40:02 -0500 | [diff] [blame] | 224 | if (!attrs) |
| 225 | return -ENOMEM; |
| 226 | |
| 227 | return simple_xattr_get(&attrs->xattrs, name, buf, size); |
| 228 | } |
| 229 | |
| 230 | ssize_t sysfs_listxattr(struct dentry *dentry, char *buf, size_t size) |
| 231 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 232 | struct kernfs_node *kn = dentry->d_fsdata; |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 233 | struct kernfs_iattrs *attrs; |
Tejun Heo | 2322392 | 2013-11-23 17:40:02 -0500 | [diff] [blame] | 234 | |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 235 | attrs = kernfs_iattrs(kn); |
Tejun Heo | 2322392 | 2013-11-23 17:40:02 -0500 | [diff] [blame] | 236 | if (!attrs) |
| 237 | return -ENOMEM; |
| 238 | |
| 239 | return simple_xattr_list(&attrs->xattrs, buf, size); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | static inline void set_default_inode_attr(struct inode *inode, umode_t mode) |
| 243 | { |
| 244 | inode->i_mode = mode; |
| 245 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 246 | } |
| 247 | |
| 248 | static inline void set_inode_attr(struct inode *inode, struct iattr *iattr) |
| 249 | { |
| 250 | inode->i_uid = iattr->ia_uid; |
| 251 | inode->i_gid = iattr->ia_gid; |
| 252 | inode->i_atime = iattr->ia_atime; |
| 253 | inode->i_mtime = iattr->ia_mtime; |
| 254 | inode->i_ctime = iattr->ia_ctime; |
| 255 | } |
| 256 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 257 | static void sysfs_refresh_inode(struct kernfs_node *kn, struct inode *inode) |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 258 | { |
Tejun Heo | c525aad | 2013-12-11 14:11:55 -0500 | [diff] [blame] | 259 | struct kernfs_iattrs *attrs = kn->iattr; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 260 | |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 261 | inode->i_mode = kn->mode; |
Tejun Heo | 9a8049a | 2013-11-23 17:40:01 -0500 | [diff] [blame] | 262 | if (attrs) { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 263 | /* |
| 264 | * kernfs_node has non-default attributes get them from |
| 265 | * persistent copy in kernfs_node. |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 266 | */ |
Tejun Heo | 9a8049a | 2013-11-23 17:40:01 -0500 | [diff] [blame] | 267 | set_inode_attr(inode, &attrs->ia_iattr); |
| 268 | security_inode_notifysecctx(inode, attrs->ia_secdata, |
| 269 | attrs->ia_secdata_len); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 270 | } |
| 271 | |
Tejun Heo | df23fc3 | 2013-12-11 14:11:56 -0500 | [diff] [blame] | 272 | if (kernfs_type(kn) == KERNFS_DIR) |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 273 | set_nlink(inode, kn->dir.subdirs + 2); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | int sysfs_getattr(struct vfsmount *mnt, struct dentry *dentry, |
| 277 | struct kstat *stat) |
| 278 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 279 | struct kernfs_node *kn = dentry->d_fsdata; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 280 | struct inode *inode = dentry->d_inode; |
| 281 | |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame^] | 282 | mutex_lock(&kernfs_mutex); |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 283 | sysfs_refresh_inode(kn, inode); |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame^] | 284 | mutex_unlock(&kernfs_mutex); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 285 | |
| 286 | generic_fillattr(inode, stat); |
| 287 | return 0; |
| 288 | } |
| 289 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 290 | static void sysfs_init_inode(struct kernfs_node *kn, struct inode *inode) |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 291 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 292 | kernfs_get(kn); |
| 293 | inode->i_private = kn; |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame^] | 294 | inode->i_mapping->a_ops = &kernfs_aops; |
| 295 | inode->i_mapping->backing_dev_info = &kernfs_bdi; |
| 296 | inode->i_op = &kernfs_iops; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 297 | |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 298 | set_default_inode_attr(inode, kn->mode); |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 299 | sysfs_refresh_inode(kn, inode); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 300 | |
| 301 | /* initialize inode according to type */ |
Tejun Heo | df23fc3 | 2013-12-11 14:11:56 -0500 | [diff] [blame] | 302 | switch (kernfs_type(kn)) { |
| 303 | case KERNFS_DIR: |
Tejun Heo | a797bfc | 2013-12-11 14:11:57 -0500 | [diff] [blame^] | 304 | inode->i_op = &kernfs_dir_iops; |
| 305 | inode->i_fop = &kernfs_dir_fops; |
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 | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 322 | * sysfs_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 | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 336 | struct inode *sysfs_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 | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 342 | sysfs_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 | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 348 | * The kernfs_node serves as both an inode and a directory entry for sysfs. |
| 349 | * To prevent the sysfs inode numbers from being freed prematurely we take |
| 350 | * a reference to kernfs_node from the sysfs 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 | */ |
| 354 | void sysfs_evict_inode(struct inode *inode) |
| 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 | |
| 358 | truncate_inode_pages(&inode->i_data, 0); |
| 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 | |
| 363 | int sysfs_permission(struct inode *inode, int mask) |
| 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 | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 373 | sysfs_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 | } |