svclock.c 18 KB

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