xref: /linux/fs/nfs/cache_lib.c (revision 60063497a95e716c9a689af3be2687d261f115b4)
1 /*
2  * linux/fs/nfs/cache_lib.c
3  *
4  * Helper routines for the NFS client caches
5  *
6  * Copyright (c) 2009 Trond Myklebust <Trond.Myklebust@netapp.com>
7  */
8 #include <linux/kmod.h>
9 #include <linux/module.h>
10 #include <linux/moduleparam.h>
11 #include <linux/mount.h>
12 #include <linux/namei.h>
13 #include <linux/slab.h>
14 #include <linux/sunrpc/cache.h>
15 #include <linux/sunrpc/rpc_pipe_fs.h>
16 
17 #include "cache_lib.h"
18 
19 #define NFS_CACHE_UPCALL_PATHLEN 256
20 #define NFS_CACHE_UPCALL_TIMEOUT 15
21 
22 static char nfs_cache_getent_prog[NFS_CACHE_UPCALL_PATHLEN] =
23 				"/sbin/nfs_cache_getent";
24 static unsigned long nfs_cache_getent_timeout = NFS_CACHE_UPCALL_TIMEOUT;
25 
26 module_param_string(cache_getent, nfs_cache_getent_prog,
27 		sizeof(nfs_cache_getent_prog), 0600);
28 MODULE_PARM_DESC(cache_getent, "Path to the client cache upcall program");
29 module_param_named(cache_getent_timeout, nfs_cache_getent_timeout, ulong, 0600);
30 MODULE_PARM_DESC(cache_getent_timeout, "Timeout (in seconds) after which "
31 		"the cache upcall is assumed to have failed");
32 
33 int nfs_cache_upcall(struct cache_detail *cd, char *entry_name)
34 {
35 	static char *envp[] = { "HOME=/",
36 		"TERM=linux",
37 		"PATH=/sbin:/usr/sbin:/bin:/usr/bin",
38 		NULL
39 	};
40 	char *argv[] = {
41 		nfs_cache_getent_prog,
42 		cd->name,
43 		entry_name,
44 		NULL
45 	};
46 	int ret = -EACCES;
47 
48 	if (nfs_cache_getent_prog[0] == '\0')
49 		goto out;
50 	ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
51 	/*
52 	 * Disable the upcall mechanism if we're getting an ENOENT or
53 	 * EACCES error. The admin can re-enable it on the fly by using
54 	 * sysfs to set the 'cache_getent' parameter once the problem
55 	 * has been fixed.
56 	 */
57 	if (ret == -ENOENT || ret == -EACCES)
58 		nfs_cache_getent_prog[0] = '\0';
59 out:
60 	return ret > 0 ? 0 : ret;
61 }
62 
63 /*
64  * Deferred request handling
65  */
66 void nfs_cache_defer_req_put(struct nfs_cache_defer_req *dreq)
67 {
68 	if (atomic_dec_and_test(&dreq->count))
69 		kfree(dreq);
70 }
71 
72 static void nfs_dns_cache_revisit(struct cache_deferred_req *d, int toomany)
73 {
74 	struct nfs_cache_defer_req *dreq;
75 
76 	dreq = container_of(d, struct nfs_cache_defer_req, deferred_req);
77 
78 	complete_all(&dreq->completion);
79 	nfs_cache_defer_req_put(dreq);
80 }
81 
82 static struct cache_deferred_req *nfs_dns_cache_defer(struct cache_req *req)
83 {
84 	struct nfs_cache_defer_req *dreq;
85 
86 	dreq = container_of(req, struct nfs_cache_defer_req, req);
87 	dreq->deferred_req.revisit = nfs_dns_cache_revisit;
88 	atomic_inc(&dreq->count);
89 
90 	return &dreq->deferred_req;
91 }
92 
93 struct nfs_cache_defer_req *nfs_cache_defer_req_alloc(void)
94 {
95 	struct nfs_cache_defer_req *dreq;
96 
97 	dreq = kzalloc(sizeof(*dreq), GFP_KERNEL);
98 	if (dreq) {
99 		init_completion(&dreq->completion);
100 		atomic_set(&dreq->count, 1);
101 		dreq->req.defer = nfs_dns_cache_defer;
102 	}
103 	return dreq;
104 }
105 
106 int nfs_cache_wait_for_upcall(struct nfs_cache_defer_req *dreq)
107 {
108 	if (wait_for_completion_timeout(&dreq->completion,
109 			nfs_cache_getent_timeout * HZ) == 0)
110 		return -ETIMEDOUT;
111 	return 0;
112 }
113 
114 int nfs_cache_register(struct cache_detail *cd)
115 {
116 	struct vfsmount *mnt;
117 	struct path path;
118 	int ret;
119 
120 	mnt = rpc_get_mount();
121 	if (IS_ERR(mnt))
122 		return PTR_ERR(mnt);
123 	ret = vfs_path_lookup(mnt->mnt_root, mnt, "/cache", 0, &path);
124 	if (ret)
125 		goto err;
126 	ret = sunrpc_cache_register_pipefs(path.dentry, cd->name, 0600, cd);
127 	path_put(&path);
128 	if (!ret)
129 		return ret;
130 err:
131 	rpc_put_mount();
132 	return ret;
133 }
134 
135 void nfs_cache_unregister(struct cache_detail *cd)
136 {
137 	sunrpc_cache_unregister_pipefs(cd);
138 	rpc_put_mount();
139 }
140 
141