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