svclock.c 19 KB

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