diff options
| author | Tejun Heo <tj@kernel.org> | 2013-11-28 14:54:32 -0500 |
|---|---|---|
| committer | Greg Kroah-Hartman <gregkh@linuxfoundation.org> | 2013-11-29 20:55:10 -0500 |
| commit | ffed24e22845a3da0ae01095ae3f11c8d16e889d (patch) | |
| tree | 7431e61de58265fbc2e58dbe6a6e1d4053875f79 /fs/sysfs | |
| parent | ae6621b0716852146e4655fef7f74a181faa6c81 (diff) | |
sysfs, kernfs: move inode code to fs/kernfs/inode.c
There's nothing sysfs-specific in fs/sysfs/inode.c. Move everything
in it to fs/kernfs/inode.c. The respective declarations in
fs/sysfs/sysfs.h are moved to fs/kernfs/kernfs-internal.h.
This is pure relocation.
Signed-off-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'fs/sysfs')
| -rw-r--r-- | fs/sysfs/Makefile | 2 | ||||
| -rw-r--r-- | fs/sysfs/inode.c | 342 | ||||
| -rw-r--r-- | fs/sysfs/sysfs.h | 13 |
3 files changed, 1 insertions, 356 deletions
diff --git a/fs/sysfs/Makefile b/fs/sysfs/Makefile index 8876ac183373..6eff6e1205a5 100644 --- a/fs/sysfs/Makefile +++ b/fs/sysfs/Makefile | |||
| @@ -2,4 +2,4 @@ | |||
| 2 | # Makefile for the sysfs virtual filesystem | 2 | # Makefile for the sysfs virtual filesystem |
| 3 | # | 3 | # |
| 4 | 4 | ||
| 5 | obj-y := inode.o file.o dir.o symlink.o mount.o group.o | 5 | obj-y := file.o dir.o symlink.o mount.o group.o |
diff --git a/fs/sysfs/inode.c b/fs/sysfs/inode.c deleted file mode 100644 index bfe4478f82bf..000000000000 --- a/fs/sysfs/inode.c +++ /dev/null | |||
| @@ -1,342 +0,0 @@ | |||
| 1 | /* | ||
| 2 | * fs/sysfs/inode.c - basic sysfs inode and dentry operations | ||
| 3 | * | ||
| 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. | ||
| 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> | ||
| 18 | #include <linux/capability.h> | ||
| 19 | #include <linux/errno.h> | ||
| 20 | #include <linux/sched.h> | ||
| 21 | #include <linux/slab.h> | ||
| 22 | #include <linux/sysfs.h> | ||
| 23 | #include <linux/xattr.h> | ||
| 24 | #include <linux/security.h> | ||
| 25 | #include "sysfs.h" | ||
| 26 | |||
| 27 | static const struct address_space_operations sysfs_aops = { | ||
| 28 | .readpage = simple_readpage, | ||
| 29 | .write_begin = simple_write_begin, | ||
| 30 | .write_end = simple_write_end, | ||
| 31 | }; | ||
| 32 | |||
| 33 | static struct backing_dev_info sysfs_backing_dev_info = { | ||
| 34 | .name = "sysfs", | ||
| 35 | .ra_pages = 0, /* No readahead */ | ||
| 36 | .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK, | ||
| 37 | }; | ||
| 38 | |||
| 39 | static const struct inode_operations sysfs_inode_operations = { | ||
| 40 | .permission = sysfs_permission, | ||
| 41 | .setattr = sysfs_setattr, | ||
| 42 | .getattr = sysfs_getattr, | ||
| 43 | .setxattr = sysfs_setxattr, | ||
| 44 | }; | ||
| 45 | |||
| 46 | int __init sysfs_inode_init(void) | ||
| 47 | { | ||
| 48 | return bdi_init(&sysfs_backing_dev_info); | ||
| 49 | } | ||
| 50 | |||
| 51 | static struct sysfs_inode_attrs *sysfs_init_inode_attrs(struct sysfs_dirent *sd) | ||
| 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; | ||
| 63 | iattrs->ia_uid = GLOBAL_ROOT_UID; | ||
| 64 | iattrs->ia_gid = GLOBAL_ROOT_GID; | ||
| 65 | iattrs->ia_atime = iattrs->ia_mtime = iattrs->ia_ctime = CURRENT_TIME; | ||
| 66 | |||
| 67 | return attrs; | ||
| 68 | } | ||
| 69 | |||
| 70 | static int __kernfs_setattr(struct sysfs_dirent *sd, const struct iattr *iattr) | ||
| 71 | { | ||
| 72 | struct sysfs_inode_attrs *sd_attrs; | ||
| 73 | struct iattr *iattrs; | ||
| 74 | unsigned int ia_valid = iattr->ia_valid; | ||
| 75 | |||
| 76 | sd_attrs = sd->s_iattr; | ||
| 77 | |||
| 78 | if (!sd_attrs) { | ||
| 79 | /* setting attributes for the first time, allocate now */ | ||
| 80 | sd_attrs = sysfs_init_inode_attrs(sd); | ||
| 81 | if (!sd_attrs) | ||
| 82 | return -ENOMEM; | ||
| 83 | sd->s_iattr = sd_attrs; | ||
| 84 | } | ||
| 85 | /* attributes were changed at least once in past */ | ||
| 86 | iattrs = &sd_attrs->ia_iattr; | ||
| 87 | |||
| 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; | ||
| 101 | } | ||
| 102 | return 0; | ||
| 103 | } | ||
| 104 | |||
| 105 | /** | ||
| 106 | * kernfs_setattr - set iattr on a node | ||
| 107 | * @sd: target node | ||
| 108 | * @iattr: iattr to set | ||
| 109 | * | ||
| 110 | * Returns 0 on success, -errno on failure. | ||
| 111 | */ | ||
| 112 | int kernfs_setattr(struct sysfs_dirent *sd, const struct iattr *iattr) | ||
| 113 | { | ||
| 114 | int ret; | ||
| 115 | |||
| 116 | mutex_lock(&sysfs_mutex); | ||
| 117 | ret = __kernfs_setattr(sd, iattr); | ||
| 118 | mutex_unlock(&sysfs_mutex); | ||
| 119 | return ret; | ||
| 120 | } | ||
| 121 | |||
| 122 | int sysfs_setattr(struct dentry *dentry, struct iattr *iattr) | ||
| 123 | { | ||
| 124 | struct inode *inode = dentry->d_inode; | ||
| 125 | struct sysfs_dirent *sd = dentry->d_fsdata; | ||
| 126 | int error; | ||
| 127 | |||
| 128 | if (!sd) | ||
| 129 | return -EINVAL; | ||
| 130 | |||
| 131 | mutex_lock(&sysfs_mutex); | ||
| 132 | error = inode_change_ok(inode, iattr); | ||
| 133 | if (error) | ||
| 134 | goto out; | ||
| 135 | |||
| 136 | error = __kernfs_setattr(sd, iattr); | ||
| 137 | if (error) | ||
| 138 | goto out; | ||
| 139 | |||
| 140 | /* this ignores size changes */ | ||
| 141 | setattr_copy(inode, iattr); | ||
| 142 | |||
| 143 | out: | ||
| 144 | mutex_unlock(&sysfs_mutex); | ||
| 145 | return error; | ||
| 146 | } | ||
| 147 | |||
| 148 | static int sysfs_sd_setsecdata(struct sysfs_dirent *sd, void **secdata, | ||
| 149 | u32 *secdata_len) | ||
| 150 | { | ||
| 151 | struct sysfs_inode_attrs *iattrs; | ||
| 152 | void *old_secdata; | ||
| 153 | size_t old_secdata_len; | ||
| 154 | |||
| 155 | if (!sd->s_iattr) { | ||
| 156 | sd->s_iattr = sysfs_init_inode_attrs(sd); | ||
| 157 | if (!sd->s_iattr) | ||
| 158 | return -ENOMEM; | ||
| 159 | } | ||
| 160 | |||
| 161 | iattrs = sd->s_iattr; | ||
| 162 | old_secdata = iattrs->ia_secdata; | ||
| 163 | old_secdata_len = iattrs->ia_secdata_len; | ||
| 164 | |||
| 165 | iattrs->ia_secdata = *secdata; | ||
| 166 | iattrs->ia_secdata_len = *secdata_len; | ||
| 167 | |||
| 168 | *secdata = old_secdata; | ||
| 169 | *secdata_len = old_secdata_len; | ||
| 170 | return 0; | ||
| 171 | } | ||
| 172 | |||
| 173 | int sysfs_setxattr(struct dentry *dentry, const char *name, const void *value, | ||
| 174 | size_t size, int flags) | ||
| 175 | { | ||
| 176 | struct sysfs_dirent *sd = dentry->d_fsdata; | ||
| 177 | void *secdata; | ||
| 178 | int error; | ||
| 179 | u32 secdata_len = 0; | ||
| 180 | |||
| 181 | if (!sd) | ||
| 182 | return -EINVAL; | ||
| 183 | |||
| 184 | if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)) { | ||
| 185 | const char *suffix = name + XATTR_SECURITY_PREFIX_LEN; | ||
| 186 | error = security_inode_setsecurity(dentry->d_inode, suffix, | ||
| 187 | value, size, flags); | ||
| 188 | if (error) | ||
| 189 | goto out; | ||
| 190 | error = security_inode_getsecctx(dentry->d_inode, | ||
| 191 | &secdata, &secdata_len); | ||
| 192 | if (error) | ||
| 193 | goto out; | ||
| 194 | |||
| 195 | mutex_lock(&sysfs_mutex); | ||
| 196 | error = sysfs_sd_setsecdata(sd, &secdata, &secdata_len); | ||
| 197 | mutex_unlock(&sysfs_mutex); | ||
| 198 | |||
| 199 | if (secdata) | ||
| 200 | security_release_secctx(secdata, secdata_len); | ||
| 201 | } else | ||
| 202 | return -EINVAL; | ||
| 203 | out: | ||
| 204 | return error; | ||
| 205 | } | ||
| 206 | |||
| 207 | static inline void set_default_inode_attr(struct inode *inode, umode_t mode) | ||
| 208 | { | ||
| 209 | inode->i_mode = mode; | ||
| 210 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; | ||
| 211 | } | ||
| 212 | |||
| 213 | static inline void set_inode_attr(struct inode *inode, struct iattr *iattr) | ||
| 214 | { | ||
| 215 | inode->i_uid = iattr->ia_uid; | ||
| 216 | inode->i_gid = iattr->ia_gid; | ||
| 217 | inode->i_atime = iattr->ia_atime; | ||
| 218 | inode->i_mtime = iattr->ia_mtime; | ||
| 219 | inode->i_ctime = iattr->ia_ctime; | ||
| 220 | } | ||
| 221 | |||
| 222 | static void sysfs_refresh_inode(struct sysfs_dirent *sd, struct inode *inode) | ||
| 223 | { | ||
| 224 | struct sysfs_inode_attrs *iattrs = sd->s_iattr; | ||
| 225 | |||
| 226 | inode->i_mode = sd->s_mode; | ||
| 227 | if (iattrs) { | ||
| 228 | /* sysfs_dirent has non-default attributes | ||
| 229 | * get them from persistent copy in sysfs_dirent | ||
| 230 | */ | ||
| 231 | set_inode_attr(inode, &iattrs->ia_iattr); | ||
| 232 | security_inode_notifysecctx(inode, | ||
| 233 | iattrs->ia_secdata, | ||
| 234 | iattrs->ia_secdata_len); | ||
| 235 | } | ||
| 236 | |||
| 237 | if (sysfs_type(sd) == SYSFS_DIR) | ||
| 238 | set_nlink(inode, sd->s_dir.subdirs + 2); | ||
| 239 | } | ||
| 240 | |||
| 241 | int sysfs_getattr(struct vfsmount *mnt, struct dentry *dentry, | ||
| 242 | struct kstat *stat) | ||
| 243 | { | ||
| 244 | struct sysfs_dirent *sd = dentry->d_fsdata; | ||
| 245 | struct inode *inode = dentry->d_inode; | ||
| 246 | |||
| 247 | mutex_lock(&sysfs_mutex); | ||
| 248 | sysfs_refresh_inode(sd, inode); | ||
| 249 | mutex_unlock(&sysfs_mutex); | ||
| 250 | |||
| 251 | generic_fillattr(inode, stat); | ||
| 252 | return 0; | ||
| 253 | } | ||
| 254 | |||
| 255 | static void sysfs_init_inode(struct sysfs_dirent *sd, struct inode *inode) | ||
| 256 | { | ||
| 257 | kernfs_get(sd); | ||
| 258 | inode->i_private = sd; | ||
| 259 | inode->i_mapping->a_ops = &sysfs_aops; | ||
| 260 | inode->i_mapping->backing_dev_info = &sysfs_backing_dev_info; | ||
| 261 | inode->i_op = &sysfs_inode_operations; | ||
| 262 | |||
| 263 | set_default_inode_attr(inode, sd->s_mode); | ||
| 264 | sysfs_refresh_inode(sd, inode); | ||
| 265 | |||
| 266 | /* initialize inode according to type */ | ||
| 267 | switch (sysfs_type(sd)) { | ||
| 268 | case SYSFS_DIR: | ||
| 269 | inode->i_op = &sysfs_dir_inode_operations; | ||
| 270 | inode->i_fop = &sysfs_dir_operations; | ||
| 271 | break; | ||
| 272 | case SYSFS_KOBJ_ATTR: | ||
| 273 | inode->i_size = sd->s_attr.size; | ||
| 274 | inode->i_fop = &kernfs_file_operations; | ||
| 275 | break; | ||
| 276 | case SYSFS_KOBJ_LINK: | ||
| 277 | inode->i_op = &sysfs_symlink_inode_operations; | ||
| 278 | break; | ||
| 279 | default: | ||
| 280 | BUG(); | ||
| 281 | } | ||
| 282 | |||
| 283 | unlock_new_inode(inode); | ||
| 284 | } | ||
| 285 | |||
| 286 | /** | ||
| 287 | * sysfs_get_inode - get inode for sysfs_dirent | ||
| 288 | * @sb: super block | ||
| 289 | * @sd: sysfs_dirent to allocate inode for | ||
| 290 | * | ||
| 291 | * Get inode for @sd. If such inode doesn't exist, a new inode | ||
| 292 | * is allocated and basics are initialized. New inode is | ||
| 293 | * returned locked. | ||
| 294 | * | ||
| 295 | * LOCKING: | ||
| 296 | * Kernel thread context (may sleep). | ||
| 297 | * | ||
| 298 | * RETURNS: | ||
| 299 | * Pointer to allocated inode on success, NULL on failure. | ||
| 300 | */ | ||
| 301 | struct inode *sysfs_get_inode(struct super_block *sb, struct sysfs_dirent *sd) | ||
| 302 | { | ||
| 303 | struct inode *inode; | ||
| 304 | |||
| 305 | inode = iget_locked(sb, sd->s_ino); | ||
| 306 | if (inode && (inode->i_state & I_NEW)) | ||
| 307 | sysfs_init_inode(sd, inode); | ||
| 308 | |||
| 309 | return inode; | ||
| 310 | } | ||
| 311 | |||
| 312 | /* | ||
| 313 | * The sysfs_dirent serves as both an inode and a directory entry for sysfs. | ||
| 314 | * To prevent the sysfs inode numbers from being freed prematurely we take a | ||
| 315 | * reference to sysfs_dirent from the sysfs inode. A | ||
| 316 | * super_operations.evict_inode() implementation is needed to drop that | ||
| 317 | * reference upon inode destruction. | ||
| 318 | */ | ||
| 319 | void sysfs_evict_inode(struct inode *inode) | ||
| 320 | { | ||
| 321 | struct sysfs_dirent *sd = inode->i_private; | ||
| 322 | |||
| 323 | truncate_inode_pages(&inode->i_data, 0); | ||
| 324 | clear_inode(inode); | ||
| 325 | kernfs_put(sd); | ||
| 326 | } | ||
| 327 | |||
| 328 | int sysfs_permission(struct inode *inode, int mask) | ||
| 329 | { | ||
| 330 | struct sysfs_dirent *sd; | ||
| 331 | |||
| 332 | if (mask & MAY_NOT_BLOCK) | ||
| 333 | return -ECHILD; | ||
| 334 | |||
| 335 | sd = inode->i_private; | ||
| 336 | |||
| 337 | mutex_lock(&sysfs_mutex); | ||
| 338 | sysfs_refresh_inode(sd, inode); | ||
| 339 | mutex_unlock(&sysfs_mutex); | ||
| 340 | |||
| 341 | return generic_permission(inode, mask); | ||
| 342 | } | ||
diff --git a/fs/sysfs/sysfs.h b/fs/sysfs/sysfs.h index f8c936f31b37..93c1910783fc 100644 --- a/fs/sysfs/sysfs.h +++ b/fs/sysfs/sysfs.h | |||
| @@ -52,19 +52,6 @@ void sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt); | |||
| 52 | struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type); | 52 | struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type); |
| 53 | 53 | ||
| 54 | /* | 54 | /* |
| 55 | * inode.c | ||
| 56 | */ | ||
| 57 | struct inode *sysfs_get_inode(struct super_block *sb, struct sysfs_dirent *sd); | ||
| 58 | void sysfs_evict_inode(struct inode *inode); | ||
| 59 | int sysfs_permission(struct inode *inode, int mask); | ||
| 60 | int sysfs_setattr(struct dentry *dentry, struct iattr *iattr); | ||
| 61 | int sysfs_getattr(struct vfsmount *mnt, struct dentry *dentry, | ||
| 62 | struct kstat *stat); | ||
| 63 | int sysfs_setxattr(struct dentry *dentry, const char *name, const void *value, | ||
| 64 | size_t size, int flags); | ||
| 65 | int sysfs_inode_init(void); | ||
| 66 | |||
| 67 | /* | ||
| 68 | * file.c | 55 | * file.c |
| 69 | */ | 56 | */ |
| 70 | extern const struct file_operations kernfs_file_operations; | 57 | extern const struct file_operations kernfs_file_operations; |
