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