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> |
Oliver Neukum | 94bebf4 | 2006-12-20 10:52:44 +0100 | [diff] [blame] | 17 | #include <asm/semaphore.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "sysfs.h" |
| 19 | |
| 20 | extern struct super_block * sysfs_sb; |
| 21 | |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 22 | static const struct address_space_operations sysfs_aops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | .readpage = simple_readpage, |
| 24 | .prepare_write = simple_prepare_write, |
| 25 | .commit_write = simple_commit_write |
| 26 | }; |
| 27 | |
| 28 | static struct backing_dev_info sysfs_backing_dev_info = { |
| 29 | .ra_pages = 0, /* No readahead */ |
| 30 | .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK, |
| 31 | }; |
| 32 | |
Arjan van de Ven | c5ef1c4 | 2007-02-12 00:55:40 -0800 | [diff] [blame] | 33 | static const struct inode_operations sysfs_inode_operations ={ |
Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 34 | .setattr = sysfs_setattr, |
| 35 | }; |
| 36 | |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 37 | void sysfs_delete_inode(struct inode *inode) |
| 38 | { |
| 39 | /* Free the shadowed directory inode operations */ |
| 40 | if (sysfs_is_shadowed_inode(inode)) { |
| 41 | kfree(inode->i_op); |
| 42 | inode->i_op = NULL; |
| 43 | } |
| 44 | return generic_delete_inode(inode); |
| 45 | } |
| 46 | |
Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 47 | int sysfs_setattr(struct dentry * dentry, struct iattr * iattr) |
| 48 | { |
| 49 | struct inode * inode = dentry->d_inode; |
| 50 | struct sysfs_dirent * sd = dentry->d_fsdata; |
| 51 | struct iattr * sd_iattr; |
| 52 | unsigned int ia_valid = iattr->ia_valid; |
| 53 | int error; |
| 54 | |
| 55 | if (!sd) |
| 56 | return -EINVAL; |
| 57 | |
| 58 | sd_iattr = sd->s_iattr; |
| 59 | |
| 60 | error = inode_change_ok(inode, iattr); |
| 61 | if (error) |
| 62 | return error; |
| 63 | |
| 64 | error = inode_setattr(inode, iattr); |
| 65 | if (error) |
| 66 | return error; |
| 67 | |
| 68 | if (!sd_iattr) { |
| 69 | /* setting attributes for the first time, allocate now */ |
Eric Sesterhenn | 58d4928 | 2006-02-22 11:18:15 +0100 | [diff] [blame] | 70 | sd_iattr = kzalloc(sizeof(struct iattr), GFP_KERNEL); |
Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 71 | if (!sd_iattr) |
| 72 | return -ENOMEM; |
| 73 | /* assign default attributes */ |
Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 74 | sd_iattr->ia_mode = sd->s_mode; |
| 75 | sd_iattr->ia_uid = 0; |
| 76 | sd_iattr->ia_gid = 0; |
| 77 | sd_iattr->ia_atime = sd_iattr->ia_mtime = sd_iattr->ia_ctime = CURRENT_TIME; |
| 78 | sd->s_iattr = sd_iattr; |
| 79 | } |
| 80 | |
| 81 | /* attributes were changed atleast once in past */ |
| 82 | |
| 83 | if (ia_valid & ATTR_UID) |
| 84 | sd_iattr->ia_uid = iattr->ia_uid; |
| 85 | if (ia_valid & ATTR_GID) |
| 86 | sd_iattr->ia_gid = iattr->ia_gid; |
| 87 | if (ia_valid & ATTR_ATIME) |
| 88 | sd_iattr->ia_atime = timespec_trunc(iattr->ia_atime, |
| 89 | inode->i_sb->s_time_gran); |
| 90 | if (ia_valid & ATTR_MTIME) |
| 91 | sd_iattr->ia_mtime = timespec_trunc(iattr->ia_mtime, |
| 92 | inode->i_sb->s_time_gran); |
| 93 | if (ia_valid & ATTR_CTIME) |
| 94 | sd_iattr->ia_ctime = timespec_trunc(iattr->ia_ctime, |
| 95 | inode->i_sb->s_time_gran); |
| 96 | if (ia_valid & ATTR_MODE) { |
| 97 | umode_t mode = iattr->ia_mode; |
| 98 | |
| 99 | if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID)) |
| 100 | mode &= ~S_ISGID; |
Maneesh Soni | 9ca1eb3 | 2005-07-29 12:14:19 -0700 | [diff] [blame] | 101 | sd_iattr->ia_mode = sd->s_mode = mode; |
Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | return error; |
| 105 | } |
| 106 | |
Maneesh Soni | 8215534 | 2005-05-31 10:39:52 +0530 | [diff] [blame] | 107 | static inline void set_default_inode_attr(struct inode * inode, mode_t mode) |
| 108 | { |
| 109 | inode->i_mode = mode; |
| 110 | inode->i_uid = 0; |
| 111 | inode->i_gid = 0; |
| 112 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 113 | } |
| 114 | |
| 115 | static inline void set_inode_attr(struct inode * inode, struct iattr * iattr) |
| 116 | { |
| 117 | inode->i_mode = iattr->ia_mode; |
| 118 | inode->i_uid = iattr->ia_uid; |
| 119 | inode->i_gid = iattr->ia_gid; |
| 120 | inode->i_atime = iattr->ia_atime; |
| 121 | inode->i_mtime = iattr->ia_mtime; |
| 122 | inode->i_ctime = iattr->ia_ctime; |
| 123 | } |
| 124 | |
Arjan van de Ven | 232ba9d | 2006-07-12 09:03:06 -0700 | [diff] [blame] | 125 | |
| 126 | /* |
| 127 | * sysfs has a different i_mutex lock order behavior for i_mutex than other |
| 128 | * filesystems; sysfs i_mutex is called in many places with subsystem locks |
| 129 | * held. At the same time, many of the VFS locking rules do not apply to |
| 130 | * sysfs at all (cross directory rename for example). To untangle this mess |
| 131 | * (which gives false positives in lockdep), we're giving sysfs inodes their |
| 132 | * own class for i_mutex. |
| 133 | */ |
| 134 | static struct lock_class_key sysfs_inode_imutex_key; |
| 135 | |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 136 | void sysfs_init_inode(struct sysfs_dirent *sd, struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | { |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 138 | inode->i_blocks = 0; |
| 139 | inode->i_mapping->a_ops = &sysfs_aops; |
| 140 | inode->i_mapping->backing_dev_info = &sysfs_backing_dev_info; |
| 141 | inode->i_op = &sysfs_inode_operations; |
| 142 | inode->i_ino = sd->s_ino; |
| 143 | lockdep_set_class(&inode->i_mutex, &sysfs_inode_imutex_key); |
Maneesh Soni | 8215534 | 2005-05-31 10:39:52 +0530 | [diff] [blame] | 144 | |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 145 | if (sd->s_iattr) { |
| 146 | /* sysfs_dirent has non-default attributes |
| 147 | * get them for the new inode from persistent copy |
| 148 | * in sysfs_dirent |
| 149 | */ |
| 150 | set_inode_attr(inode, sd->s_iattr); |
| 151 | } else |
| 152 | set_default_inode_attr(inode, sd->s_mode); |
| 153 | } |
| 154 | |
| 155 | /** |
Tejun Heo | 8312a8d | 2007-06-14 03:45:17 +0900 | [diff] [blame^] | 156 | * sysfs_get_inode - get inode for sysfs_dirent |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 157 | * @sd: sysfs_dirent to allocate inode for |
| 158 | * |
Tejun Heo | 8312a8d | 2007-06-14 03:45:17 +0900 | [diff] [blame^] | 159 | * Get inode for @sd. If such inode doesn't exist, a new inode |
| 160 | * is allocated and basics are initialized. New inode is |
| 161 | * returned locked. |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 162 | * |
| 163 | * LOCKING: |
| 164 | * Kernel thread context (may sleep). |
| 165 | * |
| 166 | * RETURNS: |
| 167 | * Pointer to allocated inode on success, NULL on failure. |
| 168 | */ |
Tejun Heo | 8312a8d | 2007-06-14 03:45:17 +0900 | [diff] [blame^] | 169 | struct inode * sysfs_get_inode(struct sysfs_dirent *sd) |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 170 | { |
| 171 | struct inode *inode; |
| 172 | |
Tejun Heo | 8312a8d | 2007-06-14 03:45:17 +0900 | [diff] [blame^] | 173 | inode = iget_locked(sysfs_sb, sd->s_ino); |
| 174 | if (inode && (inode->i_state & I_NEW)) |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 175 | sysfs_init_inode(sd, inode); |
| 176 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | return inode; |
| 178 | } |
| 179 | |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 180 | /** |
| 181 | * sysfs_instantiate - instantiate dentry |
| 182 | * @dentry: dentry to be instantiated |
| 183 | * @inode: inode associated with @sd |
| 184 | * |
Tejun Heo | 8312a8d | 2007-06-14 03:45:17 +0900 | [diff] [blame^] | 185 | * Unlock @inode if locked and instantiate @dentry with @inode. |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 186 | * |
| 187 | * LOCKING: |
| 188 | * None. |
| 189 | */ |
| 190 | void sysfs_instantiate(struct dentry *dentry, struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | { |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 192 | BUG_ON(!dentry || dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | |
Tejun Heo | 8312a8d | 2007-06-14 03:45:17 +0900 | [diff] [blame^] | 194 | if (inode->i_state & I_NEW) { |
| 195 | unlock_new_inode(inode); |
| 196 | |
| 197 | if (dentry->d_parent && dentry->d_parent->d_inode) { |
| 198 | struct inode *p_inode = dentry->d_parent->d_inode; |
| 199 | p_inode->i_mtime = p_inode->i_ctime = CURRENT_TIME; |
| 200 | } |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | d_instantiate(dentry, inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Tejun Heo | dbde0fc | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 206 | /** |
| 207 | * sysfs_drop_dentry - drop dentry for the specified sysfs_dirent |
| 208 | * @sd: target sysfs_dirent |
| 209 | * |
| 210 | * Drop dentry for @sd. @sd must have been unlinked from its |
| 211 | * parent on entry to this function such that it can't be looked |
| 212 | * up anymore. |
| 213 | * |
| 214 | * @sd->s_dentry which is protected with sysfs_lock points to the |
| 215 | * currently associated dentry but we're not holding a reference |
| 216 | * to it and racing with dput(). Grab dcache_lock and verify |
| 217 | * dentry before dropping it. If @sd->s_dentry is NULL or dput() |
| 218 | * beats us, no need to bother. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | */ |
Tejun Heo | dbde0fc | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 220 | void sysfs_drop_dentry(struct sysfs_dirent *sd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | { |
Tejun Heo | dbde0fc | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 222 | struct dentry *dentry = NULL, *parent = NULL; |
| 223 | struct inode *dir; |
| 224 | struct timespec curtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | |
Tejun Heo | dd14cbc | 2007-06-11 14:04:01 +0900 | [diff] [blame] | 226 | /* We're not holding a reference to ->s_dentry dentry but the |
| 227 | * field will stay valid as long as sysfs_lock is held. |
| 228 | */ |
| 229 | spin_lock(&sysfs_lock); |
| 230 | spin_lock(&dcache_lock); |
| 231 | |
Tejun Heo | dbde0fc | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 232 | if (sd->s_dentry && sd->s_dentry->d_inode) { |
| 233 | /* get dentry if it's there and dput() didn't kill it yet */ |
Tejun Heo | dd14cbc | 2007-06-11 14:04:01 +0900 | [diff] [blame] | 234 | dentry = dget_locked(sd->s_dentry); |
Tejun Heo | dbde0fc | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 235 | parent = dentry->d_parent; |
| 236 | } else if (sd->s_parent->s_dentry->d_inode) { |
| 237 | /* We need to update the parent even if dentry for the |
| 238 | * victim itself doesn't exist. |
| 239 | */ |
| 240 | parent = dget_locked(sd->s_parent->s_dentry); |
| 241 | } |
| 242 | |
| 243 | /* drop */ |
| 244 | if (dentry) { |
| 245 | spin_lock(&dentry->d_lock); |
| 246 | __d_drop(dentry); |
| 247 | spin_unlock(&dentry->d_lock); |
| 248 | } |
Tejun Heo | dd14cbc | 2007-06-11 14:04:01 +0900 | [diff] [blame] | 249 | |
| 250 | spin_unlock(&dcache_lock); |
| 251 | spin_unlock(&sysfs_lock); |
| 252 | |
Tejun Heo | dbde0fc | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 253 | /* nothing to do if the parent isn't in dcache */ |
| 254 | if (!parent) |
| 255 | return; |
Tejun Heo | dd14cbc | 2007-06-11 14:04:01 +0900 | [diff] [blame] | 256 | |
Tejun Heo | dbde0fc | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 257 | /* adjust nlink and update timestamp */ |
| 258 | dir = parent->d_inode; |
| 259 | mutex_lock(&dir->i_mutex); |
| 260 | |
| 261 | curtime = CURRENT_TIME; |
| 262 | |
| 263 | dir->i_ctime = dir->i_mtime = curtime; |
| 264 | |
| 265 | if (dentry) { |
| 266 | dentry->d_inode->i_ctime = curtime; |
| 267 | drop_nlink(dentry->d_inode); |
| 268 | if (sd->s_type & SYSFS_DIR) { |
| 269 | drop_nlink(dentry->d_inode); |
| 270 | drop_nlink(dir); |
| 271 | /* XXX: unpin if directory, this will go away soon */ |
| 272 | dput(dentry); |
| 273 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | } |
Tejun Heo | dbde0fc | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 275 | |
| 276 | mutex_unlock(&dir->i_mutex); |
| 277 | |
| 278 | /* bye bye */ |
| 279 | if (dentry) |
| 280 | dput(dentry); |
| 281 | else |
| 282 | dput(parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Randy.Dunlap | 995982c | 2006-07-10 23:05:25 -0700 | [diff] [blame] | 285 | int sysfs_hash_and_remove(struct dentry * dir, const char * name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | { |
| 287 | struct sysfs_dirent * sd; |
Greg Kroah-Hartman | 641e6f3 | 2006-03-16 15:44:26 -0800 | [diff] [blame] | 288 | struct sysfs_dirent * parent_sd; |
Randy.Dunlap | 995982c | 2006-07-10 23:05:25 -0700 | [diff] [blame] | 289 | int found = 0; |
Greg Kroah-Hartman | 641e6f3 | 2006-03-16 15:44:26 -0800 | [diff] [blame] | 290 | |
| 291 | if (!dir) |
Randy.Dunlap | 995982c | 2006-07-10 23:05:25 -0700 | [diff] [blame] | 292 | return -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | |
James Bottomley | 36676bc | 2005-08-26 18:34:17 -0700 | [diff] [blame] | 294 | if (dir->d_inode == NULL) |
| 295 | /* no inode means this hasn't been made visible yet */ |
Randy.Dunlap | 995982c | 2006-07-10 23:05:25 -0700 | [diff] [blame] | 296 | return -ENOENT; |
James Bottomley | 36676bc | 2005-08-26 18:34:17 -0700 | [diff] [blame] | 297 | |
Greg Kroah-Hartman | 641e6f3 | 2006-03-16 15:44:26 -0800 | [diff] [blame] | 298 | parent_sd = dir->d_fsdata; |
Frederik Deweerdt | d3fc373 | 2007-01-05 12:04:33 -0800 | [diff] [blame] | 299 | mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | list_for_each_entry(sd, &parent_sd->s_children, s_sibling) { |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 301 | if (!sd->s_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | continue; |
Tejun Heo | 0c096b5 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 303 | if (!strcmp(sd->s_name, name)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | list_del_init(&sd->s_sibling); |
Randy.Dunlap | 995982c | 2006-07-10 23:05:25 -0700 | [diff] [blame] | 305 | found = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | break; |
| 307 | } |
| 308 | } |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 309 | mutex_unlock(&dir->d_inode->i_mutex); |
Randy.Dunlap | 995982c | 2006-07-10 23:05:25 -0700 | [diff] [blame] | 310 | |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 311 | if (!found) |
| 312 | return -ENOENT; |
| 313 | |
Tejun Heo | dbde0fc | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 314 | sysfs_drop_dentry(sd); |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 315 | sysfs_deactivate(sd); |
| 316 | sysfs_put(sd); |
Tejun Heo | dbde0fc | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 317 | |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 318 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | } |