2 * file.c - operations for regular (text) files.
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>
16 #define to_sattr(a) container_of(a,struct subsys_attribute, attr)
19 * Subsystem file operations.
20 * These operations allow subsystems to have files that can be
24 subsys_attr_show(struct kobject
* kobj
, struct attribute
* attr
, char * page
)
26 struct kset
*kset
= to_kset(kobj
);
27 struct subsys_attribute
* sattr
= to_sattr(attr
);
31 ret
= sattr
->show(kset
, page
);
36 subsys_attr_store(struct kobject
* kobj
, struct attribute
* attr
,
37 const char * page
, size_t count
)
39 struct kset
*kset
= to_kset(kobj
);
40 struct subsys_attribute
* sattr
= to_sattr(attr
);
44 ret
= sattr
->store(kset
, page
, count
);
48 static struct sysfs_ops subsys_sysfs_ops
= {
49 .show
= subsys_attr_show
,
50 .store
= subsys_attr_store
,
57 struct sysfs_ops
* ops
;
64 * fill_read_buffer - allocate and fill buffer from object.
65 * @dentry: dentry pointer.
66 * @buffer: data buffer for file.
68 * Allocate @buffer->page, if it hasn't been already, then call the
69 * kobject's show() method to fill the buffer with this attribute's
71 * This is called only once, on the file's first read unless an error
74 static int fill_read_buffer(struct dentry
* dentry
, struct sysfs_buffer
* buffer
)
76 struct sysfs_dirent
*attr_sd
= dentry
->d_fsdata
;
77 struct kobject
*kobj
= attr_sd
->s_parent
->s_elem
.dir
.kobj
;
78 struct sysfs_ops
* ops
= buffer
->ops
;
83 buffer
->page
= (char *) get_zeroed_page(GFP_KERNEL
);
87 /* need attr_sd for attr and ops, its parent for kobj */
88 if (!sysfs_get_active_two(attr_sd
))
91 buffer
->event
= atomic_read(&attr_sd
->s_event
);
92 count
= ops
->show(kobj
, attr_sd
->s_elem
.attr
.attr
, buffer
->page
);
94 sysfs_put_active_two(attr_sd
);
96 BUG_ON(count
> (ssize_t
)PAGE_SIZE
);
98 buffer
->needs_read_fill
= 0;
99 buffer
->count
= count
;
107 * sysfs_read_file - read an attribute.
108 * @file: file pointer.
109 * @buf: buffer to fill.
110 * @count: number of bytes to read.
111 * @ppos: starting offset in file.
113 * Userspace wants to read an attribute file. The attribute descriptor
114 * is in the file's ->d_fsdata. The target object is in the directory's
117 * We call fill_read_buffer() to allocate and fill the buffer from the
118 * object's show() method exactly once (if the read is happening from
119 * the beginning of the file). That should fill the entire buffer with
120 * all the data the object has to offer for that attribute.
121 * We then call flush_read_buffer() to copy the buffer to userspace
122 * in the increments specified.
126 sysfs_read_file(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
128 struct sysfs_buffer
* buffer
= file
->private_data
;
132 if (buffer
->needs_read_fill
) {
133 retval
= fill_read_buffer(file
->f_path
.dentry
,buffer
);
137 pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n",
138 __FUNCTION__
, count
, *ppos
, buffer
->page
);
139 retval
= simple_read_from_buffer(buf
, count
, ppos
, buffer
->page
,
147 * fill_write_buffer - copy buffer from userspace.
148 * @buffer: data buffer for file.
149 * @buf: data from user.
150 * @count: number of bytes in @userbuf.
152 * Allocate @buffer->page if it hasn't been already, then
153 * copy the user-supplied buffer into it.
157 fill_write_buffer(struct sysfs_buffer
* buffer
, const char __user
* buf
, size_t count
)
162 buffer
->page
= (char *)get_zeroed_page(GFP_KERNEL
);
166 if (count
>= PAGE_SIZE
)
167 count
= PAGE_SIZE
- 1;
168 error
= copy_from_user(buffer
->page
,buf
,count
);
169 buffer
->needs_read_fill
= 1;
170 /* if buf is assumed to contain a string, terminate it by \0,
171 so e.g. sscanf() can scan the string easily */
172 buffer
->page
[count
] = 0;
173 return error
? -EFAULT
: count
;
178 * flush_write_buffer - push buffer to kobject.
179 * @dentry: dentry to the attribute
180 * @buffer: data buffer for file.
181 * @count: number of bytes
183 * Get the correct pointers for the kobject and the attribute we're
184 * dealing with, then call the store() method for the attribute,
185 * passing the buffer that we acquired in fill_write_buffer().
189 flush_write_buffer(struct dentry
* dentry
, struct sysfs_buffer
* buffer
, size_t count
)
191 struct sysfs_dirent
*attr_sd
= dentry
->d_fsdata
;
192 struct kobject
*kobj
= attr_sd
->s_parent
->s_elem
.dir
.kobj
;
193 struct sysfs_ops
* ops
= buffer
->ops
;
196 /* need attr_sd for attr and ops, its parent for kobj */
197 if (!sysfs_get_active_two(attr_sd
))
200 rc
= ops
->store(kobj
, attr_sd
->s_elem
.attr
.attr
, buffer
->page
, count
);
202 sysfs_put_active_two(attr_sd
);
209 * sysfs_write_file - write an attribute.
210 * @file: file pointer
211 * @buf: data to write
212 * @count: number of bytes
213 * @ppos: starting offset
215 * Similar to sysfs_read_file(), though working in the opposite direction.
216 * We allocate and fill the data from the user in fill_write_buffer(),
217 * then push it to the kobject in flush_write_buffer().
218 * There is no easy way for us to know if userspace is only doing a partial
219 * write, so we don't support them. We expect the entire buffer to come
220 * on the first write.
221 * Hint: if you're writing a value, first read the file, modify only the
222 * the value you're changing, then write entire buffer back.
226 sysfs_write_file(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*ppos
)
228 struct sysfs_buffer
* buffer
= file
->private_data
;
232 len
= fill_write_buffer(buffer
, buf
, count
);
234 len
= flush_write_buffer(file
->f_path
.dentry
, buffer
, len
);
241 static int sysfs_open_file(struct inode
*inode
, struct file
*file
)
243 struct sysfs_dirent
*attr_sd
= file
->f_path
.dentry
->d_fsdata
;
244 struct attribute
*attr
= attr_sd
->s_elem
.attr
.attr
;
245 struct kobject
*kobj
= attr_sd
->s_parent
->s_elem
.dir
.kobj
;
246 struct sysfs_buffer
* buffer
;
247 struct sysfs_ops
* ops
= NULL
;
250 /* need attr_sd for attr and ops, its parent for kobj */
251 if (!sysfs_get_active_two(attr_sd
))
254 /* Grab the module reference for this attribute */
256 if (!try_module_get(attr
->owner
))
259 /* if the kobject has no ktype, then we assume that it is a subsystem
260 * itself, and use ops for it.
262 if (kobj
->kset
&& kobj
->kset
->ktype
)
263 ops
= kobj
->kset
->ktype
->sysfs_ops
;
264 else if (kobj
->ktype
)
265 ops
= kobj
->ktype
->sysfs_ops
;
267 ops
= &subsys_sysfs_ops
;
271 /* No sysfs operations, either from having no subsystem,
272 * or the subsystem have no operations.
277 /* File needs write support.
278 * The inode's perms must say it's ok,
279 * and we must have a store method.
281 if (file
->f_mode
& FMODE_WRITE
) {
282 if (!(inode
->i_mode
& S_IWUGO
) || !ops
->store
)
286 /* File needs read support.
287 * The inode's perms must say it's ok, and we there
288 * must be a show method for it.
290 if (file
->f_mode
& FMODE_READ
) {
291 if (!(inode
->i_mode
& S_IRUGO
) || !ops
->show
)
295 /* No error? Great, allocate a buffer for the file, and store it
296 * it in file->private_data for easy access.
299 buffer
= kzalloc(sizeof(struct sysfs_buffer
), GFP_KERNEL
);
303 init_MUTEX(&buffer
->sem
);
304 buffer
->needs_read_fill
= 1;
306 file
->private_data
= buffer
;
308 /* open succeeded, put active references and pin attr_sd */
309 sysfs_put_active_two(attr_sd
);
314 module_put(attr
->owner
);
316 sysfs_put_active_two(attr_sd
);
320 static int sysfs_release(struct inode
* inode
, struct file
* filp
)
322 struct sysfs_dirent
*attr_sd
= filp
->f_path
.dentry
->d_fsdata
;
323 struct attribute
*attr
= attr_sd
->s_elem
.attr
.attr
;
324 struct sysfs_buffer
*buffer
= filp
->private_data
;
327 /* After this point, attr should not be accessed. */
328 module_put(attr
->owner
);
332 free_page((unsigned long)buffer
->page
);
338 /* Sysfs attribute files are pollable. The idea is that you read
339 * the content and then you use 'poll' or 'select' to wait for
340 * the content to change. When the content changes (assuming the
341 * manager for the kobject supports notification), poll will
342 * return POLLERR|POLLPRI, and select will return the fd whether
343 * it is waiting for read, write, or exceptions.
344 * Once poll/select indicates that the value has changed, you
345 * need to close and re-open the file, as simply seeking and reading
346 * again will not get new data, or reset the state of 'poll'.
347 * Reminder: this only works for attributes which actively support
348 * it, and it is not possible to test an attribute from userspace
349 * to see if it supports poll (Nether 'poll' or 'select' return
350 * an appropriate error code). When in doubt, set a suitable timeout value.
352 static unsigned int sysfs_poll(struct file
*filp
, poll_table
*wait
)
354 struct sysfs_buffer
* buffer
= filp
->private_data
;
355 struct sysfs_dirent
*attr_sd
= filp
->f_path
.dentry
->d_fsdata
;
356 struct kobject
*kobj
= attr_sd
->s_parent
->s_elem
.dir
.kobj
;
358 /* need parent for the kobj, grab both */
359 if (!sysfs_get_active_two(attr_sd
))
362 poll_wait(filp
, &kobj
->poll
, wait
);
364 sysfs_put_active_two(attr_sd
);
366 if (buffer
->event
!= atomic_read(&attr_sd
->s_event
))
372 buffer
->needs_read_fill
= 1;
373 return POLLERR
|POLLPRI
;
377 static struct dentry
*step_down(struct dentry
*dir
, const char * name
)
381 if (dir
== NULL
|| dir
->d_inode
== NULL
)
384 mutex_lock(&dir
->d_inode
->i_mutex
);
385 de
= lookup_one_len(name
, dir
, strlen(name
));
386 mutex_unlock(&dir
->d_inode
->i_mutex
);
390 if (de
->d_inode
== NULL
) {
397 void sysfs_notify(struct kobject
* k
, char *dir
, char *attr
)
399 struct dentry
*de
= k
->dentry
;
403 de
= step_down(de
, dir
);
405 de
= step_down(de
, attr
);
407 struct sysfs_dirent
* sd
= de
->d_fsdata
;
409 atomic_inc(&sd
->s_event
);
410 wake_up_interruptible(&k
->poll
);
414 EXPORT_SYMBOL_GPL(sysfs_notify
);
416 const struct file_operations sysfs_file_operations
= {
417 .read
= sysfs_read_file
,
418 .write
= sysfs_write_file
,
419 .llseek
= generic_file_llseek
,
420 .open
= sysfs_open_file
,
421 .release
= sysfs_release
,
426 int sysfs_add_file(struct dentry
* dir
, const struct attribute
* attr
, int type
)
428 struct sysfs_dirent
* parent_sd
= dir
->d_fsdata
;
429 umode_t mode
= (attr
->mode
& S_IALLUGO
) | S_IFREG
;
430 struct sysfs_dirent
*sd
;
433 mutex_lock(&dir
->d_inode
->i_mutex
);
435 if (sysfs_dirent_exist(parent_sd
, attr
->name
)) {
440 sd
= sysfs_new_dirent(attr
->name
, mode
, type
);
445 sd
->s_elem
.attr
.attr
= (void *)attr
;
446 sysfs_attach_dirent(sd
, parent_sd
, NULL
);
449 mutex_unlock(&dir
->d_inode
->i_mutex
);
455 * sysfs_create_file - create an attribute file for an object.
456 * @kobj: object we're creating for.
457 * @attr: atrribute descriptor.
460 int sysfs_create_file(struct kobject
* kobj
, const struct attribute
* attr
)
462 BUG_ON(!kobj
|| !kobj
->dentry
|| !attr
);
464 return sysfs_add_file(kobj
->dentry
, attr
, SYSFS_KOBJ_ATTR
);
470 * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
471 * @kobj: object we're acting for.
472 * @attr: attribute descriptor.
473 * @group: group name.
475 int sysfs_add_file_to_group(struct kobject
*kobj
,
476 const struct attribute
*attr
, const char *group
)
481 dir
= lookup_one_len(group
, kobj
->dentry
, strlen(group
));
483 error
= PTR_ERR(dir
);
485 error
= sysfs_add_file(dir
, attr
, SYSFS_KOBJ_ATTR
);
490 EXPORT_SYMBOL_GPL(sysfs_add_file_to_group
);
494 * sysfs_update_file - update the modified timestamp on an object attribute.
495 * @kobj: object we're acting for.
496 * @attr: attribute descriptor.
498 int sysfs_update_file(struct kobject
* kobj
, const struct attribute
* attr
)
500 struct dentry
* dir
= kobj
->dentry
;
501 struct dentry
* victim
;
504 mutex_lock(&dir
->d_inode
->i_mutex
);
505 victim
= lookup_one_len(attr
->name
, dir
, strlen(attr
->name
));
506 if (!IS_ERR(victim
)) {
507 /* make sure dentry is really there */
508 if (victim
->d_inode
&&
509 (victim
->d_parent
->d_inode
== dir
->d_inode
)) {
510 victim
->d_inode
->i_mtime
= CURRENT_TIME
;
511 fsnotify_modify(victim
);
517 * Drop the reference acquired from lookup_one_len() above.
521 mutex_unlock(&dir
->d_inode
->i_mutex
);
528 * sysfs_chmod_file - update the modified mode value on an object attribute.
529 * @kobj: object we're acting for.
530 * @attr: attribute descriptor.
531 * @mode: file permissions.
534 int sysfs_chmod_file(struct kobject
*kobj
, struct attribute
*attr
, mode_t mode
)
536 struct dentry
*dir
= kobj
->dentry
;
537 struct dentry
*victim
;
538 struct inode
* inode
;
539 struct iattr newattrs
;
542 mutex_lock(&dir
->d_inode
->i_mutex
);
543 victim
= lookup_one_len(attr
->name
, dir
, strlen(attr
->name
));
544 if (!IS_ERR(victim
)) {
545 if (victim
->d_inode
&&
546 (victim
->d_parent
->d_inode
== dir
->d_inode
)) {
547 inode
= victim
->d_inode
;
548 mutex_lock(&inode
->i_mutex
);
549 newattrs
.ia_mode
= (mode
& S_IALLUGO
) |
550 (inode
->i_mode
& ~S_IALLUGO
);
551 newattrs
.ia_valid
= ATTR_MODE
| ATTR_CTIME
;
552 res
= notify_change(victim
, &newattrs
);
553 mutex_unlock(&inode
->i_mutex
);
557 mutex_unlock(&dir
->d_inode
->i_mutex
);
561 EXPORT_SYMBOL_GPL(sysfs_chmod_file
);
565 * sysfs_remove_file - remove an object attribute.
566 * @kobj: object we're acting for.
567 * @attr: attribute descriptor.
569 * Hash the attribute name and kill the victim.
572 void sysfs_remove_file(struct kobject
* kobj
, const struct attribute
* attr
)
574 sysfs_hash_and_remove(kobj
->dentry
, attr
->name
);
579 * sysfs_remove_file_from_group - remove an attribute file from a group.
580 * @kobj: object we're acting for.
581 * @attr: attribute descriptor.
582 * @group: group name.
584 void sysfs_remove_file_from_group(struct kobject
*kobj
,
585 const struct attribute
*attr
, const char *group
)
589 dir
= lookup_one_len(group
, kobj
->dentry
, strlen(group
));
591 sysfs_hash_and_remove(dir
, attr
->name
);
595 EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group
);
597 struct sysfs_schedule_callback_struct
{
598 struct kobject
*kobj
;
599 void (*func
)(void *);
601 struct module
*owner
;
602 struct work_struct work
;
605 static void sysfs_schedule_callback_work(struct work_struct
*work
)
607 struct sysfs_schedule_callback_struct
*ss
= container_of(work
,
608 struct sysfs_schedule_callback_struct
, work
);
610 (ss
->func
)(ss
->data
);
611 kobject_put(ss
->kobj
);
612 module_put(ss
->owner
);
617 * sysfs_schedule_callback - helper to schedule a callback for a kobject
618 * @kobj: object we're acting for.
619 * @func: callback function to invoke later.
620 * @data: argument to pass to @func.
621 * @owner: module owning the callback code
623 * sysfs attribute methods must not unregister themselves or their parent
624 * kobject (which would amount to the same thing). Attempts to do so will
625 * deadlock, since unregistration is mutually exclusive with driver
628 * Instead methods can call this routine, which will attempt to allocate
629 * and schedule a workqueue request to call back @func with @data as its
630 * argument in the workqueue's process context. @kobj will be pinned
631 * until @func returns.
633 * Returns 0 if the request was submitted, -ENOMEM if storage could not
634 * be allocated, -ENODEV if a reference to @owner isn't available.
636 int sysfs_schedule_callback(struct kobject
*kobj
, void (*func
)(void *),
637 void *data
, struct module
*owner
)
639 struct sysfs_schedule_callback_struct
*ss
;
641 if (!try_module_get(owner
))
643 ss
= kmalloc(sizeof(*ss
), GFP_KERNEL
);
653 INIT_WORK(&ss
->work
, sysfs_schedule_callback_work
);
654 schedule_work(&ss
->work
);
657 EXPORT_SYMBOL_GPL(sysfs_schedule_callback
);
660 EXPORT_SYMBOL_GPL(sysfs_create_file
);
661 EXPORT_SYMBOL_GPL(sysfs_remove_file
);
662 EXPORT_SYMBOL_GPL(sysfs_update_file
);