1 // SPDX-License-Identifier: GPL-2.0
3 * linux/fs/nfs/cache_lib.c
5 * Helper routines for the NFS client caches
7 * Copyright (c) 2009 Trond Myklebust <Trond.Myklebust@netapp.com>
9 #include <linux/kmod.h>
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <linux/mount.h>
13 #include <linux/namei.h>
14 #include <linux/slab.h>
15 #include <linux/sunrpc/cache.h>
16 #include <linux/sunrpc/rpc_pipe_fs.h>
17 #include <net/net_namespace.h>
19 #include "cache_lib.h"
21 #define NFS_CACHE_UPCALL_PATHLEN 256
22 #define NFS_CACHE_UPCALL_TIMEOUT 15
24 static char nfs_cache_getent_prog
[NFS_CACHE_UPCALL_PATHLEN
] =
25 "/sbin/nfs_cache_getent";
26 static unsigned long nfs_cache_getent_timeout
= NFS_CACHE_UPCALL_TIMEOUT
;
28 module_param_string(cache_getent
, nfs_cache_getent_prog
,
29 sizeof(nfs_cache_getent_prog
), 0600);
30 MODULE_PARM_DESC(cache_getent
, "Path to the client cache upcall program");
31 module_param_named(cache_getent_timeout
, nfs_cache_getent_timeout
, ulong
, 0600);
32 MODULE_PARM_DESC(cache_getent_timeout
, "Timeout (in seconds) after which "
33 "the cache upcall is assumed to have failed");
35 int nfs_cache_upcall(struct cache_detail
*cd
, char *entry_name
)
37 static char *envp
[] = { "HOME=/",
39 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
43 nfs_cache_getent_prog
,
50 if (nfs_cache_getent_prog
[0] == '\0')
52 ret
= call_usermodehelper(argv
[0], argv
, envp
, UMH_WAIT_EXEC
);
54 * Disable the upcall mechanism if we're getting an ENOENT or
55 * EACCES error. The admin can re-enable it on the fly by using
56 * sysfs to set the 'cache_getent' parameter once the problem
59 if (ret
== -ENOENT
|| ret
== -EACCES
)
60 nfs_cache_getent_prog
[0] = '\0';
62 return ret
> 0 ? 0 : ret
;
66 * Deferred request handling
68 void nfs_cache_defer_req_put(struct nfs_cache_defer_req
*dreq
)
70 if (atomic_dec_and_test(&dreq
->count
))
74 static void nfs_dns_cache_revisit(struct cache_deferred_req
*d
, int toomany
)
76 struct nfs_cache_defer_req
*dreq
;
78 dreq
= container_of(d
, struct nfs_cache_defer_req
, deferred_req
);
80 complete(&dreq
->completion
);
81 nfs_cache_defer_req_put(dreq
);
84 static struct cache_deferred_req
*nfs_dns_cache_defer(struct cache_req
*req
)
86 struct nfs_cache_defer_req
*dreq
;
88 dreq
= container_of(req
, struct nfs_cache_defer_req
, req
);
89 dreq
->deferred_req
.revisit
= nfs_dns_cache_revisit
;
90 atomic_inc(&dreq
->count
);
92 return &dreq
->deferred_req
;
95 struct nfs_cache_defer_req
*nfs_cache_defer_req_alloc(void)
97 struct nfs_cache_defer_req
*dreq
;
99 dreq
= kzalloc(sizeof(*dreq
), GFP_KERNEL
);
101 init_completion(&dreq
->completion
);
102 atomic_set(&dreq
->count
, 1);
103 dreq
->req
.defer
= nfs_dns_cache_defer
;
108 int nfs_cache_wait_for_upcall(struct nfs_cache_defer_req
*dreq
)
110 if (wait_for_completion_timeout(&dreq
->completion
,
111 nfs_cache_getent_timeout
* HZ
) == 0)
116 int nfs_cache_register_sb(struct super_block
*sb
, struct cache_detail
*cd
)
121 dir
= rpc_d_lookup_sb(sb
, "cache");
122 ret
= sunrpc_cache_register_pipefs(dir
, cd
->name
, 0600, cd
);
127 int nfs_cache_register_net(struct net
*net
, struct cache_detail
*cd
)
129 struct super_block
*pipefs_sb
;
132 sunrpc_init_cache_detail(cd
);
133 pipefs_sb
= rpc_get_sb_net(net
);
135 ret
= nfs_cache_register_sb(pipefs_sb
, cd
);
138 sunrpc_destroy_cache_detail(cd
);
143 void nfs_cache_unregister_sb(struct super_block
*sb
, struct cache_detail
*cd
)
145 sunrpc_cache_unregister_pipefs(cd
);
148 void nfs_cache_unregister_net(struct net
*net
, struct cache_detail
*cd
)
150 struct super_block
*pipefs_sb
;
152 pipefs_sb
= rpc_get_sb_net(net
);
154 nfs_cache_unregister_sb(pipefs_sb
, cd
);
157 sunrpc_destroy_cache_detail(cd
);