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