svclock.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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. #include <linux/config.h>
  23. #include <linux/types.h>
  24. #include <linux/errno.h>
  25. #include <linux/kernel.h>
  26. #include <linux/sched.h>
  27. #include <linux/smp_lock.h>
  28. #include <linux/sunrpc/clnt.h>
  29. #include <linux/sunrpc/svc.h>
  30. #include <linux/lockd/nlm.h>
  31. #include <linux/lockd/lockd.h>
  32. #define NLMDBG_FACILITY NLMDBG_SVCLOCK
  33. #ifdef CONFIG_LOCKD_V4
  34. #define nlm_deadlock nlm4_deadlock
  35. #else
  36. #define nlm_deadlock nlm_lck_denied
  37. #endif
  38. static void nlmsvc_insert_block(struct nlm_block *block, unsigned long);
  39. static int nlmsvc_remove_block(struct nlm_block *block);
  40. static const struct rpc_call_ops nlmsvc_grant_ops;
  41. /*
  42. * The list of blocked locks to retry
  43. */
  44. static struct nlm_block * nlm_blocked;
  45. /*
  46. * Insert a blocked lock into the global list
  47. */
  48. static void
  49. nlmsvc_insert_block(struct nlm_block *block, unsigned long when)
  50. {
  51. struct nlm_block **bp, *b;
  52. dprintk("lockd: nlmsvc_insert_block(%p, %ld)\n", block, when);
  53. if (block->b_queued)
  54. nlmsvc_remove_block(block);
  55. bp = &nlm_blocked;
  56. if (when != NLM_NEVER) {
  57. if ((when += jiffies) == NLM_NEVER)
  58. when ++;
  59. while ((b = *bp) && time_before_eq(b->b_when,when) && b->b_when != NLM_NEVER)
  60. bp = &b->b_next;
  61. } else
  62. while ((b = *bp) != 0)
  63. bp = &b->b_next;
  64. block->b_queued = 1;
  65. block->b_when = when;
  66. block->b_next = b;
  67. *bp = block;
  68. }
  69. /*
  70. * Remove a block from the global list
  71. */
  72. static int
  73. nlmsvc_remove_block(struct nlm_block *block)
  74. {
  75. struct nlm_block **bp, *b;
  76. if (!block->b_queued)
  77. return 1;
  78. for (bp = &nlm_blocked; (b = *bp) != 0; bp = &b->b_next) {
  79. if (b == block) {
  80. *bp = block->b_next;
  81. block->b_queued = 0;
  82. return 1;
  83. }
  84. }
  85. return 0;
  86. }
  87. /*
  88. * Find a block for a given lock and optionally remove it from
  89. * the list.
  90. */
  91. static struct nlm_block *
  92. nlmsvc_lookup_block(struct nlm_file *file, struct nlm_lock *lock, int remove)
  93. {
  94. struct nlm_block **head, *block;
  95. struct file_lock *fl;
  96. dprintk("lockd: nlmsvc_lookup_block f=%p pd=%d %Ld-%Ld ty=%d\n",
  97. file, lock->fl.fl_pid,
  98. (long long)lock->fl.fl_start,
  99. (long long)lock->fl.fl_end, lock->fl.fl_type);
  100. for (head = &nlm_blocked; (block = *head) != 0; head = &block->b_next) {
  101. fl = &block->b_call.a_args.lock.fl;
  102. dprintk("lockd: check f=%p pd=%d %Ld-%Ld ty=%d cookie=%s\n",
  103. block->b_file, fl->fl_pid,
  104. (long long)fl->fl_start,
  105. (long long)fl->fl_end, fl->fl_type,
  106. nlmdbg_cookie2a(&block->b_call.a_args.cookie));
  107. if (block->b_file == file && nlm_compare_locks(fl, &lock->fl)) {
  108. if (remove) {
  109. *head = block->b_next;
  110. block->b_queued = 0;
  111. }
  112. return block;
  113. }
  114. }
  115. return NULL;
  116. }
  117. static inline int nlm_cookie_match(struct nlm_cookie *a, struct nlm_cookie *b)
  118. {
  119. if(a->len != b->len)
  120. return 0;
  121. if(memcmp(a->data,b->data,a->len))
  122. return 0;
  123. return 1;
  124. }
  125. /*
  126. * Find a block with a given NLM cookie.
  127. */
  128. static inline struct nlm_block *
  129. nlmsvc_find_block(struct nlm_cookie *cookie, struct sockaddr_in *sin)
  130. {
  131. struct nlm_block *block;
  132. for (block = nlm_blocked; block; block = block->b_next) {
  133. dprintk("cookie: head of blocked queue %p, block %p\n",
  134. nlm_blocked, block);
  135. if (nlm_cookie_match(&block->b_call.a_args.cookie,cookie)
  136. && nlm_cmp_addr(sin, &block->b_host->h_addr))
  137. break;
  138. }
  139. return block;
  140. }
  141. /*
  142. * Create a block and initialize it.
  143. *
  144. * Note: we explicitly set the cookie of the grant reply to that of
  145. * the blocked lock request. The spec explicitly mentions that the client
  146. * should _not_ rely on the callback containing the same cookie as the
  147. * request, but (as I found out later) that's because some implementations
  148. * do just this. Never mind the standards comittees, they support our
  149. * logging industries.
  150. */
  151. static inline struct nlm_block *
  152. nlmsvc_create_block(struct svc_rqst *rqstp, struct nlm_file *file,
  153. struct nlm_lock *lock, struct nlm_cookie *cookie)
  154. {
  155. struct nlm_block *block;
  156. struct nlm_host *host;
  157. struct nlm_rqst *call;
  158. /* Create host handle for callback */
  159. host = nlmclnt_lookup_host(&rqstp->rq_addr,
  160. rqstp->rq_prot, rqstp->rq_vers);
  161. if (host == NULL)
  162. return NULL;
  163. /* Allocate memory for block, and initialize arguments */
  164. if (!(block = (struct nlm_block *) kmalloc(sizeof(*block), GFP_KERNEL)))
  165. goto failed;
  166. memset(block, 0, sizeof(*block));
  167. locks_init_lock(&block->b_call.a_args.lock.fl);
  168. locks_init_lock(&block->b_call.a_res.lock.fl);
  169. if (!nlmclnt_setgrantargs(&block->b_call, lock))
  170. goto failed_free;
  171. /* Set notifier function for VFS, and init args */
  172. block->b_call.a_args.lock.fl.fl_lmops = &nlmsvc_lock_operations;
  173. block->b_call.a_args.cookie = *cookie; /* see above */
  174. dprintk("lockd: created block %p...\n", block);
  175. /* Create and initialize the block */
  176. block->b_daemon = rqstp->rq_server;
  177. block->b_host = host;
  178. block->b_file = file;
  179. /* Add to file's list of blocks */
  180. block->b_fnext = file->f_blocks;
  181. file->f_blocks = block;
  182. /* Set up RPC arguments for callback */
  183. call = &block->b_call;
  184. call->a_host = host;
  185. call->a_flags = RPC_TASK_ASYNC;
  186. return block;
  187. failed_free:
  188. kfree(block);
  189. failed:
  190. nlm_release_host(host);
  191. return NULL;
  192. }
  193. /*
  194. * Delete a block. If the lock was cancelled or the grant callback
  195. * failed, unlock is set to 1.
  196. * It is the caller's responsibility to check whether the file
  197. * can be closed hereafter.
  198. */
  199. static int
  200. nlmsvc_delete_block(struct nlm_block *block, int unlock)
  201. {
  202. struct file_lock *fl = &block->b_call.a_args.lock.fl;
  203. struct nlm_file *file = block->b_file;
  204. struct nlm_block **bp;
  205. int status = 0;
  206. dprintk("lockd: deleting block %p...\n", block);
  207. /* Remove block from list */
  208. nlmsvc_remove_block(block);
  209. if (unlock)
  210. status = posix_unblock_lock(file->f_file, fl);
  211. /* If the block is in the middle of a GRANT callback,
  212. * don't kill it yet. */
  213. if (block->b_incall) {
  214. nlmsvc_insert_block(block, NLM_NEVER);
  215. block->b_done = 1;
  216. return status;
  217. }
  218. /* Remove block from file's list of blocks */
  219. for (bp = &file->f_blocks; *bp; bp = &(*bp)->b_fnext) {
  220. if (*bp == block) {
  221. *bp = block->b_fnext;
  222. break;
  223. }
  224. }
  225. if (block->b_host)
  226. nlm_release_host(block->b_host);
  227. nlmclnt_freegrantargs(&block->b_call);
  228. kfree(block);
  229. return status;
  230. }
  231. /*
  232. * Loop over all blocks and perform the action specified.
  233. * (NLM_ACT_CHECK handled by nlmsvc_inspect_file).
  234. */
  235. int
  236. nlmsvc_traverse_blocks(struct nlm_host *host, struct nlm_file *file, int action)
  237. {
  238. struct nlm_block *block, *next;
  239. /* XXX: Will everything get cleaned up if we don't unlock here? */
  240. down(&file->f_sema);
  241. for (block = file->f_blocks; block; block = next) {
  242. next = block->b_fnext;
  243. if (action == NLM_ACT_MARK)
  244. block->b_host->h_inuse = 1;
  245. else if (action == NLM_ACT_UNLOCK) {
  246. if (host == NULL || host == block->b_host)
  247. nlmsvc_delete_block(block, 1);
  248. }
  249. }
  250. up(&file->f_sema);
  251. return 0;
  252. }
  253. /*
  254. * Attempt to establish a lock, and if it can't be granted, block it
  255. * if required.
  256. */
  257. u32
  258. nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file,
  259. struct nlm_lock *lock, int wait, struct nlm_cookie *cookie)
  260. {
  261. struct nlm_block *block;
  262. int error;
  263. u32 ret;
  264. dprintk("lockd: nlmsvc_lock(%s/%ld, ty=%d, pi=%d, %Ld-%Ld, bl=%d)\n",
  265. file->f_file->f_dentry->d_inode->i_sb->s_id,
  266. file->f_file->f_dentry->d_inode->i_ino,
  267. lock->fl.fl_type, lock->fl.fl_pid,
  268. (long long)lock->fl.fl_start,
  269. (long long)lock->fl.fl_end,
  270. wait);
  271. /* Get existing block (in case client is busy-waiting) */
  272. block = nlmsvc_lookup_block(file, lock, 0);
  273. again:
  274. /* Lock file against concurrent access */
  275. down(&file->f_sema);
  276. error = posix_lock_file(file->f_file, &lock->fl);
  277. dprintk("lockd: posix_lock_file returned %d\n", error);
  278. if (error != -EAGAIN) {
  279. if (block)
  280. nlmsvc_delete_block(block, 0);
  281. up(&file->f_sema);
  282. switch(-error) {
  283. case 0:
  284. ret = nlm_granted;
  285. goto out;
  286. case EDEADLK:
  287. ret = nlm_deadlock;
  288. goto out;
  289. default: /* includes ENOLCK */
  290. ret = nlm_lck_denied_nolocks;
  291. goto out;
  292. }
  293. }
  294. if (!wait) {
  295. ret = nlm_lck_denied;
  296. goto out_unlock;
  297. }
  298. /* If we don't have a block, create and initialize it. Then
  299. * retry because we may have slept in kmalloc. */
  300. /* We have to release f_sema as nlmsvc_create_block may try to
  301. * to claim it while doing host garbage collection */
  302. if (block == NULL) {
  303. up(&file->f_sema);
  304. dprintk("lockd: blocking on this lock (allocating).\n");
  305. if (!(block = nlmsvc_create_block(rqstp, file, lock, cookie)))
  306. return nlm_lck_denied_nolocks;
  307. goto again;
  308. }
  309. /* Append to list of blocked */
  310. nlmsvc_insert_block(block, NLM_NEVER);
  311. ret = nlm_lck_blocked;
  312. out_unlock:
  313. up(&file->f_sema);
  314. out:
  315. dprintk("lockd: nlmsvc_lock returned %u\n", ret);
  316. return ret;
  317. }
  318. /*
  319. * Test for presence of a conflicting lock.
  320. */
  321. u32
  322. nlmsvc_testlock(struct nlm_file *file, struct nlm_lock *lock,
  323. struct nlm_lock *conflock)
  324. {
  325. dprintk("lockd: nlmsvc_testlock(%s/%ld, ty=%d, %Ld-%Ld)\n",
  326. file->f_file->f_dentry->d_inode->i_sb->s_id,
  327. file->f_file->f_dentry->d_inode->i_ino,
  328. lock->fl.fl_type,
  329. (long long)lock->fl.fl_start,
  330. (long long)lock->fl.fl_end);
  331. if (posix_test_lock(file->f_file, &lock->fl, &conflock->fl)) {
  332. dprintk("lockd: conflicting lock(ty=%d, %Ld-%Ld)\n",
  333. conflock->fl.fl_type,
  334. (long long)conflock->fl.fl_start,
  335. (long long)conflock->fl.fl_end);
  336. conflock->caller = "somehost"; /* FIXME */
  337. conflock->oh.len = 0; /* don't return OH info */
  338. conflock->svid = conflock->fl.fl_pid;
  339. return nlm_lck_denied;
  340. }
  341. return nlm_granted;
  342. }
  343. /*
  344. * Remove a lock.
  345. * This implies a CANCEL call: We send a GRANT_MSG, the client replies
  346. * with a GRANT_RES call which gets lost, and calls UNLOCK immediately
  347. * afterwards. In this case the block will still be there, and hence
  348. * must be removed.
  349. */
  350. u32
  351. nlmsvc_unlock(struct nlm_file *file, struct nlm_lock *lock)
  352. {
  353. int error;
  354. dprintk("lockd: nlmsvc_unlock(%s/%ld, pi=%d, %Ld-%Ld)\n",
  355. file->f_file->f_dentry->d_inode->i_sb->s_id,
  356. file->f_file->f_dentry->d_inode->i_ino,
  357. lock->fl.fl_pid,
  358. (long long)lock->fl.fl_start,
  359. (long long)lock->fl.fl_end);
  360. /* First, cancel any lock that might be there */
  361. nlmsvc_cancel_blocked(file, lock);
  362. lock->fl.fl_type = F_UNLCK;
  363. error = posix_lock_file(file->f_file, &lock->fl);
  364. return (error < 0)? nlm_lck_denied_nolocks : nlm_granted;
  365. }
  366. /*
  367. * Cancel a previously blocked request.
  368. *
  369. * A cancel request always overrides any grant that may currently
  370. * be in progress.
  371. * The calling procedure must check whether the file can be closed.
  372. */
  373. u32
  374. nlmsvc_cancel_blocked(struct nlm_file *file, struct nlm_lock *lock)
  375. {
  376. struct nlm_block *block;
  377. int status = 0;
  378. dprintk("lockd: nlmsvc_cancel(%s/%ld, pi=%d, %Ld-%Ld)\n",
  379. file->f_file->f_dentry->d_inode->i_sb->s_id,
  380. file->f_file->f_dentry->d_inode->i_ino,
  381. lock->fl.fl_pid,
  382. (long long)lock->fl.fl_start,
  383. (long long)lock->fl.fl_end);
  384. down(&file->f_sema);
  385. if ((block = nlmsvc_lookup_block(file, lock, 1)) != NULL)
  386. status = nlmsvc_delete_block(block, 1);
  387. up(&file->f_sema);
  388. return status ? nlm_lck_denied : nlm_granted;
  389. }
  390. /*
  391. * Unblock a blocked lock request. This is a callback invoked from the
  392. * VFS layer when a lock on which we blocked is removed.
  393. *
  394. * This function doesn't grant the blocked lock instantly, but rather moves
  395. * the block to the head of nlm_blocked where it can be picked up by lockd.
  396. */
  397. static void
  398. nlmsvc_notify_blocked(struct file_lock *fl)
  399. {
  400. struct nlm_block **bp, *block;
  401. dprintk("lockd: VFS unblock notification for block %p\n", fl);
  402. for (bp = &nlm_blocked; (block = *bp) != 0; bp = &block->b_next) {
  403. if (nlm_compare_locks(&block->b_call.a_args.lock.fl, fl)) {
  404. nlmsvc_insert_block(block, 0);
  405. svc_wake_up(block->b_daemon);
  406. return;
  407. }
  408. }
  409. printk(KERN_WARNING "lockd: notification for unknown block!\n");
  410. }
  411. static int nlmsvc_same_owner(struct file_lock *fl1, struct file_lock *fl2)
  412. {
  413. return fl1->fl_owner == fl2->fl_owner && fl1->fl_pid == fl2->fl_pid;
  414. }
  415. struct lock_manager_operations nlmsvc_lock_operations = {
  416. .fl_compare_owner = nlmsvc_same_owner,
  417. .fl_notify = nlmsvc_notify_blocked,
  418. };
  419. /*
  420. * Try to claim a lock that was previously blocked.
  421. *
  422. * Note that we use both the RPC_GRANTED_MSG call _and_ an async
  423. * RPC thread when notifying the client. This seems like overkill...
  424. * Here's why:
  425. * - we don't want to use a synchronous RPC thread, otherwise
  426. * we might find ourselves hanging on a dead portmapper.
  427. * - Some lockd implementations (e.g. HP) don't react to
  428. * RPC_GRANTED calls; they seem to insist on RPC_GRANTED_MSG calls.
  429. */
  430. static void
  431. nlmsvc_grant_blocked(struct nlm_block *block)
  432. {
  433. struct nlm_file *file = block->b_file;
  434. struct nlm_lock *lock = &block->b_call.a_args.lock;
  435. int error;
  436. dprintk("lockd: grant blocked lock %p\n", block);
  437. /* First thing is lock the file */
  438. down(&file->f_sema);
  439. /* Unlink block request from list */
  440. nlmsvc_remove_block(block);
  441. /* If b_granted is true this means we've been here before.
  442. * Just retry the grant callback, possibly refreshing the RPC
  443. * binding */
  444. if (block->b_granted) {
  445. nlm_rebind_host(block->b_host);
  446. goto callback;
  447. }
  448. /* Try the lock operation again */
  449. error = posix_lock_file(file->f_file, &lock->fl);
  450. switch (error) {
  451. case 0:
  452. break;
  453. case -EAGAIN:
  454. dprintk("lockd: lock still blocked\n");
  455. nlmsvc_insert_block(block, NLM_NEVER);
  456. goto out_unlock;
  457. default:
  458. printk(KERN_WARNING "lockd: unexpected error %d in %s!\n",
  459. -error, __FUNCTION__);
  460. nlmsvc_insert_block(block, 10 * HZ);
  461. goto out_unlock;
  462. }
  463. callback:
  464. /* Lock was granted by VFS. */
  465. dprintk("lockd: GRANTing blocked lock.\n");
  466. block->b_granted = 1;
  467. block->b_incall = 1;
  468. /* Schedule next grant callback in 30 seconds */
  469. nlmsvc_insert_block(block, 30 * HZ);
  470. /* Call the client */
  471. nlm_get_host(block->b_call.a_host);
  472. if (nlmsvc_async_call(&block->b_call, NLMPROC_GRANTED_MSG,
  473. &nlmsvc_grant_ops) < 0)
  474. nlm_release_host(block->b_call.a_host);
  475. out_unlock:
  476. up(&file->f_sema);
  477. }
  478. /*
  479. * This is the callback from the RPC layer when the NLM_GRANTED_MSG
  480. * RPC call has succeeded or timed out.
  481. * Like all RPC callbacks, it is invoked by the rpciod process, so it
  482. * better not sleep. Therefore, we put the blocked lock on the nlm_blocked
  483. * chain once more in order to have it removed by lockd itself (which can
  484. * then sleep on the file semaphore without disrupting e.g. the nfs client).
  485. */
  486. static void nlmsvc_grant_callback(struct rpc_task *task, void *data)
  487. {
  488. struct nlm_rqst *call = data;
  489. struct nlm_block *block;
  490. unsigned long timeout;
  491. struct sockaddr_in *peer_addr = RPC_PEERADDR(task->tk_client);
  492. dprintk("lockd: GRANT_MSG RPC callback\n");
  493. dprintk("callback: looking for cookie %s, host (%u.%u.%u.%u)\n",
  494. nlmdbg_cookie2a(&call->a_args.cookie),
  495. NIPQUAD(peer_addr->sin_addr.s_addr));
  496. if (!(block = nlmsvc_find_block(&call->a_args.cookie, peer_addr))) {
  497. dprintk("lockd: no block for cookie %s, host (%u.%u.%u.%u)\n",
  498. nlmdbg_cookie2a(&call->a_args.cookie),
  499. NIPQUAD(peer_addr->sin_addr.s_addr));
  500. return;
  501. }
  502. /* Technically, we should down the file semaphore here. Since we
  503. * move the block towards the head of the queue only, no harm
  504. * can be done, though. */
  505. if (task->tk_status < 0) {
  506. /* RPC error: Re-insert for retransmission */
  507. timeout = 10 * HZ;
  508. } else if (block->b_done) {
  509. /* Block already removed, kill it for real */
  510. timeout = 0;
  511. } else {
  512. /* Call was successful, now wait for client callback */
  513. timeout = 60 * HZ;
  514. }
  515. nlmsvc_insert_block(block, timeout);
  516. svc_wake_up(block->b_daemon);
  517. block->b_incall = 0;
  518. nlm_release_host(call->a_host);
  519. }
  520. static const struct rpc_call_ops nlmsvc_grant_ops = {
  521. .rpc_call_done = nlmsvc_grant_callback,
  522. };
  523. /*
  524. * We received a GRANT_RES callback. Try to find the corresponding
  525. * block.
  526. */
  527. void
  528. nlmsvc_grant_reply(struct svc_rqst *rqstp, struct nlm_cookie *cookie, u32 status)
  529. {
  530. struct nlm_block *block;
  531. struct nlm_file *file;
  532. dprintk("grant_reply: looking for cookie %x, host (%08x), s=%d \n",
  533. *(unsigned int *)(cookie->data),
  534. ntohl(rqstp->rq_addr.sin_addr.s_addr), status);
  535. if (!(block = nlmsvc_find_block(cookie, &rqstp->rq_addr)))
  536. return;
  537. file = block->b_file;
  538. file->f_count++;
  539. down(&file->f_sema);
  540. block = nlmsvc_find_block(cookie, &rqstp->rq_addr);
  541. if (block) {
  542. if (status == NLM_LCK_DENIED_GRACE_PERIOD) {
  543. /* Try again in a couple of seconds */
  544. nlmsvc_insert_block(block, 10 * HZ);
  545. up(&file->f_sema);
  546. } else {
  547. /* Lock is now held by client, or has been rejected.
  548. * In both cases, the block should be removed. */
  549. up(&file->f_sema);
  550. if (status == NLM_LCK_GRANTED)
  551. nlmsvc_delete_block(block, 0);
  552. else
  553. nlmsvc_delete_block(block, 1);
  554. }
  555. }
  556. nlm_release_file(file);
  557. }
  558. /*
  559. * Retry all blocked locks that have been notified. This is where lockd
  560. * picks up locks that can be granted, or grant notifications that must
  561. * be retransmitted.
  562. */
  563. unsigned long
  564. nlmsvc_retry_blocked(void)
  565. {
  566. struct nlm_block *block;
  567. dprintk("nlmsvc_retry_blocked(%p, when=%ld)\n",
  568. nlm_blocked,
  569. nlm_blocked? nlm_blocked->b_when : 0);
  570. while ((block = nlm_blocked) != 0) {
  571. if (block->b_when == NLM_NEVER)
  572. break;
  573. if (time_after(block->b_when,jiffies))
  574. break;
  575. dprintk("nlmsvc_retry_blocked(%p, when=%ld, done=%d)\n",
  576. block, block->b_when, block->b_done);
  577. if (block->b_done)
  578. nlmsvc_delete_block(block, 0);
  579. else
  580. nlmsvc_grant_blocked(block);
  581. }
  582. if ((block = nlm_blocked) && block->b_when != NLM_NEVER)
  583. return (block->b_when - jiffies);
  584. return MAX_SCHEDULE_TIMEOUT;
  585. }