Documentation: start documenting driver design patterns
[deliverable/linux.git] / fs / sysfs / file.c
CommitLineData
1da177e4 1/*
6d66f5cd
TH
2 * fs/sysfs/file.c - sysfs regular (text) file implementation
3 *
4 * Copyright (c) 2001-3 Patrick Mochel
5 * Copyright (c) 2007 SUSE Linux Products GmbH
6 * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
7 *
8 * This file is released under the GPLv2.
9 *
10 * Please see Documentation/filesystems/sysfs.txt for more information.
1da177e4
LT
11 */
12
13#include <linux/module.h>
1da177e4 14#include <linux/kobject.h>
815d2d50 15#include <linux/kallsyms.h>
c6f87733 16#include <linux/slab.h>
94bebf4d 17#include <linux/list.h>
52e8c209 18#include <linux/mutex.h>
13c589d5 19#include <linux/seq_file.h>
1da177e4
LT
20
21#include "sysfs.h"
414985ae 22#include "../kernfs/kernfs-internal.h"
f6acf8bb 23
375b611e
TH
24/*
25 * Determine ktype->sysfs_ops for the given sysfs_dirent. This function
26 * must be called while holding an active reference.
27 */
28static const struct sysfs_ops *sysfs_file_ops(struct sysfs_dirent *sd)
29{
7c6e2d36 30 struct kobject *kobj = sd->s_parent->priv;
375b611e 31
517e64f5 32 if (sd->s_flags & SYSFS_FLAG_LOCKDEP)
785a162d 33 lockdep_assert_held(sd);
375b611e
TH
34 return kobj->ktype ? kobj->ktype->sysfs_ops : NULL;
35}
36
13c589d5
TH
37/*
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.
1da177e4 41 */
c2b19daf 42static int sysfs_kf_seq_show(struct seq_file *sf, void *v)
1da177e4 43{
13c589d5 44 struct sysfs_open_file *of = sf->private;
7c6e2d36 45 struct kobject *kobj = of->sd->s_parent->priv;
c2b19daf 46 const struct sysfs_ops *ops = sysfs_file_ops(of->sd);
1da177e4 47 ssize_t count;
c2b19daf 48 char *buf;
1da177e4 49
13c589d5
TH
50 /* acquire buffer and ensure that it's >= PAGE_SIZE */
51 count = seq_get_buf(sf, &buf);
52 if (count < PAGE_SIZE) {
53 seq_commit(sf, -1);
54 return 0;
55 }
1da177e4 56
13c589d5 57 /*
c2b19daf
TH
58 * Invoke show(). Control may reach here via seq file lseek even
59 * if @ops->show() isn't implemented.
13c589d5 60 */
c2b19daf 61 if (ops->show) {
7c6e2d36 62 count = ops->show(kobj, of->sd->priv, buf);
c2b19daf
TH
63 if (count < 0)
64 return count;
65 }
0ab66088 66
8118a859
MX
67 /*
68 * The code works fine with PAGE_SIZE return but it's likely to
69 * indicate truncated result or overflow in normal use cases.
70 */
815d2d50
AM
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;
76 }
13c589d5
TH
77 seq_commit(sf, count);
78 return 0;
1da177e4
LT
79}
80
c2b19daf
TH
81static ssize_t sysfs_kf_bin_read(struct sysfs_open_file *of, char *buf,
82 size_t count, loff_t pos)
2f0c6b75 83{
7c6e2d36
TH
84 struct bin_attribute *battr = of->sd->priv;
85 struct kobject *kobj = of->sd->s_parent->priv;
c2b19daf 86 loff_t size = file_inode(of->file)->i_size;
2f0c6b75 87
c2b19daf 88 if (!count)
2f0c6b75
TH
89 return 0;
90
91 if (size) {
c2b19daf 92 if (pos > size)
2f0c6b75 93 return 0;
c2b19daf
TH
94 if (pos + count > size)
95 count = size - pos;
2f0c6b75
TH
96 }
97
c2b19daf
TH
98 if (!battr->read)
99 return -EIO;
100
101 return battr->read(of->file, kobj, battr, buf, pos, count);
102}
103
50b38ca0
TH
104/* kernfs write callback for regular sysfs files */
105static ssize_t sysfs_kf_write(struct sysfs_open_file *of, char *buf,
106 size_t count, loff_t pos)
1da177e4 107{
50b38ca0 108 const struct sysfs_ops *ops = sysfs_file_ops(of->sd);
7c6e2d36 109 struct kobject *kobj = of->sd->s_parent->priv;
0ab66088 110
50b38ca0
TH
111 if (!count)
112 return 0;
0ab66088 113
50b38ca0
TH
114 return ops->store(kobj, of->sd->priv, buf, count);
115}
f9b9a621 116
50b38ca0
TH
117/* kernfs write callback for bin sysfs files */
118static ssize_t sysfs_kf_bin_write(struct sysfs_open_file *of, char *buf,
119 size_t count, loff_t pos)
120{
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;
f9b9a621 124
50b38ca0
TH
125 if (size) {
126 if (size <= pos)
127 return 0;
128 count = min_t(ssize_t, count, size - pos);
f9b9a621 129 }
50b38ca0
TH
130 if (!count)
131 return 0;
0ab66088 132
50b38ca0
TH
133 if (!battr->write)
134 return -EIO;
1da177e4 135
50b38ca0 136 return battr->write(of->file, kobj, battr, buf, pos, count);
1da177e4
LT
137}
138
fdbffaa4
TH
139static int sysfs_kf_bin_mmap(struct sysfs_open_file *of,
140 struct vm_area_struct *vma)
141{
142 struct bin_attribute *battr = of->sd->priv;
143 struct kobject *kobj = of->sd->s_parent->priv;
144
fdbffaa4
TH
145 return battr->mmap(of->file, kobj, battr, vma);
146}
147
8c0e3998 148void sysfs_notify(struct kobject *k, const char *dir, const char *attr)
4508a7a7 149{
024f6471 150 struct sysfs_dirent *sd = k->sd, *tmp;
51225039
TH
151
152 if (sd && dir)
ccf73cf3 153 sd = kernfs_find_and_get(sd, dir);
024f6471 154 else
ccf73cf3 155 kernfs_get(sd);
024f6471
TH
156
157 if (sd && attr) {
ccf73cf3
TH
158 tmp = kernfs_find_and_get(sd, attr);
159 kernfs_put(sd);
024f6471
TH
160 sd = tmp;
161 }
51225039 162
024f6471
TH
163 if (sd) {
164 kernfs_notify(sd);
ccf73cf3 165 kernfs_put(sd);
024f6471 166 }
4508a7a7
N
167}
168EXPORT_SYMBOL_GPL(sysfs_notify);
169
f6acf8bb
TH
170static const struct kernfs_ops sysfs_file_kfops_empty = {
171};
172
173static const struct kernfs_ops sysfs_file_kfops_ro = {
174 .seq_show = sysfs_kf_seq_show,
175};
176
177static const struct kernfs_ops sysfs_file_kfops_wo = {
178 .write = sysfs_kf_write,
179};
180
181static const struct kernfs_ops sysfs_file_kfops_rw = {
182 .seq_show = sysfs_kf_seq_show,
183 .write = sysfs_kf_write,
184};
185
186static const struct kernfs_ops sysfs_bin_kfops_ro = {
187 .read = sysfs_kf_bin_read,
188};
189
190static const struct kernfs_ops sysfs_bin_kfops_wo = {
191 .write = sysfs_kf_bin_write,
192};
193
194static const struct kernfs_ops sysfs_bin_kfops_rw = {
195 .read = sysfs_kf_bin_read,
196 .write = sysfs_kf_bin_write,
9b2db6e1
TH
197};
198
199static const struct kernfs_ops sysfs_bin_kfops_mmap = {
200 .read = sysfs_kf_bin_read,
201 .write = sysfs_kf_bin_write,
f6acf8bb
TH
202 .mmap = sysfs_kf_bin_mmap,
203};
204
58292cbe 205int sysfs_add_file_mode_ns(struct sysfs_dirent *dir_sd,
a7dc66df 206 const struct attribute *attr, bool is_bin,
496f7394 207 umode_t mode, const void *ns)
1da177e4 208{
517e64f5 209 struct lock_class_key *key = NULL;
f6acf8bb 210 const struct kernfs_ops *ops;
a26cd722 211 struct sysfs_dirent *sd;
471bd7b7 212 loff_t size;
1da177e4 213
a7dc66df 214 if (!is_bin) {
f6acf8bb
TH
215 struct kobject *kobj = dir_sd->priv;
216 const struct sysfs_ops *sysfs_ops = kobj->ktype->sysfs_ops;
217
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",
221 kobject_name(kobj)))
222 return -EINVAL;
223
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;
230 else
231 ops = &sysfs_file_kfops_empty;
471bd7b7
TH
232
233 size = PAGE_SIZE;
f6acf8bb
TH
234 } else {
235 struct bin_attribute *battr = (void *)attr;
236
9b2db6e1
TH
237 if (battr->mmap)
238 ops = &sysfs_bin_kfops_mmap;
239 else if (battr->read && battr->write)
f6acf8bb
TH
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;
245 else
246 ops = &sysfs_file_kfops_empty;
471bd7b7
TH
247
248 size = battr->size;
f6acf8bb
TH
249 }
250
517e64f5
TH
251#ifdef CONFIG_DEBUG_LOCK_ALLOC
252 if (!attr->ignore_lockdep)
253 key = attr->key ?: (struct lock_class_key *)&attr->skey;
254#endif
255 sd = kernfs_create_file_ns_key(dir_sd, attr->name, mode, size,
256 ops, (void *)attr, ns, key);
496f7394
TH
257 if (IS_ERR(sd)) {
258 if (PTR_ERR(sd) == -EEXIST)
259 sysfs_warn_dup(dir_sd, attr->name);
260 return PTR_ERR(sd);
261 }
262 return 0;
263}
264
0f423895 265int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,
a7dc66df 266 bool is_bin)
0f423895 267{
a7dc66df 268 return sysfs_add_file_mode_ns(dir_sd, attr, is_bin, attr->mode, NULL);
0f423895
JB
269}
270
1da177e4 271/**
58292cbe
TH
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
1da177e4 276 */
58292cbe
TH
277int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
278 const void *ns)
1da177e4 279{
608e266a 280 BUG_ON(!kobj || !kobj->sd || !attr);
1da177e4 281
a7dc66df 282 return sysfs_add_file_mode_ns(kobj->sd, attr, false, attr->mode, ns);
1da177e4
LT
283
284}
58292cbe 285EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
1da177e4 286
1c205ae1
AK
287int sysfs_create_files(struct kobject *kobj, const struct attribute **ptr)
288{
289 int err = 0;
290 int i;
291
292 for (i = 0; ptr[i] && !err; i++)
293 err = sysfs_create_file(kobj, ptr[i]);
294 if (err)
295 while (--i >= 0)
296 sysfs_remove_file(kobj, ptr[i]);
297 return err;
298}
1b866757 299EXPORT_SYMBOL_GPL(sysfs_create_files);
1da177e4 300
dfa87c82
AS
301/**
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.
306 */
307int sysfs_add_file_to_group(struct kobject *kobj,
308 const struct attribute *attr, const char *group)
309{
608e266a 310 struct sysfs_dirent *dir_sd;
dfa87c82
AS
311 int error;
312
ccf73cf3
TH
313 if (group) {
314 dir_sd = kernfs_find_and_get(kobj->sd, group);
315 } else {
316 dir_sd = kobj->sd;
317 kernfs_get(dir_sd);
318 }
11f24fbd 319
608e266a
TH
320 if (!dir_sd)
321 return -ENOENT;
322
a7dc66df 323 error = sysfs_add_file(dir_sd, attr, false);
ccf73cf3 324 kernfs_put(dir_sd);
608e266a 325
dfa87c82
AS
326 return error;
327}
328EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
329
31e5abe9
KS
330/**
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.
335 *
336 */
49c19400 337int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
48176a97 338 umode_t mode)
31e5abe9 339{
06fc0d66 340 struct sysfs_dirent *sd;
bc062b1b 341 struct iattr newattrs;
51225039
TH
342 int rc;
343
ccf73cf3 344 sd = kernfs_find_and_get(kobj->sd, attr->name);
06fc0d66 345 if (!sd)
5d60418e 346 return -ENOENT;
f88123ea 347
06fc0d66 348 newattrs.ia_mode = (mode & S_IALLUGO) | (sd->s_mode & ~S_IALLUGO);
4c6974f5 349 newattrs.ia_valid = ATTR_MODE;
f88123ea 350
5d60418e
TH
351 rc = kernfs_setattr(sd, &newattrs);
352
ccf73cf3 353 kernfs_put(sd);
51225039 354 return rc;
31e5abe9
KS
355}
356EXPORT_SYMBOL_GPL(sysfs_chmod_file);
357
1da177e4 358/**
58292cbe
TH
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
1da177e4 363 *
58292cbe 364 * Hash the attribute name and namespace tag and kill the victim.
1da177e4 365 */
58292cbe
TH
366void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
367 const void *ns)
1da177e4 368{
58292cbe 369 struct sysfs_dirent *dir_sd = kobj->sd;
487505c2 370
879f40d1 371 kernfs_remove_by_name_ns(dir_sd, attr->name, ns);
1da177e4 372}
58292cbe 373EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
1da177e4 374
1b18dc2b 375void sysfs_remove_files(struct kobject *kobj, const struct attribute **ptr)
1c205ae1
AK
376{
377 int i;
378 for (i = 0; ptr[i]; i++)
379 sysfs_remove_file(kobj, ptr[i]);
380}
1b866757 381EXPORT_SYMBOL_GPL(sysfs_remove_files);
1da177e4 382
dfa87c82
AS
383/**
384 * sysfs_remove_file_from_group - remove an attribute file from a group.
385 * @kobj: object we're acting for.
386 * @attr: attribute descriptor.
387 * @group: group name.
388 */
389void sysfs_remove_file_from_group(struct kobject *kobj,
390 const struct attribute *attr, const char *group)
391{
608e266a 392 struct sysfs_dirent *dir_sd;
dfa87c82 393
ccf73cf3
TH
394 if (group) {
395 dir_sd = kernfs_find_and_get(kobj->sd, group);
396 } else {
397 dir_sd = kobj->sd;
398 kernfs_get(dir_sd);
399 }
400
608e266a 401 if (dir_sd) {
879f40d1 402 kernfs_remove_by_name(dir_sd, attr->name);
ccf73cf3 403 kernfs_put(dir_sd);
dfa87c82
AS
404 }
405}
406EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
407
3124eb16
TH
408/**
409 * sysfs_create_bin_file - create binary file for object.
410 * @kobj: object.
411 * @attr: attribute descriptor.
412 */
413int sysfs_create_bin_file(struct kobject *kobj,
414 const struct bin_attribute *attr)
415{
416 BUG_ON(!kobj || !kobj->sd || !attr);
417
a7dc66df 418 return sysfs_add_file(kobj->sd, &attr->attr, true);
3124eb16
TH
419}
420EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
421
422/**
423 * sysfs_remove_bin_file - remove binary file for object.
424 * @kobj: object.
425 * @attr: attribute descriptor.
426 */
427void sysfs_remove_bin_file(struct kobject *kobj,
428 const struct bin_attribute *attr)
429{
879f40d1 430 kernfs_remove_by_name(kobj->sd, attr->attr.name);
3124eb16
TH
431}
432EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);
433
d9a9cdfb 434struct sysfs_schedule_callback_struct {
66942064
AC
435 struct list_head workq_list;
436 struct kobject *kobj;
d9a9cdfb
AS
437 void (*func)(void *);
438 void *data;
523ded71 439 struct module *owner;
d9a9cdfb
AS
440 struct work_struct work;
441};
442
d110271e 443static struct workqueue_struct *sysfs_workqueue;
66942064
AC
444static DEFINE_MUTEX(sysfs_workq_mutex);
445static LIST_HEAD(sysfs_workq);
d9a9cdfb
AS
446static void sysfs_schedule_callback_work(struct work_struct *work)
447{
448 struct sysfs_schedule_callback_struct *ss = container_of(work,
449 struct sysfs_schedule_callback_struct, work);
450
451 (ss->func)(ss->data);
452 kobject_put(ss->kobj);
523ded71 453 module_put(ss->owner);
66942064
AC
454 mutex_lock(&sysfs_workq_mutex);
455 list_del(&ss->workq_list);
456 mutex_unlock(&sysfs_workq_mutex);
d9a9cdfb
AS
457 kfree(ss);
458}
459
460/**
461 * sysfs_schedule_callback - helper to schedule a callback for a kobject
462 * @kobj: object we're acting for.
463 * @func: callback function to invoke later.
464 * @data: argument to pass to @func.
523ded71 465 * @owner: module owning the callback code
d9a9cdfb
AS
466 *
467 * sysfs attribute methods must not unregister themselves or their parent
468 * kobject (which would amount to the same thing). Attempts to do so will
469 * deadlock, since unregistration is mutually exclusive with driver
470 * callbacks.
471 *
472 * Instead methods can call this routine, which will attempt to allocate
473 * and schedule a workqueue request to call back @func with @data as its
474 * argument in the workqueue's process context. @kobj will be pinned
475 * until @func returns.
476 *
477 * Returns 0 if the request was submitted, -ENOMEM if storage could not
66942064
AC
478 * be allocated, -ENODEV if a reference to @owner isn't available,
479 * -EAGAIN if a callback has already been scheduled for @kobj.
d9a9cdfb
AS
480 */
481int sysfs_schedule_callback(struct kobject *kobj, void (*func)(void *),
523ded71 482 void *data, struct module *owner)
d9a9cdfb 483{
66942064 484 struct sysfs_schedule_callback_struct *ss, *tmp;
d9a9cdfb 485
523ded71
AS
486 if (!try_module_get(owner))
487 return -ENODEV;
66942064
AC
488
489 mutex_lock(&sysfs_workq_mutex);
490 list_for_each_entry_safe(ss, tmp, &sysfs_workq, workq_list)
491 if (ss->kobj == kobj) {
d110271e 492 module_put(owner);
66942064
AC
493 mutex_unlock(&sysfs_workq_mutex);
494 return -EAGAIN;
495 }
496 mutex_unlock(&sysfs_workq_mutex);
497
d110271e 498 if (sysfs_workqueue == NULL) {
086a377e 499 sysfs_workqueue = create_singlethread_workqueue("sysfsd");
d110271e
AC
500 if (sysfs_workqueue == NULL) {
501 module_put(owner);
502 return -ENOMEM;
503 }
504 }
505
d9a9cdfb 506 ss = kmalloc(sizeof(*ss), GFP_KERNEL);
523ded71
AS
507 if (!ss) {
508 module_put(owner);
d9a9cdfb 509 return -ENOMEM;
523ded71 510 }
d9a9cdfb
AS
511 kobject_get(kobj);
512 ss->kobj = kobj;
513 ss->func = func;
514 ss->data = data;
523ded71 515 ss->owner = owner;
d9a9cdfb 516 INIT_WORK(&ss->work, sysfs_schedule_callback_work);
66942064
AC
517 INIT_LIST_HEAD(&ss->workq_list);
518 mutex_lock(&sysfs_workq_mutex);
519 list_add_tail(&ss->workq_list, &sysfs_workq);
520 mutex_unlock(&sysfs_workq_mutex);
d110271e 521 queue_work(sysfs_workqueue, &ss->work);
d9a9cdfb
AS
522 return 0;
523}
524EXPORT_SYMBOL_GPL(sysfs_schedule_callback);
This page took 0.838254 seconds and 5 git commands to generate.