sysfs: kobject_put cleanup
[profile/ivi/kernel-x86-ivi.git] / fs / sysfs / file.c
1 /*
2  * file.c - operations for regular (text) files.
3  */
4
5 #include <linux/module.h>
6 #include <linux/fsnotify.h>
7 #include <linux/kobject.h>
8 #include <linux/namei.h>
9 #include <linux/poll.h>
10 #include <linux/list.h>
11 #include <asm/uaccess.h>
12 #include <asm/semaphore.h>
13
14 #include "sysfs.h"
15
16 #define to_subsys(k) container_of(k,struct subsystem,kset.kobj)
17 #define to_sattr(a) container_of(a,struct subsys_attribute,attr)
18
19 /*
20  * Subsystem file operations.
21  * These operations allow subsystems to have files that can be 
22  * read/written. 
23  */
24 static ssize_t 
25 subsys_attr_show(struct kobject * kobj, struct attribute * attr, char * page)
26 {
27         struct subsystem * s = to_subsys(kobj);
28         struct subsys_attribute * sattr = to_sattr(attr);
29         ssize_t ret = -EIO;
30
31         if (sattr->show)
32                 ret = sattr->show(s,page);
33         return ret;
34 }
35
36 static ssize_t 
37 subsys_attr_store(struct kobject * kobj, struct attribute * attr, 
38                   const char * page, size_t count)
39 {
40         struct subsystem * s = to_subsys(kobj);
41         struct subsys_attribute * sattr = to_sattr(attr);
42         ssize_t ret = -EIO;
43
44         if (sattr->store)
45                 ret = sattr->store(s,page,count);
46         return ret;
47 }
48
49 static struct sysfs_ops subsys_sysfs_ops = {
50         .show   = subsys_attr_show,
51         .store  = subsys_attr_store,
52 };
53
54 /**
55  *      add_to_collection - add buffer to a collection
56  *      @buffer:        buffer to be added
57  *      @node           inode of set to add to
58  */
59
60 static inline void
61 add_to_collection(struct sysfs_buffer *buffer, struct inode *node)
62 {
63         struct sysfs_buffer_collection *set = node->i_private;
64
65         mutex_lock(&node->i_mutex);
66         list_add(&buffer->associates, &set->associates);
67         mutex_unlock(&node->i_mutex);
68 }
69
70 static inline void
71 remove_from_collection(struct sysfs_buffer *buffer, struct inode *node)
72 {
73         mutex_lock(&node->i_mutex);
74         list_del(&buffer->associates);
75         mutex_unlock(&node->i_mutex);
76 }
77
78 /**
79  *      fill_read_buffer - allocate and fill buffer from object.
80  *      @dentry:        dentry pointer.
81  *      @buffer:        data buffer for file.
82  *
83  *      Allocate @buffer->page, if it hasn't been already, then call the
84  *      kobject's show() method to fill the buffer with this attribute's 
85  *      data. 
86  *      This is called only once, on the file's first read. 
87  */
88 static int fill_read_buffer(struct dentry * dentry, struct sysfs_buffer * buffer)
89 {
90         struct sysfs_dirent * sd = dentry->d_fsdata;
91         struct attribute * attr = to_attr(dentry);
92         struct kobject * kobj = to_kobj(dentry->d_parent);
93         struct sysfs_ops * ops = buffer->ops;
94         int ret = 0;
95         ssize_t count;
96
97         if (!buffer->page)
98                 buffer->page = (char *) get_zeroed_page(GFP_KERNEL);
99         if (!buffer->page)
100                 return -ENOMEM;
101
102         buffer->event = atomic_read(&sd->s_event);
103         count = ops->show(kobj,attr,buffer->page);
104         buffer->needs_read_fill = 0;
105         BUG_ON(count > (ssize_t)PAGE_SIZE);
106         if (count >= 0)
107                 buffer->count = count;
108         else
109                 ret = count;
110         return ret;
111 }
112
113
114 /**
115  *      flush_read_buffer - push buffer to userspace.
116  *      @buffer:        data buffer for file.
117  *      @buf:           user-passed buffer.
118  *      @count:         number of bytes requested.
119  *      @ppos:          file position.
120  *
121  *      Copy the buffer we filled in fill_read_buffer() to userspace.
122  *      This is done at the reader's leisure, copying and advancing 
123  *      the amount they specify each time.
124  *      This may be called continuously until the buffer is empty.
125  */
126 static int flush_read_buffer(struct sysfs_buffer * buffer, char __user * buf,
127                              size_t count, loff_t * ppos)
128 {
129         int error;
130
131         if (*ppos > buffer->count)
132                 return 0;
133
134         if (count > (buffer->count - *ppos))
135                 count = buffer->count - *ppos;
136
137         error = copy_to_user(buf,buffer->page + *ppos,count);
138         if (!error)
139                 *ppos += count;
140         return error ? -EFAULT : count;
141 }
142
143 /**
144  *      sysfs_read_file - read an attribute. 
145  *      @file:  file pointer.
146  *      @buf:   buffer to fill.
147  *      @count: number of bytes to read.
148  *      @ppos:  starting offset in file.
149  *
150  *      Userspace wants to read an attribute file. The attribute descriptor
151  *      is in the file's ->d_fsdata. The target object is in the directory's
152  *      ->d_fsdata.
153  *
154  *      We call fill_read_buffer() to allocate and fill the buffer from the
155  *      object's show() method exactly once (if the read is happening from
156  *      the beginning of the file). That should fill the entire buffer with
157  *      all the data the object has to offer for that attribute.
158  *      We then call flush_read_buffer() to copy the buffer to userspace
159  *      in the increments specified.
160  */
161
162 static ssize_t
163 sysfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
164 {
165         struct sysfs_buffer * buffer = file->private_data;
166         ssize_t retval = 0;
167
168         down(&buffer->sem);
169         if (buffer->orphaned) {
170                 retval = -ENODEV;
171                 goto out;
172         }
173         if (buffer->needs_read_fill) {
174                 if ((retval = fill_read_buffer(file->f_path.dentry,buffer)))
175                         goto out;
176         }
177         pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n",
178                  __FUNCTION__, count, *ppos, buffer->page);
179         retval = flush_read_buffer(buffer,buf,count,ppos);
180 out:
181         up(&buffer->sem);
182         return retval;
183 }
184
185 /**
186  *      fill_write_buffer - copy buffer from userspace.
187  *      @buffer:        data buffer for file.
188  *      @buf:           data from user.
189  *      @count:         number of bytes in @userbuf.
190  *
191  *      Allocate @buffer->page if it hasn't been already, then
192  *      copy the user-supplied buffer into it.
193  */
194
195 static int 
196 fill_write_buffer(struct sysfs_buffer * buffer, const char __user * buf, size_t count)
197 {
198         int error;
199
200         if (!buffer->page)
201                 buffer->page = (char *)get_zeroed_page(GFP_KERNEL);
202         if (!buffer->page)
203                 return -ENOMEM;
204
205         if (count >= PAGE_SIZE)
206                 count = PAGE_SIZE - 1;
207         error = copy_from_user(buffer->page,buf,count);
208         buffer->needs_read_fill = 1;
209         /* if buf is assumed to contain a string, terminate it by \0,
210            so e.g. sscanf() can scan the string easily */
211         buffer->page[count] = 0;
212         return error ? -EFAULT : count;
213 }
214
215
216 /**
217  *      flush_write_buffer - push buffer to kobject.
218  *      @dentry:        dentry to the attribute
219  *      @buffer:        data buffer for file.
220  *      @count:         number of bytes
221  *
222  *      Get the correct pointers for the kobject and the attribute we're
223  *      dealing with, then call the store() method for the attribute, 
224  *      passing the buffer that we acquired in fill_write_buffer().
225  */
226
227 static int 
228 flush_write_buffer(struct dentry * dentry, struct sysfs_buffer * buffer, size_t count)
229 {
230         struct attribute * attr = to_attr(dentry);
231         struct kobject * kobj = to_kobj(dentry->d_parent);
232         struct sysfs_ops * ops = buffer->ops;
233
234         return ops->store(kobj,attr,buffer->page,count);
235 }
236
237
238 /**
239  *      sysfs_write_file - write an attribute.
240  *      @file:  file pointer
241  *      @buf:   data to write
242  *      @count: number of bytes
243  *      @ppos:  starting offset
244  *
245  *      Similar to sysfs_read_file(), though working in the opposite direction.
246  *      We allocate and fill the data from the user in fill_write_buffer(),
247  *      then push it to the kobject in flush_write_buffer().
248  *      There is no easy way for us to know if userspace is only doing a partial
249  *      write, so we don't support them. We expect the entire buffer to come
250  *      on the first write. 
251  *      Hint: if you're writing a value, first read the file, modify only the
252  *      the value you're changing, then write entire buffer back. 
253  */
254
255 static ssize_t
256 sysfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
257 {
258         struct sysfs_buffer * buffer = file->private_data;
259         ssize_t len;
260
261         down(&buffer->sem);
262         if (buffer->orphaned) {
263                 len = -ENODEV;
264                 goto out;
265         }
266         len = fill_write_buffer(buffer, buf, count);
267         if (len > 0)
268                 len = flush_write_buffer(file->f_path.dentry, buffer, len);
269         if (len > 0)
270                 *ppos += len;
271 out:
272         up(&buffer->sem);
273         return len;
274 }
275
276 static int sysfs_open_file(struct inode *inode, struct file *file)
277 {
278         struct kobject *kobj = sysfs_get_kobject(file->f_path.dentry->d_parent);
279         struct attribute * attr = to_attr(file->f_path.dentry);
280         struct sysfs_buffer_collection *set;
281         struct sysfs_buffer * buffer;
282         struct sysfs_ops * ops = NULL;
283         int error = 0;
284
285         if (!kobj || !attr)
286                 goto Einval;
287
288         /* Grab the module reference for this attribute if we have one */
289         if (!try_module_get(attr->owner)) {
290                 error = -ENODEV;
291                 goto Done;
292         }
293
294         /* if the kobject has no ktype, then we assume that it is a subsystem
295          * itself, and use ops for it.
296          */
297         if (kobj->kset && kobj->kset->ktype)
298                 ops = kobj->kset->ktype->sysfs_ops;
299         else if (kobj->ktype)
300                 ops = kobj->ktype->sysfs_ops;
301         else
302                 ops = &subsys_sysfs_ops;
303
304         /* No sysfs operations, either from having no subsystem,
305          * or the subsystem have no operations.
306          */
307         if (!ops)
308                 goto Eaccess;
309
310         /* make sure we have a collection to add our buffers to */
311         mutex_lock(&inode->i_mutex);
312         if (!(set = inode->i_private)) {
313                 if (!(set = inode->i_private = kmalloc(sizeof(struct sysfs_buffer_collection), GFP_KERNEL))) {
314                         error = -ENOMEM;
315                         goto Done;
316                 } else {
317                         INIT_LIST_HEAD(&set->associates);
318                 }
319         }
320         mutex_unlock(&inode->i_mutex);
321
322         /* File needs write support.
323          * The inode's perms must say it's ok, 
324          * and we must have a store method.
325          */
326         if (file->f_mode & FMODE_WRITE) {
327
328                 if (!(inode->i_mode & S_IWUGO) || !ops->store)
329                         goto Eaccess;
330
331         }
332
333         /* File needs read support.
334          * The inode's perms must say it's ok, and we there
335          * must be a show method for it.
336          */
337         if (file->f_mode & FMODE_READ) {
338                 if (!(inode->i_mode & S_IRUGO) || !ops->show)
339                         goto Eaccess;
340         }
341
342         /* No error? Great, allocate a buffer for the file, and store it
343          * it in file->private_data for easy access.
344          */
345         buffer = kzalloc(sizeof(struct sysfs_buffer), GFP_KERNEL);
346         if (buffer) {
347                 INIT_LIST_HEAD(&buffer->associates);
348                 init_MUTEX(&buffer->sem);
349                 buffer->needs_read_fill = 1;
350                 buffer->ops = ops;
351                 add_to_collection(buffer, inode);
352                 file->private_data = buffer;
353         } else
354                 error = -ENOMEM;
355         goto Done;
356
357  Einval:
358         error = -EINVAL;
359         goto Done;
360  Eaccess:
361         error = -EACCES;
362         module_put(attr->owner);
363  Done:
364         if (error)
365                 kobject_put(kobj);
366         return error;
367 }
368
369 static int sysfs_release(struct inode * inode, struct file * filp)
370 {
371         struct kobject * kobj = to_kobj(filp->f_path.dentry->d_parent);
372         struct attribute * attr = to_attr(filp->f_path.dentry);
373         struct module * owner = attr->owner;
374         struct sysfs_buffer * buffer = filp->private_data;
375
376         if (buffer)
377                 remove_from_collection(buffer, inode);
378         kobject_put(kobj);
379         /* After this point, attr should not be accessed. */
380         module_put(owner);
381
382         if (buffer) {
383                 if (buffer->page)
384                         free_page((unsigned long)buffer->page);
385                 kfree(buffer);
386         }
387         return 0;
388 }
389
390 /* Sysfs attribute files are pollable.  The idea is that you read
391  * the content and then you use 'poll' or 'select' to wait for
392  * the content to change.  When the content changes (assuming the
393  * manager for the kobject supports notification), poll will
394  * return POLLERR|POLLPRI, and select will return the fd whether
395  * it is waiting for read, write, or exceptions.
396  * Once poll/select indicates that the value has changed, you
397  * need to close and re-open the file, as simply seeking and reading
398  * again will not get new data, or reset the state of 'poll'.
399  * Reminder: this only works for attributes which actively support
400  * it, and it is not possible to test an attribute from userspace
401  * to see if it supports poll (Nether 'poll' or 'select' return
402  * an appropriate error code).  When in doubt, set a suitable timeout value.
403  */
404 static unsigned int sysfs_poll(struct file *filp, poll_table *wait)
405 {
406         struct sysfs_buffer * buffer = filp->private_data;
407         struct kobject * kobj = to_kobj(filp->f_path.dentry->d_parent);
408         struct sysfs_dirent * sd = filp->f_path.dentry->d_fsdata;
409         int res = 0;
410
411         poll_wait(filp, &kobj->poll, wait);
412
413         if (buffer->event != atomic_read(&sd->s_event)) {
414                 res = POLLERR|POLLPRI;
415                 buffer->needs_read_fill = 1;
416         }
417
418         return res;
419 }
420
421
422 static struct dentry *step_down(struct dentry *dir, const char * name)
423 {
424         struct dentry * de;
425
426         if (dir == NULL || dir->d_inode == NULL)
427                 return NULL;
428
429         mutex_lock(&dir->d_inode->i_mutex);
430         de = lookup_one_len(name, dir, strlen(name));
431         mutex_unlock(&dir->d_inode->i_mutex);
432         dput(dir);
433         if (IS_ERR(de))
434                 return NULL;
435         if (de->d_inode == NULL) {
436                 dput(de);
437                 return NULL;
438         }
439         return de;
440 }
441
442 void sysfs_notify(struct kobject * k, char *dir, char *attr)
443 {
444         struct dentry *de = k->dentry;
445         if (de)
446                 dget(de);
447         if (de && dir)
448                 de = step_down(de, dir);
449         if (de && attr)
450                 de = step_down(de, attr);
451         if (de) {
452                 struct sysfs_dirent * sd = de->d_fsdata;
453                 if (sd)
454                         atomic_inc(&sd->s_event);
455                 wake_up_interruptible(&k->poll);
456                 dput(de);
457         }
458 }
459 EXPORT_SYMBOL_GPL(sysfs_notify);
460
461 const struct file_operations sysfs_file_operations = {
462         .read           = sysfs_read_file,
463         .write          = sysfs_write_file,
464         .llseek         = generic_file_llseek,
465         .open           = sysfs_open_file,
466         .release        = sysfs_release,
467         .poll           = sysfs_poll,
468 };
469
470
471 int sysfs_add_file(struct dentry * dir, const struct attribute * attr, int type)
472 {
473         struct sysfs_dirent * parent_sd = dir->d_fsdata;
474         umode_t mode = (attr->mode & S_IALLUGO) | S_IFREG;
475         int error = -EEXIST;
476
477         mutex_lock(&dir->d_inode->i_mutex);
478         if (!sysfs_dirent_exist(parent_sd, attr->name))
479                 error = sysfs_make_dirent(parent_sd, NULL, (void *)attr,
480                                           mode, type);
481         mutex_unlock(&dir->d_inode->i_mutex);
482
483         return error;
484 }
485
486
487 /**
488  *      sysfs_create_file - create an attribute file for an object.
489  *      @kobj:  object we're creating for. 
490  *      @attr:  atrribute descriptor.
491  */
492
493 int sysfs_create_file(struct kobject * kobj, const struct attribute * attr)
494 {
495         BUG_ON(!kobj || !kobj->dentry || !attr);
496
497         return sysfs_add_file(kobj->dentry, attr, SYSFS_KOBJ_ATTR);
498
499 }
500
501
502 /**
503  * sysfs_update_file - update the modified timestamp on an object attribute.
504  * @kobj: object we're acting for.
505  * @attr: attribute descriptor.
506  */
507 int sysfs_update_file(struct kobject * kobj, const struct attribute * attr)
508 {
509         struct dentry * dir = kobj->dentry;
510         struct dentry * victim;
511         int res = -ENOENT;
512
513         mutex_lock(&dir->d_inode->i_mutex);
514         victim = lookup_one_len(attr->name, dir, strlen(attr->name));
515         if (!IS_ERR(victim)) {
516                 /* make sure dentry is really there */
517                 if (victim->d_inode && 
518                     (victim->d_parent->d_inode == dir->d_inode)) {
519                         victim->d_inode->i_mtime = CURRENT_TIME;
520                         fsnotify_modify(victim);
521                         res = 0;
522                 } else
523                         d_drop(victim);
524                 
525                 /**
526                  * Drop the reference acquired from lookup_one_len() above.
527                  */
528                 dput(victim);
529         }
530         mutex_unlock(&dir->d_inode->i_mutex);
531
532         return res;
533 }
534
535
536 /**
537  * sysfs_chmod_file - update the modified mode value on an object attribute.
538  * @kobj: object we're acting for.
539  * @attr: attribute descriptor.
540  * @mode: file permissions.
541  *
542  */
543 int sysfs_chmod_file(struct kobject *kobj, struct attribute *attr, mode_t mode)
544 {
545         struct dentry *dir = kobj->dentry;
546         struct dentry *victim;
547         struct inode * inode;
548         struct iattr newattrs;
549         int res = -ENOENT;
550
551         mutex_lock(&dir->d_inode->i_mutex);
552         victim = lookup_one_len(attr->name, dir, strlen(attr->name));
553         if (!IS_ERR(victim)) {
554                 if (victim->d_inode &&
555                     (victim->d_parent->d_inode == dir->d_inode)) {
556                         inode = victim->d_inode;
557                         mutex_lock(&inode->i_mutex);
558                         newattrs.ia_mode = (mode & S_IALLUGO) |
559                                                 (inode->i_mode & ~S_IALLUGO);
560                         newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
561                         res = notify_change(victim, &newattrs);
562                         mutex_unlock(&inode->i_mutex);
563                 }
564                 dput(victim);
565         }
566         mutex_unlock(&dir->d_inode->i_mutex);
567
568         return res;
569 }
570 EXPORT_SYMBOL_GPL(sysfs_chmod_file);
571
572
573 /**
574  *      sysfs_remove_file - remove an object attribute.
575  *      @kobj:  object we're acting for.
576  *      @attr:  attribute descriptor.
577  *
578  *      Hash the attribute name and kill the victim.
579  */
580
581 void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr)
582 {
583         sysfs_hash_and_remove(kobj->dentry, attr->name);
584 }
585
586
587 EXPORT_SYMBOL_GPL(sysfs_create_file);
588 EXPORT_SYMBOL_GPL(sysfs_remove_file);
589 EXPORT_SYMBOL_GPL(sysfs_update_file);