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 | |
Greg Kroah-Hartman | ab9bf4b | 2013-08-21 16:21:17 -0700 | [diff] [blame] | 13 | #undef DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 21 | #include <linux/slab.h> |
Alexey Dobriyan | 57cc721 | 2011-01-10 08:18:25 +0200 | [diff] [blame] | 22 | #include <linux/sysfs.h> |
David P. Quigley | ddd29ec | 2009-09-09 14:25:37 -0400 | [diff] [blame] | 23 | #include <linux/xattr.h> |
| 24 | #include <linux/security.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include "sysfs.h" |
| 26 | |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 27 | static const struct address_space_operations sysfs_aops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | .readpage = simple_readpage, |
Nick Piggin | 800d15a | 2007-10-16 01:25:03 -0700 | [diff] [blame] | 29 | .write_begin = simple_write_begin, |
| 30 | .write_end = simple_write_end, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | static struct backing_dev_info sysfs_backing_dev_info = { |
Jens Axboe | d993831 | 2009-06-12 14:45:52 +0200 | [diff] [blame] | 34 | .name = "sysfs", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | .ra_pages = 0, /* No readahead */ |
Miklos Szeredi | e4ad08f | 2008-04-30 00:54:37 -0700 | [diff] [blame] | 36 | .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | }; |
| 38 | |
Greg Kroah-Hartman | 1b18dc2 | 2013-08-21 16:28:26 -0700 | [diff] [blame] | 39 | static const struct inode_operations sysfs_inode_operations = { |
Eric W. Biederman | e61ab4a | 2009-11-20 16:08:53 -0800 | [diff] [blame] | 40 | .permission = sysfs_permission, |
Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 41 | .setattr = sysfs_setattr, |
Eric W. Biederman | e61ab4a | 2009-11-20 16:08:53 -0800 | [diff] [blame] | 42 | .getattr = sysfs_getattr, |
David P. Quigley | ddd29ec | 2009-09-09 14:25:37 -0400 | [diff] [blame] | 43 | .setxattr = sysfs_setxattr, |
Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 44 | }; |
| 45 | |
Peter Zijlstra | e0bf68d | 2007-10-16 23:25:46 -0700 | [diff] [blame] | 46 | int __init sysfs_inode_init(void) |
| 47 | { |
| 48 | return bdi_init(&sysfs_backing_dev_info); |
| 49 | } |
| 50 | |
Stefan Richter | f38506c | 2009-10-14 20:47:32 +0200 | [diff] [blame] | 51 | static struct sysfs_inode_attrs *sysfs_init_inode_attrs(struct sysfs_dirent *sd) |
David P. Quigley | ddd29ec | 2009-09-09 14:25:37 -0400 | [diff] [blame] | 52 | { |
| 53 | struct sysfs_inode_attrs *attrs; |
| 54 | struct iattr *iattrs; |
| 55 | |
| 56 | attrs = kzalloc(sizeof(struct sysfs_inode_attrs), GFP_KERNEL); |
| 57 | if (!attrs) |
| 58 | return NULL; |
| 59 | iattrs = &attrs->ia_iattr; |
| 60 | |
| 61 | /* assign default attributes */ |
| 62 | iattrs->ia_mode = sd->s_mode; |
Eric W. Biederman | ab27b91 | 2012-02-07 15:46:26 -0800 | [diff] [blame] | 63 | iattrs->ia_uid = GLOBAL_ROOT_UID; |
| 64 | iattrs->ia_gid = GLOBAL_ROOT_GID; |
David P. Quigley | ddd29ec | 2009-09-09 14:25:37 -0400 | [diff] [blame] | 65 | iattrs->ia_atime = iattrs->ia_mtime = iattrs->ia_ctime = CURRENT_TIME; |
| 66 | |
| 67 | return attrs; |
| 68 | } |
Stefan Richter | f38506c | 2009-10-14 20:47:32 +0200 | [diff] [blame] | 69 | |
Greg Kroah-Hartman | 1b18dc2 | 2013-08-21 16:28:26 -0700 | [diff] [blame] | 70 | int sysfs_sd_setattr(struct sysfs_dirent *sd, struct iattr *iattr) |
Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 71 | { |
David P. Quigley | ddd29ec | 2009-09-09 14:25:37 -0400 | [diff] [blame] | 72 | struct sysfs_inode_attrs *sd_attrs; |
| 73 | struct iattr *iattrs; |
Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 74 | unsigned int ia_valid = iattr->ia_valid; |
Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 75 | |
David P. Quigley | ddd29ec | 2009-09-09 14:25:37 -0400 | [diff] [blame] | 76 | sd_attrs = sd->s_iattr; |
Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 77 | |
David P. Quigley | ddd29ec | 2009-09-09 14:25:37 -0400 | [diff] [blame] | 78 | if (!sd_attrs) { |
Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 79 | /* setting attributes for the first time, allocate now */ |
David P. Quigley | ddd29ec | 2009-09-09 14:25:37 -0400 | [diff] [blame] | 80 | sd_attrs = sysfs_init_inode_attrs(sd); |
| 81 | if (!sd_attrs) |
Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 82 | return -ENOMEM; |
David P. Quigley | ddd29ec | 2009-09-09 14:25:37 -0400 | [diff] [blame] | 83 | sd->s_iattr = sd_attrs; |
Eric W. Biederman | 7c0ff87 | 2010-02-03 23:13:24 -0800 | [diff] [blame] | 84 | } |
| 85 | /* attributes were changed at least once in past */ |
| 86 | iattrs = &sd_attrs->ia_iattr; |
David P. Quigley | ddd29ec | 2009-09-09 14:25:37 -0400 | [diff] [blame] | 87 | |
Eric W. Biederman | 7c0ff87 | 2010-02-03 23:13:24 -0800 | [diff] [blame] | 88 | if (ia_valid & ATTR_UID) |
| 89 | iattrs->ia_uid = iattr->ia_uid; |
| 90 | if (ia_valid & ATTR_GID) |
| 91 | iattrs->ia_gid = iattr->ia_gid; |
| 92 | if (ia_valid & ATTR_ATIME) |
| 93 | iattrs->ia_atime = iattr->ia_atime; |
| 94 | if (ia_valid & ATTR_MTIME) |
| 95 | iattrs->ia_mtime = iattr->ia_mtime; |
| 96 | if (ia_valid & ATTR_CTIME) |
| 97 | iattrs->ia_ctime = iattr->ia_ctime; |
| 98 | if (ia_valid & ATTR_MODE) { |
| 99 | umode_t mode = iattr->ia_mode; |
| 100 | iattrs->ia_mode = sd->s_mode = mode; |
Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 101 | } |
Eric W. Biederman | 35df63c | 2009-11-20 16:08:50 -0800 | [diff] [blame] | 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | int sysfs_setattr(struct dentry *dentry, struct iattr *iattr) |
| 106 | { |
| 107 | struct inode *inode = dentry->d_inode; |
| 108 | struct sysfs_dirent *sd = dentry->d_fsdata; |
| 109 | int error; |
| 110 | |
| 111 | if (!sd) |
| 112 | return -EINVAL; |
| 113 | |
Eric W. Biederman | f8d4f61 | 2010-02-12 19:22:23 -0800 | [diff] [blame] | 114 | mutex_lock(&sysfs_mutex); |
Eric W. Biederman | 35df63c | 2009-11-20 16:08:50 -0800 | [diff] [blame] | 115 | error = inode_change_ok(inode, iattr); |
| 116 | if (error) |
Eric W. Biederman | f8d4f61 | 2010-02-12 19:22:23 -0800 | [diff] [blame] | 117 | goto out; |
Eric W. Biederman | 35df63c | 2009-11-20 16:08:50 -0800 | [diff] [blame] | 118 | |
Nick Piggin | 75de46b | 2010-05-31 17:58:02 +1000 | [diff] [blame] | 119 | error = sysfs_sd_setattr(sd, iattr); |
| 120 | if (error) |
| 121 | goto out; |
| 122 | |
Nick Piggin | 3322e79 | 2010-05-27 22:42:19 +1000 | [diff] [blame] | 123 | /* this ignores size changes */ |
Christoph Hellwig | 6a1a90a | 2010-06-04 11:30:00 +0200 | [diff] [blame] | 124 | setattr_copy(inode, iattr); |
Eric W. Biederman | 35df63c | 2009-11-20 16:08:50 -0800 | [diff] [blame] | 125 | |
Eric W. Biederman | f8d4f61 | 2010-02-12 19:22:23 -0800 | [diff] [blame] | 126 | out: |
Eric W. Biederman | 35df63c | 2009-11-20 16:08:50 -0800 | [diff] [blame] | 127 | mutex_unlock(&sysfs_mutex); |
David P. Quigley | ddd29ec | 2009-09-09 14:25:37 -0400 | [diff] [blame] | 128 | return error; |
| 129 | } |
Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 130 | |
Greg Kroah-Hartman | ddfd6d0 | 2013-08-21 16:33:34 -0700 | [diff] [blame] | 131 | static int sysfs_sd_setsecdata(struct sysfs_dirent *sd, void **secdata, |
| 132 | u32 *secdata_len) |
Eric W. Biederman | f44d3e7 | 2009-11-07 23:26:59 -0800 | [diff] [blame] | 133 | { |
| 134 | struct sysfs_inode_attrs *iattrs; |
| 135 | void *old_secdata; |
| 136 | size_t old_secdata_len; |
| 137 | |
Masami Ichikawa | 93518dd | 2012-02-21 07:43:50 +0900 | [diff] [blame] | 138 | if (!sd->s_iattr) { |
| 139 | sd->s_iattr = sysfs_init_inode_attrs(sd); |
| 140 | if (!sd->s_iattr) |
| 141 | return -ENOMEM; |
| 142 | } |
Eric W. Biederman | f44d3e7 | 2009-11-07 23:26:59 -0800 | [diff] [blame] | 143 | |
Masami Ichikawa | 93518dd | 2012-02-21 07:43:50 +0900 | [diff] [blame] | 144 | iattrs = sd->s_iattr; |
Eric W. Biederman | f44d3e7 | 2009-11-07 23:26:59 -0800 | [diff] [blame] | 145 | old_secdata = iattrs->ia_secdata; |
| 146 | old_secdata_len = iattrs->ia_secdata_len; |
| 147 | |
| 148 | iattrs->ia_secdata = *secdata; |
| 149 | iattrs->ia_secdata_len = *secdata_len; |
| 150 | |
| 151 | *secdata = old_secdata; |
| 152 | *secdata_len = old_secdata_len; |
| 153 | return 0; |
| 154 | } |
| 155 | |
David P. Quigley | ddd29ec | 2009-09-09 14:25:37 -0400 | [diff] [blame] | 156 | int sysfs_setxattr(struct dentry *dentry, const char *name, const void *value, |
| 157 | size_t size, int flags) |
| 158 | { |
| 159 | struct sysfs_dirent *sd = dentry->d_fsdata; |
David P. Quigley | ddd29ec | 2009-09-09 14:25:37 -0400 | [diff] [blame] | 160 | void *secdata; |
| 161 | int error; |
| 162 | u32 secdata_len = 0; |
Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 163 | |
David P. Quigley | ddd29ec | 2009-09-09 14:25:37 -0400 | [diff] [blame] | 164 | if (!sd) |
| 165 | return -EINVAL; |
Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 166 | |
David P. Quigley | ddd29ec | 2009-09-09 14:25:37 -0400 | [diff] [blame] | 167 | if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)) { |
| 168 | const char *suffix = name + XATTR_SECURITY_PREFIX_LEN; |
| 169 | error = security_inode_setsecurity(dentry->d_inode, suffix, |
| 170 | value, size, flags); |
| 171 | if (error) |
| 172 | goto out; |
| 173 | error = security_inode_getsecctx(dentry->d_inode, |
| 174 | &secdata, &secdata_len); |
| 175 | if (error) |
| 176 | goto out; |
David P. Quigley | ddd29ec | 2009-09-09 14:25:37 -0400 | [diff] [blame] | 177 | |
Eric W. Biederman | f44d3e7 | 2009-11-07 23:26:59 -0800 | [diff] [blame] | 178 | mutex_lock(&sysfs_mutex); |
| 179 | error = sysfs_sd_setsecdata(sd, &secdata, &secdata_len); |
| 180 | mutex_unlock(&sysfs_mutex); |
| 181 | |
| 182 | if (secdata) |
| 183 | security_release_secctx(secdata, secdata_len); |
David P. Quigley | ddd29ec | 2009-09-09 14:25:37 -0400 | [diff] [blame] | 184 | } else |
| 185 | return -EINVAL; |
| 186 | out: |
Maneesh Soni | 988d186 | 2005-05-31 10:39:14 +0530 | [diff] [blame] | 187 | return error; |
| 188 | } |
| 189 | |
Greg Kroah-Hartman | 1b18dc2 | 2013-08-21 16:28:26 -0700 | [diff] [blame] | 190 | static inline void set_default_inode_attr(struct inode *inode, umode_t mode) |
Maneesh Soni | 8215534 | 2005-05-31 10:39:52 +0530 | [diff] [blame] | 191 | { |
| 192 | inode->i_mode = mode; |
Maneesh Soni | 8215534 | 2005-05-31 10:39:52 +0530 | [diff] [blame] | 193 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 194 | } |
| 195 | |
Greg Kroah-Hartman | 1b18dc2 | 2013-08-21 16:28:26 -0700 | [diff] [blame] | 196 | static inline void set_inode_attr(struct inode *inode, struct iattr *iattr) |
Maneesh Soni | 8215534 | 2005-05-31 10:39:52 +0530 | [diff] [blame] | 197 | { |
Maneesh Soni | 8215534 | 2005-05-31 10:39:52 +0530 | [diff] [blame] | 198 | inode->i_uid = iattr->ia_uid; |
| 199 | inode->i_gid = iattr->ia_gid; |
| 200 | inode->i_atime = iattr->ia_atime; |
| 201 | inode->i_mtime = iattr->ia_mtime; |
| 202 | inode->i_ctime = iattr->ia_ctime; |
| 203 | } |
| 204 | |
Eric W. Biederman | e61ab4a | 2009-11-20 16:08:53 -0800 | [diff] [blame] | 205 | static void sysfs_refresh_inode(struct sysfs_dirent *sd, struct inode *inode) |
| 206 | { |
| 207 | struct sysfs_inode_attrs *iattrs = sd->s_iattr; |
| 208 | |
| 209 | inode->i_mode = sd->s_mode; |
| 210 | if (iattrs) { |
| 211 | /* sysfs_dirent has non-default attributes |
| 212 | * get them from persistent copy in sysfs_dirent |
| 213 | */ |
| 214 | set_inode_attr(inode, &iattrs->ia_iattr); |
| 215 | security_inode_notifysecctx(inode, |
| 216 | iattrs->ia_secdata, |
| 217 | iattrs->ia_secdata_len); |
| 218 | } |
Greg Kroah-Hartman | 54d20f0 | 2012-03-08 13:03:10 -0800 | [diff] [blame] | 219 | |
| 220 | if (sysfs_type(sd) == SYSFS_DIR) |
| 221 | set_nlink(inode, sd->s_dir.subdirs + 2); |
Eric W. Biederman | e61ab4a | 2009-11-20 16:08:53 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Greg Kroah-Hartman | ddfd6d0 | 2013-08-21 16:33:34 -0700 | [diff] [blame] | 224 | int sysfs_getattr(struct vfsmount *mnt, struct dentry *dentry, |
| 225 | struct kstat *stat) |
Eric W. Biederman | e61ab4a | 2009-11-20 16:08:53 -0800 | [diff] [blame] | 226 | { |
| 227 | struct sysfs_dirent *sd = dentry->d_fsdata; |
| 228 | struct inode *inode = dentry->d_inode; |
| 229 | |
| 230 | mutex_lock(&sysfs_mutex); |
| 231 | sysfs_refresh_inode(sd, inode); |
| 232 | mutex_unlock(&sysfs_mutex); |
| 233 | |
| 234 | generic_fillattr(inode, stat); |
| 235 | return 0; |
| 236 | } |
| 237 | |
Tejun Heo | bc37e28 | 2007-07-18 14:30:28 +0900 | [diff] [blame] | 238 | static void sysfs_init_inode(struct sysfs_dirent *sd, struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | { |
Eric W. Biederman | 372e88b | 2007-08-20 21:36:29 +0900 | [diff] [blame] | 240 | struct bin_attribute *bin_attr; |
| 241 | |
Eric W. Biederman | 04256b4 | 2009-02-11 13:20:23 -0800 | [diff] [blame] | 242 | inode->i_private = sysfs_get(sd); |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 243 | inode->i_mapping->a_ops = &sysfs_aops; |
| 244 | inode->i_mapping->backing_dev_info = &sysfs_backing_dev_info; |
| 245 | inode->i_op = &sysfs_inode_operations; |
Maneesh Soni | 8215534 | 2005-05-31 10:39:52 +0530 | [diff] [blame] | 246 | |
Eric W. Biederman | e61ab4a | 2009-11-20 16:08:53 -0800 | [diff] [blame] | 247 | set_default_inode_attr(inode, sd->s_mode); |
| 248 | sysfs_refresh_inode(sd, inode); |
Eric W. Biederman | 372e88b | 2007-08-20 21:36:29 +0900 | [diff] [blame] | 249 | |
Eric W. Biederman | 372e88b | 2007-08-20 21:36:29 +0900 | [diff] [blame] | 250 | /* initialize inode according to type */ |
| 251 | switch (sysfs_type(sd)) { |
Eric W. Biederman | 372e88b | 2007-08-20 21:36:29 +0900 | [diff] [blame] | 252 | case SYSFS_DIR: |
| 253 | inode->i_op = &sysfs_dir_inode_operations; |
| 254 | inode->i_fop = &sysfs_dir_operations; |
Eric W. Biederman | 372e88b | 2007-08-20 21:36:29 +0900 | [diff] [blame] | 255 | break; |
| 256 | case SYSFS_KOBJ_ATTR: |
| 257 | inode->i_size = PAGE_SIZE; |
| 258 | inode->i_fop = &sysfs_file_operations; |
| 259 | break; |
| 260 | case SYSFS_KOBJ_BIN_ATTR: |
Tejun Heo | b1fc3d6 | 2007-09-20 16:05:11 +0900 | [diff] [blame] | 261 | bin_attr = sd->s_bin_attr.bin_attr; |
Eric W. Biederman | 372e88b | 2007-08-20 21:36:29 +0900 | [diff] [blame] | 262 | inode->i_size = bin_attr->size; |
| 263 | inode->i_fop = &bin_fops; |
| 264 | break; |
| 265 | case SYSFS_KOBJ_LINK: |
| 266 | inode->i_op = &sysfs_symlink_inode_operations; |
| 267 | break; |
| 268 | default: |
| 269 | BUG(); |
| 270 | } |
| 271 | |
| 272 | unlock_new_inode(inode); |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /** |
Tejun Heo | 8312a8d | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 276 | * sysfs_get_inode - get inode for sysfs_dirent |
Eric W. Biederman | fac2622 | 2010-02-12 19:22:27 -0800 | [diff] [blame] | 277 | * @sb: super block |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 278 | * @sd: sysfs_dirent to allocate inode for |
| 279 | * |
Tejun Heo | 8312a8d | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 280 | * Get inode for @sd. If such inode doesn't exist, a new inode |
| 281 | * is allocated and basics are initialized. New inode is |
| 282 | * returned locked. |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 283 | * |
| 284 | * LOCKING: |
| 285 | * Kernel thread context (may sleep). |
| 286 | * |
| 287 | * RETURNS: |
| 288 | * Pointer to allocated inode on success, NULL on failure. |
| 289 | */ |
Greg Kroah-Hartman | 1b18dc2 | 2013-08-21 16:28:26 -0700 | [diff] [blame] | 290 | struct inode *sysfs_get_inode(struct super_block *sb, struct sysfs_dirent *sd) |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 291 | { |
| 292 | struct inode *inode; |
| 293 | |
Eric W. Biederman | fac2622 | 2010-02-12 19:22:27 -0800 | [diff] [blame] | 294 | inode = iget_locked(sb, sd->s_ino); |
Tejun Heo | 8312a8d | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 295 | if (inode && (inode->i_state & I_NEW)) |
Tejun Heo | fc9f54b | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 296 | sysfs_init_inode(sd, inode); |
| 297 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | return inode; |
| 299 | } |
| 300 | |
Eric W. Biederman | 04256b4 | 2009-02-11 13:20:23 -0800 | [diff] [blame] | 301 | /* |
| 302 | * The sysfs_dirent serves as both an inode and a directory entry for sysfs. |
| 303 | * To prevent the sysfs inode numbers from being freed prematurely we take a |
| 304 | * reference to sysfs_dirent from the sysfs inode. A |
Al Viro | 01cd9fe | 2010-06-04 22:21:54 -0400 | [diff] [blame] | 305 | * super_operations.evict_inode() implementation is needed to drop that |
Eric W. Biederman | 04256b4 | 2009-02-11 13:20:23 -0800 | [diff] [blame] | 306 | * reference upon inode destruction. |
| 307 | */ |
Al Viro | 01cd9fe | 2010-06-04 22:21:54 -0400 | [diff] [blame] | 308 | void sysfs_evict_inode(struct inode *inode) |
Eric W. Biederman | 04256b4 | 2009-02-11 13:20:23 -0800 | [diff] [blame] | 309 | { |
| 310 | struct sysfs_dirent *sd = inode->i_private; |
| 311 | |
| 312 | truncate_inode_pages(&inode->i_data, 0); |
Jan Kara | dbd5768 | 2012-05-03 14:48:02 +0200 | [diff] [blame] | 313 | clear_inode(inode); |
Eric W. Biederman | 04256b4 | 2009-02-11 13:20:23 -0800 | [diff] [blame] | 314 | sysfs_put(sd); |
| 315 | } |
| 316 | |
Greg Kroah-Hartman | ddfd6d0 | 2013-08-21 16:33:34 -0700 | [diff] [blame] | 317 | int sysfs_hash_and_remove(struct sysfs_dirent *dir_sd, const void *ns, |
| 318 | const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | { |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 320 | struct sysfs_addrm_cxt acxt; |
Tejun Heo | 41fc1c2 | 2007-08-02 21:38:03 +0900 | [diff] [blame] | 321 | struct sysfs_dirent *sd; |
Greg Kroah-Hartman | 641e6f3 | 2006-03-16 15:44:26 -0800 | [diff] [blame] | 322 | |
Eric W. Biederman | ce59791 | 2012-01-13 21:32:59 -0800 | [diff] [blame] | 323 | if (!dir_sd) { |
| 324 | WARN(1, KERN_WARNING "sysfs: can not remove '%s', no directory\n", |
| 325 | name); |
Randy.Dunlap | 995982c | 2006-07-10 23:05:25 -0700 | [diff] [blame] | 326 | return -ENOENT; |
Eric W. Biederman | ce59791 | 2012-01-13 21:32:59 -0800 | [diff] [blame] | 327 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 329 | sysfs_addrm_start(&acxt, dir_sd); |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 330 | |
Eric W. Biederman | 3ff195b | 2010-03-30 11:31:26 -0700 | [diff] [blame] | 331 | sd = sysfs_find_dirent(dir_sd, ns, name); |
Tejun Heo | 41fc1c2 | 2007-08-02 21:38:03 +0900 | [diff] [blame] | 332 | if (sd) |
| 333 | sysfs_remove_one(&acxt, sd); |
Tejun Heo | 3007e99 | 2007-06-14 04:27:23 +0900 | [diff] [blame] | 334 | |
Tejun Heo | 990e53f | 2007-08-02 21:38:03 +0900 | [diff] [blame] | 335 | sysfs_addrm_finish(&acxt); |
| 336 | |
| 337 | if (sd) |
Tejun Heo | fb6896d | 2007-06-14 04:27:24 +0900 | [diff] [blame] | 338 | return 0; |
Tejun Heo | 990e53f | 2007-08-02 21:38:03 +0900 | [diff] [blame] | 339 | else |
| 340 | return -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | } |
Eric W. Biederman | e61ab4a | 2009-11-20 16:08:53 -0800 | [diff] [blame] | 342 | |
Al Viro | 10556cb | 2011-06-20 19:28:19 -0400 | [diff] [blame] | 343 | int sysfs_permission(struct inode *inode, int mask) |
Eric W. Biederman | e61ab4a | 2009-11-20 16:08:53 -0800 | [diff] [blame] | 344 | { |
Nick Piggin | b74c79e | 2011-01-07 17:49:58 +1100 | [diff] [blame] | 345 | struct sysfs_dirent *sd; |
| 346 | |
Al Viro | 10556cb | 2011-06-20 19:28:19 -0400 | [diff] [blame] | 347 | if (mask & MAY_NOT_BLOCK) |
Nick Piggin | b74c79e | 2011-01-07 17:49:58 +1100 | [diff] [blame] | 348 | return -ECHILD; |
| 349 | |
| 350 | sd = inode->i_private; |
Eric W. Biederman | e61ab4a | 2009-11-20 16:08:53 -0800 | [diff] [blame] | 351 | |
| 352 | mutex_lock(&sysfs_mutex); |
| 353 | sysfs_refresh_inode(sd, inode); |
| 354 | mutex_unlock(&sysfs_mutex); |
| 355 | |
Al Viro | 2830ba7 | 2011-06-20 19:16:29 -0400 | [diff] [blame] | 356 | return generic_permission(inode, mask); |
Eric W. Biederman | e61ab4a | 2009-11-20 16:08:53 -0800 | [diff] [blame] | 357 | } |