GNU Linux-libre 6.1.86-gnu
[releases.git] / fs / debugfs / file.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  file.c - part of debugfs, a tiny little debug file system
4  *
5  *  Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
6  *  Copyright (C) 2004 IBM Inc.
7  *
8  *  debugfs is for people to use instead of /proc or /sys.
9  *  See Documentation/filesystems/ for more details.
10  */
11
12 #include <linux/module.h>
13 #include <linux/fs.h>
14 #include <linux/seq_file.h>
15 #include <linux/pagemap.h>
16 #include <linux/debugfs.h>
17 #include <linux/io.h>
18 #include <linux/slab.h>
19 #include <linux/atomic.h>
20 #include <linux/device.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/poll.h>
23 #include <linux/security.h>
24
25 #include "internal.h"
26
27 struct poll_table_struct;
28
29 static ssize_t default_read_file(struct file *file, char __user *buf,
30                                  size_t count, loff_t *ppos)
31 {
32         return 0;
33 }
34
35 static ssize_t default_write_file(struct file *file, const char __user *buf,
36                                    size_t count, loff_t *ppos)
37 {
38         return count;
39 }
40
41 const struct file_operations debugfs_noop_file_operations = {
42         .read =         default_read_file,
43         .write =        default_write_file,
44         .open =         simple_open,
45         .llseek =       noop_llseek,
46 };
47
48 #define F_DENTRY(filp) ((filp)->f_path.dentry)
49
50 const struct file_operations *debugfs_real_fops(const struct file *filp)
51 {
52         struct debugfs_fsdata *fsd = F_DENTRY(filp)->d_fsdata;
53
54         if ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT) {
55                 /*
56                  * Urgh, we've been called w/o a protecting
57                  * debugfs_file_get().
58                  */
59                 WARN_ON(1);
60                 return NULL;
61         }
62
63         return fsd->real_fops;
64 }
65 EXPORT_SYMBOL_GPL(debugfs_real_fops);
66
67 /**
68  * debugfs_file_get - mark the beginning of file data access
69  * @dentry: the dentry object whose data is being accessed.
70  *
71  * Up to a matching call to debugfs_file_put(), any successive call
72  * into the file removing functions debugfs_remove() and
73  * debugfs_remove_recursive() will block. Since associated private
74  * file data may only get freed after a successful return of any of
75  * the removal functions, you may safely access it after a successful
76  * call to debugfs_file_get() without worrying about lifetime issues.
77  *
78  * If -%EIO is returned, the file has already been removed and thus,
79  * it is not safe to access any of its data. If, on the other hand,
80  * it is allowed to access the file data, zero is returned.
81  */
82 int debugfs_file_get(struct dentry *dentry)
83 {
84         struct debugfs_fsdata *fsd;
85         void *d_fsd;
86
87         /*
88          * This could only happen if some debugfs user erroneously calls
89          * debugfs_file_get() on a dentry that isn't even a file, let
90          * them know about it.
91          */
92         if (WARN_ON(!d_is_reg(dentry)))
93                 return -EINVAL;
94
95         d_fsd = READ_ONCE(dentry->d_fsdata);
96         if (!((unsigned long)d_fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT)) {
97                 fsd = d_fsd;
98         } else {
99                 fsd = kmalloc(sizeof(*fsd), GFP_KERNEL);
100                 if (!fsd)
101                         return -ENOMEM;
102
103                 fsd->real_fops = (void *)((unsigned long)d_fsd &
104                                         ~DEBUGFS_FSDATA_IS_REAL_FOPS_BIT);
105                 refcount_set(&fsd->active_users, 1);
106                 init_completion(&fsd->active_users_drained);
107                 if (cmpxchg(&dentry->d_fsdata, d_fsd, fsd) != d_fsd) {
108                         kfree(fsd);
109                         fsd = READ_ONCE(dentry->d_fsdata);
110                 }
111         }
112
113         /*
114          * In case of a successful cmpxchg() above, this check is
115          * strictly necessary and must follow it, see the comment in
116          * __debugfs_remove_file().
117          * OTOH, if the cmpxchg() hasn't been executed or wasn't
118          * successful, this serves the purpose of not starving
119          * removers.
120          */
121         if (d_unlinked(dentry))
122                 return -EIO;
123
124         if (!refcount_inc_not_zero(&fsd->active_users))
125                 return -EIO;
126
127         return 0;
128 }
129 EXPORT_SYMBOL_GPL(debugfs_file_get);
130
131 /**
132  * debugfs_file_put - mark the end of file data access
133  * @dentry: the dentry object formerly passed to
134  *          debugfs_file_get().
135  *
136  * Allow any ongoing concurrent call into debugfs_remove() or
137  * debugfs_remove_recursive() blocked by a former call to
138  * debugfs_file_get() to proceed and return to its caller.
139  */
140 void debugfs_file_put(struct dentry *dentry)
141 {
142         struct debugfs_fsdata *fsd = READ_ONCE(dentry->d_fsdata);
143
144         if (refcount_dec_and_test(&fsd->active_users))
145                 complete(&fsd->active_users_drained);
146 }
147 EXPORT_SYMBOL_GPL(debugfs_file_put);
148
149 /*
150  * Only permit access to world-readable files when the kernel is locked down.
151  * We also need to exclude any file that has ways to write or alter it as root
152  * can bypass the permissions check.
153  */
154 static int debugfs_locked_down(struct inode *inode,
155                                struct file *filp,
156                                const struct file_operations *real_fops)
157 {
158         if ((inode->i_mode & 07777 & ~0444) == 0 &&
159             !(filp->f_mode & FMODE_WRITE) &&
160             !real_fops->unlocked_ioctl &&
161             !real_fops->compat_ioctl &&
162             !real_fops->mmap)
163                 return 0;
164
165         if (security_locked_down(LOCKDOWN_DEBUGFS))
166                 return -EPERM;
167
168         return 0;
169 }
170
171 static int open_proxy_open(struct inode *inode, struct file *filp)
172 {
173         struct dentry *dentry = F_DENTRY(filp);
174         const struct file_operations *real_fops = NULL;
175         int r;
176
177         r = debugfs_file_get(dentry);
178         if (r)
179                 return r == -EIO ? -ENOENT : r;
180
181         real_fops = debugfs_real_fops(filp);
182
183         r = debugfs_locked_down(inode, filp, real_fops);
184         if (r)
185                 goto out;
186
187         if (!fops_get(real_fops)) {
188 #ifdef CONFIG_MODULES
189                 if (real_fops->owner &&
190                     real_fops->owner->state == MODULE_STATE_GOING) {
191                         r = -ENXIO;
192                         goto out;
193                 }
194 #endif
195
196                 /* Huh? Module did not clean up after itself at exit? */
197                 WARN(1, "debugfs file owner did not clean up at exit: %pd",
198                         dentry);
199                 r = -ENXIO;
200                 goto out;
201         }
202         replace_fops(filp, real_fops);
203
204         if (real_fops->open)
205                 r = real_fops->open(inode, filp);
206
207 out:
208         debugfs_file_put(dentry);
209         return r;
210 }
211
212 const struct file_operations debugfs_open_proxy_file_operations = {
213         .open = open_proxy_open,
214 };
215
216 #define PROTO(args...) args
217 #define ARGS(args...) args
218
219 #define FULL_PROXY_FUNC(name, ret_type, filp, proto, args)              \
220 static ret_type full_proxy_ ## name(proto)                              \
221 {                                                                       \
222         struct dentry *dentry = F_DENTRY(filp);                 \
223         const struct file_operations *real_fops;                        \
224         ret_type r;                                                     \
225                                                                         \
226         r = debugfs_file_get(dentry);                                   \
227         if (unlikely(r))                                                \
228                 return r;                                               \
229         real_fops = debugfs_real_fops(filp);                            \
230         r = real_fops->name(args);                                      \
231         debugfs_file_put(dentry);                                       \
232         return r;                                                       \
233 }
234
235 FULL_PROXY_FUNC(llseek, loff_t, filp,
236                 PROTO(struct file *filp, loff_t offset, int whence),
237                 ARGS(filp, offset, whence));
238
239 FULL_PROXY_FUNC(read, ssize_t, filp,
240                 PROTO(struct file *filp, char __user *buf, size_t size,
241                         loff_t *ppos),
242                 ARGS(filp, buf, size, ppos));
243
244 FULL_PROXY_FUNC(write, ssize_t, filp,
245                 PROTO(struct file *filp, const char __user *buf, size_t size,
246                         loff_t *ppos),
247                 ARGS(filp, buf, size, ppos));
248
249 FULL_PROXY_FUNC(unlocked_ioctl, long, filp,
250                 PROTO(struct file *filp, unsigned int cmd, unsigned long arg),
251                 ARGS(filp, cmd, arg));
252
253 static __poll_t full_proxy_poll(struct file *filp,
254                                 struct poll_table_struct *wait)
255 {
256         struct dentry *dentry = F_DENTRY(filp);
257         __poll_t r = 0;
258         const struct file_operations *real_fops;
259
260         if (debugfs_file_get(dentry))
261                 return EPOLLHUP;
262
263         real_fops = debugfs_real_fops(filp);
264         r = real_fops->poll(filp, wait);
265         debugfs_file_put(dentry);
266         return r;
267 }
268
269 static int full_proxy_release(struct inode *inode, struct file *filp)
270 {
271         const struct dentry *dentry = F_DENTRY(filp);
272         const struct file_operations *real_fops = debugfs_real_fops(filp);
273         const struct file_operations *proxy_fops = filp->f_op;
274         int r = 0;
275
276         /*
277          * We must not protect this against removal races here: the
278          * original releaser should be called unconditionally in order
279          * not to leak any resources. Releasers must not assume that
280          * ->i_private is still being meaningful here.
281          */
282         if (real_fops->release)
283                 r = real_fops->release(inode, filp);
284
285         replace_fops(filp, d_inode(dentry)->i_fop);
286         kfree(proxy_fops);
287         fops_put(real_fops);
288         return r;
289 }
290
291 static void __full_proxy_fops_init(struct file_operations *proxy_fops,
292                                 const struct file_operations *real_fops)
293 {
294         proxy_fops->release = full_proxy_release;
295         if (real_fops->llseek)
296                 proxy_fops->llseek = full_proxy_llseek;
297         if (real_fops->read)
298                 proxy_fops->read = full_proxy_read;
299         if (real_fops->write)
300                 proxy_fops->write = full_proxy_write;
301         if (real_fops->poll)
302                 proxy_fops->poll = full_proxy_poll;
303         if (real_fops->unlocked_ioctl)
304                 proxy_fops->unlocked_ioctl = full_proxy_unlocked_ioctl;
305 }
306
307 static int full_proxy_open(struct inode *inode, struct file *filp)
308 {
309         struct dentry *dentry = F_DENTRY(filp);
310         const struct file_operations *real_fops = NULL;
311         struct file_operations *proxy_fops = NULL;
312         int r;
313
314         r = debugfs_file_get(dentry);
315         if (r)
316                 return r == -EIO ? -ENOENT : r;
317
318         real_fops = debugfs_real_fops(filp);
319
320         r = debugfs_locked_down(inode, filp, real_fops);
321         if (r)
322                 goto out;
323
324         if (!fops_get(real_fops)) {
325 #ifdef CONFIG_MODULES
326                 if (real_fops->owner &&
327                     real_fops->owner->state == MODULE_STATE_GOING) {
328                         r = -ENXIO;
329                         goto out;
330                 }
331 #endif
332
333                 /* Huh? Module did not cleanup after itself at exit? */
334                 WARN(1, "debugfs file owner did not clean up at exit: %pd",
335                         dentry);
336                 r = -ENXIO;
337                 goto out;
338         }
339
340         proxy_fops = kzalloc(sizeof(*proxy_fops), GFP_KERNEL);
341         if (!proxy_fops) {
342                 r = -ENOMEM;
343                 goto free_proxy;
344         }
345         __full_proxy_fops_init(proxy_fops, real_fops);
346         replace_fops(filp, proxy_fops);
347
348         if (real_fops->open) {
349                 r = real_fops->open(inode, filp);
350                 if (r) {
351                         replace_fops(filp, d_inode(dentry)->i_fop);
352                         goto free_proxy;
353                 } else if (filp->f_op != proxy_fops) {
354                         /* No protection against file removal anymore. */
355                         WARN(1, "debugfs file owner replaced proxy fops: %pd",
356                                 dentry);
357                         goto free_proxy;
358                 }
359         }
360
361         goto out;
362 free_proxy:
363         kfree(proxy_fops);
364         fops_put(real_fops);
365 out:
366         debugfs_file_put(dentry);
367         return r;
368 }
369
370 const struct file_operations debugfs_full_proxy_file_operations = {
371         .open = full_proxy_open,
372 };
373
374 ssize_t debugfs_attr_read(struct file *file, char __user *buf,
375                         size_t len, loff_t *ppos)
376 {
377         struct dentry *dentry = F_DENTRY(file);
378         ssize_t ret;
379
380         ret = debugfs_file_get(dentry);
381         if (unlikely(ret))
382                 return ret;
383         ret = simple_attr_read(file, buf, len, ppos);
384         debugfs_file_put(dentry);
385         return ret;
386 }
387 EXPORT_SYMBOL_GPL(debugfs_attr_read);
388
389 static ssize_t debugfs_attr_write_xsigned(struct file *file, const char __user *buf,
390                          size_t len, loff_t *ppos, bool is_signed)
391 {
392         struct dentry *dentry = F_DENTRY(file);
393         ssize_t ret;
394
395         ret = debugfs_file_get(dentry);
396         if (unlikely(ret))
397                 return ret;
398         if (is_signed)
399                 ret = simple_attr_write_signed(file, buf, len, ppos);
400         else
401                 ret = simple_attr_write(file, buf, len, ppos);
402         debugfs_file_put(dentry);
403         return ret;
404 }
405
406 ssize_t debugfs_attr_write(struct file *file, const char __user *buf,
407                          size_t len, loff_t *ppos)
408 {
409         return debugfs_attr_write_xsigned(file, buf, len, ppos, false);
410 }
411 EXPORT_SYMBOL_GPL(debugfs_attr_write);
412
413 ssize_t debugfs_attr_write_signed(struct file *file, const char __user *buf,
414                          size_t len, loff_t *ppos)
415 {
416         return debugfs_attr_write_xsigned(file, buf, len, ppos, true);
417 }
418 EXPORT_SYMBOL_GPL(debugfs_attr_write_signed);
419
420 static struct dentry *debugfs_create_mode_unsafe(const char *name, umode_t mode,
421                                         struct dentry *parent, void *value,
422                                         const struct file_operations *fops,
423                                         const struct file_operations *fops_ro,
424                                         const struct file_operations *fops_wo)
425 {
426         /* if there are no write bits set, make read only */
427         if (!(mode & S_IWUGO))
428                 return debugfs_create_file_unsafe(name, mode, parent, value,
429                                                 fops_ro);
430         /* if there are no read bits set, make write only */
431         if (!(mode & S_IRUGO))
432                 return debugfs_create_file_unsafe(name, mode, parent, value,
433                                                 fops_wo);
434
435         return debugfs_create_file_unsafe(name, mode, parent, value, fops);
436 }
437
438 static int debugfs_u8_set(void *data, u64 val)
439 {
440         *(u8 *)data = val;
441         return 0;
442 }
443 static int debugfs_u8_get(void *data, u64 *val)
444 {
445         *val = *(u8 *)data;
446         return 0;
447 }
448 DEFINE_DEBUGFS_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");
449 DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n");
450 DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n");
451
452 /**
453  * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value
454  * @name: a pointer to a string containing the name of the file to create.
455  * @mode: the permission that the file should have
456  * @parent: a pointer to the parent dentry for this file.  This should be a
457  *          directory dentry if set.  If this parameter is %NULL, then the
458  *          file will be created in the root of the debugfs filesystem.
459  * @value: a pointer to the variable that the file should read to and write
460  *         from.
461  *
462  * This function creates a file in debugfs with the given name that
463  * contains the value of the variable @value.  If the @mode variable is so
464  * set, it can be read from, and written to.
465  */
466 void debugfs_create_u8(const char *name, umode_t mode, struct dentry *parent,
467                        u8 *value)
468 {
469         debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u8,
470                                    &fops_u8_ro, &fops_u8_wo);
471 }
472 EXPORT_SYMBOL_GPL(debugfs_create_u8);
473
474 static int debugfs_u16_set(void *data, u64 val)
475 {
476         *(u16 *)data = val;
477         return 0;
478 }
479 static int debugfs_u16_get(void *data, u64 *val)
480 {
481         *val = *(u16 *)data;
482         return 0;
483 }
484 DEFINE_DEBUGFS_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");
485 DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n");
486 DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n");
487
488 /**
489  * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value
490  * @name: a pointer to a string containing the name of the file to create.
491  * @mode: the permission that the file should have
492  * @parent: a pointer to the parent dentry for this file.  This should be a
493  *          directory dentry if set.  If this parameter is %NULL, then the
494  *          file will be created in the root of the debugfs filesystem.
495  * @value: a pointer to the variable that the file should read to and write
496  *         from.
497  *
498  * This function creates a file in debugfs with the given name that
499  * contains the value of the variable @value.  If the @mode variable is so
500  * set, it can be read from, and written to.
501  */
502 void debugfs_create_u16(const char *name, umode_t mode, struct dentry *parent,
503                         u16 *value)
504 {
505         debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u16,
506                                    &fops_u16_ro, &fops_u16_wo);
507 }
508 EXPORT_SYMBOL_GPL(debugfs_create_u16);
509
510 static int debugfs_u32_set(void *data, u64 val)
511 {
512         *(u32 *)data = val;
513         return 0;
514 }
515 static int debugfs_u32_get(void *data, u64 *val)
516 {
517         *val = *(u32 *)data;
518         return 0;
519 }
520 DEFINE_DEBUGFS_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");
521 DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n");
522 DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n");
523
524 /**
525  * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value
526  * @name: a pointer to a string containing the name of the file to create.
527  * @mode: the permission that the file should have
528  * @parent: a pointer to the parent dentry for this file.  This should be a
529  *          directory dentry if set.  If this parameter is %NULL, then the
530  *          file will be created in the root of the debugfs filesystem.
531  * @value: a pointer to the variable that the file should read to and write
532  *         from.
533  *
534  * This function creates a file in debugfs with the given name that
535  * contains the value of the variable @value.  If the @mode variable is so
536  * set, it can be read from, and written to.
537  */
538 void debugfs_create_u32(const char *name, umode_t mode, struct dentry *parent,
539                         u32 *value)
540 {
541         debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u32,
542                                    &fops_u32_ro, &fops_u32_wo);
543 }
544 EXPORT_SYMBOL_GPL(debugfs_create_u32);
545
546 static int debugfs_u64_set(void *data, u64 val)
547 {
548         *(u64 *)data = val;
549         return 0;
550 }
551
552 static int debugfs_u64_get(void *data, u64 *val)
553 {
554         *val = *(u64 *)data;
555         return 0;
556 }
557 DEFINE_DEBUGFS_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n");
558 DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n");
559 DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");
560
561 /**
562  * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value
563  * @name: a pointer to a string containing the name of the file to create.
564  * @mode: the permission that the file should have
565  * @parent: a pointer to the parent dentry for this file.  This should be a
566  *          directory dentry if set.  If this parameter is %NULL, then the
567  *          file will be created in the root of the debugfs filesystem.
568  * @value: a pointer to the variable that the file should read to and write
569  *         from.
570  *
571  * This function creates a file in debugfs with the given name that
572  * contains the value of the variable @value.  If the @mode variable is so
573  * set, it can be read from, and written to.
574  */
575 void debugfs_create_u64(const char *name, umode_t mode, struct dentry *parent,
576                         u64 *value)
577 {
578         debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u64,
579                                    &fops_u64_ro, &fops_u64_wo);
580 }
581 EXPORT_SYMBOL_GPL(debugfs_create_u64);
582
583 static int debugfs_ulong_set(void *data, u64 val)
584 {
585         *(unsigned long *)data = val;
586         return 0;
587 }
588
589 static int debugfs_ulong_get(void *data, u64 *val)
590 {
591         *val = *(unsigned long *)data;
592         return 0;
593 }
594 DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong, debugfs_ulong_get, debugfs_ulong_set,
595                         "%llu\n");
596 DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_ro, debugfs_ulong_get, NULL, "%llu\n");
597 DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_wo, NULL, debugfs_ulong_set, "%llu\n");
598
599 /**
600  * debugfs_create_ulong - create a debugfs file that is used to read and write
601  * an unsigned long value.
602  * @name: a pointer to a string containing the name of the file to create.
603  * @mode: the permission that the file should have
604  * @parent: a pointer to the parent dentry for this file.  This should be a
605  *          directory dentry if set.  If this parameter is %NULL, then the
606  *          file will be created in the root of the debugfs filesystem.
607  * @value: a pointer to the variable that the file should read to and write
608  *         from.
609  *
610  * This function creates a file in debugfs with the given name that
611  * contains the value of the variable @value.  If the @mode variable is so
612  * set, it can be read from, and written to.
613  */
614 void debugfs_create_ulong(const char *name, umode_t mode, struct dentry *parent,
615                           unsigned long *value)
616 {
617         debugfs_create_mode_unsafe(name, mode, parent, value, &fops_ulong,
618                                    &fops_ulong_ro, &fops_ulong_wo);
619 }
620 EXPORT_SYMBOL_GPL(debugfs_create_ulong);
621
622 DEFINE_DEBUGFS_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n");
623 DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n");
624 DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n");
625
626 DEFINE_DEBUGFS_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set,
627                         "0x%04llx\n");
628 DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n");
629 DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n");
630
631 DEFINE_DEBUGFS_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set,
632                         "0x%08llx\n");
633 DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n");
634 DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n");
635
636 DEFINE_DEBUGFS_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set,
637                         "0x%016llx\n");
638 DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_ro, debugfs_u64_get, NULL, "0x%016llx\n");
639 DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_wo, NULL, debugfs_u64_set, "0x%016llx\n");
640
641 /*
642  * debugfs_create_x{8,16,32,64} - create a debugfs file that is used to read and write an unsigned {8,16,32,64}-bit value
643  *
644  * These functions are exactly the same as the above functions (but use a hex
645  * output for the decimal challenged). For details look at the above unsigned
646  * decimal functions.
647  */
648
649 /**
650  * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value
651  * @name: a pointer to a string containing the name of the file to create.
652  * @mode: the permission that the file should have
653  * @parent: a pointer to the parent dentry for this file.  This should be a
654  *          directory dentry if set.  If this parameter is %NULL, then the
655  *          file will be created in the root of the debugfs filesystem.
656  * @value: a pointer to the variable that the file should read to and write
657  *         from.
658  */
659 void debugfs_create_x8(const char *name, umode_t mode, struct dentry *parent,
660                        u8 *value)
661 {
662         debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x8,
663                                    &fops_x8_ro, &fops_x8_wo);
664 }
665 EXPORT_SYMBOL_GPL(debugfs_create_x8);
666
667 /**
668  * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value
669  * @name: a pointer to a string containing the name of the file to create.
670  * @mode: the permission that the file should have
671  * @parent: a pointer to the parent dentry for this file.  This should be a
672  *          directory dentry if set.  If this parameter is %NULL, then the
673  *          file will be created in the root of the debugfs filesystem.
674  * @value: a pointer to the variable that the file should read to and write
675  *         from.
676  */
677 void debugfs_create_x16(const char *name, umode_t mode, struct dentry *parent,
678                         u16 *value)
679 {
680         debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x16,
681                                    &fops_x16_ro, &fops_x16_wo);
682 }
683 EXPORT_SYMBOL_GPL(debugfs_create_x16);
684
685 /**
686  * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value
687  * @name: a pointer to a string containing the name of the file to create.
688  * @mode: the permission that the file should have
689  * @parent: a pointer to the parent dentry for this file.  This should be a
690  *          directory dentry if set.  If this parameter is %NULL, then the
691  *          file will be created in the root of the debugfs filesystem.
692  * @value: a pointer to the variable that the file should read to and write
693  *         from.
694  */
695 void debugfs_create_x32(const char *name, umode_t mode, struct dentry *parent,
696                         u32 *value)
697 {
698         debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x32,
699                                    &fops_x32_ro, &fops_x32_wo);
700 }
701 EXPORT_SYMBOL_GPL(debugfs_create_x32);
702
703 /**
704  * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value
705  * @name: a pointer to a string containing the name of the file to create.
706  * @mode: the permission that the file should have
707  * @parent: a pointer to the parent dentry for this file.  This should be a
708  *          directory dentry if set.  If this parameter is %NULL, then the
709  *          file will be created in the root of the debugfs filesystem.
710  * @value: a pointer to the variable that the file should read to and write
711  *         from.
712  */
713 void debugfs_create_x64(const char *name, umode_t mode, struct dentry *parent,
714                         u64 *value)
715 {
716         debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x64,
717                                    &fops_x64_ro, &fops_x64_wo);
718 }
719 EXPORT_SYMBOL_GPL(debugfs_create_x64);
720
721
722 static int debugfs_size_t_set(void *data, u64 val)
723 {
724         *(size_t *)data = val;
725         return 0;
726 }
727 static int debugfs_size_t_get(void *data, u64 *val)
728 {
729         *val = *(size_t *)data;
730         return 0;
731 }
732 DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set,
733                         "%llu\n"); /* %llu and %zu are more or less the same */
734 DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_ro, debugfs_size_t_get, NULL, "%llu\n");
735 DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_wo, NULL, debugfs_size_t_set, "%llu\n");
736
737 /**
738  * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value
739  * @name: a pointer to a string containing the name of the file to create.
740  * @mode: the permission that the file should have
741  * @parent: a pointer to the parent dentry for this file.  This should be a
742  *          directory dentry if set.  If this parameter is %NULL, then the
743  *          file will be created in the root of the debugfs filesystem.
744  * @value: a pointer to the variable that the file should read to and write
745  *         from.
746  */
747 void debugfs_create_size_t(const char *name, umode_t mode,
748                            struct dentry *parent, size_t *value)
749 {
750         debugfs_create_mode_unsafe(name, mode, parent, value, &fops_size_t,
751                                    &fops_size_t_ro, &fops_size_t_wo);
752 }
753 EXPORT_SYMBOL_GPL(debugfs_create_size_t);
754
755 static int debugfs_atomic_t_set(void *data, u64 val)
756 {
757         atomic_set((atomic_t *)data, val);
758         return 0;
759 }
760 static int debugfs_atomic_t_get(void *data, u64 *val)
761 {
762         *val = atomic_read((atomic_t *)data);
763         return 0;
764 }
765 DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t, debugfs_atomic_t_get,
766                         debugfs_atomic_t_set, "%lld\n");
767 DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t_ro, debugfs_atomic_t_get, NULL,
768                         "%lld\n");
769 DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t_wo, NULL, debugfs_atomic_t_set,
770                         "%lld\n");
771
772 /**
773  * debugfs_create_atomic_t - create a debugfs file that is used to read and
774  * write an atomic_t value
775  * @name: a pointer to a string containing the name of the file to create.
776  * @mode: the permission that the file should have
777  * @parent: a pointer to the parent dentry for this file.  This should be a
778  *          directory dentry if set.  If this parameter is %NULL, then the
779  *          file will be created in the root of the debugfs filesystem.
780  * @value: a pointer to the variable that the file should read to and write
781  *         from.
782  */
783 void debugfs_create_atomic_t(const char *name, umode_t mode,
784                              struct dentry *parent, atomic_t *value)
785 {
786         debugfs_create_mode_unsafe(name, mode, parent, value, &fops_atomic_t,
787                                    &fops_atomic_t_ro, &fops_atomic_t_wo);
788 }
789 EXPORT_SYMBOL_GPL(debugfs_create_atomic_t);
790
791 ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf,
792                                size_t count, loff_t *ppos)
793 {
794         char buf[2];
795         bool val;
796         int r;
797         struct dentry *dentry = F_DENTRY(file);
798
799         r = debugfs_file_get(dentry);
800         if (unlikely(r))
801                 return r;
802         val = *(bool *)file->private_data;
803         debugfs_file_put(dentry);
804
805         if (val)
806                 buf[0] = 'Y';
807         else
808                 buf[0] = 'N';
809         buf[1] = '\n';
810         return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
811 }
812 EXPORT_SYMBOL_GPL(debugfs_read_file_bool);
813
814 ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf,
815                                 size_t count, loff_t *ppos)
816 {
817         bool bv;
818         int r;
819         bool *val = file->private_data;
820         struct dentry *dentry = F_DENTRY(file);
821
822         r = kstrtobool_from_user(user_buf, count, &bv);
823         if (!r) {
824                 r = debugfs_file_get(dentry);
825                 if (unlikely(r))
826                         return r;
827                 *val = bv;
828                 debugfs_file_put(dentry);
829         }
830
831         return count;
832 }
833 EXPORT_SYMBOL_GPL(debugfs_write_file_bool);
834
835 static const struct file_operations fops_bool = {
836         .read =         debugfs_read_file_bool,
837         .write =        debugfs_write_file_bool,
838         .open =         simple_open,
839         .llseek =       default_llseek,
840 };
841
842 static const struct file_operations fops_bool_ro = {
843         .read =         debugfs_read_file_bool,
844         .open =         simple_open,
845         .llseek =       default_llseek,
846 };
847
848 static const struct file_operations fops_bool_wo = {
849         .write =        debugfs_write_file_bool,
850         .open =         simple_open,
851         .llseek =       default_llseek,
852 };
853
854 /**
855  * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
856  * @name: a pointer to a string containing the name of the file to create.
857  * @mode: the permission that the file should have
858  * @parent: a pointer to the parent dentry for this file.  This should be a
859  *          directory dentry if set.  If this parameter is %NULL, then the
860  *          file will be created in the root of the debugfs filesystem.
861  * @value: a pointer to the variable that the file should read to and write
862  *         from.
863  *
864  * This function creates a file in debugfs with the given name that
865  * contains the value of the variable @value.  If the @mode variable is so
866  * set, it can be read from, and written to.
867  */
868 void debugfs_create_bool(const char *name, umode_t mode, struct dentry *parent,
869                          bool *value)
870 {
871         debugfs_create_mode_unsafe(name, mode, parent, value, &fops_bool,
872                                    &fops_bool_ro, &fops_bool_wo);
873 }
874 EXPORT_SYMBOL_GPL(debugfs_create_bool);
875
876 ssize_t debugfs_read_file_str(struct file *file, char __user *user_buf,
877                               size_t count, loff_t *ppos)
878 {
879         struct dentry *dentry = F_DENTRY(file);
880         char *str, *copy = NULL;
881         int copy_len, len;
882         ssize_t ret;
883
884         ret = debugfs_file_get(dentry);
885         if (unlikely(ret))
886                 return ret;
887
888         str = *(char **)file->private_data;
889         len = strlen(str) + 1;
890         copy = kmalloc(len, GFP_KERNEL);
891         if (!copy) {
892                 debugfs_file_put(dentry);
893                 return -ENOMEM;
894         }
895
896         copy_len = strscpy(copy, str, len);
897         debugfs_file_put(dentry);
898         if (copy_len < 0) {
899                 kfree(copy);
900                 return copy_len;
901         }
902
903         copy[copy_len] = '\n';
904
905         ret = simple_read_from_buffer(user_buf, count, ppos, copy, len);
906         kfree(copy);
907
908         return ret;
909 }
910
911 static ssize_t debugfs_write_file_str(struct file *file, const char __user *user_buf,
912                                       size_t count, loff_t *ppos)
913 {
914         /* This is really only for read-only strings */
915         return -EINVAL;
916 }
917
918 static const struct file_operations fops_str = {
919         .read =         debugfs_read_file_str,
920         .write =        debugfs_write_file_str,
921         .open =         simple_open,
922         .llseek =       default_llseek,
923 };
924
925 static const struct file_operations fops_str_ro = {
926         .read =         debugfs_read_file_str,
927         .open =         simple_open,
928         .llseek =       default_llseek,
929 };
930
931 static const struct file_operations fops_str_wo = {
932         .write =        debugfs_write_file_str,
933         .open =         simple_open,
934         .llseek =       default_llseek,
935 };
936
937 /**
938  * debugfs_create_str - create a debugfs file that is used to read and write a string value
939  * @name: a pointer to a string containing the name of the file to create.
940  * @mode: the permission that the file should have
941  * @parent: a pointer to the parent dentry for this file.  This should be a
942  *          directory dentry if set.  If this parameter is %NULL, then the
943  *          file will be created in the root of the debugfs filesystem.
944  * @value: a pointer to the variable that the file should read to and write
945  *         from.
946  *
947  * This function creates a file in debugfs with the given name that
948  * contains the value of the variable @value.  If the @mode variable is so
949  * set, it can be read from, and written to.
950  *
951  * This function will return a pointer to a dentry if it succeeds.  This
952  * pointer must be passed to the debugfs_remove() function when the file is
953  * to be removed (no automatic cleanup happens if your module is unloaded,
954  * you are responsible here.)  If an error occurs, ERR_PTR(-ERROR) will be
955  * returned.
956  *
957  * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will
958  * be returned.
959  */
960 void debugfs_create_str(const char *name, umode_t mode,
961                         struct dentry *parent, char **value)
962 {
963         debugfs_create_mode_unsafe(name, mode, parent, value, &fops_str,
964                                    &fops_str_ro, &fops_str_wo);
965 }
966
967 static ssize_t read_file_blob(struct file *file, char __user *user_buf,
968                               size_t count, loff_t *ppos)
969 {
970         struct debugfs_blob_wrapper *blob = file->private_data;
971         struct dentry *dentry = F_DENTRY(file);
972         ssize_t r;
973
974         r = debugfs_file_get(dentry);
975         if (unlikely(r))
976                 return r;
977         r = simple_read_from_buffer(user_buf, count, ppos, blob->data,
978                                 blob->size);
979         debugfs_file_put(dentry);
980         return r;
981 }
982
983 static const struct file_operations fops_blob = {
984         .read =         read_file_blob,
985         .open =         simple_open,
986         .llseek =       default_llseek,
987 };
988
989 /**
990  * debugfs_create_blob - create a debugfs file that is used to read a binary blob
991  * @name: a pointer to a string containing the name of the file to create.
992  * @mode: the read permission that the file should have (other permissions are
993  *        masked out)
994  * @parent: a pointer to the parent dentry for this file.  This should be a
995  *          directory dentry if set.  If this parameter is %NULL, then the
996  *          file will be created in the root of the debugfs filesystem.
997  * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
998  *        to the blob data and the size of the data.
999  *
1000  * This function creates a file in debugfs with the given name that exports
1001  * @blob->data as a binary blob. If the @mode variable is so set it can be
1002  * read from. Writing is not supported.
1003  *
1004  * This function will return a pointer to a dentry if it succeeds.  This
1005  * pointer must be passed to the debugfs_remove() function when the file is
1006  * to be removed (no automatic cleanup happens if your module is unloaded,
1007  * you are responsible here.)  If an error occurs, ERR_PTR(-ERROR) will be
1008  * returned.
1009  *
1010  * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will
1011  * be returned.
1012  */
1013 struct dentry *debugfs_create_blob(const char *name, umode_t mode,
1014                                    struct dentry *parent,
1015                                    struct debugfs_blob_wrapper *blob)
1016 {
1017         return debugfs_create_file_unsafe(name, mode & 0444, parent, blob, &fops_blob);
1018 }
1019 EXPORT_SYMBOL_GPL(debugfs_create_blob);
1020
1021 static size_t u32_format_array(char *buf, size_t bufsize,
1022                                u32 *array, int array_size)
1023 {
1024         size_t ret = 0;
1025
1026         while (--array_size >= 0) {
1027                 size_t len;
1028                 char term = array_size ? ' ' : '\n';
1029
1030                 len = snprintf(buf, bufsize, "%u%c", *array++, term);
1031                 ret += len;
1032
1033                 buf += len;
1034                 bufsize -= len;
1035         }
1036         return ret;
1037 }
1038
1039 static int u32_array_open(struct inode *inode, struct file *file)
1040 {
1041         struct debugfs_u32_array *data = inode->i_private;
1042         int size, elements = data->n_elements;
1043         char *buf;
1044
1045         /*
1046          * Max size:
1047          *  - 10 digits + ' '/'\n' = 11 bytes per number
1048          *  - terminating NUL character
1049          */
1050         size = elements*11;
1051         buf = kmalloc(size+1, GFP_KERNEL);
1052         if (!buf)
1053                 return -ENOMEM;
1054         buf[size] = 0;
1055
1056         file->private_data = buf;
1057         u32_format_array(buf, size, data->array, data->n_elements);
1058
1059         return nonseekable_open(inode, file);
1060 }
1061
1062 static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len,
1063                               loff_t *ppos)
1064 {
1065         size_t size = strlen(file->private_data);
1066
1067         return simple_read_from_buffer(buf, len, ppos,
1068                                         file->private_data, size);
1069 }
1070
1071 static int u32_array_release(struct inode *inode, struct file *file)
1072 {
1073         kfree(file->private_data);
1074
1075         return 0;
1076 }
1077
1078 static const struct file_operations u32_array_fops = {
1079         .owner   = THIS_MODULE,
1080         .open    = u32_array_open,
1081         .release = u32_array_release,
1082         .read    = u32_array_read,
1083         .llseek  = no_llseek,
1084 };
1085
1086 /**
1087  * debugfs_create_u32_array - create a debugfs file that is used to read u32
1088  * array.
1089  * @name: a pointer to a string containing the name of the file to create.
1090  * @mode: the permission that the file should have.
1091  * @parent: a pointer to the parent dentry for this file.  This should be a
1092  *          directory dentry if set.  If this parameter is %NULL, then the
1093  *          file will be created in the root of the debugfs filesystem.
1094  * @array: wrapper struct containing data pointer and size of the array.
1095  *
1096  * This function creates a file in debugfs with the given name that exports
1097  * @array as data. If the @mode variable is so set it can be read from.
1098  * Writing is not supported. Seek within the file is also not supported.
1099  * Once array is created its size can not be changed.
1100  */
1101 void debugfs_create_u32_array(const char *name, umode_t mode,
1102                               struct dentry *parent,
1103                               struct debugfs_u32_array *array)
1104 {
1105         debugfs_create_file_unsafe(name, mode, parent, array, &u32_array_fops);
1106 }
1107 EXPORT_SYMBOL_GPL(debugfs_create_u32_array);
1108
1109 #ifdef CONFIG_HAS_IOMEM
1110
1111 /*
1112  * The regset32 stuff is used to print 32-bit registers using the
1113  * seq_file utilities. We offer printing a register set in an already-opened
1114  * sequential file or create a debugfs file that only prints a regset32.
1115  */
1116
1117 /**
1118  * debugfs_print_regs32 - use seq_print to describe a set of registers
1119  * @s: the seq_file structure being used to generate output
1120  * @regs: an array if struct debugfs_reg32 structures
1121  * @nregs: the length of the above array
1122  * @base: the base address to be used in reading the registers
1123  * @prefix: a string to be prefixed to every output line
1124  *
1125  * This function outputs a text block describing the current values of
1126  * some 32-bit hardware registers. It is meant to be used within debugfs
1127  * files based on seq_file that need to show registers, intermixed with other
1128  * information. The prefix argument may be used to specify a leading string,
1129  * because some peripherals have several blocks of identical registers,
1130  * for example configuration of dma channels
1131  */
1132 void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
1133                           int nregs, void __iomem *base, char *prefix)
1134 {
1135         int i;
1136
1137         for (i = 0; i < nregs; i++, regs++) {
1138                 if (prefix)
1139                         seq_printf(s, "%s", prefix);
1140                 seq_printf(s, "%s = 0x%08x\n", regs->name,
1141                            readl(base + regs->offset));
1142                 if (seq_has_overflowed(s))
1143                         break;
1144         }
1145 }
1146 EXPORT_SYMBOL_GPL(debugfs_print_regs32);
1147
1148 static int debugfs_regset32_show(struct seq_file *s, void *data)
1149 {
1150         struct debugfs_regset32 *regset = s->private;
1151
1152         if (regset->dev)
1153                 pm_runtime_get_sync(regset->dev);
1154
1155         debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, "");
1156
1157         if (regset->dev)
1158                 pm_runtime_put(regset->dev);
1159
1160         return 0;
1161 }
1162
1163 DEFINE_SHOW_ATTRIBUTE(debugfs_regset32);
1164
1165 /**
1166  * debugfs_create_regset32 - create a debugfs file that returns register values
1167  * @name: a pointer to a string containing the name of the file to create.
1168  * @mode: the permission that the file should have
1169  * @parent: a pointer to the parent dentry for this file.  This should be a
1170  *          directory dentry if set.  If this parameter is %NULL, then the
1171  *          file will be created in the root of the debugfs filesystem.
1172  * @regset: a pointer to a struct debugfs_regset32, which contains a pointer
1173  *          to an array of register definitions, the array size and the base
1174  *          address where the register bank is to be found.
1175  *
1176  * This function creates a file in debugfs with the given name that reports
1177  * the names and values of a set of 32-bit registers. If the @mode variable
1178  * is so set it can be read from. Writing is not supported.
1179  */
1180 void debugfs_create_regset32(const char *name, umode_t mode,
1181                              struct dentry *parent,
1182                              struct debugfs_regset32 *regset)
1183 {
1184         debugfs_create_file(name, mode, parent, regset, &debugfs_regset32_fops);
1185 }
1186 EXPORT_SYMBOL_GPL(debugfs_create_regset32);
1187
1188 #endif /* CONFIG_HAS_IOMEM */
1189
1190 struct debugfs_devm_entry {
1191         int (*read)(struct seq_file *seq, void *data);
1192         struct device *dev;
1193 };
1194
1195 static int debugfs_devm_entry_open(struct inode *inode, struct file *f)
1196 {
1197         struct debugfs_devm_entry *entry = inode->i_private;
1198
1199         return single_open(f, entry->read, entry->dev);
1200 }
1201
1202 static const struct file_operations debugfs_devm_entry_ops = {
1203         .owner = THIS_MODULE,
1204         .open = debugfs_devm_entry_open,
1205         .release = single_release,
1206         .read = seq_read,
1207         .llseek = seq_lseek
1208 };
1209
1210 /**
1211  * debugfs_create_devm_seqfile - create a debugfs file that is bound to device.
1212  *
1213  * @dev: device related to this debugfs file.
1214  * @name: name of the debugfs file.
1215  * @parent: a pointer to the parent dentry for this file.  This should be a
1216  *      directory dentry if set.  If this parameter is %NULL, then the
1217  *      file will be created in the root of the debugfs filesystem.
1218  * @read_fn: function pointer called to print the seq_file content.
1219  */
1220 void debugfs_create_devm_seqfile(struct device *dev, const char *name,
1221                                  struct dentry *parent,
1222                                  int (*read_fn)(struct seq_file *s, void *data))
1223 {
1224         struct debugfs_devm_entry *entry;
1225
1226         if (IS_ERR(parent))
1227                 return;
1228
1229         entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL);
1230         if (!entry)
1231                 return;
1232
1233         entry->read = read_fn;
1234         entry->dev = dev;
1235
1236         debugfs_create_file(name, S_IRUGO, parent, entry,
1237                             &debugfs_devm_entry_ops);
1238 }
1239 EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile);