Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Tejun Heo | 6d66f5c | 2007-09-20 17:31:38 +0900 | [diff] [blame] | 2 | * fs/sysfs/inode.c - basic sysfs inode and dentry operations |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
Tejun Heo | 6d66f5c | 2007-09-20 17:31:38 +0900 | [diff] [blame] | 4 | * Copyright (c) 2001-3 Patrick Mochel |
| 5 | * Copyright (c) 2007 SUSE Linux Products GmbH |
| 6 | * Copyright (c) 2007 Tejun Heo <teheo@suse.de> |
| 7 | * |
| 8 | * This file is released under the GPLv2. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * |
| 10 | * Please see Documentation/filesystems/sysfs.txt for more information. |
| 11 | */ |
| 12 | |
| 13 | #undef DEBUG |
| 14 | |
| 15 | #include <linux/pagemap.h> |
| 16 | #include <linux/namei.h> |
| 17 | #include <linux/backing-dev.h> |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 18 | #include <linux/capability.h> |
Randy.Dunlap | 995982c | 2006-07-10 23:05:25 -0700 | [diff] [blame] | 19 | #include <linux/errno.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 20 | #include <linux/sched.h> |
David P. Quigley | ddd29ec | 2009-09-09 14:25:37 -0400 | [diff] [blame] | 21 | #include <linux/xattr.h> |
| 22 | #include <linux/security.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include "sysfs.h" |
| 24 | |
| 25 | extern struct super_block * sysfs_sb; |
| 26 | |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 27 | static const struct address_space_operations sysfs_aops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | .readpage = simple_readpage, |
Nick Piggin | 800d15a | 2007-10-16 01:25:03 -0700 | [diff] [blame] | 29 | .write_begin = simple_write_begin, |
| 30 | .write_end = simple_write_end, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | static struct backing_dev_info sysfs_backing_dev_info = { |
Jens Axboe | d993831 | 2009-06-12 14:45:52 +0200 | [diff] [blame] | 34 | .name = "sysfs", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | .ra_pages = 0, /* No readahead */ |
Miklos Szeredi | e4ad08f | 2008-04-30 00:54:37 -0700 | [diff] [blame] | 36 | .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | }; |
| 38 | |
Arjan van de Ven | c5ef1c4 | 2007-02-12 00:55:40 -0800 | [diff] [blame] | 39 | static const struct inode_operations sysfs_inode_operations ={ |
Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 40 | .setattr = sysfs_setattr, |
David P. Quigley | ddd29ec | 2009-09-09 14:25:37 -0400 | [diff] [blame] | 41 | .setxattr = sysfs_setxattr, |
Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 42 | }; |
| 43 | |
Peter Zijlstra | e0bf68d | 2007-10-16 23:25:46 -0700 | [diff] [blame] | 44 | int __init sysfs_inode_init(void) |
| 45 | { |
| 46 | return bdi_init(&sysfs_backing_dev_info); |
| 47 | } |
| 48 | |
David P. Quigley | ddd29ec | 2009-09-09 14:25:37 -0400 | [diff] [blame] | 49 | struct sysfs_inode_attrs *sysfs_init_inode_attrs(struct sysfs_dirent *sd) |
| 50 | { |
| 51 | struct sysfs_inode_attrs *attrs; |
| 52 | struct iattr *iattrs; |
| 53 | |
| 54 | attrs = kzalloc(sizeof(struct sysfs_inode_attrs), GFP_KERNEL); |
| 55 | if (!attrs) |
| 56 | return NULL; |
| 57 | iattrs = &attrs->ia_iattr; |
| 58 | |
| 59 | /* assign default attributes */ |
| 60 | iattrs->ia_mode = sd->s_mode; |
| 61 | iattrs->ia_uid = 0; |
| 62 | iattrs->ia_gid = 0; |
| 63 | iattrs->ia_atime = iattrs->ia_mtime = iattrs->ia_ctime = CURRENT_TIME; |
| 64 | |
| 65 | return attrs; |
| 66 | } |
Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 67 | int sysfs_setattr(struct dentry * dentry, struct iattr * iattr) |
| 68 | { |
| 69 | struct inode * inode = dentry->d_inode; |
| 70 | struct sysfs_dirent * sd = dentry->d_fsdata; |
David P. Quigley | ddd29ec | 2009-09-09 14:25:37 -0400 | [diff] [blame] | 71 | struct sysfs_inode_attrs *sd_attrs; |
| 72 | struct iattr *iattrs; |
Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 73 | unsigned int ia_valid = iattr->ia_valid; |
| 74 | int error; |
| 75 | |
| 76 | if (!sd) |
| 77 | return -EINVAL; |
| 78 | |
David P. Quigley | ddd29ec | 2009-09-09 14:25:37 -0400 | [diff] [blame] | 79 | sd_attrs = sd->s_iattr; |
Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 80 | |
| 81 | error = inode_change_ok(inode, iattr); |
| 82 | if (error) |
| 83 | return error; |
| 84 | |
Ben Hutchings | 40a2159 | 2008-04-28 15:59:58 +0100 | [diff] [blame] | 85 | iattr->ia_valid &= ~ATTR_SIZE; /* ignore size changes */ |
| 86 | |
Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 87 | error = inode_setattr(inode, iattr); |
| 88 | if (error) |
| 89 | return error; |
| 90 | |
David P. Quigley | ddd29ec | 2009-09-09 14:25:37 -0400 | [diff] [blame] | 91 | if (!sd_attrs) { |
Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 92 | /* setting attributes for the first time, allocate now */ |
David P. Quigley | ddd29ec | 2009-09-09 14:25:37 -0400 | [diff] [blame] | 93 | sd_attrs = sysfs_init_inode_attrs(sd); |
| 94 | if (!sd_attrs) |
Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 95 | return -ENOMEM; |
David P. Quigley | ddd29ec | 2009-09-09 14:25:37 -0400 | [diff] [blame] | 96 | sd->s_iattr = sd_attrs; |
| 97 | } else { |
| 98 | /* attributes were changed at least once in past */ |
| 99 | iattrs = &sd_attrs->ia_iattr; |
| 100 | |
| 101 | if (ia_valid & ATTR_UID) |
| 102 | iattrs->ia_uid = iattr->ia_uid; |
| 103 | if (ia_valid & ATTR_GID) |
| 104 | iattrs->ia_gid = iattr->ia_gid; |
| 105 | if (ia_valid & ATTR_ATIME) |
| 106 | iattrs->ia_atime = timespec_trunc(iattr->ia_atime, |
| 107 | inode->i_sb->s_time_gran); |
| 108 | if (ia_valid & ATTR_MTIME) |
| 109 | iattrs->ia_mtime = timespec_trunc(iattr->ia_mtime, |
| 110 | inode->i_sb->s_time_gran); |
| 111 | if (ia_valid & ATTR_CTIME) |
| 112 | iattrs->ia_ctime = timespec_trunc(iattr->ia_ctime, |
| 113 | inode->i_sb->s_time_gran); |
| 114 | if (ia_valid & ATTR_MODE) { |
| 115 | umode_t mode = iattr->ia_mode; |
| 116 | |
| 117 | if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID)) |
| 118 | mode &= ~S_ISGID; |
| 119 | iattrs->ia_mode = sd->s_mode = mode; |
| 120 | } |
Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 121 | } |
David P. Quigley | ddd29ec | 2009-09-09 14:25:37 -0400 | [diff] [blame] | 122 | return error; |
| 123 | } |
Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 124 | |
David P. Quigley | ddd29ec | 2009-09-09 14:25:37 -0400 | [diff] [blame] | 125 | int sysfs_setxattr(struct dentry *dentry, const char *name, const void *value, |
| 126 | size_t size, int flags) |
| 127 | { |
| 128 | struct sysfs_dirent *sd = dentry->d_fsdata; |
| 129 | struct sysfs_inode_attrs *iattrs; |
| 130 | void *secdata; |
| 131 | int error; |
| 132 | u32 secdata_len = 0; |
Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 133 | |
David P. Quigley | ddd29ec | 2009-09-09 14:25:37 -0400 | [diff] [blame] | 134 | if (!sd) |
| 135 | return -EINVAL; |
| 136 | if (!sd->s_iattr) |
| 137 | sd->s_iattr = sysfs_init_inode_attrs(sd); |
| 138 | if (!sd->s_iattr) |
| 139 | return -ENOMEM; |
Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 140 | |
David P. Quigley | ddd29ec | 2009-09-09 14:25:37 -0400 | [diff] [blame] | 141 | iattrs = sd->s_iattr; |
Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 142 | |
David P. Quigley | ddd29ec | 2009-09-09 14:25:37 -0400 | [diff] [blame] | 143 | if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)) { |
| 144 | const char *suffix = name + XATTR_SECURITY_PREFIX_LEN; |
| 145 | error = security_inode_setsecurity(dentry->d_inode, suffix, |
| 146 | value, size, flags); |
| 147 | if (error) |
| 148 | goto out; |
| 149 | error = security_inode_getsecctx(dentry->d_inode, |
| 150 | &secdata, &secdata_len); |
| 151 | if (error) |
| 152 | goto out; |
| 153 | if (iattrs->ia_secdata) |
| 154 | security_release_secctx(iattrs->ia_secdata, |
| 155 | iattrs->ia_secdata_len); |
| 156 | iattrs->ia_secdata = secdata; |
| 157 | iattrs->ia_secdata_len = secdata_len; |
| 158 | |
| 159 | } else |
| 160 | return -EINVAL; |
| 161 | out: |
Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 162 | return error; |
| 163 | } |
| 164 | |
Maneesh Soni | 8215534 | 2005-05-31 10:39:52 +0530 | [diff] [blame] | 165 | static inline void set_default_inode_attr(struct inode * inode, mode_t mode) |
| 166 | { |
| 167 | inode->i_mode = mode; |
Maneesh Soni | 8215534 | 2005-05-31 10:39:52 +0530 | [diff] [blame] | 168 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 169 | } |
| 170 | |
| 171 | static inline void set_inode_attr(struct inode * inode, struct iattr * iattr) |
| 172 | { |
| 173 | inode->i_mode = iattr->ia_mode; |
| 174 | inode->i_uid = iattr->ia_uid; |
| 175 | inode->i_gid = iattr->ia_gid; |
| 176 | inode->i_atime = iattr->ia_atime; |
| 177 | inode->i_mtime = iattr->ia_mtime; |
| 178 | inode->i_ctime = iattr->ia_ctime; |
| 179 | } |
| 180 | |
Arjan van de Ven | 232ba9d | 2006-07-12 09:03:06 -0700 | [diff] [blame] | 181 | |
| 182 | /* |
| 183 | * sysfs has a different i_mutex lock order behavior for i_mutex than other |
| 184 | * filesystems; sysfs i_mutex is called in many places with subsystem locks |
| 185 | * held. At the same time, many of the VFS locking rules do not apply to |
| 186 | * sysfs at all (cross directory rename for example). To untangle this mess |
| 187 | * (which gives false positives in lockdep), we're giving sysfs inodes their |
| 188 | * own class for i_mutex. |
| 189 | */ |
| 190 | static struct lock_class_key sysfs_inode_imutex_key; |
| 191 | |
Eric W. Biederman | 372e88b | 2007-08-20 21:36:29 +0900 | [diff] [blame] | 192 | static int sysfs_count_nlink(struct sysfs_dirent *sd) |
| 193 | { |
| 194 | struct sysfs_dirent *child; |
| 195 | int nr = 0; |
| 196 | |
Tejun Heo | bc747f3 | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 197 | for (child = sd->s_dir.children; child; child = child->s_sibling) |
Eric W. Biederman | 372e88b | 2007-08-20 21:36:29 +0900 | [diff] [blame] | 198 | if (sysfs_type(child) == SYSFS_DIR) |
| 199 | nr++; |
| 200 | |
| 201 | return nr + 2; |
| 202 | } |
| 203 | |
Tejun Heo | bc37e28 | 2007-07-18 14:30:28 +0900 | [diff] [blame] | 204 | static void sysfs_init_inode(struct sysfs_dirent *sd, struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | { |
Eric W. Biederman | 372e88b | 2007-08-20 21:36:29 +0900 | [diff] [blame] | 206 | struct bin_attribute *bin_attr; |
David P. Quigley | ddd29ec | 2009-09-09 14:25:37 -0400 | [diff] [blame] | 207 | struct sysfs_inode_attrs *iattrs; |
Eric W. Biederman | 372e88b | 2007-08-20 21:36:29 +0900 | [diff] [blame] | 208 | |
Eric W. Biederman | 04256b4 | 2009-02-11 13:20:23 -0800 | [diff] [blame] | 209 | inode->i_private = sysfs_get(sd); |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 210 | inode->i_mapping->a_ops = &sysfs_aops; |
| 211 | inode->i_mapping->backing_dev_info = &sysfs_backing_dev_info; |
| 212 | inode->i_op = &sysfs_inode_operations; |
| 213 | inode->i_ino = sd->s_ino; |
| 214 | lockdep_set_class(&inode->i_mutex, &sysfs_inode_imutex_key); |
Maneesh Soni | 8215534 | 2005-05-31 10:39:52 +0530 | [diff] [blame] | 215 | |
David P. Quigley | ddd29ec | 2009-09-09 14:25:37 -0400 | [diff] [blame] | 216 | iattrs = sd->s_iattr; |
| 217 | if (iattrs) { |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 218 | /* sysfs_dirent has non-default attributes |
| 219 | * get them for the new inode from persistent copy |
| 220 | * in sysfs_dirent |
| 221 | */ |
David P. Quigley | ddd29ec | 2009-09-09 14:25:37 -0400 | [diff] [blame] | 222 | set_inode_attr(inode, &iattrs->ia_iattr); |
| 223 | if (iattrs->ia_secdata) |
| 224 | security_inode_notifysecctx(inode, |
| 225 | iattrs->ia_secdata, |
| 226 | iattrs->ia_secdata_len); |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 227 | } else |
| 228 | set_default_inode_attr(inode, sd->s_mode); |
Eric W. Biederman | 372e88b | 2007-08-20 21:36:29 +0900 | [diff] [blame] | 229 | |
Eric W. Biederman | 372e88b | 2007-08-20 21:36:29 +0900 | [diff] [blame] | 230 | /* initialize inode according to type */ |
| 231 | switch (sysfs_type(sd)) { |
Eric W. Biederman | 372e88b | 2007-08-20 21:36:29 +0900 | [diff] [blame] | 232 | case SYSFS_DIR: |
| 233 | inode->i_op = &sysfs_dir_inode_operations; |
| 234 | inode->i_fop = &sysfs_dir_operations; |
| 235 | inode->i_nlink = sysfs_count_nlink(sd); |
| 236 | break; |
| 237 | case SYSFS_KOBJ_ATTR: |
| 238 | inode->i_size = PAGE_SIZE; |
| 239 | inode->i_fop = &sysfs_file_operations; |
| 240 | break; |
| 241 | case SYSFS_KOBJ_BIN_ATTR: |
Tejun Heo | b1fc3d6 | 2007-09-20 16:05:11 +0900 | [diff] [blame] | 242 | bin_attr = sd->s_bin_attr.bin_attr; |
Eric W. Biederman | 372e88b | 2007-08-20 21:36:29 +0900 | [diff] [blame] | 243 | inode->i_size = bin_attr->size; |
| 244 | inode->i_fop = &bin_fops; |
| 245 | break; |
| 246 | case SYSFS_KOBJ_LINK: |
| 247 | inode->i_op = &sysfs_symlink_inode_operations; |
| 248 | break; |
| 249 | default: |
| 250 | BUG(); |
| 251 | } |
| 252 | |
| 253 | unlock_new_inode(inode); |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /** |
Tejun Heo | 8312a8d | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 257 | * sysfs_get_inode - get inode for sysfs_dirent |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 258 | * @sd: sysfs_dirent to allocate inode for |
| 259 | * |
Tejun Heo | 8312a8d | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 260 | * Get inode for @sd. If such inode doesn't exist, a new inode |
| 261 | * is allocated and basics are initialized. New inode is |
| 262 | * returned locked. |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 263 | * |
| 264 | * LOCKING: |
| 265 | * Kernel thread context (may sleep). |
| 266 | * |
| 267 | * RETURNS: |
| 268 | * Pointer to allocated inode on success, NULL on failure. |
| 269 | */ |
Tejun Heo | 8312a8d | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 270 | struct inode * sysfs_get_inode(struct sysfs_dirent *sd) |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 271 | { |
| 272 | struct inode *inode; |
| 273 | |
Tejun Heo | 8312a8d | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 274 | inode = iget_locked(sysfs_sb, sd->s_ino); |
| 275 | if (inode && (inode->i_state & I_NEW)) |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 276 | sysfs_init_inode(sd, inode); |
| 277 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | return inode; |
| 279 | } |
| 280 | |
Eric W. Biederman | 04256b4 | 2009-02-11 13:20:23 -0800 | [diff] [blame] | 281 | /* |
| 282 | * The sysfs_dirent serves as both an inode and a directory entry for sysfs. |
| 283 | * To prevent the sysfs inode numbers from being freed prematurely we take a |
| 284 | * reference to sysfs_dirent from the sysfs inode. A |
| 285 | * super_operations.delete_inode() implementation is needed to drop that |
| 286 | * reference upon inode destruction. |
| 287 | */ |
| 288 | void sysfs_delete_inode(struct inode *inode) |
| 289 | { |
| 290 | struct sysfs_dirent *sd = inode->i_private; |
| 291 | |
| 292 | truncate_inode_pages(&inode->i_data, 0); |
| 293 | clear_inode(inode); |
| 294 | sysfs_put(sd); |
| 295 | } |
| 296 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 297 | int sysfs_hash_and_remove(struct sysfs_dirent *dir_sd, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | { |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 299 | struct sysfs_addrm_cxt acxt; |
Tejun Heo | 41fc1c2 | 2007-08-02 21:38:03 +0900 | [diff] [blame] | 300 | struct sysfs_dirent *sd; |
Greg Kroah-Hartman | 641e6f3 | 2006-03-16 15:44:26 -0800 | [diff] [blame] | 301 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 302 | if (!dir_sd) |
Randy.Dunlap | 995982c | 2006-07-10 23:05:25 -0700 | [diff] [blame] | 303 | return -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 305 | sysfs_addrm_start(&acxt, dir_sd); |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 306 | |
Tejun Heo | 41fc1c2 | 2007-08-02 21:38:03 +0900 | [diff] [blame] | 307 | sd = sysfs_find_dirent(dir_sd, name); |
| 308 | if (sd) |
| 309 | sysfs_remove_one(&acxt, sd); |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 310 | |
Tejun Heo | 990e53f | 2007-08-02 21:38:03 +0900 | [diff] [blame] | 311 | sysfs_addrm_finish(&acxt); |
| 312 | |
| 313 | if (sd) |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 314 | return 0; |
Tejun Heo | 990e53f | 2007-08-02 21:38:03 +0900 | [diff] [blame] | 315 | else |
| 316 | return -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | } |