Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/fs/lockd/svclock.c | |
3 | * | |
4 | * Handling of server-side locks, mostly of the blocked variety. | |
5 | * This is the ugliest part of lockd because we tread on very thin ice. | |
6 | * GRANT and CANCEL calls may get stuck, meet in mid-flight, etc. | |
7 | * IMNSHO introducing the grant callback into the NLM protocol was one | |
8 | * of the worst ideas Sun ever had. Except maybe for the idea of doing | |
9 | * NFS file locking at all. | |
10 | * | |
11 | * I'm trying hard to avoid race conditions by protecting most accesses | |
12 | * to a file's list of blocked locks through a semaphore. The global | |
13 | * list of blocked locks is not protected in this fashion however. | |
14 | * Therefore, some functions (such as the RPC callback for the async grant | |
15 | * call) move blocked locks towards the head of the list *while some other | |
16 | * process might be traversing it*. This should not be a problem in | |
17 | * practice, because this will only cause functions traversing the list | |
18 | * to visit some blocks twice. | |
19 | * | |
20 | * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de> | |
21 | */ | |
22 | ||
1da177e4 | 23 | #include <linux/types.h> |
5a0e3ad6 | 24 | #include <linux/slab.h> |
1da177e4 LT |
25 | #include <linux/errno.h> |
26 | #include <linux/kernel.h> | |
27 | #include <linux/sched.h> | |
28 | #include <linux/smp_lock.h> | |
29 | #include <linux/sunrpc/clnt.h> | |
30 | #include <linux/sunrpc/svc.h> | |
31 | #include <linux/lockd/nlm.h> | |
32 | #include <linux/lockd/lockd.h> | |
d751a7cd | 33 | #include <linux/kthread.h> |
1da177e4 LT |
34 | |
35 | #define NLMDBG_FACILITY NLMDBG_SVCLOCK | |
36 | ||
37 | #ifdef CONFIG_LOCKD_V4 | |
38 | #define nlm_deadlock nlm4_deadlock | |
39 | #else | |
40 | #define nlm_deadlock nlm_lck_denied | |
41 | #endif | |
42 | ||
6849c0ca | 43 | static void nlmsvc_release_block(struct nlm_block *block); |
1da177e4 | 44 | static void nlmsvc_insert_block(struct nlm_block *block, unsigned long); |
68a2d76c | 45 | static void nlmsvc_remove_block(struct nlm_block *block); |
963d8fe5 | 46 | |
5e1abf8c TM |
47 | static int nlmsvc_setgrantargs(struct nlm_rqst *call, struct nlm_lock *lock); |
48 | static void nlmsvc_freegrantargs(struct nlm_rqst *call); | |
963d8fe5 | 49 | static const struct rpc_call_ops nlmsvc_grant_ops; |
1da177e4 LT |
50 | |
51 | /* | |
52 | * The list of blocked locks to retry | |
53 | */ | |
68a2d76c | 54 | static LIST_HEAD(nlm_blocked); |
1da177e4 LT |
55 | |
56 | /* | |
57 | * Insert a blocked lock into the global list | |
58 | */ | |
59 | static void | |
60 | nlmsvc_insert_block(struct nlm_block *block, unsigned long when) | |
61 | { | |
68a2d76c OK |
62 | struct nlm_block *b; |
63 | struct list_head *pos; | |
1da177e4 LT |
64 | |
65 | dprintk("lockd: nlmsvc_insert_block(%p, %ld)\n", block, when); | |
68a2d76c OK |
66 | if (list_empty(&block->b_list)) { |
67 | kref_get(&block->b_count); | |
68 | } else { | |
69 | list_del_init(&block->b_list); | |
70 | } | |
71 | ||
72 | pos = &nlm_blocked; | |
1da177e4 LT |
73 | if (when != NLM_NEVER) { |
74 | if ((when += jiffies) == NLM_NEVER) | |
75 | when ++; | |
68a2d76c OK |
76 | list_for_each(pos, &nlm_blocked) { |
77 | b = list_entry(pos, struct nlm_block, b_list); | |
78 | if (time_after(b->b_when,when) || b->b_when == NLM_NEVER) | |
79 | break; | |
80 | } | |
81 | /* On normal exit from the loop, pos == &nlm_blocked, | |
82 | * so we will be adding to the end of the list - good | |
83 | */ | |
84 | } | |
1da177e4 | 85 | |
68a2d76c | 86 | list_add_tail(&block->b_list, pos); |
1da177e4 | 87 | block->b_when = when; |
1da177e4 LT |
88 | } |
89 | ||
90 | /* | |
91 | * Remove a block from the global list | |
92 | */ | |
68a2d76c | 93 | static inline void |
1da177e4 LT |
94 | nlmsvc_remove_block(struct nlm_block *block) |
95 | { | |
68a2d76c OK |
96 | if (!list_empty(&block->b_list)) { |
97 | list_del_init(&block->b_list); | |
98 | nlmsvc_release_block(block); | |
1da177e4 | 99 | } |
1da177e4 LT |
100 | } |
101 | ||
102 | /* | |
d9f6eb75 | 103 | * Find a block for a given lock |
1da177e4 LT |
104 | */ |
105 | static struct nlm_block * | |
d9f6eb75 | 106 | nlmsvc_lookup_block(struct nlm_file *file, struct nlm_lock *lock) |
1da177e4 | 107 | { |
68a2d76c | 108 | struct nlm_block *block; |
1da177e4 LT |
109 | struct file_lock *fl; |
110 | ||
111 | dprintk("lockd: nlmsvc_lookup_block f=%p pd=%d %Ld-%Ld ty=%d\n", | |
112 | file, lock->fl.fl_pid, | |
113 | (long long)lock->fl.fl_start, | |
114 | (long long)lock->fl.fl_end, lock->fl.fl_type); | |
68a2d76c | 115 | list_for_each_entry(block, &nlm_blocked, b_list) { |
92737230 | 116 | fl = &block->b_call->a_args.lock.fl; |
1da177e4 LT |
117 | dprintk("lockd: check f=%p pd=%d %Ld-%Ld ty=%d cookie=%s\n", |
118 | block->b_file, fl->fl_pid, | |
119 | (long long)fl->fl_start, | |
120 | (long long)fl->fl_end, fl->fl_type, | |
92737230 | 121 | nlmdbg_cookie2a(&block->b_call->a_args.cookie)); |
1da177e4 | 122 | if (block->b_file == file && nlm_compare_locks(fl, &lock->fl)) { |
6849c0ca | 123 | kref_get(&block->b_count); |
1da177e4 LT |
124 | return block; |
125 | } | |
126 | } | |
127 | ||
128 | return NULL; | |
129 | } | |
130 | ||
131 | static inline int nlm_cookie_match(struct nlm_cookie *a, struct nlm_cookie *b) | |
132 | { | |
6d7bbbba | 133 | if (a->len != b->len) |
1da177e4 | 134 | return 0; |
6d7bbbba | 135 | if (memcmp(a->data, b->data, a->len)) |
1da177e4 LT |
136 | return 0; |
137 | return 1; | |
138 | } | |
139 | ||
140 | /* | |
141 | * Find a block with a given NLM cookie. | |
142 | */ | |
143 | static inline struct nlm_block * | |
39be4502 | 144 | nlmsvc_find_block(struct nlm_cookie *cookie) |
1da177e4 LT |
145 | { |
146 | struct nlm_block *block; | |
147 | ||
68a2d76c | 148 | list_for_each_entry(block, &nlm_blocked, b_list) { |
39be4502 | 149 | if (nlm_cookie_match(&block->b_call->a_args.cookie,cookie)) |
68a2d76c | 150 | goto found; |
1da177e4 LT |
151 | } |
152 | ||
68a2d76c OK |
153 | return NULL; |
154 | ||
155 | found: | |
39be4502 | 156 | dprintk("nlmsvc_find_block(%s): block=%p\n", nlmdbg_cookie2a(cookie), block); |
68a2d76c | 157 | kref_get(&block->b_count); |
1da177e4 LT |
158 | return block; |
159 | } | |
160 | ||
161 | /* | |
162 | * Create a block and initialize it. | |
163 | * | |
164 | * Note: we explicitly set the cookie of the grant reply to that of | |
165 | * the blocked lock request. The spec explicitly mentions that the client | |
166 | * should _not_ rely on the callback containing the same cookie as the | |
167 | * request, but (as I found out later) that's because some implementations | |
168 | * do just this. Never mind the standards comittees, they support our | |
169 | * logging industries. | |
39be4502 OK |
170 | * |
171 | * 10 years later: I hope we can safely ignore these old and broken | |
172 | * clients by now. Let's fix this so we can uniquely identify an incoming | |
173 | * GRANTED_RES message by cookie, without having to rely on the client's IP | |
174 | * address. --okir | |
1da177e4 | 175 | */ |
255129d1 TM |
176 | static struct nlm_block * |
177 | nlmsvc_create_block(struct svc_rqst *rqstp, struct nlm_host *host, | |
178 | struct nlm_file *file, struct nlm_lock *lock, | |
179 | struct nlm_cookie *cookie) | |
1da177e4 LT |
180 | { |
181 | struct nlm_block *block; | |
92737230 | 182 | struct nlm_rqst *call = NULL; |
1da177e4 | 183 | |
560de0e6 | 184 | nlm_get_host(host); |
92737230 TM |
185 | call = nlm_alloc_call(host); |
186 | if (call == NULL) | |
187 | return NULL; | |
188 | ||
1da177e4 | 189 | /* Allocate memory for block, and initialize arguments */ |
92737230 TM |
190 | block = kzalloc(sizeof(*block), GFP_KERNEL); |
191 | if (block == NULL) | |
1da177e4 | 192 | goto failed; |
6849c0ca | 193 | kref_init(&block->b_count); |
68a2d76c OK |
194 | INIT_LIST_HEAD(&block->b_list); |
195 | INIT_LIST_HEAD(&block->b_flist); | |
1da177e4 | 196 | |
92737230 | 197 | if (!nlmsvc_setgrantargs(call, lock)) |
1da177e4 LT |
198 | goto failed_free; |
199 | ||
200 | /* Set notifier function for VFS, and init args */ | |
92737230 TM |
201 | call->a_args.lock.fl.fl_flags |= FL_SLEEP; |
202 | call->a_args.lock.fl.fl_lmops = &nlmsvc_lock_operations; | |
39be4502 | 203 | nlmclnt_next_cookie(&call->a_args.cookie); |
1da177e4 LT |
204 | |
205 | dprintk("lockd: created block %p...\n", block); | |
206 | ||
207 | /* Create and initialize the block */ | |
208 | block->b_daemon = rqstp->rq_server; | |
209 | block->b_host = host; | |
210 | block->b_file = file; | |
5ea0d750 | 211 | block->b_fl = NULL; |
d9f6eb75 | 212 | file->f_count++; |
1da177e4 LT |
213 | |
214 | /* Add to file's list of blocks */ | |
68a2d76c | 215 | list_add(&block->b_flist, &file->f_blocks); |
1da177e4 LT |
216 | |
217 | /* Set up RPC arguments for callback */ | |
92737230 | 218 | block->b_call = call; |
1da177e4 | 219 | call->a_flags = RPC_TASK_ASYNC; |
92737230 | 220 | call->a_block = block; |
1da177e4 LT |
221 | |
222 | return block; | |
223 | ||
224 | failed_free: | |
225 | kfree(block); | |
226 | failed: | |
92737230 | 227 | nlm_release_call(call); |
1da177e4 LT |
228 | return NULL; |
229 | } | |
230 | ||
231 | /* | |
3c61eecb | 232 | * Delete a block. |
1da177e4 LT |
233 | * It is the caller's responsibility to check whether the file |
234 | * can be closed hereafter. | |
235 | */ | |
6849c0ca | 236 | static int nlmsvc_unlink_block(struct nlm_block *block) |
1da177e4 | 237 | { |
09c7938c | 238 | int status; |
6849c0ca | 239 | dprintk("lockd: unlinking block %p...\n", block); |
1da177e4 LT |
240 | |
241 | /* Remove block from list */ | |
92737230 | 242 | status = posix_unblock_lock(block->b_file->f_file, &block->b_call->a_args.lock.fl); |
1da177e4 | 243 | nlmsvc_remove_block(block); |
6849c0ca TM |
244 | return status; |
245 | } | |
1da177e4 | 246 | |
6849c0ca TM |
247 | static void nlmsvc_free_block(struct kref *kref) |
248 | { | |
249 | struct nlm_block *block = container_of(kref, struct nlm_block, b_count); | |
250 | struct nlm_file *file = block->b_file; | |
6849c0ca TM |
251 | |
252 | dprintk("lockd: freeing block %p...\n", block); | |
1da177e4 LT |
253 | |
254 | /* Remove block from file's list of blocks */ | |
89e63ef6 | 255 | mutex_lock(&file->f_mutex); |
68a2d76c | 256 | list_del_init(&block->b_flist); |
89e63ef6 | 257 | mutex_unlock(&file->f_mutex); |
1da177e4 | 258 | |
92737230 TM |
259 | nlmsvc_freegrantargs(block->b_call); |
260 | nlm_release_call(block->b_call); | |
d9f6eb75 | 261 | nlm_release_file(block->b_file); |
0e4ac9d9 | 262 | kfree(block->b_fl); |
1da177e4 | 263 | kfree(block); |
6849c0ca TM |
264 | } |
265 | ||
266 | static void nlmsvc_release_block(struct nlm_block *block) | |
267 | { | |
268 | if (block != NULL) | |
269 | kref_put(&block->b_count, nlmsvc_free_block); | |
1da177e4 LT |
270 | } |
271 | ||
f2af793d OK |
272 | /* |
273 | * Loop over all blocks and delete blocks held by | |
274 | * a matching host. | |
275 | */ | |
276 | void nlmsvc_traverse_blocks(struct nlm_host *host, | |
277 | struct nlm_file *file, | |
278 | nlm_host_match_fn_t match) | |
d9f6eb75 | 279 | { |
68a2d76c | 280 | struct nlm_block *block, *next; |
d9f6eb75 TM |
281 | |
282 | restart: | |
89e63ef6 | 283 | mutex_lock(&file->f_mutex); |
68a2d76c | 284 | list_for_each_entry_safe(block, next, &file->f_blocks, b_flist) { |
f2af793d | 285 | if (!match(block->b_host, host)) |
d9f6eb75 | 286 | continue; |
68a2d76c OK |
287 | /* Do not destroy blocks that are not on |
288 | * the global retry list - why? */ | |
289 | if (list_empty(&block->b_list)) | |
d9f6eb75 TM |
290 | continue; |
291 | kref_get(&block->b_count); | |
89e63ef6 | 292 | mutex_unlock(&file->f_mutex); |
d9f6eb75 TM |
293 | nlmsvc_unlink_block(block); |
294 | nlmsvc_release_block(block); | |
295 | goto restart; | |
296 | } | |
89e63ef6 | 297 | mutex_unlock(&file->f_mutex); |
d9f6eb75 TM |
298 | } |
299 | ||
5e1abf8c TM |
300 | /* |
301 | * Initialize arguments for GRANTED call. The nlm_rqst structure | |
302 | * has been cleared already. | |
303 | */ | |
304 | static int nlmsvc_setgrantargs(struct nlm_rqst *call, struct nlm_lock *lock) | |
305 | { | |
306 | locks_copy_lock(&call->a_args.lock.fl, &lock->fl); | |
307 | memcpy(&call->a_args.lock.fh, &lock->fh, sizeof(call->a_args.lock.fh)); | |
e9ff3990 | 308 | call->a_args.lock.caller = utsname()->nodename; |
5e1abf8c TM |
309 | call->a_args.lock.oh.len = lock->oh.len; |
310 | ||
311 | /* set default data area */ | |
312 | call->a_args.lock.oh.data = call->a_owner; | |
313 | call->a_args.lock.svid = lock->fl.fl_pid; | |
314 | ||
315 | if (lock->oh.len > NLMCLNT_OHSIZE) { | |
316 | void *data = kmalloc(lock->oh.len, GFP_KERNEL); | |
92737230 | 317 | if (!data) |
5e1abf8c | 318 | return 0; |
5e1abf8c TM |
319 | call->a_args.lock.oh.data = (u8 *) data; |
320 | } | |
321 | ||
322 | memcpy(call->a_args.lock.oh.data, lock->oh.data, lock->oh.len); | |
323 | return 1; | |
324 | } | |
325 | ||
326 | static void nlmsvc_freegrantargs(struct nlm_rqst *call) | |
327 | { | |
92737230 | 328 | if (call->a_args.lock.oh.data != call->a_owner) |
5e1abf8c | 329 | kfree(call->a_args.lock.oh.data); |
a9e61e25 FB |
330 | |
331 | locks_release_private(&call->a_args.lock.fl); | |
5e1abf8c TM |
332 | } |
333 | ||
2b36f412 ME |
334 | /* |
335 | * Deferred lock request handling for non-blocking lock | |
336 | */ | |
ca5c8cde | 337 | static __be32 |
2b36f412 ME |
338 | nlmsvc_defer_lock_rqst(struct svc_rqst *rqstp, struct nlm_block *block) |
339 | { | |
ca5c8cde | 340 | __be32 status = nlm_lck_denied_nolocks; |
2b36f412 ME |
341 | |
342 | block->b_flags |= B_QUEUED; | |
343 | ||
344 | nlmsvc_insert_block(block, NLM_TIMEOUT); | |
345 | ||
346 | block->b_cache_req = &rqstp->rq_chandle; | |
347 | if (rqstp->rq_chandle.defer) { | |
348 | block->b_deferred_req = | |
349 | rqstp->rq_chandle.defer(block->b_cache_req); | |
350 | if (block->b_deferred_req != NULL) | |
351 | status = nlm_drop_reply; | |
352 | } | |
353 | dprintk("lockd: nlmsvc_defer_lock_rqst block %p flags %d status %d\n", | |
ca5c8cde | 354 | block, block->b_flags, ntohl(status)); |
2b36f412 ME |
355 | |
356 | return status; | |
357 | } | |
358 | ||
1da177e4 LT |
359 | /* |
360 | * Attempt to establish a lock, and if it can't be granted, block it | |
361 | * if required. | |
362 | */ | |
52921e02 | 363 | __be32 |
1da177e4 | 364 | nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file, |
6cde4de8 | 365 | struct nlm_host *host, struct nlm_lock *lock, int wait, |
b2b50289 | 366 | struct nlm_cookie *cookie, int reclaim) |
1da177e4 | 367 | { |
f8120480 | 368 | struct nlm_block *block = NULL; |
1da177e4 | 369 | int error; |
52921e02 | 370 | __be32 ret; |
1da177e4 LT |
371 | |
372 | dprintk("lockd: nlmsvc_lock(%s/%ld, ty=%d, pi=%d, %Ld-%Ld, bl=%d)\n", | |
225a719f JS |
373 | file->f_file->f_path.dentry->d_inode->i_sb->s_id, |
374 | file->f_file->f_path.dentry->d_inode->i_ino, | |
1da177e4 LT |
375 | lock->fl.fl_type, lock->fl.fl_pid, |
376 | (long long)lock->fl.fl_start, | |
377 | (long long)lock->fl.fl_end, | |
378 | wait); | |
379 | ||
1da177e4 | 380 | /* Lock file against concurrent access */ |
89e63ef6 | 381 | mutex_lock(&file->f_mutex); |
f8120480 ME |
382 | /* Get existing block (in case client is busy-waiting) |
383 | * or create new block | |
384 | */ | |
d9f6eb75 | 385 | block = nlmsvc_lookup_block(file, lock); |
09c7938c | 386 | if (block == NULL) { |
560de0e6 | 387 | block = nlmsvc_create_block(rqstp, host, file, lock, cookie); |
f8120480 ME |
388 | ret = nlm_lck_denied_nolocks; |
389 | if (block == NULL) | |
390 | goto out; | |
92737230 | 391 | lock = &block->b_call->a_args.lock; |
f8120480 ME |
392 | } else |
393 | lock->fl.fl_flags &= ~FL_SLEEP; | |
1da177e4 | 394 | |
1a8322b2 ME |
395 | if (block->b_flags & B_QUEUED) { |
396 | dprintk("lockd: nlmsvc_lock deferred block %p flags %d\n", | |
397 | block, block->b_flags); | |
398 | if (block->b_granted) { | |
399 | nlmsvc_unlink_block(block); | |
400 | ret = nlm_granted; | |
401 | goto out; | |
402 | } | |
403 | if (block->b_flags & B_TIMED_OUT) { | |
404 | nlmsvc_unlink_block(block); | |
405 | ret = nlm_lck_denied; | |
406 | goto out; | |
407 | } | |
408 | ret = nlm_drop_reply; | |
409 | goto out; | |
410 | } | |
1da177e4 | 411 | |
b2b50289 BF |
412 | if (locks_in_grace() && !reclaim) { |
413 | ret = nlm_lck_denied_grace_period; | |
414 | goto out; | |
415 | } | |
d22b1cff BF |
416 | if (reclaim && !locks_in_grace()) { |
417 | ret = nlm_lck_denied_grace_period; | |
418 | goto out; | |
419 | } | |
b2b50289 | 420 | |
1a8322b2 ME |
421 | if (!wait) |
422 | lock->fl.fl_flags &= ~FL_SLEEP; | |
423 | error = vfs_lock_file(file->f_file, F_SETLK, &lock->fl, NULL); | |
424 | lock->fl.fl_flags &= ~FL_SLEEP; | |
a85f193e | 425 | |
1a8322b2 | 426 | dprintk("lockd: vfs_lock_file returned %d\n", error); |
6d7bbbba | 427 | switch (error) { |
1da177e4 | 428 | case 0: |
15dadef9 AA |
429 | ret = nlm_granted; |
430 | goto out; | |
09c7938c | 431 | case -EAGAIN: |
e33d1ea6 MS |
432 | /* |
433 | * If this is a blocking request for an | |
434 | * already pending lock request then we need | |
435 | * to put it back on lockd's block list | |
436 | */ | |
437 | if (wait) | |
438 | break; | |
1a8322b2 | 439 | ret = nlm_lck_denied; |
e33d1ea6 | 440 | goto out; |
bde74e4b | 441 | case FILE_LOCK_DEFERRED: |
1a8322b2 ME |
442 | if (wait) |
443 | break; | |
444 | /* Filesystem lock operation is in progress | |
445 | Add it to the queue waiting for callback */ | |
446 | ret = nlmsvc_defer_lock_rqst(rqstp, block); | |
447 | goto out; | |
09c7938c | 448 | case -EDEADLK: |
15dadef9 AA |
449 | ret = nlm_deadlock; |
450 | goto out; | |
1da177e4 | 451 | default: /* includes ENOLCK */ |
15dadef9 AA |
452 | ret = nlm_lck_denied_nolocks; |
453 | goto out; | |
1da177e4 LT |
454 | } |
455 | ||
09c7938c | 456 | ret = nlm_lck_blocked; |
1da177e4 LT |
457 | |
458 | /* Append to list of blocked */ | |
f8120480 | 459 | nlmsvc_insert_block(block, NLM_NEVER); |
15dadef9 | 460 | out: |
89e63ef6 | 461 | mutex_unlock(&file->f_mutex); |
6849c0ca | 462 | nlmsvc_release_block(block); |
15dadef9 AA |
463 | dprintk("lockd: nlmsvc_lock returned %u\n", ret); |
464 | return ret; | |
1da177e4 LT |
465 | } |
466 | ||
467 | /* | |
468 | * Test for presence of a conflicting lock. | |
469 | */ | |
52921e02 | 470 | __be32 |
85f3f1b3 | 471 | nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file, |
8f920d5e JL |
472 | struct nlm_host *host, struct nlm_lock *lock, |
473 | struct nlm_lock *conflock, struct nlm_cookie *cookie) | |
1da177e4 | 474 | { |
5ea0d750 ME |
475 | struct nlm_block *block = NULL; |
476 | int error; | |
477 | __be32 ret; | |
478 | ||
1da177e4 | 479 | dprintk("lockd: nlmsvc_testlock(%s/%ld, ty=%d, %Ld-%Ld)\n", |
225a719f JS |
480 | file->f_file->f_path.dentry->d_inode->i_sb->s_id, |
481 | file->f_file->f_path.dentry->d_inode->i_ino, | |
1da177e4 LT |
482 | lock->fl.fl_type, |
483 | (long long)lock->fl.fl_start, | |
484 | (long long)lock->fl.fl_end); | |
485 | ||
5ea0d750 ME |
486 | /* Get existing block (in case client is busy-waiting) */ |
487 | block = nlmsvc_lookup_block(file, lock); | |
488 | ||
489 | if (block == NULL) { | |
490 | struct file_lock *conf = kzalloc(sizeof(*conf), GFP_KERNEL); | |
491 | ||
492 | if (conf == NULL) | |
493 | return nlm_granted; | |
255129d1 | 494 | block = nlmsvc_create_block(rqstp, host, file, lock, cookie); |
5ea0d750 ME |
495 | if (block == NULL) { |
496 | kfree(conf); | |
497 | return nlm_granted; | |
498 | } | |
499 | block->b_fl = conf; | |
500 | } | |
501 | if (block->b_flags & B_QUEUED) { | |
502 | dprintk("lockd: nlmsvc_testlock deferred block %p flags %d fl %p\n", | |
503 | block, block->b_flags, block->b_fl); | |
504 | if (block->b_flags & B_TIMED_OUT) { | |
505 | nlmsvc_unlink_block(block); | |
29dbf546 OD |
506 | ret = nlm_lck_denied; |
507 | goto out; | |
5ea0d750 ME |
508 | } |
509 | if (block->b_flags & B_GOT_CALLBACK) { | |
54ca95eb | 510 | nlmsvc_unlink_block(block); |
5ea0d750 ME |
511 | if (block->b_fl != NULL |
512 | && block->b_fl->fl_type != F_UNLCK) { | |
513 | lock->fl = *block->b_fl; | |
514 | goto conf_lock; | |
29dbf546 | 515 | } else { |
29dbf546 OD |
516 | ret = nlm_granted; |
517 | goto out; | |
5ea0d750 ME |
518 | } |
519 | } | |
29dbf546 OD |
520 | ret = nlm_drop_reply; |
521 | goto out; | |
1da177e4 LT |
522 | } |
523 | ||
b2b50289 BF |
524 | if (locks_in_grace()) { |
525 | ret = nlm_lck_denied_grace_period; | |
526 | goto out; | |
527 | } | |
5ea0d750 | 528 | error = vfs_test_lock(file->f_file, &lock->fl); |
bde74e4b | 529 | if (error == FILE_LOCK_DEFERRED) { |
29dbf546 OD |
530 | ret = nlmsvc_defer_lock_rqst(rqstp, block); |
531 | goto out; | |
532 | } | |
5ea0d750 ME |
533 | if (error) { |
534 | ret = nlm_lck_denied_nolocks; | |
535 | goto out; | |
536 | } | |
537 | if (lock->fl.fl_type == F_UNLCK) { | |
538 | ret = nlm_granted; | |
539 | goto out; | |
540 | } | |
541 | ||
542 | conf_lock: | |
543 | dprintk("lockd: conflicting lock(ty=%d, %Ld-%Ld)\n", | |
544 | lock->fl.fl_type, (long long)lock->fl.fl_start, | |
545 | (long long)lock->fl.fl_end); | |
546 | conflock->caller = "somehost"; /* FIXME */ | |
547 | conflock->len = strlen(conflock->caller); | |
548 | conflock->oh.len = 0; /* don't return OH info */ | |
549 | conflock->svid = lock->fl.fl_pid; | |
550 | conflock->fl.fl_type = lock->fl.fl_type; | |
551 | conflock->fl.fl_start = lock->fl.fl_start; | |
552 | conflock->fl.fl_end = lock->fl.fl_end; | |
553 | ret = nlm_lck_denied; | |
554 | out: | |
555 | if (block) | |
556 | nlmsvc_release_block(block); | |
557 | return ret; | |
1da177e4 LT |
558 | } |
559 | ||
560 | /* | |
561 | * Remove a lock. | |
562 | * This implies a CANCEL call: We send a GRANT_MSG, the client replies | |
563 | * with a GRANT_RES call which gets lost, and calls UNLOCK immediately | |
564 | * afterwards. In this case the block will still be there, and hence | |
565 | * must be removed. | |
566 | */ | |
52921e02 | 567 | __be32 |
1da177e4 LT |
568 | nlmsvc_unlock(struct nlm_file *file, struct nlm_lock *lock) |
569 | { | |
570 | int error; | |
571 | ||
572 | dprintk("lockd: nlmsvc_unlock(%s/%ld, pi=%d, %Ld-%Ld)\n", | |
225a719f JS |
573 | file->f_file->f_path.dentry->d_inode->i_sb->s_id, |
574 | file->f_file->f_path.dentry->d_inode->i_ino, | |
1da177e4 LT |
575 | lock->fl.fl_pid, |
576 | (long long)lock->fl.fl_start, | |
577 | (long long)lock->fl.fl_end); | |
578 | ||
579 | /* First, cancel any lock that might be there */ | |
580 | nlmsvc_cancel_blocked(file, lock); | |
581 | ||
582 | lock->fl.fl_type = F_UNLCK; | |
1a8322b2 | 583 | error = vfs_lock_file(file->f_file, F_SETLK, &lock->fl, NULL); |
1da177e4 LT |
584 | |
585 | return (error < 0)? nlm_lck_denied_nolocks : nlm_granted; | |
586 | } | |
587 | ||
588 | /* | |
589 | * Cancel a previously blocked request. | |
590 | * | |
591 | * A cancel request always overrides any grant that may currently | |
592 | * be in progress. | |
593 | * The calling procedure must check whether the file can be closed. | |
594 | */ | |
52921e02 | 595 | __be32 |
1da177e4 LT |
596 | nlmsvc_cancel_blocked(struct nlm_file *file, struct nlm_lock *lock) |
597 | { | |
598 | struct nlm_block *block; | |
64a318ee | 599 | int status = 0; |
1da177e4 LT |
600 | |
601 | dprintk("lockd: nlmsvc_cancel(%s/%ld, pi=%d, %Ld-%Ld)\n", | |
225a719f JS |
602 | file->f_file->f_path.dentry->d_inode->i_sb->s_id, |
603 | file->f_file->f_path.dentry->d_inode->i_ino, | |
1da177e4 LT |
604 | lock->fl.fl_pid, |
605 | (long long)lock->fl.fl_start, | |
606 | (long long)lock->fl.fl_end); | |
607 | ||
b2b50289 BF |
608 | if (locks_in_grace()) |
609 | return nlm_lck_denied_grace_period; | |
610 | ||
89e63ef6 | 611 | mutex_lock(&file->f_mutex); |
d9f6eb75 | 612 | block = nlmsvc_lookup_block(file, lock); |
89e63ef6 | 613 | mutex_unlock(&file->f_mutex); |
d9f6eb75 | 614 | if (block != NULL) { |
1a8322b2 ME |
615 | vfs_cancel_lock(block->b_file->f_file, |
616 | &block->b_call->a_args.lock.fl); | |
6849c0ca TM |
617 | status = nlmsvc_unlink_block(block); |
618 | nlmsvc_release_block(block); | |
619 | } | |
64a318ee | 620 | return status ? nlm_lck_denied : nlm_granted; |
1da177e4 LT |
621 | } |
622 | ||
0e4ac9d9 ME |
623 | /* |
624 | * This is a callback from the filesystem for VFS file lock requests. | |
625 | * It will be used if fl_grant is defined and the filesystem can not | |
626 | * respond to the request immediately. | |
627 | * For GETLK request it will copy the reply to the nlm_block. | |
628 | * For SETLK or SETLKW request it will get the local posix lock. | |
629 | * In all cases it will move the block to the head of nlm_blocked q where | |
630 | * nlmsvc_retry_blocked() can send back a reply for SETLKW or revisit the | |
631 | * deferred rpc for GETLK and SETLK. | |
632 | */ | |
633 | static void | |
634 | nlmsvc_update_deferred_block(struct nlm_block *block, struct file_lock *conf, | |
635 | int result) | |
636 | { | |
637 | block->b_flags |= B_GOT_CALLBACK; | |
638 | if (result == 0) | |
639 | block->b_granted = 1; | |
640 | else | |
641 | block->b_flags |= B_TIMED_OUT; | |
642 | if (conf) { | |
0e4ac9d9 | 643 | if (block->b_fl) |
1a747ee0 | 644 | __locks_copy_lock(block->b_fl, conf); |
0e4ac9d9 ME |
645 | } |
646 | } | |
647 | ||
648 | static int nlmsvc_grant_deferred(struct file_lock *fl, struct file_lock *conf, | |
649 | int result) | |
650 | { | |
651 | struct nlm_block *block; | |
652 | int rc = -ENOENT; | |
653 | ||
654 | lock_kernel(); | |
655 | list_for_each_entry(block, &nlm_blocked, b_list) { | |
656 | if (nlm_compare_locks(&block->b_call->a_args.lock.fl, fl)) { | |
657 | dprintk("lockd: nlmsvc_notify_blocked block %p flags %d\n", | |
658 | block, block->b_flags); | |
659 | if (block->b_flags & B_QUEUED) { | |
660 | if (block->b_flags & B_TIMED_OUT) { | |
661 | rc = -ENOLCK; | |
662 | break; | |
663 | } | |
664 | nlmsvc_update_deferred_block(block, conf, result); | |
665 | } else if (result == 0) | |
666 | block->b_granted = 1; | |
667 | ||
668 | nlmsvc_insert_block(block, 0); | |
669 | svc_wake_up(block->b_daemon); | |
670 | rc = 0; | |
671 | break; | |
672 | } | |
673 | } | |
674 | unlock_kernel(); | |
675 | if (rc == -ENOENT) | |
676 | printk(KERN_WARNING "lockd: grant for unknown block\n"); | |
677 | return rc; | |
678 | } | |
679 | ||
1da177e4 LT |
680 | /* |
681 | * Unblock a blocked lock request. This is a callback invoked from the | |
682 | * VFS layer when a lock on which we blocked is removed. | |
683 | * | |
684 | * This function doesn't grant the blocked lock instantly, but rather moves | |
685 | * the block to the head of nlm_blocked where it can be picked up by lockd. | |
686 | */ | |
687 | static void | |
688 | nlmsvc_notify_blocked(struct file_lock *fl) | |
689 | { | |
68a2d76c | 690 | struct nlm_block *block; |
1da177e4 LT |
691 | |
692 | dprintk("lockd: VFS unblock notification for block %p\n", fl); | |
68a2d76c | 693 | list_for_each_entry(block, &nlm_blocked, b_list) { |
92737230 | 694 | if (nlm_compare_locks(&block->b_call->a_args.lock.fl, fl)) { |
1da177e4 LT |
695 | nlmsvc_insert_block(block, 0); |
696 | svc_wake_up(block->b_daemon); | |
697 | return; | |
698 | } | |
699 | } | |
700 | ||
701 | printk(KERN_WARNING "lockd: notification for unknown block!\n"); | |
702 | } | |
703 | ||
704 | static int nlmsvc_same_owner(struct file_lock *fl1, struct file_lock *fl2) | |
705 | { | |
706 | return fl1->fl_owner == fl2->fl_owner && fl1->fl_pid == fl2->fl_pid; | |
707 | } | |
708 | ||
7b021967 | 709 | const struct lock_manager_operations nlmsvc_lock_operations = { |
1da177e4 LT |
710 | .fl_compare_owner = nlmsvc_same_owner, |
711 | .fl_notify = nlmsvc_notify_blocked, | |
0e4ac9d9 | 712 | .fl_grant = nlmsvc_grant_deferred, |
1da177e4 LT |
713 | }; |
714 | ||
715 | /* | |
716 | * Try to claim a lock that was previously blocked. | |
717 | * | |
718 | * Note that we use both the RPC_GRANTED_MSG call _and_ an async | |
719 | * RPC thread when notifying the client. This seems like overkill... | |
720 | * Here's why: | |
721 | * - we don't want to use a synchronous RPC thread, otherwise | |
722 | * we might find ourselves hanging on a dead portmapper. | |
723 | * - Some lockd implementations (e.g. HP) don't react to | |
724 | * RPC_GRANTED calls; they seem to insist on RPC_GRANTED_MSG calls. | |
725 | */ | |
726 | static void | |
727 | nlmsvc_grant_blocked(struct nlm_block *block) | |
728 | { | |
729 | struct nlm_file *file = block->b_file; | |
92737230 | 730 | struct nlm_lock *lock = &block->b_call->a_args.lock; |
1da177e4 LT |
731 | int error; |
732 | ||
733 | dprintk("lockd: grant blocked lock %p\n", block); | |
734 | ||
0e4ac9d9 ME |
735 | kref_get(&block->b_count); |
736 | ||
1da177e4 | 737 | /* Unlink block request from list */ |
6849c0ca | 738 | nlmsvc_unlink_block(block); |
1da177e4 LT |
739 | |
740 | /* If b_granted is true this means we've been here before. | |
741 | * Just retry the grant callback, possibly refreshing the RPC | |
742 | * binding */ | |
743 | if (block->b_granted) { | |
744 | nlm_rebind_host(block->b_host); | |
745 | goto callback; | |
746 | } | |
747 | ||
748 | /* Try the lock operation again */ | |
09c7938c | 749 | lock->fl.fl_flags |= FL_SLEEP; |
1a8322b2 | 750 | error = vfs_lock_file(file->f_file, F_SETLK, &lock->fl, NULL); |
09c7938c TM |
751 | lock->fl.fl_flags &= ~FL_SLEEP; |
752 | ||
5de0e502 AA |
753 | switch (error) { |
754 | case 0: | |
755 | break; | |
bde74e4b | 756 | case FILE_LOCK_DEFERRED: |
1a8322b2 | 757 | dprintk("lockd: lock still blocked error %d\n", error); |
1da177e4 | 758 | nlmsvc_insert_block(block, NLM_NEVER); |
0e4ac9d9 | 759 | nlmsvc_release_block(block); |
d9f6eb75 | 760 | return; |
5de0e502 | 761 | default: |
1da177e4 | 762 | printk(KERN_WARNING "lockd: unexpected error %d in %s!\n", |
8e24eea7 | 763 | -error, __func__); |
1da177e4 | 764 | nlmsvc_insert_block(block, 10 * HZ); |
0e4ac9d9 | 765 | nlmsvc_release_block(block); |
d9f6eb75 | 766 | return; |
1da177e4 LT |
767 | } |
768 | ||
769 | callback: | |
770 | /* Lock was granted by VFS. */ | |
771 | dprintk("lockd: GRANTing blocked lock.\n"); | |
772 | block->b_granted = 1; | |
1da177e4 | 773 | |
9706501e JL |
774 | /* keep block on the list, but don't reattempt until the RPC |
775 | * completes or the submission fails | |
776 | */ | |
777 | nlmsvc_insert_block(block, NLM_NEVER); | |
1da177e4 | 778 | |
9706501e JL |
779 | /* Call the client -- use a soft RPC task since nlmsvc_retry_blocked |
780 | * will queue up a new one if this one times out | |
781 | */ | |
782 | error = nlm_async_call(block->b_call, NLMPROC_GRANTED_MSG, | |
783 | &nlmsvc_grant_ops); | |
784 | ||
785 | /* RPC submission failed, wait a bit and retry */ | |
786 | if (error < 0) | |
787 | nlmsvc_insert_block(block, 10 * HZ); | |
1da177e4 LT |
788 | } |
789 | ||
790 | /* | |
791 | * This is the callback from the RPC layer when the NLM_GRANTED_MSG | |
792 | * RPC call has succeeded or timed out. | |
793 | * Like all RPC callbacks, it is invoked by the rpciod process, so it | |
794 | * better not sleep. Therefore, we put the blocked lock on the nlm_blocked | |
795 | * chain once more in order to have it removed by lockd itself (which can | |
796 | * then sleep on the file semaphore without disrupting e.g. the nfs client). | |
797 | */ | |
963d8fe5 | 798 | static void nlmsvc_grant_callback(struct rpc_task *task, void *data) |
1da177e4 | 799 | { |
963d8fe5 | 800 | struct nlm_rqst *call = data; |
92737230 | 801 | struct nlm_block *block = call->a_block; |
1da177e4 | 802 | unsigned long timeout; |
1da177e4 LT |
803 | |
804 | dprintk("lockd: GRANT_MSG RPC callback\n"); | |
1da177e4 | 805 | |
a86dc496 | 806 | lock_kernel(); |
c64e80d5 JL |
807 | /* if the block is not on a list at this point then it has |
808 | * been invalidated. Don't try to requeue it. | |
809 | * | |
810 | * FIXME: it's possible that the block is removed from the list | |
811 | * after this check but before the nlmsvc_insert_block. In that | |
812 | * case it will be added back. Perhaps we need better locking | |
813 | * for nlm_blocked? | |
814 | */ | |
815 | if (list_empty(&block->b_list)) | |
a86dc496 | 816 | goto out; |
c64e80d5 | 817 | |
1da177e4 LT |
818 | /* Technically, we should down the file semaphore here. Since we |
819 | * move the block towards the head of the queue only, no harm | |
820 | * can be done, though. */ | |
821 | if (task->tk_status < 0) { | |
822 | /* RPC error: Re-insert for retransmission */ | |
823 | timeout = 10 * HZ; | |
1da177e4 LT |
824 | } else { |
825 | /* Call was successful, now wait for client callback */ | |
826 | timeout = 60 * HZ; | |
827 | } | |
828 | nlmsvc_insert_block(block, timeout); | |
829 | svc_wake_up(block->b_daemon); | |
a86dc496 TM |
830 | out: |
831 | unlock_kernel(); | |
5e1abf8c TM |
832 | } |
833 | ||
ec535ce1 | 834 | static void nlmsvc_grant_release(void *data) |
5e1abf8c | 835 | { |
6041b791 TM |
836 | struct nlm_rqst *call = data; |
837 | ||
a86dc496 | 838 | lock_kernel(); |
6041b791 | 839 | nlmsvc_release_block(call->a_block); |
a86dc496 | 840 | unlock_kernel(); |
1da177e4 LT |
841 | } |
842 | ||
963d8fe5 TM |
843 | static const struct rpc_call_ops nlmsvc_grant_ops = { |
844 | .rpc_call_done = nlmsvc_grant_callback, | |
5e1abf8c | 845 | .rpc_release = nlmsvc_grant_release, |
963d8fe5 TM |
846 | }; |
847 | ||
1da177e4 LT |
848 | /* |
849 | * We received a GRANT_RES callback. Try to find the corresponding | |
850 | * block. | |
851 | */ | |
852 | void | |
e8c5c045 | 853 | nlmsvc_grant_reply(struct nlm_cookie *cookie, __be32 status) |
1da177e4 LT |
854 | { |
855 | struct nlm_block *block; | |
1da177e4 | 856 | |
39be4502 OK |
857 | dprintk("grant_reply: looking for cookie %x, s=%d \n", |
858 | *(unsigned int *)(cookie->data), status); | |
859 | if (!(block = nlmsvc_find_block(cookie))) | |
1da177e4 | 860 | return; |
1da177e4 | 861 | |
f232142c | 862 | if (block) { |
e8c5c045 | 863 | if (status == nlm_lck_denied_grace_period) { |
1da177e4 LT |
864 | /* Try again in a couple of seconds */ |
865 | nlmsvc_insert_block(block, 10 * HZ); | |
1da177e4 LT |
866 | } else { |
867 | /* Lock is now held by client, or has been rejected. | |
868 | * In both cases, the block should be removed. */ | |
6849c0ca | 869 | nlmsvc_unlink_block(block); |
1da177e4 LT |
870 | } |
871 | } | |
6849c0ca | 872 | nlmsvc_release_block(block); |
1da177e4 LT |
873 | } |
874 | ||
0e4ac9d9 ME |
875 | /* Helper function to handle retry of a deferred block. |
876 | * If it is a blocking lock, call grant_blocked. | |
877 | * For a non-blocking lock or test lock, revisit the request. | |
878 | */ | |
879 | static void | |
880 | retry_deferred_block(struct nlm_block *block) | |
881 | { | |
882 | if (!(block->b_flags & B_GOT_CALLBACK)) | |
883 | block->b_flags |= B_TIMED_OUT; | |
884 | nlmsvc_insert_block(block, NLM_TIMEOUT); | |
885 | dprintk("revisit block %p flags %d\n", block, block->b_flags); | |
886 | if (block->b_deferred_req) { | |
887 | block->b_deferred_req->revisit(block->b_deferred_req, 0); | |
888 | block->b_deferred_req = NULL; | |
889 | } | |
890 | } | |
891 | ||
1da177e4 LT |
892 | /* |
893 | * Retry all blocked locks that have been notified. This is where lockd | |
894 | * picks up locks that can be granted, or grant notifications that must | |
895 | * be retransmitted. | |
896 | */ | |
897 | unsigned long | |
898 | nlmsvc_retry_blocked(void) | |
899 | { | |
68a2d76c OK |
900 | unsigned long timeout = MAX_SCHEDULE_TIMEOUT; |
901 | struct nlm_block *block; | |
902 | ||
d751a7cd | 903 | while (!list_empty(&nlm_blocked) && !kthread_should_stop()) { |
68a2d76c | 904 | block = list_entry(nlm_blocked.next, struct nlm_block, b_list); |
1da177e4 | 905 | |
1da177e4 LT |
906 | if (block->b_when == NLM_NEVER) |
907 | break; | |
6d7bbbba | 908 | if (time_after(block->b_when, jiffies)) { |
68a2d76c | 909 | timeout = block->b_when - jiffies; |
1da177e4 | 910 | break; |
68a2d76c OK |
911 | } |
912 | ||
f3d43c76 BF |
913 | dprintk("nlmsvc_retry_blocked(%p, when=%ld)\n", |
914 | block, block->b_when); | |
0e4ac9d9 ME |
915 | if (block->b_flags & B_QUEUED) { |
916 | dprintk("nlmsvc_retry_blocked delete block (%p, granted=%d, flags=%d)\n", | |
917 | block, block->b_granted, block->b_flags); | |
918 | retry_deferred_block(block); | |
919 | } else | |
920 | nlmsvc_grant_blocked(block); | |
1da177e4 LT |
921 | } |
922 | ||
68a2d76c | 923 | return timeout; |
1da177e4 | 924 | } |