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