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