m68k: kill page walker compile warning
[wrt350n-kernel.git] / fs / sysfs / file.c
bloba271c87c4472d530bee00c35da17a06606dfc97c
1 /*
2 * fs/sysfs/file.c - sysfs regular (text) file implementation
4 * Copyright (c) 2001-3 Patrick Mochel
5 * Copyright (c) 2007 SUSE Linux Products GmbH
6 * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
8 * This file is released under the GPLv2.
10 * Please see Documentation/filesystems/sysfs.txt for more information.
13 #include <linux/module.h>
14 #include <linux/kobject.h>
15 #include <linux/namei.h>
16 #include <linux/poll.h>
17 #include <linux/list.h>
18 #include <linux/mutex.h>
19 #include <asm/uaccess.h>
21 #include "sysfs.h"
24 * There's one sysfs_buffer for each open file and one
25 * sysfs_open_dirent for each sysfs_dirent with one or more open
26 * files.
28 * filp->private_data points to sysfs_buffer and
29 * sysfs_dirent->s_attr.open points to sysfs_open_dirent. s_attr.open
30 * is protected by sysfs_open_dirent_lock.
32 static DEFINE_SPINLOCK(sysfs_open_dirent_lock);
34 struct sysfs_open_dirent {
35 atomic_t refcnt;
36 atomic_t event;
37 wait_queue_head_t poll;
38 struct list_head buffers; /* goes through sysfs_buffer.list */
41 struct sysfs_buffer {
42 size_t count;
43 loff_t pos;
44 char * page;
45 struct sysfs_ops * ops;
46 struct mutex mutex;
47 int needs_read_fill;
48 int event;
49 struct list_head list;
52 /**
53 * fill_read_buffer - allocate and fill buffer from object.
54 * @dentry: dentry pointer.
55 * @buffer: data buffer for file.
57 * Allocate @buffer->page, if it hasn't been already, then call the
58 * kobject's show() method to fill the buffer with this attribute's
59 * data.
60 * This is called only once, on the file's first read unless an error
61 * is returned.
63 static int fill_read_buffer(struct dentry * dentry, struct sysfs_buffer * buffer)
65 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
66 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
67 struct sysfs_ops * ops = buffer->ops;
68 int ret = 0;
69 ssize_t count;
71 if (!buffer->page)
72 buffer->page = (char *) get_zeroed_page(GFP_KERNEL);
73 if (!buffer->page)
74 return -ENOMEM;
76 /* need attr_sd for attr and ops, its parent for kobj */
77 if (!sysfs_get_active_two(attr_sd))
78 return -ENODEV;
80 buffer->event = atomic_read(&attr_sd->s_attr.open->event);
81 count = ops->show(kobj, attr_sd->s_attr.attr, buffer->page);
83 sysfs_put_active_two(attr_sd);
86 * The code works fine with PAGE_SIZE return but it's likely to
87 * indicate truncated result or overflow in normal use cases.
89 BUG_ON(count >= (ssize_t)PAGE_SIZE);
90 if (count >= 0) {
91 buffer->needs_read_fill = 0;
92 buffer->count = count;
93 } else {
94 ret = count;
96 return ret;
99 /**
100 * sysfs_read_file - read an attribute.
101 * @file: file pointer.
102 * @buf: buffer to fill.
103 * @count: number of bytes to read.
104 * @ppos: starting offset in file.
106 * Userspace wants to read an attribute file. The attribute descriptor
107 * is in the file's ->d_fsdata. The target object is in the directory's
108 * ->d_fsdata.
110 * We call fill_read_buffer() to allocate and fill the buffer from the
111 * object's show() method exactly once (if the read is happening from
112 * the beginning of the file). That should fill the entire buffer with
113 * all the data the object has to offer for that attribute.
114 * We then call flush_read_buffer() to copy the buffer to userspace
115 * in the increments specified.
118 static ssize_t
119 sysfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
121 struct sysfs_buffer * buffer = file->private_data;
122 ssize_t retval = 0;
124 mutex_lock(&buffer->mutex);
125 if (buffer->needs_read_fill) {
126 retval = fill_read_buffer(file->f_path.dentry,buffer);
127 if (retval)
128 goto out;
130 pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n",
131 __FUNCTION__, count, *ppos, buffer->page);
132 retval = simple_read_from_buffer(buf, count, ppos, buffer->page,
133 buffer->count);
134 out:
135 mutex_unlock(&buffer->mutex);
136 return retval;
140 * fill_write_buffer - copy buffer from userspace.
141 * @buffer: data buffer for file.
142 * @buf: data from user.
143 * @count: number of bytes in @userbuf.
145 * Allocate @buffer->page if it hasn't been already, then
146 * copy the user-supplied buffer into it.
149 static int
150 fill_write_buffer(struct sysfs_buffer * buffer, const char __user * buf, size_t count)
152 int error;
154 if (!buffer->page)
155 buffer->page = (char *)get_zeroed_page(GFP_KERNEL);
156 if (!buffer->page)
157 return -ENOMEM;
159 if (count >= PAGE_SIZE)
160 count = PAGE_SIZE - 1;
161 error = copy_from_user(buffer->page,buf,count);
162 buffer->needs_read_fill = 1;
163 /* if buf is assumed to contain a string, terminate it by \0,
164 so e.g. sscanf() can scan the string easily */
165 buffer->page[count] = 0;
166 return error ? -EFAULT : count;
171 * flush_write_buffer - push buffer to kobject.
172 * @dentry: dentry to the attribute
173 * @buffer: data buffer for file.
174 * @count: number of bytes
176 * Get the correct pointers for the kobject and the attribute we're
177 * dealing with, then call the store() method for the attribute,
178 * passing the buffer that we acquired in fill_write_buffer().
181 static int
182 flush_write_buffer(struct dentry * dentry, struct sysfs_buffer * buffer, size_t count)
184 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
185 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
186 struct sysfs_ops * ops = buffer->ops;
187 int rc;
189 /* need attr_sd for attr and ops, its parent for kobj */
190 if (!sysfs_get_active_two(attr_sd))
191 return -ENODEV;
193 rc = ops->store(kobj, attr_sd->s_attr.attr, buffer->page, count);
195 sysfs_put_active_two(attr_sd);
197 return rc;
202 * sysfs_write_file - write an attribute.
203 * @file: file pointer
204 * @buf: data to write
205 * @count: number of bytes
206 * @ppos: starting offset
208 * Similar to sysfs_read_file(), though working in the opposite direction.
209 * We allocate and fill the data from the user in fill_write_buffer(),
210 * then push it to the kobject in flush_write_buffer().
211 * There is no easy way for us to know if userspace is only doing a partial
212 * write, so we don't support them. We expect the entire buffer to come
213 * on the first write.
214 * Hint: if you're writing a value, first read the file, modify only the
215 * the value you're changing, then write entire buffer back.
218 static ssize_t
219 sysfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
221 struct sysfs_buffer * buffer = file->private_data;
222 ssize_t len;
224 mutex_lock(&buffer->mutex);
225 len = fill_write_buffer(buffer, buf, count);
226 if (len > 0)
227 len = flush_write_buffer(file->f_path.dentry, buffer, len);
228 if (len > 0)
229 *ppos += len;
230 mutex_unlock(&buffer->mutex);
231 return len;
235 * sysfs_get_open_dirent - get or create sysfs_open_dirent
236 * @sd: target sysfs_dirent
237 * @buffer: sysfs_buffer for this instance of open
239 * If @sd->s_attr.open exists, increment its reference count;
240 * otherwise, create one. @buffer is chained to the buffers
241 * list.
243 * LOCKING:
244 * Kernel thread context (may sleep).
246 * RETURNS:
247 * 0 on success, -errno on failure.
249 static int sysfs_get_open_dirent(struct sysfs_dirent *sd,
250 struct sysfs_buffer *buffer)
252 struct sysfs_open_dirent *od, *new_od = NULL;
254 retry:
255 spin_lock(&sysfs_open_dirent_lock);
257 if (!sd->s_attr.open && new_od) {
258 sd->s_attr.open = new_od;
259 new_od = NULL;
262 od = sd->s_attr.open;
263 if (od) {
264 atomic_inc(&od->refcnt);
265 list_add_tail(&buffer->list, &od->buffers);
268 spin_unlock(&sysfs_open_dirent_lock);
270 if (od) {
271 kfree(new_od);
272 return 0;
275 /* not there, initialize a new one and retry */
276 new_od = kmalloc(sizeof(*new_od), GFP_KERNEL);
277 if (!new_od)
278 return -ENOMEM;
280 atomic_set(&new_od->refcnt, 0);
281 atomic_set(&new_od->event, 1);
282 init_waitqueue_head(&new_od->poll);
283 INIT_LIST_HEAD(&new_od->buffers);
284 goto retry;
288 * sysfs_put_open_dirent - put sysfs_open_dirent
289 * @sd: target sysfs_dirent
290 * @buffer: associated sysfs_buffer
292 * Put @sd->s_attr.open and unlink @buffer from the buffers list.
293 * If reference count reaches zero, disassociate and free it.
295 * LOCKING:
296 * None.
298 static void sysfs_put_open_dirent(struct sysfs_dirent *sd,
299 struct sysfs_buffer *buffer)
301 struct sysfs_open_dirent *od = sd->s_attr.open;
303 spin_lock(&sysfs_open_dirent_lock);
305 list_del(&buffer->list);
306 if (atomic_dec_and_test(&od->refcnt))
307 sd->s_attr.open = NULL;
308 else
309 od = NULL;
311 spin_unlock(&sysfs_open_dirent_lock);
313 kfree(od);
316 static int sysfs_open_file(struct inode *inode, struct file *file)
318 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
319 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
320 struct sysfs_buffer *buffer;
321 struct sysfs_ops *ops;
322 int error = -EACCES;
324 /* need attr_sd for attr and ops, its parent for kobj */
325 if (!sysfs_get_active_two(attr_sd))
326 return -ENODEV;
328 /* every kobject with an attribute needs a ktype assigned */
329 if (kobj->ktype && kobj->ktype->sysfs_ops)
330 ops = kobj->ktype->sysfs_ops;
331 else {
332 printk(KERN_ERR "missing sysfs attribute operations for "
333 "kobject: %s\n", kobject_name(kobj));
334 WARN_ON(1);
335 goto err_out;
338 /* File needs write support.
339 * The inode's perms must say it's ok,
340 * and we must have a store method.
342 if (file->f_mode & FMODE_WRITE) {
343 if (!(inode->i_mode & S_IWUGO) || !ops->store)
344 goto err_out;
347 /* File needs read support.
348 * The inode's perms must say it's ok, and we there
349 * must be a show method for it.
351 if (file->f_mode & FMODE_READ) {
352 if (!(inode->i_mode & S_IRUGO) || !ops->show)
353 goto err_out;
356 /* No error? Great, allocate a buffer for the file, and store it
357 * it in file->private_data for easy access.
359 error = -ENOMEM;
360 buffer = kzalloc(sizeof(struct sysfs_buffer), GFP_KERNEL);
361 if (!buffer)
362 goto err_out;
364 mutex_init(&buffer->mutex);
365 buffer->needs_read_fill = 1;
366 buffer->ops = ops;
367 file->private_data = buffer;
369 /* make sure we have open dirent struct */
370 error = sysfs_get_open_dirent(attr_sd, buffer);
371 if (error)
372 goto err_free;
374 /* open succeeded, put active references */
375 sysfs_put_active_two(attr_sd);
376 return 0;
378 err_free:
379 kfree(buffer);
380 err_out:
381 sysfs_put_active_two(attr_sd);
382 return error;
385 static int sysfs_release(struct inode *inode, struct file *filp)
387 struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata;
388 struct sysfs_buffer *buffer = filp->private_data;
390 sysfs_put_open_dirent(sd, buffer);
392 if (buffer->page)
393 free_page((unsigned long)buffer->page);
394 kfree(buffer);
396 return 0;
399 /* Sysfs attribute files are pollable. The idea is that you read
400 * the content and then you use 'poll' or 'select' to wait for
401 * the content to change. When the content changes (assuming the
402 * manager for the kobject supports notification), poll will
403 * return POLLERR|POLLPRI, and select will return the fd whether
404 * it is waiting for read, write, or exceptions.
405 * Once poll/select indicates that the value has changed, you
406 * need to close and re-open the file, as simply seeking and reading
407 * again will not get new data, or reset the state of 'poll'.
408 * Reminder: this only works for attributes which actively support
409 * it, and it is not possible to test an attribute from userspace
410 * to see if it supports poll (Neither 'poll' nor 'select' return
411 * an appropriate error code). When in doubt, set a suitable timeout value.
413 static unsigned int sysfs_poll(struct file *filp, poll_table *wait)
415 struct sysfs_buffer * buffer = filp->private_data;
416 struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata;
417 struct sysfs_open_dirent *od = attr_sd->s_attr.open;
419 /* need parent for the kobj, grab both */
420 if (!sysfs_get_active_two(attr_sd))
421 goto trigger;
423 poll_wait(filp, &od->poll, wait);
425 sysfs_put_active_two(attr_sd);
427 if (buffer->event != atomic_read(&od->event))
428 goto trigger;
430 return 0;
432 trigger:
433 buffer->needs_read_fill = 1;
434 return POLLERR|POLLPRI;
437 void sysfs_notify(struct kobject *k, char *dir, char *attr)
439 struct sysfs_dirent *sd = k->sd;
441 mutex_lock(&sysfs_mutex);
443 if (sd && dir)
444 sd = sysfs_find_dirent(sd, dir);
445 if (sd && attr)
446 sd = sysfs_find_dirent(sd, attr);
447 if (sd) {
448 struct sysfs_open_dirent *od;
450 spin_lock(&sysfs_open_dirent_lock);
452 od = sd->s_attr.open;
453 if (od) {
454 atomic_inc(&od->event);
455 wake_up_interruptible(&od->poll);
458 spin_unlock(&sysfs_open_dirent_lock);
461 mutex_unlock(&sysfs_mutex);
463 EXPORT_SYMBOL_GPL(sysfs_notify);
465 const struct file_operations sysfs_file_operations = {
466 .read = sysfs_read_file,
467 .write = sysfs_write_file,
468 .llseek = generic_file_llseek,
469 .open = sysfs_open_file,
470 .release = sysfs_release,
471 .poll = sysfs_poll,
475 int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,
476 int type)
478 umode_t mode = (attr->mode & S_IALLUGO) | S_IFREG;
479 struct sysfs_addrm_cxt acxt;
480 struct sysfs_dirent *sd;
481 int rc;
483 sd = sysfs_new_dirent(attr->name, mode, type);
484 if (!sd)
485 return -ENOMEM;
486 sd->s_attr.attr = (void *)attr;
488 sysfs_addrm_start(&acxt, dir_sd);
489 rc = sysfs_add_one(&acxt, sd);
490 sysfs_addrm_finish(&acxt);
492 if (rc)
493 sysfs_put(sd);
495 return rc;
500 * sysfs_create_file - create an attribute file for an object.
501 * @kobj: object we're creating for.
502 * @attr: attribute descriptor.
505 int sysfs_create_file(struct kobject * kobj, const struct attribute * attr)
507 BUG_ON(!kobj || !kobj->sd || !attr);
509 return sysfs_add_file(kobj->sd, attr, SYSFS_KOBJ_ATTR);
515 * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
516 * @kobj: object we're acting for.
517 * @attr: attribute descriptor.
518 * @group: group name.
520 int sysfs_add_file_to_group(struct kobject *kobj,
521 const struct attribute *attr, const char *group)
523 struct sysfs_dirent *dir_sd;
524 int error;
526 if (group)
527 dir_sd = sysfs_get_dirent(kobj->sd, group);
528 else
529 dir_sd = sysfs_get(kobj->sd);
531 if (!dir_sd)
532 return -ENOENT;
534 error = sysfs_add_file(dir_sd, attr, SYSFS_KOBJ_ATTR);
535 sysfs_put(dir_sd);
537 return error;
539 EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
542 * sysfs_chmod_file - update the modified mode value on an object attribute.
543 * @kobj: object we're acting for.
544 * @attr: attribute descriptor.
545 * @mode: file permissions.
548 int sysfs_chmod_file(struct kobject *kobj, struct attribute *attr, mode_t mode)
550 struct sysfs_dirent *victim_sd = NULL;
551 struct dentry *victim = NULL;
552 struct inode * inode;
553 struct iattr newattrs;
554 int rc;
556 rc = -ENOENT;
557 victim_sd = sysfs_get_dirent(kobj->sd, attr->name);
558 if (!victim_sd)
559 goto out;
561 mutex_lock(&sysfs_rename_mutex);
562 victim = sysfs_get_dentry(victim_sd);
563 mutex_unlock(&sysfs_rename_mutex);
564 if (IS_ERR(victim)) {
565 rc = PTR_ERR(victim);
566 victim = NULL;
567 goto out;
570 inode = victim->d_inode;
572 mutex_lock(&inode->i_mutex);
574 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
575 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
576 rc = notify_change(victim, &newattrs);
578 if (rc == 0) {
579 mutex_lock(&sysfs_mutex);
580 victim_sd->s_mode = newattrs.ia_mode;
581 mutex_unlock(&sysfs_mutex);
584 mutex_unlock(&inode->i_mutex);
585 out:
586 dput(victim);
587 sysfs_put(victim_sd);
588 return rc;
590 EXPORT_SYMBOL_GPL(sysfs_chmod_file);
594 * sysfs_remove_file - remove an object attribute.
595 * @kobj: object we're acting for.
596 * @attr: attribute descriptor.
598 * Hash the attribute name and kill the victim.
601 void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr)
603 sysfs_hash_and_remove(kobj->sd, attr->name);
608 * sysfs_remove_file_from_group - remove an attribute file from a group.
609 * @kobj: object we're acting for.
610 * @attr: attribute descriptor.
611 * @group: group name.
613 void sysfs_remove_file_from_group(struct kobject *kobj,
614 const struct attribute *attr, const char *group)
616 struct sysfs_dirent *dir_sd;
618 if (group)
619 dir_sd = sysfs_get_dirent(kobj->sd, group);
620 else
621 dir_sd = sysfs_get(kobj->sd);
622 if (dir_sd) {
623 sysfs_hash_and_remove(dir_sd, attr->name);
624 sysfs_put(dir_sd);
627 EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
629 struct sysfs_schedule_callback_struct {
630 struct kobject *kobj;
631 void (*func)(void *);
632 void *data;
633 struct module *owner;
634 struct work_struct work;
637 static void sysfs_schedule_callback_work(struct work_struct *work)
639 struct sysfs_schedule_callback_struct *ss = container_of(work,
640 struct sysfs_schedule_callback_struct, work);
642 (ss->func)(ss->data);
643 kobject_put(ss->kobj);
644 module_put(ss->owner);
645 kfree(ss);
649 * sysfs_schedule_callback - helper to schedule a callback for a kobject
650 * @kobj: object we're acting for.
651 * @func: callback function to invoke later.
652 * @data: argument to pass to @func.
653 * @owner: module owning the callback code
655 * sysfs attribute methods must not unregister themselves or their parent
656 * kobject (which would amount to the same thing). Attempts to do so will
657 * deadlock, since unregistration is mutually exclusive with driver
658 * callbacks.
660 * Instead methods can call this routine, which will attempt to allocate
661 * and schedule a workqueue request to call back @func with @data as its
662 * argument in the workqueue's process context. @kobj will be pinned
663 * until @func returns.
665 * Returns 0 if the request was submitted, -ENOMEM if storage could not
666 * be allocated, -ENODEV if a reference to @owner isn't available.
668 int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
669 void *data, struct module *owner)
671 struct sysfs_schedule_callback_struct *ss;
673 if (!try_module_get(owner))
674 return -ENODEV;
675 ss = kmalloc(sizeof(*ss), GFP_KERNEL);
676 if (!ss) {
677 module_put(owner);
678 return -ENOMEM;
680 kobject_get(kobj);
681 ss->kobj = kobj;
682 ss->func = func;
683 ss->data = data;
684 ss->owner = owner;
685 INIT_WORK(&ss->work, sysfs_schedule_callback_work);
686 schedule_work(&ss->work);
687 return 0;
689 EXPORT_SYMBOL_GPL(sysfs_schedule_callback);
692 EXPORT_SYMBOL_GPL(sysfs_create_file);
693 EXPORT_SYMBOL_GPL(sysfs_remove_file);