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