Merge branch 'x86-efi-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[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 24/*
324a56e1 25 * Determine ktype->sysfs_ops for the given kernfs_node. This function
375b611e
TH
26 * must be called while holding an active reference.
27 */
324a56e1 28static const struct sysfs_ops *sysfs_file_ops(struct kernfs_node *kn)
375b611e 29{
adc5e8b5 30 struct kobject *kobj = kn->parent->priv;
375b611e 31
df23fc39 32 if (kn->flags & KERNFS_LOCKDEP)
324a56e1 33 lockdep_assert_held(kn);
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{
c525aadd 44 struct kernfs_open_file *of = sf->private;
adc5e8b5 45 struct kobject *kobj = of->kn->parent->priv;
324a56e1 46 const struct sysfs_ops *ops = sysfs_file_ops(of->kn);
1da177e4 47 ssize_t count;
c2b19daf 48 char *buf;
1da177e4 49
f5c16f29 50 /* acquire buffer and ensure that it's >= PAGE_SIZE and clear */
13c589d5
TH
51 count = seq_get_buf(sf, &buf);
52 if (count < PAGE_SIZE) {
53 seq_commit(sf, -1);
54 return 0;
55 }
f5c16f29 56 memset(buf, 0, PAGE_SIZE);
1da177e4 57
13c589d5 58 /*
c2b19daf
TH
59 * Invoke show(). Control may reach here via seq file lseek even
60 * if @ops->show() isn't implemented.
13c589d5 61 */
c2b19daf 62 if (ops->show) {
324a56e1 63 count = ops->show(kobj, of->kn->priv, buf);
c2b19daf
TH
64 if (count < 0)
65 return count;
66 }
0ab66088 67
8118a859
MX
68 /*
69 * The code works fine with PAGE_SIZE return but it's likely to
70 * indicate truncated result or overflow in normal use cases.
71 */
815d2d50
AM
72 if (count >= (ssize_t)PAGE_SIZE) {
73 print_symbol("fill_read_buffer: %s returned bad count\n",
74 (unsigned long)ops->show);
75 /* Try to struggle along */
76 count = PAGE_SIZE - 1;
77 }
13c589d5
TH
78 seq_commit(sf, count);
79 return 0;
1da177e4
LT
80}
81
c525aadd 82static ssize_t sysfs_kf_bin_read(struct kernfs_open_file *of, char *buf,
c2b19daf 83 size_t count, loff_t pos)
2f0c6b75 84{
324a56e1 85 struct bin_attribute *battr = of->kn->priv;
adc5e8b5 86 struct kobject *kobj = of->kn->parent->priv;
c2b19daf 87 loff_t size = file_inode(of->file)->i_size;
2f0c6b75 88
c2b19daf 89 if (!count)
2f0c6b75
TH
90 return 0;
91
92 if (size) {
c2b19daf 93 if (pos > size)
2f0c6b75 94 return 0;
c2b19daf
TH
95 if (pos + count > size)
96 count = size - pos;
2f0c6b75
TH
97 }
98
c2b19daf
TH
99 if (!battr->read)
100 return -EIO;
101
102 return battr->read(of->file, kobj, battr, buf, pos, count);
103}
104
50b38ca0 105/* kernfs write callback for regular sysfs files */
c525aadd 106static ssize_t sysfs_kf_write(struct kernfs_open_file *of, char *buf,
50b38ca0 107 size_t count, loff_t pos)
1da177e4 108{
324a56e1 109 const struct sysfs_ops *ops = sysfs_file_ops(of->kn);
adc5e8b5 110 struct kobject *kobj = of->kn->parent->priv;
0ab66088 111
50b38ca0
TH
112 if (!count)
113 return 0;
0ab66088 114
324a56e1 115 return ops->store(kobj, of->kn->priv, buf, count);
50b38ca0 116}
f9b9a621 117
50b38ca0 118/* kernfs write callback for bin sysfs files */
c525aadd 119static ssize_t sysfs_kf_bin_write(struct kernfs_open_file *of, char *buf,
50b38ca0
TH
120 size_t count, loff_t pos)
121{
324a56e1 122 struct bin_attribute *battr = of->kn->priv;
adc5e8b5 123 struct kobject *kobj = of->kn->parent->priv;
50b38ca0 124 loff_t size = file_inode(of->file)->i_size;
f9b9a621 125
50b38ca0
TH
126 if (size) {
127 if (size <= pos)
128 return 0;
129 count = min_t(ssize_t, count, size - pos);
f9b9a621 130 }
50b38ca0
TH
131 if (!count)
132 return 0;
0ab66088 133
50b38ca0
TH
134 if (!battr->write)
135 return -EIO;
1da177e4 136
50b38ca0 137 return battr->write(of->file, kobj, battr, buf, pos, count);
1da177e4
LT
138}
139
c525aadd 140static int sysfs_kf_bin_mmap(struct kernfs_open_file *of,
fdbffaa4
TH
141 struct vm_area_struct *vma)
142{
324a56e1 143 struct bin_attribute *battr = of->kn->priv;
adc5e8b5 144 struct kobject *kobj = of->kn->parent->priv;
fdbffaa4 145
fdbffaa4
TH
146 return battr->mmap(of->file, kobj, battr, vma);
147}
148
324a56e1 149void sysfs_notify(struct kobject *kobj, const char *dir, const char *attr)
4508a7a7 150{
324a56e1 151 struct kernfs_node *kn = kobj->sd, *tmp;
51225039 152
324a56e1
TH
153 if (kn && dir)
154 kn = kernfs_find_and_get(kn, dir);
024f6471 155 else
324a56e1 156 kernfs_get(kn);
024f6471 157
324a56e1
TH
158 if (kn && attr) {
159 tmp = kernfs_find_and_get(kn, attr);
160 kernfs_put(kn);
161 kn = tmp;
024f6471 162 }
51225039 163
324a56e1
TH
164 if (kn) {
165 kernfs_notify(kn);
166 kernfs_put(kn);
024f6471 167 }
4508a7a7
N
168}
169EXPORT_SYMBOL_GPL(sysfs_notify);
170
f6acf8bb
TH
171static const struct kernfs_ops sysfs_file_kfops_empty = {
172};
173
174static const struct kernfs_ops sysfs_file_kfops_ro = {
175 .seq_show = sysfs_kf_seq_show,
176};
177
178static const struct kernfs_ops sysfs_file_kfops_wo = {
179 .write = sysfs_kf_write,
180};
181
182static const struct kernfs_ops sysfs_file_kfops_rw = {
183 .seq_show = sysfs_kf_seq_show,
184 .write = sysfs_kf_write,
185};
186
187static const struct kernfs_ops sysfs_bin_kfops_ro = {
188 .read = sysfs_kf_bin_read,
189};
190
191static const struct kernfs_ops sysfs_bin_kfops_wo = {
192 .write = sysfs_kf_bin_write,
193};
194
195static const struct kernfs_ops sysfs_bin_kfops_rw = {
196 .read = sysfs_kf_bin_read,
197 .write = sysfs_kf_bin_write,
9b2db6e1
TH
198};
199
200static const struct kernfs_ops sysfs_bin_kfops_mmap = {
201 .read = sysfs_kf_bin_read,
202 .write = sysfs_kf_bin_write,
f6acf8bb
TH
203 .mmap = sysfs_kf_bin_mmap,
204};
205
324a56e1 206int sysfs_add_file_mode_ns(struct kernfs_node *parent,
a7dc66df 207 const struct attribute *attr, bool is_bin,
496f7394 208 umode_t mode, const void *ns)
1da177e4 209{
517e64f5 210 struct lock_class_key *key = NULL;
f6acf8bb 211 const struct kernfs_ops *ops;
324a56e1 212 struct kernfs_node *kn;
471bd7b7 213 loff_t size;
1da177e4 214
a7dc66df 215 if (!is_bin) {
324a56e1 216 struct kobject *kobj = parent->priv;
f6acf8bb
TH
217 const struct sysfs_ops *sysfs_ops = kobj->ktype->sysfs_ops;
218
219 /* every kobject with an attribute needs a ktype assigned */
220 if (WARN(!sysfs_ops, KERN_ERR
221 "missing sysfs attribute operations for kobject: %s\n",
222 kobject_name(kobj)))
223 return -EINVAL;
224
225 if (sysfs_ops->show && sysfs_ops->store)
226 ops = &sysfs_file_kfops_rw;
227 else if (sysfs_ops->show)
228 ops = &sysfs_file_kfops_ro;
229 else if (sysfs_ops->store)
230 ops = &sysfs_file_kfops_wo;
231 else
232 ops = &sysfs_file_kfops_empty;
471bd7b7
TH
233
234 size = PAGE_SIZE;
f6acf8bb
TH
235 } else {
236 struct bin_attribute *battr = (void *)attr;
237
9b2db6e1
TH
238 if (battr->mmap)
239 ops = &sysfs_bin_kfops_mmap;
240 else if (battr->read && battr->write)
f6acf8bb
TH
241 ops = &sysfs_bin_kfops_rw;
242 else if (battr->read)
243 ops = &sysfs_bin_kfops_ro;
244 else if (battr->write)
245 ops = &sysfs_bin_kfops_wo;
246 else
247 ops = &sysfs_file_kfops_empty;
471bd7b7
TH
248
249 size = battr->size;
f6acf8bb
TH
250 }
251
517e64f5
TH
252#ifdef CONFIG_DEBUG_LOCK_ALLOC
253 if (!attr->ignore_lockdep)
254 key = attr->key ?: (struct lock_class_key *)&attr->skey;
255#endif
2063d608
TH
256 kn = __kernfs_create_file(parent, attr->name, mode, size, ops,
257 (void *)attr, ns, true, key);
324a56e1
TH
258 if (IS_ERR(kn)) {
259 if (PTR_ERR(kn) == -EEXIST)
260 sysfs_warn_dup(parent, attr->name);
261 return PTR_ERR(kn);
496f7394
TH
262 }
263 return 0;
264}
265
324a56e1 266int sysfs_add_file(struct kernfs_node *parent, const struct attribute *attr,
a7dc66df 267 bool is_bin)
0f423895 268{
324a56e1 269 return sysfs_add_file_mode_ns(parent, attr, is_bin, attr->mode, NULL);
0f423895
JB
270}
271
1da177e4 272/**
58292cbe
TH
273 * sysfs_create_file_ns - create an attribute file for an object with custom ns
274 * @kobj: object we're creating for
275 * @attr: attribute descriptor
276 * @ns: namespace the new file should belong to
1da177e4 277 */
58292cbe
TH
278int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
279 const void *ns)
1da177e4 280{
608e266a 281 BUG_ON(!kobj || !kobj->sd || !attr);
1da177e4 282
a7dc66df 283 return sysfs_add_file_mode_ns(kobj->sd, attr, false, attr->mode, ns);
1da177e4
LT
284
285}
58292cbe 286EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
1da177e4 287
1c205ae1
AK
288int sysfs_create_files(struct kobject *kobj, const struct attribute **ptr)
289{
290 int err = 0;
291 int i;
292
293 for (i = 0; ptr[i] && !err; i++)
294 err = sysfs_create_file(kobj, ptr[i]);
295 if (err)
296 while (--i >= 0)
297 sysfs_remove_file(kobj, ptr[i]);
298 return err;
299}
1b866757 300EXPORT_SYMBOL_GPL(sysfs_create_files);
1da177e4 301
dfa87c82
AS
302/**
303 * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
304 * @kobj: object we're acting for.
305 * @attr: attribute descriptor.
306 * @group: group name.
307 */
308int sysfs_add_file_to_group(struct kobject *kobj,
309 const struct attribute *attr, const char *group)
310{
324a56e1 311 struct kernfs_node *parent;
dfa87c82
AS
312 int error;
313
ccf73cf3 314 if (group) {
324a56e1 315 parent = kernfs_find_and_get(kobj->sd, group);
ccf73cf3 316 } else {
324a56e1
TH
317 parent = kobj->sd;
318 kernfs_get(parent);
ccf73cf3 319 }
11f24fbd 320
324a56e1 321 if (!parent)
608e266a
TH
322 return -ENOENT;
323
324a56e1
TH
324 error = sysfs_add_file(parent, attr, false);
325 kernfs_put(parent);
608e266a 326
dfa87c82
AS
327 return error;
328}
329EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
330
31e5abe9
KS
331/**
332 * sysfs_chmod_file - update the modified mode value on an object attribute.
333 * @kobj: object we're acting for.
334 * @attr: attribute descriptor.
335 * @mode: file permissions.
336 *
337 */
49c19400 338int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
48176a97 339 umode_t mode)
31e5abe9 340{
324a56e1 341 struct kernfs_node *kn;
bc062b1b 342 struct iattr newattrs;
51225039
TH
343 int rc;
344
324a56e1
TH
345 kn = kernfs_find_and_get(kobj->sd, attr->name);
346 if (!kn)
5d60418e 347 return -ENOENT;
f88123ea 348
adc5e8b5 349 newattrs.ia_mode = (mode & S_IALLUGO) | (kn->mode & ~S_IALLUGO);
4c6974f5 350 newattrs.ia_valid = ATTR_MODE;
f88123ea 351
324a56e1 352 rc = kernfs_setattr(kn, &newattrs);
5d60418e 353
324a56e1 354 kernfs_put(kn);
51225039 355 return rc;
31e5abe9
KS
356}
357EXPORT_SYMBOL_GPL(sysfs_chmod_file);
358
1da177e4 359/**
58292cbe
TH
360 * sysfs_remove_file_ns - remove an object attribute with a custom ns tag
361 * @kobj: object we're acting for
362 * @attr: attribute descriptor
363 * @ns: namespace tag of the file to remove
1da177e4 364 *
58292cbe 365 * Hash the attribute name and namespace tag and kill the victim.
1da177e4 366 */
58292cbe
TH
367void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
368 const void *ns)
1da177e4 369{
324a56e1 370 struct kernfs_node *parent = kobj->sd;
487505c2 371
324a56e1 372 kernfs_remove_by_name_ns(parent, attr->name, ns);
1da177e4 373}
58292cbe 374EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
1da177e4 375
6b0afc2a
TH
376/**
377 * sysfs_remove_file_self - remove an object attribute from its own method
378 * @kobj: object we're acting for
379 * @attr: attribute descriptor
380 *
381 * See kernfs_remove_self() for details.
382 */
383bool sysfs_remove_file_self(struct kobject *kobj, const struct attribute *attr)
384{
385 struct kernfs_node *parent = kobj->sd;
386 struct kernfs_node *kn;
387 bool ret;
388
389 kn = kernfs_find_and_get(parent, attr->name);
390 if (WARN_ON_ONCE(!kn))
391 return false;
392
393 ret = kernfs_remove_self(kn);
394
395 kernfs_put(kn);
396 return ret;
397}
398
1b18dc2b 399void sysfs_remove_files(struct kobject *kobj, const struct attribute **ptr)
1c205ae1
AK
400{
401 int i;
402 for (i = 0; ptr[i]; i++)
403 sysfs_remove_file(kobj, ptr[i]);
404}
1b866757 405EXPORT_SYMBOL_GPL(sysfs_remove_files);
1da177e4 406
dfa87c82
AS
407/**
408 * sysfs_remove_file_from_group - remove an attribute file from a group.
409 * @kobj: object we're acting for.
410 * @attr: attribute descriptor.
411 * @group: group name.
412 */
413void sysfs_remove_file_from_group(struct kobject *kobj,
414 const struct attribute *attr, const char *group)
415{
324a56e1 416 struct kernfs_node *parent;
dfa87c82 417
ccf73cf3 418 if (group) {
324a56e1 419 parent = kernfs_find_and_get(kobj->sd, group);
ccf73cf3 420 } else {
324a56e1
TH
421 parent = kobj->sd;
422 kernfs_get(parent);
ccf73cf3
TH
423 }
424
324a56e1
TH
425 if (parent) {
426 kernfs_remove_by_name(parent, attr->name);
427 kernfs_put(parent);
dfa87c82
AS
428 }
429}
430EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
431
3124eb16
TH
432/**
433 * sysfs_create_bin_file - create binary file for object.
434 * @kobj: object.
435 * @attr: attribute descriptor.
436 */
437int sysfs_create_bin_file(struct kobject *kobj,
438 const struct bin_attribute *attr)
439{
440 BUG_ON(!kobj || !kobj->sd || !attr);
441
a7dc66df 442 return sysfs_add_file(kobj->sd, &attr->attr, true);
3124eb16
TH
443}
444EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
445
446/**
447 * sysfs_remove_bin_file - remove binary file for object.
448 * @kobj: object.
449 * @attr: attribute descriptor.
450 */
451void sysfs_remove_bin_file(struct kobject *kobj,
452 const struct bin_attribute *attr)
453{
879f40d1 454 kernfs_remove_by_name(kobj->sd, attr->attr.name);
3124eb16
TH
455}
456EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);
This page took 0.804349 seconds and 5 git commands to generate.