svclock.c 19 KB

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