blob: 7e6f22e2a3b42bd0a2257f0f5e3e253e035153a6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/lockd/clntproc.c
3 *
4 * RPC procedures for the client side NLM implementation
5 *
6 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
7 */
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/module.h>
10#include <linux/types.h>
11#include <linux/errno.h>
12#include <linux/fs.h>
13#include <linux/nfs_fs.h>
14#include <linux/utsname.h>
15#include <linux/smp_lock.h>
16#include <linux/sunrpc/clnt.h>
17#include <linux/sunrpc/svc.h>
18#include <linux/lockd/lockd.h>
19#include <linux/lockd/sm_inter.h>
20
21#define NLMDBG_FACILITY NLMDBG_CLIENT
22#define NLMCLNT_GRACE_WAIT (5*HZ)
Trond Myklebustecdbf762005-06-22 17:16:31 +000023#define NLMCLNT_POLL_TIMEOUT (30*HZ)
Trond Myklebustaaaa9942006-02-01 12:18:25 -050024#define NLMCLNT_MAX_RETRIES 3
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26static int nlmclnt_test(struct nlm_rqst *, struct file_lock *);
27static int nlmclnt_lock(struct nlm_rqst *, struct file_lock *);
28static int nlmclnt_unlock(struct nlm_rqst *, struct file_lock *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029static int nlm_stat_to_errno(u32 stat);
30static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host);
Trond Myklebust16fb2422006-02-01 12:18:22 -050031static int nlmclnt_cancel(struct nlm_host *, int , struct file_lock *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Trond Myklebust963d8fe2006-01-03 09:55:04 +010033static const struct rpc_call_ops nlmclnt_unlock_ops;
34static const struct rpc_call_ops nlmclnt_cancel_ops;
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/*
37 * Cookie counter for NLM requests
38 */
39static u32 nlm_cookie = 0x1234;
40
41static inline void nlmclnt_next_cookie(struct nlm_cookie *c)
42{
43 memcpy(c->data, &nlm_cookie, 4);
44 memset(c->data+4, 0, 4);
45 c->len=4;
46 nlm_cookie++;
47}
48
49static struct nlm_lockowner *nlm_get_lockowner(struct nlm_lockowner *lockowner)
50{
51 atomic_inc(&lockowner->count);
52 return lockowner;
53}
54
55static void nlm_put_lockowner(struct nlm_lockowner *lockowner)
56{
57 if (!atomic_dec_and_lock(&lockowner->count, &lockowner->host->h_lock))
58 return;
59 list_del(&lockowner->list);
60 spin_unlock(&lockowner->host->h_lock);
61 nlm_release_host(lockowner->host);
62 kfree(lockowner);
63}
64
65static inline int nlm_pidbusy(struct nlm_host *host, uint32_t pid)
66{
67 struct nlm_lockowner *lockowner;
68 list_for_each_entry(lockowner, &host->h_lockowners, list) {
69 if (lockowner->pid == pid)
70 return -EBUSY;
71 }
72 return 0;
73}
74
75static inline uint32_t __nlm_alloc_pid(struct nlm_host *host)
76{
77 uint32_t res;
78 do {
79 res = host->h_pidcount++;
80 } while (nlm_pidbusy(host, res) < 0);
81 return res;
82}
83
84static struct nlm_lockowner *__nlm_find_lockowner(struct nlm_host *host, fl_owner_t owner)
85{
86 struct nlm_lockowner *lockowner;
87 list_for_each_entry(lockowner, &host->h_lockowners, list) {
88 if (lockowner->owner != owner)
89 continue;
90 return nlm_get_lockowner(lockowner);
91 }
92 return NULL;
93}
94
95static struct nlm_lockowner *nlm_find_lockowner(struct nlm_host *host, fl_owner_t owner)
96{
97 struct nlm_lockowner *res, *new = NULL;
98
99 spin_lock(&host->h_lock);
100 res = __nlm_find_lockowner(host, owner);
101 if (res == NULL) {
102 spin_unlock(&host->h_lock);
Panagiotis Issarisf52720c2006-09-27 01:49:39 -0700103 new = kmalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 spin_lock(&host->h_lock);
105 res = __nlm_find_lockowner(host, owner);
106 if (res == NULL && new != NULL) {
107 res = new;
108 atomic_set(&new->count, 1);
109 new->owner = owner;
110 new->pid = __nlm_alloc_pid(host);
111 new->host = nlm_get_host(host);
112 list_add(&new->list, &host->h_lockowners);
113 new = NULL;
114 }
115 }
116 spin_unlock(&host->h_lock);
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800117 kfree(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return res;
119}
120
121/*
122 * Initialize arguments for TEST/LOCK/UNLOCK/CANCEL calls
123 */
124static void nlmclnt_setlockargs(struct nlm_rqst *req, struct file_lock *fl)
125{
126 struct nlm_args *argp = &req->a_args;
127 struct nlm_lock *lock = &argp->lock;
128
129 nlmclnt_next_cookie(&argp->cookie);
130 argp->state = nsm_local_state;
131 memcpy(&lock->fh, NFS_FH(fl->fl_file->f_dentry->d_inode), sizeof(struct nfs_fh));
Serge E. Hallyne9ff3992006-10-02 02:18:11 -0700132 lock->caller = utsname()->nodename;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 lock->oh.data = req->a_owner;
Trond Myklebust7bab3772006-03-20 13:44:06 -0500134 lock->oh.len = snprintf(req->a_owner, sizeof(req->a_owner), "%u@%s",
135 (unsigned int)fl->fl_u.nfs_fl.owner->pid,
Serge E. Hallyne9ff3992006-10-02 02:18:11 -0700136 utsname()->nodename);
Trond Myklebust7bab3772006-03-20 13:44:06 -0500137 lock->svid = fl->fl_u.nfs_fl.owner->pid;
Trond Myklebust3a649b82006-03-20 13:44:44 -0500138 lock->fl.fl_start = fl->fl_start;
139 lock->fl.fl_end = fl->fl_end;
140 lock->fl.fl_type = fl->fl_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
143static void nlmclnt_release_lockargs(struct nlm_rqst *req)
144{
Trond Myklebust3a649b82006-03-20 13:44:44 -0500145 BUG_ON(req->a_args.lock.fl.fl_ops != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
148/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 * This is the main entry point for the NLM client.
150 */
151int
152nlmclnt_proc(struct inode *inode, int cmd, struct file_lock *fl)
153{
Chuck Lever44c31be2006-08-22 20:06:17 -0400154 struct rpc_clnt *client = NFS_CLIENT(inode);
155 struct sockaddr_in addr;
Olaf Kirchdb4e4c92006-10-04 02:15:52 -0700156 struct nfs_server *nfssrv = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 struct nlm_host *host;
Trond Myklebust92737232006-03-20 13:44:45 -0500158 struct nlm_rqst *call;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 sigset_t oldset;
160 unsigned long flags;
Chuck Lever44c31be2006-08-22 20:06:17 -0400161 int status, vers;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 vers = (NFS_PROTO(inode)->version == 3) ? 4 : 1;
164 if (NFS_PROTO(inode)->version > 3) {
165 printk(KERN_NOTICE "NFSv4 file locking not implemented!\n");
166 return -ENOLCK;
167 }
168
Chuck Lever44c31be2006-08-22 20:06:17 -0400169 rpc_peeraddr(client, (struct sockaddr *) &addr, sizeof(addr));
Olaf Kirchdb4e4c92006-10-04 02:15:52 -0700170 host = nlmclnt_lookup_host(&addr, client->cl_xprt->prot, vers,
171 nfssrv->nfs_client->cl_hostname,
172 strlen(nfssrv->nfs_client->cl_hostname));
Trond Myklebust92737232006-03-20 13:44:45 -0500173 if (host == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return -ENOLCK;
175
Trond Myklebust92737232006-03-20 13:44:45 -0500176 call = nlm_alloc_call(host);
177 if (call == NULL)
178 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Trond Myklebust92737232006-03-20 13:44:45 -0500180 nlmclnt_locks_init_private(fl, host);
181 /* Set up the argument struct */
182 nlmclnt_setlockargs(call, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 /* Keep the old signal mask */
185 spin_lock_irqsave(&current->sighand->siglock, flags);
186 oldset = current->blocked;
187
188 /* If we're cleaning up locks because the process is exiting,
189 * perform the RPC call asynchronously. */
190 if ((IS_SETLK(cmd) || IS_SETLKW(cmd))
191 && fl->fl_type == F_UNLCK
192 && (current->flags & PF_EXITING)) {
193 sigfillset(&current->blocked); /* Mask all signals */
194 recalc_sigpending();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 call->a_flags = RPC_TASK_ASYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 }
Trond Myklebust92737232006-03-20 13:44:45 -0500198 spin_unlock_irqrestore(&current->sighand->siglock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 if (IS_SETLK(cmd) || IS_SETLKW(cmd)) {
201 if (fl->fl_type != F_UNLCK) {
202 call->a_args.block = IS_SETLKW(cmd) ? 1 : 0;
203 status = nlmclnt_lock(call, fl);
204 } else
205 status = nlmclnt_unlock(call, fl);
206 } else if (IS_GETLK(cmd))
207 status = nlmclnt_test(call, fl);
208 else
209 status = -EINVAL;
210
Trond Myklebust92737232006-03-20 13:44:45 -0500211 fl->fl_ops->fl_release_private(fl);
212 fl->fl_ops = NULL;
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 spin_lock_irqsave(&current->sighand->siglock, flags);
215 current->blocked = oldset;
216 recalc_sigpending();
217 spin_unlock_irqrestore(&current->sighand->siglock, flags);
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 dprintk("lockd: clnt proc returns %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return status;
221}
222EXPORT_SYMBOL(nlmclnt_proc);
223
224/*
225 * Allocate an NLM RPC call struct
Trond Myklebust92737232006-03-20 13:44:45 -0500226 *
227 * Note: the caller must hold a reference to host. In case of failure,
228 * this reference will be released.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 */
Trond Myklebust92737232006-03-20 13:44:45 -0500230struct nlm_rqst *nlm_alloc_call(struct nlm_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
232 struct nlm_rqst *call;
233
Trond Myklebust36943fa2006-03-20 13:44:05 -0500234 for(;;) {
235 call = kzalloc(sizeof(*call), GFP_KERNEL);
236 if (call != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 locks_init_lock(&call->a_args.lock.fl);
238 locks_init_lock(&call->a_res.lock.fl);
Trond Myklebust92737232006-03-20 13:44:45 -0500239 call->a_host = host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return call;
241 }
Trond Myklebust36943fa2006-03-20 13:44:05 -0500242 if (signalled())
243 break;
Trond Myklebust92737232006-03-20 13:44:45 -0500244 printk("nlm_alloc_call: failed, waiting for memory\n");
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -0700245 schedule_timeout_interruptible(5*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 }
Trond Myklebust92737232006-03-20 13:44:45 -0500247 nlm_release_host(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return NULL;
249}
250
Trond Myklebust92737232006-03-20 13:44:45 -0500251void nlm_release_call(struct nlm_rqst *call)
252{
253 nlm_release_host(call->a_host);
254 nlmclnt_release_lockargs(call);
255 kfree(call);
256}
257
258static void nlmclnt_rpc_release(void *data)
259{
260 return nlm_release_call(data);
261}
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263static int nlm_wait_on_grace(wait_queue_head_t *queue)
264{
265 DEFINE_WAIT(wait);
266 int status = -EINTR;
267
268 prepare_to_wait(queue, &wait, TASK_INTERRUPTIBLE);
269 if (!signalled ()) {
270 schedule_timeout(NLMCLNT_GRACE_WAIT);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700271 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 if (!signalled ())
273 status = 0;
274 }
275 finish_wait(queue, &wait);
276 return status;
277}
278
279/*
280 * Generic NLM call
281 */
282static int
283nlmclnt_call(struct nlm_rqst *req, u32 proc)
284{
285 struct nlm_host *host = req->a_host;
286 struct rpc_clnt *clnt;
287 struct nlm_args *argp = &req->a_args;
288 struct nlm_res *resp = &req->a_res;
289 struct rpc_message msg = {
290 .rpc_argp = argp,
291 .rpc_resp = resp,
292 };
293 int status;
294
295 dprintk("lockd: call procedure %d on %s\n",
296 (int)proc, host->h_name);
297
298 do {
299 if (host->h_reclaiming && !argp->reclaim)
300 goto in_grace_period;
301
302 /* If we have no RPC client yet, create one. */
303 if ((clnt = nlm_bind_host(host)) == NULL)
304 return -ENOLCK;
305 msg.rpc_proc = &clnt->cl_procinfo[proc];
306
307 /* Perform the RPC call. If an error occurs, try again */
308 if ((status = rpc_call_sync(clnt, &msg, 0)) < 0) {
309 dprintk("lockd: rpc_call returned error %d\n", -status);
310 switch (status) {
311 case -EPROTONOSUPPORT:
312 status = -EINVAL;
313 break;
314 case -ECONNREFUSED:
315 case -ETIMEDOUT:
316 case -ENOTCONN:
317 nlm_rebind_host(host);
318 status = -EAGAIN;
319 break;
320 case -ERESTARTSYS:
321 return signalled () ? -EINTR : status;
322 default:
323 break;
324 }
325 break;
326 } else
327 if (resp->status == NLM_LCK_DENIED_GRACE_PERIOD) {
328 dprintk("lockd: server in grace period\n");
329 if (argp->reclaim) {
330 printk(KERN_WARNING
331 "lockd: spurious grace period reject?!\n");
332 return -ENOLCK;
333 }
334 } else {
335 if (!argp->reclaim) {
336 /* We appear to be out of the grace period */
337 wake_up_all(&host->h_gracewait);
338 }
339 dprintk("lockd: server returns status %d\n", resp->status);
340 return 0; /* Okay, call complete */
341 }
342
343in_grace_period:
344 /*
345 * The server has rebooted and appears to be in the grace
346 * period during which locks are only allowed to be
347 * reclaimed.
348 * We can only back off and try again later.
349 */
350 status = nlm_wait_on_grace(&host->h_gracewait);
351 } while (status == 0);
352
353 return status;
354}
355
356/*
357 * Generic NLM call, async version.
358 */
Trond Myklebustd4716622006-03-20 13:44:45 -0500359static int __nlm_async_call(struct nlm_rqst *req, u32 proc, struct rpc_message *msg, const struct rpc_call_ops *tk_ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
361 struct nlm_host *host = req->a_host;
362 struct rpc_clnt *clnt;
Trond Myklebust92737232006-03-20 13:44:45 -0500363 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 dprintk("lockd: call procedure %d on %s (async)\n",
366 (int)proc, host->h_name);
367
368 /* If we have no RPC client yet, create one. */
Trond Myklebust92737232006-03-20 13:44:45 -0500369 clnt = nlm_bind_host(host);
370 if (clnt == NULL)
371 goto out_err;
Trond Myklebustd4716622006-03-20 13:44:45 -0500372 msg->rpc_proc = &clnt->cl_procinfo[proc];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 /* bootstrap and kick off the async RPC call */
Trond Myklebustd4716622006-03-20 13:44:45 -0500375 status = rpc_call_async(clnt, msg, RPC_TASK_ASYNC, tk_ops, req);
Trond Myklebust92737232006-03-20 13:44:45 -0500376 if (status == 0)
377 return 0;
378out_err:
379 nlm_release_call(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return status;
381}
382
Trond Myklebustd4716622006-03-20 13:44:45 -0500383int nlm_async_call(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
384{
385 struct rpc_message msg = {
386 .rpc_argp = &req->a_args,
387 .rpc_resp = &req->a_res,
388 };
389 return __nlm_async_call(req, proc, &msg, tk_ops);
390}
391
392int nlm_async_reply(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
393{
394 struct rpc_message msg = {
395 .rpc_argp = &req->a_res,
396 };
397 return __nlm_async_call(req, proc, &msg, tk_ops);
398}
399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400/*
401 * TEST for the presence of a conflicting lock
402 */
403static int
404nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl)
405{
406 int status;
407
408 status = nlmclnt_call(req, NLMPROC_TEST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if (status < 0)
Trond Myklebust92737232006-03-20 13:44:45 -0500410 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Trond Myklebust92737232006-03-20 13:44:45 -0500412 switch (req->a_res.status) {
413 case NLM_LCK_GRANTED:
414 fl->fl_type = F_UNLCK;
415 break;
416 case NLM_LCK_DENIED:
417 /*
418 * Report the conflicting lock back to the application.
419 */
420 fl->fl_start = req->a_res.lock.fl.fl_start;
421 fl->fl_end = req->a_res.lock.fl.fl_start;
422 fl->fl_type = req->a_res.lock.fl.fl_type;
423 fl->fl_pid = 0;
424 break;
425 default:
426 status = nlm_stat_to_errno(req->a_res.status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
Trond Myklebust92737232006-03-20 13:44:45 -0500428out:
429 nlm_release_call(req);
430 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431}
432
433static void nlmclnt_locks_copy_lock(struct file_lock *new, struct file_lock *fl)
434{
Trond Myklebust4c060b52006-03-20 13:44:41 -0500435 new->fl_u.nfs_fl.state = fl->fl_u.nfs_fl.state;
436 new->fl_u.nfs_fl.owner = nlm_get_lockowner(fl->fl_u.nfs_fl.owner);
437 list_add_tail(&new->fl_u.nfs_fl.list, &fl->fl_u.nfs_fl.owner->host->h_granted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
440static void nlmclnt_locks_release_private(struct file_lock *fl)
441{
Trond Myklebust4c060b52006-03-20 13:44:41 -0500442 list_del(&fl->fl_u.nfs_fl.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 nlm_put_lockowner(fl->fl_u.nfs_fl.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
446static struct file_lock_operations nlmclnt_lock_ops = {
447 .fl_copy_lock = nlmclnt_locks_copy_lock,
448 .fl_release_private = nlmclnt_locks_release_private,
449};
450
451static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host)
452{
453 BUG_ON(fl->fl_ops != NULL);
454 fl->fl_u.nfs_fl.state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 fl->fl_u.nfs_fl.owner = nlm_find_lockowner(host, fl->fl_owner);
Trond Myklebust4c060b52006-03-20 13:44:41 -0500456 INIT_LIST_HEAD(&fl->fl_u.nfs_fl.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 fl->fl_ops = &nlmclnt_lock_ops;
458}
459
Trond Myklebust9b073572006-06-29 16:38:34 -0400460static int do_vfs_lock(struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
462 int res = 0;
463 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
464 case FL_POSIX:
465 res = posix_lock_file_wait(fl->fl_file, fl);
466 break;
467 case FL_FLOCK:
468 res = flock_lock_file_wait(fl->fl_file, fl);
469 break;
470 default:
471 BUG();
472 }
Trond Myklebust9b073572006-06-29 16:38:34 -0400473 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
476/*
477 * LOCK: Try to create a lock
478 *
479 * Programmer Harassment Alert
480 *
481 * When given a blocking lock request in a sync RPC call, the HPUX lockd
482 * will faithfully return LCK_BLOCKED but never cares to notify us when
483 * the lock could be granted. This way, our local process could hang
484 * around forever waiting for the callback.
485 *
486 * Solution A: Implement busy-waiting
487 * Solution B: Use the async version of the call (NLM_LOCK_{MSG,RES})
488 *
489 * For now I am implementing solution A, because I hate the idea of
490 * re-implementing lockd for a third time in two months. The async
491 * calls shouldn't be too hard to do, however.
492 *
493 * This is one of the lovely things about standards in the NFS area:
494 * they're so soft and squishy you can't really blame HP for doing this.
495 */
496static int
497nlmclnt_lock(struct nlm_rqst *req, struct file_lock *fl)
498{
499 struct nlm_host *host = req->a_host;
500 struct nlm_res *resp = &req->a_res;
Trond Myklebust3a649b82006-03-20 13:44:44 -0500501 struct nlm_wait *block = NULL;
Trond Myklebust01c3b862006-06-29 16:38:39 -0400502 unsigned char fl_flags = fl->fl_flags;
Trond Myklebust3a649b82006-03-20 13:44:44 -0500503 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Olaf Kirch977faf32006-10-04 02:15:51 -0700505 if (nsm_monitor(host) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 printk(KERN_NOTICE "lockd: failed to monitor %s\n",
507 host->h_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 goto out;
509 }
Trond Myklebust01c3b862006-06-29 16:38:39 -0400510 fl->fl_flags |= FL_ACCESS;
511 status = do_vfs_lock(fl);
512 if (status < 0)
513 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Trond Myklebust3a649b82006-03-20 13:44:44 -0500515 block = nlmclnt_prepare_block(host, fl);
Trond Myklebust28df9552006-06-09 09:40:27 -0400516again:
Trond Myklebustecdbf762005-06-22 17:16:31 +0000517 for(;;) {
Trond Myklebust28df9552006-06-09 09:40:27 -0400518 /* Reboot protection */
519 fl->fl_u.nfs_fl.state = host->h_state;
Trond Myklebustecdbf762005-06-22 17:16:31 +0000520 status = nlmclnt_call(req, NLMPROC_LOCK);
521 if (status < 0)
522 goto out_unblock;
Trond Myklebust3a649b82006-03-20 13:44:44 -0500523 if (!req->a_args.block)
Trond Myklebustecdbf762005-06-22 17:16:31 +0000524 break;
Trond Myklebustecdbf762005-06-22 17:16:31 +0000525 /* Did a reclaimer thread notify us of a server reboot? */
526 if (resp->status == NLM_LCK_DENIED_GRACE_PERIOD)
527 continue;
528 if (resp->status != NLM_LCK_BLOCKED)
529 break;
Trond Myklebust3a649b82006-03-20 13:44:44 -0500530 /* Wait on an NLM blocking lock */
531 status = nlmclnt_block(block, req, NLMCLNT_POLL_TIMEOUT);
532 /* if we were interrupted. Send a CANCEL request to the server
Trond Myklebustecdbf762005-06-22 17:16:31 +0000533 * and exit
534 */
Trond Myklebust3a649b82006-03-20 13:44:44 -0500535 if (status < 0)
536 goto out_unblock;
537 if (resp->status != NLM_LCK_BLOCKED)
538 break;
Trond Myklebustecdbf762005-06-22 17:16:31 +0000539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 if (resp->status == NLM_LCK_GRANTED) {
Trond Myklebust28df9552006-06-09 09:40:27 -0400542 down_read(&host->h_rwsem);
543 /* Check whether or not the server has rebooted */
544 if (fl->fl_u.nfs_fl.state != host->h_state) {
545 up_read(&host->h_rwsem);
546 goto again;
547 }
Trond Myklebust4c060b52006-03-20 13:44:41 -0500548 /* Ensure the resulting lock will get added to granted list */
Trond Myklebust01c3b862006-06-29 16:38:39 -0400549 fl->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust9b073572006-06-29 16:38:34 -0400550 if (do_vfs_lock(fl) < 0)
551 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
Trond Myklebust28df9552006-06-09 09:40:27 -0400552 up_read(&host->h_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
554 status = nlm_stat_to_errno(resp->status);
Trond Myklebustecdbf762005-06-22 17:16:31 +0000555out_unblock:
Trond Myklebust3a649b82006-03-20 13:44:44 -0500556 nlmclnt_finish_block(block);
Trond Myklebustecdbf762005-06-22 17:16:31 +0000557 /* Cancel the blocked request if it is still pending */
558 if (resp->status == NLM_LCK_BLOCKED)
Trond Myklebust16fb2422006-02-01 12:18:22 -0500559 nlmclnt_cancel(host, req->a_args.block, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560out:
Trond Myklebust92737232006-03-20 13:44:45 -0500561 nlm_release_call(req);
Trond Myklebust01c3b862006-06-29 16:38:39 -0400562 fl->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return status;
564}
565
566/*
567 * RECLAIM: Try to reclaim a lock
568 */
569int
570nlmclnt_reclaim(struct nlm_host *host, struct file_lock *fl)
571{
572 struct nlm_rqst reqst, *req;
573 int status;
574
575 req = &reqst;
576 memset(req, 0, sizeof(*req));
577 locks_init_lock(&req->a_args.lock.fl);
578 locks_init_lock(&req->a_res.lock.fl);
579 req->a_host = host;
580 req->a_flags = 0;
581
582 /* Set up the argument struct */
583 nlmclnt_setlockargs(req, fl);
584 req->a_args.reclaim = 1;
585
586 if ((status = nlmclnt_call(req, NLMPROC_LOCK)) >= 0
587 && req->a_res.status == NLM_LCK_GRANTED)
588 return 0;
589
590 printk(KERN_WARNING "lockd: failed to reclaim lock for pid %d "
591 "(errno %d, status %d)\n", fl->fl_pid,
592 status, req->a_res.status);
593
594 /*
595 * FIXME: This is a serious failure. We can
596 *
597 * a. Ignore the problem
598 * b. Send the owning process some signal (Linux doesn't have
599 * SIGLOST, though...)
600 * c. Retry the operation
601 *
602 * Until someone comes up with a simple implementation
603 * for b or c, I'll choose option a.
604 */
605
606 return -ENOLCK;
607}
608
609/*
610 * UNLOCK: remove an existing lock
611 */
612static int
613nlmclnt_unlock(struct nlm_rqst *req, struct file_lock *fl)
614{
Trond Myklebust28df9552006-06-09 09:40:27 -0400615 struct nlm_host *host = req->a_host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 struct nlm_res *resp = &req->a_res;
Trond Myklebust9b073572006-06-29 16:38:34 -0400617 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Christoph Hellwig26bcbf92006-03-20 13:44:40 -0500619 /*
Trond Myklebust30f4e202006-03-13 21:20:49 -0800620 * Note: the server is supposed to either grant us the unlock
621 * request, or to deny it with NLM_LCK_DENIED_GRACE_PERIOD. In either
622 * case, we want to unlock.
623 */
Trond Myklebust9b073572006-06-29 16:38:34 -0400624 fl->fl_flags |= FL_EXISTS;
Trond Myklebust28df9552006-06-09 09:40:27 -0400625 down_read(&host->h_rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -0400626 if (do_vfs_lock(fl) == -ENOENT) {
627 up_read(&host->h_rwsem);
628 goto out;
629 }
Trond Myklebust28df9552006-06-09 09:40:27 -0400630 up_read(&host->h_rwsem);
Trond Myklebust30f4e202006-03-13 21:20:49 -0800631
Trond Myklebust92737232006-03-20 13:44:45 -0500632 if (req->a_flags & RPC_TASK_ASYNC)
633 return nlm_async_call(req, NLMPROC_UNLOCK, &nlmclnt_unlock_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 status = nlmclnt_call(req, NLMPROC_UNLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 if (status < 0)
Trond Myklebust92737232006-03-20 13:44:45 -0500637 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 if (resp->status == NLM_LCK_GRANTED)
Trond Myklebust92737232006-03-20 13:44:45 -0500640 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 if (resp->status != NLM_LCK_DENIED_NOLOCKS)
643 printk("lockd: unexpected unlock status: %d\n", resp->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 /* What to do now? I'm out of my depth... */
Trond Myklebust92737232006-03-20 13:44:45 -0500645 status = -ENOLCK;
646out:
647 nlm_release_call(req);
648 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
650
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100651static void nlmclnt_unlock_callback(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100653 struct nlm_rqst *req = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 int status = req->a_res.status;
655
656 if (RPC_ASSASSINATED(task))
657 goto die;
658
659 if (task->tk_status < 0) {
660 dprintk("lockd: unlock failed (err = %d)\n", -task->tk_status);
661 goto retry_rebind;
662 }
663 if (status == NLM_LCK_DENIED_GRACE_PERIOD) {
664 rpc_delay(task, NLMCLNT_GRACE_WAIT);
665 goto retry_unlock;
666 }
667 if (status != NLM_LCK_GRANTED)
668 printk(KERN_WARNING "lockd: unexpected unlock status: %d\n", status);
669die:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return;
671 retry_rebind:
672 nlm_rebind_host(req->a_host);
673 retry_unlock:
674 rpc_restart_call(task);
675}
676
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100677static const struct rpc_call_ops nlmclnt_unlock_ops = {
678 .rpc_call_done = nlmclnt_unlock_callback,
Trond Myklebust92737232006-03-20 13:44:45 -0500679 .rpc_release = nlmclnt_rpc_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100680};
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682/*
683 * Cancel a blocked lock request.
684 * We always use an async RPC call for this in order not to hang a
685 * process that has been Ctrl-C'ed.
686 */
Trond Myklebust16fb2422006-02-01 12:18:22 -0500687static int nlmclnt_cancel(struct nlm_host *host, int block, struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
689 struct nlm_rqst *req;
690 unsigned long flags;
691 sigset_t oldset;
692 int status;
693
694 /* Block all signals while setting up call */
695 spin_lock_irqsave(&current->sighand->siglock, flags);
696 oldset = current->blocked;
697 sigfillset(&current->blocked);
698 recalc_sigpending();
699 spin_unlock_irqrestore(&current->sighand->siglock, flags);
700
Trond Myklebust92737232006-03-20 13:44:45 -0500701 req = nlm_alloc_call(nlm_get_host(host));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if (!req)
703 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 req->a_flags = RPC_TASK_ASYNC;
705
706 nlmclnt_setlockargs(req, fl);
Trond Myklebust16fb2422006-02-01 12:18:22 -0500707 req->a_args.block = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Trond Myklebust92737232006-03-20 13:44:45 -0500709 status = nlm_async_call(req, NLMPROC_CANCEL, &nlmclnt_cancel_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711 spin_lock_irqsave(&current->sighand->siglock, flags);
712 current->blocked = oldset;
713 recalc_sigpending();
714 spin_unlock_irqrestore(&current->sighand->siglock, flags);
715
716 return status;
717}
718
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100719static void nlmclnt_cancel_callback(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100721 struct nlm_rqst *req = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 if (RPC_ASSASSINATED(task))
724 goto die;
725
726 if (task->tk_status < 0) {
727 dprintk("lockd: CANCEL call error %d, retrying.\n",
728 task->tk_status);
729 goto retry_cancel;
730 }
731
732 dprintk("lockd: cancel status %d (task %d)\n",
733 req->a_res.status, task->tk_pid);
734
735 switch (req->a_res.status) {
736 case NLM_LCK_GRANTED:
737 case NLM_LCK_DENIED_GRACE_PERIOD:
Trond Myklebust35576cb2006-03-20 13:44:41 -0500738 case NLM_LCK_DENIED:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 /* Everything's good */
740 break;
741 case NLM_LCK_DENIED_NOLOCKS:
742 dprintk("lockd: CANCEL failed (server has no locks)\n");
743 goto retry_cancel;
744 default:
745 printk(KERN_NOTICE "lockd: weird return %d for CANCEL call\n",
746 req->a_res.status);
747 }
748
749die:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 return;
751
752retry_cancel:
Trond Myklebustaaaa9942006-02-01 12:18:25 -0500753 /* Don't ever retry more than 3 times */
754 if (req->a_retries++ >= NLMCLNT_MAX_RETRIES)
755 goto die;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 nlm_rebind_host(req->a_host);
757 rpc_restart_call(task);
758 rpc_delay(task, 30 * HZ);
759}
760
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100761static const struct rpc_call_ops nlmclnt_cancel_ops = {
762 .rpc_call_done = nlmclnt_cancel_callback,
Trond Myklebust92737232006-03-20 13:44:45 -0500763 .rpc_release = nlmclnt_rpc_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100764};
765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766/*
767 * Convert an NLM status code to a generic kernel errno
768 */
769static int
770nlm_stat_to_errno(u32 status)
771{
772 switch(status) {
773 case NLM_LCK_GRANTED:
774 return 0;
775 case NLM_LCK_DENIED:
776 return -EAGAIN;
777 case NLM_LCK_DENIED_NOLOCKS:
778 case NLM_LCK_DENIED_GRACE_PERIOD:
779 return -ENOLCK;
780 case NLM_LCK_BLOCKED:
781 printk(KERN_NOTICE "lockd: unexpected status NLM_BLOCKED\n");
782 return -ENOLCK;
783#ifdef CONFIG_LOCKD_V4
784 case NLM_DEADLCK:
785 return -EDEADLK;
786 case NLM_ROFS:
787 return -EROFS;
788 case NLM_STALE_FH:
789 return -ESTALE;
790 case NLM_FBIG:
791 return -EOVERFLOW;
792 case NLM_FAILED:
793 return -ENOLCK;
794#endif
795 }
796 printk(KERN_NOTICE "lockd: unexpected server status %d\n", status);
797 return -ENOLCK;
798}