Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (c) 2011 Bryan Schumaker <bjschuma@netapp.com>
0004  *
0005  * Uses debugfs to create fault injection points for client testing
0006  */
0007 
0008 #include <linux/types.h>
0009 #include <linux/fs.h>
0010 #include <linux/debugfs.h>
0011 #include <linux/module.h>
0012 #include <linux/nsproxy.h>
0013 #include <linux/sunrpc/addr.h>
0014 #include <linux/uaccess.h>
0015 #include <linux/kernel.h>
0016 
0017 #include "state.h"
0018 #include "netns.h"
0019 
0020 struct nfsd_fault_inject_op {
0021     char *file;
0022     u64 (*get)(void);
0023     u64 (*set_val)(u64);
0024     u64 (*set_clnt)(struct sockaddr_storage *, size_t);
0025 };
0026 
0027 static struct dentry *debug_dir;
0028 
0029 static ssize_t fault_inject_read(struct file *file, char __user *buf,
0030                  size_t len, loff_t *ppos)
0031 {
0032     static u64 val;
0033     char read_buf[25];
0034     size_t size;
0035     loff_t pos = *ppos;
0036     struct nfsd_fault_inject_op *op = file_inode(file)->i_private;
0037 
0038     if (!pos)
0039         val = op->get();
0040     size = scnprintf(read_buf, sizeof(read_buf), "%llu\n", val);
0041 
0042     return simple_read_from_buffer(buf, len, ppos, read_buf, size);
0043 }
0044 
0045 static ssize_t fault_inject_write(struct file *file, const char __user *buf,
0046                   size_t len, loff_t *ppos)
0047 {
0048     char write_buf[INET6_ADDRSTRLEN];
0049     size_t size = min(sizeof(write_buf) - 1, len);
0050     struct net *net = current->nsproxy->net_ns;
0051     struct sockaddr_storage sa;
0052     struct nfsd_fault_inject_op *op = file_inode(file)->i_private;
0053     u64 val;
0054     char *nl;
0055 
0056     if (copy_from_user(write_buf, buf, size))
0057         return -EFAULT;
0058     write_buf[size] = '\0';
0059 
0060     /* Deal with any embedded newlines in the string */
0061     nl = strchr(write_buf, '\n');
0062     if (nl) {
0063         size = nl - write_buf;
0064         *nl = '\0';
0065     }
0066 
0067     size = rpc_pton(net, write_buf, size, (struct sockaddr *)&sa, sizeof(sa));
0068     if (size > 0) {
0069         val = op->set_clnt(&sa, size);
0070         if (val)
0071             pr_info("NFSD [%s]: Client %s had %llu state object(s)\n",
0072                 op->file, write_buf, val);
0073     } else {
0074         val = simple_strtoll(write_buf, NULL, 0);
0075         if (val == 0)
0076             pr_info("NFSD Fault Injection: %s (all)", op->file);
0077         else
0078             pr_info("NFSD Fault Injection: %s (n = %llu)",
0079                 op->file, val);
0080         val = op->set_val(val);
0081         pr_info("NFSD: %s: found %llu", op->file, val);
0082     }
0083     return len; /* on success, claim we got the whole input */
0084 }
0085 
0086 static const struct file_operations fops_nfsd = {
0087     .owner   = THIS_MODULE,
0088     .read    = fault_inject_read,
0089     .write   = fault_inject_write,
0090 };
0091 
0092 void nfsd_fault_inject_cleanup(void)
0093 {
0094     debugfs_remove_recursive(debug_dir);
0095 }
0096 
0097 static struct nfsd_fault_inject_op inject_ops[] = {
0098     {
0099         .file     = "forget_clients",
0100         .get      = nfsd_inject_print_clients,
0101         .set_val  = nfsd_inject_forget_clients,
0102         .set_clnt = nfsd_inject_forget_client,
0103     },
0104     {
0105         .file     = "forget_locks",
0106         .get      = nfsd_inject_print_locks,
0107         .set_val  = nfsd_inject_forget_locks,
0108         .set_clnt = nfsd_inject_forget_client_locks,
0109     },
0110     {
0111         .file     = "forget_openowners",
0112         .get      = nfsd_inject_print_openowners,
0113         .set_val  = nfsd_inject_forget_openowners,
0114         .set_clnt = nfsd_inject_forget_client_openowners,
0115     },
0116     {
0117         .file     = "forget_delegations",
0118         .get      = nfsd_inject_print_delegations,
0119         .set_val  = nfsd_inject_forget_delegations,
0120         .set_clnt = nfsd_inject_forget_client_delegations,
0121     },
0122     {
0123         .file     = "recall_delegations",
0124         .get      = nfsd_inject_print_delegations,
0125         .set_val  = nfsd_inject_recall_delegations,
0126         .set_clnt = nfsd_inject_recall_client_delegations,
0127     },
0128 };
0129 
0130 void nfsd_fault_inject_init(void)
0131 {
0132     unsigned int i;
0133     struct nfsd_fault_inject_op *op;
0134     umode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
0135 
0136     debug_dir = debugfs_create_dir("nfsd", NULL);
0137 
0138     for (i = 0; i < ARRAY_SIZE(inject_ops); i++) {
0139         op = &inject_ops[i];
0140         debugfs_create_file(op->file, mode, debug_dir, op, &fops_nfsd);
0141     }
0142 }