diff options
Diffstat (limited to 'fs/lockd/svclock.c')
-rw-r--r-- | fs/lockd/svclock.c | 349 |
1 files changed, 194 insertions, 155 deletions
diff --git a/fs/lockd/svclock.c b/fs/lockd/svclock.c index 9cfced65d4a2..d2b66bad7d50 100644 --- a/fs/lockd/svclock.c +++ b/fs/lockd/svclock.c | |||
@@ -39,9 +39,12 @@ | |||
39 | #define nlm_deadlock nlm_lck_denied | 39 | #define nlm_deadlock nlm_lck_denied |
40 | #endif | 40 | #endif |
41 | 41 | ||
42 | static void nlmsvc_release_block(struct nlm_block *block); | ||
42 | static void nlmsvc_insert_block(struct nlm_block *block, unsigned long); | 43 | static void nlmsvc_insert_block(struct nlm_block *block, unsigned long); |
43 | static int nlmsvc_remove_block(struct nlm_block *block); | 44 | static int nlmsvc_remove_block(struct nlm_block *block); |
44 | 45 | ||
46 | static int nlmsvc_setgrantargs(struct nlm_rqst *call, struct nlm_lock *lock); | ||
47 | static void nlmsvc_freegrantargs(struct nlm_rqst *call); | ||
45 | static const struct rpc_call_ops nlmsvc_grant_ops; | 48 | static const struct rpc_call_ops nlmsvc_grant_ops; |
46 | 49 | ||
47 | /* | 50 | /* |
@@ -58,6 +61,7 @@ nlmsvc_insert_block(struct nlm_block *block, unsigned long when) | |||
58 | struct nlm_block **bp, *b; | 61 | struct nlm_block **bp, *b; |
59 | 62 | ||
60 | dprintk("lockd: nlmsvc_insert_block(%p, %ld)\n", block, when); | 63 | dprintk("lockd: nlmsvc_insert_block(%p, %ld)\n", block, when); |
64 | kref_get(&block->b_count); | ||
61 | if (block->b_queued) | 65 | if (block->b_queued) |
62 | nlmsvc_remove_block(block); | 66 | nlmsvc_remove_block(block); |
63 | bp = &nlm_blocked; | 67 | bp = &nlm_blocked; |
@@ -90,6 +94,7 @@ nlmsvc_remove_block(struct nlm_block *block) | |||
90 | if (b == block) { | 94 | if (b == block) { |
91 | *bp = block->b_next; | 95 | *bp = block->b_next; |
92 | block->b_queued = 0; | 96 | block->b_queued = 0; |
97 | nlmsvc_release_block(block); | ||
93 | return 1; | 98 | return 1; |
94 | } | 99 | } |
95 | } | 100 | } |
@@ -98,11 +103,10 @@ nlmsvc_remove_block(struct nlm_block *block) | |||
98 | } | 103 | } |
99 | 104 | ||
100 | /* | 105 | /* |
101 | * Find a block for a given lock and optionally remove it from | 106 | * Find a block for a given lock |
102 | * the list. | ||
103 | */ | 107 | */ |
104 | static struct nlm_block * | 108 | static struct nlm_block * |
105 | nlmsvc_lookup_block(struct nlm_file *file, struct nlm_lock *lock, int remove) | 109 | nlmsvc_lookup_block(struct nlm_file *file, struct nlm_lock *lock) |
106 | { | 110 | { |
107 | struct nlm_block **head, *block; | 111 | struct nlm_block **head, *block; |
108 | struct file_lock *fl; | 112 | struct file_lock *fl; |
@@ -112,17 +116,14 @@ nlmsvc_lookup_block(struct nlm_file *file, struct nlm_lock *lock, int remove) | |||
112 | (long long)lock->fl.fl_start, | 116 | (long long)lock->fl.fl_start, |
113 | (long long)lock->fl.fl_end, lock->fl.fl_type); | 117 | (long long)lock->fl.fl_end, lock->fl.fl_type); |
114 | for (head = &nlm_blocked; (block = *head) != 0; head = &block->b_next) { | 118 | for (head = &nlm_blocked; (block = *head) != 0; head = &block->b_next) { |
115 | fl = &block->b_call.a_args.lock.fl; | 119 | fl = &block->b_call->a_args.lock.fl; |
116 | dprintk("lockd: check f=%p pd=%d %Ld-%Ld ty=%d cookie=%s\n", | 120 | dprintk("lockd: check f=%p pd=%d %Ld-%Ld ty=%d cookie=%s\n", |
117 | block->b_file, fl->fl_pid, | 121 | block->b_file, fl->fl_pid, |
118 | (long long)fl->fl_start, | 122 | (long long)fl->fl_start, |
119 | (long long)fl->fl_end, fl->fl_type, | 123 | (long long)fl->fl_end, fl->fl_type, |
120 | nlmdbg_cookie2a(&block->b_call.a_args.cookie)); | 124 | nlmdbg_cookie2a(&block->b_call->a_args.cookie)); |
121 | if (block->b_file == file && nlm_compare_locks(fl, &lock->fl)) { | 125 | if (block->b_file == file && nlm_compare_locks(fl, &lock->fl)) { |
122 | if (remove) { | 126 | kref_get(&block->b_count); |
123 | *head = block->b_next; | ||
124 | block->b_queued = 0; | ||
125 | } | ||
126 | return block; | 127 | return block; |
127 | } | 128 | } |
128 | } | 129 | } |
@@ -150,11 +151,13 @@ nlmsvc_find_block(struct nlm_cookie *cookie, struct sockaddr_in *sin) | |||
150 | for (block = nlm_blocked; block; block = block->b_next) { | 151 | for (block = nlm_blocked; block; block = block->b_next) { |
151 | dprintk("cookie: head of blocked queue %p, block %p\n", | 152 | dprintk("cookie: head of blocked queue %p, block %p\n", |
152 | nlm_blocked, block); | 153 | nlm_blocked, block); |
153 | if (nlm_cookie_match(&block->b_call.a_args.cookie,cookie) | 154 | if (nlm_cookie_match(&block->b_call->a_args.cookie,cookie) |
154 | && nlm_cmp_addr(sin, &block->b_host->h_addr)) | 155 | && nlm_cmp_addr(sin, &block->b_host->h_addr)) |
155 | break; | 156 | break; |
156 | } | 157 | } |
157 | 158 | ||
159 | if (block != NULL) | ||
160 | kref_get(&block->b_count); | ||
158 | return block; | 161 | return block; |
159 | } | 162 | } |
160 | 163 | ||
@@ -174,27 +177,30 @@ nlmsvc_create_block(struct svc_rqst *rqstp, struct nlm_file *file, | |||
174 | { | 177 | { |
175 | struct nlm_block *block; | 178 | struct nlm_block *block; |
176 | struct nlm_host *host; | 179 | struct nlm_host *host; |
177 | struct nlm_rqst *call; | 180 | struct nlm_rqst *call = NULL; |
178 | 181 | ||
179 | /* Create host handle for callback */ | 182 | /* Create host handle for callback */ |
180 | host = nlmclnt_lookup_host(&rqstp->rq_addr, | 183 | host = nlmsvc_lookup_host(rqstp); |
181 | rqstp->rq_prot, rqstp->rq_vers); | ||
182 | if (host == NULL) | 184 | if (host == NULL) |
183 | return NULL; | 185 | return NULL; |
184 | 186 | ||
187 | call = nlm_alloc_call(host); | ||
188 | if (call == NULL) | ||
189 | return NULL; | ||
190 | |||
185 | /* Allocate memory for block, and initialize arguments */ | 191 | /* Allocate memory for block, and initialize arguments */ |
186 | if (!(block = (struct nlm_block *) kmalloc(sizeof(*block), GFP_KERNEL))) | 192 | block = kzalloc(sizeof(*block), GFP_KERNEL); |
193 | if (block == NULL) | ||
187 | goto failed; | 194 | goto failed; |
188 | memset(block, 0, sizeof(*block)); | 195 | kref_init(&block->b_count); |
189 | locks_init_lock(&block->b_call.a_args.lock.fl); | ||
190 | locks_init_lock(&block->b_call.a_res.lock.fl); | ||
191 | 196 | ||
192 | if (!nlmclnt_setgrantargs(&block->b_call, lock)) | 197 | if (!nlmsvc_setgrantargs(call, lock)) |
193 | goto failed_free; | 198 | goto failed_free; |
194 | 199 | ||
195 | /* Set notifier function for VFS, and init args */ | 200 | /* Set notifier function for VFS, and init args */ |
196 | block->b_call.a_args.lock.fl.fl_lmops = &nlmsvc_lock_operations; | 201 | call->a_args.lock.fl.fl_flags |= FL_SLEEP; |
197 | block->b_call.a_args.cookie = *cookie; /* see above */ | 202 | call->a_args.lock.fl.fl_lmops = &nlmsvc_lock_operations; |
203 | call->a_args.cookie = *cookie; /* see above */ | ||
198 | 204 | ||
199 | dprintk("lockd: created block %p...\n", block); | 205 | dprintk("lockd: created block %p...\n", block); |
200 | 206 | ||
@@ -202,22 +208,23 @@ nlmsvc_create_block(struct svc_rqst *rqstp, struct nlm_file *file, | |||
202 | block->b_daemon = rqstp->rq_server; | 208 | block->b_daemon = rqstp->rq_server; |
203 | block->b_host = host; | 209 | block->b_host = host; |
204 | block->b_file = file; | 210 | block->b_file = file; |
211 | file->f_count++; | ||
205 | 212 | ||
206 | /* Add to file's list of blocks */ | 213 | /* Add to file's list of blocks */ |
207 | block->b_fnext = file->f_blocks; | 214 | block->b_fnext = file->f_blocks; |
208 | file->f_blocks = block; | 215 | file->f_blocks = block; |
209 | 216 | ||
210 | /* Set up RPC arguments for callback */ | 217 | /* Set up RPC arguments for callback */ |
211 | call = &block->b_call; | 218 | block->b_call = call; |
212 | call->a_host = host; | ||
213 | call->a_flags = RPC_TASK_ASYNC; | 219 | call->a_flags = RPC_TASK_ASYNC; |
220 | call->a_block = block; | ||
214 | 221 | ||
215 | return block; | 222 | return block; |
216 | 223 | ||
217 | failed_free: | 224 | failed_free: |
218 | kfree(block); | 225 | kfree(block); |
219 | failed: | 226 | failed: |
220 | nlm_release_host(host); | 227 | nlm_release_call(call); |
221 | return NULL; | 228 | return NULL; |
222 | } | 229 | } |
223 | 230 | ||
@@ -227,29 +234,26 @@ failed: | |||
227 | * It is the caller's responsibility to check whether the file | 234 | * It is the caller's responsibility to check whether the file |
228 | * can be closed hereafter. | 235 | * can be closed hereafter. |
229 | */ | 236 | */ |
230 | static int | 237 | static int nlmsvc_unlink_block(struct nlm_block *block) |
231 | nlmsvc_delete_block(struct nlm_block *block, int unlock) | ||
232 | { | 238 | { |
233 | struct file_lock *fl = &block->b_call.a_args.lock.fl; | 239 | int status; |
234 | struct nlm_file *file = block->b_file; | 240 | dprintk("lockd: unlinking block %p...\n", block); |
235 | struct nlm_block **bp; | ||
236 | int status = 0; | ||
237 | |||
238 | dprintk("lockd: deleting block %p...\n", block); | ||
239 | 241 | ||
240 | /* Remove block from list */ | 242 | /* Remove block from list */ |
243 | status = posix_unblock_lock(block->b_file->f_file, &block->b_call->a_args.lock.fl); | ||
241 | nlmsvc_remove_block(block); | 244 | nlmsvc_remove_block(block); |
242 | if (unlock) | 245 | return status; |
243 | status = posix_unblock_lock(file->f_file, fl); | 246 | } |
244 | 247 | ||
245 | /* If the block is in the middle of a GRANT callback, | 248 | static void nlmsvc_free_block(struct kref *kref) |
246 | * don't kill it yet. */ | 249 | { |
247 | if (block->b_incall) { | 250 | struct nlm_block *block = container_of(kref, struct nlm_block, b_count); |
248 | nlmsvc_insert_block(block, NLM_NEVER); | 251 | struct nlm_file *file = block->b_file; |
249 | block->b_done = 1; | 252 | struct nlm_block **bp; |
250 | return status; | ||
251 | } | ||
252 | 253 | ||
254 | dprintk("lockd: freeing block %p...\n", block); | ||
255 | |||
256 | down(&file->f_sema); | ||
253 | /* Remove block from file's list of blocks */ | 257 | /* Remove block from file's list of blocks */ |
254 | for (bp = &file->f_blocks; *bp; bp = &(*bp)->b_fnext) { | 258 | for (bp = &file->f_blocks; *bp; bp = &(*bp)->b_fnext) { |
255 | if (*bp == block) { | 259 | if (*bp == block) { |
@@ -257,36 +261,93 @@ nlmsvc_delete_block(struct nlm_block *block, int unlock) | |||
257 | break; | 261 | break; |
258 | } | 262 | } |
259 | } | 263 | } |
264 | up(&file->f_sema); | ||
260 | 265 | ||
261 | if (block->b_host) | 266 | nlmsvc_freegrantargs(block->b_call); |
262 | nlm_release_host(block->b_host); | 267 | nlm_release_call(block->b_call); |
263 | nlmclnt_freegrantargs(&block->b_call); | 268 | nlm_release_file(block->b_file); |
264 | kfree(block); | 269 | kfree(block); |
265 | return status; | 270 | } |
271 | |||
272 | static void nlmsvc_release_block(struct nlm_block *block) | ||
273 | { | ||
274 | if (block != NULL) | ||
275 | kref_put(&block->b_count, nlmsvc_free_block); | ||
276 | } | ||
277 | |||
278 | static void nlmsvc_act_mark(struct nlm_host *host, struct nlm_file *file) | ||
279 | { | ||
280 | struct nlm_block *block; | ||
281 | |||
282 | down(&file->f_sema); | ||
283 | for (block = file->f_blocks; block != NULL; block = block->b_fnext) | ||
284 | block->b_host->h_inuse = 1; | ||
285 | up(&file->f_sema); | ||
286 | } | ||
287 | |||
288 | static void nlmsvc_act_unlock(struct nlm_host *host, struct nlm_file *file) | ||
289 | { | ||
290 | struct nlm_block *block; | ||
291 | |||
292 | restart: | ||
293 | down(&file->f_sema); | ||
294 | for (block = file->f_blocks; block != NULL; block = block->b_fnext) { | ||
295 | if (host != NULL && host != block->b_host) | ||
296 | continue; | ||
297 | if (!block->b_queued) | ||
298 | continue; | ||
299 | kref_get(&block->b_count); | ||
300 | up(&file->f_sema); | ||
301 | nlmsvc_unlink_block(block); | ||
302 | nlmsvc_release_block(block); | ||
303 | goto restart; | ||
304 | } | ||
305 | up(&file->f_sema); | ||
266 | } | 306 | } |
267 | 307 | ||
268 | /* | 308 | /* |
269 | * Loop over all blocks and perform the action specified. | 309 | * Loop over all blocks and perform the action specified. |
270 | * (NLM_ACT_CHECK handled by nlmsvc_inspect_file). | 310 | * (NLM_ACT_CHECK handled by nlmsvc_inspect_file). |
271 | */ | 311 | */ |
272 | int | 312 | void |
273 | nlmsvc_traverse_blocks(struct nlm_host *host, struct nlm_file *file, int action) | 313 | nlmsvc_traverse_blocks(struct nlm_host *host, struct nlm_file *file, int action) |
274 | { | 314 | { |
275 | struct nlm_block *block, *next; | 315 | if (action == NLM_ACT_MARK) |
276 | /* XXX: Will everything get cleaned up if we don't unlock here? */ | 316 | nlmsvc_act_mark(host, file); |
317 | else | ||
318 | nlmsvc_act_unlock(host, file); | ||
319 | } | ||
277 | 320 | ||
278 | down(&file->f_sema); | 321 | /* |
279 | for (block = file->f_blocks; block; block = next) { | 322 | * Initialize arguments for GRANTED call. The nlm_rqst structure |
280 | next = block->b_fnext; | 323 | * has been cleared already. |
281 | if (action == NLM_ACT_MARK) | 324 | */ |
282 | block->b_host->h_inuse = 1; | 325 | static int nlmsvc_setgrantargs(struct nlm_rqst *call, struct nlm_lock *lock) |
283 | else if (action == NLM_ACT_UNLOCK) { | 326 | { |
284 | if (host == NULL || host == block->b_host) | 327 | locks_copy_lock(&call->a_args.lock.fl, &lock->fl); |
285 | nlmsvc_delete_block(block, 1); | 328 | memcpy(&call->a_args.lock.fh, &lock->fh, sizeof(call->a_args.lock.fh)); |
286 | } | 329 | call->a_args.lock.caller = system_utsname.nodename; |
330 | call->a_args.lock.oh.len = lock->oh.len; | ||
331 | |||
332 | /* set default data area */ | ||
333 | call->a_args.lock.oh.data = call->a_owner; | ||
334 | call->a_args.lock.svid = lock->fl.fl_pid; | ||
335 | |||
336 | if (lock->oh.len > NLMCLNT_OHSIZE) { | ||
337 | void *data = kmalloc(lock->oh.len, GFP_KERNEL); | ||
338 | if (!data) | ||
339 | return 0; | ||
340 | call->a_args.lock.oh.data = (u8 *) data; | ||
287 | } | 341 | } |
288 | up(&file->f_sema); | 342 | |
289 | return 0; | 343 | memcpy(call->a_args.lock.oh.data, lock->oh.data, lock->oh.len); |
344 | return 1; | ||
345 | } | ||
346 | |||
347 | static void nlmsvc_freegrantargs(struct nlm_rqst *call) | ||
348 | { | ||
349 | if (call->a_args.lock.oh.data != call->a_owner) | ||
350 | kfree(call->a_args.lock.oh.data); | ||
290 | } | 351 | } |
291 | 352 | ||
292 | /* | 353 | /* |
@@ -297,9 +358,9 @@ u32 | |||
297 | nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file, | 358 | nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file, |
298 | struct nlm_lock *lock, int wait, struct nlm_cookie *cookie) | 359 | struct nlm_lock *lock, int wait, struct nlm_cookie *cookie) |
299 | { | 360 | { |
300 | struct file_lock *conflock; | 361 | struct nlm_block *block, *newblock = NULL; |
301 | struct nlm_block *block; | ||
302 | int error; | 362 | int error; |
363 | u32 ret; | ||
303 | 364 | ||
304 | dprintk("lockd: nlmsvc_lock(%s/%ld, ty=%d, pi=%d, %Ld-%Ld, bl=%d)\n", | 365 | dprintk("lockd: nlmsvc_lock(%s/%ld, ty=%d, pi=%d, %Ld-%Ld, bl=%d)\n", |
305 | file->f_file->f_dentry->d_inode->i_sb->s_id, | 366 | file->f_file->f_dentry->d_inode->i_sb->s_id, |
@@ -310,69 +371,65 @@ nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file, | |||
310 | wait); | 371 | wait); |
311 | 372 | ||
312 | 373 | ||
313 | /* Get existing block (in case client is busy-waiting) */ | 374 | lock->fl.fl_flags &= ~FL_SLEEP; |
314 | block = nlmsvc_lookup_block(file, lock, 0); | ||
315 | |||
316 | lock->fl.fl_flags |= FL_LOCKD; | ||
317 | |||
318 | again: | 375 | again: |
319 | /* Lock file against concurrent access */ | 376 | /* Lock file against concurrent access */ |
320 | down(&file->f_sema); | 377 | down(&file->f_sema); |
378 | /* Get existing block (in case client is busy-waiting) */ | ||
379 | block = nlmsvc_lookup_block(file, lock); | ||
380 | if (block == NULL) { | ||
381 | if (newblock != NULL) | ||
382 | lock = &newblock->b_call->a_args.lock; | ||
383 | } else | ||
384 | lock = &block->b_call->a_args.lock; | ||
321 | 385 | ||
322 | if (!(conflock = posix_test_lock(file->f_file, &lock->fl))) { | 386 | error = posix_lock_file(file->f_file, &lock->fl); |
323 | error = posix_lock_file(file->f_file, &lock->fl); | 387 | lock->fl.fl_flags &= ~FL_SLEEP; |
324 | 388 | ||
325 | if (block) | 389 | dprintk("lockd: posix_lock_file returned %d\n", error); |
326 | nlmsvc_delete_block(block, 0); | ||
327 | up(&file->f_sema); | ||
328 | 390 | ||
329 | dprintk("lockd: posix_lock_file returned %d\n", -error); | 391 | switch(error) { |
330 | switch(-error) { | ||
331 | case 0: | 392 | case 0: |
332 | return nlm_granted; | 393 | ret = nlm_granted; |
333 | case EDEADLK: | 394 | goto out; |
334 | return nlm_deadlock; | 395 | case -EAGAIN: |
335 | case EAGAIN: | 396 | break; |
336 | return nlm_lck_denied; | 397 | case -EDEADLK: |
398 | ret = nlm_deadlock; | ||
399 | goto out; | ||
337 | default: /* includes ENOLCK */ | 400 | default: /* includes ENOLCK */ |
338 | return nlm_lck_denied_nolocks; | 401 | ret = nlm_lck_denied_nolocks; |
339 | } | 402 | goto out; |
340 | } | 403 | } |
341 | 404 | ||
342 | if (!wait) { | 405 | ret = nlm_lck_denied; |
343 | up(&file->f_sema); | 406 | if (!wait) |
344 | return nlm_lck_denied; | 407 | goto out; |
345 | } | ||
346 | 408 | ||
347 | if (posix_locks_deadlock(&lock->fl, conflock)) { | 409 | ret = nlm_lck_blocked; |
348 | up(&file->f_sema); | 410 | if (block != NULL) |
349 | return nlm_deadlock; | 411 | goto out; |
350 | } | ||
351 | 412 | ||
352 | /* If we don't have a block, create and initialize it. Then | 413 | /* If we don't have a block, create and initialize it. Then |
353 | * retry because we may have slept in kmalloc. */ | 414 | * retry because we may have slept in kmalloc. */ |
354 | /* We have to release f_sema as nlmsvc_create_block may try to | 415 | /* We have to release f_sema as nlmsvc_create_block may try to |
355 | * to claim it while doing host garbage collection */ | 416 | * to claim it while doing host garbage collection */ |
356 | if (block == NULL) { | 417 | if (newblock == NULL) { |
357 | up(&file->f_sema); | 418 | up(&file->f_sema); |
358 | dprintk("lockd: blocking on this lock (allocating).\n"); | 419 | dprintk("lockd: blocking on this lock (allocating).\n"); |
359 | if (!(block = nlmsvc_create_block(rqstp, file, lock, cookie))) | 420 | if (!(newblock = nlmsvc_create_block(rqstp, file, lock, cookie))) |
360 | return nlm_lck_denied_nolocks; | 421 | return nlm_lck_denied_nolocks; |
361 | goto again; | 422 | goto again; |
362 | } | 423 | } |
363 | 424 | ||
364 | /* Append to list of blocked */ | 425 | /* Append to list of blocked */ |
365 | nlmsvc_insert_block(block, NLM_NEVER); | 426 | nlmsvc_insert_block(newblock, NLM_NEVER); |
366 | 427 | out: | |
367 | if (list_empty(&block->b_call.a_args.lock.fl.fl_block)) { | ||
368 | /* Now add block to block list of the conflicting lock | ||
369 | if we haven't done so. */ | ||
370 | dprintk("lockd: blocking on this lock.\n"); | ||
371 | posix_block_lock(conflock, &block->b_call.a_args.lock.fl); | ||
372 | } | ||
373 | |||
374 | up(&file->f_sema); | 428 | up(&file->f_sema); |
375 | return nlm_lck_blocked; | 429 | nlmsvc_release_block(newblock); |
430 | nlmsvc_release_block(block); | ||
431 | dprintk("lockd: nlmsvc_lock returned %u\n", ret); | ||
432 | return ret; | ||
376 | } | 433 | } |
377 | 434 | ||
378 | /* | 435 | /* |
@@ -382,8 +439,6 @@ u32 | |||
382 | nlmsvc_testlock(struct nlm_file *file, struct nlm_lock *lock, | 439 | nlmsvc_testlock(struct nlm_file *file, struct nlm_lock *lock, |
383 | struct nlm_lock *conflock) | 440 | struct nlm_lock *conflock) |
384 | { | 441 | { |
385 | struct file_lock *fl; | ||
386 | |||
387 | dprintk("lockd: nlmsvc_testlock(%s/%ld, ty=%d, %Ld-%Ld)\n", | 442 | dprintk("lockd: nlmsvc_testlock(%s/%ld, ty=%d, %Ld-%Ld)\n", |
388 | file->f_file->f_dentry->d_inode->i_sb->s_id, | 443 | file->f_file->f_dentry->d_inode->i_sb->s_id, |
389 | file->f_file->f_dentry->d_inode->i_ino, | 444 | file->f_file->f_dentry->d_inode->i_ino, |
@@ -391,13 +446,14 @@ nlmsvc_testlock(struct nlm_file *file, struct nlm_lock *lock, | |||
391 | (long long)lock->fl.fl_start, | 446 | (long long)lock->fl.fl_start, |
392 | (long long)lock->fl.fl_end); | 447 | (long long)lock->fl.fl_end); |
393 | 448 | ||
394 | if ((fl = posix_test_lock(file->f_file, &lock->fl)) != NULL) { | 449 | if (posix_test_lock(file->f_file, &lock->fl, &conflock->fl)) { |
395 | dprintk("lockd: conflicting lock(ty=%d, %Ld-%Ld)\n", | 450 | dprintk("lockd: conflicting lock(ty=%d, %Ld-%Ld)\n", |
396 | fl->fl_type, (long long)fl->fl_start, | 451 | conflock->fl.fl_type, |
397 | (long long)fl->fl_end); | 452 | (long long)conflock->fl.fl_start, |
453 | (long long)conflock->fl.fl_end); | ||
398 | conflock->caller = "somehost"; /* FIXME */ | 454 | conflock->caller = "somehost"; /* FIXME */ |
399 | conflock->oh.len = 0; /* don't return OH info */ | 455 | conflock->oh.len = 0; /* don't return OH info */ |
400 | conflock->fl = *fl; | 456 | conflock->svid = conflock->fl.fl_pid; |
401 | return nlm_lck_denied; | 457 | return nlm_lck_denied; |
402 | } | 458 | } |
403 | 459 | ||
@@ -453,9 +509,12 @@ nlmsvc_cancel_blocked(struct nlm_file *file, struct nlm_lock *lock) | |||
453 | (long long)lock->fl.fl_end); | 509 | (long long)lock->fl.fl_end); |
454 | 510 | ||
455 | down(&file->f_sema); | 511 | down(&file->f_sema); |
456 | if ((block = nlmsvc_lookup_block(file, lock, 1)) != NULL) | 512 | block = nlmsvc_lookup_block(file, lock); |
457 | status = nlmsvc_delete_block(block, 1); | ||
458 | up(&file->f_sema); | 513 | up(&file->f_sema); |
514 | if (block != NULL) { | ||
515 | status = nlmsvc_unlink_block(block); | ||
516 | nlmsvc_release_block(block); | ||
517 | } | ||
459 | return status ? nlm_lck_denied : nlm_granted; | 518 | return status ? nlm_lck_denied : nlm_granted; |
460 | } | 519 | } |
461 | 520 | ||
@@ -473,7 +532,7 @@ nlmsvc_notify_blocked(struct file_lock *fl) | |||
473 | 532 | ||
474 | dprintk("lockd: VFS unblock notification for block %p\n", fl); | 533 | dprintk("lockd: VFS unblock notification for block %p\n", fl); |
475 | for (bp = &nlm_blocked; (block = *bp) != 0; bp = &block->b_next) { | 534 | for (bp = &nlm_blocked; (block = *bp) != 0; bp = &block->b_next) { |
476 | if (nlm_compare_locks(&block->b_call.a_args.lock.fl, fl)) { | 535 | if (nlm_compare_locks(&block->b_call->a_args.lock.fl, fl)) { |
477 | nlmsvc_insert_block(block, 0); | 536 | nlmsvc_insert_block(block, 0); |
478 | svc_wake_up(block->b_daemon); | 537 | svc_wake_up(block->b_daemon); |
479 | return; | 538 | return; |
@@ -508,17 +567,13 @@ static void | |||
508 | nlmsvc_grant_blocked(struct nlm_block *block) | 567 | nlmsvc_grant_blocked(struct nlm_block *block) |
509 | { | 568 | { |
510 | struct nlm_file *file = block->b_file; | 569 | struct nlm_file *file = block->b_file; |
511 | struct nlm_lock *lock = &block->b_call.a_args.lock; | 570 | struct nlm_lock *lock = &block->b_call->a_args.lock; |
512 | struct file_lock *conflock; | ||
513 | int error; | 571 | int error; |
514 | 572 | ||
515 | dprintk("lockd: grant blocked lock %p\n", block); | 573 | dprintk("lockd: grant blocked lock %p\n", block); |
516 | 574 | ||
517 | /* First thing is lock the file */ | ||
518 | down(&file->f_sema); | ||
519 | |||
520 | /* Unlink block request from list */ | 575 | /* Unlink block request from list */ |
521 | nlmsvc_remove_block(block); | 576 | nlmsvc_unlink_block(block); |
522 | 577 | ||
523 | /* If b_granted is true this means we've been here before. | 578 | /* If b_granted is true this means we've been here before. |
524 | * Just retry the grant callback, possibly refreshing the RPC | 579 | * Just retry the grant callback, possibly refreshing the RPC |
@@ -529,24 +584,21 @@ nlmsvc_grant_blocked(struct nlm_block *block) | |||
529 | } | 584 | } |
530 | 585 | ||
531 | /* Try the lock operation again */ | 586 | /* Try the lock operation again */ |
532 | if ((conflock = posix_test_lock(file->f_file, &lock->fl)) != NULL) { | 587 | lock->fl.fl_flags |= FL_SLEEP; |
533 | /* Bummer, we blocked again */ | 588 | error = posix_lock_file(file->f_file, &lock->fl); |
589 | lock->fl.fl_flags &= ~FL_SLEEP; | ||
590 | |||
591 | switch (error) { | ||
592 | case 0: | ||
593 | break; | ||
594 | case -EAGAIN: | ||
534 | dprintk("lockd: lock still blocked\n"); | 595 | dprintk("lockd: lock still blocked\n"); |
535 | nlmsvc_insert_block(block, NLM_NEVER); | 596 | nlmsvc_insert_block(block, NLM_NEVER); |
536 | posix_block_lock(conflock, &lock->fl); | ||
537 | up(&file->f_sema); | ||
538 | return; | 597 | return; |
539 | } | 598 | default: |
540 | |||
541 | /* Alright, no conflicting lock. Now lock it for real. If the | ||
542 | * following yields an error, this is most probably due to low | ||
543 | * memory. Retry the lock in a few seconds. | ||
544 | */ | ||
545 | if ((error = posix_lock_file(file->f_file, &lock->fl)) < 0) { | ||
546 | printk(KERN_WARNING "lockd: unexpected error %d in %s!\n", | 599 | printk(KERN_WARNING "lockd: unexpected error %d in %s!\n", |
547 | -error, __FUNCTION__); | 600 | -error, __FUNCTION__); |
548 | nlmsvc_insert_block(block, 10 * HZ); | 601 | nlmsvc_insert_block(block, 10 * HZ); |
549 | up(&file->f_sema); | ||
550 | return; | 602 | return; |
551 | } | 603 | } |
552 | 604 | ||
@@ -554,17 +606,15 @@ callback: | |||
554 | /* Lock was granted by VFS. */ | 606 | /* Lock was granted by VFS. */ |
555 | dprintk("lockd: GRANTing blocked lock.\n"); | 607 | dprintk("lockd: GRANTing blocked lock.\n"); |
556 | block->b_granted = 1; | 608 | block->b_granted = 1; |
557 | block->b_incall = 1; | ||
558 | 609 | ||
559 | /* Schedule next grant callback in 30 seconds */ | 610 | /* Schedule next grant callback in 30 seconds */ |
560 | nlmsvc_insert_block(block, 30 * HZ); | 611 | nlmsvc_insert_block(block, 30 * HZ); |
561 | 612 | ||
562 | /* Call the client */ | 613 | /* Call the client */ |
563 | nlm_get_host(block->b_call.a_host); | 614 | kref_get(&block->b_count); |
564 | if (nlmsvc_async_call(&block->b_call, NLMPROC_GRANTED_MSG, | 615 | if (nlm_async_call(block->b_call, NLMPROC_GRANTED_MSG, |
565 | &nlmsvc_grant_ops) < 0) | 616 | &nlmsvc_grant_ops) < 0) |
566 | nlm_release_host(block->b_call.a_host); | 617 | nlmsvc_release_block(block); |
567 | up(&file->f_sema); | ||
568 | } | 618 | } |
569 | 619 | ||
570 | /* | 620 | /* |
@@ -578,20 +628,10 @@ callback: | |||
578 | static void nlmsvc_grant_callback(struct rpc_task *task, void *data) | 628 | static void nlmsvc_grant_callback(struct rpc_task *task, void *data) |
579 | { | 629 | { |
580 | struct nlm_rqst *call = data; | 630 | struct nlm_rqst *call = data; |
581 | struct nlm_block *block; | 631 | struct nlm_block *block = call->a_block; |
582 | unsigned long timeout; | 632 | unsigned long timeout; |
583 | struct sockaddr_in *peer_addr = RPC_PEERADDR(task->tk_client); | ||
584 | 633 | ||
585 | dprintk("lockd: GRANT_MSG RPC callback\n"); | 634 | dprintk("lockd: GRANT_MSG RPC callback\n"); |
586 | dprintk("callback: looking for cookie %s, host (%u.%u.%u.%u)\n", | ||
587 | nlmdbg_cookie2a(&call->a_args.cookie), | ||
588 | NIPQUAD(peer_addr->sin_addr.s_addr)); | ||
589 | if (!(block = nlmsvc_find_block(&call->a_args.cookie, peer_addr))) { | ||
590 | dprintk("lockd: no block for cookie %s, host (%u.%u.%u.%u)\n", | ||
591 | nlmdbg_cookie2a(&call->a_args.cookie), | ||
592 | NIPQUAD(peer_addr->sin_addr.s_addr)); | ||
593 | return; | ||
594 | } | ||
595 | 635 | ||
596 | /* Technically, we should down the file semaphore here. Since we | 636 | /* Technically, we should down the file semaphore here. Since we |
597 | * move the block towards the head of the queue only, no harm | 637 | * move the block towards the head of the queue only, no harm |
@@ -608,13 +648,18 @@ static void nlmsvc_grant_callback(struct rpc_task *task, void *data) | |||
608 | } | 648 | } |
609 | nlmsvc_insert_block(block, timeout); | 649 | nlmsvc_insert_block(block, timeout); |
610 | svc_wake_up(block->b_daemon); | 650 | svc_wake_up(block->b_daemon); |
611 | block->b_incall = 0; | 651 | } |
612 | 652 | ||
613 | nlm_release_host(call->a_host); | 653 | void nlmsvc_grant_release(void *data) |
654 | { | ||
655 | struct nlm_rqst *call = data; | ||
656 | |||
657 | nlmsvc_release_block(call->a_block); | ||
614 | } | 658 | } |
615 | 659 | ||
616 | static const struct rpc_call_ops nlmsvc_grant_ops = { | 660 | static const struct rpc_call_ops nlmsvc_grant_ops = { |
617 | .rpc_call_done = nlmsvc_grant_callback, | 661 | .rpc_call_done = nlmsvc_grant_callback, |
662 | .rpc_release = nlmsvc_grant_release, | ||
618 | }; | 663 | }; |
619 | 664 | ||
620 | /* | 665 | /* |
@@ -634,25 +679,17 @@ nlmsvc_grant_reply(struct svc_rqst *rqstp, struct nlm_cookie *cookie, u32 status | |||
634 | return; | 679 | return; |
635 | file = block->b_file; | 680 | file = block->b_file; |
636 | 681 | ||
637 | file->f_count++; | ||
638 | down(&file->f_sema); | ||
639 | block = nlmsvc_find_block(cookie, &rqstp->rq_addr); | ||
640 | if (block) { | 682 | if (block) { |
641 | if (status == NLM_LCK_DENIED_GRACE_PERIOD) { | 683 | if (status == NLM_LCK_DENIED_GRACE_PERIOD) { |
642 | /* Try again in a couple of seconds */ | 684 | /* Try again in a couple of seconds */ |
643 | nlmsvc_insert_block(block, 10 * HZ); | 685 | nlmsvc_insert_block(block, 10 * HZ); |
644 | up(&file->f_sema); | ||
645 | } else { | 686 | } else { |
646 | /* Lock is now held by client, or has been rejected. | 687 | /* Lock is now held by client, or has been rejected. |
647 | * In both cases, the block should be removed. */ | 688 | * In both cases, the block should be removed. */ |
648 | up(&file->f_sema); | 689 | nlmsvc_unlink_block(block); |
649 | if (status == NLM_LCK_GRANTED) | ||
650 | nlmsvc_delete_block(block, 0); | ||
651 | else | ||
652 | nlmsvc_delete_block(block, 1); | ||
653 | } | 690 | } |
654 | } | 691 | } |
655 | nlm_release_file(file); | 692 | nlmsvc_release_block(block); |
656 | } | 693 | } |
657 | 694 | ||
658 | /* | 695 | /* |
@@ -675,10 +712,12 @@ nlmsvc_retry_blocked(void) | |||
675 | break; | 712 | break; |
676 | dprintk("nlmsvc_retry_blocked(%p, when=%ld, done=%d)\n", | 713 | dprintk("nlmsvc_retry_blocked(%p, when=%ld, done=%d)\n", |
677 | block, block->b_when, block->b_done); | 714 | block, block->b_when, block->b_done); |
715 | kref_get(&block->b_count); | ||
678 | if (block->b_done) | 716 | if (block->b_done) |
679 | nlmsvc_delete_block(block, 0); | 717 | nlmsvc_unlink_block(block); |
680 | else | 718 | else |
681 | nlmsvc_grant_blocked(block); | 719 | nlmsvc_grant_blocked(block); |
720 | nlmsvc_release_block(block); | ||
682 | } | 721 | } |
683 | 722 | ||
684 | if ((block = nlm_blocked) && block->b_when != NLM_NEVER) | 723 | if ((block = nlm_blocked) && block->b_when != NLM_NEVER) |