[PATCH] sysfs-iattr: add sysfs_setattr
[linux-2.6.git] / fs / sysfs / inode.c
1 /*
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
16 extern struct super_block * sysfs_sb;
17
18 static struct address_space_operations sysfs_aops = {
19         .readpage       = simple_readpage,
20         .prepare_write  = simple_prepare_write,
21         .commit_write   = simple_commit_write
22 };
23
24 static 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
29 static struct inode_operations sysfs_inode_operations ={
30         .setattr        = sysfs_setattr,
31 };
32
33 int 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
94 struct inode * sysfs_new_inode(mode_t mode)
95 {
96         struct inode * inode = new_inode(sysfs_sb);
97         if (inode) {
98                 inode->i_mode = mode;
99                 inode->i_uid = 0;
100                 inode->i_gid = 0;
101                 inode->i_blksize = PAGE_CACHE_SIZE;
102                 inode->i_blocks = 0;
103                 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
104                 inode->i_mapping->a_ops = &sysfs_aops;
105                 inode->i_mapping->backing_dev_info = &sysfs_backing_dev_info;
106         }
107         return inode;
108 }
109
110 int sysfs_create(struct dentry * dentry, int mode, int (*init)(struct inode *))
111 {
112         int error = 0;
113         struct inode * inode = NULL;
114         if (dentry) {
115                 if (!dentry->d_inode) {
116                         if ((inode = sysfs_new_inode(mode))) {
117                                 if (dentry->d_parent && dentry->d_parent->d_inode) {
118                                         struct inode *p_inode = dentry->d_parent->d_inode;
119                                         p_inode->i_mtime = p_inode->i_ctime = CURRENT_TIME;
120                                 }
121                                 goto Proceed;
122                         }
123                         else 
124                                 error = -ENOMEM;
125                 } else
126                         error = -EEXIST;
127         } else 
128                 error = -ENOENT;
129         goto Done;
130
131  Proceed:
132         if (init)
133                 error = init(inode);
134         if (!error) {
135                 d_instantiate(dentry, inode);
136                 if (S_ISDIR(mode))
137                         dget(dentry);  /* pin only directory dentry in core */
138         } else
139                 iput(inode);
140  Done:
141         return error;
142 }
143
144 struct dentry * sysfs_get_dentry(struct dentry * parent, const char * name)
145 {
146         struct qstr qstr;
147
148         qstr.name = name;
149         qstr.len = strlen(name);
150         qstr.hash = full_name_hash(name,qstr.len);
151         return lookup_hash(&qstr,parent);
152 }
153
154 /*
155  * Get the name for corresponding element represented by the given sysfs_dirent
156  */
157 const unsigned char * sysfs_get_name(struct sysfs_dirent *sd)
158 {
159         struct attribute * attr;
160         struct bin_attribute * bin_attr;
161         struct sysfs_symlink  * sl;
162
163         if (!sd || !sd->s_element)
164                 BUG();
165
166         switch (sd->s_type) {
167                 case SYSFS_DIR:
168                         /* Always have a dentry so use that */
169                         return sd->s_dentry->d_name.name;
170
171                 case SYSFS_KOBJ_ATTR:
172                         attr = sd->s_element;
173                         return attr->name;
174
175                 case SYSFS_KOBJ_BIN_ATTR:
176                         bin_attr = sd->s_element;
177                         return bin_attr->attr.name;
178
179                 case SYSFS_KOBJ_LINK:
180                         sl = sd->s_element;
181                         return sl->link_name;
182         }
183         return NULL;
184 }
185
186
187 /*
188  * Unhashes the dentry corresponding to given sysfs_dirent
189  * Called with parent inode's i_sem held.
190  */
191 void sysfs_drop_dentry(struct sysfs_dirent * sd, struct dentry * parent)
192 {
193         struct dentry * dentry = sd->s_dentry;
194
195         if (dentry) {
196                 spin_lock(&dcache_lock);
197                 spin_lock(&dentry->d_lock);
198                 if (!(d_unhashed(dentry) && dentry->d_inode)) {
199                         dget_locked(dentry);
200                         __d_drop(dentry);
201                         spin_unlock(&dentry->d_lock);
202                         spin_unlock(&dcache_lock);
203                         simple_unlink(parent->d_inode, dentry);
204                 } else {
205                         spin_unlock(&dentry->d_lock);
206                         spin_unlock(&dcache_lock);
207                 }
208         }
209 }
210
211 void sysfs_hash_and_remove(struct dentry * dir, const char * name)
212 {
213         struct sysfs_dirent * sd;
214         struct sysfs_dirent * parent_sd = dir->d_fsdata;
215
216         down(&dir->d_inode->i_sem);
217         list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
218                 if (!sd->s_element)
219                         continue;
220                 if (!strcmp(sysfs_get_name(sd), name)) {
221                         list_del_init(&sd->s_sibling);
222                         sysfs_drop_dentry(sd, dir);
223                         sysfs_put(sd);
224                         break;
225                 }
226         }
227         up(&dir->d_inode->i_sem);
228 }
229
230