2 * Copyright (c) 2011 Bryan Schumaker <bjschuma@netapp.com>
4 * Uses debugfs to create fault injection points for client testing
7 #include <linux/types.h>
9 #include <linux/debugfs.h>
10 #include <linux/module.h>
11 #include <linux/nsproxy.h>
12 #include <linux/sunrpc/addr.h>
13 #include <asm/uaccess.h>
18 struct nfsd_fault_inject_op
{
20 u64 (*forget
)(struct nfs4_client
*, u64
);
21 u64 (*print
)(struct nfs4_client
*, u64
);
24 static struct nfsd_fault_inject_op inject_ops
[] = {
26 .file
= "forget_clients",
27 .forget
= nfsd_forget_client
,
28 .print
= nfsd_print_client
,
31 .file
= "forget_locks",
32 .forget
= nfsd_forget_client_locks
,
33 .print
= nfsd_print_client_locks
,
36 .file
= "forget_openowners",
37 .forget
= nfsd_forget_client_openowners
,
38 .print
= nfsd_print_client_openowners
,
41 .file
= "forget_delegations",
42 .forget
= nfsd_forget_client_delegations
,
43 .print
= nfsd_print_client_delegations
,
46 .file
= "recall_delegations",
47 .forget
= nfsd_recall_client_delegations
,
48 .print
= nfsd_print_client_delegations
,
52 static long int NUM_INJECT_OPS
= sizeof(inject_ops
) / sizeof(struct nfsd_fault_inject_op
);
53 static struct dentry
*debug_dir
;
55 static void nfsd_inject_set(struct nfsd_fault_inject_op
*op
, u64 val
)
60 printk(KERN_INFO
"NFSD Fault Injection: %s (all)", op
->file
);
62 printk(KERN_INFO
"NFSD Fault Injection: %s (n = %llu)", op
->file
, val
);
65 count
= nfsd_for_n_state(val
, op
->forget
);
67 printk(KERN_INFO
"NFSD: %s: found %llu", op
->file
, count
);
70 static void nfsd_inject_set_client(struct nfsd_fault_inject_op
*op
,
71 struct sockaddr_storage
*addr
,
74 char buf
[INET6_ADDRSTRLEN
];
75 struct nfs4_client
*clp
;
79 clp
= nfsd_find_client(addr
, addr_size
);
81 count
= op
->forget(clp
, 0);
82 rpc_ntop((struct sockaddr
*)&clp
->cl_addr
, buf
, sizeof(buf
));
83 printk(KERN_INFO
"NFSD [%s]: Client %s had %llu state object(s)\n", op
->file
, buf
, count
);
88 static void nfsd_inject_get(struct nfsd_fault_inject_op
*op
, u64
*val
)
91 *val
= nfsd_for_n_state(0, op
->print
);
95 static ssize_t
fault_inject_read(struct file
*file
, char __user
*buf
,
96 size_t len
, loff_t
*ppos
)
104 nfsd_inject_get(file_inode(file
)->i_private
, &val
);
105 size
= scnprintf(read_buf
, sizeof(read_buf
), "%llu\n", val
);
107 return simple_read_from_buffer(buf
, len
, ppos
, read_buf
, size
);
110 static ssize_t
fault_inject_write(struct file
*file
, const char __user
*buf
,
111 size_t len
, loff_t
*ppos
)
113 char write_buf
[INET6_ADDRSTRLEN
];
114 size_t size
= min(sizeof(write_buf
) - 1, len
);
115 struct net
*net
= current
->nsproxy
->net_ns
;
116 struct sockaddr_storage sa
;
119 if (copy_from_user(write_buf
, buf
, size
))
121 write_buf
[size
] = '\0';
123 size
= rpc_pton(net
, write_buf
, size
, (struct sockaddr
*)&sa
, sizeof(sa
));
125 nfsd_inject_set_client(file_inode(file
)->i_private
, &sa
, size
);
127 val
= simple_strtoll(write_buf
, NULL
, 0);
128 nfsd_inject_set(file_inode(file
)->i_private
, val
);
130 return len
; /* on success, claim we got the whole input */
133 static const struct file_operations fops_nfsd
= {
134 .owner
= THIS_MODULE
,
135 .read
= fault_inject_read
,
136 .write
= fault_inject_write
,
139 void nfsd_fault_inject_cleanup(void)
141 debugfs_remove_recursive(debug_dir
);
144 int nfsd_fault_inject_init(void)
147 struct nfsd_fault_inject_op
*op
;
148 umode_t mode
= S_IFREG
| S_IRUSR
| S_IWUSR
;
150 debug_dir
= debugfs_create_dir("nfsd", NULL
);
154 for (i
= 0; i
< NUM_INJECT_OPS
; i
++) {
156 if (!debugfs_create_file(op
->file
, mode
, debug_dir
, op
, &fops_nfsd
))
162 nfsd_fault_inject_cleanup();