xref: /linux/fs/nfs/cache_lib.c (revision 664b0bae0b87f69bc9deb098f5e0158b9cf18e04)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2e571cbf1STrond Myklebust /*
3e571cbf1STrond Myklebust  * linux/fs/nfs/cache_lib.c
4e571cbf1STrond Myklebust  *
5e571cbf1STrond Myklebust  * Helper routines for the NFS client caches
6e571cbf1STrond Myklebust  *
7e571cbf1STrond Myklebust  * Copyright (c) 2009 Trond Myklebust <Trond.Myklebust@netapp.com>
8e571cbf1STrond Myklebust  */
9e571cbf1STrond Myklebust #include <linux/kmod.h>
10e571cbf1STrond Myklebust #include <linux/module.h>
11e571cbf1STrond Myklebust #include <linux/moduleparam.h>
12e571cbf1STrond Myklebust #include <linux/mount.h>
13e571cbf1STrond Myklebust #include <linux/namei.h>
145a0e3ad6STejun Heo #include <linux/slab.h>
15e571cbf1STrond Myklebust #include <linux/sunrpc/cache.h>
16e571cbf1STrond Myklebust #include <linux/sunrpc/rpc_pipe_fs.h>
175c1cacb1SStanislav Kinsbursky #include <net/net_namespace.h>
18e571cbf1STrond Myklebust 
19e571cbf1STrond Myklebust #include "cache_lib.h"
20e571cbf1STrond Myklebust 
21e571cbf1STrond Myklebust #define NFS_CACHE_UPCALL_PATHLEN 256
22e571cbf1STrond Myklebust #define NFS_CACHE_UPCALL_TIMEOUT 15
23e571cbf1STrond Myklebust 
24e571cbf1STrond Myklebust static char nfs_cache_getent_prog[NFS_CACHE_UPCALL_PATHLEN] =
25e571cbf1STrond Myklebust 				"/sbin/nfs_cache_getent";
26e571cbf1STrond Myklebust static unsigned long nfs_cache_getent_timeout = NFS_CACHE_UPCALL_TIMEOUT;
27e571cbf1STrond Myklebust 
28e571cbf1STrond Myklebust module_param_string(cache_getent, nfs_cache_getent_prog,
29e571cbf1STrond Myklebust 		sizeof(nfs_cache_getent_prog), 0600);
30e571cbf1STrond Myklebust MODULE_PARM_DESC(cache_getent, "Path to the client cache upcall program");
31e571cbf1STrond Myklebust module_param_named(cache_getent_timeout, nfs_cache_getent_timeout, ulong, 0600);
32e571cbf1STrond Myklebust MODULE_PARM_DESC(cache_getent_timeout, "Timeout (in seconds) after which "
33e571cbf1STrond Myklebust 		"the cache upcall is assumed to have failed");
34e571cbf1STrond Myklebust 
nfs_cache_upcall(struct cache_detail * cd,char * entry_name)35e571cbf1STrond Myklebust int nfs_cache_upcall(struct cache_detail *cd, char *entry_name)
36e571cbf1STrond Myklebust {
37e571cbf1STrond Myklebust 	static char *envp[] = { "HOME=/",
38e571cbf1STrond Myklebust 		"TERM=linux",
39e571cbf1STrond Myklebust 		"PATH=/sbin:/usr/sbin:/bin:/usr/bin",
40e571cbf1STrond Myklebust 		NULL
41e571cbf1STrond Myklebust 	};
42e571cbf1STrond Myklebust 	char *argv[] = {
43e571cbf1STrond Myklebust 		nfs_cache_getent_prog,
44e571cbf1STrond Myklebust 		cd->name,
45e571cbf1STrond Myklebust 		entry_name,
46e571cbf1STrond Myklebust 		NULL
47e571cbf1STrond Myklebust 	};
48e571cbf1STrond Myklebust 	int ret = -EACCES;
49e571cbf1STrond Myklebust 
50e571cbf1STrond Myklebust 	if (nfs_cache_getent_prog[0] == '\0')
51e571cbf1STrond Myklebust 		goto out;
52e571cbf1STrond Myklebust 	ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
53e571cbf1STrond Myklebust 	/*
54e571cbf1STrond Myklebust 	 * Disable the upcall mechanism if we're getting an ENOENT or
55e571cbf1STrond Myklebust 	 * EACCES error. The admin can re-enable it on the fly by using
56e571cbf1STrond Myklebust 	 * sysfs to set the 'cache_getent' parameter once the problem
57e571cbf1STrond Myklebust 	 * has been fixed.
58e571cbf1STrond Myklebust 	 */
59e571cbf1STrond Myklebust 	if (ret == -ENOENT || ret == -EACCES)
60e571cbf1STrond Myklebust 		nfs_cache_getent_prog[0] = '\0';
61e571cbf1STrond Myklebust out:
62e571cbf1STrond Myklebust 	return ret > 0 ? 0 : ret;
63e571cbf1STrond Myklebust }
64e571cbf1STrond Myklebust 
65e571cbf1STrond Myklebust /*
66e571cbf1STrond Myklebust  * Deferred request handling
67e571cbf1STrond Myklebust  */
nfs_cache_defer_req_put(struct nfs_cache_defer_req * dreq)68e571cbf1STrond Myklebust void nfs_cache_defer_req_put(struct nfs_cache_defer_req *dreq)
69e571cbf1STrond Myklebust {
70*0896cadeSElena Reshetova 	if (refcount_dec_and_test(&dreq->count))
71e571cbf1STrond Myklebust 		kfree(dreq);
72e571cbf1STrond Myklebust }
73e571cbf1STrond Myklebust 
nfs_dns_cache_revisit(struct cache_deferred_req * d,int toomany)74e571cbf1STrond Myklebust static void nfs_dns_cache_revisit(struct cache_deferred_req *d, int toomany)
75e571cbf1STrond Myklebust {
76e571cbf1STrond Myklebust 	struct nfs_cache_defer_req *dreq;
77e571cbf1STrond Myklebust 
78e571cbf1STrond Myklebust 	dreq = container_of(d, struct nfs_cache_defer_req, deferred_req);
79e571cbf1STrond Myklebust 
802a446a5dSDaniel Wagner 	complete(&dreq->completion);
81e571cbf1STrond Myklebust 	nfs_cache_defer_req_put(dreq);
82e571cbf1STrond Myklebust }
83e571cbf1STrond Myklebust 
nfs_dns_cache_defer(struct cache_req * req)84e571cbf1STrond Myklebust static struct cache_deferred_req *nfs_dns_cache_defer(struct cache_req *req)
85e571cbf1STrond Myklebust {
86e571cbf1STrond Myklebust 	struct nfs_cache_defer_req *dreq;
87e571cbf1STrond Myklebust 
88e571cbf1STrond Myklebust 	dreq = container_of(req, struct nfs_cache_defer_req, req);
89e571cbf1STrond Myklebust 	dreq->deferred_req.revisit = nfs_dns_cache_revisit;
90*0896cadeSElena Reshetova 	refcount_inc(&dreq->count);
91e571cbf1STrond Myklebust 
92e571cbf1STrond Myklebust 	return &dreq->deferred_req;
93e571cbf1STrond Myklebust }
94e571cbf1STrond Myklebust 
nfs_cache_defer_req_alloc(void)95e571cbf1STrond Myklebust struct nfs_cache_defer_req *nfs_cache_defer_req_alloc(void)
96e571cbf1STrond Myklebust {
97e571cbf1STrond Myklebust 	struct nfs_cache_defer_req *dreq;
98e571cbf1STrond Myklebust 
99e571cbf1STrond Myklebust 	dreq = kzalloc(sizeof(*dreq), GFP_KERNEL);
100e571cbf1STrond Myklebust 	if (dreq) {
101e571cbf1STrond Myklebust 		init_completion(&dreq->completion);
102*0896cadeSElena Reshetova 		refcount_set(&dreq->count, 1);
103e571cbf1STrond Myklebust 		dreq->req.defer = nfs_dns_cache_defer;
104e571cbf1STrond Myklebust 	}
105e571cbf1STrond Myklebust 	return dreq;
106e571cbf1STrond Myklebust }
107e571cbf1STrond Myklebust 
nfs_cache_wait_for_upcall(struct nfs_cache_defer_req * dreq)108e571cbf1STrond Myklebust int nfs_cache_wait_for_upcall(struct nfs_cache_defer_req *dreq)
109e571cbf1STrond Myklebust {
110e571cbf1STrond Myklebust 	if (wait_for_completion_timeout(&dreq->completion,
111e571cbf1STrond Myklebust 			nfs_cache_getent_timeout * HZ) == 0)
112e571cbf1STrond Myklebust 		return -ETIMEDOUT;
113e571cbf1STrond Myklebust 	return 0;
114e571cbf1STrond Myklebust }
115e571cbf1STrond Myklebust 
nfs_cache_register_sb(struct super_block * sb,struct cache_detail * cd)1169df69c81SStanislav Kinsbursky int nfs_cache_register_sb(struct super_block *sb, struct cache_detail *cd)
1175c1cacb1SStanislav Kinsbursky {
1185c1cacb1SStanislav Kinsbursky 	int ret;
1195c1cacb1SStanislav Kinsbursky 	struct dentry *dir;
1205c1cacb1SStanislav Kinsbursky 
1215c1cacb1SStanislav Kinsbursky 	dir = rpc_d_lookup_sb(sb, "cache");
1225c1cacb1SStanislav Kinsbursky 	ret = sunrpc_cache_register_pipefs(dir, cd->name, 0600, cd);
1235c1cacb1SStanislav Kinsbursky 	dput(dir);
1245c1cacb1SStanislav Kinsbursky 	return ret;
1255c1cacb1SStanislav Kinsbursky }
1265c1cacb1SStanislav Kinsbursky 
nfs_cache_register_net(struct net * net,struct cache_detail * cd)1275c1cacb1SStanislav Kinsbursky int nfs_cache_register_net(struct net *net, struct cache_detail *cd)
128e571cbf1STrond Myklebust {
1295c1cacb1SStanislav Kinsbursky 	struct super_block *pipefs_sb;
13039cb67b9SStanislav Kinsbursky 	int ret = 0;
131e571cbf1STrond Myklebust 
132462b8f6bSStanislav Kinsbursky 	sunrpc_init_cache_detail(cd);
1335c1cacb1SStanislav Kinsbursky 	pipefs_sb = rpc_get_sb_net(net);
13439cb67b9SStanislav Kinsbursky 	if (pipefs_sb) {
1355c1cacb1SStanislav Kinsbursky 		ret = nfs_cache_register_sb(pipefs_sb, cd);
1365c1cacb1SStanislav Kinsbursky 		rpc_put_sb_net(net);
137462b8f6bSStanislav Kinsbursky 		if (ret)
138462b8f6bSStanislav Kinsbursky 			sunrpc_destroy_cache_detail(cd);
13939cb67b9SStanislav Kinsbursky 	}
140e571cbf1STrond Myklebust 	return ret;
141e571cbf1STrond Myklebust }
142e571cbf1STrond Myklebust 
nfs_cache_unregister_sb(struct super_block * sb,struct cache_detail * cd)1439df69c81SStanislav Kinsbursky void nfs_cache_unregister_sb(struct super_block *sb, struct cache_detail *cd)
144e571cbf1STrond Myklebust {
145e571cbf1STrond Myklebust 	sunrpc_cache_unregister_pipefs(cd);
1465c1cacb1SStanislav Kinsbursky }
1475c1cacb1SStanislav Kinsbursky 
nfs_cache_unregister_net(struct net * net,struct cache_detail * cd)1485c1cacb1SStanislav Kinsbursky void nfs_cache_unregister_net(struct net *net, struct cache_detail *cd)
1495c1cacb1SStanislav Kinsbursky {
1505c1cacb1SStanislav Kinsbursky 	struct super_block *pipefs_sb;
1515c1cacb1SStanislav Kinsbursky 
1525c1cacb1SStanislav Kinsbursky 	pipefs_sb = rpc_get_sb_net(net);
1535c1cacb1SStanislav Kinsbursky 	if (pipefs_sb) {
1545c1cacb1SStanislav Kinsbursky 		nfs_cache_unregister_sb(pipefs_sb, cd);
1555c1cacb1SStanislav Kinsbursky 		rpc_put_sb_net(net);
1565c1cacb1SStanislav Kinsbursky 	}
1579222b955SStanislav Kinsbursky 	sunrpc_destroy_cache_detail(cd);
1589222b955SStanislav Kinsbursky }
159