svclock.c 19 KB

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