Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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 | ||
1da177e4 LT |
9 | #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) | |
ecdbf769 | 23 | #define NLMCLNT_POLL_TIMEOUT (30*HZ) |
aaaa9942 | 24 | #define NLMCLNT_MAX_RETRIES 3 |
1da177e4 LT |
25 | |
26 | static int nlmclnt_test(struct nlm_rqst *, struct file_lock *); | |
27 | static int nlmclnt_lock(struct nlm_rqst *, struct file_lock *); | |
28 | static int nlmclnt_unlock(struct nlm_rqst *, struct file_lock *); | |
1da177e4 LT |
29 | static int nlm_stat_to_errno(u32 stat); |
30 | static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host); | |
16fb2425 | 31 | static int nlmclnt_cancel(struct nlm_host *, int , struct file_lock *); |
1da177e4 | 32 | |
963d8fe5 TM |
33 | static const struct rpc_call_ops nlmclnt_unlock_ops; |
34 | static const struct rpc_call_ops nlmclnt_cancel_ops; | |
35 | ||
1da177e4 LT |
36 | /* |
37 | * Cookie counter for NLM requests | |
38 | */ | |
39 | static u32 nlm_cookie = 0x1234; | |
40 | ||
41 | static 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 | ||
49 | static struct nlm_lockowner *nlm_get_lockowner(struct nlm_lockowner *lockowner) | |
50 | { | |
51 | atomic_inc(&lockowner->count); | |
52 | return lockowner; | |
53 | } | |
54 | ||
55 | static 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 | ||
65 | static 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 | ||
75 | static 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 | ||
84 | static 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 | ||
95 | static 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); | |
f52720ca | 103 | new = kmalloc(sizeof(*new), GFP_KERNEL); |
1da177e4 LT |
104 | 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); | |
f99d49ad | 117 | kfree(new); |
1da177e4 LT |
118 | return res; |
119 | } | |
120 | ||
121 | /* | |
122 | * Initialize arguments for TEST/LOCK/UNLOCK/CANCEL calls | |
123 | */ | |
124 | static 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)); | |
e9ff3990 | 132 | lock->caller = utsname()->nodename; |
1da177e4 | 133 | lock->oh.data = req->a_owner; |
7bab377f TM |
134 | lock->oh.len = snprintf(req->a_owner, sizeof(req->a_owner), "%u@%s", |
135 | (unsigned int)fl->fl_u.nfs_fl.owner->pid, | |
e9ff3990 | 136 | utsname()->nodename); |
7bab377f | 137 | lock->svid = fl->fl_u.nfs_fl.owner->pid; |
3a649b88 TM |
138 | lock->fl.fl_start = fl->fl_start; |
139 | lock->fl.fl_end = fl->fl_end; | |
140 | lock->fl.fl_type = fl->fl_type; | |
1da177e4 LT |
141 | } |
142 | ||
143 | static void nlmclnt_release_lockargs(struct nlm_rqst *req) | |
144 | { | |
3a649b88 | 145 | BUG_ON(req->a_args.lock.fl.fl_ops != NULL); |
1da177e4 LT |
146 | } |
147 | ||
1da177e4 LT |
148 | /* |
149 | * This is the main entry point for the NLM client. | |
150 | */ | |
151 | int | |
152 | nlmclnt_proc(struct inode *inode, int cmd, struct file_lock *fl) | |
153 | { | |
44c31be2 CL |
154 | struct rpc_clnt *client = NFS_CLIENT(inode); |
155 | struct sockaddr_in addr; | |
1da177e4 | 156 | struct nlm_host *host; |
92737230 | 157 | struct nlm_rqst *call; |
1da177e4 LT |
158 | sigset_t oldset; |
159 | unsigned long flags; | |
44c31be2 | 160 | int status, vers; |
1da177e4 LT |
161 | |
162 | vers = (NFS_PROTO(inode)->version == 3) ? 4 : 1; | |
163 | if (NFS_PROTO(inode)->version > 3) { | |
164 | printk(KERN_NOTICE "NFSv4 file locking not implemented!\n"); | |
165 | return -ENOLCK; | |
166 | } | |
167 | ||
44c31be2 CL |
168 | rpc_peeraddr(client, (struct sockaddr *) &addr, sizeof(addr)); |
169 | host = nlmclnt_lookup_host(&addr, client->cl_xprt->prot, vers); | |
92737230 | 170 | if (host == NULL) |
1da177e4 LT |
171 | return -ENOLCK; |
172 | ||
92737230 TM |
173 | call = nlm_alloc_call(host); |
174 | if (call == NULL) | |
175 | return -ENOMEM; | |
1da177e4 | 176 | |
92737230 TM |
177 | nlmclnt_locks_init_private(fl, host); |
178 | /* Set up the argument struct */ | |
179 | nlmclnt_setlockargs(call, fl); | |
1da177e4 LT |
180 | |
181 | /* Keep the old signal mask */ | |
182 | spin_lock_irqsave(¤t->sighand->siglock, flags); | |
183 | oldset = current->blocked; | |
184 | ||
185 | /* If we're cleaning up locks because the process is exiting, | |
186 | * perform the RPC call asynchronously. */ | |
187 | if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) | |
188 | && fl->fl_type == F_UNLCK | |
189 | && (current->flags & PF_EXITING)) { | |
190 | sigfillset(¤t->blocked); /* Mask all signals */ | |
191 | recalc_sigpending(); | |
1da177e4 | 192 | |
1da177e4 | 193 | call->a_flags = RPC_TASK_ASYNC; |
1da177e4 | 194 | } |
92737230 | 195 | spin_unlock_irqrestore(¤t->sighand->siglock, flags); |
1da177e4 LT |
196 | |
197 | if (IS_SETLK(cmd) || IS_SETLKW(cmd)) { | |
198 | if (fl->fl_type != F_UNLCK) { | |
199 | call->a_args.block = IS_SETLKW(cmd) ? 1 : 0; | |
200 | status = nlmclnt_lock(call, fl); | |
201 | } else | |
202 | status = nlmclnt_unlock(call, fl); | |
203 | } else if (IS_GETLK(cmd)) | |
204 | status = nlmclnt_test(call, fl); | |
205 | else | |
206 | status = -EINVAL; | |
207 | ||
92737230 TM |
208 | fl->fl_ops->fl_release_private(fl); |
209 | fl->fl_ops = NULL; | |
210 | ||
1da177e4 LT |
211 | spin_lock_irqsave(¤t->sighand->siglock, flags); |
212 | current->blocked = oldset; | |
213 | recalc_sigpending(); | |
214 | spin_unlock_irqrestore(¤t->sighand->siglock, flags); | |
215 | ||
1da177e4 | 216 | dprintk("lockd: clnt proc returns %d\n", status); |
1da177e4 LT |
217 | return status; |
218 | } | |
219 | EXPORT_SYMBOL(nlmclnt_proc); | |
220 | ||
221 | /* | |
222 | * Allocate an NLM RPC call struct | |
92737230 TM |
223 | * |
224 | * Note: the caller must hold a reference to host. In case of failure, | |
225 | * this reference will be released. | |
1da177e4 | 226 | */ |
92737230 | 227 | struct nlm_rqst *nlm_alloc_call(struct nlm_host *host) |
1da177e4 LT |
228 | { |
229 | struct nlm_rqst *call; | |
230 | ||
36943fa4 TM |
231 | for(;;) { |
232 | call = kzalloc(sizeof(*call), GFP_KERNEL); | |
233 | if (call != NULL) { | |
1da177e4 LT |
234 | locks_init_lock(&call->a_args.lock.fl); |
235 | locks_init_lock(&call->a_res.lock.fl); | |
92737230 | 236 | call->a_host = host; |
1da177e4 LT |
237 | return call; |
238 | } | |
36943fa4 TM |
239 | if (signalled()) |
240 | break; | |
92737230 | 241 | printk("nlm_alloc_call: failed, waiting for memory\n"); |
041e0e3b | 242 | schedule_timeout_interruptible(5*HZ); |
1da177e4 | 243 | } |
92737230 | 244 | nlm_release_host(host); |
1da177e4 LT |
245 | return NULL; |
246 | } | |
247 | ||
92737230 TM |
248 | void nlm_release_call(struct nlm_rqst *call) |
249 | { | |
250 | nlm_release_host(call->a_host); | |
251 | nlmclnt_release_lockargs(call); | |
252 | kfree(call); | |
253 | } | |
254 | ||
255 | static void nlmclnt_rpc_release(void *data) | |
256 | { | |
257 | return nlm_release_call(data); | |
258 | } | |
259 | ||
1da177e4 LT |
260 | static int nlm_wait_on_grace(wait_queue_head_t *queue) |
261 | { | |
262 | DEFINE_WAIT(wait); | |
263 | int status = -EINTR; | |
264 | ||
265 | prepare_to_wait(queue, &wait, TASK_INTERRUPTIBLE); | |
266 | if (!signalled ()) { | |
267 | schedule_timeout(NLMCLNT_GRACE_WAIT); | |
3e1d1d28 | 268 | try_to_freeze(); |
1da177e4 LT |
269 | if (!signalled ()) |
270 | status = 0; | |
271 | } | |
272 | finish_wait(queue, &wait); | |
273 | return status; | |
274 | } | |
275 | ||
276 | /* | |
277 | * Generic NLM call | |
278 | */ | |
279 | static int | |
280 | nlmclnt_call(struct nlm_rqst *req, u32 proc) | |
281 | { | |
282 | struct nlm_host *host = req->a_host; | |
283 | struct rpc_clnt *clnt; | |
284 | struct nlm_args *argp = &req->a_args; | |
285 | struct nlm_res *resp = &req->a_res; | |
286 | struct rpc_message msg = { | |
287 | .rpc_argp = argp, | |
288 | .rpc_resp = resp, | |
289 | }; | |
290 | int status; | |
291 | ||
292 | dprintk("lockd: call procedure %d on %s\n", | |
293 | (int)proc, host->h_name); | |
294 | ||
295 | do { | |
296 | if (host->h_reclaiming && !argp->reclaim) | |
297 | goto in_grace_period; | |
298 | ||
299 | /* If we have no RPC client yet, create one. */ | |
300 | if ((clnt = nlm_bind_host(host)) == NULL) | |
301 | return -ENOLCK; | |
302 | msg.rpc_proc = &clnt->cl_procinfo[proc]; | |
303 | ||
304 | /* Perform the RPC call. If an error occurs, try again */ | |
305 | if ((status = rpc_call_sync(clnt, &msg, 0)) < 0) { | |
306 | dprintk("lockd: rpc_call returned error %d\n", -status); | |
307 | switch (status) { | |
308 | case -EPROTONOSUPPORT: | |
309 | status = -EINVAL; | |
310 | break; | |
311 | case -ECONNREFUSED: | |
312 | case -ETIMEDOUT: | |
313 | case -ENOTCONN: | |
314 | nlm_rebind_host(host); | |
315 | status = -EAGAIN; | |
316 | break; | |
317 | case -ERESTARTSYS: | |
318 | return signalled () ? -EINTR : status; | |
319 | default: | |
320 | break; | |
321 | } | |
322 | break; | |
323 | } else | |
324 | if (resp->status == NLM_LCK_DENIED_GRACE_PERIOD) { | |
325 | dprintk("lockd: server in grace period\n"); | |
326 | if (argp->reclaim) { | |
327 | printk(KERN_WARNING | |
328 | "lockd: spurious grace period reject?!\n"); | |
329 | return -ENOLCK; | |
330 | } | |
331 | } else { | |
332 | if (!argp->reclaim) { | |
333 | /* We appear to be out of the grace period */ | |
334 | wake_up_all(&host->h_gracewait); | |
335 | } | |
336 | dprintk("lockd: server returns status %d\n", resp->status); | |
337 | return 0; /* Okay, call complete */ | |
338 | } | |
339 | ||
340 | in_grace_period: | |
341 | /* | |
342 | * The server has rebooted and appears to be in the grace | |
343 | * period during which locks are only allowed to be | |
344 | * reclaimed. | |
345 | * We can only back off and try again later. | |
346 | */ | |
347 | status = nlm_wait_on_grace(&host->h_gracewait); | |
348 | } while (status == 0); | |
349 | ||
350 | return status; | |
351 | } | |
352 | ||
353 | /* | |
354 | * Generic NLM call, async version. | |
355 | */ | |
d4716624 | 356 | static int __nlm_async_call(struct nlm_rqst *req, u32 proc, struct rpc_message *msg, const struct rpc_call_ops *tk_ops) |
1da177e4 LT |
357 | { |
358 | struct nlm_host *host = req->a_host; | |
359 | struct rpc_clnt *clnt; | |
92737230 | 360 | int status = -ENOLCK; |
1da177e4 LT |
361 | |
362 | dprintk("lockd: call procedure %d on %s (async)\n", | |
363 | (int)proc, host->h_name); | |
364 | ||
365 | /* If we have no RPC client yet, create one. */ | |
92737230 TM |
366 | clnt = nlm_bind_host(host); |
367 | if (clnt == NULL) | |
368 | goto out_err; | |
d4716624 | 369 | msg->rpc_proc = &clnt->cl_procinfo[proc]; |
1da177e4 | 370 | |
1da177e4 | 371 | /* bootstrap and kick off the async RPC call */ |
d4716624 | 372 | status = rpc_call_async(clnt, msg, RPC_TASK_ASYNC, tk_ops, req); |
92737230 TM |
373 | if (status == 0) |
374 | return 0; | |
375 | out_err: | |
376 | nlm_release_call(req); | |
1da177e4 LT |
377 | return status; |
378 | } | |
379 | ||
d4716624 TM |
380 | int nlm_async_call(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops) |
381 | { | |
382 | struct rpc_message msg = { | |
383 | .rpc_argp = &req->a_args, | |
384 | .rpc_resp = &req->a_res, | |
385 | }; | |
386 | return __nlm_async_call(req, proc, &msg, tk_ops); | |
387 | } | |
388 | ||
389 | int nlm_async_reply(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops) | |
390 | { | |
391 | struct rpc_message msg = { | |
392 | .rpc_argp = &req->a_res, | |
393 | }; | |
394 | return __nlm_async_call(req, proc, &msg, tk_ops); | |
395 | } | |
396 | ||
1da177e4 LT |
397 | /* |
398 | * TEST for the presence of a conflicting lock | |
399 | */ | |
400 | static int | |
401 | nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl) | |
402 | { | |
403 | int status; | |
404 | ||
405 | status = nlmclnt_call(req, NLMPROC_TEST); | |
1da177e4 | 406 | if (status < 0) |
92737230 | 407 | goto out; |
1da177e4 | 408 | |
92737230 TM |
409 | switch (req->a_res.status) { |
410 | case NLM_LCK_GRANTED: | |
411 | fl->fl_type = F_UNLCK; | |
412 | break; | |
413 | case NLM_LCK_DENIED: | |
414 | /* | |
415 | * Report the conflicting lock back to the application. | |
416 | */ | |
417 | fl->fl_start = req->a_res.lock.fl.fl_start; | |
418 | fl->fl_end = req->a_res.lock.fl.fl_start; | |
419 | fl->fl_type = req->a_res.lock.fl.fl_type; | |
420 | fl->fl_pid = 0; | |
421 | break; | |
422 | default: | |
423 | status = nlm_stat_to_errno(req->a_res.status); | |
1da177e4 | 424 | } |
92737230 TM |
425 | out: |
426 | nlm_release_call(req); | |
427 | return status; | |
1da177e4 LT |
428 | } |
429 | ||
430 | static void nlmclnt_locks_copy_lock(struct file_lock *new, struct file_lock *fl) | |
431 | { | |
4c060b53 TM |
432 | new->fl_u.nfs_fl.state = fl->fl_u.nfs_fl.state; |
433 | new->fl_u.nfs_fl.owner = nlm_get_lockowner(fl->fl_u.nfs_fl.owner); | |
434 | list_add_tail(&new->fl_u.nfs_fl.list, &fl->fl_u.nfs_fl.owner->host->h_granted); | |
1da177e4 LT |
435 | } |
436 | ||
437 | static void nlmclnt_locks_release_private(struct file_lock *fl) | |
438 | { | |
4c060b53 | 439 | list_del(&fl->fl_u.nfs_fl.list); |
1da177e4 | 440 | nlm_put_lockowner(fl->fl_u.nfs_fl.owner); |
1da177e4 LT |
441 | } |
442 | ||
443 | static struct file_lock_operations nlmclnt_lock_ops = { | |
444 | .fl_copy_lock = nlmclnt_locks_copy_lock, | |
445 | .fl_release_private = nlmclnt_locks_release_private, | |
446 | }; | |
447 | ||
448 | static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host) | |
449 | { | |
450 | BUG_ON(fl->fl_ops != NULL); | |
451 | fl->fl_u.nfs_fl.state = 0; | |
1da177e4 | 452 | fl->fl_u.nfs_fl.owner = nlm_find_lockowner(host, fl->fl_owner); |
4c060b53 | 453 | INIT_LIST_HEAD(&fl->fl_u.nfs_fl.list); |
1da177e4 LT |
454 | fl->fl_ops = &nlmclnt_lock_ops; |
455 | } | |
456 | ||
9b073574 | 457 | static int do_vfs_lock(struct file_lock *fl) |
1da177e4 LT |
458 | { |
459 | int res = 0; | |
460 | switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) { | |
461 | case FL_POSIX: | |
462 | res = posix_lock_file_wait(fl->fl_file, fl); | |
463 | break; | |
464 | case FL_FLOCK: | |
465 | res = flock_lock_file_wait(fl->fl_file, fl); | |
466 | break; | |
467 | default: | |
468 | BUG(); | |
469 | } | |
9b073574 | 470 | return res; |
1da177e4 LT |
471 | } |
472 | ||
473 | /* | |
474 | * LOCK: Try to create a lock | |
475 | * | |
476 | * Programmer Harassment Alert | |
477 | * | |
478 | * When given a blocking lock request in a sync RPC call, the HPUX lockd | |
479 | * will faithfully return LCK_BLOCKED but never cares to notify us when | |
480 | * the lock could be granted. This way, our local process could hang | |
481 | * around forever waiting for the callback. | |
482 | * | |
483 | * Solution A: Implement busy-waiting | |
484 | * Solution B: Use the async version of the call (NLM_LOCK_{MSG,RES}) | |
485 | * | |
486 | * For now I am implementing solution A, because I hate the idea of | |
487 | * re-implementing lockd for a third time in two months. The async | |
488 | * calls shouldn't be too hard to do, however. | |
489 | * | |
490 | * This is one of the lovely things about standards in the NFS area: | |
491 | * they're so soft and squishy you can't really blame HP for doing this. | |
492 | */ | |
493 | static int | |
494 | nlmclnt_lock(struct nlm_rqst *req, struct file_lock *fl) | |
495 | { | |
496 | struct nlm_host *host = req->a_host; | |
497 | struct nlm_res *resp = &req->a_res; | |
3a649b88 | 498 | struct nlm_wait *block = NULL; |
01c3b861 | 499 | unsigned char fl_flags = fl->fl_flags; |
3a649b88 | 500 | int status = -ENOLCK; |
1da177e4 LT |
501 | |
502 | if (!host->h_monitored && nsm_monitor(host) < 0) { | |
503 | printk(KERN_NOTICE "lockd: failed to monitor %s\n", | |
504 | host->h_name); | |
1da177e4 LT |
505 | goto out; |
506 | } | |
01c3b861 TM |
507 | fl->fl_flags |= FL_ACCESS; |
508 | status = do_vfs_lock(fl); | |
509 | if (status < 0) | |
510 | goto out; | |
1da177e4 | 511 | |
3a649b88 | 512 | block = nlmclnt_prepare_block(host, fl); |
28df955a | 513 | again: |
ecdbf769 | 514 | for(;;) { |
28df955a TM |
515 | /* Reboot protection */ |
516 | fl->fl_u.nfs_fl.state = host->h_state; | |
ecdbf769 TM |
517 | status = nlmclnt_call(req, NLMPROC_LOCK); |
518 | if (status < 0) | |
519 | goto out_unblock; | |
3a649b88 | 520 | if (!req->a_args.block) |
ecdbf769 | 521 | break; |
ecdbf769 TM |
522 | /* Did a reclaimer thread notify us of a server reboot? */ |
523 | if (resp->status == NLM_LCK_DENIED_GRACE_PERIOD) | |
524 | continue; | |
525 | if (resp->status != NLM_LCK_BLOCKED) | |
526 | break; | |
3a649b88 TM |
527 | /* Wait on an NLM blocking lock */ |
528 | status = nlmclnt_block(block, req, NLMCLNT_POLL_TIMEOUT); | |
529 | /* if we were interrupted. Send a CANCEL request to the server | |
ecdbf769 TM |
530 | * and exit |
531 | */ | |
3a649b88 TM |
532 | if (status < 0) |
533 | goto out_unblock; | |
534 | if (resp->status != NLM_LCK_BLOCKED) | |
535 | break; | |
ecdbf769 | 536 | } |
1da177e4 LT |
537 | |
538 | if (resp->status == NLM_LCK_GRANTED) { | |
28df955a TM |
539 | down_read(&host->h_rwsem); |
540 | /* Check whether or not the server has rebooted */ | |
541 | if (fl->fl_u.nfs_fl.state != host->h_state) { | |
542 | up_read(&host->h_rwsem); | |
543 | goto again; | |
544 | } | |
4c060b53 | 545 | /* Ensure the resulting lock will get added to granted list */ |
01c3b861 | 546 | fl->fl_flags = fl_flags | FL_SLEEP; |
9b073574 TM |
547 | if (do_vfs_lock(fl) < 0) |
548 | printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__); | |
28df955a | 549 | up_read(&host->h_rwsem); |
1da177e4 LT |
550 | } |
551 | status = nlm_stat_to_errno(resp->status); | |
ecdbf769 | 552 | out_unblock: |
3a649b88 | 553 | nlmclnt_finish_block(block); |
ecdbf769 TM |
554 | /* Cancel the blocked request if it is still pending */ |
555 | if (resp->status == NLM_LCK_BLOCKED) | |
16fb2425 | 556 | nlmclnt_cancel(host, req->a_args.block, fl); |
1da177e4 | 557 | out: |
92737230 | 558 | nlm_release_call(req); |
01c3b861 | 559 | fl->fl_flags = fl_flags; |
1da177e4 LT |
560 | return status; |
561 | } | |
562 | ||
563 | /* | |
564 | * RECLAIM: Try to reclaim a lock | |
565 | */ | |
566 | int | |
567 | nlmclnt_reclaim(struct nlm_host *host, struct file_lock *fl) | |
568 | { | |
569 | struct nlm_rqst reqst, *req; | |
570 | int status; | |
571 | ||
572 | req = &reqst; | |
573 | memset(req, 0, sizeof(*req)); | |
574 | locks_init_lock(&req->a_args.lock.fl); | |
575 | locks_init_lock(&req->a_res.lock.fl); | |
576 | req->a_host = host; | |
577 | req->a_flags = 0; | |
578 | ||
579 | /* Set up the argument struct */ | |
580 | nlmclnt_setlockargs(req, fl); | |
581 | req->a_args.reclaim = 1; | |
582 | ||
583 | if ((status = nlmclnt_call(req, NLMPROC_LOCK)) >= 0 | |
584 | && req->a_res.status == NLM_LCK_GRANTED) | |
585 | return 0; | |
586 | ||
587 | printk(KERN_WARNING "lockd: failed to reclaim lock for pid %d " | |
588 | "(errno %d, status %d)\n", fl->fl_pid, | |
589 | status, req->a_res.status); | |
590 | ||
591 | /* | |
592 | * FIXME: This is a serious failure. We can | |
593 | * | |
594 | * a. Ignore the problem | |
595 | * b. Send the owning process some signal (Linux doesn't have | |
596 | * SIGLOST, though...) | |
597 | * c. Retry the operation | |
598 | * | |
599 | * Until someone comes up with a simple implementation | |
600 | * for b or c, I'll choose option a. | |
601 | */ | |
602 | ||
603 | return -ENOLCK; | |
604 | } | |
605 | ||
606 | /* | |
607 | * UNLOCK: remove an existing lock | |
608 | */ | |
609 | static int | |
610 | nlmclnt_unlock(struct nlm_rqst *req, struct file_lock *fl) | |
611 | { | |
28df955a | 612 | struct nlm_host *host = req->a_host; |
1da177e4 | 613 | struct nlm_res *resp = &req->a_res; |
9b073574 | 614 | int status = 0; |
1da177e4 | 615 | |
30f4e20a TM |
616 | /* |
617 | * Note: the server is supposed to either grant us the unlock | |
618 | * request, or to deny it with NLM_LCK_DENIED_GRACE_PERIOD. In either | |
619 | * case, we want to unlock. | |
620 | */ | |
9b073574 | 621 | fl->fl_flags |= FL_EXISTS; |
28df955a | 622 | down_read(&host->h_rwsem); |
9b073574 TM |
623 | if (do_vfs_lock(fl) == -ENOENT) { |
624 | up_read(&host->h_rwsem); | |
625 | goto out; | |
626 | } | |
28df955a | 627 | up_read(&host->h_rwsem); |
30f4e20a | 628 | |
92737230 TM |
629 | if (req->a_flags & RPC_TASK_ASYNC) |
630 | return nlm_async_call(req, NLMPROC_UNLOCK, &nlmclnt_unlock_ops); | |
1da177e4 LT |
631 | |
632 | status = nlmclnt_call(req, NLMPROC_UNLOCK); | |
1da177e4 | 633 | if (status < 0) |
92737230 | 634 | goto out; |
1da177e4 | 635 | |
1da177e4 | 636 | if (resp->status == NLM_LCK_GRANTED) |
92737230 | 637 | goto out; |
1da177e4 LT |
638 | |
639 | if (resp->status != NLM_LCK_DENIED_NOLOCKS) | |
640 | printk("lockd: unexpected unlock status: %d\n", resp->status); | |
1da177e4 | 641 | /* What to do now? I'm out of my depth... */ |
92737230 TM |
642 | status = -ENOLCK; |
643 | out: | |
644 | nlm_release_call(req); | |
645 | return status; | |
1da177e4 LT |
646 | } |
647 | ||
963d8fe5 | 648 | static void nlmclnt_unlock_callback(struct rpc_task *task, void *data) |
1da177e4 | 649 | { |
963d8fe5 | 650 | struct nlm_rqst *req = data; |
1da177e4 LT |
651 | int status = req->a_res.status; |
652 | ||
653 | if (RPC_ASSASSINATED(task)) | |
654 | goto die; | |
655 | ||
656 | if (task->tk_status < 0) { | |
657 | dprintk("lockd: unlock failed (err = %d)\n", -task->tk_status); | |
658 | goto retry_rebind; | |
659 | } | |
660 | if (status == NLM_LCK_DENIED_GRACE_PERIOD) { | |
661 | rpc_delay(task, NLMCLNT_GRACE_WAIT); | |
662 | goto retry_unlock; | |
663 | } | |
664 | if (status != NLM_LCK_GRANTED) | |
665 | printk(KERN_WARNING "lockd: unexpected unlock status: %d\n", status); | |
666 | die: | |
1da177e4 LT |
667 | return; |
668 | retry_rebind: | |
669 | nlm_rebind_host(req->a_host); | |
670 | retry_unlock: | |
671 | rpc_restart_call(task); | |
672 | } | |
673 | ||
963d8fe5 TM |
674 | static const struct rpc_call_ops nlmclnt_unlock_ops = { |
675 | .rpc_call_done = nlmclnt_unlock_callback, | |
92737230 | 676 | .rpc_release = nlmclnt_rpc_release, |
963d8fe5 TM |
677 | }; |
678 | ||
1da177e4 LT |
679 | /* |
680 | * Cancel a blocked lock request. | |
681 | * We always use an async RPC call for this in order not to hang a | |
682 | * process that has been Ctrl-C'ed. | |
683 | */ | |
16fb2425 | 684 | static int nlmclnt_cancel(struct nlm_host *host, int block, struct file_lock *fl) |
1da177e4 LT |
685 | { |
686 | struct nlm_rqst *req; | |
687 | unsigned long flags; | |
688 | sigset_t oldset; | |
689 | int status; | |
690 | ||
691 | /* Block all signals while setting up call */ | |
692 | spin_lock_irqsave(¤t->sighand->siglock, flags); | |
693 | oldset = current->blocked; | |
694 | sigfillset(¤t->blocked); | |
695 | recalc_sigpending(); | |
696 | spin_unlock_irqrestore(¤t->sighand->siglock, flags); | |
697 | ||
92737230 | 698 | req = nlm_alloc_call(nlm_get_host(host)); |
1da177e4 LT |
699 | if (!req) |
700 | return -ENOMEM; | |
1da177e4 LT |
701 | req->a_flags = RPC_TASK_ASYNC; |
702 | ||
703 | nlmclnt_setlockargs(req, fl); | |
16fb2425 | 704 | req->a_args.block = block; |
1da177e4 | 705 | |
92737230 | 706 | status = nlm_async_call(req, NLMPROC_CANCEL, &nlmclnt_cancel_ops); |
1da177e4 LT |
707 | |
708 | spin_lock_irqsave(¤t->sighand->siglock, flags); | |
709 | current->blocked = oldset; | |
710 | recalc_sigpending(); | |
711 | spin_unlock_irqrestore(¤t->sighand->siglock, flags); | |
712 | ||
713 | return status; | |
714 | } | |
715 | ||
963d8fe5 | 716 | static void nlmclnt_cancel_callback(struct rpc_task *task, void *data) |
1da177e4 | 717 | { |
963d8fe5 | 718 | struct nlm_rqst *req = data; |
1da177e4 LT |
719 | |
720 | if (RPC_ASSASSINATED(task)) | |
721 | goto die; | |
722 | ||
723 | if (task->tk_status < 0) { | |
724 | dprintk("lockd: CANCEL call error %d, retrying.\n", | |
725 | task->tk_status); | |
726 | goto retry_cancel; | |
727 | } | |
728 | ||
729 | dprintk("lockd: cancel status %d (task %d)\n", | |
730 | req->a_res.status, task->tk_pid); | |
731 | ||
732 | switch (req->a_res.status) { | |
733 | case NLM_LCK_GRANTED: | |
734 | case NLM_LCK_DENIED_GRACE_PERIOD: | |
35576cba | 735 | case NLM_LCK_DENIED: |
1da177e4 LT |
736 | /* Everything's good */ |
737 | break; | |
738 | case NLM_LCK_DENIED_NOLOCKS: | |
739 | dprintk("lockd: CANCEL failed (server has no locks)\n"); | |
740 | goto retry_cancel; | |
741 | default: | |
742 | printk(KERN_NOTICE "lockd: weird return %d for CANCEL call\n", | |
743 | req->a_res.status); | |
744 | } | |
745 | ||
746 | die: | |
1da177e4 LT |
747 | return; |
748 | ||
749 | retry_cancel: | |
aaaa9942 TM |
750 | /* Don't ever retry more than 3 times */ |
751 | if (req->a_retries++ >= NLMCLNT_MAX_RETRIES) | |
752 | goto die; | |
1da177e4 LT |
753 | nlm_rebind_host(req->a_host); |
754 | rpc_restart_call(task); | |
755 | rpc_delay(task, 30 * HZ); | |
756 | } | |
757 | ||
963d8fe5 TM |
758 | static const struct rpc_call_ops nlmclnt_cancel_ops = { |
759 | .rpc_call_done = nlmclnt_cancel_callback, | |
92737230 | 760 | .rpc_release = nlmclnt_rpc_release, |
963d8fe5 TM |
761 | }; |
762 | ||
1da177e4 LT |
763 | /* |
764 | * Convert an NLM status code to a generic kernel errno | |
765 | */ | |
766 | static int | |
767 | nlm_stat_to_errno(u32 status) | |
768 | { | |
769 | switch(status) { | |
770 | case NLM_LCK_GRANTED: | |
771 | return 0; | |
772 | case NLM_LCK_DENIED: | |
773 | return -EAGAIN; | |
774 | case NLM_LCK_DENIED_NOLOCKS: | |
775 | case NLM_LCK_DENIED_GRACE_PERIOD: | |
776 | return -ENOLCK; | |
777 | case NLM_LCK_BLOCKED: | |
778 | printk(KERN_NOTICE "lockd: unexpected status NLM_BLOCKED\n"); | |
779 | return -ENOLCK; | |
780 | #ifdef CONFIG_LOCKD_V4 | |
781 | case NLM_DEADLCK: | |
782 | return -EDEADLK; | |
783 | case NLM_ROFS: | |
784 | return -EROFS; | |
785 | case NLM_STALE_FH: | |
786 | return -ESTALE; | |
787 | case NLM_FBIG: | |
788 | return -EOVERFLOW; | |
789 | case NLM_FAILED: | |
790 | return -ENOLCK; | |
791 | #endif | |
792 | } | |
793 | printk(KERN_NOTICE "lockd: unexpected server status %d\n", status); | |
794 | return -ENOLCK; | |
795 | } |