13bce94fdSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * file.c - part of debugfs, a tiny little debug file system 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Copyright (C) 2004 Greg Kroah-Hartman <[email protected]> 61da177e4SLinus Torvalds * Copyright (C) 2004 IBM Inc. 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * debugfs is for people to use instead of /proc or /sys. 9e1b4fc7aSMauro Carvalho Chehab * See Documentation/filesystems/ for more details. 101da177e4SLinus Torvalds */ 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds #include <linux/module.h> 131da177e4SLinus Torvalds #include <linux/fs.h> 141a087c6aSAlessandro Rubini #include <linux/seq_file.h> 151da177e4SLinus Torvalds #include <linux/pagemap.h> 161da177e4SLinus Torvalds #include <linux/debugfs.h> 1703e099fbSAlessandro Rubini #include <linux/io.h> 189fe2a701SSrivatsa Vaddagiri #include <linux/slab.h> 193a76e5e0SSeth Jennings #include <linux/atomic.h> 2098210b7fSArend van Spriel #include <linux/device.h> 2130332eeeSGeert Uytterhoeven #include <linux/pm_runtime.h> 22cfe39442SAl Viro #include <linux/poll.h> 235496197fSDavid Howells #include <linux/security.h> 249fd4dcecSNicolai Stange 259fd4dcecSNicolai Stange #include "internal.h" 261da177e4SLinus Torvalds 2749d200deSNicolai Stange struct poll_table_struct; 2849d200deSNicolai Stange 291da177e4SLinus Torvalds static ssize_t default_read_file(struct file *file, char __user *buf, 301da177e4SLinus Torvalds size_t count, loff_t *ppos) 311da177e4SLinus Torvalds { 321da177e4SLinus Torvalds return 0; 331da177e4SLinus Torvalds } 341da177e4SLinus Torvalds 351da177e4SLinus Torvalds static ssize_t default_write_file(struct file *file, const char __user *buf, 361da177e4SLinus Torvalds size_t count, loff_t *ppos) 371da177e4SLinus Torvalds { 381da177e4SLinus Torvalds return count; 391da177e4SLinus Torvalds } 401da177e4SLinus Torvalds 419fd4dcecSNicolai Stange const struct file_operations debugfs_noop_file_operations = { 421da177e4SLinus Torvalds .read = default_read_file, 431da177e4SLinus Torvalds .write = default_write_file, 44234e3405SStephen Boyd .open = simple_open, 456038f373SArnd Bergmann .llseek = noop_llseek, 461da177e4SLinus Torvalds }; 471da177e4SLinus Torvalds 489fd4dcecSNicolai Stange #define F_DENTRY(filp) ((filp)->f_path.dentry) 499fd4dcecSNicolai Stange 507c8d4698SNicolai Stange const struct file_operations *debugfs_real_fops(const struct file *filp) 517c8d4698SNicolai Stange { 527c8d4698SNicolai Stange struct debugfs_fsdata *fsd = F_DENTRY(filp)->d_fsdata; 53055ab8e3SNicolai Stange 547d39bc50SNicolai Stange if ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT) { 557d39bc50SNicolai Stange /* 567d39bc50SNicolai Stange * Urgh, we've been called w/o a protecting 577d39bc50SNicolai Stange * debugfs_file_get(). 587d39bc50SNicolai Stange */ 597d39bc50SNicolai Stange WARN_ON(1); 607d39bc50SNicolai Stange return NULL; 617d39bc50SNicolai Stange } 627d39bc50SNicolai Stange 637c8d4698SNicolai Stange return fsd->real_fops; 647c8d4698SNicolai Stange } 657c8d4698SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_real_fops); 667c8d4698SNicolai Stange 67e9117a5aSNicolai Stange /** 68e9117a5aSNicolai Stange * debugfs_file_get - mark the beginning of file data access 69e9117a5aSNicolai Stange * @dentry: the dentry object whose data is being accessed. 70e9117a5aSNicolai Stange * 71e9117a5aSNicolai Stange * Up to a matching call to debugfs_file_put(), any successive call 72e9117a5aSNicolai Stange * into the file removing functions debugfs_remove() and 73e9117a5aSNicolai Stange * debugfs_remove_recursive() will block. Since associated private 74e9117a5aSNicolai Stange * file data may only get freed after a successful return of any of 75e9117a5aSNicolai Stange * the removal functions, you may safely access it after a successful 76e9117a5aSNicolai Stange * call to debugfs_file_get() without worrying about lifetime issues. 77e9117a5aSNicolai Stange * 78e9117a5aSNicolai Stange * If -%EIO is returned, the file has already been removed and thus, 79e9117a5aSNicolai Stange * it is not safe to access any of its data. If, on the other hand, 80e9117a5aSNicolai Stange * it is allowed to access the file data, zero is returned. 81e9117a5aSNicolai Stange */ 82e9117a5aSNicolai Stange int debugfs_file_get(struct dentry *dentry) 83e9117a5aSNicolai Stange { 847d39bc50SNicolai Stange struct debugfs_fsdata *fsd; 857d39bc50SNicolai Stange void *d_fsd; 86e9117a5aSNicolai Stange 870ed04a18SJohannes Berg /* 880ed04a18SJohannes Berg * This could only happen if some debugfs user erroneously calls 890ed04a18SJohannes Berg * debugfs_file_get() on a dentry that isn't even a file, let 900ed04a18SJohannes Berg * them know about it. 910ed04a18SJohannes Berg */ 920ed04a18SJohannes Berg if (WARN_ON(!d_is_reg(dentry))) 930ed04a18SJohannes Berg return -EINVAL; 940ed04a18SJohannes Berg 957d39bc50SNicolai Stange d_fsd = READ_ONCE(dentry->d_fsdata); 967d39bc50SNicolai Stange if (!((unsigned long)d_fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT)) { 977d39bc50SNicolai Stange fsd = d_fsd; 987d39bc50SNicolai Stange } else { 997d39bc50SNicolai Stange fsd = kmalloc(sizeof(*fsd), GFP_KERNEL); 1007d39bc50SNicolai Stange if (!fsd) 1017d39bc50SNicolai Stange return -ENOMEM; 1027d39bc50SNicolai Stange 1037d39bc50SNicolai Stange fsd->real_fops = (void *)((unsigned long)d_fsd & 1047d39bc50SNicolai Stange ~DEBUGFS_FSDATA_IS_REAL_FOPS_BIT); 1057d39bc50SNicolai Stange refcount_set(&fsd->active_users, 1); 1067d39bc50SNicolai Stange init_completion(&fsd->active_users_drained); 107*159f5bdaSJohannes Berg INIT_LIST_HEAD(&fsd->cancellations); 108*159f5bdaSJohannes Berg mutex_init(&fsd->cancellations_mtx); 109*159f5bdaSJohannes Berg 1107d39bc50SNicolai Stange if (cmpxchg(&dentry->d_fsdata, d_fsd, fsd) != d_fsd) { 111*159f5bdaSJohannes Berg mutex_destroy(&fsd->cancellations_mtx); 1127d39bc50SNicolai Stange kfree(fsd); 1137d39bc50SNicolai Stange fsd = READ_ONCE(dentry->d_fsdata); 1147d39bc50SNicolai Stange } 1157d39bc50SNicolai Stange } 1167d39bc50SNicolai Stange 1177d39bc50SNicolai Stange /* 1187d39bc50SNicolai Stange * In case of a successful cmpxchg() above, this check is 1197d39bc50SNicolai Stange * strictly necessary and must follow it, see the comment in 1207d39bc50SNicolai Stange * __debugfs_remove_file(). 1217d39bc50SNicolai Stange * OTOH, if the cmpxchg() hasn't been executed or wasn't 1227d39bc50SNicolai Stange * successful, this serves the purpose of not starving 1237d39bc50SNicolai Stange * removers. 1247d39bc50SNicolai Stange */ 125e9117a5aSNicolai Stange if (d_unlinked(dentry)) 126e9117a5aSNicolai Stange return -EIO; 127e9117a5aSNicolai Stange 128e9117a5aSNicolai Stange if (!refcount_inc_not_zero(&fsd->active_users)) 129e9117a5aSNicolai Stange return -EIO; 130e9117a5aSNicolai Stange 131e9117a5aSNicolai Stange return 0; 132e9117a5aSNicolai Stange } 133e9117a5aSNicolai Stange EXPORT_SYMBOL_GPL(debugfs_file_get); 134e9117a5aSNicolai Stange 135e9117a5aSNicolai Stange /** 136e9117a5aSNicolai Stange * debugfs_file_put - mark the end of file data access 137e9117a5aSNicolai Stange * @dentry: the dentry object formerly passed to 138e9117a5aSNicolai Stange * debugfs_file_get(). 139e9117a5aSNicolai Stange * 140e9117a5aSNicolai Stange * Allow any ongoing concurrent call into debugfs_remove() or 141e9117a5aSNicolai Stange * debugfs_remove_recursive() blocked by a former call to 142e9117a5aSNicolai Stange * debugfs_file_get() to proceed and return to its caller. 143e9117a5aSNicolai Stange */ 144e9117a5aSNicolai Stange void debugfs_file_put(struct dentry *dentry) 145e9117a5aSNicolai Stange { 1467d39bc50SNicolai Stange struct debugfs_fsdata *fsd = READ_ONCE(dentry->d_fsdata); 147e9117a5aSNicolai Stange 148e9117a5aSNicolai Stange if (refcount_dec_and_test(&fsd->active_users)) 149e9117a5aSNicolai Stange complete(&fsd->active_users_drained); 150e9117a5aSNicolai Stange } 151e9117a5aSNicolai Stange EXPORT_SYMBOL_GPL(debugfs_file_put); 152e9117a5aSNicolai Stange 1538c88a474SJohannes Berg /** 1548c88a474SJohannes Berg * debugfs_enter_cancellation - enter a debugfs cancellation 1558c88a474SJohannes Berg * @file: the file being accessed 1568c88a474SJohannes Berg * @cancellation: the cancellation object, the cancel callback 1578c88a474SJohannes Berg * inside of it must be initialized 1588c88a474SJohannes Berg * 1598c88a474SJohannes Berg * When a debugfs file is removed it needs to wait for all active 1608c88a474SJohannes Berg * operations to complete. However, the operation itself may need 1618c88a474SJohannes Berg * to wait for hardware or completion of some asynchronous process 1628c88a474SJohannes Berg * or similar. As such, it may need to be cancelled to avoid long 1638c88a474SJohannes Berg * waits or even deadlocks. 1648c88a474SJohannes Berg * 1658c88a474SJohannes Berg * This function can be used inside a debugfs handler that may 1668c88a474SJohannes Berg * need to be cancelled. As soon as this function is called, the 1678c88a474SJohannes Berg * cancellation's 'cancel' callback may be called, at which point 1688c88a474SJohannes Berg * the caller should proceed to call debugfs_leave_cancellation() 1698c88a474SJohannes Berg * and leave the debugfs handler function as soon as possible. 1708c88a474SJohannes Berg * Note that the 'cancel' callback is only ever called in the 1718c88a474SJohannes Berg * context of some kind of debugfs_remove(). 1728c88a474SJohannes Berg * 1738c88a474SJohannes Berg * This function must be paired with debugfs_leave_cancellation(). 1748c88a474SJohannes Berg */ 1758c88a474SJohannes Berg void debugfs_enter_cancellation(struct file *file, 1768c88a474SJohannes Berg struct debugfs_cancellation *cancellation) 1778c88a474SJohannes Berg { 1788c88a474SJohannes Berg struct debugfs_fsdata *fsd; 1798c88a474SJohannes Berg struct dentry *dentry = F_DENTRY(file); 1808c88a474SJohannes Berg 1818c88a474SJohannes Berg INIT_LIST_HEAD(&cancellation->list); 1828c88a474SJohannes Berg 1838c88a474SJohannes Berg if (WARN_ON(!d_is_reg(dentry))) 1848c88a474SJohannes Berg return; 1858c88a474SJohannes Berg 1868c88a474SJohannes Berg if (WARN_ON(!cancellation->cancel)) 1878c88a474SJohannes Berg return; 1888c88a474SJohannes Berg 1898c88a474SJohannes Berg fsd = READ_ONCE(dentry->d_fsdata); 1908c88a474SJohannes Berg if (WARN_ON(!fsd || 1918c88a474SJohannes Berg ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT))) 1928c88a474SJohannes Berg return; 1938c88a474SJohannes Berg 1948c88a474SJohannes Berg mutex_lock(&fsd->cancellations_mtx); 1958c88a474SJohannes Berg list_add(&cancellation->list, &fsd->cancellations); 1968c88a474SJohannes Berg mutex_unlock(&fsd->cancellations_mtx); 1978c88a474SJohannes Berg 1988c88a474SJohannes Berg /* if we're already removing wake it up to cancel */ 1998c88a474SJohannes Berg if (d_unlinked(dentry)) 2008c88a474SJohannes Berg complete(&fsd->active_users_drained); 2018c88a474SJohannes Berg } 2028c88a474SJohannes Berg EXPORT_SYMBOL_GPL(debugfs_enter_cancellation); 2038c88a474SJohannes Berg 2048c88a474SJohannes Berg /** 2058c88a474SJohannes Berg * debugfs_leave_cancellation - leave cancellation section 2068c88a474SJohannes Berg * @file: the file being accessed 2078c88a474SJohannes Berg * @cancellation: the cancellation previously registered with 2088c88a474SJohannes Berg * debugfs_enter_cancellation() 2098c88a474SJohannes Berg * 2108c88a474SJohannes Berg * See the documentation of debugfs_enter_cancellation(). 2118c88a474SJohannes Berg */ 2128c88a474SJohannes Berg void debugfs_leave_cancellation(struct file *file, 2138c88a474SJohannes Berg struct debugfs_cancellation *cancellation) 2148c88a474SJohannes Berg { 2158c88a474SJohannes Berg struct debugfs_fsdata *fsd; 2168c88a474SJohannes Berg struct dentry *dentry = F_DENTRY(file); 2178c88a474SJohannes Berg 2188c88a474SJohannes Berg if (WARN_ON(!d_is_reg(dentry))) 2198c88a474SJohannes Berg return; 2208c88a474SJohannes Berg 2218c88a474SJohannes Berg fsd = READ_ONCE(dentry->d_fsdata); 2228c88a474SJohannes Berg if (WARN_ON(!fsd || 2238c88a474SJohannes Berg ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT))) 2248c88a474SJohannes Berg return; 2258c88a474SJohannes Berg 2268c88a474SJohannes Berg mutex_lock(&fsd->cancellations_mtx); 2278c88a474SJohannes Berg if (!list_empty(&cancellation->list)) 2288c88a474SJohannes Berg list_del(&cancellation->list); 2298c88a474SJohannes Berg mutex_unlock(&fsd->cancellations_mtx); 2308c88a474SJohannes Berg } 2318c88a474SJohannes Berg EXPORT_SYMBOL_GPL(debugfs_leave_cancellation); 2328c88a474SJohannes Berg 2335496197fSDavid Howells /* 2345496197fSDavid Howells * Only permit access to world-readable files when the kernel is locked down. 2355496197fSDavid Howells * We also need to exclude any file that has ways to write or alter it as root 2365496197fSDavid Howells * can bypass the permissions check. 2375496197fSDavid Howells */ 238a37f4958SEric Snowberg static int debugfs_locked_down(struct inode *inode, 2395496197fSDavid Howells struct file *filp, 2405496197fSDavid Howells const struct file_operations *real_fops) 2415496197fSDavid Howells { 242358fcf5dSMichal Suchanek if ((inode->i_mode & 07777 & ~0444) == 0 && 2435496197fSDavid Howells !(filp->f_mode & FMODE_WRITE) && 2445496197fSDavid Howells !real_fops->unlocked_ioctl && 2455496197fSDavid Howells !real_fops->compat_ioctl && 2465496197fSDavid Howells !real_fops->mmap) 247a37f4958SEric Snowberg return 0; 2485496197fSDavid Howells 249a37f4958SEric Snowberg if (security_locked_down(LOCKDOWN_DEBUGFS)) 250a37f4958SEric Snowberg return -EPERM; 251a37f4958SEric Snowberg 252a37f4958SEric Snowberg return 0; 2535496197fSDavid Howells } 2545496197fSDavid Howells 2559fd4dcecSNicolai Stange static int open_proxy_open(struct inode *inode, struct file *filp) 2569fd4dcecSNicolai Stange { 25769d29f9eSNicolai Stange struct dentry *dentry = F_DENTRY(filp); 2589fd4dcecSNicolai Stange const struct file_operations *real_fops = NULL; 2597d39bc50SNicolai Stange int r; 2609fd4dcecSNicolai Stange 2617d39bc50SNicolai Stange r = debugfs_file_get(dentry); 2627d39bc50SNicolai Stange if (r) 2637d39bc50SNicolai Stange return r == -EIO ? -ENOENT : r; 2649fd4dcecSNicolai Stange 26586f0e067SChristian Lamparter real_fops = debugfs_real_fops(filp); 2665496197fSDavid Howells 267a37f4958SEric Snowberg r = debugfs_locked_down(inode, filp, real_fops); 2685496197fSDavid Howells if (r) 2695496197fSDavid Howells goto out; 2705496197fSDavid Howells 271275678e7STaehee Yoo if (!fops_get(real_fops)) { 272e3b9fc7eSVladis Dronov #ifdef CONFIG_MODULES 273275678e7STaehee Yoo if (real_fops->owner && 274112cedc8SSven Eckelmann real_fops->owner->state == MODULE_STATE_GOING) { 275112cedc8SSven Eckelmann r = -ENXIO; 276275678e7STaehee Yoo goto out; 277112cedc8SSven Eckelmann } 278275678e7STaehee Yoo #endif 279275678e7STaehee Yoo 2809fd4dcecSNicolai Stange /* Huh? Module did not clean up after itself at exit? */ 2819fd4dcecSNicolai Stange WARN(1, "debugfs file owner did not clean up at exit: %pd", 2829fd4dcecSNicolai Stange dentry); 2839fd4dcecSNicolai Stange r = -ENXIO; 2849fd4dcecSNicolai Stange goto out; 2859fd4dcecSNicolai Stange } 2869fd4dcecSNicolai Stange replace_fops(filp, real_fops); 2879fd4dcecSNicolai Stange 2889fd4dcecSNicolai Stange if (real_fops->open) 2899fd4dcecSNicolai Stange r = real_fops->open(inode, filp); 2909fd4dcecSNicolai Stange 2919fd4dcecSNicolai Stange out: 29269d29f9eSNicolai Stange debugfs_file_put(dentry); 2939fd4dcecSNicolai Stange return r; 2949fd4dcecSNicolai Stange } 2959fd4dcecSNicolai Stange 2969fd4dcecSNicolai Stange const struct file_operations debugfs_open_proxy_file_operations = { 2979fd4dcecSNicolai Stange .open = open_proxy_open, 2989fd4dcecSNicolai Stange }; 2999fd4dcecSNicolai Stange 30049d200deSNicolai Stange #define PROTO(args...) args 30149d200deSNicolai Stange #define ARGS(args...) args 30249d200deSNicolai Stange 30349d200deSNicolai Stange #define FULL_PROXY_FUNC(name, ret_type, filp, proto, args) \ 30449d200deSNicolai Stange static ret_type full_proxy_ ## name(proto) \ 30549d200deSNicolai Stange { \ 30669d29f9eSNicolai Stange struct dentry *dentry = F_DENTRY(filp); \ 307154b9d75SNicolai Stange const struct file_operations *real_fops; \ 30849d200deSNicolai Stange ret_type r; \ 30949d200deSNicolai Stange \ 31069d29f9eSNicolai Stange r = debugfs_file_get(dentry); \ 31169d29f9eSNicolai Stange if (unlikely(r)) \ 31269d29f9eSNicolai Stange return r; \ 313154b9d75SNicolai Stange real_fops = debugfs_real_fops(filp); \ 31449d200deSNicolai Stange r = real_fops->name(args); \ 31569d29f9eSNicolai Stange debugfs_file_put(dentry); \ 31649d200deSNicolai Stange return r; \ 31749d200deSNicolai Stange } 31849d200deSNicolai Stange 31949d200deSNicolai Stange FULL_PROXY_FUNC(llseek, loff_t, filp, 32049d200deSNicolai Stange PROTO(struct file *filp, loff_t offset, int whence), 32149d200deSNicolai Stange ARGS(filp, offset, whence)); 32249d200deSNicolai Stange 32349d200deSNicolai Stange FULL_PROXY_FUNC(read, ssize_t, filp, 32449d200deSNicolai Stange PROTO(struct file *filp, char __user *buf, size_t size, 32549d200deSNicolai Stange loff_t *ppos), 32649d200deSNicolai Stange ARGS(filp, buf, size, ppos)); 32749d200deSNicolai Stange 32849d200deSNicolai Stange FULL_PROXY_FUNC(write, ssize_t, filp, 32949d200deSNicolai Stange PROTO(struct file *filp, const char __user *buf, size_t size, 33049d200deSNicolai Stange loff_t *ppos), 33149d200deSNicolai Stange ARGS(filp, buf, size, ppos)); 33249d200deSNicolai Stange 33349d200deSNicolai Stange FULL_PROXY_FUNC(unlocked_ioctl, long, filp, 33449d200deSNicolai Stange PROTO(struct file *filp, unsigned int cmd, unsigned long arg), 33549d200deSNicolai Stange ARGS(filp, cmd, arg)); 33649d200deSNicolai Stange 337076ccb76SAl Viro static __poll_t full_proxy_poll(struct file *filp, 33849d200deSNicolai Stange struct poll_table_struct *wait) 33949d200deSNicolai Stange { 34069d29f9eSNicolai Stange struct dentry *dentry = F_DENTRY(filp); 341e6c8adcaSAl Viro __poll_t r = 0; 342154b9d75SNicolai Stange const struct file_operations *real_fops; 34349d200deSNicolai Stange 34469d29f9eSNicolai Stange if (debugfs_file_get(dentry)) 345a9a08845SLinus Torvalds return EPOLLHUP; 34649d200deSNicolai Stange 347154b9d75SNicolai Stange real_fops = debugfs_real_fops(filp); 34849d200deSNicolai Stange r = real_fops->poll(filp, wait); 34969d29f9eSNicolai Stange debugfs_file_put(dentry); 35049d200deSNicolai Stange return r; 35149d200deSNicolai Stange } 35249d200deSNicolai Stange 35349d200deSNicolai Stange static int full_proxy_release(struct inode *inode, struct file *filp) 35449d200deSNicolai Stange { 35549d200deSNicolai Stange const struct dentry *dentry = F_DENTRY(filp); 35686f0e067SChristian Lamparter const struct file_operations *real_fops = debugfs_real_fops(filp); 35749d200deSNicolai Stange const struct file_operations *proxy_fops = filp->f_op; 35849d200deSNicolai Stange int r = 0; 35949d200deSNicolai Stange 36049d200deSNicolai Stange /* 36149d200deSNicolai Stange * We must not protect this against removal races here: the 36249d200deSNicolai Stange * original releaser should be called unconditionally in order 36349d200deSNicolai Stange * not to leak any resources. Releasers must not assume that 36449d200deSNicolai Stange * ->i_private is still being meaningful here. 36549d200deSNicolai Stange */ 36649d200deSNicolai Stange if (real_fops->release) 36749d200deSNicolai Stange r = real_fops->release(inode, filp); 36849d200deSNicolai Stange 36949d200deSNicolai Stange replace_fops(filp, d_inode(dentry)->i_fop); 370c80a67bdSXu Wang kfree(proxy_fops); 37149d200deSNicolai Stange fops_put(real_fops); 372a1a9e5d2SEric Engestrom return r; 37349d200deSNicolai Stange } 37449d200deSNicolai Stange 37549d200deSNicolai Stange static void __full_proxy_fops_init(struct file_operations *proxy_fops, 37649d200deSNicolai Stange const struct file_operations *real_fops) 37749d200deSNicolai Stange { 37849d200deSNicolai Stange proxy_fops->release = full_proxy_release; 37949d200deSNicolai Stange if (real_fops->llseek) 38049d200deSNicolai Stange proxy_fops->llseek = full_proxy_llseek; 38149d200deSNicolai Stange if (real_fops->read) 38249d200deSNicolai Stange proxy_fops->read = full_proxy_read; 38349d200deSNicolai Stange if (real_fops->write) 38449d200deSNicolai Stange proxy_fops->write = full_proxy_write; 38549d200deSNicolai Stange if (real_fops->poll) 38649d200deSNicolai Stange proxy_fops->poll = full_proxy_poll; 38749d200deSNicolai Stange if (real_fops->unlocked_ioctl) 38849d200deSNicolai Stange proxy_fops->unlocked_ioctl = full_proxy_unlocked_ioctl; 38949d200deSNicolai Stange } 39049d200deSNicolai Stange 39149d200deSNicolai Stange static int full_proxy_open(struct inode *inode, struct file *filp) 39249d200deSNicolai Stange { 39369d29f9eSNicolai Stange struct dentry *dentry = F_DENTRY(filp); 39449d200deSNicolai Stange const struct file_operations *real_fops = NULL; 39549d200deSNicolai Stange struct file_operations *proxy_fops = NULL; 3967d39bc50SNicolai Stange int r; 39749d200deSNicolai Stange 3987d39bc50SNicolai Stange r = debugfs_file_get(dentry); 3997d39bc50SNicolai Stange if (r) 4007d39bc50SNicolai Stange return r == -EIO ? -ENOENT : r; 40149d200deSNicolai Stange 40286f0e067SChristian Lamparter real_fops = debugfs_real_fops(filp); 4035496197fSDavid Howells 404a37f4958SEric Snowberg r = debugfs_locked_down(inode, filp, real_fops); 4055496197fSDavid Howells if (r) 4065496197fSDavid Howells goto out; 4075496197fSDavid Howells 408275678e7STaehee Yoo if (!fops_get(real_fops)) { 409e3b9fc7eSVladis Dronov #ifdef CONFIG_MODULES 410275678e7STaehee Yoo if (real_fops->owner && 411112cedc8SSven Eckelmann real_fops->owner->state == MODULE_STATE_GOING) { 412112cedc8SSven Eckelmann r = -ENXIO; 413275678e7STaehee Yoo goto out; 414112cedc8SSven Eckelmann } 415275678e7STaehee Yoo #endif 416275678e7STaehee Yoo 41749d200deSNicolai Stange /* Huh? Module did not cleanup after itself at exit? */ 41849d200deSNicolai Stange WARN(1, "debugfs file owner did not clean up at exit: %pd", 41949d200deSNicolai Stange dentry); 42049d200deSNicolai Stange r = -ENXIO; 42149d200deSNicolai Stange goto out; 42249d200deSNicolai Stange } 42349d200deSNicolai Stange 42449d200deSNicolai Stange proxy_fops = kzalloc(sizeof(*proxy_fops), GFP_KERNEL); 42549d200deSNicolai Stange if (!proxy_fops) { 42649d200deSNicolai Stange r = -ENOMEM; 42749d200deSNicolai Stange goto free_proxy; 42849d200deSNicolai Stange } 42949d200deSNicolai Stange __full_proxy_fops_init(proxy_fops, real_fops); 43049d200deSNicolai Stange replace_fops(filp, proxy_fops); 43149d200deSNicolai Stange 43249d200deSNicolai Stange if (real_fops->open) { 43349d200deSNicolai Stange r = real_fops->open(inode, filp); 434b10e3e90SNicolai Stange if (r) { 435b10e3e90SNicolai Stange replace_fops(filp, d_inode(dentry)->i_fop); 436b10e3e90SNicolai Stange goto free_proxy; 437b10e3e90SNicolai Stange } else if (filp->f_op != proxy_fops) { 43849d200deSNicolai Stange /* No protection against file removal anymore. */ 43949d200deSNicolai Stange WARN(1, "debugfs file owner replaced proxy fops: %pd", 44049d200deSNicolai Stange dentry); 44149d200deSNicolai Stange goto free_proxy; 44249d200deSNicolai Stange } 44349d200deSNicolai Stange } 44449d200deSNicolai Stange 44549d200deSNicolai Stange goto out; 44649d200deSNicolai Stange free_proxy: 44749d200deSNicolai Stange kfree(proxy_fops); 44849d200deSNicolai Stange fops_put(real_fops); 44949d200deSNicolai Stange out: 45069d29f9eSNicolai Stange debugfs_file_put(dentry); 45149d200deSNicolai Stange return r; 45249d200deSNicolai Stange } 45349d200deSNicolai Stange 45449d200deSNicolai Stange const struct file_operations debugfs_full_proxy_file_operations = { 45549d200deSNicolai Stange .open = full_proxy_open, 45649d200deSNicolai Stange }; 45749d200deSNicolai Stange 458c6468808SNicolai Stange ssize_t debugfs_attr_read(struct file *file, char __user *buf, 459c6468808SNicolai Stange size_t len, loff_t *ppos) 460c6468808SNicolai Stange { 46169d29f9eSNicolai Stange struct dentry *dentry = F_DENTRY(file); 462c6468808SNicolai Stange ssize_t ret; 463c6468808SNicolai Stange 46469d29f9eSNicolai Stange ret = debugfs_file_get(dentry); 46569d29f9eSNicolai Stange if (unlikely(ret)) 46669d29f9eSNicolai Stange return ret; 467c6468808SNicolai Stange ret = simple_attr_read(file, buf, len, ppos); 46869d29f9eSNicolai Stange debugfs_file_put(dentry); 469c6468808SNicolai Stange return ret; 470c6468808SNicolai Stange } 471c6468808SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_attr_read); 472c6468808SNicolai Stange 473d472cf79SAkinobu Mita static ssize_t debugfs_attr_write_xsigned(struct file *file, const char __user *buf, 474d472cf79SAkinobu Mita size_t len, loff_t *ppos, bool is_signed) 475c6468808SNicolai Stange { 47669d29f9eSNicolai Stange struct dentry *dentry = F_DENTRY(file); 477c6468808SNicolai Stange ssize_t ret; 478c6468808SNicolai Stange 47969d29f9eSNicolai Stange ret = debugfs_file_get(dentry); 48069d29f9eSNicolai Stange if (unlikely(ret)) 48169d29f9eSNicolai Stange return ret; 482d472cf79SAkinobu Mita if (is_signed) 483d472cf79SAkinobu Mita ret = simple_attr_write_signed(file, buf, len, ppos); 484d472cf79SAkinobu Mita else 485c6468808SNicolai Stange ret = simple_attr_write(file, buf, len, ppos); 48669d29f9eSNicolai Stange debugfs_file_put(dentry); 487c6468808SNicolai Stange return ret; 488c6468808SNicolai Stange } 489d472cf79SAkinobu Mita 490d472cf79SAkinobu Mita ssize_t debugfs_attr_write(struct file *file, const char __user *buf, 491d472cf79SAkinobu Mita size_t len, loff_t *ppos) 492d472cf79SAkinobu Mita { 493d472cf79SAkinobu Mita return debugfs_attr_write_xsigned(file, buf, len, ppos, false); 494d472cf79SAkinobu Mita } 495c6468808SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_attr_write); 496c6468808SNicolai Stange 497d472cf79SAkinobu Mita ssize_t debugfs_attr_write_signed(struct file *file, const char __user *buf, 498d472cf79SAkinobu Mita size_t len, loff_t *ppos) 499d472cf79SAkinobu Mita { 500d472cf79SAkinobu Mita return debugfs_attr_write_xsigned(file, buf, len, ppos, true); 501d472cf79SAkinobu Mita } 502d472cf79SAkinobu Mita EXPORT_SYMBOL_GPL(debugfs_attr_write_signed); 503d472cf79SAkinobu Mita 5044909f168SNicolai Stange static struct dentry *debugfs_create_mode_unsafe(const char *name, umode_t mode, 5054909f168SNicolai Stange struct dentry *parent, void *value, 5064909f168SNicolai Stange const struct file_operations *fops, 5074909f168SNicolai Stange const struct file_operations *fops_ro, 5084909f168SNicolai Stange const struct file_operations *fops_wo) 5094909f168SNicolai Stange { 5104909f168SNicolai Stange /* if there are no write bits set, make read only */ 5114909f168SNicolai Stange if (!(mode & S_IWUGO)) 5124909f168SNicolai Stange return debugfs_create_file_unsafe(name, mode, parent, value, 5134909f168SNicolai Stange fops_ro); 5144909f168SNicolai Stange /* if there are no read bits set, make write only */ 5154909f168SNicolai Stange if (!(mode & S_IRUGO)) 5164909f168SNicolai Stange return debugfs_create_file_unsafe(name, mode, parent, value, 5174909f168SNicolai Stange fops_wo); 5184909f168SNicolai Stange 5194909f168SNicolai Stange return debugfs_create_file_unsafe(name, mode, parent, value, fops); 5204909f168SNicolai Stange } 5214909f168SNicolai Stange 5228b88b099SChristoph Hellwig static int debugfs_u8_set(void *data, u64 val) 523acaefc25SArnd Bergmann { 524acaefc25SArnd Bergmann *(u8 *)data = val; 5258b88b099SChristoph Hellwig return 0; 526acaefc25SArnd Bergmann } 5278b88b099SChristoph Hellwig static int debugfs_u8_get(void *data, u64 *val) 528acaefc25SArnd Bergmann { 5298b88b099SChristoph Hellwig *val = *(u8 *)data; 5308b88b099SChristoph Hellwig return 0; 531acaefc25SArnd Bergmann } 5324909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n"); 5334909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n"); 5344909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n"); 5351da177e4SLinus Torvalds 5361da177e4SLinus Torvalds /** 5376468b3afSRandy Dunlap * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value 5381da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the file to create. 5391da177e4SLinus Torvalds * @mode: the permission that the file should have 5401da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 5416468b3afSRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 5421da177e4SLinus Torvalds * file will be created in the root of the debugfs filesystem. 5431da177e4SLinus Torvalds * @value: a pointer to the variable that the file should read to and write 5441da177e4SLinus Torvalds * from. 5451da177e4SLinus Torvalds * 5461da177e4SLinus Torvalds * This function creates a file in debugfs with the given name that 5471da177e4SLinus Torvalds * contains the value of the variable @value. If the @mode variable is so 5481da177e4SLinus Torvalds * set, it can be read from, and written to. 5491da177e4SLinus Torvalds */ 5509655ac4aSGreg Kroah-Hartman void debugfs_create_u8(const char *name, umode_t mode, struct dentry *parent, 5519655ac4aSGreg Kroah-Hartman u8 *value) 5521da177e4SLinus Torvalds { 5539655ac4aSGreg Kroah-Hartman debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u8, 554b97f6799SStephen Boyd &fops_u8_ro, &fops_u8_wo); 5551da177e4SLinus Torvalds } 5561da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u8); 5571da177e4SLinus Torvalds 5588b88b099SChristoph Hellwig static int debugfs_u16_set(void *data, u64 val) 559acaefc25SArnd Bergmann { 560acaefc25SArnd Bergmann *(u16 *)data = val; 5618b88b099SChristoph Hellwig return 0; 562acaefc25SArnd Bergmann } 5638b88b099SChristoph Hellwig static int debugfs_u16_get(void *data, u64 *val) 564acaefc25SArnd Bergmann { 5658b88b099SChristoph Hellwig *val = *(u16 *)data; 5668b88b099SChristoph Hellwig return 0; 567acaefc25SArnd Bergmann } 5684909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n"); 5694909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n"); 5704909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n"); 571acaefc25SArnd Bergmann 5721da177e4SLinus Torvalds /** 5736468b3afSRandy Dunlap * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value 5741da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the file to create. 5751da177e4SLinus Torvalds * @mode: the permission that the file should have 5761da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 5776468b3afSRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 5781da177e4SLinus Torvalds * file will be created in the root of the debugfs filesystem. 5791da177e4SLinus Torvalds * @value: a pointer to the variable that the file should read to and write 5801da177e4SLinus Torvalds * from. 5811da177e4SLinus Torvalds * 5821da177e4SLinus Torvalds * This function creates a file in debugfs with the given name that 5831da177e4SLinus Torvalds * contains the value of the variable @value. If the @mode variable is so 5841da177e4SLinus Torvalds * set, it can be read from, and written to. 5851da177e4SLinus Torvalds */ 586313f5dbbSGreg Kroah-Hartman void debugfs_create_u16(const char *name, umode_t mode, struct dentry *parent, 587313f5dbbSGreg Kroah-Hartman u16 *value) 5881da177e4SLinus Torvalds { 589313f5dbbSGreg Kroah-Hartman debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u16, 590b97f6799SStephen Boyd &fops_u16_ro, &fops_u16_wo); 5911da177e4SLinus Torvalds } 5921da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u16); 5931da177e4SLinus Torvalds 5948b88b099SChristoph Hellwig static int debugfs_u32_set(void *data, u64 val) 595acaefc25SArnd Bergmann { 596acaefc25SArnd Bergmann *(u32 *)data = val; 5978b88b099SChristoph Hellwig return 0; 598acaefc25SArnd Bergmann } 5998b88b099SChristoph Hellwig static int debugfs_u32_get(void *data, u64 *val) 600acaefc25SArnd Bergmann { 6018b88b099SChristoph Hellwig *val = *(u32 *)data; 6028b88b099SChristoph Hellwig return 0; 603acaefc25SArnd Bergmann } 6044909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n"); 6054909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n"); 6064909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n"); 607acaefc25SArnd Bergmann 6081da177e4SLinus Torvalds /** 6096468b3afSRandy Dunlap * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value 6101da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the file to create. 6111da177e4SLinus Torvalds * @mode: the permission that the file should have 6121da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 6136468b3afSRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 6141da177e4SLinus Torvalds * file will be created in the root of the debugfs filesystem. 6151da177e4SLinus Torvalds * @value: a pointer to the variable that the file should read to and write 6161da177e4SLinus Torvalds * from. 6171da177e4SLinus Torvalds * 6181da177e4SLinus Torvalds * This function creates a file in debugfs with the given name that 6191da177e4SLinus Torvalds * contains the value of the variable @value. If the @mode variable is so 6201da177e4SLinus Torvalds * set, it can be read from, and written to. 6211da177e4SLinus Torvalds */ 6222b07021aSGreg Kroah-Hartman void debugfs_create_u32(const char *name, umode_t mode, struct dentry *parent, 6232b07021aSGreg Kroah-Hartman u32 *value) 6241da177e4SLinus Torvalds { 6252b07021aSGreg Kroah-Hartman debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u32, 626b97f6799SStephen Boyd &fops_u32_ro, &fops_u32_wo); 6271da177e4SLinus Torvalds } 6281da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_u32); 6291da177e4SLinus Torvalds 6308b88b099SChristoph Hellwig static int debugfs_u64_set(void *data, u64 val) 6318447891fSMichael Ellerman { 6328447891fSMichael Ellerman *(u64 *)data = val; 6338b88b099SChristoph Hellwig return 0; 6348447891fSMichael Ellerman } 6358447891fSMichael Ellerman 6368b88b099SChristoph Hellwig static int debugfs_u64_get(void *data, u64 *val) 6378447891fSMichael Ellerman { 6388b88b099SChristoph Hellwig *val = *(u64 *)data; 6398b88b099SChristoph Hellwig return 0; 6408447891fSMichael Ellerman } 6414909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n"); 6424909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n"); 6434909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n"); 6448447891fSMichael Ellerman 6458447891fSMichael Ellerman /** 6468447891fSMichael Ellerman * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value 6478447891fSMichael Ellerman * @name: a pointer to a string containing the name of the file to create. 6488447891fSMichael Ellerman * @mode: the permission that the file should have 6498447891fSMichael Ellerman * @parent: a pointer to the parent dentry for this file. This should be a 6508447891fSMichael Ellerman * directory dentry if set. If this parameter is %NULL, then the 6518447891fSMichael Ellerman * file will be created in the root of the debugfs filesystem. 6528447891fSMichael Ellerman * @value: a pointer to the variable that the file should read to and write 6538447891fSMichael Ellerman * from. 6548447891fSMichael Ellerman * 6558447891fSMichael Ellerman * This function creates a file in debugfs with the given name that 6568447891fSMichael Ellerman * contains the value of the variable @value. If the @mode variable is so 6578447891fSMichael Ellerman * set, it can be read from, and written to. 6588447891fSMichael Ellerman */ 659ad26221fSGreg Kroah-Hartman void debugfs_create_u64(const char *name, umode_t mode, struct dentry *parent, 660ad26221fSGreg Kroah-Hartman u64 *value) 6618447891fSMichael Ellerman { 662ad26221fSGreg Kroah-Hartman debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u64, 663b97f6799SStephen Boyd &fops_u64_ro, &fops_u64_wo); 6648447891fSMichael Ellerman } 6658447891fSMichael Ellerman EXPORT_SYMBOL_GPL(debugfs_create_u64); 6668447891fSMichael Ellerman 667c23fe831SViresh Kumar static int debugfs_ulong_set(void *data, u64 val) 668c23fe831SViresh Kumar { 669c23fe831SViresh Kumar *(unsigned long *)data = val; 670c23fe831SViresh Kumar return 0; 671c23fe831SViresh Kumar } 672c23fe831SViresh Kumar 673c23fe831SViresh Kumar static int debugfs_ulong_get(void *data, u64 *val) 674c23fe831SViresh Kumar { 675c23fe831SViresh Kumar *val = *(unsigned long *)data; 676c23fe831SViresh Kumar return 0; 677c23fe831SViresh Kumar } 6784909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong, debugfs_ulong_get, debugfs_ulong_set, 6794909f168SNicolai Stange "%llu\n"); 6804909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_ro, debugfs_ulong_get, NULL, "%llu\n"); 6814909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_wo, NULL, debugfs_ulong_set, "%llu\n"); 682c23fe831SViresh Kumar 683c23fe831SViresh Kumar /** 684c23fe831SViresh Kumar * debugfs_create_ulong - create a debugfs file that is used to read and write 685c23fe831SViresh Kumar * an unsigned long value. 686c23fe831SViresh Kumar * @name: a pointer to a string containing the name of the file to create. 687c23fe831SViresh Kumar * @mode: the permission that the file should have 688c23fe831SViresh Kumar * @parent: a pointer to the parent dentry for this file. This should be a 689c23fe831SViresh Kumar * directory dentry if set. If this parameter is %NULL, then the 690c23fe831SViresh Kumar * file will be created in the root of the debugfs filesystem. 691c23fe831SViresh Kumar * @value: a pointer to the variable that the file should read to and write 692c23fe831SViresh Kumar * from. 693c23fe831SViresh Kumar * 694c23fe831SViresh Kumar * This function creates a file in debugfs with the given name that 695c23fe831SViresh Kumar * contains the value of the variable @value. If the @mode variable is so 696c23fe831SViresh Kumar * set, it can be read from, and written to. 697c23fe831SViresh Kumar */ 698fb05b14cSGreg Kroah-Hartman void debugfs_create_ulong(const char *name, umode_t mode, struct dentry *parent, 699fb05b14cSGreg Kroah-Hartman unsigned long *value) 700c23fe831SViresh Kumar { 701fb05b14cSGreg Kroah-Hartman debugfs_create_mode_unsafe(name, mode, parent, value, &fops_ulong, 702fb05b14cSGreg Kroah-Hartman &fops_ulong_ro, &fops_ulong_wo); 703c23fe831SViresh Kumar } 704c23fe831SViresh Kumar EXPORT_SYMBOL_GPL(debugfs_create_ulong); 705c23fe831SViresh Kumar 7064909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n"); 7074909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n"); 7084909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n"); 7092ebefc50SRobin Getz 7104909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set, 7114909f168SNicolai Stange "0x%04llx\n"); 7124909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n"); 7134909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n"); 7142ebefc50SRobin Getz 7154909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set, 7164909f168SNicolai Stange "0x%08llx\n"); 7174909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n"); 7184909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n"); 7192ebefc50SRobin Getz 7204909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set, 7214909f168SNicolai Stange "0x%016llx\n"); 7224909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_ro, debugfs_u64_get, NULL, "0x%016llx\n"); 7234909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_wo, NULL, debugfs_u64_set, "0x%016llx\n"); 72415b0beaaSHuang Ying 725e6716b87SRandy Dunlap /* 72615b0beaaSHuang Ying * 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 727e6716b87SRandy Dunlap * 728e6716b87SRandy Dunlap * These functions are exactly the same as the above functions (but use a hex 729e6716b87SRandy Dunlap * output for the decimal challenged). For details look at the above unsigned 730e6716b87SRandy Dunlap * decimal functions. 731e6716b87SRandy Dunlap */ 732e6716b87SRandy Dunlap 7332ebefc50SRobin Getz /** 7342ebefc50SRobin Getz * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value 735e6716b87SRandy Dunlap * @name: a pointer to a string containing the name of the file to create. 736e6716b87SRandy Dunlap * @mode: the permission that the file should have 737e6716b87SRandy Dunlap * @parent: a pointer to the parent dentry for this file. This should be a 738e6716b87SRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 739e6716b87SRandy Dunlap * file will be created in the root of the debugfs filesystem. 740e6716b87SRandy Dunlap * @value: a pointer to the variable that the file should read to and write 741e6716b87SRandy Dunlap * from. 7422ebefc50SRobin Getz */ 743c7c11689SGreg Kroah-Hartman void debugfs_create_x8(const char *name, umode_t mode, struct dentry *parent, 744c7c11689SGreg Kroah-Hartman u8 *value) 7452ebefc50SRobin Getz { 746c7c11689SGreg Kroah-Hartman debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x8, 747b97f6799SStephen Boyd &fops_x8_ro, &fops_x8_wo); 7482ebefc50SRobin Getz } 7492ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x8); 7502ebefc50SRobin Getz 751e6716b87SRandy Dunlap /** 752e6716b87SRandy Dunlap * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value 753e6716b87SRandy Dunlap * @name: a pointer to a string containing the name of the file to create. 754e6716b87SRandy Dunlap * @mode: the permission that the file should have 755e6716b87SRandy Dunlap * @parent: a pointer to the parent dentry for this file. This should be a 756e6716b87SRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 757e6716b87SRandy Dunlap * file will be created in the root of the debugfs filesystem. 758e6716b87SRandy Dunlap * @value: a pointer to the variable that the file should read to and write 759e6716b87SRandy Dunlap * from. 760e6716b87SRandy Dunlap */ 761e40d38f2SGreg Kroah-Hartman void debugfs_create_x16(const char *name, umode_t mode, struct dentry *parent, 762e40d38f2SGreg Kroah-Hartman u16 *value) 7632ebefc50SRobin Getz { 764e40d38f2SGreg Kroah-Hartman debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x16, 765b97f6799SStephen Boyd &fops_x16_ro, &fops_x16_wo); 7662ebefc50SRobin Getz } 7672ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x16); 7682ebefc50SRobin Getz 769e6716b87SRandy Dunlap /** 770e6716b87SRandy Dunlap * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value 771e6716b87SRandy Dunlap * @name: a pointer to a string containing the name of the file to create. 772e6716b87SRandy Dunlap * @mode: the permission that the file should have 773e6716b87SRandy Dunlap * @parent: a pointer to the parent dentry for this file. This should be a 774e6716b87SRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 775e6716b87SRandy Dunlap * file will be created in the root of the debugfs filesystem. 776e6716b87SRandy Dunlap * @value: a pointer to the variable that the file should read to and write 777e6716b87SRandy Dunlap * from. 778e6716b87SRandy Dunlap */ 779f5cb0a7eSGreg Kroah-Hartman void debugfs_create_x32(const char *name, umode_t mode, struct dentry *parent, 780f5cb0a7eSGreg Kroah-Hartman u32 *value) 7812ebefc50SRobin Getz { 782f5cb0a7eSGreg Kroah-Hartman debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x32, 783b97f6799SStephen Boyd &fops_x32_ro, &fops_x32_wo); 7842ebefc50SRobin Getz } 7852ebefc50SRobin Getz EXPORT_SYMBOL_GPL(debugfs_create_x32); 7862ebefc50SRobin Getz 78715b0beaaSHuang Ying /** 78815b0beaaSHuang Ying * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value 78915b0beaaSHuang Ying * @name: a pointer to a string containing the name of the file to create. 79015b0beaaSHuang Ying * @mode: the permission that the file should have 79115b0beaaSHuang Ying * @parent: a pointer to the parent dentry for this file. This should be a 79215b0beaaSHuang Ying * directory dentry if set. If this parameter is %NULL, then the 79315b0beaaSHuang Ying * file will be created in the root of the debugfs filesystem. 79415b0beaaSHuang Ying * @value: a pointer to the variable that the file should read to and write 79515b0beaaSHuang Ying * from. 79615b0beaaSHuang Ying */ 7970864c408SGreg Kroah-Hartman void debugfs_create_x64(const char *name, umode_t mode, struct dentry *parent, 7980864c408SGreg Kroah-Hartman u64 *value) 79915b0beaaSHuang Ying { 8000864c408SGreg Kroah-Hartman debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x64, 80182b7d4fbSStephen Boyd &fops_x64_ro, &fops_x64_wo); 80215b0beaaSHuang Ying } 80315b0beaaSHuang Ying EXPORT_SYMBOL_GPL(debugfs_create_x64); 80415b0beaaSHuang Ying 8055e078787SInaky Perez-Gonzalez 8065e078787SInaky Perez-Gonzalez static int debugfs_size_t_set(void *data, u64 val) 8075e078787SInaky Perez-Gonzalez { 8085e078787SInaky Perez-Gonzalez *(size_t *)data = val; 8095e078787SInaky Perez-Gonzalez return 0; 8105e078787SInaky Perez-Gonzalez } 8115e078787SInaky Perez-Gonzalez static int debugfs_size_t_get(void *data, u64 *val) 8125e078787SInaky Perez-Gonzalez { 8135e078787SInaky Perez-Gonzalez *val = *(size_t *)data; 8145e078787SInaky Perez-Gonzalez return 0; 8155e078787SInaky Perez-Gonzalez } 8164909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set, 8175e078787SInaky Perez-Gonzalez "%llu\n"); /* %llu and %zu are more or less the same */ 8184909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_ro, debugfs_size_t_get, NULL, "%llu\n"); 8194909f168SNicolai Stange DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_wo, NULL, debugfs_size_t_set, "%llu\n"); 8205e078787SInaky Perez-Gonzalez 8215e078787SInaky Perez-Gonzalez /** 8225e078787SInaky Perez-Gonzalez * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value 8235e078787SInaky Perez-Gonzalez * @name: a pointer to a string containing the name of the file to create. 8245e078787SInaky Perez-Gonzalez * @mode: the permission that the file should have 8255e078787SInaky Perez-Gonzalez * @parent: a pointer to the parent dentry for this file. This should be a 8265e078787SInaky Perez-Gonzalez * directory dentry if set. If this parameter is %NULL, then the 8275e078787SInaky Perez-Gonzalez * file will be created in the root of the debugfs filesystem. 8285e078787SInaky Perez-Gonzalez * @value: a pointer to the variable that the file should read to and write 8295e078787SInaky Perez-Gonzalez * from. 8305e078787SInaky Perez-Gonzalez */ 8318e580263SGreg Kroah-Hartman void debugfs_create_size_t(const char *name, umode_t mode, 8325e078787SInaky Perez-Gonzalez struct dentry *parent, size_t *value) 8335e078787SInaky Perez-Gonzalez { 8348e580263SGreg Kroah-Hartman debugfs_create_mode_unsafe(name, mode, parent, value, &fops_size_t, 8358e580263SGreg Kroah-Hartman &fops_size_t_ro, &fops_size_t_wo); 8365e078787SInaky Perez-Gonzalez } 8375e078787SInaky Perez-Gonzalez EXPORT_SYMBOL_GPL(debugfs_create_size_t); 8385e078787SInaky Perez-Gonzalez 8393a76e5e0SSeth Jennings static int debugfs_atomic_t_set(void *data, u64 val) 8403a76e5e0SSeth Jennings { 8413a76e5e0SSeth Jennings atomic_set((atomic_t *)data, val); 8423a76e5e0SSeth Jennings return 0; 8433a76e5e0SSeth Jennings } 8443a76e5e0SSeth Jennings static int debugfs_atomic_t_get(void *data, u64 *val) 8453a76e5e0SSeth Jennings { 8463a76e5e0SSeth Jennings *val = atomic_read((atomic_t *)data); 8473a76e5e0SSeth Jennings return 0; 8483a76e5e0SSeth Jennings } 849d472cf79SAkinobu Mita DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t, debugfs_atomic_t_get, 8503a76e5e0SSeth Jennings debugfs_atomic_t_set, "%lld\n"); 851d472cf79SAkinobu Mita DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t_ro, debugfs_atomic_t_get, NULL, 8524909f168SNicolai Stange "%lld\n"); 853d472cf79SAkinobu Mita DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t_wo, NULL, debugfs_atomic_t_set, 8544909f168SNicolai Stange "%lld\n"); 8553a76e5e0SSeth Jennings 8563a76e5e0SSeth Jennings /** 8573a76e5e0SSeth Jennings * debugfs_create_atomic_t - create a debugfs file that is used to read and 8583a76e5e0SSeth Jennings * write an atomic_t value 8593a76e5e0SSeth Jennings * @name: a pointer to a string containing the name of the file to create. 8603a76e5e0SSeth Jennings * @mode: the permission that the file should have 8613a76e5e0SSeth Jennings * @parent: a pointer to the parent dentry for this file. This should be a 8623a76e5e0SSeth Jennings * directory dentry if set. If this parameter is %NULL, then the 8633a76e5e0SSeth Jennings * file will be created in the root of the debugfs filesystem. 8643a76e5e0SSeth Jennings * @value: a pointer to the variable that the file should read to and write 8653a76e5e0SSeth Jennings * from. 8663a76e5e0SSeth Jennings */ 8679927c6faSGreg Kroah-Hartman void debugfs_create_atomic_t(const char *name, umode_t mode, 8683a76e5e0SSeth Jennings struct dentry *parent, atomic_t *value) 8693a76e5e0SSeth Jennings { 8709927c6faSGreg Kroah-Hartman debugfs_create_mode_unsafe(name, mode, parent, value, &fops_atomic_t, 8719927c6faSGreg Kroah-Hartman &fops_atomic_t_ro, &fops_atomic_t_wo); 8723a76e5e0SSeth Jennings } 8733a76e5e0SSeth Jennings EXPORT_SYMBOL_GPL(debugfs_create_atomic_t); 8745e078787SInaky Perez-Gonzalez 8750642ef6fSRichard Fitzgerald ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf, 8761da177e4SLinus Torvalds size_t count, loff_t *ppos) 8771da177e4SLinus Torvalds { 878c8a9c285SRasmus Villemoes char buf[2]; 8794d45f797SNicolai Stange bool val; 88069d29f9eSNicolai Stange int r; 88169d29f9eSNicolai Stange struct dentry *dentry = F_DENTRY(file); 8821da177e4SLinus Torvalds 88369d29f9eSNicolai Stange r = debugfs_file_get(dentry); 88469d29f9eSNicolai Stange if (unlikely(r)) 8854d45f797SNicolai Stange return r; 88669d29f9eSNicolai Stange val = *(bool *)file->private_data; 88769d29f9eSNicolai Stange debugfs_file_put(dentry); 8884d45f797SNicolai Stange 8894d45f797SNicolai Stange if (val) 8901da177e4SLinus Torvalds buf[0] = 'Y'; 8911da177e4SLinus Torvalds else 8921da177e4SLinus Torvalds buf[0] = 'N'; 8931da177e4SLinus Torvalds buf[1] = '\n'; 8941da177e4SLinus Torvalds return simple_read_from_buffer(user_buf, count, ppos, buf, 2); 8951da177e4SLinus Torvalds } 8960642ef6fSRichard Fitzgerald EXPORT_SYMBOL_GPL(debugfs_read_file_bool); 8971da177e4SLinus Torvalds 8980642ef6fSRichard Fitzgerald ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf, 8991da177e4SLinus Torvalds size_t count, loff_t *ppos) 9001da177e4SLinus Torvalds { 9018705b48eSJonathan Cameron bool bv; 90269d29f9eSNicolai Stange int r; 903621a5f7aSViresh Kumar bool *val = file->private_data; 90469d29f9eSNicolai Stange struct dentry *dentry = F_DENTRY(file); 9051da177e4SLinus Torvalds 906964f8363SAndy Shevchenko r = kstrtobool_from_user(user_buf, count, &bv); 907964f8363SAndy Shevchenko if (!r) { 90869d29f9eSNicolai Stange r = debugfs_file_get(dentry); 90969d29f9eSNicolai Stange if (unlikely(r)) 9104d45f797SNicolai Stange return r; 91169d29f9eSNicolai Stange *val = bv; 91269d29f9eSNicolai Stange debugfs_file_put(dentry); 9134d45f797SNicolai Stange } 9141da177e4SLinus Torvalds 9151da177e4SLinus Torvalds return count; 9161da177e4SLinus Torvalds } 9170642ef6fSRichard Fitzgerald EXPORT_SYMBOL_GPL(debugfs_write_file_bool); 9181da177e4SLinus Torvalds 9194b6f5d20SArjan van de Ven static const struct file_operations fops_bool = { 9200642ef6fSRichard Fitzgerald .read = debugfs_read_file_bool, 9210642ef6fSRichard Fitzgerald .write = debugfs_write_file_bool, 922234e3405SStephen Boyd .open = simple_open, 9236038f373SArnd Bergmann .llseek = default_llseek, 9241da177e4SLinus Torvalds }; 9251da177e4SLinus Torvalds 9266713e8fbSStephen Boyd static const struct file_operations fops_bool_ro = { 9276713e8fbSStephen Boyd .read = debugfs_read_file_bool, 9286713e8fbSStephen Boyd .open = simple_open, 9296713e8fbSStephen Boyd .llseek = default_llseek, 9306713e8fbSStephen Boyd }; 9316713e8fbSStephen Boyd 9326713e8fbSStephen Boyd static const struct file_operations fops_bool_wo = { 9336713e8fbSStephen Boyd .write = debugfs_write_file_bool, 9346713e8fbSStephen Boyd .open = simple_open, 9356713e8fbSStephen Boyd .llseek = default_llseek, 9366713e8fbSStephen Boyd }; 9376713e8fbSStephen Boyd 9381da177e4SLinus Torvalds /** 9396468b3afSRandy Dunlap * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value 9401da177e4SLinus Torvalds * @name: a pointer to a string containing the name of the file to create. 9411da177e4SLinus Torvalds * @mode: the permission that the file should have 9421da177e4SLinus Torvalds * @parent: a pointer to the parent dentry for this file. This should be a 9436468b3afSRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 9441da177e4SLinus Torvalds * file will be created in the root of the debugfs filesystem. 9451da177e4SLinus Torvalds * @value: a pointer to the variable that the file should read to and write 9461da177e4SLinus Torvalds * from. 9471da177e4SLinus Torvalds * 9481da177e4SLinus Torvalds * This function creates a file in debugfs with the given name that 9491da177e4SLinus Torvalds * contains the value of the variable @value. If the @mode variable is so 9501da177e4SLinus Torvalds * set, it can be read from, and written to. 9511da177e4SLinus Torvalds */ 952393b0638SGreg Kroah-Hartman void debugfs_create_bool(const char *name, umode_t mode, struct dentry *parent, 953393b0638SGreg Kroah-Hartman bool *value) 9541da177e4SLinus Torvalds { 955393b0638SGreg Kroah-Hartman debugfs_create_mode_unsafe(name, mode, parent, value, &fops_bool, 9566713e8fbSStephen Boyd &fops_bool_ro, &fops_bool_wo); 9571da177e4SLinus Torvalds } 9581da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_bool); 9591da177e4SLinus Torvalds 9609af0440eSPeter Zijlstra ssize_t debugfs_read_file_str(struct file *file, char __user *user_buf, 9619af0440eSPeter Zijlstra size_t count, loff_t *ppos) 9629af0440eSPeter Zijlstra { 9639af0440eSPeter Zijlstra struct dentry *dentry = F_DENTRY(file); 9649af0440eSPeter Zijlstra char *str, *copy = NULL; 9659af0440eSPeter Zijlstra int copy_len, len; 9669af0440eSPeter Zijlstra ssize_t ret; 9679af0440eSPeter Zijlstra 9689af0440eSPeter Zijlstra ret = debugfs_file_get(dentry); 9699af0440eSPeter Zijlstra if (unlikely(ret)) 9709af0440eSPeter Zijlstra return ret; 9719af0440eSPeter Zijlstra 9729af0440eSPeter Zijlstra str = *(char **)file->private_data; 9739af0440eSPeter Zijlstra len = strlen(str) + 1; 9749af0440eSPeter Zijlstra copy = kmalloc(len, GFP_KERNEL); 9759af0440eSPeter Zijlstra if (!copy) { 9769af0440eSPeter Zijlstra debugfs_file_put(dentry); 9779af0440eSPeter Zijlstra return -ENOMEM; 9789af0440eSPeter Zijlstra } 9799af0440eSPeter Zijlstra 9809af0440eSPeter Zijlstra copy_len = strscpy(copy, str, len); 9819af0440eSPeter Zijlstra debugfs_file_put(dentry); 9829af0440eSPeter Zijlstra if (copy_len < 0) { 9839af0440eSPeter Zijlstra kfree(copy); 9849af0440eSPeter Zijlstra return copy_len; 9859af0440eSPeter Zijlstra } 9869af0440eSPeter Zijlstra 9879af0440eSPeter Zijlstra copy[copy_len] = '\n'; 9889af0440eSPeter Zijlstra 989f501b6a2SDietmar Eggemann ret = simple_read_from_buffer(user_buf, count, ppos, copy, len); 9909af0440eSPeter Zijlstra kfree(copy); 9919af0440eSPeter Zijlstra 9929af0440eSPeter Zijlstra return ret; 9939af0440eSPeter Zijlstra } 994d60b59b9SCristian Marussi EXPORT_SYMBOL_GPL(debugfs_create_str); 9959af0440eSPeter Zijlstra 9969af0440eSPeter Zijlstra static ssize_t debugfs_write_file_str(struct file *file, const char __user *user_buf, 9979af0440eSPeter Zijlstra size_t count, loff_t *ppos) 9989af0440eSPeter Zijlstra { 99986b54881SMike Tipton struct dentry *dentry = F_DENTRY(file); 100086b54881SMike Tipton char *old, *new = NULL; 100186b54881SMike Tipton int pos = *ppos; 100286b54881SMike Tipton int r; 100386b54881SMike Tipton 100486b54881SMike Tipton r = debugfs_file_get(dentry); 100586b54881SMike Tipton if (unlikely(r)) 100686b54881SMike Tipton return r; 100786b54881SMike Tipton 100886b54881SMike Tipton old = *(char **)file->private_data; 100986b54881SMike Tipton 101086b54881SMike Tipton /* only allow strict concatenation */ 101186b54881SMike Tipton r = -EINVAL; 101286b54881SMike Tipton if (pos && pos != strlen(old)) 101386b54881SMike Tipton goto error; 101486b54881SMike Tipton 101586b54881SMike Tipton r = -E2BIG; 101686b54881SMike Tipton if (pos + count + 1 > PAGE_SIZE) 101786b54881SMike Tipton goto error; 101886b54881SMike Tipton 101986b54881SMike Tipton r = -ENOMEM; 102086b54881SMike Tipton new = kmalloc(pos + count + 1, GFP_KERNEL); 102186b54881SMike Tipton if (!new) 102286b54881SMike Tipton goto error; 102386b54881SMike Tipton 102486b54881SMike Tipton if (pos) 102586b54881SMike Tipton memcpy(new, old, pos); 102686b54881SMike Tipton 102786b54881SMike Tipton r = -EFAULT; 102886b54881SMike Tipton if (copy_from_user(new + pos, user_buf, count)) 102986b54881SMike Tipton goto error; 103086b54881SMike Tipton 103186b54881SMike Tipton new[pos + count] = '\0'; 103286b54881SMike Tipton strim(new); 103386b54881SMike Tipton 10347360a48bSMike Tipton rcu_assign_pointer(*(char __rcu **)file->private_data, new); 103586b54881SMike Tipton synchronize_rcu(); 103686b54881SMike Tipton kfree(old); 103786b54881SMike Tipton 103886b54881SMike Tipton debugfs_file_put(dentry); 103986b54881SMike Tipton return count; 104086b54881SMike Tipton 104186b54881SMike Tipton error: 104286b54881SMike Tipton kfree(new); 104386b54881SMike Tipton debugfs_file_put(dentry); 104486b54881SMike Tipton return r; 10459af0440eSPeter Zijlstra } 10469af0440eSPeter Zijlstra 10479af0440eSPeter Zijlstra static const struct file_operations fops_str = { 10489af0440eSPeter Zijlstra .read = debugfs_read_file_str, 10499af0440eSPeter Zijlstra .write = debugfs_write_file_str, 10509af0440eSPeter Zijlstra .open = simple_open, 10519af0440eSPeter Zijlstra .llseek = default_llseek, 10529af0440eSPeter Zijlstra }; 10539af0440eSPeter Zijlstra 10549af0440eSPeter Zijlstra static const struct file_operations fops_str_ro = { 10559af0440eSPeter Zijlstra .read = debugfs_read_file_str, 10569af0440eSPeter Zijlstra .open = simple_open, 10579af0440eSPeter Zijlstra .llseek = default_llseek, 10589af0440eSPeter Zijlstra }; 10599af0440eSPeter Zijlstra 10609af0440eSPeter Zijlstra static const struct file_operations fops_str_wo = { 10619af0440eSPeter Zijlstra .write = debugfs_write_file_str, 10629af0440eSPeter Zijlstra .open = simple_open, 10639af0440eSPeter Zijlstra .llseek = default_llseek, 10649af0440eSPeter Zijlstra }; 10659af0440eSPeter Zijlstra 10669af0440eSPeter Zijlstra /** 10679af0440eSPeter Zijlstra * debugfs_create_str - create a debugfs file that is used to read and write a string value 10689af0440eSPeter Zijlstra * @name: a pointer to a string containing the name of the file to create. 10699af0440eSPeter Zijlstra * @mode: the permission that the file should have 10709af0440eSPeter Zijlstra * @parent: a pointer to the parent dentry for this file. This should be a 10719af0440eSPeter Zijlstra * directory dentry if set. If this parameter is %NULL, then the 10729af0440eSPeter Zijlstra * file will be created in the root of the debugfs filesystem. 10739af0440eSPeter Zijlstra * @value: a pointer to the variable that the file should read to and write 10749af0440eSPeter Zijlstra * from. 10759af0440eSPeter Zijlstra * 10769af0440eSPeter Zijlstra * This function creates a file in debugfs with the given name that 10779af0440eSPeter Zijlstra * contains the value of the variable @value. If the @mode variable is so 10789af0440eSPeter Zijlstra * set, it can be read from, and written to. 10799af0440eSPeter Zijlstra */ 10809af0440eSPeter Zijlstra void debugfs_create_str(const char *name, umode_t mode, 10819af0440eSPeter Zijlstra struct dentry *parent, char **value) 10829af0440eSPeter Zijlstra { 10839af0440eSPeter Zijlstra debugfs_create_mode_unsafe(name, mode, parent, value, &fops_str, 10849af0440eSPeter Zijlstra &fops_str_ro, &fops_str_wo); 10859af0440eSPeter Zijlstra } 10869af0440eSPeter Zijlstra 1087dd308bc3SMichael Ellerman static ssize_t read_file_blob(struct file *file, char __user *user_buf, 1088dd308bc3SMichael Ellerman size_t count, loff_t *ppos) 1089dd308bc3SMichael Ellerman { 1090dd308bc3SMichael Ellerman struct debugfs_blob_wrapper *blob = file->private_data; 109169d29f9eSNicolai Stange struct dentry *dentry = F_DENTRY(file); 109283b711cbSNicolai Stange ssize_t r; 109383b711cbSNicolai Stange 109469d29f9eSNicolai Stange r = debugfs_file_get(dentry); 109569d29f9eSNicolai Stange if (unlikely(r)) 109669d29f9eSNicolai Stange return r; 109783b711cbSNicolai Stange r = simple_read_from_buffer(user_buf, count, ppos, blob->data, 1098dd308bc3SMichael Ellerman blob->size); 109969d29f9eSNicolai Stange debugfs_file_put(dentry); 110083b711cbSNicolai Stange return r; 1101dd308bc3SMichael Ellerman } 1102dd308bc3SMichael Ellerman 110300977a59SArjan van de Ven static const struct file_operations fops_blob = { 1104dd308bc3SMichael Ellerman .read = read_file_blob, 1105234e3405SStephen Boyd .open = simple_open, 11066038f373SArnd Bergmann .llseek = default_llseek, 1107dd308bc3SMichael Ellerman }; 1108dd308bc3SMichael Ellerman 1109dd308bc3SMichael Ellerman /** 1110400ced61SJonathan Corbet * debugfs_create_blob - create a debugfs file that is used to read a binary blob 1111dd308bc3SMichael Ellerman * @name: a pointer to a string containing the name of the file to create. 1112d616f56dSWolfram Sang * @mode: the read permission that the file should have (other permissions are 1113d616f56dSWolfram Sang * masked out) 1114dd308bc3SMichael Ellerman * @parent: a pointer to the parent dentry for this file. This should be a 11156468b3afSRandy Dunlap * directory dentry if set. If this parameter is %NULL, then the 1116dd308bc3SMichael Ellerman * file will be created in the root of the debugfs filesystem. 1117dd308bc3SMichael Ellerman * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer 1118dd308bc3SMichael Ellerman * to the blob data and the size of the data. 1119dd308bc3SMichael Ellerman * 1120dd308bc3SMichael Ellerman * This function creates a file in debugfs with the given name that exports 1121dd308bc3SMichael Ellerman * @blob->data as a binary blob. If the @mode variable is so set it can be 1122dd308bc3SMichael Ellerman * read from. Writing is not supported. 1123dd308bc3SMichael Ellerman * 1124dd308bc3SMichael Ellerman * This function will return a pointer to a dentry if it succeeds. This 1125dd308bc3SMichael Ellerman * pointer must be passed to the debugfs_remove() function when the file is 1126dd308bc3SMichael Ellerman * to be removed (no automatic cleanup happens if your module is unloaded, 1127adc92dd4SDaniel W. S. Almeida * you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be 11289abb2499SRonald Tschalär * returned. 1129dd308bc3SMichael Ellerman * 1130adc92dd4SDaniel W. S. Almeida * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will 11319abb2499SRonald Tschalär * be returned. 1132dd308bc3SMichael Ellerman */ 1133f4ae40a6SAl Viro struct dentry *debugfs_create_blob(const char *name, umode_t mode, 1134dd308bc3SMichael Ellerman struct dentry *parent, 1135dd308bc3SMichael Ellerman struct debugfs_blob_wrapper *blob) 1136dd308bc3SMichael Ellerman { 1137d616f56dSWolfram Sang return debugfs_create_file_unsafe(name, mode & 0444, parent, blob, &fops_blob); 1138dd308bc3SMichael Ellerman } 1139dd308bc3SMichael Ellerman EXPORT_SYMBOL_GPL(debugfs_create_blob); 11401a087c6aSAlessandro Rubini 1141e05e279eSLinus Torvalds static size_t u32_format_array(char *buf, size_t bufsize, 1142e05e279eSLinus Torvalds u32 *array, int array_size) 11439fe2a701SSrivatsa Vaddagiri { 11449fe2a701SSrivatsa Vaddagiri size_t ret = 0; 11459fe2a701SSrivatsa Vaddagiri 1146e05e279eSLinus Torvalds while (--array_size >= 0) { 11479fe2a701SSrivatsa Vaddagiri size_t len; 1148e05e279eSLinus Torvalds char term = array_size ? ' ' : '\n'; 11499fe2a701SSrivatsa Vaddagiri 1150e05e279eSLinus Torvalds len = snprintf(buf, bufsize, "%u%c", *array++, term); 11519fe2a701SSrivatsa Vaddagiri ret += len; 11529fe2a701SSrivatsa Vaddagiri 11539fe2a701SSrivatsa Vaddagiri buf += len; 11549fe2a701SSrivatsa Vaddagiri bufsize -= len; 11559fe2a701SSrivatsa Vaddagiri } 11569fe2a701SSrivatsa Vaddagiri return ret; 11579fe2a701SSrivatsa Vaddagiri } 11589fe2a701SSrivatsa Vaddagiri 115936048853SDavid Rientjes static int u32_array_open(struct inode *inode, struct file *file) 11609fe2a701SSrivatsa Vaddagiri { 1161a2b992c8SJakub Kicinski struct debugfs_u32_array *data = inode->i_private; 1162a2b992c8SJakub Kicinski int size, elements = data->n_elements; 1163e05e279eSLinus Torvalds char *buf; 11649fe2a701SSrivatsa Vaddagiri 1165e05e279eSLinus Torvalds /* 1166e05e279eSLinus Torvalds * Max size: 1167e05e279eSLinus Torvalds * - 10 digits + ' '/'\n' = 11 bytes per number 1168e05e279eSLinus Torvalds * - terminating NUL character 1169e05e279eSLinus Torvalds */ 1170e05e279eSLinus Torvalds size = elements*11; 1171e05e279eSLinus Torvalds buf = kmalloc(size+1, GFP_KERNEL); 1172e05e279eSLinus Torvalds if (!buf) 117336048853SDavid Rientjes return -ENOMEM; 1174e05e279eSLinus Torvalds buf[size] = 0; 1175e05e279eSLinus Torvalds 1176e05e279eSLinus Torvalds file->private_data = buf; 1177a2b992c8SJakub Kicinski u32_format_array(buf, size, data->array, data->n_elements); 1178e05e279eSLinus Torvalds 117936048853SDavid Rientjes return nonseekable_open(inode, file); 11809fe2a701SSrivatsa Vaddagiri } 11819fe2a701SSrivatsa Vaddagiri 118236048853SDavid Rientjes static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len, 118336048853SDavid Rientjes loff_t *ppos) 118436048853SDavid Rientjes { 118536048853SDavid Rientjes size_t size = strlen(file->private_data); 11869fe2a701SSrivatsa Vaddagiri 11879fe2a701SSrivatsa Vaddagiri return simple_read_from_buffer(buf, len, ppos, 11889fe2a701SSrivatsa Vaddagiri file->private_data, size); 11899fe2a701SSrivatsa Vaddagiri } 11909fe2a701SSrivatsa Vaddagiri 11919fe2a701SSrivatsa Vaddagiri static int u32_array_release(struct inode *inode, struct file *file) 11929fe2a701SSrivatsa Vaddagiri { 11939fe2a701SSrivatsa Vaddagiri kfree(file->private_data); 11949fe2a701SSrivatsa Vaddagiri 11959fe2a701SSrivatsa Vaddagiri return 0; 11969fe2a701SSrivatsa Vaddagiri } 11979fe2a701SSrivatsa Vaddagiri 11989fe2a701SSrivatsa Vaddagiri static const struct file_operations u32_array_fops = { 11999fe2a701SSrivatsa Vaddagiri .owner = THIS_MODULE, 12009fe2a701SSrivatsa Vaddagiri .open = u32_array_open, 12019fe2a701SSrivatsa Vaddagiri .release = u32_array_release, 12029fe2a701SSrivatsa Vaddagiri .read = u32_array_read, 12039fe2a701SSrivatsa Vaddagiri .llseek = no_llseek, 12049fe2a701SSrivatsa Vaddagiri }; 12059fe2a701SSrivatsa Vaddagiri 12069fe2a701SSrivatsa Vaddagiri /** 12079fe2a701SSrivatsa Vaddagiri * debugfs_create_u32_array - create a debugfs file that is used to read u32 12089fe2a701SSrivatsa Vaddagiri * array. 12099fe2a701SSrivatsa Vaddagiri * @name: a pointer to a string containing the name of the file to create. 12109fe2a701SSrivatsa Vaddagiri * @mode: the permission that the file should have. 12119fe2a701SSrivatsa Vaddagiri * @parent: a pointer to the parent dentry for this file. This should be a 12129fe2a701SSrivatsa Vaddagiri * directory dentry if set. If this parameter is %NULL, then the 12139fe2a701SSrivatsa Vaddagiri * file will be created in the root of the debugfs filesystem. 1214a2b992c8SJakub Kicinski * @array: wrapper struct containing data pointer and size of the array. 12159fe2a701SSrivatsa Vaddagiri * 12169fe2a701SSrivatsa Vaddagiri * This function creates a file in debugfs with the given name that exports 12179fe2a701SSrivatsa Vaddagiri * @array as data. If the @mode variable is so set it can be read from. 12189fe2a701SSrivatsa Vaddagiri * Writing is not supported. Seek within the file is also not supported. 12199fe2a701SSrivatsa Vaddagiri * Once array is created its size can not be changed. 12209fe2a701SSrivatsa Vaddagiri */ 1221c9c2c27dSGreg Kroah-Hartman void debugfs_create_u32_array(const char *name, umode_t mode, 1222a2b992c8SJakub Kicinski struct dentry *parent, 1223a2b992c8SJakub Kicinski struct debugfs_u32_array *array) 12249fe2a701SSrivatsa Vaddagiri { 1225a2b992c8SJakub Kicinski debugfs_create_file_unsafe(name, mode, parent, array, &u32_array_fops); 12269fe2a701SSrivatsa Vaddagiri } 12279fe2a701SSrivatsa Vaddagiri EXPORT_SYMBOL_GPL(debugfs_create_u32_array); 12289fe2a701SSrivatsa Vaddagiri 12293b85e4abSHeiko Carstens #ifdef CONFIG_HAS_IOMEM 12303b85e4abSHeiko Carstens 12311a087c6aSAlessandro Rubini /* 12321a087c6aSAlessandro Rubini * The regset32 stuff is used to print 32-bit registers using the 12331a087c6aSAlessandro Rubini * seq_file utilities. We offer printing a register set in an already-opened 12341a087c6aSAlessandro Rubini * sequential file or create a debugfs file that only prints a regset32. 12351a087c6aSAlessandro Rubini */ 12361a087c6aSAlessandro Rubini 12371a087c6aSAlessandro Rubini /** 12381a087c6aSAlessandro Rubini * debugfs_print_regs32 - use seq_print to describe a set of registers 12391a087c6aSAlessandro Rubini * @s: the seq_file structure being used to generate output 12401a087c6aSAlessandro Rubini * @regs: an array if struct debugfs_reg32 structures 1241b5763accSRandy Dunlap * @nregs: the length of the above array 12421a087c6aSAlessandro Rubini * @base: the base address to be used in reading the registers 12431a087c6aSAlessandro Rubini * @prefix: a string to be prefixed to every output line 12441a087c6aSAlessandro Rubini * 12451a087c6aSAlessandro Rubini * This function outputs a text block describing the current values of 12461a087c6aSAlessandro Rubini * some 32-bit hardware registers. It is meant to be used within debugfs 12471a087c6aSAlessandro Rubini * files based on seq_file that need to show registers, intermixed with other 12481a087c6aSAlessandro Rubini * information. The prefix argument may be used to specify a leading string, 12491a087c6aSAlessandro Rubini * because some peripherals have several blocks of identical registers, 12501a087c6aSAlessandro Rubini * for example configuration of dma channels 12511a087c6aSAlessandro Rubini */ 12529761536eSJoe Perches void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs, 12531a087c6aSAlessandro Rubini int nregs, void __iomem *base, char *prefix) 12541a087c6aSAlessandro Rubini { 12559761536eSJoe Perches int i; 12561a087c6aSAlessandro Rubini 12571a087c6aSAlessandro Rubini for (i = 0; i < nregs; i++, regs++) { 12581a087c6aSAlessandro Rubini if (prefix) 12599761536eSJoe Perches seq_printf(s, "%s", prefix); 12609761536eSJoe Perches seq_printf(s, "%s = 0x%08x\n", regs->name, 12615858441cSDan Carpenter readl(base + regs->offset)); 12629761536eSJoe Perches if (seq_has_overflowed(s)) 12639761536eSJoe Perches break; 12641a087c6aSAlessandro Rubini } 12651a087c6aSAlessandro Rubini } 12661a087c6aSAlessandro Rubini EXPORT_SYMBOL_GPL(debugfs_print_regs32); 12671a087c6aSAlessandro Rubini 126819029f3fSChenXiaoSong static int debugfs_regset32_show(struct seq_file *s, void *data) 12691a087c6aSAlessandro Rubini { 12701a087c6aSAlessandro Rubini struct debugfs_regset32 *regset = s->private; 12711a087c6aSAlessandro Rubini 127230332eeeSGeert Uytterhoeven if (regset->dev) 127330332eeeSGeert Uytterhoeven pm_runtime_get_sync(regset->dev); 127430332eeeSGeert Uytterhoeven 12751a087c6aSAlessandro Rubini debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, ""); 127630332eeeSGeert Uytterhoeven 127730332eeeSGeert Uytterhoeven if (regset->dev) 127830332eeeSGeert Uytterhoeven pm_runtime_put(regset->dev); 127930332eeeSGeert Uytterhoeven 12801a087c6aSAlessandro Rubini return 0; 12811a087c6aSAlessandro Rubini } 12821a087c6aSAlessandro Rubini 128319029f3fSChenXiaoSong DEFINE_SHOW_ATTRIBUTE(debugfs_regset32); 12841a087c6aSAlessandro Rubini 12851a087c6aSAlessandro Rubini /** 12861a087c6aSAlessandro Rubini * debugfs_create_regset32 - create a debugfs file that returns register values 12871a087c6aSAlessandro Rubini * @name: a pointer to a string containing the name of the file to create. 12881a087c6aSAlessandro Rubini * @mode: the permission that the file should have 12891a087c6aSAlessandro Rubini * @parent: a pointer to the parent dentry for this file. This should be a 12901a087c6aSAlessandro Rubini * directory dentry if set. If this parameter is %NULL, then the 12911a087c6aSAlessandro Rubini * file will be created in the root of the debugfs filesystem. 12921a087c6aSAlessandro Rubini * @regset: a pointer to a struct debugfs_regset32, which contains a pointer 12931a087c6aSAlessandro Rubini * to an array of register definitions, the array size and the base 12941a087c6aSAlessandro Rubini * address where the register bank is to be found. 12951a087c6aSAlessandro Rubini * 12961a087c6aSAlessandro Rubini * This function creates a file in debugfs with the given name that reports 12971a087c6aSAlessandro Rubini * the names and values of a set of 32-bit registers. If the @mode variable 12981a087c6aSAlessandro Rubini * is so set it can be read from. Writing is not supported. 12991a087c6aSAlessandro Rubini */ 1300ae91c925SGreg Kroah-Hartman void debugfs_create_regset32(const char *name, umode_t mode, 13011a087c6aSAlessandro Rubini struct dentry *parent, 13021a087c6aSAlessandro Rubini struct debugfs_regset32 *regset) 13031a087c6aSAlessandro Rubini { 130419029f3fSChenXiaoSong debugfs_create_file(name, mode, parent, regset, &debugfs_regset32_fops); 13051a087c6aSAlessandro Rubini } 13061a087c6aSAlessandro Rubini EXPORT_SYMBOL_GPL(debugfs_create_regset32); 13073b85e4abSHeiko Carstens 13083b85e4abSHeiko Carstens #endif /* CONFIG_HAS_IOMEM */ 130998210b7fSArend van Spriel 131098210b7fSArend van Spriel struct debugfs_devm_entry { 131198210b7fSArend van Spriel int (*read)(struct seq_file *seq, void *data); 131298210b7fSArend van Spriel struct device *dev; 131398210b7fSArend van Spriel }; 131498210b7fSArend van Spriel 131598210b7fSArend van Spriel static int debugfs_devm_entry_open(struct inode *inode, struct file *f) 131698210b7fSArend van Spriel { 131798210b7fSArend van Spriel struct debugfs_devm_entry *entry = inode->i_private; 131898210b7fSArend van Spriel 131998210b7fSArend van Spriel return single_open(f, entry->read, entry->dev); 132098210b7fSArend van Spriel } 132198210b7fSArend van Spriel 132298210b7fSArend van Spriel static const struct file_operations debugfs_devm_entry_ops = { 132398210b7fSArend van Spriel .owner = THIS_MODULE, 132498210b7fSArend van Spriel .open = debugfs_devm_entry_open, 132598210b7fSArend van Spriel .release = single_release, 132698210b7fSArend van Spriel .read = seq_read, 132798210b7fSArend van Spriel .llseek = seq_lseek 132898210b7fSArend van Spriel }; 132998210b7fSArend van Spriel 133098210b7fSArend van Spriel /** 133198210b7fSArend van Spriel * debugfs_create_devm_seqfile - create a debugfs file that is bound to device. 133298210b7fSArend van Spriel * 133398210b7fSArend van Spriel * @dev: device related to this debugfs file. 133498210b7fSArend van Spriel * @name: name of the debugfs file. 133598210b7fSArend van Spriel * @parent: a pointer to the parent dentry for this file. This should be a 133698210b7fSArend van Spriel * directory dentry if set. If this parameter is %NULL, then the 133798210b7fSArend van Spriel * file will be created in the root of the debugfs filesystem. 133898210b7fSArend van Spriel * @read_fn: function pointer called to print the seq_file content. 133998210b7fSArend van Spriel */ 13400d519cbfSGreg Kroah-Hartman void debugfs_create_devm_seqfile(struct device *dev, const char *name, 134198210b7fSArend van Spriel struct dentry *parent, 13420d519cbfSGreg Kroah-Hartman int (*read_fn)(struct seq_file *s, void *data)) 134398210b7fSArend van Spriel { 134498210b7fSArend van Spriel struct debugfs_devm_entry *entry; 134598210b7fSArend van Spriel 134698210b7fSArend van Spriel if (IS_ERR(parent)) 13470d519cbfSGreg Kroah-Hartman return; 134898210b7fSArend van Spriel 134998210b7fSArend van Spriel entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL); 135098210b7fSArend van Spriel if (!entry) 13510d519cbfSGreg Kroah-Hartman return; 135298210b7fSArend van Spriel 135398210b7fSArend van Spriel entry->read = read_fn; 135498210b7fSArend van Spriel entry->dev = dev; 135598210b7fSArend van Spriel 13560d519cbfSGreg Kroah-Hartman debugfs_create_file(name, S_IRUGO, parent, entry, 135798210b7fSArend van Spriel &debugfs_devm_entry_ops); 135898210b7fSArend van Spriel } 135998210b7fSArend van Spriel EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile); 1360