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