[NETFILTER]: nf_conntrack: automatic helper assignment for expectations
[linux-2.6.22.y-op.git] / fs / sysfs / inode.c
blobe79e38d52c0067185ba5fabf29a1f0c3e474172b
1 /*
2 * inode.c - basic inode and dentry operations.
4 * sysfs is Copyright (c) 2001-3 Patrick Mochel
6 * Please see Documentation/filesystems/sysfs.txt for more information.
7 */
9 #undef DEBUG
11 #include <linux/pagemap.h>
12 #include <linux/namei.h>
13 #include <linux/backing-dev.h>
14 #include <linux/capability.h>
15 #include <linux/errno.h>
16 #include "sysfs.h"
18 extern struct super_block * sysfs_sb;
20 static const struct address_space_operations sysfs_aops = {
21 .readpage = simple_readpage,
22 .prepare_write = simple_prepare_write,
23 .commit_write = simple_commit_write
26 static struct backing_dev_info sysfs_backing_dev_info = {
27 .ra_pages = 0, /* No readahead */
28 .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
31 static struct inode_operations sysfs_inode_operations ={
32 .setattr = sysfs_setattr,
35 int sysfs_setattr(struct dentry * dentry, struct iattr * iattr)
37 struct inode * inode = dentry->d_inode;
38 struct sysfs_dirent * sd = dentry->d_fsdata;
39 struct iattr * sd_iattr;
40 unsigned int ia_valid = iattr->ia_valid;
41 int error;
43 if (!sd)
44 return -EINVAL;
46 sd_iattr = sd->s_iattr;
48 error = inode_change_ok(inode, iattr);
49 if (error)
50 return error;
52 error = inode_setattr(inode, iattr);
53 if (error)
54 return error;
56 if (!sd_iattr) {
57 /* setting attributes for the first time, allocate now */
58 sd_iattr = kzalloc(sizeof(struct iattr), GFP_KERNEL);
59 if (!sd_iattr)
60 return -ENOMEM;
61 /* assign default attributes */
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;
69 /* attributes were changed atleast once in past */
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;
87 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
88 mode &= ~S_ISGID;
89 sd_iattr->ia_mode = sd->s_mode = mode;
92 return error;
95 static inline void set_default_inode_attr(struct inode * inode, mode_t mode)
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;
103 static inline void set_inode_attr(struct inode * inode, struct iattr * iattr)
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;
115 * sysfs has a different i_mutex lock order behavior for i_mutex than other
116 * filesystems; sysfs i_mutex is called in many places with subsystem locks
117 * held. At the same time, many of the VFS locking rules do not apply to
118 * sysfs at all (cross directory rename for example). To untangle this mess
119 * (which gives false positives in lockdep), we're giving sysfs inodes their
120 * own class for i_mutex.
122 static struct lock_class_key sysfs_inode_imutex_key;
124 struct inode * sysfs_new_inode(mode_t mode, struct sysfs_dirent * sd)
126 struct inode * inode = new_inode(sysfs_sb);
127 if (inode) {
128 inode->i_blocks = 0;
129 inode->i_mapping->a_ops = &sysfs_aops;
130 inode->i_mapping->backing_dev_info = &sysfs_backing_dev_info;
131 inode->i_op = &sysfs_inode_operations;
132 lockdep_set_class(&inode->i_mutex, &sysfs_inode_imutex_key);
134 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
139 set_inode_attr(inode, sd->s_iattr);
140 } else
141 set_default_inode_attr(inode, mode);
143 return inode;
146 int sysfs_create(struct dentry * dentry, int mode, int (*init)(struct inode *))
148 int error = 0;
149 struct inode * inode = NULL;
150 if (dentry) {
151 if (!dentry->d_inode) {
152 struct sysfs_dirent * sd = dentry->d_fsdata;
153 if ((inode = sysfs_new_inode(mode, sd))) {
154 if (dentry->d_parent && dentry->d_parent->d_inode) {
155 struct inode *p_inode = dentry->d_parent->d_inode;
156 p_inode->i_mtime = p_inode->i_ctime = CURRENT_TIME;
158 goto Proceed;
160 else
161 error = -ENOMEM;
162 } else
163 error = -EEXIST;
164 } else
165 error = -ENOENT;
166 goto Done;
168 Proceed:
169 if (init)
170 error = init(inode);
171 if (!error) {
172 d_instantiate(dentry, inode);
173 if (S_ISDIR(mode))
174 dget(dentry); /* pin only directory dentry in core */
175 } else
176 iput(inode);
177 Done:
178 return error;
182 * Get the name for corresponding element represented by the given sysfs_dirent
184 const unsigned char * sysfs_get_name(struct sysfs_dirent *sd)
186 struct attribute * attr;
187 struct bin_attribute * bin_attr;
188 struct sysfs_symlink * sl;
190 BUG_ON(!sd || !sd->s_element);
192 switch (sd->s_type) {
193 case SYSFS_DIR:
194 /* Always have a dentry so use that */
195 return sd->s_dentry->d_name.name;
197 case SYSFS_KOBJ_ATTR:
198 attr = sd->s_element;
199 return attr->name;
201 case SYSFS_KOBJ_BIN_ATTR:
202 bin_attr = sd->s_element;
203 return bin_attr->attr.name;
205 case SYSFS_KOBJ_LINK:
206 sl = sd->s_element;
207 return sl->link_name;
209 return NULL;
214 * Unhashes the dentry corresponding to given sysfs_dirent
215 * Called with parent inode's i_mutex held.
217 void sysfs_drop_dentry(struct sysfs_dirent * sd, struct dentry * parent)
219 struct dentry * dentry = sd->s_dentry;
221 if (dentry) {
222 spin_lock(&dcache_lock);
223 spin_lock(&dentry->d_lock);
224 if (!(d_unhashed(dentry) && dentry->d_inode)) {
225 dget_locked(dentry);
226 __d_drop(dentry);
227 spin_unlock(&dentry->d_lock);
228 spin_unlock(&dcache_lock);
229 simple_unlink(parent->d_inode, dentry);
230 } else {
231 spin_unlock(&dentry->d_lock);
232 spin_unlock(&dcache_lock);
237 int sysfs_hash_and_remove(struct dentry * dir, const char * name)
239 struct sysfs_dirent * sd;
240 struct sysfs_dirent * parent_sd;
241 int found = 0;
243 if (!dir)
244 return -ENOENT;
246 if (dir->d_inode == NULL)
247 /* no inode means this hasn't been made visible yet */
248 return -ENOENT;
250 parent_sd = dir->d_fsdata;
251 mutex_lock(&dir->d_inode->i_mutex);
252 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
253 if (!sd->s_element)
254 continue;
255 if (!strcmp(sysfs_get_name(sd), name)) {
256 list_del_init(&sd->s_sibling);
257 sysfs_drop_dentry(sd, dir);
258 sysfs_put(sd);
259 found = 1;
260 break;
263 mutex_unlock(&dir->d_inode->i_mutex);
265 return found ? 0 : -ENOENT;