^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * linux/fs/lockd/clntproc.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * RPC procedures for the client side NLM implementation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^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/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/nfs_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/utsname.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/freezer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/sunrpc/clnt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/sunrpc/svc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/lockd/lockd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define NLMDBG_FACILITY NLMDBG_CLIENT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define NLMCLNT_GRACE_WAIT (5*HZ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define NLMCLNT_POLL_TIMEOUT (30*HZ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define NLMCLNT_MAX_RETRIES 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static int nlmclnt_test(struct nlm_rqst *, struct file_lock *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) static int nlmclnt_lock(struct nlm_rqst *, struct file_lock *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) static int nlmclnt_unlock(struct nlm_rqst *, struct file_lock *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) static int nlm_stat_to_errno(__be32 stat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) static int nlmclnt_cancel(struct nlm_host *, int , struct file_lock *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static const struct rpc_call_ops nlmclnt_unlock_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) static const struct rpc_call_ops nlmclnt_cancel_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * Cookie counter for NLM requests
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) static atomic_t nlm_cookie = ATOMIC_INIT(0x1234);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) void nlmclnt_next_cookie(struct nlm_cookie *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) u32 cookie = atomic_inc_return(&nlm_cookie);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) memcpy(c->data, &cookie, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) c->len=4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) static struct nlm_lockowner *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) nlmclnt_get_lockowner(struct nlm_lockowner *lockowner)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) refcount_inc(&lockowner->count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return lockowner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) static void nlmclnt_put_lockowner(struct nlm_lockowner *lockowner)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) if (!refcount_dec_and_lock(&lockowner->count, &lockowner->host->h_lock))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) list_del(&lockowner->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) spin_unlock(&lockowner->host->h_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) nlmclnt_release_host(lockowner->host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) kfree(lockowner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static inline int nlm_pidbusy(struct nlm_host *host, uint32_t pid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct nlm_lockowner *lockowner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) list_for_each_entry(lockowner, &host->h_lockowners, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (lockowner->pid == pid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) static inline uint32_t __nlm_alloc_pid(struct nlm_host *host)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) uint32_t res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) res = host->h_pidcount++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) } while (nlm_pidbusy(host, res) < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) static struct nlm_lockowner *__nlmclnt_find_lockowner(struct nlm_host *host, fl_owner_t owner)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) struct nlm_lockowner *lockowner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) list_for_each_entry(lockowner, &host->h_lockowners, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (lockowner->owner != owner)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) return nlmclnt_get_lockowner(lockowner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) static struct nlm_lockowner *nlmclnt_find_lockowner(struct nlm_host *host, fl_owner_t owner)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) struct nlm_lockowner *res, *new = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) spin_lock(&host->h_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) res = __nlmclnt_find_lockowner(host, owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) if (res == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) spin_unlock(&host->h_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) new = kmalloc(sizeof(*new), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) spin_lock(&host->h_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) res = __nlmclnt_find_lockowner(host, owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (res == NULL && new != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) res = new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) refcount_set(&new->count, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) new->owner = owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) new->pid = __nlm_alloc_pid(host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) new->host = nlm_get_host(host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) list_add(&new->list, &host->h_lockowners);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) new = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) spin_unlock(&host->h_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) kfree(new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) * Initialize arguments for TEST/LOCK/UNLOCK/CANCEL calls
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) static void nlmclnt_setlockargs(struct nlm_rqst *req, struct file_lock *fl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) struct nlm_args *argp = &req->a_args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) struct nlm_lock *lock = &argp->lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) char *nodename = req->a_host->h_rpcclnt->cl_nodename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) nlmclnt_next_cookie(&argp->cookie);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) memcpy(&lock->fh, NFS_FH(locks_inode(fl->fl_file)), sizeof(struct nfs_fh));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) lock->caller = nodename;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) lock->oh.data = req->a_owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) lock->oh.len = snprintf(req->a_owner, sizeof(req->a_owner), "%u@%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) (unsigned int)fl->fl_u.nfs_fl.owner->pid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) nodename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) lock->svid = fl->fl_u.nfs_fl.owner->pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) lock->fl.fl_start = fl->fl_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) lock->fl.fl_end = fl->fl_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) lock->fl.fl_type = fl->fl_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static void nlmclnt_release_lockargs(struct nlm_rqst *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) WARN_ON_ONCE(req->a_args.lock.fl.fl_ops != NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * nlmclnt_proc - Perform a single client-side lock request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) * @host: address of a valid nlm_host context representing the NLM server
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) * @cmd: fcntl-style file lock operation to perform
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) * @fl: address of arguments for the lock operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) * @data: address of data to be sent to callback operations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) int nlmclnt_proc(struct nlm_host *host, int cmd, struct file_lock *fl, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) struct nlm_rqst *call;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) const struct nlmclnt_operations *nlmclnt_ops = host->h_nlmclnt_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) call = nlm_alloc_call(host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) if (call == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) if (nlmclnt_ops && nlmclnt_ops->nlmclnt_alloc_call)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) nlmclnt_ops->nlmclnt_alloc_call(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) nlmclnt_locks_init_private(fl, host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) if (!fl->fl_u.nfs_fl.owner) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) /* lockowner allocation has failed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) nlmclnt_release_call(call);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) /* Set up the argument struct */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) nlmclnt_setlockargs(call, fl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) call->a_callback_data = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if (IS_SETLK(cmd) || IS_SETLKW(cmd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if (fl->fl_type != F_UNLCK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) call->a_args.block = IS_SETLKW(cmd) ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) status = nlmclnt_lock(call, fl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) status = nlmclnt_unlock(call, fl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) } else if (IS_GETLK(cmd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) status = nlmclnt_test(call, fl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) status = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) fl->fl_ops->fl_release_private(fl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) fl->fl_ops = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) dprintk("lockd: clnt proc returns %d\n", status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) EXPORT_SYMBOL_GPL(nlmclnt_proc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) * Allocate an NLM RPC call struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) struct nlm_rqst *nlm_alloc_call(struct nlm_host *host)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) struct nlm_rqst *call;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) for(;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) call = kzalloc(sizeof(*call), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) if (call != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) refcount_set(&call->a_count, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) locks_init_lock(&call->a_args.lock.fl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) locks_init_lock(&call->a_res.lock.fl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) call->a_host = nlm_get_host(host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) return call;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) if (signalled())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) printk("nlm_alloc_call: failed, waiting for memory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) schedule_timeout_interruptible(5*HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) void nlmclnt_release_call(struct nlm_rqst *call)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) const struct nlmclnt_operations *nlmclnt_ops = call->a_host->h_nlmclnt_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) if (!refcount_dec_and_test(&call->a_count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) if (nlmclnt_ops && nlmclnt_ops->nlmclnt_release_call)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) nlmclnt_ops->nlmclnt_release_call(call->a_callback_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) nlmclnt_release_host(call->a_host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) nlmclnt_release_lockargs(call);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) kfree(call);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) static void nlmclnt_rpc_release(void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) nlmclnt_release_call(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) static int nlm_wait_on_grace(wait_queue_head_t *queue)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) DEFINE_WAIT(wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) int status = -EINTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) prepare_to_wait(queue, &wait, TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) if (!signalled ()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) schedule_timeout(NLMCLNT_GRACE_WAIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) try_to_freeze();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) if (!signalled ())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) finish_wait(queue, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) * Generic NLM call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) nlmclnt_call(const struct cred *cred, struct nlm_rqst *req, u32 proc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) struct nlm_host *host = req->a_host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) struct rpc_clnt *clnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) struct nlm_args *argp = &req->a_args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) struct nlm_res *resp = &req->a_res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) struct rpc_message msg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) .rpc_argp = argp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) .rpc_resp = resp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) .rpc_cred = cred,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) dprintk("lockd: call procedure %d on %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) (int)proc, host->h_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) if (host->h_reclaiming && !argp->reclaim)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) goto in_grace_period;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) /* If we have no RPC client yet, create one. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) if ((clnt = nlm_bind_host(host)) == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) return -ENOLCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) msg.rpc_proc = &clnt->cl_procinfo[proc];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) /* Perform the RPC call. If an error occurs, try again */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) if ((status = rpc_call_sync(clnt, &msg, 0)) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) dprintk("lockd: rpc_call returned error %d\n", -status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) switch (status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) case -EPROTONOSUPPORT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) status = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) case -ECONNREFUSED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) case -ETIMEDOUT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) case -ENOTCONN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) nlm_rebind_host(host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) status = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) case -ERESTARTSYS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) return signalled () ? -EINTR : status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) if (resp->status == nlm_lck_denied_grace_period) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) dprintk("lockd: server in grace period\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) if (argp->reclaim) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) printk(KERN_WARNING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) "lockd: spurious grace period reject?!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) return -ENOLCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) if (!argp->reclaim) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) /* We appear to be out of the grace period */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) wake_up_all(&host->h_gracewait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) dprintk("lockd: server returns status %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) ntohl(resp->status));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) return 0; /* Okay, call complete */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) in_grace_period:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) * The server has rebooted and appears to be in the grace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) * period during which locks are only allowed to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) * reclaimed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) * We can only back off and try again later.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) status = nlm_wait_on_grace(&host->h_gracewait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) } while (status == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) * Generic NLM call, async version.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) static struct rpc_task *__nlm_async_call(struct nlm_rqst *req, u32 proc, struct rpc_message *msg, const struct rpc_call_ops *tk_ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) struct nlm_host *host = req->a_host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) struct rpc_clnt *clnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) struct rpc_task_setup task_setup_data = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) .rpc_message = msg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) .callback_ops = tk_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) .callback_data = req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) .flags = RPC_TASK_ASYNC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) dprintk("lockd: call procedure %d on %s (async)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) (int)proc, host->h_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) /* If we have no RPC client yet, create one. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) clnt = nlm_bind_host(host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) if (clnt == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) msg->rpc_proc = &clnt->cl_procinfo[proc];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) task_setup_data.rpc_client = clnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) /* bootstrap and kick off the async RPC call */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) return rpc_run_task(&task_setup_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) tk_ops->rpc_release(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) return ERR_PTR(-ENOLCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) static int nlm_do_async_call(struct nlm_rqst *req, u32 proc, struct rpc_message *msg, const struct rpc_call_ops *tk_ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) struct rpc_task *task;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) task = __nlm_async_call(req, proc, msg, tk_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) if (IS_ERR(task))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) return PTR_ERR(task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) rpc_put_task(task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) * NLM asynchronous call.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) int nlm_async_call(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) struct rpc_message msg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) .rpc_argp = &req->a_args,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) .rpc_resp = &req->a_res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) return nlm_do_async_call(req, proc, &msg, tk_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) int nlm_async_reply(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) struct rpc_message msg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) .rpc_argp = &req->a_res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) return nlm_do_async_call(req, proc, &msg, tk_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) * NLM client asynchronous call.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) * Note that although the calls are asynchronous, and are therefore
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) * guaranteed to complete, we still always attempt to wait for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) * completion in order to be able to correctly track the lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) * state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) static int nlmclnt_async_call(const struct cred *cred, struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) struct rpc_message msg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) .rpc_argp = &req->a_args,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) .rpc_resp = &req->a_res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) .rpc_cred = cred,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) struct rpc_task *task;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) task = __nlm_async_call(req, proc, &msg, tk_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) if (IS_ERR(task))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) return PTR_ERR(task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) err = rpc_wait_for_completion_task(task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) rpc_put_task(task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) * TEST for the presence of a conflicting lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) status = nlmclnt_call(nfs_file_cred(fl->fl_file), req, NLMPROC_TEST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) switch (req->a_res.status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) case nlm_granted:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) fl->fl_type = F_UNLCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) case nlm_lck_denied:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) * Report the conflicting lock back to the application.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) fl->fl_start = req->a_res.lock.fl.fl_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) fl->fl_end = req->a_res.lock.fl.fl_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) fl->fl_type = req->a_res.lock.fl.fl_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) fl->fl_pid = -req->a_res.lock.fl.fl_pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) status = nlm_stat_to_errno(req->a_res.status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) nlmclnt_release_call(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) static void nlmclnt_locks_copy_lock(struct file_lock *new, struct file_lock *fl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) spin_lock(&fl->fl_u.nfs_fl.owner->host->h_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) new->fl_u.nfs_fl.state = fl->fl_u.nfs_fl.state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) new->fl_u.nfs_fl.owner = nlmclnt_get_lockowner(fl->fl_u.nfs_fl.owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) list_add_tail(&new->fl_u.nfs_fl.list, &fl->fl_u.nfs_fl.owner->host->h_granted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) spin_unlock(&fl->fl_u.nfs_fl.owner->host->h_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) static void nlmclnt_locks_release_private(struct file_lock *fl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) spin_lock(&fl->fl_u.nfs_fl.owner->host->h_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) list_del(&fl->fl_u.nfs_fl.list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) spin_unlock(&fl->fl_u.nfs_fl.owner->host->h_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) nlmclnt_put_lockowner(fl->fl_u.nfs_fl.owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) static const struct file_lock_operations nlmclnt_lock_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) .fl_copy_lock = nlmclnt_locks_copy_lock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) .fl_release_private = nlmclnt_locks_release_private,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) fl->fl_u.nfs_fl.state = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) fl->fl_u.nfs_fl.owner = nlmclnt_find_lockowner(host, fl->fl_owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) INIT_LIST_HEAD(&fl->fl_u.nfs_fl.list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) fl->fl_ops = &nlmclnt_lock_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) static int do_vfs_lock(struct file_lock *fl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) return locks_lock_file_wait(fl->fl_file, fl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) * LOCK: Try to create a lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) * Programmer Harassment Alert
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) * When given a blocking lock request in a sync RPC call, the HPUX lockd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) * will faithfully return LCK_BLOCKED but never cares to notify us when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) * the lock could be granted. This way, our local process could hang
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) * around forever waiting for the callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) * Solution A: Implement busy-waiting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) * Solution B: Use the async version of the call (NLM_LOCK_{MSG,RES})
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) * For now I am implementing solution A, because I hate the idea of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) * re-implementing lockd for a third time in two months. The async
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) * calls shouldn't be too hard to do, however.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) * This is one of the lovely things about standards in the NFS area:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) * they're so soft and squishy you can't really blame HP for doing this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) nlmclnt_lock(struct nlm_rqst *req, struct file_lock *fl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) const struct cred *cred = nfs_file_cred(fl->fl_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) struct nlm_host *host = req->a_host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) struct nlm_res *resp = &req->a_res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) struct nlm_wait *block = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) unsigned char fl_flags = fl->fl_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) unsigned char fl_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) int status = -ENOLCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) if (nsm_monitor(host) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) req->a_args.state = nsm_local_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) fl->fl_flags |= FL_ACCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) status = do_vfs_lock(fl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) fl->fl_flags = fl_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) block = nlmclnt_prepare_block(host, fl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) * Initialise resp->status to a valid non-zero value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) * since 0 == nlm_lck_granted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) resp->status = nlm_lck_blocked;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) for(;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) /* Reboot protection */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) fl->fl_u.nfs_fl.state = host->h_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) status = nlmclnt_call(cred, req, NLMPROC_LOCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) /* Did a reclaimer thread notify us of a server reboot? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) if (resp->status == nlm_lck_denied_grace_period)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) if (resp->status != nlm_lck_blocked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) /* Wait on an NLM blocking lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) status = nlmclnt_block(block, req, NLMCLNT_POLL_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) if (resp->status != nlm_lck_blocked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) /* if we were interrupted while blocking, then cancel the lock request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) * and exit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) if (resp->status == nlm_lck_blocked) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) if (!req->a_args.block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) if (nlmclnt_cancel(host, req->a_args.block, fl) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) goto out_unblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) if (resp->status == nlm_granted) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) down_read(&host->h_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) /* Check whether or not the server has rebooted */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) if (fl->fl_u.nfs_fl.state != host->h_state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) up_read(&host->h_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) /* Ensure the resulting lock will get added to granted list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) fl->fl_flags |= FL_SLEEP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) if (do_vfs_lock(fl) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) up_read(&host->h_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) fl->fl_flags = fl_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) * EAGAIN doesn't make sense for sleeping locks, and in some
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) * cases NLM_LCK_DENIED is returned for a permanent error. So
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) * turn it into an ENOLCK.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) if (resp->status == nlm_lck_denied && (fl_flags & FL_SLEEP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) status = -ENOLCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) status = nlm_stat_to_errno(resp->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) out_unblock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) nlmclnt_finish_block(block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) nlmclnt_release_call(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) /* Fatal error: ensure that we remove the lock altogether */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) dprintk("lockd: lock attempt ended in fatal error.\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) " Attempting to unlock.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) nlmclnt_finish_block(block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) fl_type = fl->fl_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) fl->fl_type = F_UNLCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) down_read(&host->h_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) do_vfs_lock(fl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) up_read(&host->h_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) fl->fl_type = fl_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) fl->fl_flags = fl_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) nlmclnt_async_call(cred, req, NLMPROC_UNLOCK, &nlmclnt_unlock_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) * RECLAIM: Try to reclaim a lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) nlmclnt_reclaim(struct nlm_host *host, struct file_lock *fl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) struct nlm_rqst *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) memset(req, 0, sizeof(*req));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) locks_init_lock(&req->a_args.lock.fl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) locks_init_lock(&req->a_res.lock.fl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) req->a_host = host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) /* Set up the argument struct */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) nlmclnt_setlockargs(req, fl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) req->a_args.reclaim = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) status = nlmclnt_call(nfs_file_cred(fl->fl_file), req, NLMPROC_LOCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) if (status >= 0 && req->a_res.status == nlm_granted)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) printk(KERN_WARNING "lockd: failed to reclaim lock for pid %d "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) "(errno %d, status %d)\n", fl->fl_pid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) status, ntohl(req->a_res.status));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) * FIXME: This is a serious failure. We can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) * a. Ignore the problem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) * b. Send the owning process some signal (Linux doesn't have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) * SIGLOST, though...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) * c. Retry the operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) * Until someone comes up with a simple implementation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) * for b or c, I'll choose option a.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) return -ENOLCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) * UNLOCK: remove an existing lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) nlmclnt_unlock(struct nlm_rqst *req, struct file_lock *fl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) struct nlm_host *host = req->a_host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) struct nlm_res *resp = &req->a_res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) unsigned char fl_flags = fl->fl_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) * Note: the server is supposed to either grant us the unlock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) * request, or to deny it with NLM_LCK_DENIED_GRACE_PERIOD. In either
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) * case, we want to unlock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) fl->fl_flags |= FL_EXISTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) down_read(&host->h_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) status = do_vfs_lock(fl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) up_read(&host->h_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) fl->fl_flags = fl_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) if (status == -ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) refcount_inc(&req->a_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) status = nlmclnt_async_call(nfs_file_cred(fl->fl_file), req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) NLMPROC_UNLOCK, &nlmclnt_unlock_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) if (resp->status == nlm_granted)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) if (resp->status != nlm_lck_denied_nolocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) printk("lockd: unexpected unlock status: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) ntohl(resp->status));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) /* What to do now? I'm out of my depth... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) status = -ENOLCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) nlmclnt_release_call(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) static void nlmclnt_unlock_prepare(struct rpc_task *task, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) struct nlm_rqst *req = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) const struct nlmclnt_operations *nlmclnt_ops = req->a_host->h_nlmclnt_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) bool defer_call = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) if (nlmclnt_ops && nlmclnt_ops->nlmclnt_unlock_prepare)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) defer_call = nlmclnt_ops->nlmclnt_unlock_prepare(task, req->a_callback_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) if (!defer_call)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) rpc_call_start(task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) static void nlmclnt_unlock_callback(struct rpc_task *task, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) struct nlm_rqst *req = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) u32 status = ntohl(req->a_res.status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) if (RPC_SIGNALLED(task))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) goto die;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) if (task->tk_status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) dprintk("lockd: unlock failed (err = %d)\n", -task->tk_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) switch (task->tk_status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) case -EACCES:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) case -EIO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) goto die;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) goto retry_rebind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) if (status == NLM_LCK_DENIED_GRACE_PERIOD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) rpc_delay(task, NLMCLNT_GRACE_WAIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) goto retry_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) if (status != NLM_LCK_GRANTED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) printk(KERN_WARNING "lockd: unexpected unlock status: %d\n", status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) die:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) retry_rebind:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) nlm_rebind_host(req->a_host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) retry_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) rpc_restart_call(task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) static const struct rpc_call_ops nlmclnt_unlock_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) .rpc_call_prepare = nlmclnt_unlock_prepare,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) .rpc_call_done = nlmclnt_unlock_callback,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) .rpc_release = nlmclnt_rpc_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) * Cancel a blocked lock request.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) * We always use an async RPC call for this in order not to hang a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) * process that has been Ctrl-C'ed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) static int nlmclnt_cancel(struct nlm_host *host, int block, struct file_lock *fl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) struct nlm_rqst *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) dprintk("lockd: blocking lock attempt was interrupted by a signal.\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) " Attempting to cancel lock.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) req = nlm_alloc_call(host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) if (!req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) req->a_flags = RPC_TASK_ASYNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) nlmclnt_setlockargs(req, fl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) req->a_args.block = block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) refcount_inc(&req->a_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) status = nlmclnt_async_call(nfs_file_cred(fl->fl_file), req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) NLMPROC_CANCEL, &nlmclnt_cancel_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) if (status == 0 && req->a_res.status == nlm_lck_denied)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) status = -ENOLCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) nlmclnt_release_call(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) static void nlmclnt_cancel_callback(struct rpc_task *task, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) struct nlm_rqst *req = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) u32 status = ntohl(req->a_res.status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) if (RPC_SIGNALLED(task))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) goto die;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) if (task->tk_status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) dprintk("lockd: CANCEL call error %d, retrying.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) task->tk_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) goto retry_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) dprintk("lockd: cancel status %u (task %u)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) status, task->tk_pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) switch (status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) case NLM_LCK_GRANTED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) case NLM_LCK_DENIED_GRACE_PERIOD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) case NLM_LCK_DENIED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) /* Everything's good */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) case NLM_LCK_DENIED_NOLOCKS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) dprintk("lockd: CANCEL failed (server has no locks)\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) goto retry_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) printk(KERN_NOTICE "lockd: weird return %d for CANCEL call\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) die:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) retry_cancel:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) /* Don't ever retry more than 3 times */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) if (req->a_retries++ >= NLMCLNT_MAX_RETRIES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) goto die;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) nlm_rebind_host(req->a_host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) rpc_restart_call(task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) rpc_delay(task, 30 * HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) static const struct rpc_call_ops nlmclnt_cancel_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) .rpc_call_done = nlmclnt_cancel_callback,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) .rpc_release = nlmclnt_rpc_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) * Convert an NLM status code to a generic kernel errno
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) nlm_stat_to_errno(__be32 status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) switch(ntohl(status)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) case NLM_LCK_GRANTED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) case NLM_LCK_DENIED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) case NLM_LCK_DENIED_NOLOCKS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) case NLM_LCK_DENIED_GRACE_PERIOD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) return -ENOLCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) case NLM_LCK_BLOCKED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) printk(KERN_NOTICE "lockd: unexpected status NLM_BLOCKED\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) return -ENOLCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) #ifdef CONFIG_LOCKD_V4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) case NLM_DEADLCK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) return -EDEADLK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) case NLM_ROFS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) return -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) case NLM_STALE_FH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) return -ESTALE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) case NLM_FBIG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) return -EOVERFLOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) case NLM_FAILED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) return -ENOLCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) printk(KERN_NOTICE "lockd: unexpected server status %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) ntohl(status));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) return -ENOLCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) }