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