blob: efb4062fe099db0049cea26070a622b59effdcd2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Dunlap16f7e0f2006-01-11 12:17:46 -080014#include <linux/capability.h>
Randy.Dunlap995982c2006-07-10 23:05:25 -070015#include <linux/errno.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040016#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include "sysfs.h"
18
19extern struct super_block * sysfs_sb;
20
Christoph Hellwigf5e54d62006-06-28 04:26:44 -070021static const struct address_space_operations sysfs_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 .readpage = simple_readpage,
23 .prepare_write = simple_prepare_write,
24 .commit_write = simple_commit_write
25};
26
27static struct backing_dev_info sysfs_backing_dev_info = {
28 .ra_pages = 0, /* No readahead */
29 .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
30};
31
Arjan van de Venc5ef1c42007-02-12 00:55:40 -080032static const struct inode_operations sysfs_inode_operations ={
Maneesh Soni988d1862005-05-31 10:39:14 +053033 .setattr = sysfs_setattr,
34};
35
36int sysfs_setattr(struct dentry * dentry, struct iattr * iattr)
37{
38 struct inode * inode = dentry->d_inode;
39 struct sysfs_dirent * sd = dentry->d_fsdata;
40 struct iattr * sd_iattr;
41 unsigned int ia_valid = iattr->ia_valid;
42 int error;
43
44 if (!sd)
45 return -EINVAL;
46
47 sd_iattr = sd->s_iattr;
48
49 error = inode_change_ok(inode, iattr);
50 if (error)
51 return error;
52
53 error = inode_setattr(inode, iattr);
54 if (error)
55 return error;
56
57 if (!sd_iattr) {
58 /* setting attributes for the first time, allocate now */
Eric Sesterhenn58d49282006-02-22 11:18:15 +010059 sd_iattr = kzalloc(sizeof(struct iattr), GFP_KERNEL);
Maneesh Soni988d1862005-05-31 10:39:14 +053060 if (!sd_iattr)
61 return -ENOMEM;
62 /* assign default attributes */
Maneesh Soni988d1862005-05-31 10:39:14 +053063 sd_iattr->ia_mode = sd->s_mode;
64 sd_iattr->ia_uid = 0;
65 sd_iattr->ia_gid = 0;
66 sd_iattr->ia_atime = sd_iattr->ia_mtime = sd_iattr->ia_ctime = CURRENT_TIME;
67 sd->s_iattr = sd_iattr;
68 }
69
70 /* attributes were changed atleast once in past */
71
72 if (ia_valid & ATTR_UID)
73 sd_iattr->ia_uid = iattr->ia_uid;
74 if (ia_valid & ATTR_GID)
75 sd_iattr->ia_gid = iattr->ia_gid;
76 if (ia_valid & ATTR_ATIME)
77 sd_iattr->ia_atime = timespec_trunc(iattr->ia_atime,
78 inode->i_sb->s_time_gran);
79 if (ia_valid & ATTR_MTIME)
80 sd_iattr->ia_mtime = timespec_trunc(iattr->ia_mtime,
81 inode->i_sb->s_time_gran);
82 if (ia_valid & ATTR_CTIME)
83 sd_iattr->ia_ctime = timespec_trunc(iattr->ia_ctime,
84 inode->i_sb->s_time_gran);
85 if (ia_valid & ATTR_MODE) {
86 umode_t mode = iattr->ia_mode;
87
88 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
89 mode &= ~S_ISGID;
Maneesh Soni9ca1eb32005-07-29 12:14:19 -070090 sd_iattr->ia_mode = sd->s_mode = mode;
Maneesh Soni988d1862005-05-31 10:39:14 +053091 }
92
93 return error;
94}
95
Maneesh Soni82155342005-05-31 10:39:52 +053096static inline void set_default_inode_attr(struct inode * inode, mode_t mode)
97{
98 inode->i_mode = mode;
99 inode->i_uid = 0;
100 inode->i_gid = 0;
101 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
102}
103
104static inline void set_inode_attr(struct inode * inode, struct iattr * iattr)
105{
106 inode->i_mode = iattr->ia_mode;
107 inode->i_uid = iattr->ia_uid;
108 inode->i_gid = iattr->ia_gid;
109 inode->i_atime = iattr->ia_atime;
110 inode->i_mtime = iattr->ia_mtime;
111 inode->i_ctime = iattr->ia_ctime;
112}
113
Arjan van de Ven232ba9d2006-07-12 09:03:06 -0700114
115/*
116 * sysfs has a different i_mutex lock order behavior for i_mutex than other
117 * filesystems; sysfs i_mutex is called in many places with subsystem locks
118 * held. At the same time, many of the VFS locking rules do not apply to
119 * sysfs at all (cross directory rename for example). To untangle this mess
120 * (which gives false positives in lockdep), we're giving sysfs inodes their
121 * own class for i_mutex.
122 */
123static struct lock_class_key sysfs_inode_imutex_key;
124
Tejun Heobc37e282007-07-18 14:30:28 +0900125static void sysfs_init_inode(struct sysfs_dirent *sd, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Tejun Heofc9f54b2007-06-14 03:45:17 +0900127 inode->i_blocks = 0;
128 inode->i_mapping->a_ops = &sysfs_aops;
129 inode->i_mapping->backing_dev_info = &sysfs_backing_dev_info;
130 inode->i_op = &sysfs_inode_operations;
131 inode->i_ino = sd->s_ino;
132 lockdep_set_class(&inode->i_mutex, &sysfs_inode_imutex_key);
Maneesh Soni82155342005-05-31 10:39:52 +0530133
Tejun Heofc9f54b2007-06-14 03:45:17 +0900134 if (sd->s_iattr) {
135 /* sysfs_dirent has non-default attributes
136 * get them for the new inode from persistent copy
137 * in sysfs_dirent
138 */
139 set_inode_attr(inode, sd->s_iattr);
140 } else
141 set_default_inode_attr(inode, sd->s_mode);
142}
143
144/**
Tejun Heo8312a8d2007-06-14 03:45:17 +0900145 * sysfs_get_inode - get inode for sysfs_dirent
Tejun Heofc9f54b2007-06-14 03:45:17 +0900146 * @sd: sysfs_dirent to allocate inode for
147 *
Tejun Heo8312a8d2007-06-14 03:45:17 +0900148 * Get inode for @sd. If such inode doesn't exist, a new inode
149 * is allocated and basics are initialized. New inode is
150 * returned locked.
Tejun Heofc9f54b2007-06-14 03:45:17 +0900151 *
152 * LOCKING:
153 * Kernel thread context (may sleep).
154 *
155 * RETURNS:
156 * Pointer to allocated inode on success, NULL on failure.
157 */
Tejun Heo8312a8d2007-06-14 03:45:17 +0900158struct inode * sysfs_get_inode(struct sysfs_dirent *sd)
Tejun Heofc9f54b2007-06-14 03:45:17 +0900159{
160 struct inode *inode;
161
Tejun Heo8312a8d2007-06-14 03:45:17 +0900162 inode = iget_locked(sysfs_sb, sd->s_ino);
163 if (inode && (inode->i_state & I_NEW))
Tejun Heofc9f54b2007-06-14 03:45:17 +0900164 sysfs_init_inode(sd, inode);
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 return inode;
167}
168
Tejun Heofc9f54b2007-06-14 03:45:17 +0900169/**
170 * sysfs_instantiate - instantiate dentry
171 * @dentry: dentry to be instantiated
172 * @inode: inode associated with @sd
173 *
Tejun Heo8312a8d2007-06-14 03:45:17 +0900174 * Unlock @inode if locked and instantiate @dentry with @inode.
Tejun Heofc9f54b2007-06-14 03:45:17 +0900175 *
176 * LOCKING:
177 * None.
178 */
179void sysfs_instantiate(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Tejun Heofc9f54b2007-06-14 03:45:17 +0900181 BUG_ON(!dentry || dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Tejun Heofb6896d2007-06-14 04:27:24 +0900183 if (inode->i_state & I_NEW)
Tejun Heo8312a8d2007-06-14 03:45:17 +0900184 unlock_new_inode(inode);
185
Tejun Heofc9f54b2007-06-14 03:45:17 +0900186 d_instantiate(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
Tejun Heo608e2662007-06-14 04:27:22 +0900189int sysfs_hash_and_remove(struct sysfs_dirent *dir_sd, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Tejun Heofb6896d2007-06-14 04:27:24 +0900191 struct sysfs_addrm_cxt acxt;
Tejun Heo41fc1c22007-08-02 21:38:03 +0900192 struct sysfs_dirent *sd;
Greg Kroah-Hartman641e6f32006-03-16 15:44:26 -0800193
Tejun Heo608e2662007-06-14 04:27:22 +0900194 if (!dir_sd)
Randy.Dunlap995982c2006-07-10 23:05:25 -0700195 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Tejun Heofb6896d2007-06-14 04:27:24 +0900197 sysfs_addrm_start(&acxt, dir_sd);
Tejun Heo608e2662007-06-14 04:27:22 +0900198
Tejun Heo41fc1c22007-08-02 21:38:03 +0900199 sd = sysfs_find_dirent(dir_sd, name);
200 if (sd)
201 sysfs_remove_one(&acxt, sd);
Tejun Heo3007e992007-06-14 04:27:23 +0900202
Tejun Heofb6896d2007-06-14 04:27:24 +0900203 if (sysfs_addrm_finish(&acxt))
204 return 0;
205 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}