/*1* file.c - part of debugfs, a tiny little debug file system2*3* Copyright (C) 2004 Greg Kroah-Hartman <[email protected]>4* Copyright (C) 2004 IBM Inc.5*6* This program is free software; you can redistribute it and/or7* modify it under the terms of the GNU General Public License version8* 2 as published by the Free Software Foundation.9*10* debugfs is for people to use instead of /proc or /sys.11* See Documentation/DocBook/filesystems for more details.12*13*/1415#include <linux/module.h>16#include <linux/fs.h>17#include <linux/pagemap.h>18#include <linux/namei.h>19#include <linux/debugfs.h>2021static ssize_t default_read_file(struct file *file, char __user *buf,22size_t count, loff_t *ppos)23{24return 0;25}2627static ssize_t default_write_file(struct file *file, const char __user *buf,28size_t count, loff_t *ppos)29{30return count;31}3233static int default_open(struct inode *inode, struct file *file)34{35if (inode->i_private)36file->private_data = inode->i_private;3738return 0;39}4041const struct file_operations debugfs_file_operations = {42.read = default_read_file,43.write = default_write_file,44.open = default_open,45.llseek = noop_llseek,46};4748static void *debugfs_follow_link(struct dentry *dentry, struct nameidata *nd)49{50nd_set_link(nd, dentry->d_inode->i_private);51return NULL;52}5354const struct inode_operations debugfs_link_operations = {55.readlink = generic_readlink,56.follow_link = debugfs_follow_link,57};5859static int debugfs_u8_set(void *data, u64 val)60{61*(u8 *)data = val;62return 0;63}64static int debugfs_u8_get(void *data, u64 *val)65{66*val = *(u8 *)data;67return 0;68}69DEFINE_SIMPLE_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");70DEFINE_SIMPLE_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n");71DEFINE_SIMPLE_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n");7273/**74* debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value75* @name: a pointer to a string containing the name of the file to create.76* @mode: the permission that the file should have77* @parent: a pointer to the parent dentry for this file. This should be a78* directory dentry if set. If this parameter is %NULL, then the79* file will be created in the root of the debugfs filesystem.80* @value: a pointer to the variable that the file should read to and write81* from.82*83* This function creates a file in debugfs with the given name that84* contains the value of the variable @value. If the @mode variable is so85* set, it can be read from, and written to.86*87* This function will return a pointer to a dentry if it succeeds. This88* pointer must be passed to the debugfs_remove() function when the file is89* to be removed (no automatic cleanup happens if your module is unloaded,90* you are responsible here.) If an error occurs, %NULL will be returned.91*92* If debugfs is not enabled in the kernel, the value -%ENODEV will be93* returned. It is not wise to check for this value, but rather, check for94* %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling95* code.96*/97struct dentry *debugfs_create_u8(const char *name, mode_t mode,98struct dentry *parent, u8 *value)99{100/* if there are no write bits set, make read only */101if (!(mode & S_IWUGO))102return debugfs_create_file(name, mode, parent, value, &fops_u8_ro);103/* if there are no read bits set, make write only */104if (!(mode & S_IRUGO))105return debugfs_create_file(name, mode, parent, value, &fops_u8_wo);106107return debugfs_create_file(name, mode, parent, value, &fops_u8);108}109EXPORT_SYMBOL_GPL(debugfs_create_u8);110111static int debugfs_u16_set(void *data, u64 val)112{113*(u16 *)data = val;114return 0;115}116static int debugfs_u16_get(void *data, u64 *val)117{118*val = *(u16 *)data;119return 0;120}121DEFINE_SIMPLE_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");122DEFINE_SIMPLE_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n");123DEFINE_SIMPLE_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n");124125/**126* debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value127* @name: a pointer to a string containing the name of the file to create.128* @mode: the permission that the file should have129* @parent: a pointer to the parent dentry for this file. This should be a130* directory dentry if set. If this parameter is %NULL, then the131* file will be created in the root of the debugfs filesystem.132* @value: a pointer to the variable that the file should read to and write133* from.134*135* This function creates a file in debugfs with the given name that136* contains the value of the variable @value. If the @mode variable is so137* set, it can be read from, and written to.138*139* This function will return a pointer to a dentry if it succeeds. This140* pointer must be passed to the debugfs_remove() function when the file is141* to be removed (no automatic cleanup happens if your module is unloaded,142* you are responsible here.) If an error occurs, %NULL will be returned.143*144* If debugfs is not enabled in the kernel, the value -%ENODEV will be145* returned. It is not wise to check for this value, but rather, check for146* %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling147* code.148*/149struct dentry *debugfs_create_u16(const char *name, mode_t mode,150struct dentry *parent, u16 *value)151{152/* if there are no write bits set, make read only */153if (!(mode & S_IWUGO))154return debugfs_create_file(name, mode, parent, value, &fops_u16_ro);155/* if there are no read bits set, make write only */156if (!(mode & S_IRUGO))157return debugfs_create_file(name, mode, parent, value, &fops_u16_wo);158159return debugfs_create_file(name, mode, parent, value, &fops_u16);160}161EXPORT_SYMBOL_GPL(debugfs_create_u16);162163static int debugfs_u32_set(void *data, u64 val)164{165*(u32 *)data = val;166return 0;167}168static int debugfs_u32_get(void *data, u64 *val)169{170*val = *(u32 *)data;171return 0;172}173DEFINE_SIMPLE_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");174DEFINE_SIMPLE_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n");175DEFINE_SIMPLE_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n");176177/**178* debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value179* @name: a pointer to a string containing the name of the file to create.180* @mode: the permission that the file should have181* @parent: a pointer to the parent dentry for this file. This should be a182* directory dentry if set. If this parameter is %NULL, then the183* file will be created in the root of the debugfs filesystem.184* @value: a pointer to the variable that the file should read to and write185* from.186*187* This function creates a file in debugfs with the given name that188* contains the value of the variable @value. If the @mode variable is so189* set, it can be read from, and written to.190*191* This function will return a pointer to a dentry if it succeeds. This192* pointer must be passed to the debugfs_remove() function when the file is193* to be removed (no automatic cleanup happens if your module is unloaded,194* you are responsible here.) If an error occurs, %NULL will be returned.195*196* If debugfs is not enabled in the kernel, the value -%ENODEV will be197* returned. It is not wise to check for this value, but rather, check for198* %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling199* code.200*/201struct dentry *debugfs_create_u32(const char *name, mode_t mode,202struct dentry *parent, u32 *value)203{204/* if there are no write bits set, make read only */205if (!(mode & S_IWUGO))206return debugfs_create_file(name, mode, parent, value, &fops_u32_ro);207/* if there are no read bits set, make write only */208if (!(mode & S_IRUGO))209return debugfs_create_file(name, mode, parent, value, &fops_u32_wo);210211return debugfs_create_file(name, mode, parent, value, &fops_u32);212}213EXPORT_SYMBOL_GPL(debugfs_create_u32);214215static int debugfs_u64_set(void *data, u64 val)216{217*(u64 *)data = val;218return 0;219}220221static int debugfs_u64_get(void *data, u64 *val)222{223*val = *(u64 *)data;224return 0;225}226DEFINE_SIMPLE_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n");227DEFINE_SIMPLE_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n");228DEFINE_SIMPLE_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");229230/**231* debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value232* @name: a pointer to a string containing the name of the file to create.233* @mode: the permission that the file should have234* @parent: a pointer to the parent dentry for this file. This should be a235* directory dentry if set. If this parameter is %NULL, then the236* file will be created in the root of the debugfs filesystem.237* @value: a pointer to the variable that the file should read to and write238* from.239*240* This function creates a file in debugfs with the given name that241* contains the value of the variable @value. If the @mode variable is so242* set, it can be read from, and written to.243*244* This function will return a pointer to a dentry if it succeeds. This245* pointer must be passed to the debugfs_remove() function when the file is246* to be removed (no automatic cleanup happens if your module is unloaded,247* you are responsible here.) If an error occurs, %NULL will be returned.248*249* If debugfs is not enabled in the kernel, the value -%ENODEV will be250* returned. It is not wise to check for this value, but rather, check for251* %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling252* code.253*/254struct dentry *debugfs_create_u64(const char *name, mode_t mode,255struct dentry *parent, u64 *value)256{257/* if there are no write bits set, make read only */258if (!(mode & S_IWUGO))259return debugfs_create_file(name, mode, parent, value, &fops_u64_ro);260/* if there are no read bits set, make write only */261if (!(mode & S_IRUGO))262return debugfs_create_file(name, mode, parent, value, &fops_u64_wo);263264return debugfs_create_file(name, mode, parent, value, &fops_u64);265}266EXPORT_SYMBOL_GPL(debugfs_create_u64);267268DEFINE_SIMPLE_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n");269DEFINE_SIMPLE_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n");270DEFINE_SIMPLE_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n");271272DEFINE_SIMPLE_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set, "0x%04llx\n");273DEFINE_SIMPLE_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n");274DEFINE_SIMPLE_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n");275276DEFINE_SIMPLE_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set, "0x%08llx\n");277DEFINE_SIMPLE_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n");278DEFINE_SIMPLE_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n");279280DEFINE_SIMPLE_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set, "0x%016llx\n");281282/*283* 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 value284*285* These functions are exactly the same as the above functions (but use a hex286* output for the decimal challenged). For details look at the above unsigned287* decimal functions.288*/289290/**291* debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value292* @name: a pointer to a string containing the name of the file to create.293* @mode: the permission that the file should have294* @parent: a pointer to the parent dentry for this file. This should be a295* directory dentry if set. If this parameter is %NULL, then the296* file will be created in the root of the debugfs filesystem.297* @value: a pointer to the variable that the file should read to and write298* from.299*/300struct dentry *debugfs_create_x8(const char *name, mode_t mode,301struct dentry *parent, u8 *value)302{303/* if there are no write bits set, make read only */304if (!(mode & S_IWUGO))305return debugfs_create_file(name, mode, parent, value, &fops_x8_ro);306/* if there are no read bits set, make write only */307if (!(mode & S_IRUGO))308return debugfs_create_file(name, mode, parent, value, &fops_x8_wo);309310return debugfs_create_file(name, mode, parent, value, &fops_x8);311}312EXPORT_SYMBOL_GPL(debugfs_create_x8);313314/**315* debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value316* @name: a pointer to a string containing the name of the file to create.317* @mode: the permission that the file should have318* @parent: a pointer to the parent dentry for this file. This should be a319* directory dentry if set. If this parameter is %NULL, then the320* file will be created in the root of the debugfs filesystem.321* @value: a pointer to the variable that the file should read to and write322* from.323*/324struct dentry *debugfs_create_x16(const char *name, mode_t mode,325struct dentry *parent, u16 *value)326{327/* if there are no write bits set, make read only */328if (!(mode & S_IWUGO))329return debugfs_create_file(name, mode, parent, value, &fops_x16_ro);330/* if there are no read bits set, make write only */331if (!(mode & S_IRUGO))332return debugfs_create_file(name, mode, parent, value, &fops_x16_wo);333334return debugfs_create_file(name, mode, parent, value, &fops_x16);335}336EXPORT_SYMBOL_GPL(debugfs_create_x16);337338/**339* debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value340* @name: a pointer to a string containing the name of the file to create.341* @mode: the permission that the file should have342* @parent: a pointer to the parent dentry for this file. This should be a343* directory dentry if set. If this parameter is %NULL, then the344* file will be created in the root of the debugfs filesystem.345* @value: a pointer to the variable that the file should read to and write346* from.347*/348struct dentry *debugfs_create_x32(const char *name, mode_t mode,349struct dentry *parent, u32 *value)350{351/* if there are no write bits set, make read only */352if (!(mode & S_IWUGO))353return debugfs_create_file(name, mode, parent, value, &fops_x32_ro);354/* if there are no read bits set, make write only */355if (!(mode & S_IRUGO))356return debugfs_create_file(name, mode, parent, value, &fops_x32_wo);357358return debugfs_create_file(name, mode, parent, value, &fops_x32);359}360EXPORT_SYMBOL_GPL(debugfs_create_x32);361362/**363* debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value364* @name: a pointer to a string containing the name of the file to create.365* @mode: the permission that the file should have366* @parent: a pointer to the parent dentry for this file. This should be a367* directory dentry if set. If this parameter is %NULL, then the368* file will be created in the root of the debugfs filesystem.369* @value: a pointer to the variable that the file should read to and write370* from.371*/372struct dentry *debugfs_create_x64(const char *name, mode_t mode,373struct dentry *parent, u64 *value)374{375return debugfs_create_file(name, mode, parent, value, &fops_x64);376}377EXPORT_SYMBOL_GPL(debugfs_create_x64);378379380static int debugfs_size_t_set(void *data, u64 val)381{382*(size_t *)data = val;383return 0;384}385static int debugfs_size_t_get(void *data, u64 *val)386{387*val = *(size_t *)data;388return 0;389}390DEFINE_SIMPLE_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set,391"%llu\n"); /* %llu and %zu are more or less the same */392393/**394* debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value395* @name: a pointer to a string containing the name of the file to create.396* @mode: the permission that the file should have397* @parent: a pointer to the parent dentry for this file. This should be a398* directory dentry if set. If this parameter is %NULL, then the399* file will be created in the root of the debugfs filesystem.400* @value: a pointer to the variable that the file should read to and write401* from.402*/403struct dentry *debugfs_create_size_t(const char *name, mode_t mode,404struct dentry *parent, size_t *value)405{406return debugfs_create_file(name, mode, parent, value, &fops_size_t);407}408EXPORT_SYMBOL_GPL(debugfs_create_size_t);409410411static ssize_t read_file_bool(struct file *file, char __user *user_buf,412size_t count, loff_t *ppos)413{414char buf[3];415u32 *val = file->private_data;416417if (*val)418buf[0] = 'Y';419else420buf[0] = 'N';421buf[1] = '\n';422buf[2] = 0x00;423return simple_read_from_buffer(user_buf, count, ppos, buf, 2);424}425426static ssize_t write_file_bool(struct file *file, const char __user *user_buf,427size_t count, loff_t *ppos)428{429char buf[32];430size_t buf_size;431bool bv;432u32 *val = file->private_data;433434buf_size = min(count, (sizeof(buf)-1));435if (copy_from_user(buf, user_buf, buf_size))436return -EFAULT;437438if (strtobool(buf, &bv) == 0)439*val = bv;440441return count;442}443444static const struct file_operations fops_bool = {445.read = read_file_bool,446.write = write_file_bool,447.open = default_open,448.llseek = default_llseek,449};450451/**452* debugfs_create_bool - create a debugfs file that is used to read and write a boolean value453* @name: a pointer to a string containing the name of the file to create.454* @mode: the permission that the file should have455* @parent: a pointer to the parent dentry for this file. This should be a456* directory dentry if set. If this parameter is %NULL, then the457* file will be created in the root of the debugfs filesystem.458* @value: a pointer to the variable that the file should read to and write459* from.460*461* This function creates a file in debugfs with the given name that462* contains the value of the variable @value. If the @mode variable is so463* set, it can be read from, and written to.464*465* This function will return a pointer to a dentry if it succeeds. This466* pointer must be passed to the debugfs_remove() function when the file is467* to be removed (no automatic cleanup happens if your module is unloaded,468* you are responsible here.) If an error occurs, %NULL will be returned.469*470* If debugfs is not enabled in the kernel, the value -%ENODEV will be471* returned. It is not wise to check for this value, but rather, check for472* %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling473* code.474*/475struct dentry *debugfs_create_bool(const char *name, mode_t mode,476struct dentry *parent, u32 *value)477{478return debugfs_create_file(name, mode, parent, value, &fops_bool);479}480EXPORT_SYMBOL_GPL(debugfs_create_bool);481482static ssize_t read_file_blob(struct file *file, char __user *user_buf,483size_t count, loff_t *ppos)484{485struct debugfs_blob_wrapper *blob = file->private_data;486return simple_read_from_buffer(user_buf, count, ppos, blob->data,487blob->size);488}489490static const struct file_operations fops_blob = {491.read = read_file_blob,492.open = default_open,493.llseek = default_llseek,494};495496/**497* debugfs_create_blob - create a debugfs file that is used to read a binary blob498* @name: a pointer to a string containing the name of the file to create.499* @mode: the permission that the file should have500* @parent: a pointer to the parent dentry for this file. This should be a501* directory dentry if set. If this parameter is %NULL, then the502* file will be created in the root of the debugfs filesystem.503* @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer504* to the blob data and the size of the data.505*506* This function creates a file in debugfs with the given name that exports507* @blob->data as a binary blob. If the @mode variable is so set it can be508* read from. Writing is not supported.509*510* This function will return a pointer to a dentry if it succeeds. This511* pointer must be passed to the debugfs_remove() function when the file is512* to be removed (no automatic cleanup happens if your module is unloaded,513* you are responsible here.) If an error occurs, %NULL will be returned.514*515* If debugfs is not enabled in the kernel, the value -%ENODEV will be516* returned. It is not wise to check for this value, but rather, check for517* %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling518* code.519*/520struct dentry *debugfs_create_blob(const char *name, mode_t mode,521struct dentry *parent,522struct debugfs_blob_wrapper *blob)523{524return debugfs_create_file(name, mode, parent, blob, &fops_blob);525}526EXPORT_SYMBOL_GPL(debugfs_create_blob);527528529