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