^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (c) 2011 Bryan Schumaker <bjschuma@netapp.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Uses debugfs to create fault injection points for client testing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/debugfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/nsproxy.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/sunrpc/addr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include "state.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "netns.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) struct nfsd_fault_inject_op {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) char *file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) u64 (*get)(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) u64 (*set_val)(u64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) u64 (*set_clnt)(struct sockaddr_storage *, size_t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static struct dentry *debug_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) static ssize_t fault_inject_read(struct file *file, char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) size_t len, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) static u64 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) char read_buf[25];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) size_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) loff_t pos = *ppos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct nfsd_fault_inject_op *op = file_inode(file)->i_private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (!pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) val = op->get();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) size = scnprintf(read_buf, sizeof(read_buf), "%llu\n", val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) return simple_read_from_buffer(buf, len, ppos, read_buf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) static ssize_t fault_inject_write(struct file *file, const char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) size_t len, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) char write_buf[INET6_ADDRSTRLEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) size_t size = min(sizeof(write_buf) - 1, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct net *net = current->nsproxy->net_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct sockaddr_storage sa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct nfsd_fault_inject_op *op = file_inode(file)->i_private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) u64 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) char *nl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (copy_from_user(write_buf, buf, size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) write_buf[size] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) /* Deal with any embedded newlines in the string */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) nl = strchr(write_buf, '\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) if (nl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) size = nl - write_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) *nl = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) size = rpc_pton(net, write_buf, size, (struct sockaddr *)&sa, sizeof(sa));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (size > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) val = op->set_clnt(&sa, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) if (val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) pr_info("NFSD [%s]: Client %s had %llu state object(s)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) op->file, write_buf, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) val = simple_strtoll(write_buf, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (val == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) pr_info("NFSD Fault Injection: %s (all)", op->file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) pr_info("NFSD Fault Injection: %s (n = %llu)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) op->file, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) val = op->set_val(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) pr_info("NFSD: %s: found %llu", op->file, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return len; /* on success, claim we got the whole input */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) static const struct file_operations fops_nfsd = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) .read = fault_inject_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) .write = fault_inject_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) void nfsd_fault_inject_cleanup(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) debugfs_remove_recursive(debug_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) static struct nfsd_fault_inject_op inject_ops[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) .file = "forget_clients",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) .get = nfsd_inject_print_clients,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) .set_val = nfsd_inject_forget_clients,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) .set_clnt = nfsd_inject_forget_client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) .file = "forget_locks",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) .get = nfsd_inject_print_locks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) .set_val = nfsd_inject_forget_locks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) .set_clnt = nfsd_inject_forget_client_locks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) .file = "forget_openowners",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) .get = nfsd_inject_print_openowners,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) .set_val = nfsd_inject_forget_openowners,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) .set_clnt = nfsd_inject_forget_client_openowners,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) .file = "forget_delegations",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) .get = nfsd_inject_print_delegations,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) .set_val = nfsd_inject_forget_delegations,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) .set_clnt = nfsd_inject_forget_client_delegations,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) .file = "recall_delegations",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) .get = nfsd_inject_print_delegations,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) .set_val = nfsd_inject_recall_delegations,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) .set_clnt = nfsd_inject_recall_client_delegations,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) void nfsd_fault_inject_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) struct nfsd_fault_inject_op *op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) umode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) debug_dir = debugfs_create_dir("nfsd", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) for (i = 0; i < ARRAY_SIZE(inject_ops); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) op = &inject_ops[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) debugfs_create_file(op->file, mode, debug_dir, op, &fops_nfsd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }