patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / fs / sysfs / file.c
1 /*
2  * file.c - operations for regular (text) files.
3  */
4
5 #include <linux/module.h>
6 #include <linux/dnotify.h>
7 #include <linux/kobject.h>
8 #include <asm/uaccess.h>
9
10 #include "sysfs.h"
11
12 static struct file_operations sysfs_file_operations;
13
14 static int init_file(struct inode * inode)
15 {
16         inode->i_size = PAGE_SIZE;
17         inode->i_fop = &sysfs_file_operations;
18         return 0;
19 }
20
21 #define to_subsys(k) container_of(k,struct subsystem,kset.kobj)
22 #define to_sattr(a) container_of(a,struct subsys_attribute,attr)
23
24 /**
25  * Subsystem file operations.
26  * These operations allow subsystems to have files that can be 
27  * read/written. 
28  */
29 static ssize_t 
30 subsys_attr_show(struct kobject * kobj, struct attribute * attr, char * page)
31 {
32         struct subsystem * s = to_subsys(kobj);
33         struct subsys_attribute * sattr = to_sattr(attr);
34         ssize_t ret = 0;
35
36         if (sattr->show)
37                 ret = sattr->show(s,page);
38         return ret;
39 }
40
41 static ssize_t 
42 subsys_attr_store(struct kobject * kobj, struct attribute * attr, 
43                   const char * page, size_t count)
44 {
45         struct subsystem * s = to_subsys(kobj);
46         struct subsys_attribute * sattr = to_sattr(attr);
47         ssize_t ret = 0;
48
49         if (sattr->store)
50                 ret = sattr->store(s,page,count);
51         return ret;
52 }
53
54 static struct sysfs_ops subsys_sysfs_ops = {
55         .show   = subsys_attr_show,
56         .store  = subsys_attr_store,
57 };
58
59
60 struct sysfs_buffer {
61         size_t                  count;
62         loff_t                  pos;
63         char                    * page;
64         struct sysfs_ops        * ops;
65 };
66
67
68 /**
69  *      fill_read_buffer - allocate and fill buffer from object.
70  *      @file:          file pointer.
71  *      @buffer:        data buffer for file.
72  *
73  *      Allocate @buffer->page, if it hasn't been already, then call the
74  *      kobject's show() method to fill the buffer with this attribute's 
75  *      data. 
76  *      This is called only once, on the file's first read. 
77  */
78 static int fill_read_buffer(struct file * file, struct sysfs_buffer * buffer)
79 {
80         struct attribute * attr = file->f_dentry->d_fsdata;
81         struct kobject * kobj = file->f_dentry->d_parent->d_fsdata;
82         struct sysfs_ops * ops = buffer->ops;
83         int ret = 0;
84         ssize_t count;
85
86         if (!buffer->page)
87                 buffer->page = (char *) get_zeroed_page(GFP_KERNEL);
88         if (!buffer->page)
89                 return -ENOMEM;
90
91         count = ops->show(kobj,attr,buffer->page);
92         BUG_ON(count > PAGE_SIZE);
93         if (count >= 0)
94                 buffer->count = count;
95         else
96                 ret = count;
97         return ret;
98 }
99
100
101 /**
102  *      flush_read_buffer - push buffer to userspace.
103  *      @buffer:        data buffer for file.
104  *      @userbuf:       user-passed buffer.
105  *      @count:         number of bytes requested.
106  *      @ppos:          file position.
107  *
108  *      Copy the buffer we filled in fill_read_buffer() to userspace.
109  *      This is done at the reader's leisure, copying and advancing 
110  *      the amount they specify each time.
111  *      This may be called continuously until the buffer is empty.
112  */
113 static int flush_read_buffer(struct sysfs_buffer * buffer, char __user * buf,
114                              size_t count, loff_t * ppos)
115 {
116         int error;
117
118         if (count > (buffer->count - *ppos))
119                 count = buffer->count - *ppos;
120
121         error = copy_to_user(buf,buffer->page + *ppos,count);
122         if (!error)
123                 *ppos += count;
124         return error ? -EFAULT : count;
125 }
126
127 /**
128  *      sysfs_read_file - read an attribute. 
129  *      @file:  file pointer.
130  *      @buf:   buffer to fill.
131  *      @count: number of bytes to read.
132  *      @ppos:  starting offset in file.
133  *
134  *      Userspace wants to read an attribute file. The attribute descriptor
135  *      is in the file's ->d_fsdata. The target object is in the directory's
136  *      ->d_fsdata.
137  *
138  *      We call fill_read_buffer() to allocate and fill the buffer from the
139  *      object's show() method exactly once (if the read is happening from
140  *      the beginning of the file). That should fill the entire buffer with
141  *      all the data the object has to offer for that attribute.
142  *      We then call flush_read_buffer() to copy the buffer to userspace
143  *      in the increments specified.
144  */
145
146 static ssize_t
147 sysfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
148 {
149         struct sysfs_buffer * buffer = file->private_data;
150         ssize_t retval = 0;
151
152         if (!*ppos) {
153                 if ((retval = fill_read_buffer(file,buffer)))
154                         return retval;
155         }
156         pr_debug("%s: count = %d, ppos = %lld, buf = %s\n",
157                  __FUNCTION__,count,*ppos,buffer->page);
158         return flush_read_buffer(buffer,buf,count,ppos);
159 }
160
161
162 /**
163  *      fill_write_buffer - copy buffer from userspace.
164  *      @buffer:        data buffer for file.
165  *      @userbuf:       data from user.
166  *      @count:         number of bytes in @userbuf.
167  *
168  *      Allocate @buffer->page if it hasn't been already, then
169  *      copy the user-supplied buffer into it.
170  */
171
172 static int 
173 fill_write_buffer(struct sysfs_buffer * buffer, const char __user * buf, size_t count)
174 {
175         int error;
176
177         if (!buffer->page)
178                 buffer->page = (char *)get_zeroed_page(GFP_KERNEL);
179         if (!buffer->page)
180                 return -ENOMEM;
181
182         if (count >= PAGE_SIZE)
183                 count = PAGE_SIZE - 1;
184         error = copy_from_user(buffer->page,buf,count);
185         return error ? -EFAULT : count;
186 }
187
188
189 /**
190  *      flush_write_buffer - push buffer to kobject.
191  *      @file:          file pointer.
192  *      @buffer:        data buffer for file.
193  *
194  *      Get the correct pointers for the kobject and the attribute we're
195  *      dealing with, then call the store() method for the attribute, 
196  *      passing the buffer that we acquired in fill_write_buffer().
197  */
198
199 static int 
200 flush_write_buffer(struct file * file, struct sysfs_buffer * buffer, size_t count)
201 {
202         struct attribute * attr = file->f_dentry->d_fsdata;
203         struct kobject * kobj = file->f_dentry->d_parent->d_fsdata;
204         struct sysfs_ops * ops = buffer->ops;
205
206         return ops->store(kobj,attr,buffer->page,count);
207 }
208
209
210 /**
211  *      sysfs_write_file - write an attribute.
212  *      @file:  file pointer
213  *      @buf:   data to write
214  *      @count: number of bytes
215  *      @ppos:  starting offset
216  *
217  *      Similar to sysfs_read_file(), though working in the opposite direction.
218  *      We allocate and fill the data from the user in fill_write_buffer(),
219  *      then push it to the kobject in flush_write_buffer().
220  *      There is no easy way for us to know if userspace is only doing a partial
221  *      write, so we don't support them. We expect the entire buffer to come
222  *      on the first write. 
223  *      Hint: if you're writing a value, first read the file, modify only the
224  *      the value you're changing, then write entire buffer back. 
225  */
226
227 static ssize_t
228 sysfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
229 {
230         struct sysfs_buffer * buffer = file->private_data;
231
232         count = fill_write_buffer(buffer,buf,count);
233         if (count > 0)
234                 count = flush_write_buffer(file,buffer,count);
235         if (count > 0)
236                 *ppos += count;
237         return count;
238 }
239
240 static int check_perm(struct inode * inode, struct file * file)
241 {
242         struct kobject *kobj = sysfs_get_kobject(file->f_dentry->d_parent);
243         struct attribute * attr = file->f_dentry->d_fsdata;
244         struct sysfs_buffer * buffer;
245         struct sysfs_ops * ops = NULL;
246         int error = 0;
247
248         if (!kobj || !attr)
249                 goto Einval;
250
251         /* Grab the module reference for this attribute if we have one */
252         if (!try_module_get(attr->owner)) {
253                 error = -ENODEV;
254                 goto Done;
255         }
256
257         /* if the kobject has no ktype, then we assume that it is a subsystem
258          * itself, and use ops for it.
259          */
260         if (kobj->kset && kobj->kset->ktype)
261                 ops = kobj->kset->ktype->sysfs_ops;
262         else if (kobj->ktype)
263                 ops = kobj->ktype->sysfs_ops;
264         else
265                 ops = &subsys_sysfs_ops;
266
267         /* No sysfs operations, either from having no subsystem,
268          * or the subsystem have no operations.
269          */
270         if (!ops)
271                 goto Eaccess;
272
273         /* File needs write support.
274          * The inode's perms must say it's ok, 
275          * and we must have a store method.
276          */
277         if (file->f_mode & FMODE_WRITE) {
278
279                 if (!(inode->i_mode & S_IWUGO) || !ops->store)
280                         goto Eaccess;
281
282         }
283
284         /* File needs read support.
285          * The inode's perms must say it's ok, and we there
286          * must be a show method for it.
287          */
288         if (file->f_mode & FMODE_READ) {
289                 if (!(inode->i_mode & S_IRUGO) || !ops->show)
290                         goto Eaccess;
291         }
292
293         /* No error? Great, allocate a buffer for the file, and store it
294          * it in file->private_data for easy access.
295          */
296         buffer = kmalloc(sizeof(struct sysfs_buffer),GFP_KERNEL);
297         if (buffer) {
298                 memset(buffer,0,sizeof(struct sysfs_buffer));
299                 buffer->ops = ops;
300                 file->private_data = buffer;
301         } else
302                 error = -ENOMEM;
303         goto Done;
304
305  Einval:
306         error = -EINVAL;
307         goto Done;
308  Eaccess:
309         error = -EACCES;
310         module_put(attr->owner);
311  Done:
312         if (error && kobj)
313                 kobject_put(kobj);
314         return error;
315 }
316
317 static int sysfs_open_file(struct inode * inode, struct file * filp)
318 {
319         return check_perm(inode,filp);
320 }
321
322 static int sysfs_release(struct inode * inode, struct file * filp)
323 {
324         struct kobject * kobj = filp->f_dentry->d_parent->d_fsdata;
325         struct attribute * attr = filp->f_dentry->d_fsdata;
326         struct sysfs_buffer * buffer = filp->private_data;
327
328         if (kobj) 
329                 kobject_put(kobj);
330         module_put(attr->owner);
331
332         if (buffer) {
333                 if (buffer->page)
334                         free_page((unsigned long)buffer->page);
335                 kfree(buffer);
336         }
337         return 0;
338 }
339
340 static struct file_operations sysfs_file_operations = {
341         .read           = sysfs_read_file,
342         .write          = sysfs_write_file,
343         .llseek         = generic_file_llseek,
344         .open           = sysfs_open_file,
345         .release        = sysfs_release,
346 };
347
348
349 int sysfs_add_file(struct dentry * dir, const struct attribute * attr)
350 {
351         struct dentry * dentry;
352         int error;
353
354         down(&dir->d_inode->i_sem);
355         dentry = sysfs_get_dentry(dir,attr->name);
356         if (!IS_ERR(dentry)) {
357                 error = sysfs_create(dentry,
358                                      (attr->mode & S_IALLUGO) | S_IFREG,
359                                      init_file);
360                 if (!error)
361                         dentry->d_fsdata = (void *)attr;
362                 dput(dentry);
363         } else
364                 error = PTR_ERR(dentry);
365         up(&dir->d_inode->i_sem);
366         return error;
367 }
368
369
370 /**
371  *      sysfs_create_file - create an attribute file for an object.
372  *      @kobj:  object we're creating for. 
373  *      @attr:  atrribute descriptor.
374  */
375
376 int sysfs_create_file(struct kobject * kobj, const struct attribute * attr)
377 {
378         if (kobj && attr)
379                 return sysfs_add_file(kobj->dentry,attr);
380         return -EINVAL;
381 }
382
383
384 /**
385  * sysfs_update_file - update the modified timestamp on an object attribute.
386  * @kobj: object we're acting for.
387  * @attr: attribute descriptor.
388  *
389  * Also call dnotify for the dentry, which lots of userspace programs
390  * use.
391  */
392 int sysfs_update_file(struct kobject * kobj, const struct attribute * attr)
393 {
394         struct dentry * dir = kobj->dentry;
395         struct dentry * victim;
396         int res = -ENOENT;
397
398         down(&dir->d_inode->i_sem);
399         victim = sysfs_get_dentry(dir, attr->name);
400         if (!IS_ERR(victim)) {
401                 /* make sure dentry is really there */
402                 if (victim->d_inode && 
403                     (victim->d_parent->d_inode == dir->d_inode)) {
404                         victim->d_inode->i_mtime = CURRENT_TIME;
405                         dnotify_parent(victim, DN_MODIFY);
406
407                         /**
408                          * Drop reference from initial sysfs_get_dentry().
409                          */
410                         dput(victim);
411                         res = 0;
412                 }
413                 
414                 /**
415                  * Drop the reference acquired from sysfs_get_dentry() above.
416                  */
417                 dput(victim);
418         }
419         up(&dir->d_inode->i_sem);
420
421         return res;
422 }
423
424
425 /**
426  *      sysfs_remove_file - remove an object attribute.
427  *      @kobj:  object we're acting for.
428  *      @attr:  attribute descriptor.
429  *
430  *      Hash the attribute name and kill the victim.
431  */
432
433 void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr)
434 {
435         sysfs_hash_and_remove(kobj->dentry,attr->name);
436 }
437
438
439 EXPORT_SYMBOL(sysfs_create_file);
440 EXPORT_SYMBOL(sysfs_remove_file);
441 EXPORT_SYMBOL(sysfs_update_file);
442