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/kallsyms.h>
16 #include <linux/slab.h>
17 #include <linux/list.h>
18 #include <linux/mutex.h>
19 #include <linux/seq_file.h>
22 #include "../kernfs/kernfs-internal.h"
25 * Determine ktype->sysfs_ops for the given kernfs_node. This function
26 * must be called while holding an active reference.
28 static const struct sysfs_ops *sysfs_file_ops(struct kernfs_node *kn)
30 struct kobject *kobj = kn->parent->priv;
32 if (kn->flags & KERNFS_LOCKDEP)
33 lockdep_assert_held(kn);
34 return kobj->ktype ? kobj->ktype->sysfs_ops : NULL;
38 * Reads on sysfs are handled through seq_file, which takes care of hairy
39 * details like buffering and seeking. The following function pipes
40 * sysfs_ops->show() result through seq_file.
42 static int sysfs_kf_seq_show(struct seq_file *sf, void *v)
44 struct kernfs_open_file *of = sf->private;
45 struct kobject *kobj = of->kn->parent->priv;
46 const struct sysfs_ops *ops = sysfs_file_ops(of->kn);
50 /* acquire buffer and ensure that it's >= PAGE_SIZE */
51 count = seq_get_buf(sf, &buf);
52 if (count < PAGE_SIZE) {
58 * Invoke show(). Control may reach here via seq file lseek even
59 * if @ops->show() isn't implemented.
62 count = ops->show(kobj, of->kn->priv, buf);
68 * The code works fine with PAGE_SIZE return but it's likely to
69 * indicate truncated result or overflow in normal use cases.
71 if (count >= (ssize_t)PAGE_SIZE) {
72 print_symbol("fill_read_buffer: %s returned bad count\n",
73 (unsigned long)ops->show);
74 /* Try to struggle along */
75 count = PAGE_SIZE - 1;
77 seq_commit(sf, count);
81 static ssize_t sysfs_kf_bin_read(struct kernfs_open_file *of, char *buf,
82 size_t count, loff_t pos)
84 struct bin_attribute *battr = of->kn->priv;
85 struct kobject *kobj = of->kn->parent->priv;
86 loff_t size = file_inode(of->file)->i_size;
94 if (pos + count > size)
101 return battr->read(of->file, kobj, battr, buf, pos, count);
104 /* kernfs write callback for regular sysfs files */
105 static ssize_t sysfs_kf_write(struct kernfs_open_file *of, char *buf,
106 size_t count, loff_t pos)
108 const struct sysfs_ops *ops = sysfs_file_ops(of->kn);
109 struct kobject *kobj = of->kn->parent->priv;
114 return ops->store(kobj, of->kn->priv, buf, count);
117 /* kernfs write callback for bin sysfs files */
118 static ssize_t sysfs_kf_bin_write(struct kernfs_open_file *of, char *buf,
119 size_t count, loff_t pos)
121 struct bin_attribute *battr = of->kn->priv;
122 struct kobject *kobj = of->kn->parent->priv;
123 loff_t size = file_inode(of->file)->i_size;
128 count = min_t(ssize_t, count, size - pos);
136 return battr->write(of->file, kobj, battr, buf, pos, count);
139 static int sysfs_kf_bin_mmap(struct kernfs_open_file *of,
140 struct vm_area_struct *vma)
142 struct bin_attribute *battr = of->kn->priv;
143 struct kobject *kobj = of->kn->parent->priv;
145 return battr->mmap(of->file, kobj, battr, vma);
148 void sysfs_notify(struct kobject *kobj, const char *dir, const char *attr)
150 struct kernfs_node *kn = kobj->sd, *tmp;
153 kn = kernfs_find_and_get(kn, dir);
158 tmp = kernfs_find_and_get(kn, attr);
168 EXPORT_SYMBOL_GPL(sysfs_notify);
170 static const struct kernfs_ops sysfs_file_kfops_empty = {
173 static const struct kernfs_ops sysfs_file_kfops_ro = {
174 .seq_show = sysfs_kf_seq_show,
177 static const struct kernfs_ops sysfs_file_kfops_wo = {
178 .write = sysfs_kf_write,
181 static const struct kernfs_ops sysfs_file_kfops_rw = {
182 .seq_show = sysfs_kf_seq_show,
183 .write = sysfs_kf_write,
186 static const struct kernfs_ops sysfs_bin_kfops_ro = {
187 .read = sysfs_kf_bin_read,
190 static const struct kernfs_ops sysfs_bin_kfops_wo = {
191 .write = sysfs_kf_bin_write,
194 static const struct kernfs_ops sysfs_bin_kfops_rw = {
195 .read = sysfs_kf_bin_read,
196 .write = sysfs_kf_bin_write,
199 static const struct kernfs_ops sysfs_bin_kfops_mmap = {
200 .read = sysfs_kf_bin_read,
201 .write = sysfs_kf_bin_write,
202 .mmap = sysfs_kf_bin_mmap,
205 int sysfs_add_file_mode_ns(struct kernfs_node *parent,
206 const struct attribute *attr, bool is_bin,
207 umode_t mode, const void *ns)
209 struct lock_class_key *key = NULL;
210 const struct kernfs_ops *ops;
211 struct kernfs_node *kn;
215 struct kobject *kobj = parent->priv;
216 const struct sysfs_ops *sysfs_ops = kobj->ktype->sysfs_ops;
218 /* every kobject with an attribute needs a ktype assigned */
219 if (WARN(!sysfs_ops, KERN_ERR
220 "missing sysfs attribute operations for kobject: %s\n",
224 if (sysfs_ops->show && sysfs_ops->store)
225 ops = &sysfs_file_kfops_rw;
226 else if (sysfs_ops->show)
227 ops = &sysfs_file_kfops_ro;
228 else if (sysfs_ops->store)
229 ops = &sysfs_file_kfops_wo;
231 ops = &sysfs_file_kfops_empty;
235 struct bin_attribute *battr = (void *)attr;
238 ops = &sysfs_bin_kfops_mmap;
239 else if (battr->read && battr->write)
240 ops = &sysfs_bin_kfops_rw;
241 else if (battr->read)
242 ops = &sysfs_bin_kfops_ro;
243 else if (battr->write)
244 ops = &sysfs_bin_kfops_wo;
246 ops = &sysfs_file_kfops_empty;
251 #ifdef CONFIG_DEBUG_LOCK_ALLOC
252 if (!attr->ignore_lockdep)
253 key = attr->key ?: (struct lock_class_key *)&attr->skey;
255 kn = __kernfs_create_file(parent, attr->name, mode, size, ops,
256 (void *)attr, ns, true, key);
258 if (PTR_ERR(kn) == -EEXIST)
259 sysfs_warn_dup(parent, attr->name);
265 int sysfs_add_file(struct kernfs_node *parent, const struct attribute *attr,
268 return sysfs_add_file_mode_ns(parent, attr, is_bin, attr->mode, NULL);
272 * sysfs_create_file_ns - create an attribute file for an object with custom ns
273 * @kobj: object we're creating for
274 * @attr: attribute descriptor
275 * @ns: namespace the new file should belong to
277 int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
280 BUG_ON(!kobj || !kobj->sd || !attr);
282 return sysfs_add_file_mode_ns(kobj->sd, attr, false, attr->mode, ns);
285 EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
287 int sysfs_create_files(struct kobject *kobj, const struct attribute **ptr)
292 for (i = 0; ptr[i] && !err; i++)
293 err = sysfs_create_file(kobj, ptr[i]);
296 sysfs_remove_file(kobj, ptr[i]);
299 EXPORT_SYMBOL_GPL(sysfs_create_files);
302 * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
303 * @kobj: object we're acting for.
304 * @attr: attribute descriptor.
305 * @group: group name.
307 int sysfs_add_file_to_group(struct kobject *kobj,
308 const struct attribute *attr, const char *group)
310 struct kernfs_node *parent;
314 parent = kernfs_find_and_get(kobj->sd, group);
323 error = sysfs_add_file(parent, attr, false);
328 EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
331 * sysfs_chmod_file - update the modified mode value on an object attribute.
332 * @kobj: object we're acting for.
333 * @attr: attribute descriptor.
334 * @mode: file permissions.
337 int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
340 struct kernfs_node *kn;
341 struct iattr newattrs;
344 kn = kernfs_find_and_get(kobj->sd, attr->name);
348 newattrs.ia_mode = (mode & S_IALLUGO) | (kn->mode & ~S_IALLUGO);
349 newattrs.ia_valid = ATTR_MODE;
351 rc = kernfs_setattr(kn, &newattrs);
356 EXPORT_SYMBOL_GPL(sysfs_chmod_file);
359 * sysfs_remove_file_ns - remove an object attribute with a custom ns tag
360 * @kobj: object we're acting for
361 * @attr: attribute descriptor
362 * @ns: namespace tag of the file to remove
364 * Hash the attribute name and namespace tag and kill the victim.
366 void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
369 struct kernfs_node *parent = kobj->sd;
371 kernfs_remove_by_name_ns(parent, attr->name, ns);
373 EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
376 * sysfs_remove_file_self - remove an object attribute from its own method
377 * @kobj: object we're acting for
378 * @attr: attribute descriptor
380 * See kernfs_remove_self() for details.
382 bool sysfs_remove_file_self(struct kobject *kobj, const struct attribute *attr)
384 struct kernfs_node *parent = kobj->sd;
385 struct kernfs_node *kn;
388 kn = kernfs_find_and_get(parent, attr->name);
389 if (WARN_ON_ONCE(!kn))
392 ret = kernfs_remove_self(kn);
398 void sysfs_remove_files(struct kobject *kobj, const struct attribute **ptr)
401 for (i = 0; ptr[i]; i++)
402 sysfs_remove_file(kobj, ptr[i]);
404 EXPORT_SYMBOL_GPL(sysfs_remove_files);
407 * sysfs_remove_file_from_group - remove an attribute file from a group.
408 * @kobj: object we're acting for.
409 * @attr: attribute descriptor.
410 * @group: group name.
412 void sysfs_remove_file_from_group(struct kobject *kobj,
413 const struct attribute *attr, const char *group)
415 struct kernfs_node *parent;
418 parent = kernfs_find_and_get(kobj->sd, group);
425 kernfs_remove_by_name(parent, attr->name);
429 EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
432 * sysfs_create_bin_file - create binary file for object.
434 * @attr: attribute descriptor.
436 int sysfs_create_bin_file(struct kobject *kobj,
437 const struct bin_attribute *attr)
439 BUG_ON(!kobj || !kobj->sd || !attr);
441 return sysfs_add_file(kobj->sd, &attr->attr, true);
443 EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
446 * sysfs_remove_bin_file - remove binary file for object.
448 * @attr: attribute descriptor.
450 void sysfs_remove_bin_file(struct kobject *kobj,
451 const struct bin_attribute *attr)
453 kernfs_remove_by_name(kobj->sd, attr->attr.name);
455 EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);
457 struct sysfs_schedule_callback_struct {
458 struct list_head workq_list;
459 struct kobject *kobj;
460 void (*func)(void *);
462 struct module *owner;
463 struct work_struct work;
466 static struct workqueue_struct *sysfs_workqueue;
467 static DEFINE_MUTEX(sysfs_workq_mutex);
468 static LIST_HEAD(sysfs_workq);
469 static void sysfs_schedule_callback_work(struct work_struct *work)
471 struct sysfs_schedule_callback_struct *ss = container_of(work,
472 struct sysfs_schedule_callback_struct, work);
474 (ss->func)(ss->data);
475 kobject_put(ss->kobj);
476 module_put(ss->owner);
477 mutex_lock(&sysfs_workq_mutex);
478 list_del(&ss->workq_list);
479 mutex_unlock(&sysfs_workq_mutex);
484 * sysfs_schedule_callback - helper to schedule a callback for a kobject
485 * @kobj: object we're acting for.
486 * @func: callback function to invoke later.
487 * @data: argument to pass to @func.
488 * @owner: module owning the callback code
490 * sysfs attribute methods must not unregister themselves or their parent
491 * kobject (which would amount to the same thing). Attempts to do so will
492 * deadlock, since unregistration is mutually exclusive with driver
495 * Instead methods can call this routine, which will attempt to allocate
496 * and schedule a workqueue request to call back @func with @data as its
497 * argument in the workqueue's process context. @kobj will be pinned
498 * until @func returns.
500 * Returns 0 if the request was submitted, -ENOMEM if storage could not
501 * be allocated, -ENODEV if a reference to @owner isn't available,
502 * -EAGAIN if a callback has already been scheduled for @kobj.
504 int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
505 void *data, struct module *owner)
507 struct sysfs_schedule_callback_struct *ss, *tmp;
509 if (!try_module_get(owner))
512 mutex_lock(&sysfs_workq_mutex);
513 list_for_each_entry_safe(ss, tmp, &sysfs_workq, workq_list)
514 if (ss->kobj == kobj) {
516 mutex_unlock(&sysfs_workq_mutex);
519 mutex_unlock(&sysfs_workq_mutex);
521 if (sysfs_workqueue == NULL) {
522 sysfs_workqueue = create_singlethread_workqueue("sysfsd");
523 if (sysfs_workqueue == NULL) {
529 ss = kmalloc(sizeof(*ss), GFP_KERNEL);
539 INIT_WORK(&ss->work, sysfs_schedule_callback_work);
540 INIT_LIST_HEAD(&ss->workq_list);
541 mutex_lock(&sysfs_workq_mutex);
542 list_add_tail(&ss->workq_list, &sysfs_workq);
543 mutex_unlock(&sysfs_workq_mutex);
544 queue_work(sysfs_workqueue, &ss->work);
547 EXPORT_SYMBOL_GPL(sysfs_schedule_callback);