sysfs/kernfs: allow attributes to request write buffer be pre-allocated.
[firefly-linux-kernel-4.4.55.git] / fs / sysfs / file.c
1 /*
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.
11  */
12
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>
20
21 #include "sysfs.h"
22 #include "../kernfs/kernfs-internal.h"
23
24 /*
25  * Determine ktype->sysfs_ops for the given kernfs_node.  This function
26  * must be called while holding an active reference.
27  */
28 static const struct sysfs_ops *sysfs_file_ops(struct kernfs_node *kn)
29 {
30         struct kobject *kobj = kn->parent->priv;
31
32         if (kn->flags & KERNFS_LOCKDEP)
33                 lockdep_assert_held(kn);
34         return kobj->ktype ? kobj->ktype->sysfs_ops : NULL;
35 }
36
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.
41  */
42 static int sysfs_kf_seq_show(struct seq_file *sf, void *v)
43 {
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);
47         ssize_t count;
48         char *buf;
49
50         /* acquire buffer and ensure that it's >= PAGE_SIZE and clear */
51         count = seq_get_buf(sf, &buf);
52         if (count < PAGE_SIZE) {
53                 seq_commit(sf, -1);
54                 return 0;
55         }
56         memset(buf, 0, PAGE_SIZE);
57
58         /*
59          * Invoke show().  Control may reach here via seq file lseek even
60          * if @ops->show() isn't implemented.
61          */
62         if (ops->show) {
63                 count = ops->show(kobj, of->kn->priv, buf);
64                 if (count < 0)
65                         return count;
66         }
67
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          */
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         }
78         seq_commit(sf, count);
79         return 0;
80 }
81
82 static ssize_t sysfs_kf_bin_read(struct kernfs_open_file *of, char *buf,
83                                  size_t count, loff_t pos)
84 {
85         struct bin_attribute *battr = of->kn->priv;
86         struct kobject *kobj = of->kn->parent->priv;
87         loff_t size = file_inode(of->file)->i_size;
88
89         if (!count)
90                 return 0;
91
92         if (size) {
93                 if (pos > size)
94                         return 0;
95                 if (pos + count > size)
96                         count = size - pos;
97         }
98
99         if (!battr->read)
100                 return -EIO;
101
102         return battr->read(of->file, kobj, battr, buf, pos, count);
103 }
104
105 /* kernfs write callback for regular sysfs files */
106 static ssize_t sysfs_kf_write(struct kernfs_open_file *of, char *buf,
107                               size_t count, loff_t pos)
108 {
109         const struct sysfs_ops *ops = sysfs_file_ops(of->kn);
110         struct kobject *kobj = of->kn->parent->priv;
111
112         if (!count)
113                 return 0;
114
115         return ops->store(kobj, of->kn->priv, buf, count);
116 }
117
118 /* kernfs write callback for bin sysfs files */
119 static ssize_t sysfs_kf_bin_write(struct kernfs_open_file *of, char *buf,
120                                   size_t count, loff_t pos)
121 {
122         struct bin_attribute *battr = of->kn->priv;
123         struct kobject *kobj = of->kn->parent->priv;
124         loff_t size = file_inode(of->file)->i_size;
125
126         if (size) {
127                 if (size <= pos)
128                         return -EFBIG;
129                 count = min_t(ssize_t, count, size - pos);
130         }
131         if (!count)
132                 return 0;
133
134         if (!battr->write)
135                 return -EIO;
136
137         return battr->write(of->file, kobj, battr, buf, pos, count);
138 }
139
140 static int sysfs_kf_bin_mmap(struct kernfs_open_file *of,
141                              struct vm_area_struct *vma)
142 {
143         struct bin_attribute *battr = of->kn->priv;
144         struct kobject *kobj = of->kn->parent->priv;
145
146         return battr->mmap(of->file, kobj, battr, vma);
147 }
148
149 void sysfs_notify(struct kobject *kobj, const char *dir, const char *attr)
150 {
151         struct kernfs_node *kn = kobj->sd, *tmp;
152
153         if (kn && dir)
154                 kn = kernfs_find_and_get(kn, dir);
155         else
156                 kernfs_get(kn);
157
158         if (kn && attr) {
159                 tmp = kernfs_find_and_get(kn, attr);
160                 kernfs_put(kn);
161                 kn = tmp;
162         }
163
164         if (kn) {
165                 kernfs_notify(kn);
166                 kernfs_put(kn);
167         }
168 }
169 EXPORT_SYMBOL_GPL(sysfs_notify);
170
171 static const struct kernfs_ops sysfs_file_kfops_empty = {
172 };
173
174 static const struct kernfs_ops sysfs_file_kfops_ro = {
175         .seq_show       = sysfs_kf_seq_show,
176 };
177
178 static const struct kernfs_ops sysfs_file_kfops_wo = {
179         .write          = sysfs_kf_write,
180 };
181
182 static const struct kernfs_ops sysfs_file_kfops_rw = {
183         .seq_show       = sysfs_kf_seq_show,
184         .write          = sysfs_kf_write,
185 };
186
187 static const struct kernfs_ops sysfs_prealloc_kfops_wo = {
188         .write          = sysfs_kf_write,
189         .prealloc       = true,
190 };
191
192 static const struct kernfs_ops sysfs_prealloc_kfops_rw = {
193         .seq_show       = sysfs_kf_seq_show,
194         .write          = sysfs_kf_write,
195         .prealloc       = true,
196 };
197
198 static const struct kernfs_ops sysfs_bin_kfops_ro = {
199         .read           = sysfs_kf_bin_read,
200 };
201
202 static const struct kernfs_ops sysfs_bin_kfops_wo = {
203         .write          = sysfs_kf_bin_write,
204 };
205
206 static const struct kernfs_ops sysfs_bin_kfops_rw = {
207         .read           = sysfs_kf_bin_read,
208         .write          = sysfs_kf_bin_write,
209 };
210
211 static const struct kernfs_ops sysfs_bin_kfops_mmap = {
212         .read           = sysfs_kf_bin_read,
213         .write          = sysfs_kf_bin_write,
214         .mmap           = sysfs_kf_bin_mmap,
215 };
216
217 int sysfs_add_file_mode_ns(struct kernfs_node *parent,
218                            const struct attribute *attr, bool is_bin,
219                            umode_t mode, const void *ns)
220 {
221         struct lock_class_key *key = NULL;
222         const struct kernfs_ops *ops;
223         struct kernfs_node *kn;
224         loff_t size;
225
226         if (!is_bin) {
227                 struct kobject *kobj = parent->priv;
228                 const struct sysfs_ops *sysfs_ops = kobj->ktype->sysfs_ops;
229
230                 /* every kobject with an attribute needs a ktype assigned */
231                 if (WARN(!sysfs_ops, KERN_ERR
232                          "missing sysfs attribute operations for kobject: %s\n",
233                          kobject_name(kobj)))
234                         return -EINVAL;
235
236                 if (sysfs_ops->show && sysfs_ops->store) {
237                         if (mode & SYSFS_PREALLOC)
238                                 ops = &sysfs_prealloc_kfops_rw;
239                         else
240                                 ops = &sysfs_file_kfops_rw;
241                 } else if (sysfs_ops->show)
242                         ops = &sysfs_file_kfops_ro;
243                 else if (sysfs_ops->store) {
244                         if (mode & SYSFS_PREALLOC)
245                                 ops = &sysfs_prealloc_kfops_wo;
246                         else
247                                 ops = &sysfs_file_kfops_wo;
248                 } else
249                         ops = &sysfs_file_kfops_empty;
250
251                 size = PAGE_SIZE;
252         } else {
253                 struct bin_attribute *battr = (void *)attr;
254
255                 if (battr->mmap)
256                         ops = &sysfs_bin_kfops_mmap;
257                 else if (battr->read && battr->write)
258                         ops = &sysfs_bin_kfops_rw;
259                 else if (battr->read)
260                         ops = &sysfs_bin_kfops_ro;
261                 else if (battr->write)
262                         ops = &sysfs_bin_kfops_wo;
263                 else
264                         ops = &sysfs_file_kfops_empty;
265
266                 size = battr->size;
267         }
268
269 #ifdef CONFIG_DEBUG_LOCK_ALLOC
270         if (!attr->ignore_lockdep)
271                 key = attr->key ?: (struct lock_class_key *)&attr->skey;
272 #endif
273         kn = __kernfs_create_file(parent, attr->name, mode & 0777, size, ops,
274                                   (void *)attr, ns, true, key);
275         if (IS_ERR(kn)) {
276                 if (PTR_ERR(kn) == -EEXIST)
277                         sysfs_warn_dup(parent, attr->name);
278                 return PTR_ERR(kn);
279         }
280         return 0;
281 }
282
283 int sysfs_add_file(struct kernfs_node *parent, const struct attribute *attr,
284                    bool is_bin)
285 {
286         return sysfs_add_file_mode_ns(parent, attr, is_bin, attr->mode, NULL);
287 }
288
289 /**
290  * sysfs_create_file_ns - create an attribute file for an object with custom ns
291  * @kobj: object we're creating for
292  * @attr: attribute descriptor
293  * @ns: namespace the new file should belong to
294  */
295 int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr,
296                          const void *ns)
297 {
298         BUG_ON(!kobj || !kobj->sd || !attr);
299
300         return sysfs_add_file_mode_ns(kobj->sd, attr, false, attr->mode, ns);
301
302 }
303 EXPORT_SYMBOL_GPL(sysfs_create_file_ns);
304
305 int sysfs_create_files(struct kobject *kobj, const struct attribute **ptr)
306 {
307         int err = 0;
308         int i;
309
310         for (i = 0; ptr[i] && !err; i++)
311                 err = sysfs_create_file(kobj, ptr[i]);
312         if (err)
313                 while (--i >= 0)
314                         sysfs_remove_file(kobj, ptr[i]);
315         return err;
316 }
317 EXPORT_SYMBOL_GPL(sysfs_create_files);
318
319 /**
320  * sysfs_add_file_to_group - add an attribute file to a pre-existing group.
321  * @kobj: object we're acting for.
322  * @attr: attribute descriptor.
323  * @group: group name.
324  */
325 int sysfs_add_file_to_group(struct kobject *kobj,
326                 const struct attribute *attr, const char *group)
327 {
328         struct kernfs_node *parent;
329         int error;
330
331         if (group) {
332                 parent = kernfs_find_and_get(kobj->sd, group);
333         } else {
334                 parent = kobj->sd;
335                 kernfs_get(parent);
336         }
337
338         if (!parent)
339                 return -ENOENT;
340
341         error = sysfs_add_file(parent, attr, false);
342         kernfs_put(parent);
343
344         return error;
345 }
346 EXPORT_SYMBOL_GPL(sysfs_add_file_to_group);
347
348 /**
349  * sysfs_chmod_file - update the modified mode value on an object attribute.
350  * @kobj: object we're acting for.
351  * @attr: attribute descriptor.
352  * @mode: file permissions.
353  *
354  */
355 int sysfs_chmod_file(struct kobject *kobj, const struct attribute *attr,
356                      umode_t mode)
357 {
358         struct kernfs_node *kn;
359         struct iattr newattrs;
360         int rc;
361
362         kn = kernfs_find_and_get(kobj->sd, attr->name);
363         if (!kn)
364                 return -ENOENT;
365
366         newattrs.ia_mode = (mode & S_IALLUGO) | (kn->mode & ~S_IALLUGO);
367         newattrs.ia_valid = ATTR_MODE;
368
369         rc = kernfs_setattr(kn, &newattrs);
370
371         kernfs_put(kn);
372         return rc;
373 }
374 EXPORT_SYMBOL_GPL(sysfs_chmod_file);
375
376 /**
377  * sysfs_remove_file_ns - remove an object attribute with a custom ns tag
378  * @kobj: object we're acting for
379  * @attr: attribute descriptor
380  * @ns: namespace tag of the file to remove
381  *
382  * Hash the attribute name and namespace tag and kill the victim.
383  */
384 void sysfs_remove_file_ns(struct kobject *kobj, const struct attribute *attr,
385                           const void *ns)
386 {
387         struct kernfs_node *parent = kobj->sd;
388
389         kernfs_remove_by_name_ns(parent, attr->name, ns);
390 }
391 EXPORT_SYMBOL_GPL(sysfs_remove_file_ns);
392
393 /**
394  * sysfs_remove_file_self - remove an object attribute from its own method
395  * @kobj: object we're acting for
396  * @attr: attribute descriptor
397  *
398  * See kernfs_remove_self() for details.
399  */
400 bool sysfs_remove_file_self(struct kobject *kobj, const struct attribute *attr)
401 {
402         struct kernfs_node *parent = kobj->sd;
403         struct kernfs_node *kn;
404         bool ret;
405
406         kn = kernfs_find_and_get(parent, attr->name);
407         if (WARN_ON_ONCE(!kn))
408                 return false;
409
410         ret = kernfs_remove_self(kn);
411
412         kernfs_put(kn);
413         return ret;
414 }
415
416 void sysfs_remove_files(struct kobject *kobj, const struct attribute **ptr)
417 {
418         int i;
419         for (i = 0; ptr[i]; i++)
420                 sysfs_remove_file(kobj, ptr[i]);
421 }
422 EXPORT_SYMBOL_GPL(sysfs_remove_files);
423
424 /**
425  * sysfs_remove_file_from_group - remove an attribute file from a group.
426  * @kobj: object we're acting for.
427  * @attr: attribute descriptor.
428  * @group: group name.
429  */
430 void sysfs_remove_file_from_group(struct kobject *kobj,
431                 const struct attribute *attr, const char *group)
432 {
433         struct kernfs_node *parent;
434
435         if (group) {
436                 parent = kernfs_find_and_get(kobj->sd, group);
437         } else {
438                 parent = kobj->sd;
439                 kernfs_get(parent);
440         }
441
442         if (parent) {
443                 kernfs_remove_by_name(parent, attr->name);
444                 kernfs_put(parent);
445         }
446 }
447 EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group);
448
449 /**
450  *      sysfs_create_bin_file - create binary file for object.
451  *      @kobj:  object.
452  *      @attr:  attribute descriptor.
453  */
454 int sysfs_create_bin_file(struct kobject *kobj,
455                           const struct bin_attribute *attr)
456 {
457         BUG_ON(!kobj || !kobj->sd || !attr);
458
459         return sysfs_add_file(kobj->sd, &attr->attr, true);
460 }
461 EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
462
463 /**
464  *      sysfs_remove_bin_file - remove binary file for object.
465  *      @kobj:  object.
466  *      @attr:  attribute descriptor.
467  */
468 void sysfs_remove_bin_file(struct kobject *kobj,
469                            const struct bin_attribute *attr)
470 {
471         kernfs_remove_by_name(kobj->sd, attr->attr.name);
472 }
473 EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);