blob: 565cac1d42006d50713544bc400a2bded2a2680a [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>
14#include "sysfs.h"
15
16extern struct super_block * sysfs_sb;
17
18static struct address_space_operations sysfs_aops = {
19 .readpage = simple_readpage,
20 .prepare_write = simple_prepare_write,
21 .commit_write = simple_commit_write
22};
23
24static struct backing_dev_info sysfs_backing_dev_info = {
25 .ra_pages = 0, /* No readahead */
26 .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
27};
28
Maneesh Soni988d1862005-05-31 10:39:14 +053029static struct inode_operations sysfs_inode_operations ={
30 .setattr = sysfs_setattr,
31};
32
33int sysfs_setattr(struct dentry * dentry, struct iattr * iattr)
34{
35 struct inode * inode = dentry->d_inode;
36 struct sysfs_dirent * sd = dentry->d_fsdata;
37 struct iattr * sd_iattr;
38 unsigned int ia_valid = iattr->ia_valid;
39 int error;
40
41 if (!sd)
42 return -EINVAL;
43
44 sd_iattr = sd->s_iattr;
45
46 error = inode_change_ok(inode, iattr);
47 if (error)
48 return error;
49
50 error = inode_setattr(inode, iattr);
51 if (error)
52 return error;
53
54 if (!sd_iattr) {
55 /* setting attributes for the first time, allocate now */
56 sd_iattr = kmalloc(sizeof(struct iattr), GFP_KERNEL);
57 if (!sd_iattr)
58 return -ENOMEM;
59 /* assign default attributes */
60 memset(sd_iattr, 0, sizeof(struct iattr));
61 sd_iattr->ia_mode = sd->s_mode;
62 sd_iattr->ia_uid = 0;
63 sd_iattr->ia_gid = 0;
64 sd_iattr->ia_atime = sd_iattr->ia_mtime = sd_iattr->ia_ctime = CURRENT_TIME;
65 sd->s_iattr = sd_iattr;
66 }
67
68 /* attributes were changed atleast once in past */
69
70 if (ia_valid & ATTR_UID)
71 sd_iattr->ia_uid = iattr->ia_uid;
72 if (ia_valid & ATTR_GID)
73 sd_iattr->ia_gid = iattr->ia_gid;
74 if (ia_valid & ATTR_ATIME)
75 sd_iattr->ia_atime = timespec_trunc(iattr->ia_atime,
76 inode->i_sb->s_time_gran);
77 if (ia_valid & ATTR_MTIME)
78 sd_iattr->ia_mtime = timespec_trunc(iattr->ia_mtime,
79 inode->i_sb->s_time_gran);
80 if (ia_valid & ATTR_CTIME)
81 sd_iattr->ia_ctime = timespec_trunc(iattr->ia_ctime,
82 inode->i_sb->s_time_gran);
83 if (ia_valid & ATTR_MODE) {
84 umode_t mode = iattr->ia_mode;
85
86 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
87 mode &= ~S_ISGID;
88 sd_iattr->ia_mode = mode;
89 }
90
91 return error;
92}
93
Maneesh Soni82155342005-05-31 10:39:52 +053094static inline void set_default_inode_attr(struct inode * inode, mode_t mode)
95{
96 inode->i_mode = mode;
97 inode->i_uid = 0;
98 inode->i_gid = 0;
99 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
100}
101
102static inline void set_inode_attr(struct inode * inode, struct iattr * iattr)
103{
104 inode->i_mode = iattr->ia_mode;
105 inode->i_uid = iattr->ia_uid;
106 inode->i_gid = iattr->ia_gid;
107 inode->i_atime = iattr->ia_atime;
108 inode->i_mtime = iattr->ia_mtime;
109 inode->i_ctime = iattr->ia_ctime;
110}
111
112struct inode * sysfs_new_inode(mode_t mode, struct sysfs_dirent * sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
114 struct inode * inode = new_inode(sysfs_sb);
115 if (inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 inode->i_blksize = PAGE_CACHE_SIZE;
117 inode->i_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 inode->i_mapping->a_ops = &sysfs_aops;
119 inode->i_mapping->backing_dev_info = &sysfs_backing_dev_info;
Maneesh Soni82155342005-05-31 10:39:52 +0530120 inode->i_op = &sysfs_inode_operations;
121
122 if (sd->s_iattr) {
123 /* sysfs_dirent has non-default attributes
124 * get them for the new inode from persistent copy
125 * in sysfs_dirent
126 */
127 set_inode_attr(inode, sd->s_iattr);
128 } else
129 set_default_inode_attr(inode, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 }
131 return inode;
132}
133
134int sysfs_create(struct dentry * dentry, int mode, int (*init)(struct inode *))
135{
136 int error = 0;
137 struct inode * inode = NULL;
138 if (dentry) {
139 if (!dentry->d_inode) {
Maneesh Soni82155342005-05-31 10:39:52 +0530140 struct sysfs_dirent * sd = dentry->d_fsdata;
141 if ((inode = sysfs_new_inode(mode, sd))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 if (dentry->d_parent && dentry->d_parent->d_inode) {
143 struct inode *p_inode = dentry->d_parent->d_inode;
144 p_inode->i_mtime = p_inode->i_ctime = CURRENT_TIME;
145 }
146 goto Proceed;
147 }
148 else
149 error = -ENOMEM;
150 } else
151 error = -EEXIST;
152 } else
153 error = -ENOENT;
154 goto Done;
155
156 Proceed:
157 if (init)
158 error = init(inode);
159 if (!error) {
160 d_instantiate(dentry, inode);
161 if (S_ISDIR(mode))
162 dget(dentry); /* pin only directory dentry in core */
163 } else
164 iput(inode);
165 Done:
166 return error;
167}
168
169struct dentry * sysfs_get_dentry(struct dentry * parent, const char * name)
170{
171 struct qstr qstr;
172
173 qstr.name = name;
174 qstr.len = strlen(name);
175 qstr.hash = full_name_hash(name,qstr.len);
176 return lookup_hash(&qstr,parent);
177}
178
179/*
180 * Get the name for corresponding element represented by the given sysfs_dirent
181 */
182const unsigned char * sysfs_get_name(struct sysfs_dirent *sd)
183{
184 struct attribute * attr;
185 struct bin_attribute * bin_attr;
186 struct sysfs_symlink * sl;
187
188 if (!sd || !sd->s_element)
189 BUG();
190
191 switch (sd->s_type) {
192 case SYSFS_DIR:
193 /* Always have a dentry so use that */
194 return sd->s_dentry->d_name.name;
195
196 case SYSFS_KOBJ_ATTR:
197 attr = sd->s_element;
198 return attr->name;
199
200 case SYSFS_KOBJ_BIN_ATTR:
201 bin_attr = sd->s_element;
202 return bin_attr->attr.name;
203
204 case SYSFS_KOBJ_LINK:
205 sl = sd->s_element;
206 return sl->link_name;
207 }
208 return NULL;
209}
210
211
212/*
213 * Unhashes the dentry corresponding to given sysfs_dirent
214 * Called with parent inode's i_sem held.
215 */
216void sysfs_drop_dentry(struct sysfs_dirent * sd, struct dentry * parent)
217{
218 struct dentry * dentry = sd->s_dentry;
219
220 if (dentry) {
221 spin_lock(&dcache_lock);
222 spin_lock(&dentry->d_lock);
223 if (!(d_unhashed(dentry) && dentry->d_inode)) {
224 dget_locked(dentry);
225 __d_drop(dentry);
226 spin_unlock(&dentry->d_lock);
227 spin_unlock(&dcache_lock);
228 simple_unlink(parent->d_inode, dentry);
229 } else {
230 spin_unlock(&dentry->d_lock);
231 spin_unlock(&dcache_lock);
232 }
233 }
234}
235
236void sysfs_hash_and_remove(struct dentry * dir, const char * name)
237{
238 struct sysfs_dirent * sd;
239 struct sysfs_dirent * parent_sd = dir->d_fsdata;
240
241 down(&dir->d_inode->i_sem);
242 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
243 if (!sd->s_element)
244 continue;
245 if (!strcmp(sysfs_get_name(sd), name)) {
246 list_del_init(&sd->s_sibling);
247 sysfs_drop_dentry(sd, dir);
248 sysfs_put(sd);
249 break;
250 }
251 }
252 up(&dir->d_inode->i_sem);
253}
254
255