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