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 | |
| 21 | static const struct address_space_operations sysfs_aops = { |
| 22 | .readpage = simple_readpage, |
| 23 | .write_begin = simple_write_begin, |
| 24 | .write_end = simple_write_end, |
| 25 | }; |
| 26 | |
| 27 | static struct backing_dev_info sysfs_backing_dev_info = { |
| 28 | .name = "sysfs", |
| 29 | .ra_pages = 0, /* No readahead */ |
| 30 | .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK, |
| 31 | }; |
| 32 | |
| 33 | static const struct inode_operations sysfs_inode_operations = { |
| 34 | .permission = sysfs_permission, |
| 35 | .setattr = sysfs_setattr, |
| 36 | .getattr = sysfs_getattr, |
| 37 | .setxattr = sysfs_setxattr, |
| 38 | }; |
| 39 | |
Tejun Heo | 4b93dc9 | 2013-11-28 14:54:43 -0500 | [diff] [blame] | 40 | void __init sysfs_inode_init(void) |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 41 | { |
Tejun Heo | 4b93dc9 | 2013-11-28 14:54:43 -0500 | [diff] [blame] | 42 | if (bdi_init(&sysfs_backing_dev_info)) |
| 43 | panic("failed to init sysfs_backing_dev_info"); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 44 | } |
| 45 | |
Tejun Heo | 9a8049a | 2013-11-23 17:40:01 -0500 | [diff] [blame^] | 46 | static struct sysfs_inode_attrs *sysfs_inode_attrs(struct sysfs_dirent *sd) |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 47 | { |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 48 | struct iattr *iattrs; |
| 49 | |
Tejun Heo | 9a8049a | 2013-11-23 17:40:01 -0500 | [diff] [blame^] | 50 | if (sd->s_iattr) |
| 51 | return sd->s_iattr; |
| 52 | |
| 53 | sd->s_iattr = kzalloc(sizeof(struct sysfs_inode_attrs), GFP_KERNEL); |
| 54 | if (!sd->s_iattr) |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 55 | return NULL; |
Tejun Heo | 9a8049a | 2013-11-23 17:40:01 -0500 | [diff] [blame^] | 56 | iattrs = &sd->s_iattr->ia_iattr; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 57 | |
| 58 | /* assign default attributes */ |
| 59 | iattrs->ia_mode = sd->s_mode; |
| 60 | iattrs->ia_uid = GLOBAL_ROOT_UID; |
| 61 | iattrs->ia_gid = GLOBAL_ROOT_GID; |
| 62 | iattrs->ia_atime = iattrs->ia_mtime = iattrs->ia_ctime = CURRENT_TIME; |
| 63 | |
Tejun Heo | 9a8049a | 2013-11-23 17:40:01 -0500 | [diff] [blame^] | 64 | return sd->s_iattr; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | static int __kernfs_setattr(struct sysfs_dirent *sd, const struct iattr *iattr) |
| 68 | { |
Tejun Heo | 9a8049a | 2013-11-23 17:40:01 -0500 | [diff] [blame^] | 69 | struct sysfs_inode_attrs *attrs; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 70 | struct iattr *iattrs; |
| 71 | unsigned int ia_valid = iattr->ia_valid; |
| 72 | |
Tejun Heo | 9a8049a | 2013-11-23 17:40:01 -0500 | [diff] [blame^] | 73 | attrs = sysfs_inode_attrs(sd); |
| 74 | if (!attrs) |
| 75 | return -ENOMEM; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 76 | |
Tejun Heo | 9a8049a | 2013-11-23 17:40:01 -0500 | [diff] [blame^] | 77 | iattrs = &attrs->ia_iattr; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 78 | |
| 79 | if (ia_valid & ATTR_UID) |
| 80 | iattrs->ia_uid = iattr->ia_uid; |
| 81 | if (ia_valid & ATTR_GID) |
| 82 | iattrs->ia_gid = iattr->ia_gid; |
| 83 | if (ia_valid & ATTR_ATIME) |
| 84 | iattrs->ia_atime = iattr->ia_atime; |
| 85 | if (ia_valid & ATTR_MTIME) |
| 86 | iattrs->ia_mtime = iattr->ia_mtime; |
| 87 | if (ia_valid & ATTR_CTIME) |
| 88 | iattrs->ia_ctime = iattr->ia_ctime; |
| 89 | if (ia_valid & ATTR_MODE) { |
| 90 | umode_t mode = iattr->ia_mode; |
| 91 | iattrs->ia_mode = sd->s_mode = mode; |
| 92 | } |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * kernfs_setattr - set iattr on a node |
| 98 | * @sd: target node |
| 99 | * @iattr: iattr to set |
| 100 | * |
| 101 | * Returns 0 on success, -errno on failure. |
| 102 | */ |
| 103 | int kernfs_setattr(struct sysfs_dirent *sd, const struct iattr *iattr) |
| 104 | { |
| 105 | int ret; |
| 106 | |
| 107 | mutex_lock(&sysfs_mutex); |
| 108 | ret = __kernfs_setattr(sd, iattr); |
| 109 | mutex_unlock(&sysfs_mutex); |
| 110 | return ret; |
| 111 | } |
| 112 | |
| 113 | int sysfs_setattr(struct dentry *dentry, struct iattr *iattr) |
| 114 | { |
| 115 | struct inode *inode = dentry->d_inode; |
| 116 | struct sysfs_dirent *sd = dentry->d_fsdata; |
| 117 | int error; |
| 118 | |
| 119 | if (!sd) |
| 120 | return -EINVAL; |
| 121 | |
| 122 | mutex_lock(&sysfs_mutex); |
| 123 | error = inode_change_ok(inode, iattr); |
| 124 | if (error) |
| 125 | goto out; |
| 126 | |
| 127 | error = __kernfs_setattr(sd, iattr); |
| 128 | if (error) |
| 129 | goto out; |
| 130 | |
| 131 | /* this ignores size changes */ |
| 132 | setattr_copy(inode, iattr); |
| 133 | |
| 134 | out: |
| 135 | mutex_unlock(&sysfs_mutex); |
| 136 | return error; |
| 137 | } |
| 138 | |
| 139 | static int sysfs_sd_setsecdata(struct sysfs_dirent *sd, void **secdata, |
| 140 | u32 *secdata_len) |
| 141 | { |
Tejun Heo | 9a8049a | 2013-11-23 17:40:01 -0500 | [diff] [blame^] | 142 | struct sysfs_inode_attrs *attrs; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 143 | void *old_secdata; |
| 144 | size_t old_secdata_len; |
| 145 | |
Tejun Heo | 9a8049a | 2013-11-23 17:40:01 -0500 | [diff] [blame^] | 146 | attrs = sysfs_inode_attrs(sd); |
| 147 | if (!attrs) |
| 148 | return -ENOMEM; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 149 | |
Tejun Heo | 9a8049a | 2013-11-23 17:40:01 -0500 | [diff] [blame^] | 150 | old_secdata = attrs->ia_secdata; |
| 151 | old_secdata_len = attrs->ia_secdata_len; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 152 | |
Tejun Heo | 9a8049a | 2013-11-23 17:40:01 -0500 | [diff] [blame^] | 153 | attrs->ia_secdata = *secdata; |
| 154 | attrs->ia_secdata_len = *secdata_len; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 155 | |
| 156 | *secdata = old_secdata; |
| 157 | *secdata_len = old_secdata_len; |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | int sysfs_setxattr(struct dentry *dentry, const char *name, const void *value, |
| 162 | size_t size, int flags) |
| 163 | { |
| 164 | struct sysfs_dirent *sd = dentry->d_fsdata; |
| 165 | void *secdata; |
| 166 | int error; |
| 167 | u32 secdata_len = 0; |
| 168 | |
| 169 | if (!sd) |
| 170 | return -EINVAL; |
| 171 | |
| 172 | if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)) { |
| 173 | const char *suffix = name + XATTR_SECURITY_PREFIX_LEN; |
| 174 | error = security_inode_setsecurity(dentry->d_inode, suffix, |
| 175 | value, size, flags); |
| 176 | if (error) |
| 177 | goto out; |
| 178 | error = security_inode_getsecctx(dentry->d_inode, |
| 179 | &secdata, &secdata_len); |
| 180 | if (error) |
| 181 | goto out; |
| 182 | |
| 183 | mutex_lock(&sysfs_mutex); |
| 184 | error = sysfs_sd_setsecdata(sd, &secdata, &secdata_len); |
| 185 | mutex_unlock(&sysfs_mutex); |
| 186 | |
| 187 | if (secdata) |
| 188 | security_release_secctx(secdata, secdata_len); |
| 189 | } else |
| 190 | return -EINVAL; |
| 191 | out: |
| 192 | return error; |
| 193 | } |
| 194 | |
| 195 | static inline void set_default_inode_attr(struct inode *inode, umode_t mode) |
| 196 | { |
| 197 | inode->i_mode = mode; |
| 198 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 199 | } |
| 200 | |
| 201 | static inline void set_inode_attr(struct inode *inode, struct iattr *iattr) |
| 202 | { |
| 203 | inode->i_uid = iattr->ia_uid; |
| 204 | inode->i_gid = iattr->ia_gid; |
| 205 | inode->i_atime = iattr->ia_atime; |
| 206 | inode->i_mtime = iattr->ia_mtime; |
| 207 | inode->i_ctime = iattr->ia_ctime; |
| 208 | } |
| 209 | |
| 210 | static void sysfs_refresh_inode(struct sysfs_dirent *sd, struct inode *inode) |
| 211 | { |
Tejun Heo | 9a8049a | 2013-11-23 17:40:01 -0500 | [diff] [blame^] | 212 | struct sysfs_inode_attrs *attrs = sd->s_iattr; |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 213 | |
| 214 | inode->i_mode = sd->s_mode; |
Tejun Heo | 9a8049a | 2013-11-23 17:40:01 -0500 | [diff] [blame^] | 215 | if (attrs) { |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 216 | /* sysfs_dirent has non-default attributes |
| 217 | * get them from persistent copy in sysfs_dirent |
| 218 | */ |
Tejun Heo | 9a8049a | 2013-11-23 17:40:01 -0500 | [diff] [blame^] | 219 | set_inode_attr(inode, &attrs->ia_iattr); |
| 220 | security_inode_notifysecctx(inode, attrs->ia_secdata, |
| 221 | attrs->ia_secdata_len); |
Tejun Heo | ffed24e | 2013-11-28 14:54:32 -0500 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | if (sysfs_type(sd) == SYSFS_DIR) |
| 225 | set_nlink(inode, sd->s_dir.subdirs + 2); |
| 226 | } |
| 227 | |
| 228 | int sysfs_getattr(struct vfsmount *mnt, struct dentry *dentry, |
| 229 | struct kstat *stat) |
| 230 | { |
| 231 | struct sysfs_dirent *sd = dentry->d_fsdata; |
| 232 | struct inode *inode = dentry->d_inode; |
| 233 | |
| 234 | mutex_lock(&sysfs_mutex); |
| 235 | sysfs_refresh_inode(sd, inode); |
| 236 | mutex_unlock(&sysfs_mutex); |
| 237 | |
| 238 | generic_fillattr(inode, stat); |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | static void sysfs_init_inode(struct sysfs_dirent *sd, struct inode *inode) |
| 243 | { |
| 244 | kernfs_get(sd); |
| 245 | inode->i_private = sd; |
| 246 | inode->i_mapping->a_ops = &sysfs_aops; |
| 247 | inode->i_mapping->backing_dev_info = &sysfs_backing_dev_info; |
| 248 | inode->i_op = &sysfs_inode_operations; |
| 249 | |
| 250 | set_default_inode_attr(inode, sd->s_mode); |
| 251 | sysfs_refresh_inode(sd, inode); |
| 252 | |
| 253 | /* initialize inode according to type */ |
| 254 | switch (sysfs_type(sd)) { |
| 255 | case SYSFS_DIR: |
| 256 | inode->i_op = &sysfs_dir_inode_operations; |
| 257 | inode->i_fop = &sysfs_dir_operations; |
| 258 | break; |
| 259 | case SYSFS_KOBJ_ATTR: |
| 260 | inode->i_size = sd->s_attr.size; |
| 261 | inode->i_fop = &kernfs_file_operations; |
| 262 | break; |
| 263 | case SYSFS_KOBJ_LINK: |
| 264 | inode->i_op = &sysfs_symlink_inode_operations; |
| 265 | break; |
| 266 | default: |
| 267 | BUG(); |
| 268 | } |
| 269 | |
| 270 | unlock_new_inode(inode); |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * sysfs_get_inode - get inode for sysfs_dirent |
| 275 | * @sb: super block |
| 276 | * @sd: sysfs_dirent to allocate inode for |
| 277 | * |
| 278 | * Get inode for @sd. If such inode doesn't exist, a new inode |
| 279 | * is allocated and basics are initialized. New inode is |
| 280 | * returned locked. |
| 281 | * |
| 282 | * LOCKING: |
| 283 | * Kernel thread context (may sleep). |
| 284 | * |
| 285 | * RETURNS: |
| 286 | * Pointer to allocated inode on success, NULL on failure. |
| 287 | */ |
| 288 | struct inode *sysfs_get_inode(struct super_block *sb, struct sysfs_dirent *sd) |
| 289 | { |
| 290 | struct inode *inode; |
| 291 | |
| 292 | inode = iget_locked(sb, sd->s_ino); |
| 293 | if (inode && (inode->i_state & I_NEW)) |
| 294 | sysfs_init_inode(sd, inode); |
| 295 | |
| 296 | return inode; |
| 297 | } |
| 298 | |
| 299 | /* |
| 300 | * The sysfs_dirent serves as both an inode and a directory entry for sysfs. |
| 301 | * To prevent the sysfs inode numbers from being freed prematurely we take a |
| 302 | * reference to sysfs_dirent from the sysfs inode. A |
| 303 | * super_operations.evict_inode() implementation is needed to drop that |
| 304 | * reference upon inode destruction. |
| 305 | */ |
| 306 | void sysfs_evict_inode(struct inode *inode) |
| 307 | { |
| 308 | struct sysfs_dirent *sd = inode->i_private; |
| 309 | |
| 310 | truncate_inode_pages(&inode->i_data, 0); |
| 311 | clear_inode(inode); |
| 312 | kernfs_put(sd); |
| 313 | } |
| 314 | |
| 315 | int sysfs_permission(struct inode *inode, int mask) |
| 316 | { |
| 317 | struct sysfs_dirent *sd; |
| 318 | |
| 319 | if (mask & MAY_NOT_BLOCK) |
| 320 | return -ECHILD; |
| 321 | |
| 322 | sd = inode->i_private; |
| 323 | |
| 324 | mutex_lock(&sysfs_mutex); |
| 325 | sysfs_refresh_inode(sd, inode); |
| 326 | mutex_unlock(&sysfs_mutex); |
| 327 | |
| 328 | return generic_permission(inode, mask); |
| 329 | } |