svclock.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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 file_lock *conflock;
  262. struct nlm_block *block;
  263. int error;
  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. lock->fl.fl_flags |= FL_LOCKD;
  274. again:
  275. /* Lock file against concurrent access */
  276. down(&file->f_sema);
  277. if (!(conflock = posix_test_lock(file->f_file, &lock->fl))) {
  278. error = posix_lock_file(file->f_file, &lock->fl);
  279. if (block)
  280. nlmsvc_delete_block(block, 0);
  281. up(&file->f_sema);
  282. dprintk("lockd: posix_lock_file returned %d\n", -error);
  283. switch(-error) {
  284. case 0:
  285. return nlm_granted;
  286. case EDEADLK:
  287. return nlm_deadlock;
  288. case EAGAIN:
  289. return nlm_lck_denied;
  290. default: /* includes ENOLCK */
  291. return nlm_lck_denied_nolocks;
  292. }
  293. }
  294. if (!wait) {
  295. up(&file->f_sema);
  296. return nlm_lck_denied;
  297. }
  298. if (posix_locks_deadlock(&lock->fl, conflock)) {
  299. up(&file->f_sema);
  300. return nlm_deadlock;
  301. }
  302. /* If we don't have a block, create and initialize it. Then
  303. * retry because we may have slept in kmalloc. */
  304. /* We have to release f_sema as nlmsvc_create_block may try to
  305. * to claim it while doing host garbage collection */
  306. if (block == NULL) {
  307. up(&file->f_sema);
  308. dprintk("lockd: blocking on this lock (allocating).\n");
  309. if (!(block = nlmsvc_create_block(rqstp, file, lock, cookie)))
  310. return nlm_lck_denied_nolocks;
  311. goto again;
  312. }
  313. /* Append to list of blocked */
  314. nlmsvc_insert_block(block, NLM_NEVER);
  315. if (list_empty(&block->b_call.a_args.lock.fl.fl_block)) {
  316. /* Now add block to block list of the conflicting lock
  317. if we haven't done so. */
  318. dprintk("lockd: blocking on this lock.\n");
  319. posix_block_lock(conflock, &block->b_call.a_args.lock.fl);
  320. }
  321. up(&file->f_sema);
  322. return nlm_lck_blocked;
  323. }
  324. /*
  325. * Test for presence of a conflicting lock.
  326. */
  327. u32
  328. nlmsvc_testlock(struct nlm_file *file, struct nlm_lock *lock,
  329. struct nlm_lock *conflock)
  330. {
  331. struct file_lock *fl;
  332. dprintk("lockd: nlmsvc_testlock(%s/%ld, ty=%d, %Ld-%Ld)\n",
  333. file->f_file->f_dentry->d_inode->i_sb->s_id,
  334. file->f_file->f_dentry->d_inode->i_ino,
  335. lock->fl.fl_type,
  336. (long long)lock->fl.fl_start,
  337. (long long)lock->fl.fl_end);
  338. if ((fl = posix_test_lock(file->f_file, &lock->fl)) != NULL) {
  339. dprintk("lockd: conflicting lock(ty=%d, %Ld-%Ld)\n",
  340. fl->fl_type, (long long)fl->fl_start,
  341. (long long)fl->fl_end);
  342. conflock->caller = "somehost"; /* FIXME */
  343. conflock->oh.len = 0; /* don't return OH info */
  344. conflock->fl = *fl;
  345. return nlm_lck_denied;
  346. }
  347. return nlm_granted;
  348. }
  349. /*
  350. * Remove a lock.
  351. * This implies a CANCEL call: We send a GRANT_MSG, the client replies
  352. * with a GRANT_RES call which gets lost, and calls UNLOCK immediately
  353. * afterwards. In this case the block will still be there, and hence
  354. * must be removed.
  355. */
  356. u32
  357. nlmsvc_unlock(struct nlm_file *file, struct nlm_lock *lock)
  358. {
  359. int error;
  360. dprintk("lockd: nlmsvc_unlock(%s/%ld, pi=%d, %Ld-%Ld)\n",
  361. file->f_file->f_dentry->d_inode->i_sb->s_id,
  362. file->f_file->f_dentry->d_inode->i_ino,
  363. lock->fl.fl_pid,
  364. (long long)lock->fl.fl_start,
  365. (long long)lock->fl.fl_end);
  366. /* First, cancel any lock that might be there */
  367. nlmsvc_cancel_blocked(file, lock);
  368. lock->fl.fl_type = F_UNLCK;
  369. error = posix_lock_file(file->f_file, &lock->fl);
  370. return (error < 0)? nlm_lck_denied_nolocks : nlm_granted;
  371. }
  372. /*
  373. * Cancel a previously blocked request.
  374. *
  375. * A cancel request always overrides any grant that may currently
  376. * be in progress.
  377. * The calling procedure must check whether the file can be closed.
  378. */
  379. u32
  380. nlmsvc_cancel_blocked(struct nlm_file *file, struct nlm_lock *lock)
  381. {
  382. struct nlm_block *block;
  383. int status = 0;
  384. dprintk("lockd: nlmsvc_cancel(%s/%ld, pi=%d, %Ld-%Ld)\n",
  385. file->f_file->f_dentry->d_inode->i_sb->s_id,
  386. file->f_file->f_dentry->d_inode->i_ino,
  387. lock->fl.fl_pid,
  388. (long long)lock->fl.fl_start,
  389. (long long)lock->fl.fl_end);
  390. down(&file->f_sema);
  391. if ((block = nlmsvc_lookup_block(file, lock, 1)) != NULL)
  392. status = nlmsvc_delete_block(block, 1);
  393. up(&file->f_sema);
  394. return status ? nlm_lck_denied : nlm_granted;
  395. }
  396. /*
  397. * Unblock a blocked lock request. This is a callback invoked from the
  398. * VFS layer when a lock on which we blocked is removed.
  399. *
  400. * This function doesn't grant the blocked lock instantly, but rather moves
  401. * the block to the head of nlm_blocked where it can be picked up by lockd.
  402. */
  403. static void
  404. nlmsvc_notify_blocked(struct file_lock *fl)
  405. {
  406. struct nlm_block **bp, *block;
  407. dprintk("lockd: VFS unblock notification for block %p\n", fl);
  408. for (bp = &nlm_blocked; (block = *bp) != 0; bp = &block->b_next) {
  409. if (nlm_compare_locks(&block->b_call.a_args.lock.fl, fl)) {
  410. nlmsvc_insert_block(block, 0);
  411. svc_wake_up(block->b_daemon);
  412. return;
  413. }
  414. }
  415. printk(KERN_WARNING "lockd: notification for unknown block!\n");
  416. }
  417. static int nlmsvc_same_owner(struct file_lock *fl1, struct file_lock *fl2)
  418. {
  419. return fl1->fl_owner == fl2->fl_owner && fl1->fl_pid == fl2->fl_pid;
  420. }
  421. struct lock_manager_operations nlmsvc_lock_operations = {
  422. .fl_compare_owner = nlmsvc_same_owner,
  423. .fl_notify = nlmsvc_notify_blocked,
  424. };
  425. /*
  426. * Try to claim a lock that was previously blocked.
  427. *
  428. * Note that we use both the RPC_GRANTED_MSG call _and_ an async
  429. * RPC thread when notifying the client. This seems like overkill...
  430. * Here's why:
  431. * - we don't want to use a synchronous RPC thread, otherwise
  432. * we might find ourselves hanging on a dead portmapper.
  433. * - Some lockd implementations (e.g. HP) don't react to
  434. * RPC_GRANTED calls; they seem to insist on RPC_GRANTED_MSG calls.
  435. */
  436. static void
  437. nlmsvc_grant_blocked(struct nlm_block *block)
  438. {
  439. struct nlm_file *file = block->b_file;
  440. struct nlm_lock *lock = &block->b_call.a_args.lock;
  441. struct file_lock *conflock;
  442. int error;
  443. dprintk("lockd: grant blocked lock %p\n", block);
  444. /* First thing is lock the file */
  445. down(&file->f_sema);
  446. /* Unlink block request from list */
  447. nlmsvc_remove_block(block);
  448. /* If b_granted is true this means we've been here before.
  449. * Just retry the grant callback, possibly refreshing the RPC
  450. * binding */
  451. if (block->b_granted) {
  452. nlm_rebind_host(block->b_host);
  453. goto callback;
  454. }
  455. /* Try the lock operation again */
  456. if ((conflock = posix_test_lock(file->f_file, &lock->fl)) != NULL) {
  457. /* Bummer, we blocked again */
  458. dprintk("lockd: lock still blocked\n");
  459. nlmsvc_insert_block(block, NLM_NEVER);
  460. posix_block_lock(conflock, &lock->fl);
  461. up(&file->f_sema);
  462. return;
  463. }
  464. /* Alright, no conflicting lock. Now lock it for real. If the
  465. * following yields an error, this is most probably due to low
  466. * memory. Retry the lock in a few seconds.
  467. */
  468. if ((error = posix_lock_file(file->f_file, &lock->fl)) < 0) {
  469. printk(KERN_WARNING "lockd: unexpected error %d in %s!\n",
  470. -error, __FUNCTION__);
  471. nlmsvc_insert_block(block, 10 * HZ);
  472. up(&file->f_sema);
  473. return;
  474. }
  475. callback:
  476. /* Lock was granted by VFS. */
  477. dprintk("lockd: GRANTing blocked lock.\n");
  478. block->b_granted = 1;
  479. block->b_incall = 1;
  480. /* Schedule next grant callback in 30 seconds */
  481. nlmsvc_insert_block(block, 30 * HZ);
  482. /* Call the client */
  483. nlm_get_host(block->b_call.a_host);
  484. if (nlmsvc_async_call(&block->b_call, NLMPROC_GRANTED_MSG,
  485. &nlmsvc_grant_ops) < 0)
  486. nlm_release_host(block->b_call.a_host);
  487. up(&file->f_sema);
  488. }
  489. /*
  490. * This is the callback from the RPC layer when the NLM_GRANTED_MSG
  491. * RPC call has succeeded or timed out.
  492. * Like all RPC callbacks, it is invoked by the rpciod process, so it
  493. * better not sleep. Therefore, we put the blocked lock on the nlm_blocked
  494. * chain once more in order to have it removed by lockd itself (which can
  495. * then sleep on the file semaphore without disrupting e.g. the nfs client).
  496. */
  497. static void nlmsvc_grant_callback(struct rpc_task *task, void *data)
  498. {
  499. struct nlm_rqst *call = data;
  500. struct nlm_block *block;
  501. unsigned long timeout;
  502. struct sockaddr_in *peer_addr = RPC_PEERADDR(task->tk_client);
  503. dprintk("lockd: GRANT_MSG RPC callback\n");
  504. dprintk("callback: looking for cookie %s, host (%u.%u.%u.%u)\n",
  505. nlmdbg_cookie2a(&call->a_args.cookie),
  506. NIPQUAD(peer_addr->sin_addr.s_addr));
  507. if (!(block = nlmsvc_find_block(&call->a_args.cookie, peer_addr))) {
  508. dprintk("lockd: no block for cookie %s, host (%u.%u.%u.%u)\n",
  509. nlmdbg_cookie2a(&call->a_args.cookie),
  510. NIPQUAD(peer_addr->sin_addr.s_addr));
  511. return;
  512. }
  513. /* Technically, we should down the file semaphore here. Since we
  514. * move the block towards the head of the queue only, no harm
  515. * can be done, though. */
  516. if (task->tk_status < 0) {
  517. /* RPC error: Re-insert for retransmission */
  518. timeout = 10 * HZ;
  519. } else if (block->b_done) {
  520. /* Block already removed, kill it for real */
  521. timeout = 0;
  522. } else {
  523. /* Call was successful, now wait for client callback */
  524. timeout = 60 * HZ;
  525. }
  526. nlmsvc_insert_block(block, timeout);
  527. svc_wake_up(block->b_daemon);
  528. block->b_incall = 0;
  529. nlm_release_host(call->a_host);
  530. }
  531. static const struct rpc_call_ops nlmsvc_grant_ops = {
  532. .rpc_call_done = nlmsvc_grant_callback,
  533. };
  534. /*
  535. * We received a GRANT_RES callback. Try to find the corresponding
  536. * block.
  537. */
  538. void
  539. nlmsvc_grant_reply(struct svc_rqst *rqstp, struct nlm_cookie *cookie, u32 status)
  540. {
  541. struct nlm_block *block;
  542. struct nlm_file *file;
  543. dprintk("grant_reply: looking for cookie %x, host (%08x), s=%d \n",
  544. *(unsigned int *)(cookie->data),
  545. ntohl(rqstp->rq_addr.sin_addr.s_addr), status);
  546. if (!(block = nlmsvc_find_block(cookie, &rqstp->rq_addr)))
  547. return;
  548. file = block->b_file;
  549. file->f_count++;
  550. down(&file->f_sema);
  551. block = nlmsvc_find_block(cookie, &rqstp->rq_addr);
  552. if (block) {
  553. if (status == NLM_LCK_DENIED_GRACE_PERIOD) {
  554. /* Try again in a couple of seconds */
  555. nlmsvc_insert_block(block, 10 * HZ);
  556. up(&file->f_sema);
  557. } else {
  558. /* Lock is now held by client, or has been rejected.
  559. * In both cases, the block should be removed. */
  560. up(&file->f_sema);
  561. if (status == NLM_LCK_GRANTED)
  562. nlmsvc_delete_block(block, 0);
  563. else
  564. nlmsvc_delete_block(block, 1);
  565. }
  566. }
  567. nlm_release_file(file);
  568. }
  569. /*
  570. * Retry all blocked locks that have been notified. This is where lockd
  571. * picks up locks that can be granted, or grant notifications that must
  572. * be retransmitted.
  573. */
  574. unsigned long
  575. nlmsvc_retry_blocked(void)
  576. {
  577. struct nlm_block *block;
  578. dprintk("nlmsvc_retry_blocked(%p, when=%ld)\n",
  579. nlm_blocked,
  580. nlm_blocked? nlm_blocked->b_when : 0);
  581. while ((block = nlm_blocked) != 0) {
  582. if (block->b_when == NLM_NEVER)
  583. break;
  584. if (time_after(block->b_when,jiffies))
  585. break;
  586. dprintk("nlmsvc_retry_blocked(%p, when=%ld, done=%d)\n",
  587. block, block->b_when, block->b_done);
  588. if (block->b_done)
  589. nlmsvc_delete_block(block, 0);
  590. else
  591. nlmsvc_grant_blocked(block);
  592. }
  593. if ((block = nlm_blocked) && block->b_when != NLM_NEVER)
  594. return (block->b_when - jiffies);
  595. return MAX_SCHEDULE_TIMEOUT;
  596. }