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 sysfs_dirent. This function
26 * must be called while holding an active reference.
28 static const struct sysfs_ops *sysfs_file_ops(struct sysfs_dirent *sd)
30 struct kobject *kobj = sd->s_parent->priv;
32 if (sd->s_flags & SYSFS_FLAG_LOCKDEP)
33 lockdep_assert_held(sd);
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 sysfs_open_file *of = sf->private;
45 struct kobject *kobj = of->sd->s_parent->priv;
46 const struct sysfs_ops *ops = sysfs_file_ops(of->sd);
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->sd->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 sysfs_open_file *of, char *buf,
82 size_t count, loff_t pos)
84 struct bin_attribute *battr = of->sd->priv;
85 struct kobject *kobj = of->sd->s_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 sysfs_open_file *of, char *buf,
106 size_t count, loff_t pos)
108 const struct sysfs_ops *ops = sysfs_file_ops(of->sd);
109 struct kobject *kobj = of->sd->s_parent->priv;
114 return ops->store(kobj, of->sd->priv, buf, count);
117 /* kernfs write callback for bin sysfs files */
118 static ssize_t sysfs_kf_bin_write(struct sysfs_open_file *of, char *buf,
119 size_t count, loff_t pos)
121 struct bin_attribute *battr = of->sd->priv;
122 struct kobject *kobj = of->sd->s_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 sysfs_open_file *of,
140 struct vm_area_struct *vma)
142 struct bin_attribute *battr = of->sd->priv;
143 struct kobject *kobj = of->sd->s_parent->priv;
148 return battr->mmap(of->file, kobj, battr, vma);
151 void sysfs_notify(struct kobject *k, const char *dir, const char *attr)
153 struct sysfs_dirent *sd = k->sd, *tmp;
156 sd = kernfs_find_and_get(sd, dir);
161 tmp = kernfs_find_and_get(sd, attr);
171 EXPORT_SYMBOL_GPL(sysfs_notify);
173 static const struct kernfs_ops sysfs_file_kfops_empty = {
176 static const struct kernfs_ops sysfs_file_kfops_ro = {
177 .seq_show = sysfs_kf_seq_show,
180 static const struct kernfs_ops sysfs_file_kfops_wo = {
181 .write = sysfs_kf_write,
184 static const struct kernfs_ops sysfs_file_kfops_rw = {
185 .seq_show = sysfs_kf_seq_show,
186 .write = sysfs_kf_write,
189 static const struct kernfs_ops sysfs_bin_kfops_ro = {
190 .read = sysfs_kf_bin_read,
193 static const struct kernfs_ops sysfs_bin_kfops_wo = {
194 .write = sysfs_kf_bin_write,
197 static const struct kernfs_ops sysfs_bin_kfops_rw = {
198 .read = sysfs_kf_bin_read,
199 .write = sysfs_kf_bin_write,
200 .mmap = sysfs_kf_bin_mmap,
203 int sysfs_add_file_mode_ns(struct sysfs_dirent *dir_sd,
204 const struct attribute *attr, bool is_bin,
205 umode_t mode, const void *ns)
207 struct lock_class_key *key = NULL;
208 const struct kernfs_ops *ops;
209 struct sysfs_dirent *sd;
213 struct kobject *kobj = dir_sd->priv;
214 const struct sysfs_ops *sysfs_ops = kobj->ktype->sysfs_ops;
216 /* every kobject with an attribute needs a ktype assigned */
217 if (WARN(!sysfs_ops, KERN_ERR
218 "missing sysfs attribute operations for kobject: %s\n",
222 if (sysfs_ops->show && sysfs_ops->store)
223 ops = &sysfs_file_kfops_rw;
224 else if (sysfs_ops->show)
225 ops = &sysfs_file_kfops_ro;
226 else if (sysfs_ops->store)
227 ops = &sysfs_file_kfops_wo;
229 ops = &sysfs_file_kfops_empty;
233 struct bin_attribute *battr = (void *)attr;
235 if ((battr->read && battr->write) || battr->mmap)
236 ops = &sysfs_bin_kfops_rw;
237 else if (battr->read)
238 ops = &sysfs_bin_kfops_ro;
239 else if (battr->write)
240 ops = &sysfs_bin_kfops_wo;
242 ops = &sysfs_file_kfops_empty;
247 #ifdef CONFIG_DEBUG_LOCK_ALLOC
248 if (!attr->ignore_lockdep)
249 key = attr->key ?: (struct lock_class_key *)&attr->skey;
251 sd = kernfs_create_file_ns_key(dir_sd, attr->name, mode, size,
252 ops, (void *)attr, ns, key);
254 if (PTR_ERR(sd) == -EEXIST)
255 sysfs_warn_dup(dir_sd, attr->name);
261 int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,
264 return sysfs_add_file_mode_ns(dir_sd, attr, is_bin, attr->mode, NULL);
268 * sysfs_create_file_ns - create an attribute file for an object with custom ns
269 * @kobj: object we're creating for
270 * @attr: attribute descriptor
271 * @ns: namespace the new file should belong to
273 int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
276 BUG_ON(!kobj || !kobj->sd || !attr);
278 return sysfs_add_file_mode_ns(kobj->sd, attr, false, attr->mode, ns);
281 EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
283 int sysfs_create_files(struct kobject *kobj, const struct attribute **ptr)
288 for (i = 0; ptr[i] && !err; i++)
289 err = sysfs_create_file(kobj, ptr[i]);
292 sysfs_remove_file(kobj, ptr[i]);
295 EXPORT_SYMBOL_GPL(sysfs_create_files);
298 * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
299 * @kobj: object we're acting for.
300 * @attr: attribute descriptor.
301 * @group: group name.
303 int sysfs_add_file_to_group(struct kobject *kobj,
304 const struct attribute *attr, const char *group)
306 struct sysfs_dirent *dir_sd;
310 dir_sd = kernfs_find_and_get(kobj->sd, group);
319 error = sysfs_add_file(dir_sd, attr, false);
324 EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
327 * sysfs_chmod_file - update the modified mode value on an object attribute.
328 * @kobj: object we're acting for.
329 * @attr: attribute descriptor.
330 * @mode: file permissions.
333 int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
336 struct sysfs_dirent *sd;
337 struct iattr newattrs;
340 sd = kernfs_find_and_get(kobj->sd, attr->name);
344 newattrs.ia_mode = (mode & S_IALLUGO) | (sd->s_mode & ~S_IALLUGO);
345 newattrs.ia_valid = ATTR_MODE;
347 rc = kernfs_setattr(sd, &newattrs);
352 EXPORT_SYMBOL_GPL(sysfs_chmod_file);
355 * sysfs_remove_file_ns - remove an object attribute with a custom ns tag
356 * @kobj: object we're acting for
357 * @attr: attribute descriptor
358 * @ns: namespace tag of the file to remove
360 * Hash the attribute name and namespace tag and kill the victim.
362 void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
365 struct sysfs_dirent *dir_sd = kobj->sd;
367 kernfs_remove_by_name_ns(dir_sd, attr->name, ns);
369 EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
371 void sysfs_remove_files(struct kobject *kobj, const struct attribute **ptr)
374 for (i = 0; ptr[i]; i++)
375 sysfs_remove_file(kobj, ptr[i]);
377 EXPORT_SYMBOL_GPL(sysfs_remove_files);
380 * sysfs_remove_file_from_group - remove an attribute file from a group.
381 * @kobj: object we're acting for.
382 * @attr: attribute descriptor.
383 * @group: group name.
385 void sysfs_remove_file_from_group(struct kobject *kobj,
386 const struct attribute *attr, const char *group)
388 struct sysfs_dirent *dir_sd;
391 dir_sd = kernfs_find_and_get(kobj->sd, group);
398 kernfs_remove_by_name(dir_sd, attr->name);
402 EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
405 * sysfs_create_bin_file - create binary file for object.
407 * @attr: attribute descriptor.
409 int sysfs_create_bin_file(struct kobject *kobj,
410 const struct bin_attribute *attr)
412 BUG_ON(!kobj || !kobj->sd || !attr);
414 return sysfs_add_file(kobj->sd, &attr->attr, true);
416 EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
419 * sysfs_remove_bin_file - remove binary file for object.
421 * @attr: attribute descriptor.
423 void sysfs_remove_bin_file(struct kobject *kobj,
424 const struct bin_attribute *attr)
426 kernfs_remove_by_name(kobj->sd, attr->attr.name);
428 EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);
430 struct sysfs_schedule_callback_struct {
431 struct list_head workq_list;
432 struct kobject *kobj;
433 void (*func)(void *);
435 struct module *owner;
436 struct work_struct work;
439 static struct workqueue_struct *sysfs_workqueue;
440 static DEFINE_MUTEX(sysfs_workq_mutex);
441 static LIST_HEAD(sysfs_workq);
442 static void sysfs_schedule_callback_work(struct work_struct *work)
444 struct sysfs_schedule_callback_struct *ss = container_of(work,
445 struct sysfs_schedule_callback_struct, work);
447 (ss->func)(ss->data);
448 kobject_put(ss->kobj);
449 module_put(ss->owner);
450 mutex_lock(&sysfs_workq_mutex);
451 list_del(&ss->workq_list);
452 mutex_unlock(&sysfs_workq_mutex);
457 * sysfs_schedule_callback - helper to schedule a callback for a kobject
458 * @kobj: object we're acting for.
459 * @func: callback function to invoke later.
460 * @data: argument to pass to @func.
461 * @owner: module owning the callback code
463 * sysfs attribute methods must not unregister themselves or their parent
464 * kobject (which would amount to the same thing). Attempts to do so will
465 * deadlock, since unregistration is mutually exclusive with driver
468 * Instead methods can call this routine, which will attempt to allocate
469 * and schedule a workqueue request to call back @func with @data as its
470 * argument in the workqueue's process context. @kobj will be pinned
471 * until @func returns.
473 * Returns 0 if the request was submitted, -ENOMEM if storage could not
474 * be allocated, -ENODEV if a reference to @owner isn't available,
475 * -EAGAIN if a callback has already been scheduled for @kobj.
477 int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
478 void *data, struct module *owner)
480 struct sysfs_schedule_callback_struct *ss, *tmp;
482 if (!try_module_get(owner))
485 mutex_lock(&sysfs_workq_mutex);
486 list_for_each_entry_safe(ss, tmp, &sysfs_workq, workq_list)
487 if (ss->kobj == kobj) {
489 mutex_unlock(&sysfs_workq_mutex);
492 mutex_unlock(&sysfs_workq_mutex);
494 if (sysfs_workqueue == NULL) {
495 sysfs_workqueue = create_singlethread_workqueue("sysfsd");
496 if (sysfs_workqueue == NULL) {
502 ss = kmalloc(sizeof(*ss), GFP_KERNEL);
512 INIT_WORK(&ss->work, sysfs_schedule_callback_work);
513 INIT_LIST_HEAD(&ss->workq_list);
514 mutex_lock(&sysfs_workq_mutex);
515 list_add_tail(&ss->workq_list, &sysfs_workq);
516 mutex_unlock(&sysfs_workq_mutex);
517 queue_work(sysfs_workqueue, &ss->work);
520 EXPORT_SYMBOL_GPL(sysfs_schedule_callback);