svclock.c 19 KB

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