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