Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * inode.c - basic inode and dentry operations. |
| 3 | * |
| 4 | * sysfs is Copyright (c) 2001-3 Patrick Mochel |
| 5 | * |
| 6 | * Please see Documentation/filesystems/sysfs.txt for more information. |
| 7 | */ |
| 8 | |
| 9 | #undef DEBUG |
| 10 | |
| 11 | #include <linux/pagemap.h> |
| 12 | #include <linux/namei.h> |
| 13 | #include <linux/backing-dev.h> |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 14 | #include <linux/capability.h> |
Randy.Dunlap | 995982c | 2006-07-10 23:05:25 -0700 | [diff] [blame] | 15 | #include <linux/errno.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 16 | #include <linux/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include "sysfs.h" |
| 18 | |
| 19 | extern struct super_block * sysfs_sb; |
| 20 | |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 21 | static const struct address_space_operations sysfs_aops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | .readpage = simple_readpage, |
| 23 | .prepare_write = simple_prepare_write, |
| 24 | .commit_write = simple_commit_write |
| 25 | }; |
| 26 | |
| 27 | static struct backing_dev_info sysfs_backing_dev_info = { |
| 28 | .ra_pages = 0, /* No readahead */ |
| 29 | .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK, |
| 30 | }; |
| 31 | |
Arjan van de Ven | c5ef1c4 | 2007-02-12 00:55:40 -0800 | [diff] [blame] | 32 | static const struct inode_operations sysfs_inode_operations ={ |
Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 33 | .setattr = sysfs_setattr, |
| 34 | }; |
| 35 | |
| 36 | int sysfs_setattr(struct dentry * dentry, struct iattr * iattr) |
| 37 | { |
| 38 | struct inode * inode = dentry->d_inode; |
| 39 | struct sysfs_dirent * sd = dentry->d_fsdata; |
| 40 | struct iattr * sd_iattr; |
| 41 | unsigned int ia_valid = iattr->ia_valid; |
| 42 | int error; |
| 43 | |
| 44 | if (!sd) |
| 45 | return -EINVAL; |
| 46 | |
| 47 | sd_iattr = sd->s_iattr; |
| 48 | |
| 49 | error = inode_change_ok(inode, iattr); |
| 50 | if (error) |
| 51 | return error; |
| 52 | |
| 53 | error = inode_setattr(inode, iattr); |
| 54 | if (error) |
| 55 | return error; |
| 56 | |
| 57 | if (!sd_iattr) { |
| 58 | /* setting attributes for the first time, allocate now */ |
Eric Sesterhenn | 58d4928 | 2006-02-22 11:18:15 +0100 | [diff] [blame] | 59 | sd_iattr = kzalloc(sizeof(struct iattr), GFP_KERNEL); |
Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 60 | if (!sd_iattr) |
| 61 | return -ENOMEM; |
| 62 | /* assign default attributes */ |
Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 63 | sd_iattr->ia_mode = sd->s_mode; |
| 64 | sd_iattr->ia_uid = 0; |
| 65 | sd_iattr->ia_gid = 0; |
| 66 | sd_iattr->ia_atime = sd_iattr->ia_mtime = sd_iattr->ia_ctime = CURRENT_TIME; |
| 67 | sd->s_iattr = sd_iattr; |
| 68 | } |
| 69 | |
| 70 | /* attributes were changed atleast once in past */ |
| 71 | |
| 72 | if (ia_valid & ATTR_UID) |
| 73 | sd_iattr->ia_uid = iattr->ia_uid; |
| 74 | if (ia_valid & ATTR_GID) |
| 75 | sd_iattr->ia_gid = iattr->ia_gid; |
| 76 | if (ia_valid & ATTR_ATIME) |
| 77 | sd_iattr->ia_atime = timespec_trunc(iattr->ia_atime, |
| 78 | inode->i_sb->s_time_gran); |
| 79 | if (ia_valid & ATTR_MTIME) |
| 80 | sd_iattr->ia_mtime = timespec_trunc(iattr->ia_mtime, |
| 81 | inode->i_sb->s_time_gran); |
| 82 | if (ia_valid & ATTR_CTIME) |
| 83 | sd_iattr->ia_ctime = timespec_trunc(iattr->ia_ctime, |
| 84 | inode->i_sb->s_time_gran); |
| 85 | if (ia_valid & ATTR_MODE) { |
| 86 | umode_t mode = iattr->ia_mode; |
| 87 | |
| 88 | if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID)) |
| 89 | mode &= ~S_ISGID; |
Maneesh Soni | 9ca1eb3 | 2005-07-29 12:14:19 -0700 | [diff] [blame] | 90 | sd_iattr->ia_mode = sd->s_mode = mode; |
Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | return error; |
| 94 | } |
| 95 | |
Maneesh Soni | 8215534 | 2005-05-31 10:39:52 +0530 | [diff] [blame] | 96 | static inline void set_default_inode_attr(struct inode * inode, mode_t mode) |
| 97 | { |
| 98 | inode->i_mode = mode; |
| 99 | inode->i_uid = 0; |
| 100 | inode->i_gid = 0; |
| 101 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 102 | } |
| 103 | |
| 104 | static inline void set_inode_attr(struct inode * inode, struct iattr * iattr) |
| 105 | { |
| 106 | inode->i_mode = iattr->ia_mode; |
| 107 | inode->i_uid = iattr->ia_uid; |
| 108 | inode->i_gid = iattr->ia_gid; |
| 109 | inode->i_atime = iattr->ia_atime; |
| 110 | inode->i_mtime = iattr->ia_mtime; |
| 111 | inode->i_ctime = iattr->ia_ctime; |
| 112 | } |
| 113 | |
Arjan van de Ven | 232ba9d | 2006-07-12 09:03:06 -0700 | [diff] [blame] | 114 | |
| 115 | /* |
| 116 | * sysfs has a different i_mutex lock order behavior for i_mutex than other |
| 117 | * filesystems; sysfs i_mutex is called in many places with subsystem locks |
| 118 | * held. At the same time, many of the VFS locking rules do not apply to |
| 119 | * sysfs at all (cross directory rename for example). To untangle this mess |
| 120 | * (which gives false positives in lockdep), we're giving sysfs inodes their |
| 121 | * own class for i_mutex. |
| 122 | */ |
| 123 | static struct lock_class_key sysfs_inode_imutex_key; |
| 124 | |
Eric W. Biederman | 372e88b | 2007-08-20 21:36:29 +0900 | [diff] [blame^] | 125 | static int sysfs_count_nlink(struct sysfs_dirent *sd) |
| 126 | { |
| 127 | struct sysfs_dirent *child; |
| 128 | int nr = 0; |
| 129 | |
| 130 | for (child = sd->s_children; child; child = child->s_sibling) |
| 131 | if (sysfs_type(child) == SYSFS_DIR) |
| 132 | nr++; |
| 133 | |
| 134 | return nr + 2; |
| 135 | } |
| 136 | |
Tejun Heo | bc37e28 | 2007-07-18 14:30:28 +0900 | [diff] [blame] | 137 | static void sysfs_init_inode(struct sysfs_dirent *sd, struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | { |
Eric W. Biederman | 372e88b | 2007-08-20 21:36:29 +0900 | [diff] [blame^] | 139 | struct bin_attribute *bin_attr; |
| 140 | |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 141 | inode->i_blocks = 0; |
| 142 | inode->i_mapping->a_ops = &sysfs_aops; |
| 143 | inode->i_mapping->backing_dev_info = &sysfs_backing_dev_info; |
| 144 | inode->i_op = &sysfs_inode_operations; |
| 145 | inode->i_ino = sd->s_ino; |
| 146 | lockdep_set_class(&inode->i_mutex, &sysfs_inode_imutex_key); |
Maneesh Soni | 8215534 | 2005-05-31 10:39:52 +0530 | [diff] [blame] | 147 | |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 148 | if (sd->s_iattr) { |
| 149 | /* sysfs_dirent has non-default attributes |
| 150 | * get them for the new inode from persistent copy |
| 151 | * in sysfs_dirent |
| 152 | */ |
| 153 | set_inode_attr(inode, sd->s_iattr); |
| 154 | } else |
| 155 | set_default_inode_attr(inode, sd->s_mode); |
Eric W. Biederman | 372e88b | 2007-08-20 21:36:29 +0900 | [diff] [blame^] | 156 | |
| 157 | |
| 158 | /* initialize inode according to type */ |
| 159 | switch (sysfs_type(sd)) { |
| 160 | case SYSFS_ROOT: |
| 161 | inode->i_op = &sysfs_dir_inode_operations; |
| 162 | inode->i_fop = &sysfs_dir_operations; |
| 163 | inc_nlink(inode); /* directory, account for "." */ |
| 164 | break; |
| 165 | case SYSFS_DIR: |
| 166 | inode->i_op = &sysfs_dir_inode_operations; |
| 167 | inode->i_fop = &sysfs_dir_operations; |
| 168 | inode->i_nlink = sysfs_count_nlink(sd); |
| 169 | break; |
| 170 | case SYSFS_KOBJ_ATTR: |
| 171 | inode->i_size = PAGE_SIZE; |
| 172 | inode->i_fop = &sysfs_file_operations; |
| 173 | break; |
| 174 | case SYSFS_KOBJ_BIN_ATTR: |
| 175 | bin_attr = sd->s_elem.bin_attr.bin_attr; |
| 176 | inode->i_size = bin_attr->size; |
| 177 | inode->i_fop = &bin_fops; |
| 178 | break; |
| 179 | case SYSFS_KOBJ_LINK: |
| 180 | inode->i_op = &sysfs_symlink_inode_operations; |
| 181 | break; |
| 182 | default: |
| 183 | BUG(); |
| 184 | } |
| 185 | |
| 186 | unlock_new_inode(inode); |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /** |
Tejun Heo | 8312a8d | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 190 | * sysfs_get_inode - get inode for sysfs_dirent |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 191 | * @sd: sysfs_dirent to allocate inode for |
| 192 | * |
Tejun Heo | 8312a8d | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 193 | * Get inode for @sd. If such inode doesn't exist, a new inode |
| 194 | * is allocated and basics are initialized. New inode is |
| 195 | * returned locked. |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 196 | * |
| 197 | * LOCKING: |
| 198 | * Kernel thread context (may sleep). |
| 199 | * |
| 200 | * RETURNS: |
| 201 | * Pointer to allocated inode on success, NULL on failure. |
| 202 | */ |
Tejun Heo | 8312a8d | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 203 | struct inode * sysfs_get_inode(struct sysfs_dirent *sd) |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 204 | { |
| 205 | struct inode *inode; |
| 206 | |
Tejun Heo | 8312a8d | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 207 | inode = iget_locked(sysfs_sb, sd->s_ino); |
| 208 | if (inode && (inode->i_state & I_NEW)) |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 209 | sysfs_init_inode(sd, inode); |
| 210 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | return inode; |
| 212 | } |
| 213 | |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 214 | /** |
| 215 | * sysfs_instantiate - instantiate dentry |
| 216 | * @dentry: dentry to be instantiated |
| 217 | * @inode: inode associated with @sd |
| 218 | * |
Tejun Heo | 8312a8d | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 219 | * Unlock @inode if locked and instantiate @dentry with @inode. |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 220 | * |
| 221 | * LOCKING: |
| 222 | * None. |
| 223 | */ |
| 224 | void sysfs_instantiate(struct dentry *dentry, struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | { |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 226 | BUG_ON(!dentry || dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 228 | d_instantiate(dentry, inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 231 | 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] | 232 | { |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 233 | struct sysfs_addrm_cxt acxt; |
Tejun Heo | 41fc1c2 | 2007-08-02 21:38:03 +0900 | [diff] [blame] | 234 | struct sysfs_dirent *sd; |
Greg Kroah-Hartman | 641e6f3 | 2006-03-16 15:44:26 -0800 | [diff] [blame] | 235 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 236 | if (!dir_sd) |
Randy.Dunlap | 995982c | 2006-07-10 23:05:25 -0700 | [diff] [blame] | 237 | return -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 239 | sysfs_addrm_start(&acxt, dir_sd); |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 240 | |
Tejun Heo | 41fc1c2 | 2007-08-02 21:38:03 +0900 | [diff] [blame] | 241 | sd = sysfs_find_dirent(dir_sd, name); |
| 242 | if (sd) |
| 243 | sysfs_remove_one(&acxt, sd); |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 244 | |
Tejun Heo | 990e53f | 2007-08-02 21:38:03 +0900 | [diff] [blame] | 245 | sysfs_addrm_finish(&acxt); |
| 246 | |
| 247 | if (sd) |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 248 | return 0; |
Tejun Heo | 990e53f | 2007-08-02 21:38:03 +0900 | [diff] [blame] | 249 | else |
| 250 | return -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | } |