Linux 6.16-rc6
[linux-block.git] / fs / debugfs / file.c
CommitLineData
3bce94fd 1// SPDX-License-Identifier: GPL-2.0
1da177e4
LT
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 *
1da177e4 8 * debugfs is for people to use instead of /proc or /sys.
e1b4fc7a 9 * See Documentation/filesystems/ for more details.
1da177e4
LT
10 */
11
1da177e4
LT
12#include <linux/module.h>
13#include <linux/fs.h>
1a087c6a 14#include <linux/seq_file.h>
1da177e4
LT
15#include <linux/pagemap.h>
16#include <linux/debugfs.h>
03e099fb 17#include <linux/io.h>
9fe2a701 18#include <linux/slab.h>
3a76e5e0 19#include <linux/atomic.h>
98210b7f 20#include <linux/device.h>
30332eee 21#include <linux/pm_runtime.h>
cfe39442 22#include <linux/poll.h>
5496197f 23#include <linux/security.h>
9fd4dcec
NS
24
25#include "internal.h"
1da177e4 26
49d200de
NS
27struct poll_table_struct;
28
1da177e4
LT
29static ssize_t default_read_file(struct file *file, char __user *buf,
30 size_t count, loff_t *ppos)
31{
32 return 0;
33}
34
35static 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
9fd4dcec 41const struct file_operations debugfs_noop_file_operations = {
1da177e4
LT
42 .read = default_read_file,
43 .write = default_write_file,
234e3405 44 .open = simple_open,
6038f373 45 .llseek = noop_llseek,
1da177e4
LT
46};
47
9fd4dcec
NS
48#define F_DENTRY(filp) ((filp)->f_path.dentry)
49
12c92098
AV
50const void *debugfs_get_aux(const struct file *file)
51{
52 return DEBUGFS_I(file_inode(file))->aux;
53}
54EXPORT_SYMBOL_GPL(debugfs_get_aux);
55
7c8d4698 56const struct file_operations *debugfs_real_fops(const struct file *filp)
7c8d4698
NS
57{
58 struct debugfs_fsdata *fsd = F_DENTRY(filp)->d_fsdata;
055ab8e3 59
95688800 60 if (!fsd) {
7d39bc50
NS
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
7c8d4698
NS
69 return fsd->real_fops;
70}
71EXPORT_SYMBOL_GPL(debugfs_real_fops);
72
f8f25893
JB
73enum dbgfs_get_mode {
74 DBGFS_GET_ALREADY,
75 DBGFS_GET_REGULAR,
76 DBGFS_GET_SHORT,
77};
78
79static int __debugfs_file_get(struct dentry *dentry, enum dbgfs_get_mode mode)
e9117a5a 80{
7d39bc50
NS
81 struct debugfs_fsdata *fsd;
82 void *d_fsd;
83
0ed04a18
JB
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
7d39bc50 92 d_fsd = READ_ONCE(dentry->d_fsdata);
95688800 93 if (d_fsd) {
7d39bc50
NS
94 fsd = d_fsd;
95 } else {
95688800 96 struct inode *inode = dentry->d_inode;
57b31475 97 unsigned int methods = 0;
95688800 98
f8f25893
JB
99 if (WARN_ON(mode == DBGFS_GET_ALREADY))
100 return -EINVAL;
101
7d39bc50
NS
102 fsd = kmalloc(sizeof(*fsd), GFP_KERNEL);
103 if (!fsd)
104 return -ENOMEM;
105
f8f25893 106 if (mode == DBGFS_GET_SHORT) {
41a0ecc0 107 const struct debugfs_short_fops *ops;
95688800 108 ops = fsd->short_fops = DEBUGFS_I(inode)->short_fops;
41a0ecc0 109 if (ops->llseek)
57b31475 110 methods |= HAS_LSEEK;
41a0ecc0 111 if (ops->read)
57b31475 112 methods |= HAS_READ;
41a0ecc0 113 if (ops->write)
57b31475
AV
114 methods |= HAS_WRITE;
115 fsd->real_fops = NULL;
8dc6d81c 116 } else {
41a0ecc0 117 const struct file_operations *ops;
95688800 118 ops = fsd->real_fops = DEBUGFS_I(inode)->real_fops;
41a0ecc0 119 if (ops->llseek)
57b31475 120 methods |= HAS_LSEEK;
41a0ecc0 121 if (ops->read)
57b31475 122 methods |= HAS_READ;
41a0ecc0 123 if (ops->write)
57b31475 124 methods |= HAS_WRITE;
41a0ecc0 125 if (ops->unlocked_ioctl)
57b31475 126 methods |= HAS_IOCTL;
41a0ecc0 127 if (ops->poll)
57b31475
AV
128 methods |= HAS_POLL;
129 fsd->short_fops = NULL;
8dc6d81c 130 }
57b31475 131 fsd->methods = methods;
7d39bc50
NS
132 refcount_set(&fsd->active_users, 1);
133 init_completion(&fsd->active_users_drained);
159f5bda
JB
134 INIT_LIST_HEAD(&fsd->cancellations);
135 mutex_init(&fsd->cancellations_mtx);
136
95688800
AV
137 d_fsd = cmpxchg(&dentry->d_fsdata, NULL, fsd);
138 if (d_fsd) {
159f5bda 139 mutex_destroy(&fsd->cancellations_mtx);
7d39bc50 140 kfree(fsd);
95688800 141 fsd = d_fsd;
7d39bc50
NS
142 }
143 }
e9117a5a 144
7d39bc50
NS
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 */
e9117a5a
NS
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}
f8f25893
JB
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 */
177int debugfs_file_get(struct dentry *dentry)
178{
179 return __debugfs_file_get(dentry, DBGFS_GET_ALREADY);
180}
e9117a5a
NS
181EXPORT_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 */
192void debugfs_file_put(struct dentry *dentry)
193{
7d39bc50 194 struct debugfs_fsdata *fsd = READ_ONCE(dentry->d_fsdata);
e9117a5a
NS
195
196 if (refcount_dec_and_test(&fsd->active_users))
197 complete(&fsd->active_users_drained);
198}
199EXPORT_SYMBOL_GPL(debugfs_file_put);
200
8c88a474
JB
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 */
223void 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);
95688800 238 if (WARN_ON(!fsd))
8c88a474
JB
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}
249EXPORT_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 */
259void 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);
95688800 269 if (WARN_ON(!fsd))
8c88a474
JB
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}
277EXPORT_SYMBOL_GPL(debugfs_leave_cancellation);
278
5496197f
DH
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 */
a37f4958
ES
284static int debugfs_locked_down(struct inode *inode,
285 struct file *filp,
286 const struct file_operations *real_fops)
5496197f 287{
358fcf5d 288 if ((inode->i_mode & 07777 & ~0444) == 0 &&
5496197f 289 !(filp->f_mode & FMODE_WRITE) &&
8dc6d81c
JB
290 (!real_fops ||
291 (!real_fops->unlocked_ioctl &&
292 !real_fops->compat_ioctl &&
293 !real_fops->mmap)))
a37f4958 294 return 0;
5496197f 295
a37f4958
ES
296 if (security_locked_down(LOCKDOWN_DEBUGFS))
297 return -EPERM;
298
299 return 0;
5496197f
DH
300}
301
9fd4dcec
NS
302static int open_proxy_open(struct inode *inode, struct file *filp)
303{
69d29f9e 304 struct dentry *dentry = F_DENTRY(filp);
9fd4dcec 305 const struct file_operations *real_fops = NULL;
7d39bc50 306 int r;
9fd4dcec 307
67510d7e 308 r = __debugfs_file_get(dentry, DBGFS_GET_REGULAR);
7d39bc50
NS
309 if (r)
310 return r == -EIO ? -ENOENT : r;
9fd4dcec 311
86f0e067 312 real_fops = debugfs_real_fops(filp);
5496197f 313
a37f4958 314 r = debugfs_locked_down(inode, filp, real_fops);
5496197f
DH
315 if (r)
316 goto out;
317
275678e7 318 if (!fops_get(real_fops)) {
e3b9fc7e 319#ifdef CONFIG_MODULES
275678e7 320 if (real_fops->owner &&
112cedc8
SE
321 real_fops->owner->state == MODULE_STATE_GOING) {
322 r = -ENXIO;
275678e7 323 goto out;
112cedc8 324 }
275678e7
TY
325#endif
326
9fd4dcec
NS
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
338out:
69d29f9e 339 debugfs_file_put(dentry);
9fd4dcec
NS
340 return r;
341}
342
343const struct file_operations debugfs_open_proxy_file_operations = {
344 .open = open_proxy_open,
345};
346
49d200de
NS
347#define PROTO(args...) args
348#define ARGS(args...) args
349
41a0ecc0 350#define FULL_PROXY_FUNC(name, ret_type, filp, proto, args, bit, ret) \
49d200de
NS
351static ret_type full_proxy_ ## name(proto) \
352{ \
41a0ecc0
AV
353 struct dentry *dentry = F_DENTRY(filp); \
354 struct debugfs_fsdata *fsd = dentry->d_fsdata; \
154b9d75 355 const struct file_operations *real_fops; \
49d200de
NS
356 ret_type r; \
357 \
41a0ecc0
AV
358 if (!(fsd->methods & bit)) \
359 return ret; \
69d29f9e
NS
360 r = debugfs_file_get(dentry); \
361 if (unlikely(r)) \
362 return r; \
154b9d75 363 real_fops = debugfs_real_fops(filp); \
69d29f9e
NS
364 r = real_fops->name(args); \
365 debugfs_file_put(dentry); \
49d200de
NS
366 return r; \
367}
368
41a0ecc0 369#define FULL_PROXY_FUNC_BOTH(name, ret_type, filp, proto, args, bit, ret) \
8dc6d81c
JB
370static ret_type full_proxy_ ## name(proto) \
371{ \
372 struct dentry *dentry = F_DENTRY(filp); \
41a0ecc0 373 struct debugfs_fsdata *fsd = dentry->d_fsdata; \
8dc6d81c
JB
374 ret_type r; \
375 \
41a0ecc0
AV
376 if (!(fsd->methods & bit)) \
377 return ret; \
8dc6d81c
JB
378 r = debugfs_file_get(dentry); \
379 if (unlikely(r)) \
380 return r; \
8dc6d81c
JB
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
389FULL_PROXY_FUNC_BOTH(llseek, loff_t, filp,
390 PROTO(struct file *filp, loff_t offset, int whence),
41a0ecc0 391 ARGS(filp, offset, whence), HAS_LSEEK, -ESPIPE);
49d200de 392
8dc6d81c
JB
393FULL_PROXY_FUNC_BOTH(read, ssize_t, filp,
394 PROTO(struct file *filp, char __user *buf, size_t size,
395 loff_t *ppos),
41a0ecc0 396 ARGS(filp, buf, size, ppos), HAS_READ, -EINVAL);
49d200de 397
8dc6d81c
JB
398FULL_PROXY_FUNC_BOTH(write, ssize_t, filp,
399 PROTO(struct file *filp, const char __user *buf,
400 size_t size, loff_t *ppos),
41a0ecc0 401 ARGS(filp, buf, size, ppos), HAS_WRITE, -EINVAL);
49d200de
NS
402
403FULL_PROXY_FUNC(unlocked_ioctl, long, filp,
404 PROTO(struct file *filp, unsigned int cmd, unsigned long arg),
41a0ecc0 405 ARGS(filp, cmd, arg), HAS_IOCTL, -ENOTTY);
49d200de 406
076ccb76 407static __poll_t full_proxy_poll(struct file *filp,
49d200de
NS
408 struct poll_table_struct *wait)
409{
69d29f9e 410 struct dentry *dentry = F_DENTRY(filp);
41a0ecc0 411 struct debugfs_fsdata *fsd = dentry->d_fsdata;
e6c8adca 412 __poll_t r = 0;
154b9d75 413 const struct file_operations *real_fops;
49d200de 414
41a0ecc0
AV
415 if (!(fsd->methods & HAS_POLL))
416 return DEFAULT_POLLMASK;
69d29f9e 417 if (debugfs_file_get(dentry))
a9a08845 418 return EPOLLHUP;
49d200de 419
154b9d75 420 real_fops = debugfs_real_fops(filp);
49d200de 421 r = real_fops->poll(filp, wait);
69d29f9e 422 debugfs_file_put(dentry);
49d200de
NS
423 return r;
424}
425
426static int full_proxy_release(struct inode *inode, struct file *filp)
427{
86f0e067 428 const struct file_operations *real_fops = debugfs_real_fops(filp);
49d200de
NS
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 */
95688800 437 if (real_fops->release)
49d200de
NS
438 r = real_fops->release(inode, filp);
439
49d200de 440 fops_put(real_fops);
a1a9e5d2 441 return r;
49d200de
NS
442}
443
95688800 444static int full_proxy_open_regular(struct inode *inode, struct file *filp)
49d200de 445{
69d29f9e 446 struct dentry *dentry = F_DENTRY(filp);
8dc6d81c 447 const struct file_operations *real_fops;
8dc6d81c 448 struct debugfs_fsdata *fsd;
7d39bc50 449 int r;
49d200de 450
95688800 451 r = __debugfs_file_get(dentry, DBGFS_GET_REGULAR);
7d39bc50
NS
452 if (r)
453 return r == -EIO ? -ENOENT : r;
49d200de 454
8dc6d81c
JB
455 fsd = dentry->d_fsdata;
456 real_fops = fsd->real_fops;
a37f4958 457 r = debugfs_locked_down(inode, filp, real_fops);
5496197f
DH
458 if (r)
459 goto out;
460
95688800 461 if (!fops_get(real_fops)) {
e3b9fc7e 462#ifdef CONFIG_MODULES
275678e7 463 if (real_fops->owner &&
112cedc8
SE
464 real_fops->owner->state == MODULE_STATE_GOING) {
465 r = -ENXIO;
275678e7 466 goto out;
112cedc8 467 }
275678e7
TY
468#endif
469
49d200de
NS
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
95688800
AV
477 if (real_fops->open) {
478 r = real_fops->open(inode, filp);
b10e3e90 479 if (r) {
41a0ecc0
AV
480 fops_put(real_fops);
481 } else if (filp->f_op != &debugfs_full_proxy_file_operations) {
49d200de
NS
482 /* No protection against file removal anymore. */
483 WARN(1, "debugfs file owner replaced proxy fops: %pd",
484 dentry);
41a0ecc0 485 fops_put(real_fops);
49d200de
NS
486 }
487 }
49d200de 488out:
69d29f9e 489 debugfs_file_put(dentry);
49d200de
NS
490 return r;
491}
492
493const struct file_operations debugfs_full_proxy_file_operations = {
f8f25893 494 .open = full_proxy_open_regular,
41a0ecc0
AV
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
f8f25893
JB
501};
502
503static int full_proxy_open_short(struct inode *inode, struct file *filp)
504{
95688800
AV
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;
f8f25893
JB
516}
517
518const struct file_operations debugfs_full_short_proxy_file_operations = {
519 .open = full_proxy_open_short,
41a0ecc0
AV
520 .llseek = full_proxy_llseek,
521 .read = full_proxy_read,
522 .write = full_proxy_write,
49d200de
NS
523};
524
c6468808
NS
525ssize_t debugfs_attr_read(struct file *file, char __user *buf,
526 size_t len, loff_t *ppos)
527{
69d29f9e 528 struct dentry *dentry = F_DENTRY(file);
c6468808 529 ssize_t ret;
c6468808 530
69d29f9e
NS
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);
c6468808
NS
536 return ret;
537}
538EXPORT_SYMBOL_GPL(debugfs_attr_read);
539
d472cf79
AM
540static ssize_t debugfs_attr_write_xsigned(struct file *file, const char __user *buf,
541 size_t len, loff_t *ppos, bool is_signed)
c6468808 542{
69d29f9e 543 struct dentry *dentry = F_DENTRY(file);
c6468808 544 ssize_t ret;
c6468808 545
69d29f9e
NS
546 ret = debugfs_file_get(dentry);
547 if (unlikely(ret))
548 return ret;
d472cf79
AM
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);
69d29f9e 553 debugfs_file_put(dentry);
c6468808
NS
554 return ret;
555}
d472cf79
AM
556
557ssize_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}
c6468808
NS
562EXPORT_SYMBOL_GPL(debugfs_attr_write);
563
d472cf79
AM
564ssize_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}
569EXPORT_SYMBOL_GPL(debugfs_attr_write_signed);
570
4909f168
NS
571static 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
8b88b099 589static int debugfs_u8_set(void *data, u64 val)
acaefc25
AB
590{
591 *(u8 *)data = val;
8b88b099 592 return 0;
acaefc25 593}
8b88b099 594static int debugfs_u8_get(void *data, u64 *val)
acaefc25 595{
8b88b099
CH
596 *val = *(u8 *)data;
597 return 0;
acaefc25 598}
4909f168
NS
599DEFINE_DEBUGFS_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");
600DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n");
601DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n");
1da177e4
LT
602
603/**
6468b3af 604 * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value
1da177e4
LT
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
6468b3af 608 * directory dentry if set. If this parameter is %NULL, then the
1da177e4
LT
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.
1da177e4 616 */
9655ac4a
GKH
617void debugfs_create_u8(const char *name, umode_t mode, struct dentry *parent,
618 u8 *value)
1da177e4 619{
9655ac4a 620 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u8,
b97f6799 621 &fops_u8_ro, &fops_u8_wo);
1da177e4
LT
622}
623EXPORT_SYMBOL_GPL(debugfs_create_u8);
624
8b88b099 625static int debugfs_u16_set(void *data, u64 val)
acaefc25
AB
626{
627 *(u16 *)data = val;
8b88b099 628 return 0;
acaefc25 629}
8b88b099 630static int debugfs_u16_get(void *data, u64 *val)
acaefc25 631{
8b88b099
CH
632 *val = *(u16 *)data;
633 return 0;
acaefc25 634}
4909f168
NS
635DEFINE_DEBUGFS_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");
636DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n");
637DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n");
acaefc25 638
1da177e4 639/**
6468b3af 640 * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value
1da177e4
LT
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
6468b3af 644 * directory dentry if set. If this parameter is %NULL, then the
1da177e4
LT
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.
1da177e4 652 */
313f5dbb
GKH
653void debugfs_create_u16(const char *name, umode_t mode, struct dentry *parent,
654 u16 *value)
1da177e4 655{
313f5dbb 656 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u16,
b97f6799 657 &fops_u16_ro, &fops_u16_wo);
1da177e4
LT
658}
659EXPORT_SYMBOL_GPL(debugfs_create_u16);
660
8b88b099 661static int debugfs_u32_set(void *data, u64 val)
acaefc25
AB
662{
663 *(u32 *)data = val;
8b88b099 664 return 0;
acaefc25 665}
8b88b099 666static int debugfs_u32_get(void *data, u64 *val)
acaefc25 667{
8b88b099
CH
668 *val = *(u32 *)data;
669 return 0;
acaefc25 670}
4909f168
NS
671DEFINE_DEBUGFS_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");
672DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n");
673DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n");
acaefc25 674
1da177e4 675/**
6468b3af 676 * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value
1da177e4
LT
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
6468b3af 680 * directory dentry if set. If this parameter is %NULL, then the
1da177e4
LT
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.
1da177e4 688 */
2b07021a
GKH
689void debugfs_create_u32(const char *name, umode_t mode, struct dentry *parent,
690 u32 *value)
1da177e4 691{
2b07021a 692 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u32,
b97f6799 693 &fops_u32_ro, &fops_u32_wo);
1da177e4
LT
694}
695EXPORT_SYMBOL_GPL(debugfs_create_u32);
696
8b88b099 697static int debugfs_u64_set(void *data, u64 val)
8447891f
ME
698{
699 *(u64 *)data = val;
8b88b099 700 return 0;
8447891f
ME
701}
702
8b88b099 703static int debugfs_u64_get(void *data, u64 *val)
8447891f 704{
8b88b099
CH
705 *val = *(u64 *)data;
706 return 0;
8447891f 707}
4909f168
NS
708DEFINE_DEBUGFS_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n");
709DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n");
710DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");
8447891f
ME
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.
8447891f 725 */
ad26221f
GKH
726void debugfs_create_u64(const char *name, umode_t mode, struct dentry *parent,
727 u64 *value)
8447891f 728{
ad26221f 729 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u64,
b97f6799 730 &fops_u64_ro, &fops_u64_wo);
8447891f
ME
731}
732EXPORT_SYMBOL_GPL(debugfs_create_u64);
733
c23fe831
VK
734static int debugfs_ulong_set(void *data, u64 val)
735{
736 *(unsigned long *)data = val;
737 return 0;
738}
739
740static int debugfs_ulong_get(void *data, u64 *val)
741{
742 *val = *(unsigned long *)data;
743 return 0;
744}
4909f168
NS
745DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong, debugfs_ulong_get, debugfs_ulong_set,
746 "%llu\n");
747DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_ro, debugfs_ulong_get, NULL, "%llu\n");
748DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_wo, NULL, debugfs_ulong_set, "%llu\n");
c23fe831
VK
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.
c23fe831 764 */
fb05b14c
GKH
765void debugfs_create_ulong(const char *name, umode_t mode, struct dentry *parent,
766 unsigned long *value)
c23fe831 767{
fb05b14c
GKH
768 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_ulong,
769 &fops_ulong_ro, &fops_ulong_wo);
c23fe831
VK
770}
771EXPORT_SYMBOL_GPL(debugfs_create_ulong);
772
4909f168
NS
773DEFINE_DEBUGFS_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n");
774DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n");
775DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n");
2ebefc50 776
4909f168
NS
777DEFINE_DEBUGFS_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set,
778 "0x%04llx\n");
779DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n");
780DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n");
2ebefc50 781
4909f168
NS
782DEFINE_DEBUGFS_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set,
783 "0x%08llx\n");
784DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n");
785DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n");
2ebefc50 786
4909f168
NS
787DEFINE_DEBUGFS_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set,
788 "0x%016llx\n");
789DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_ro, debugfs_u64_get, NULL, "0x%016llx\n");
790DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_wo, NULL, debugfs_u64_set, "0x%016llx\n");
15b0beaa 791
e6716b87 792/*
15b0beaa 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
2ebefc50 794 *
e6716b87
RD
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
2ebefc50
RG
797 * decimal functions.
798 */
e6716b87
RD
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 */
c7c11689
GKH
810void debugfs_create_x8(const char *name, umode_t mode, struct dentry *parent,
811 u8 *value)
2ebefc50 812{
c7c11689 813 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x8,
b97f6799 814 &fops_x8_ro, &fops_x8_wo);
2ebefc50
RG
815}
816EXPORT_SYMBOL_GPL(debugfs_create_x8);
817
e6716b87
RD
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 */
e40d38f2
GKH
828void debugfs_create_x16(const char *name, umode_t mode, struct dentry *parent,
829 u16 *value)
2ebefc50 830{
e40d38f2 831 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x16,
b97f6799 832 &fops_x16_ro, &fops_x16_wo);
2ebefc50
RG
833}
834EXPORT_SYMBOL_GPL(debugfs_create_x16);
835
e6716b87
RD
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 */
f5cb0a7e
GKH
846void debugfs_create_x32(const char *name, umode_t mode, struct dentry *parent,
847 u32 *value)
2ebefc50 848{
f5cb0a7e 849 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x32,
b97f6799 850 &fops_x32_ro, &fops_x32_wo);
2ebefc50
RG
851}
852EXPORT_SYMBOL_GPL(debugfs_create_x32);
853
15b0beaa
HY
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 */
0864c408
GKH
864void debugfs_create_x64(const char *name, umode_t mode, struct dentry *parent,
865 u64 *value)
15b0beaa 866{
0864c408 867 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x64,
82b7d4fb 868 &fops_x64_ro, &fops_x64_wo);
15b0beaa
HY
869}
870EXPORT_SYMBOL_GPL(debugfs_create_x64);
871
5e078787
IPG
872
873static int debugfs_size_t_set(void *data, u64 val)
874{
875 *(size_t *)data = val;
876 return 0;
877}
878static int debugfs_size_t_get(void *data, u64 *val)
879{
880 *val = *(size_t *)data;
881 return 0;
882}
4909f168
NS
883DEFINE_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 */
885DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_ro, debugfs_size_t_get, NULL, "%llu\n");
886DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_wo, NULL, debugfs_size_t_set, "%llu\n");
5e078787
IPG
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 */
8e580263
GKH
898void debugfs_create_size_t(const char *name, umode_t mode,
899 struct dentry *parent, size_t *value)
5e078787 900{
8e580263
GKH
901 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_size_t,
902 &fops_size_t_ro, &fops_size_t_wo);
5e078787
IPG
903}
904EXPORT_SYMBOL_GPL(debugfs_create_size_t);
905
3a76e5e0
SJ
906static int debugfs_atomic_t_set(void *data, u64 val)
907{
908 atomic_set((atomic_t *)data, val);
909 return 0;
910}
911static int debugfs_atomic_t_get(void *data, u64 *val)
912{
913 *val = atomic_read((atomic_t *)data);
914 return 0;
915}
d472cf79 916DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t, debugfs_atomic_t_get,
3a76e5e0 917 debugfs_atomic_t_set, "%lld\n");
d472cf79 918DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t_ro, debugfs_atomic_t_get, NULL,
4909f168 919 "%lld\n");
d472cf79 920DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t_wo, NULL, debugfs_atomic_t_set,
4909f168 921 "%lld\n");
3a76e5e0
SJ
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 */
9927c6fa
GKH
934void debugfs_create_atomic_t(const char *name, umode_t mode,
935 struct dentry *parent, atomic_t *value)
3a76e5e0 936{
9927c6fa
GKH
937 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_atomic_t,
938 &fops_atomic_t_ro, &fops_atomic_t_wo);
3a76e5e0
SJ
939}
940EXPORT_SYMBOL_GPL(debugfs_create_atomic_t);
5e078787 941
0642ef6f
RF
942ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf,
943 size_t count, loff_t *ppos)
1da177e4 944{
c8a9c285 945 char buf[2];
4d45f797 946 bool val;
69d29f9e
NS
947 int r;
948 struct dentry *dentry = F_DENTRY(file);
4d45f797 949
69d29f9e
NS
950 r = debugfs_file_get(dentry);
951 if (unlikely(r))
4d45f797 952 return r;
69d29f9e
NS
953 val = *(bool *)file->private_data;
954 debugfs_file_put(dentry);
88e412ea 955
4d45f797 956 if (val)
1da177e4
LT
957 buf[0] = 'Y';
958 else
959 buf[0] = 'N';
960 buf[1] = '\n';
1da177e4
LT
961 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
962}
0642ef6f 963EXPORT_SYMBOL_GPL(debugfs_read_file_bool);
1da177e4 964
0642ef6f
RF
965ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf,
966 size_t count, loff_t *ppos)
1da177e4 967{
8705b48e 968 bool bv;
69d29f9e 969 int r;
621a5f7a 970 bool *val = file->private_data;
69d29f9e 971 struct dentry *dentry = F_DENTRY(file);
1da177e4 972
964f8363
AS
973 r = kstrtobool_from_user(user_buf, count, &bv);
974 if (!r) {
69d29f9e
NS
975 r = debugfs_file_get(dentry);
976 if (unlikely(r))
4d45f797 977 return r;
69d29f9e
NS
978 *val = bv;
979 debugfs_file_put(dentry);
4d45f797 980 }
8705b48e 981
1da177e4
LT
982 return count;
983}
0642ef6f 984EXPORT_SYMBOL_GPL(debugfs_write_file_bool);
1da177e4 985
4b6f5d20 986static const struct file_operations fops_bool = {
0642ef6f
RF
987 .read = debugfs_read_file_bool,
988 .write = debugfs_write_file_bool,
234e3405 989 .open = simple_open,
6038f373 990 .llseek = default_llseek,
1da177e4
LT
991};
992
6713e8fb
SB
993static const struct file_operations fops_bool_ro = {
994 .read = debugfs_read_file_bool,
995 .open = simple_open,
996 .llseek = default_llseek,
997};
998
999static const struct file_operations fops_bool_wo = {
1000 .write = debugfs_write_file_bool,
1001 .open = simple_open,
1002 .llseek = default_llseek,
1003};
1004
1da177e4 1005/**
6468b3af 1006 * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
1da177e4
LT
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
6468b3af 1010 * directory dentry if set. If this parameter is %NULL, then the
1da177e4
LT
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.
1da177e4 1018 */
393b0638
GKH
1019void debugfs_create_bool(const char *name, umode_t mode, struct dentry *parent,
1020 bool *value)
1da177e4 1021{
393b0638 1022 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_bool,
6713e8fb 1023 &fops_bool_ro, &fops_bool_wo);
1da177e4
LT
1024}
1025EXPORT_SYMBOL_GPL(debugfs_create_bool);
1026
9af0440e
PZ
1027ssize_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
f501b6a2 1056 ret = simple_read_from_buffer(user_buf, count, ppos, copy, len);
9af0440e
PZ
1057 kfree(copy);
1058
1059 return ret;
1060}
d60b59b9 1061EXPORT_SYMBOL_GPL(debugfs_create_str);
9af0440e
PZ
1062
1063static ssize_t debugfs_write_file_str(struct file *file, const char __user *user_buf,
1064 size_t count, loff_t *ppos)
1065{
86b54881
MT
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
7360a48b 1101 rcu_assign_pointer(*(char __rcu **)file->private_data, new);
86b54881
MT
1102 synchronize_rcu();
1103 kfree(old);
1104
1105 debugfs_file_put(dentry);
1106 return count;
1107
1108error:
1109 kfree(new);
1110 debugfs_file_put(dentry);
1111 return r;
9af0440e
PZ
1112}
1113
1114static 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
1121static const struct file_operations fops_str_ro = {
1122 .read = debugfs_read_file_str,
1123 .open = simple_open,
1124 .llseek = default_llseek,
1125};
1126
1127static 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.
9af0440e
PZ
1146 */
1147void 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
dd308bc3
ME
1154static 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;
69d29f9e 1158 struct dentry *dentry = F_DENTRY(file);
83b711cb 1159 ssize_t r;
83b711cb 1160
69d29f9e
NS
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);
83b711cb 1167 return r;
dd308bc3
ME
1168}
1169
71cd3c63
AN
1170static 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
00977a59 1187static const struct file_operations fops_blob = {
dd308bc3 1188 .read = read_file_blob,
71cd3c63 1189 .write = write_file_blob,
234e3405 1190 .open = simple_open,
6038f373 1191 .llseek = default_llseek,
dd308bc3
ME
1192};
1193
1194/**
71cd3c63
AN
1195 * debugfs_create_blob - create a debugfs file that is used to read and write
1196 * a binary blob
dd308bc3 1197 * @name: a pointer to a string containing the name of the file to create.
71cd3c63 1198 * @mode: the permission that the file should have
dd308bc3 1199 * @parent: a pointer to the parent dentry for this file. This should be a
6468b3af 1200 * directory dentry if set. If this parameter is %NULL, then the
dd308bc3
ME
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
71cd3c63 1207 * read from and written to.
dd308bc3
ME
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,
adc92dd4 1212 * you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be
9abb2499 1213 * returned.
dd308bc3 1214 *
adc92dd4 1215 * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will
9abb2499 1216 * be returned.
dd308bc3 1217 */
f4ae40a6 1218struct dentry *debugfs_create_blob(const char *name, umode_t mode,
dd308bc3
ME
1219 struct dentry *parent,
1220 struct debugfs_blob_wrapper *blob)
1221{
71cd3c63 1222 return debugfs_create_file_unsafe(name, mode & 0644, parent, blob, &fops_blob);
dd308bc3
ME
1223}
1224EXPORT_SYMBOL_GPL(debugfs_create_blob);
1a087c6a 1225
e05e279e
LT
1226static size_t u32_format_array(char *buf, size_t bufsize,
1227 u32 *array, int array_size)
9fe2a701
SV
1228{
1229 size_t ret = 0;
9fe2a701 1230
e05e279e 1231 while (--array_size >= 0) {
9fe2a701 1232 size_t len;
e05e279e 1233 char term = array_size ? ' ' : '\n';
9fe2a701 1234
e05e279e 1235 len = snprintf(buf, bufsize, "%u%c", *array++, term);
9fe2a701
SV
1236 ret += len;
1237
e05e279e
LT
1238 buf += len;
1239 bufsize -= len;
9fe2a701 1240 }
9fe2a701
SV
1241 return ret;
1242}
1243
36048853 1244static int u32_array_open(struct inode *inode, struct file *file)
9fe2a701 1245{
a2b992c8
JK
1246 struct debugfs_u32_array *data = inode->i_private;
1247 int size, elements = data->n_elements;
e05e279e
LT
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)
36048853 1258 return -ENOMEM;
e05e279e
LT
1259 buf[size] = 0;
1260
1261 file->private_data = buf;
a2b992c8 1262 u32_format_array(buf, size, data->array, data->n_elements);
e05e279e 1263
36048853
DR
1264 return nonseekable_open(inode, file);
1265}
9fe2a701 1266
36048853
DR
1267static 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);
9fe2a701
SV
1271
1272 return simple_read_from_buffer(buf, len, ppos,
1273 file->private_data, size);
1274}
1275
1276static int u32_array_release(struct inode *inode, struct file *file)
1277{
1278 kfree(file->private_data);
1279
1280 return 0;
1281}
1282
1283static 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,
9fe2a701
SV
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.
a2b992c8 1298 * @array: wrapper struct containing data pointer and size of the array.
9fe2a701
SV
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.
9fe2a701 1304 */
c9c2c27d 1305void debugfs_create_u32_array(const char *name, umode_t mode,
a2b992c8
JK
1306 struct dentry *parent,
1307 struct debugfs_u32_array *array)
9fe2a701 1308{
a2b992c8 1309 debugfs_create_file_unsafe(name, mode, parent, array, &u32_array_fops);
9fe2a701
SV
1310}
1311EXPORT_SYMBOL_GPL(debugfs_create_u32_array);
1312
3b85e4ab
HC
1313#ifdef CONFIG_HAS_IOMEM
1314
1a087c6a
AR
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
b5763acc 1325 * @nregs: the length of the above array
1a087c6a
AR
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 */
9761536e
JP
1336void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
1337 int nregs, void __iomem *base, char *prefix)
1a087c6a 1338{
9761536e 1339 int i;
1a087c6a
AR
1340
1341 for (i = 0; i < nregs; i++, regs++) {
1342 if (prefix)
9761536e
JP
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;
1a087c6a 1348 }
1a087c6a
AR
1349}
1350EXPORT_SYMBOL_GPL(debugfs_print_regs32);
1351
19029f3f 1352static int debugfs_regset32_show(struct seq_file *s, void *data)
1a087c6a
AR
1353{
1354 struct debugfs_regset32 *regset = s->private;
1355
30332eee
GU
1356 if (regset->dev)
1357 pm_runtime_get_sync(regset->dev);
1358
1a087c6a 1359 debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, "");
30332eee
GU
1360
1361 if (regset->dev)
1362 pm_runtime_put(regset->dev);
1363
1a087c6a
AR
1364 return 0;
1365}
1366
19029f3f 1367DEFINE_SHOW_ATTRIBUTE(debugfs_regset32);
1a087c6a
AR
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.
1a087c6a 1383 */
ae91c925
GKH
1384void debugfs_create_regset32(const char *name, umode_t mode,
1385 struct dentry *parent,
1386 struct debugfs_regset32 *regset)
1a087c6a 1387{
19029f3f 1388 debugfs_create_file(name, mode, parent, regset, &debugfs_regset32_fops);
1a087c6a
AR
1389}
1390EXPORT_SYMBOL_GPL(debugfs_create_regset32);
3b85e4ab
HC
1391
1392#endif /* CONFIG_HAS_IOMEM */
98210b7f
AS
1393
1394struct debugfs_devm_entry {
1395 int (*read)(struct seq_file *seq, void *data);
1396 struct device *dev;
1397};
1398
1399static 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
1406static 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 */
0d519cbf
GKH
1424void debugfs_create_devm_seqfile(struct device *dev, const char *name,
1425 struct dentry *parent,
1426 int (*read_fn)(struct seq_file *s, void *data))
98210b7f
AS
1427{
1428 struct debugfs_devm_entry *entry;
1429
1430 if (IS_ERR(parent))
0d519cbf 1431 return;
98210b7f
AS
1432
1433 entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL);
1434 if (!entry)
0d519cbf 1435 return;
98210b7f
AS
1436
1437 entry->read = read_fn;
1438 entry->dev = dev;
1439
0d519cbf
GKH
1440 debugfs_create_file(name, S_IRUGO, parent, entry,
1441 &debugfs_devm_entry_ops);
98210b7f
AS
1442}
1443EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile);