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