svclock.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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;
  166. /* Create host handle for callback */
  167. host = nlmsvc_lookup_host(rqstp);
  168. if (host == NULL)
  169. return NULL;
  170. /* Allocate memory for block, and initialize arguments */
  171. if (!(block = (struct nlm_block *) kmalloc(sizeof(*block), GFP_KERNEL)))
  172. goto failed;
  173. memset(block, 0, sizeof(*block));
  174. locks_init_lock(&block->b_call.a_args.lock.fl);
  175. locks_init_lock(&block->b_call.a_res.lock.fl);
  176. kref_init(&block->b_count);
  177. if (!nlmsvc_setgrantargs(&block->b_call, lock))
  178. goto failed_free;
  179. /* Set notifier function for VFS, and init args */
  180. block->b_call.a_args.lock.fl.fl_flags |= FL_SLEEP;
  181. block->b_call.a_args.lock.fl.fl_lmops = &nlmsvc_lock_operations;
  182. block->b_call.a_args.cookie = *cookie; /* see above */
  183. dprintk("lockd: created block %p...\n", block);
  184. /* Create and initialize the block */
  185. block->b_daemon = rqstp->rq_server;
  186. block->b_host = host;
  187. block->b_file = file;
  188. /* Add to file's list of blocks */
  189. block->b_fnext = file->f_blocks;
  190. file->f_blocks = block;
  191. /* Set up RPC arguments for callback */
  192. call = &block->b_call;
  193. call->a_host = host;
  194. call->a_flags = RPC_TASK_ASYNC;
  195. return block;
  196. failed_free:
  197. kfree(block);
  198. failed:
  199. nlm_release_host(host);
  200. return NULL;
  201. }
  202. /*
  203. * Delete a block. If the lock was cancelled or the grant callback
  204. * failed, unlock is set to 1.
  205. * It is the caller's responsibility to check whether the file
  206. * can be closed hereafter.
  207. */
  208. static int nlmsvc_unlink_block(struct nlm_block *block)
  209. {
  210. int status;
  211. dprintk("lockd: unlinking block %p...\n", block);
  212. /* Remove block from list */
  213. status = posix_unblock_lock(block->b_file->f_file, &block->b_call.a_args.lock.fl);
  214. nlmsvc_remove_block(block);
  215. return status;
  216. }
  217. static void nlmsvc_free_block(struct kref *kref)
  218. {
  219. struct nlm_block *block = container_of(kref, struct nlm_block, b_count);
  220. struct nlm_file *file = block->b_file;
  221. struct nlm_block **bp;
  222. dprintk("lockd: freeing block %p...\n", block);
  223. /* Remove block from file's list of blocks */
  224. for (bp = &file->f_blocks; *bp; bp = &(*bp)->b_fnext) {
  225. if (*bp == block) {
  226. *bp = block->b_fnext;
  227. break;
  228. }
  229. }
  230. if (block->b_host)
  231. nlm_release_host(block->b_host);
  232. nlmsvc_freegrantargs(&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. nlmsvc_freegrantargs(call);
  279. return 0;
  280. }
  281. call->a_args.lock.oh.data = (u8 *) data;
  282. }
  283. memcpy(call->a_args.lock.oh.data, lock->oh.data, lock->oh.len);
  284. return 1;
  285. }
  286. static void nlmsvc_freegrantargs(struct nlm_rqst *call)
  287. {
  288. struct file_lock *fl = &call->a_args.lock.fl;
  289. /*
  290. * Check whether we allocated memory for the owner.
  291. */
  292. if (call->a_args.lock.oh.data != (u8 *) call->a_owner) {
  293. kfree(call->a_args.lock.oh.data);
  294. }
  295. if (fl->fl_ops && fl->fl_ops->fl_release_private)
  296. fl->fl_ops->fl_release_private(fl);
  297. if (fl->fl_lmops && fl->fl_lmops->fl_release_private)
  298. fl->fl_lmops->fl_release_private(fl);
  299. }
  300. /*
  301. * Attempt to establish a lock, and if it can't be granted, block it
  302. * if required.
  303. */
  304. u32
  305. nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file,
  306. struct nlm_lock *lock, int wait, struct nlm_cookie *cookie)
  307. {
  308. struct nlm_block *block, *newblock = NULL;
  309. int error;
  310. u32 ret;
  311. dprintk("lockd: nlmsvc_lock(%s/%ld, ty=%d, pi=%d, %Ld-%Ld, bl=%d)\n",
  312. file->f_file->f_dentry->d_inode->i_sb->s_id,
  313. file->f_file->f_dentry->d_inode->i_ino,
  314. lock->fl.fl_type, lock->fl.fl_pid,
  315. (long long)lock->fl.fl_start,
  316. (long long)lock->fl.fl_end,
  317. wait);
  318. lock->fl.fl_flags &= ~FL_SLEEP;
  319. again:
  320. /* Lock file against concurrent access */
  321. down(&file->f_sema);
  322. /* Get existing block (in case client is busy-waiting) */
  323. block = nlmsvc_lookup_block(file, lock, 0);
  324. if (block == NULL) {
  325. if (newblock != NULL)
  326. lock = &newblock->b_call.a_args.lock;
  327. } else
  328. lock = &block->b_call.a_args.lock;
  329. error = posix_lock_file(file->f_file, &lock->fl);
  330. lock->fl.fl_flags &= ~FL_SLEEP;
  331. dprintk("lockd: posix_lock_file returned %d\n", error);
  332. switch(error) {
  333. case 0:
  334. ret = nlm_granted;
  335. goto out;
  336. case -EAGAIN:
  337. break;
  338. case -EDEADLK:
  339. ret = nlm_deadlock;
  340. goto out;
  341. default: /* includes ENOLCK */
  342. ret = nlm_lck_denied_nolocks;
  343. goto out;
  344. }
  345. ret = nlm_lck_denied;
  346. if (!wait)
  347. goto out;
  348. ret = nlm_lck_blocked;
  349. if (block != NULL)
  350. goto out;
  351. /* If we don't have a block, create and initialize it. Then
  352. * retry because we may have slept in kmalloc. */
  353. /* We have to release f_sema as nlmsvc_create_block may try to
  354. * to claim it while doing host garbage collection */
  355. if (newblock == NULL) {
  356. up(&file->f_sema);
  357. dprintk("lockd: blocking on this lock (allocating).\n");
  358. if (!(newblock = nlmsvc_create_block(rqstp, file, lock, cookie)))
  359. return nlm_lck_denied_nolocks;
  360. goto again;
  361. }
  362. /* Append to list of blocked */
  363. nlmsvc_insert_block(newblock, NLM_NEVER);
  364. out:
  365. up(&file->f_sema);
  366. nlmsvc_release_block(newblock);
  367. nlmsvc_release_block(block);
  368. dprintk("lockd: nlmsvc_lock returned %u\n", ret);
  369. return ret;
  370. }
  371. /*
  372. * Test for presence of a conflicting lock.
  373. */
  374. u32
  375. nlmsvc_testlock(struct nlm_file *file, struct nlm_lock *lock,
  376. struct nlm_lock *conflock)
  377. {
  378. dprintk("lockd: nlmsvc_testlock(%s/%ld, ty=%d, %Ld-%Ld)\n",
  379. file->f_file->f_dentry->d_inode->i_sb->s_id,
  380. file->f_file->f_dentry->d_inode->i_ino,
  381. lock->fl.fl_type,
  382. (long long)lock->fl.fl_start,
  383. (long long)lock->fl.fl_end);
  384. if (posix_test_lock(file->f_file, &lock->fl, &conflock->fl)) {
  385. dprintk("lockd: conflicting lock(ty=%d, %Ld-%Ld)\n",
  386. conflock->fl.fl_type,
  387. (long long)conflock->fl.fl_start,
  388. (long long)conflock->fl.fl_end);
  389. conflock->caller = "somehost"; /* FIXME */
  390. conflock->oh.len = 0; /* don't return OH info */
  391. conflock->svid = conflock->fl.fl_pid;
  392. return nlm_lck_denied;
  393. }
  394. return nlm_granted;
  395. }
  396. /*
  397. * Remove a lock.
  398. * This implies a CANCEL call: We send a GRANT_MSG, the client replies
  399. * with a GRANT_RES call which gets lost, and calls UNLOCK immediately
  400. * afterwards. In this case the block will still be there, and hence
  401. * must be removed.
  402. */
  403. u32
  404. nlmsvc_unlock(struct nlm_file *file, struct nlm_lock *lock)
  405. {
  406. int error;
  407. dprintk("lockd: nlmsvc_unlock(%s/%ld, pi=%d, %Ld-%Ld)\n",
  408. file->f_file->f_dentry->d_inode->i_sb->s_id,
  409. file->f_file->f_dentry->d_inode->i_ino,
  410. lock->fl.fl_pid,
  411. (long long)lock->fl.fl_start,
  412. (long long)lock->fl.fl_end);
  413. /* First, cancel any lock that might be there */
  414. nlmsvc_cancel_blocked(file, lock);
  415. lock->fl.fl_type = F_UNLCK;
  416. error = posix_lock_file(file->f_file, &lock->fl);
  417. return (error < 0)? nlm_lck_denied_nolocks : nlm_granted;
  418. }
  419. /*
  420. * Cancel a previously blocked request.
  421. *
  422. * A cancel request always overrides any grant that may currently
  423. * be in progress.
  424. * The calling procedure must check whether the file can be closed.
  425. */
  426. u32
  427. nlmsvc_cancel_blocked(struct nlm_file *file, struct nlm_lock *lock)
  428. {
  429. struct nlm_block *block;
  430. int status = 0;
  431. dprintk("lockd: nlmsvc_cancel(%s/%ld, pi=%d, %Ld-%Ld)\n",
  432. file->f_file->f_dentry->d_inode->i_sb->s_id,
  433. file->f_file->f_dentry->d_inode->i_ino,
  434. lock->fl.fl_pid,
  435. (long long)lock->fl.fl_start,
  436. (long long)lock->fl.fl_end);
  437. down(&file->f_sema);
  438. if ((block = nlmsvc_lookup_block(file, lock, 1)) != NULL) {
  439. status = nlmsvc_unlink_block(block);
  440. nlmsvc_release_block(block);
  441. }
  442. up(&file->f_sema);
  443. return status ? nlm_lck_denied : nlm_granted;
  444. }
  445. /*
  446. * Unblock a blocked lock request. This is a callback invoked from the
  447. * VFS layer when a lock on which we blocked is removed.
  448. *
  449. * This function doesn't grant the blocked lock instantly, but rather moves
  450. * the block to the head of nlm_blocked where it can be picked up by lockd.
  451. */
  452. static void
  453. nlmsvc_notify_blocked(struct file_lock *fl)
  454. {
  455. struct nlm_block **bp, *block;
  456. dprintk("lockd: VFS unblock notification for block %p\n", fl);
  457. for (bp = &nlm_blocked; (block = *bp) != 0; bp = &block->b_next) {
  458. if (nlm_compare_locks(&block->b_call.a_args.lock.fl, fl)) {
  459. nlmsvc_insert_block(block, 0);
  460. svc_wake_up(block->b_daemon);
  461. return;
  462. }
  463. }
  464. printk(KERN_WARNING "lockd: notification for unknown block!\n");
  465. }
  466. static int nlmsvc_same_owner(struct file_lock *fl1, struct file_lock *fl2)
  467. {
  468. return fl1->fl_owner == fl2->fl_owner && fl1->fl_pid == fl2->fl_pid;
  469. }
  470. struct lock_manager_operations nlmsvc_lock_operations = {
  471. .fl_compare_owner = nlmsvc_same_owner,
  472. .fl_notify = nlmsvc_notify_blocked,
  473. };
  474. /*
  475. * Try to claim a lock that was previously blocked.
  476. *
  477. * Note that we use both the RPC_GRANTED_MSG call _and_ an async
  478. * RPC thread when notifying the client. This seems like overkill...
  479. * Here's why:
  480. * - we don't want to use a synchronous RPC thread, otherwise
  481. * we might find ourselves hanging on a dead portmapper.
  482. * - Some lockd implementations (e.g. HP) don't react to
  483. * RPC_GRANTED calls; they seem to insist on RPC_GRANTED_MSG calls.
  484. */
  485. static void
  486. nlmsvc_grant_blocked(struct nlm_block *block)
  487. {
  488. struct nlm_file *file = block->b_file;
  489. struct nlm_lock *lock = &block->b_call.a_args.lock;
  490. int error;
  491. dprintk("lockd: grant blocked lock %p\n", block);
  492. /* First thing is lock the file */
  493. down(&file->f_sema);
  494. /* Unlink block request from list */
  495. nlmsvc_unlink_block(block);
  496. /* If b_granted is true this means we've been here before.
  497. * Just retry the grant callback, possibly refreshing the RPC
  498. * binding */
  499. if (block->b_granted) {
  500. nlm_rebind_host(block->b_host);
  501. goto callback;
  502. }
  503. /* Try the lock operation again */
  504. lock->fl.fl_flags |= FL_SLEEP;
  505. error = posix_lock_file(file->f_file, &lock->fl);
  506. lock->fl.fl_flags &= ~FL_SLEEP;
  507. switch (error) {
  508. case 0:
  509. break;
  510. case -EAGAIN:
  511. dprintk("lockd: lock still blocked\n");
  512. nlmsvc_insert_block(block, NLM_NEVER);
  513. goto out_unlock;
  514. default:
  515. printk(KERN_WARNING "lockd: unexpected error %d in %s!\n",
  516. -error, __FUNCTION__);
  517. nlmsvc_insert_block(block, 10 * HZ);
  518. goto out_unlock;
  519. }
  520. callback:
  521. /* Lock was granted by VFS. */
  522. dprintk("lockd: GRANTing blocked lock.\n");
  523. block->b_granted = 1;
  524. /* Schedule next grant callback in 30 seconds */
  525. nlmsvc_insert_block(block, 30 * HZ);
  526. /* Call the client */
  527. kref_get(&block->b_count);
  528. if (nlmsvc_async_call(&block->b_call, NLMPROC_GRANTED_MSG,
  529. &nlmsvc_grant_ops) < 0)
  530. nlmsvc_release_block(block);
  531. out_unlock:
  532. up(&file->f_sema);
  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 = container_of(call, struct nlm_block, b_call);
  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 if (block->b_done) {
  555. /* Block already removed, kill it for real */
  556. timeout = 0;
  557. } else {
  558. /* Call was successful, now wait for client callback */
  559. timeout = 60 * HZ;
  560. }
  561. nlmsvc_insert_block(block, timeout);
  562. svc_wake_up(block->b_daemon);
  563. }
  564. void nlmsvc_grant_release(void *data)
  565. {
  566. nlmsvc_release_block(data);
  567. }
  568. static const struct rpc_call_ops nlmsvc_grant_ops = {
  569. .rpc_call_done = nlmsvc_grant_callback,
  570. .rpc_release = nlmsvc_grant_release,
  571. };
  572. /*
  573. * We received a GRANT_RES callback. Try to find the corresponding
  574. * block.
  575. */
  576. void
  577. nlmsvc_grant_reply(struct svc_rqst *rqstp, struct nlm_cookie *cookie, u32 status)
  578. {
  579. struct nlm_block *block;
  580. struct nlm_file *file;
  581. dprintk("grant_reply: looking for cookie %x, host (%08x), s=%d \n",
  582. *(unsigned int *)(cookie->data),
  583. ntohl(rqstp->rq_addr.sin_addr.s_addr), status);
  584. if (!(block = nlmsvc_find_block(cookie, &rqstp->rq_addr)))
  585. return;
  586. file = block->b_file;
  587. file->f_count++;
  588. down(&file->f_sema);
  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. up(&file->f_sema);
  600. nlm_release_file(file);
  601. nlmsvc_release_block(block);
  602. }
  603. /*
  604. * Retry all blocked locks that have been notified. This is where lockd
  605. * picks up locks that can be granted, or grant notifications that must
  606. * be retransmitted.
  607. */
  608. unsigned long
  609. nlmsvc_retry_blocked(void)
  610. {
  611. struct nlm_block *block;
  612. dprintk("nlmsvc_retry_blocked(%p, when=%ld)\n",
  613. nlm_blocked,
  614. nlm_blocked? nlm_blocked->b_when : 0);
  615. while ((block = nlm_blocked) != 0) {
  616. if (block->b_when == NLM_NEVER)
  617. break;
  618. if (time_after(block->b_when,jiffies))
  619. break;
  620. dprintk("nlmsvc_retry_blocked(%p, when=%ld, done=%d)\n",
  621. block, block->b_when, block->b_done);
  622. kref_get(&block->b_count);
  623. if (block->b_done)
  624. nlmsvc_unlink_block(block);
  625. else
  626. nlmsvc_grant_blocked(block);
  627. nlmsvc_release_block(block);
  628. }
  629. if ((block = nlm_blocked) && block->b_when != NLM_NEVER)
  630. return (block->b_when - jiffies);
  631. return MAX_SCHEDULE_TIMEOUT;
  632. }