| 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 */ | 
| Miklos Szeredi | e4ad08f | 2008-04-30 00:54:37 -0700 | [diff] [blame] | 33 | .capabilities	= BDI_CAP_NO_ACCT_AND_WRITEBACK, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |  | 
| Peter Zijlstra | e0bf68d | 2007-10-16 23:25:46 -0700 | [diff] [blame] | 40 | int __init sysfs_inode_init(void) | 
|  | 41 | { | 
|  | 42 | return bdi_init(&sysfs_backing_dev_info); | 
|  | 43 | } | 
|  | 44 |  | 
| Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 45 | int sysfs_setattr(struct dentry * dentry, struct iattr * iattr) | 
|  | 46 | { | 
|  | 47 | struct inode * inode = dentry->d_inode; | 
|  | 48 | struct sysfs_dirent * sd = dentry->d_fsdata; | 
|  | 49 | struct iattr * sd_iattr; | 
|  | 50 | unsigned int ia_valid = iattr->ia_valid; | 
|  | 51 | int error; | 
|  | 52 |  | 
|  | 53 | if (!sd) | 
|  | 54 | return -EINVAL; | 
|  | 55 |  | 
|  | 56 | sd_iattr = sd->s_iattr; | 
|  | 57 |  | 
|  | 58 | error = inode_change_ok(inode, iattr); | 
|  | 59 | if (error) | 
|  | 60 | return error; | 
|  | 61 |  | 
| Ben Hutchings | 40a2159 | 2008-04-28 15:59:58 +0100 | [diff] [blame] | 62 | iattr->ia_valid &= ~ATTR_SIZE; /* ignore size changes */ | 
|  | 63 |  | 
| Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 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 |  | 
| Eric W. Biederman | 372e88b | 2007-08-20 21:36:29 +0900 | [diff] [blame] | 136 | static int sysfs_count_nlink(struct sysfs_dirent *sd) | 
|  | 137 | { | 
|  | 138 | struct sysfs_dirent *child; | 
|  | 139 | int nr = 0; | 
|  | 140 |  | 
| Tejun Heo | bc747f3 | 2007-09-20 16:05:12 +0900 | [diff] [blame] | 141 | for (child = sd->s_dir.children; child; child = child->s_sibling) | 
| Eric W. Biederman | 372e88b | 2007-08-20 21:36:29 +0900 | [diff] [blame] | 142 | if (sysfs_type(child) == SYSFS_DIR) | 
|  | 143 | nr++; | 
|  | 144 |  | 
|  | 145 | return nr + 2; | 
|  | 146 | } | 
|  | 147 |  | 
| Tejun Heo | bc37e28 | 2007-07-18 14:30:28 +0900 | [diff] [blame] | 148 | static void sysfs_init_inode(struct sysfs_dirent *sd, struct inode *inode) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | { | 
| Eric W. Biederman | 372e88b | 2007-08-20 21:36:29 +0900 | [diff] [blame] | 150 | struct bin_attribute *bin_attr; | 
|  | 151 |  | 
| Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 152 | inode->i_blocks = 0; | 
|  | 153 | inode->i_mapping->a_ops = &sysfs_aops; | 
|  | 154 | inode->i_mapping->backing_dev_info = &sysfs_backing_dev_info; | 
|  | 155 | inode->i_op = &sysfs_inode_operations; | 
|  | 156 | inode->i_ino = sd->s_ino; | 
|  | 157 | lockdep_set_class(&inode->i_mutex, &sysfs_inode_imutex_key); | 
| Maneesh Soni | 8215534 | 2005-05-31 10:39:52 +0530 | [diff] [blame] | 158 |  | 
| Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 159 | if (sd->s_iattr) { | 
|  | 160 | /* sysfs_dirent has non-default attributes | 
|  | 161 | * get them for the new inode from persistent copy | 
|  | 162 | * in sysfs_dirent | 
|  | 163 | */ | 
|  | 164 | set_inode_attr(inode, sd->s_iattr); | 
|  | 165 | } else | 
|  | 166 | set_default_inode_attr(inode, sd->s_mode); | 
| Eric W. Biederman | 372e88b | 2007-08-20 21:36:29 +0900 | [diff] [blame] | 167 |  | 
|  | 168 |  | 
|  | 169 | /* initialize inode according to type */ | 
|  | 170 | switch (sysfs_type(sd)) { | 
| Eric W. Biederman | 372e88b | 2007-08-20 21:36:29 +0900 | [diff] [blame] | 171 | case SYSFS_DIR: | 
|  | 172 | inode->i_op = &sysfs_dir_inode_operations; | 
|  | 173 | inode->i_fop = &sysfs_dir_operations; | 
|  | 174 | inode->i_nlink = sysfs_count_nlink(sd); | 
|  | 175 | break; | 
|  | 176 | case SYSFS_KOBJ_ATTR: | 
|  | 177 | inode->i_size = PAGE_SIZE; | 
|  | 178 | inode->i_fop = &sysfs_file_operations; | 
|  | 179 | break; | 
|  | 180 | case SYSFS_KOBJ_BIN_ATTR: | 
| Tejun Heo | b1fc3d6 | 2007-09-20 16:05:11 +0900 | [diff] [blame] | 181 | bin_attr = sd->s_bin_attr.bin_attr; | 
| Eric W. Biederman | 372e88b | 2007-08-20 21:36:29 +0900 | [diff] [blame] | 182 | inode->i_size = bin_attr->size; | 
|  | 183 | inode->i_fop = &bin_fops; | 
|  | 184 | break; | 
|  | 185 | case SYSFS_KOBJ_LINK: | 
|  | 186 | inode->i_op = &sysfs_symlink_inode_operations; | 
|  | 187 | break; | 
|  | 188 | default: | 
|  | 189 | BUG(); | 
|  | 190 | } | 
|  | 191 |  | 
|  | 192 | unlock_new_inode(inode); | 
| Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 193 | } | 
|  | 194 |  | 
|  | 195 | /** | 
| Tejun Heo | 8312a8d | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 196 | *	sysfs_get_inode - get inode for sysfs_dirent | 
| Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 197 | *	@sd: sysfs_dirent to allocate inode for | 
|  | 198 | * | 
| Tejun Heo | 8312a8d | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 199 | *	Get inode for @sd.  If such inode doesn't exist, a new inode | 
|  | 200 | *	is allocated and basics are initialized.  New inode is | 
|  | 201 | *	returned locked. | 
| Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 202 | * | 
|  | 203 | *	LOCKING: | 
|  | 204 | *	Kernel thread context (may sleep). | 
|  | 205 | * | 
|  | 206 | *	RETURNS: | 
|  | 207 | *	Pointer to allocated inode on success, NULL on failure. | 
|  | 208 | */ | 
| Tejun Heo | 8312a8d | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 209 | struct inode * sysfs_get_inode(struct sysfs_dirent *sd) | 
| Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 210 | { | 
|  | 211 | struct inode *inode; | 
|  | 212 |  | 
| Tejun Heo | 8312a8d | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 213 | inode = iget_locked(sysfs_sb, sd->s_ino); | 
|  | 214 | if (inode && (inode->i_state & I_NEW)) | 
| Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 215 | sysfs_init_inode(sd, inode); | 
|  | 216 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | return inode; | 
|  | 218 | } | 
|  | 219 |  | 
| Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 220 | 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] | 221 | { | 
| Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 222 | struct sysfs_addrm_cxt acxt; | 
| Tejun Heo | 41fc1c2 | 2007-08-02 21:38:03 +0900 | [diff] [blame] | 223 | struct sysfs_dirent *sd; | 
| Greg Kroah-Hartman | 641e6f3 | 2006-03-16 15:44:26 -0800 | [diff] [blame] | 224 |  | 
| Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 225 | if (!dir_sd) | 
| Randy.Dunlap | 995982c | 2006-07-10 23:05:25 -0700 | [diff] [blame] | 226 | return -ENOENT; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 |  | 
| Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 228 | sysfs_addrm_start(&acxt, dir_sd); | 
| Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 229 |  | 
| Tejun Heo | 41fc1c2 | 2007-08-02 21:38:03 +0900 | [diff] [blame] | 230 | sd = sysfs_find_dirent(dir_sd, name); | 
|  | 231 | if (sd) | 
|  | 232 | sysfs_remove_one(&acxt, sd); | 
| Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 233 |  | 
| Tejun Heo | 990e53f | 2007-08-02 21:38:03 +0900 | [diff] [blame] | 234 | sysfs_addrm_finish(&acxt); | 
|  | 235 |  | 
|  | 236 | if (sd) | 
| Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 237 | return 0; | 
| Tejun Heo | 990e53f | 2007-08-02 21:38:03 +0900 | [diff] [blame] | 238 | else | 
|  | 239 | return -ENOENT; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | } |