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