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> |
Oliver Neukum | 94bebf4 | 2006-12-20 10:52:44 +0100 | [diff] [blame] | 16 | #include <asm/semaphore.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 | |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 36 | void sysfs_delete_inode(struct inode *inode) |
| 37 | { |
| 38 | /* Free the shadowed directory inode operations */ |
| 39 | if (sysfs_is_shadowed_inode(inode)) { |
| 40 | kfree(inode->i_op); |
| 41 | inode->i_op = NULL; |
| 42 | } |
| 43 | return generic_delete_inode(inode); |
| 44 | } |
| 45 | |
Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 46 | int sysfs_setattr(struct dentry * dentry, struct iattr * iattr) |
| 47 | { |
| 48 | struct inode * inode = dentry->d_inode; |
| 49 | struct sysfs_dirent * sd = dentry->d_fsdata; |
| 50 | struct iattr * sd_iattr; |
| 51 | unsigned int ia_valid = iattr->ia_valid; |
| 52 | int error; |
| 53 | |
| 54 | if (!sd) |
| 55 | return -EINVAL; |
| 56 | |
| 57 | sd_iattr = sd->s_iattr; |
| 58 | |
| 59 | error = inode_change_ok(inode, iattr); |
| 60 | if (error) |
| 61 | return error; |
| 62 | |
| 63 | error = inode_setattr(inode, iattr); |
| 64 | if (error) |
| 65 | return error; |
| 66 | |
| 67 | if (!sd_iattr) { |
| 68 | /* setting attributes for the first time, allocate now */ |
Eric Sesterhenn | 58d4928 | 2006-02-22 11:18:15 +0100 | [diff] [blame] | 69 | sd_iattr = kzalloc(sizeof(struct iattr), GFP_KERNEL); |
Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 70 | if (!sd_iattr) |
| 71 | return -ENOMEM; |
| 72 | /* assign default attributes */ |
Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 73 | sd_iattr->ia_mode = sd->s_mode; |
| 74 | sd_iattr->ia_uid = 0; |
| 75 | sd_iattr->ia_gid = 0; |
| 76 | sd_iattr->ia_atime = sd_iattr->ia_mtime = sd_iattr->ia_ctime = CURRENT_TIME; |
| 77 | sd->s_iattr = sd_iattr; |
| 78 | } |
| 79 | |
| 80 | /* attributes were changed atleast once in past */ |
| 81 | |
| 82 | if (ia_valid & ATTR_UID) |
| 83 | sd_iattr->ia_uid = iattr->ia_uid; |
| 84 | if (ia_valid & ATTR_GID) |
| 85 | sd_iattr->ia_gid = iattr->ia_gid; |
| 86 | if (ia_valid & ATTR_ATIME) |
| 87 | sd_iattr->ia_atime = timespec_trunc(iattr->ia_atime, |
| 88 | inode->i_sb->s_time_gran); |
| 89 | if (ia_valid & ATTR_MTIME) |
| 90 | sd_iattr->ia_mtime = timespec_trunc(iattr->ia_mtime, |
| 91 | inode->i_sb->s_time_gran); |
| 92 | if (ia_valid & ATTR_CTIME) |
| 93 | sd_iattr->ia_ctime = timespec_trunc(iattr->ia_ctime, |
| 94 | inode->i_sb->s_time_gran); |
| 95 | if (ia_valid & ATTR_MODE) { |
| 96 | umode_t mode = iattr->ia_mode; |
| 97 | |
| 98 | if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID)) |
| 99 | mode &= ~S_ISGID; |
Maneesh Soni | 9ca1eb3 | 2005-07-29 12:14:19 -0700 | [diff] [blame] | 100 | sd_iattr->ia_mode = sd->s_mode = mode; |
Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | return error; |
| 104 | } |
| 105 | |
Maneesh Soni | 8215534 | 2005-05-31 10:39:52 +0530 | [diff] [blame] | 106 | static inline void set_default_inode_attr(struct inode * inode, mode_t mode) |
| 107 | { |
| 108 | inode->i_mode = mode; |
| 109 | inode->i_uid = 0; |
| 110 | inode->i_gid = 0; |
| 111 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 112 | } |
| 113 | |
| 114 | static inline void set_inode_attr(struct inode * inode, struct iattr * iattr) |
| 115 | { |
| 116 | inode->i_mode = iattr->ia_mode; |
| 117 | inode->i_uid = iattr->ia_uid; |
| 118 | inode->i_gid = iattr->ia_gid; |
| 119 | inode->i_atime = iattr->ia_atime; |
| 120 | inode->i_mtime = iattr->ia_mtime; |
| 121 | inode->i_ctime = iattr->ia_ctime; |
| 122 | } |
| 123 | |
Arjan van de Ven | 232ba9d | 2006-07-12 09:03:06 -0700 | [diff] [blame] | 124 | |
| 125 | /* |
| 126 | * sysfs has a different i_mutex lock order behavior for i_mutex than other |
| 127 | * filesystems; sysfs i_mutex is called in many places with subsystem locks |
| 128 | * held. At the same time, many of the VFS locking rules do not apply to |
| 129 | * sysfs at all (cross directory rename for example). To untangle this mess |
| 130 | * (which gives false positives in lockdep), we're giving sysfs inodes their |
| 131 | * own class for i_mutex. |
| 132 | */ |
| 133 | static struct lock_class_key sysfs_inode_imutex_key; |
| 134 | |
Maneesh Soni | 8215534 | 2005-05-31 10:39:52 +0530 | [diff] [blame] | 135 | struct inode * sysfs_new_inode(mode_t mode, struct sysfs_dirent * sd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | { |
| 137 | struct inode * inode = new_inode(sysfs_sb); |
| 138 | if (inode) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | inode->i_blocks = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | inode->i_mapping->a_ops = &sysfs_aops; |
| 141 | inode->i_mapping->backing_dev_info = &sysfs_backing_dev_info; |
Maneesh Soni | 8215534 | 2005-05-31 10:39:52 +0530 | [diff] [blame] | 142 | inode->i_op = &sysfs_inode_operations; |
Arjan van de Ven | 232ba9d | 2006-07-12 09:03:06 -0700 | [diff] [blame] | 143 | lockdep_set_class(&inode->i_mutex, &sysfs_inode_imutex_key); |
Maneesh Soni | 8215534 | 2005-05-31 10:39:52 +0530 | [diff] [blame] | 144 | |
| 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, mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | } |
| 154 | return inode; |
| 155 | } |
| 156 | |
| 157 | int sysfs_create(struct dentry * dentry, int mode, int (*init)(struct inode *)) |
| 158 | { |
| 159 | int error = 0; |
| 160 | struct inode * inode = NULL; |
| 161 | if (dentry) { |
| 162 | if (!dentry->d_inode) { |
Maneesh Soni | 8215534 | 2005-05-31 10:39:52 +0530 | [diff] [blame] | 163 | struct sysfs_dirent * sd = dentry->d_fsdata; |
| 164 | if ((inode = sysfs_new_inode(mode, sd))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | if (dentry->d_parent && dentry->d_parent->d_inode) { |
| 166 | struct inode *p_inode = dentry->d_parent->d_inode; |
| 167 | p_inode->i_mtime = p_inode->i_ctime = CURRENT_TIME; |
| 168 | } |
| 169 | goto Proceed; |
| 170 | } |
| 171 | else |
| 172 | error = -ENOMEM; |
| 173 | } else |
| 174 | error = -EEXIST; |
| 175 | } else |
| 176 | error = -ENOENT; |
| 177 | goto Done; |
| 178 | |
| 179 | Proceed: |
| 180 | if (init) |
| 181 | error = init(inode); |
| 182 | if (!error) { |
| 183 | d_instantiate(dentry, inode); |
| 184 | if (S_ISDIR(mode)) |
| 185 | dget(dentry); /* pin only directory dentry in core */ |
| 186 | } else |
| 187 | iput(inode); |
| 188 | Done: |
| 189 | return error; |
| 190 | } |
| 191 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | /* |
| 193 | * Get the name for corresponding element represented by the given sysfs_dirent |
| 194 | */ |
| 195 | const unsigned char * sysfs_get_name(struct sysfs_dirent *sd) |
| 196 | { |
| 197 | struct attribute * attr; |
| 198 | struct bin_attribute * bin_attr; |
| 199 | struct sysfs_symlink * sl; |
| 200 | |
Eric Sesterhenn | 99cee0c | 2006-04-01 01:18:38 +0200 | [diff] [blame] | 201 | BUG_ON(!sd || !sd->s_element); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | |
| 203 | switch (sd->s_type) { |
| 204 | case SYSFS_DIR: |
| 205 | /* Always have a dentry so use that */ |
| 206 | return sd->s_dentry->d_name.name; |
| 207 | |
| 208 | case SYSFS_KOBJ_ATTR: |
| 209 | attr = sd->s_element; |
| 210 | return attr->name; |
| 211 | |
| 212 | case SYSFS_KOBJ_BIN_ATTR: |
| 213 | bin_attr = sd->s_element; |
| 214 | return bin_attr->attr.name; |
| 215 | |
| 216 | case SYSFS_KOBJ_LINK: |
| 217 | sl = sd->s_element; |
| 218 | return sl->link_name; |
| 219 | } |
| 220 | return NULL; |
| 221 | } |
| 222 | |
Oliver Neukum | 94bebf4 | 2006-12-20 10:52:44 +0100 | [diff] [blame] | 223 | static inline void orphan_all_buffers(struct inode *node) |
| 224 | { |
Alan Stern | e7b0d26 | 2007-03-15 15:51:28 -0400 | [diff] [blame] | 225 | struct sysfs_buffer_collection *set; |
Oliver Neukum | 94bebf4 | 2006-12-20 10:52:44 +0100 | [diff] [blame] | 226 | struct sysfs_buffer *buf; |
| 227 | |
Frederik Deweerdt | d3fc373 | 2007-01-05 12:04:33 -0800 | [diff] [blame] | 228 | mutex_lock_nested(&node->i_mutex, I_MUTEX_CHILD); |
Alan Stern | e7b0d26 | 2007-03-15 15:51:28 -0400 | [diff] [blame] | 229 | set = node->i_private; |
| 230 | if (set) { |
| 231 | list_for_each_entry(buf, &set->associates, associates) { |
| 232 | down(&buf->sem); |
Oliver Neukum | 94bebf4 | 2006-12-20 10:52:44 +0100 | [diff] [blame] | 233 | buf->orphaned = 1; |
Alan Stern | e7b0d26 | 2007-03-15 15:51:28 -0400 | [diff] [blame] | 234 | up(&buf->sem); |
| 235 | } |
Oliver Neukum | 94bebf4 | 2006-12-20 10:52:44 +0100 | [diff] [blame] | 236 | } |
| 237 | mutex_unlock(&node->i_mutex); |
| 238 | } |
| 239 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | |
| 241 | /* |
| 242 | * Unhashes the dentry corresponding to given sysfs_dirent |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 243 | * Called with parent inode's i_mutex held. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | */ |
| 245 | void sysfs_drop_dentry(struct sysfs_dirent * sd, struct dentry * parent) |
| 246 | { |
| 247 | struct dentry * dentry = sd->s_dentry; |
Oliver Neukum | 94bebf4 | 2006-12-20 10:52:44 +0100 | [diff] [blame] | 248 | struct inode *inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | |
| 250 | if (dentry) { |
| 251 | spin_lock(&dcache_lock); |
| 252 | spin_lock(&dentry->d_lock); |
| 253 | if (!(d_unhashed(dentry) && dentry->d_inode)) { |
Oliver Neukum | 94bebf4 | 2006-12-20 10:52:44 +0100 | [diff] [blame] | 254 | inode = dentry->d_inode; |
| 255 | spin_lock(&inode->i_lock); |
| 256 | __iget(inode); |
| 257 | spin_unlock(&inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | dget_locked(dentry); |
| 259 | __d_drop(dentry); |
| 260 | spin_unlock(&dentry->d_lock); |
| 261 | spin_unlock(&dcache_lock); |
| 262 | simple_unlink(parent->d_inode, dentry); |
Oliver Neukum | 94bebf4 | 2006-12-20 10:52:44 +0100 | [diff] [blame] | 263 | orphan_all_buffers(inode); |
| 264 | iput(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | } else { |
| 266 | spin_unlock(&dentry->d_lock); |
| 267 | spin_unlock(&dcache_lock); |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | |
Randy.Dunlap | 995982c | 2006-07-10 23:05:25 -0700 | [diff] [blame] | 272 | int sysfs_hash_and_remove(struct dentry * dir, const char * name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | { |
| 274 | struct sysfs_dirent * sd; |
Greg Kroah-Hartman | 641e6f3 | 2006-03-16 15:44:26 -0800 | [diff] [blame] | 275 | struct sysfs_dirent * parent_sd; |
Randy.Dunlap | 995982c | 2006-07-10 23:05:25 -0700 | [diff] [blame] | 276 | int found = 0; |
Greg Kroah-Hartman | 641e6f3 | 2006-03-16 15:44:26 -0800 | [diff] [blame] | 277 | |
| 278 | if (!dir) |
Randy.Dunlap | 995982c | 2006-07-10 23:05:25 -0700 | [diff] [blame] | 279 | return -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | |
James Bottomley | 36676bc | 2005-08-26 18:34:17 -0700 | [diff] [blame] | 281 | if (dir->d_inode == NULL) |
| 282 | /* no inode means this hasn't been made visible yet */ |
Randy.Dunlap | 995982c | 2006-07-10 23:05:25 -0700 | [diff] [blame] | 283 | return -ENOENT; |
James Bottomley | 36676bc | 2005-08-26 18:34:17 -0700 | [diff] [blame] | 284 | |
Greg Kroah-Hartman | 641e6f3 | 2006-03-16 15:44:26 -0800 | [diff] [blame] | 285 | parent_sd = dir->d_fsdata; |
Frederik Deweerdt | d3fc373 | 2007-01-05 12:04:33 -0800 | [diff] [blame] | 286 | mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | list_for_each_entry(sd, &parent_sd->s_children, s_sibling) { |
| 288 | if (!sd->s_element) |
| 289 | continue; |
| 290 | if (!strcmp(sysfs_get_name(sd), name)) { |
| 291 | list_del_init(&sd->s_sibling); |
| 292 | sysfs_drop_dentry(sd, dir); |
| 293 | sysfs_put(sd); |
Randy.Dunlap | 995982c | 2006-07-10 23:05:25 -0700 | [diff] [blame] | 294 | found = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | break; |
| 296 | } |
| 297 | } |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 298 | mutex_unlock(&dir->d_inode->i_mutex); |
Randy.Dunlap | 995982c | 2006-07-10 23:05:25 -0700 | [diff] [blame] | 299 | |
| 300 | return found ? 0 : -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | } |