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