svclock.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  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/slab.h>
  24. #include <linux/errno.h>
  25. #include <linux/kernel.h>
  26. #include <linux/sched.h>
  27. #include <linux/sunrpc/clnt.h>
  28. #include <linux/sunrpc/svc_xprt.h>
  29. #include <linux/lockd/nlm.h>
  30. #include <linux/lockd/lockd.h>
  31. #include <linux/kthread.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 void 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 LIST_HEAD(nlm_blocked);
  48. static DEFINE_SPINLOCK(nlm_blocked_lock);
  49. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  50. static const char *nlmdbg_cookie2a(const struct nlm_cookie *cookie)
  51. {
  52. /*
  53. * We can get away with a static buffer because this is only called
  54. * from lockd, which is single-threaded.
  55. */
  56. static char buf[2*NLM_MAXCOOKIELEN+1];
  57. unsigned int i, len = sizeof(buf);
  58. char *p = buf;
  59. len--; /* allow for trailing \0 */
  60. if (len < 3)
  61. return "???";
  62. for (i = 0 ; i < cookie->len ; i++) {
  63. if (len < 2) {
  64. strcpy(p-3, "...");
  65. break;
  66. }
  67. sprintf(p, "%02x", cookie->data[i]);
  68. p += 2;
  69. len -= 2;
  70. }
  71. *p = '\0';
  72. return buf;
  73. }
  74. #endif
  75. /*
  76. * Insert a blocked lock into the global list
  77. */
  78. static void
  79. nlmsvc_insert_block_locked(struct nlm_block *block, unsigned long when)
  80. {
  81. struct nlm_block *b;
  82. struct list_head *pos;
  83. dprintk("lockd: nlmsvc_insert_block(%p, %ld)\n", block, when);
  84. if (list_empty(&block->b_list)) {
  85. kref_get(&block->b_count);
  86. } else {
  87. list_del_init(&block->b_list);
  88. }
  89. pos = &nlm_blocked;
  90. if (when != NLM_NEVER) {
  91. if ((when += jiffies) == NLM_NEVER)
  92. when ++;
  93. list_for_each(pos, &nlm_blocked) {
  94. b = list_entry(pos, struct nlm_block, b_list);
  95. if (time_after(b->b_when,when) || b->b_when == NLM_NEVER)
  96. break;
  97. }
  98. /* On normal exit from the loop, pos == &nlm_blocked,
  99. * so we will be adding to the end of the list - good
  100. */
  101. }
  102. list_add_tail(&block->b_list, pos);
  103. block->b_when = when;
  104. }
  105. static void nlmsvc_insert_block(struct nlm_block *block, unsigned long when)
  106. {
  107. spin_lock(&nlm_blocked_lock);
  108. nlmsvc_insert_block_locked(block, when);
  109. spin_unlock(&nlm_blocked_lock);
  110. }
  111. /*
  112. * Remove a block from the global list
  113. */
  114. static inline void
  115. nlmsvc_remove_block(struct nlm_block *block)
  116. {
  117. if (!list_empty(&block->b_list)) {
  118. spin_lock(&nlm_blocked_lock);
  119. list_del_init(&block->b_list);
  120. spin_unlock(&nlm_blocked_lock);
  121. nlmsvc_release_block(block);
  122. }
  123. }
  124. /*
  125. * Find a block for a given lock
  126. */
  127. static struct nlm_block *
  128. nlmsvc_lookup_block(struct nlm_file *file, struct nlm_lock *lock)
  129. {
  130. struct nlm_block *block;
  131. struct file_lock *fl;
  132. dprintk("lockd: nlmsvc_lookup_block f=%p pd=%d %Ld-%Ld ty=%d\n",
  133. file, lock->fl.fl_pid,
  134. (long long)lock->fl.fl_start,
  135. (long long)lock->fl.fl_end, lock->fl.fl_type);
  136. list_for_each_entry(block, &nlm_blocked, b_list) {
  137. fl = &block->b_call->a_args.lock.fl;
  138. dprintk("lockd: check f=%p pd=%d %Ld-%Ld ty=%d cookie=%s\n",
  139. block->b_file, fl->fl_pid,
  140. (long long)fl->fl_start,
  141. (long long)fl->fl_end, fl->fl_type,
  142. nlmdbg_cookie2a(&block->b_call->a_args.cookie));
  143. if (block->b_file == file && nlm_compare_locks(fl, &lock->fl)) {
  144. kref_get(&block->b_count);
  145. return block;
  146. }
  147. }
  148. return NULL;
  149. }
  150. static inline int nlm_cookie_match(struct nlm_cookie *a, struct nlm_cookie *b)
  151. {
  152. if (a->len != b->len)
  153. return 0;
  154. if (memcmp(a->data, b->data, a->len))
  155. return 0;
  156. return 1;
  157. }
  158. /*
  159. * Find a block with a given NLM cookie.
  160. */
  161. static inline struct nlm_block *
  162. nlmsvc_find_block(struct nlm_cookie *cookie)
  163. {
  164. struct nlm_block *block;
  165. list_for_each_entry(block, &nlm_blocked, b_list) {
  166. if (nlm_cookie_match(&block->b_call->a_args.cookie,cookie))
  167. goto found;
  168. }
  169. return NULL;
  170. found:
  171. dprintk("nlmsvc_find_block(%s): block=%p\n", nlmdbg_cookie2a(cookie), block);
  172. kref_get(&block->b_count);
  173. return block;
  174. }
  175. /*
  176. * Create a block and initialize it.
  177. *
  178. * Note: we explicitly set the cookie of the grant reply to that of
  179. * the blocked lock request. The spec explicitly mentions that the client
  180. * should _not_ rely on the callback containing the same cookie as the
  181. * request, but (as I found out later) that's because some implementations
  182. * do just this. Never mind the standards comittees, they support our
  183. * logging industries.
  184. *
  185. * 10 years later: I hope we can safely ignore these old and broken
  186. * clients by now. Let's fix this so we can uniquely identify an incoming
  187. * GRANTED_RES message by cookie, without having to rely on the client's IP
  188. * address. --okir
  189. */
  190. static struct nlm_block *
  191. nlmsvc_create_block(struct svc_rqst *rqstp, struct nlm_host *host,
  192. struct nlm_file *file, struct nlm_lock *lock,
  193. struct nlm_cookie *cookie)
  194. {
  195. struct nlm_block *block;
  196. struct nlm_rqst *call = NULL;
  197. call = nlm_alloc_call(host);
  198. if (call == NULL)
  199. return NULL;
  200. /* Allocate memory for block, and initialize arguments */
  201. block = kzalloc(sizeof(*block), GFP_KERNEL);
  202. if (block == NULL)
  203. goto failed;
  204. kref_init(&block->b_count);
  205. INIT_LIST_HEAD(&block->b_list);
  206. INIT_LIST_HEAD(&block->b_flist);
  207. if (!nlmsvc_setgrantargs(call, lock))
  208. goto failed_free;
  209. /* Set notifier function for VFS, and init args */
  210. call->a_args.lock.fl.fl_flags |= FL_SLEEP;
  211. call->a_args.lock.fl.fl_lmops = &nlmsvc_lock_operations;
  212. nlmclnt_next_cookie(&call->a_args.cookie);
  213. dprintk("lockd: created block %p...\n", block);
  214. /* Create and initialize the block */
  215. block->b_daemon = rqstp->rq_server;
  216. block->b_host = host;
  217. block->b_file = file;
  218. file->f_count++;
  219. /* Add to file's list of blocks */
  220. list_add(&block->b_flist, &file->f_blocks);
  221. /* Set up RPC arguments for callback */
  222. block->b_call = call;
  223. call->a_flags = RPC_TASK_ASYNC;
  224. call->a_block = block;
  225. return block;
  226. failed_free:
  227. kfree(block);
  228. failed:
  229. nlmsvc_release_call(call);
  230. return NULL;
  231. }
  232. /*
  233. * Delete a block.
  234. * It is the caller's responsibility to check whether the file
  235. * can be closed hereafter.
  236. */
  237. static int nlmsvc_unlink_block(struct nlm_block *block)
  238. {
  239. int status;
  240. dprintk("lockd: unlinking block %p...\n", block);
  241. /* Remove block from list */
  242. status = posix_unblock_lock(&block->b_call->a_args.lock.fl);
  243. nlmsvc_remove_block(block);
  244. return status;
  245. }
  246. static void nlmsvc_free_block(struct kref *kref)
  247. {
  248. struct nlm_block *block = container_of(kref, struct nlm_block, b_count);
  249. struct nlm_file *file = block->b_file;
  250. dprintk("lockd: freeing block %p...\n", block);
  251. /* Remove block from file's list of blocks */
  252. list_del_init(&block->b_flist);
  253. mutex_unlock(&file->f_mutex);
  254. nlmsvc_freegrantargs(block->b_call);
  255. nlmsvc_release_call(block->b_call);
  256. nlm_release_file(block->b_file);
  257. kfree(block);
  258. }
  259. static void nlmsvc_release_block(struct nlm_block *block)
  260. {
  261. if (block != NULL)
  262. kref_put_mutex(&block->b_count, nlmsvc_free_block, &block->b_file->f_mutex);
  263. }
  264. /*
  265. * Loop over all blocks and delete blocks held by
  266. * a matching host.
  267. */
  268. void nlmsvc_traverse_blocks(struct nlm_host *host,
  269. struct nlm_file *file,
  270. nlm_host_match_fn_t match)
  271. {
  272. struct nlm_block *block, *next;
  273. restart:
  274. mutex_lock(&file->f_mutex);
  275. list_for_each_entry_safe(block, next, &file->f_blocks, b_flist) {
  276. if (!match(block->b_host, host))
  277. continue;
  278. /* Do not destroy blocks that are not on
  279. * the global retry list - why? */
  280. if (list_empty(&block->b_list))
  281. continue;
  282. kref_get(&block->b_count);
  283. mutex_unlock(&file->f_mutex);
  284. nlmsvc_unlink_block(block);
  285. nlmsvc_release_block(block);
  286. goto restart;
  287. }
  288. mutex_unlock(&file->f_mutex);
  289. }
  290. /*
  291. * Initialize arguments for GRANTED call. The nlm_rqst structure
  292. * has been cleared already.
  293. */
  294. static int nlmsvc_setgrantargs(struct nlm_rqst *call, struct nlm_lock *lock)
  295. {
  296. locks_copy_lock(&call->a_args.lock.fl, &lock->fl);
  297. memcpy(&call->a_args.lock.fh, &lock->fh, sizeof(call->a_args.lock.fh));
  298. call->a_args.lock.caller = utsname()->nodename;
  299. call->a_args.lock.oh.len = lock->oh.len;
  300. /* set default data area */
  301. call->a_args.lock.oh.data = call->a_owner;
  302. call->a_args.lock.svid = lock->fl.fl_pid;
  303. if (lock->oh.len > NLMCLNT_OHSIZE) {
  304. void *data = kmalloc(lock->oh.len, GFP_KERNEL);
  305. if (!data)
  306. return 0;
  307. call->a_args.lock.oh.data = (u8 *) data;
  308. }
  309. memcpy(call->a_args.lock.oh.data, lock->oh.data, lock->oh.len);
  310. return 1;
  311. }
  312. static void nlmsvc_freegrantargs(struct nlm_rqst *call)
  313. {
  314. if (call->a_args.lock.oh.data != call->a_owner)
  315. kfree(call->a_args.lock.oh.data);
  316. locks_release_private(&call->a_args.lock.fl);
  317. }
  318. /*
  319. * Deferred lock request handling for non-blocking lock
  320. */
  321. static __be32
  322. nlmsvc_defer_lock_rqst(struct svc_rqst *rqstp, struct nlm_block *block)
  323. {
  324. __be32 status = nlm_lck_denied_nolocks;
  325. block->b_flags |= B_QUEUED;
  326. nlmsvc_insert_block(block, NLM_TIMEOUT);
  327. block->b_cache_req = &rqstp->rq_chandle;
  328. if (rqstp->rq_chandle.defer) {
  329. block->b_deferred_req =
  330. rqstp->rq_chandle.defer(block->b_cache_req);
  331. if (block->b_deferred_req != NULL)
  332. status = nlm_drop_reply;
  333. }
  334. dprintk("lockd: nlmsvc_defer_lock_rqst block %p flags %d status %d\n",
  335. block, block->b_flags, ntohl(status));
  336. return status;
  337. }
  338. /*
  339. * Attempt to establish a lock, and if it can't be granted, block it
  340. * if required.
  341. */
  342. __be32
  343. nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file,
  344. struct nlm_host *host, struct nlm_lock *lock, int wait,
  345. struct nlm_cookie *cookie, int reclaim)
  346. {
  347. struct nlm_block *block = NULL;
  348. int error;
  349. __be32 ret;
  350. dprintk("lockd: nlmsvc_lock(%s/%ld, ty=%d, pi=%d, %Ld-%Ld, bl=%d)\n",
  351. file_inode(file->f_file)->i_sb->s_id,
  352. file_inode(file->f_file)->i_ino,
  353. lock->fl.fl_type, lock->fl.fl_pid,
  354. (long long)lock->fl.fl_start,
  355. (long long)lock->fl.fl_end,
  356. wait);
  357. /* Lock file against concurrent access */
  358. mutex_lock(&file->f_mutex);
  359. /* Get existing block (in case client is busy-waiting)
  360. * or create new block
  361. */
  362. block = nlmsvc_lookup_block(file, lock);
  363. if (block == NULL) {
  364. block = nlmsvc_create_block(rqstp, host, file, lock, cookie);
  365. ret = nlm_lck_denied_nolocks;
  366. if (block == NULL)
  367. goto out;
  368. lock = &block->b_call->a_args.lock;
  369. } else
  370. lock->fl.fl_flags &= ~FL_SLEEP;
  371. if (block->b_flags & B_QUEUED) {
  372. dprintk("lockd: nlmsvc_lock deferred block %p flags %d\n",
  373. block, block->b_flags);
  374. if (block->b_granted) {
  375. nlmsvc_unlink_block(block);
  376. ret = nlm_granted;
  377. goto out;
  378. }
  379. if (block->b_flags & B_TIMED_OUT) {
  380. nlmsvc_unlink_block(block);
  381. ret = nlm_lck_denied;
  382. goto out;
  383. }
  384. ret = nlm_drop_reply;
  385. goto out;
  386. }
  387. if (locks_in_grace(SVC_NET(rqstp)) && !reclaim) {
  388. ret = nlm_lck_denied_grace_period;
  389. goto out;
  390. }
  391. if (reclaim && !locks_in_grace(SVC_NET(rqstp))) {
  392. ret = nlm_lck_denied_grace_period;
  393. goto out;
  394. }
  395. if (!wait)
  396. lock->fl.fl_flags &= ~FL_SLEEP;
  397. error = vfs_lock_file(file->f_file, F_SETLK, &lock->fl, NULL);
  398. lock->fl.fl_flags &= ~FL_SLEEP;
  399. dprintk("lockd: vfs_lock_file returned %d\n", error);
  400. switch (error) {
  401. case 0:
  402. ret = nlm_granted;
  403. goto out;
  404. case -EAGAIN:
  405. /*
  406. * If this is a blocking request for an
  407. * already pending lock request then we need
  408. * to put it back on lockd's block list
  409. */
  410. if (wait)
  411. break;
  412. ret = nlm_lck_denied;
  413. goto out;
  414. case FILE_LOCK_DEFERRED:
  415. if (wait)
  416. break;
  417. /* Filesystem lock operation is in progress
  418. Add it to the queue waiting for callback */
  419. ret = nlmsvc_defer_lock_rqst(rqstp, block);
  420. goto out;
  421. case -EDEADLK:
  422. ret = nlm_deadlock;
  423. goto out;
  424. default: /* includes ENOLCK */
  425. ret = nlm_lck_denied_nolocks;
  426. goto out;
  427. }
  428. ret = nlm_lck_blocked;
  429. /* Append to list of blocked */
  430. nlmsvc_insert_block(block, NLM_NEVER);
  431. out:
  432. mutex_unlock(&file->f_mutex);
  433. nlmsvc_release_block(block);
  434. dprintk("lockd: nlmsvc_lock returned %u\n", ret);
  435. return ret;
  436. }
  437. /*
  438. * Test for presence of a conflicting lock.
  439. */
  440. __be32
  441. nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file,
  442. struct nlm_host *host, struct nlm_lock *lock,
  443. struct nlm_lock *conflock, struct nlm_cookie *cookie)
  444. {
  445. int error;
  446. __be32 ret;
  447. dprintk("lockd: nlmsvc_testlock(%s/%ld, ty=%d, %Ld-%Ld)\n",
  448. file_inode(file->f_file)->i_sb->s_id,
  449. file_inode(file->f_file)->i_ino,
  450. lock->fl.fl_type,
  451. (long long)lock->fl.fl_start,
  452. (long long)lock->fl.fl_end);
  453. if (locks_in_grace(SVC_NET(rqstp))) {
  454. ret = nlm_lck_denied_grace_period;
  455. goto out;
  456. }
  457. error = vfs_test_lock(file->f_file, &lock->fl);
  458. if (error) {
  459. /* We can't currently deal with deferred test requests */
  460. if (error == FILE_LOCK_DEFERRED)
  461. WARN_ON_ONCE(1);
  462. ret = nlm_lck_denied_nolocks;
  463. goto out;
  464. }
  465. if (lock->fl.fl_type == F_UNLCK) {
  466. ret = nlm_granted;
  467. goto out;
  468. }
  469. dprintk("lockd: conflicting lock(ty=%d, %Ld-%Ld)\n",
  470. lock->fl.fl_type, (long long)lock->fl.fl_start,
  471. (long long)lock->fl.fl_end);
  472. conflock->caller = "somehost"; /* FIXME */
  473. conflock->len = strlen(conflock->caller);
  474. conflock->oh.len = 0; /* don't return OH info */
  475. conflock->svid = lock->fl.fl_pid;
  476. conflock->fl.fl_type = lock->fl.fl_type;
  477. conflock->fl.fl_start = lock->fl.fl_start;
  478. conflock->fl.fl_end = lock->fl.fl_end;
  479. locks_release_private(&lock->fl);
  480. ret = nlm_lck_denied;
  481. out:
  482. return ret;
  483. }
  484. /*
  485. * Remove a lock.
  486. * This implies a CANCEL call: We send a GRANT_MSG, the client replies
  487. * with a GRANT_RES call which gets lost, and calls UNLOCK immediately
  488. * afterwards. In this case the block will still be there, and hence
  489. * must be removed.
  490. */
  491. __be32
  492. nlmsvc_unlock(struct net *net, struct nlm_file *file, struct nlm_lock *lock)
  493. {
  494. int error;
  495. dprintk("lockd: nlmsvc_unlock(%s/%ld, pi=%d, %Ld-%Ld)\n",
  496. file_inode(file->f_file)->i_sb->s_id,
  497. file_inode(file->f_file)->i_ino,
  498. lock->fl.fl_pid,
  499. (long long)lock->fl.fl_start,
  500. (long long)lock->fl.fl_end);
  501. /* First, cancel any lock that might be there */
  502. nlmsvc_cancel_blocked(net, file, lock);
  503. lock->fl.fl_type = F_UNLCK;
  504. error = vfs_lock_file(file->f_file, F_SETLK, &lock->fl, NULL);
  505. return (error < 0)? nlm_lck_denied_nolocks : nlm_granted;
  506. }
  507. /*
  508. * Cancel a previously blocked request.
  509. *
  510. * A cancel request always overrides any grant that may currently
  511. * be in progress.
  512. * The calling procedure must check whether the file can be closed.
  513. */
  514. __be32
  515. nlmsvc_cancel_blocked(struct net *net, struct nlm_file *file, struct nlm_lock *lock)
  516. {
  517. struct nlm_block *block;
  518. int status = 0;
  519. dprintk("lockd: nlmsvc_cancel(%s/%ld, pi=%d, %Ld-%Ld)\n",
  520. file_inode(file->f_file)->i_sb->s_id,
  521. file_inode(file->f_file)->i_ino,
  522. lock->fl.fl_pid,
  523. (long long)lock->fl.fl_start,
  524. (long long)lock->fl.fl_end);
  525. if (locks_in_grace(net))
  526. return nlm_lck_denied_grace_period;
  527. mutex_lock(&file->f_mutex);
  528. block = nlmsvc_lookup_block(file, lock);
  529. mutex_unlock(&file->f_mutex);
  530. if (block != NULL) {
  531. vfs_cancel_lock(block->b_file->f_file,
  532. &block->b_call->a_args.lock.fl);
  533. status = nlmsvc_unlink_block(block);
  534. nlmsvc_release_block(block);
  535. }
  536. return status ? nlm_lck_denied : nlm_granted;
  537. }
  538. /*
  539. * This is a callback from the filesystem for VFS file lock requests.
  540. * It will be used if lm_grant is defined and the filesystem can not
  541. * respond to the request immediately.
  542. * For SETLK or SETLKW request it will get the local posix lock.
  543. * In all cases it will move the block to the head of nlm_blocked q where
  544. * nlmsvc_retry_blocked() can send back a reply for SETLKW or revisit the
  545. * deferred rpc for GETLK and SETLK.
  546. */
  547. static void
  548. nlmsvc_update_deferred_block(struct nlm_block *block, int result)
  549. {
  550. block->b_flags |= B_GOT_CALLBACK;
  551. if (result == 0)
  552. block->b_granted = 1;
  553. else
  554. block->b_flags |= B_TIMED_OUT;
  555. }
  556. static int nlmsvc_grant_deferred(struct file_lock *fl, int result)
  557. {
  558. struct nlm_block *block;
  559. int rc = -ENOENT;
  560. spin_lock(&nlm_blocked_lock);
  561. list_for_each_entry(block, &nlm_blocked, b_list) {
  562. if (nlm_compare_locks(&block->b_call->a_args.lock.fl, fl)) {
  563. dprintk("lockd: nlmsvc_notify_blocked block %p flags %d\n",
  564. block, block->b_flags);
  565. if (block->b_flags & B_QUEUED) {
  566. if (block->b_flags & B_TIMED_OUT) {
  567. rc = -ENOLCK;
  568. break;
  569. }
  570. nlmsvc_update_deferred_block(block, result);
  571. } else if (result == 0)
  572. block->b_granted = 1;
  573. nlmsvc_insert_block_locked(block, 0);
  574. svc_wake_up(block->b_daemon);
  575. rc = 0;
  576. break;
  577. }
  578. }
  579. spin_unlock(&nlm_blocked_lock);
  580. if (rc == -ENOENT)
  581. printk(KERN_WARNING "lockd: grant for unknown block\n");
  582. return rc;
  583. }
  584. /*
  585. * Unblock a blocked lock request. This is a callback invoked from the
  586. * VFS layer when a lock on which we blocked is removed.
  587. *
  588. * This function doesn't grant the blocked lock instantly, but rather moves
  589. * the block to the head of nlm_blocked where it can be picked up by lockd.
  590. */
  591. static void
  592. nlmsvc_notify_blocked(struct file_lock *fl)
  593. {
  594. struct nlm_block *block;
  595. dprintk("lockd: VFS unblock notification for block %p\n", fl);
  596. spin_lock(&nlm_blocked_lock);
  597. list_for_each_entry(block, &nlm_blocked, b_list) {
  598. if (nlm_compare_locks(&block->b_call->a_args.lock.fl, fl)) {
  599. nlmsvc_insert_block_locked(block, 0);
  600. spin_unlock(&nlm_blocked_lock);
  601. svc_wake_up(block->b_daemon);
  602. return;
  603. }
  604. }
  605. spin_unlock(&nlm_blocked_lock);
  606. printk(KERN_WARNING "lockd: notification for unknown block!\n");
  607. }
  608. static int nlmsvc_same_owner(struct file_lock *fl1, struct file_lock *fl2)
  609. {
  610. return fl1->fl_owner == fl2->fl_owner && fl1->fl_pid == fl2->fl_pid;
  611. }
  612. /*
  613. * Since NLM uses two "keys" for tracking locks, we need to hash them down
  614. * to one for the blocked_hash. Here, we're just xor'ing the host address
  615. * with the pid in order to create a key value for picking a hash bucket.
  616. */
  617. static unsigned long
  618. nlmsvc_owner_key(struct file_lock *fl)
  619. {
  620. return (unsigned long)fl->fl_owner ^ (unsigned long)fl->fl_pid;
  621. }
  622. const struct lock_manager_operations nlmsvc_lock_operations = {
  623. .lm_compare_owner = nlmsvc_same_owner,
  624. .lm_owner_key = nlmsvc_owner_key,
  625. .lm_notify = nlmsvc_notify_blocked,
  626. .lm_grant = nlmsvc_grant_deferred,
  627. };
  628. /*
  629. * Try to claim a lock that was previously blocked.
  630. *
  631. * Note that we use both the RPC_GRANTED_MSG call _and_ an async
  632. * RPC thread when notifying the client. This seems like overkill...
  633. * Here's why:
  634. * - we don't want to use a synchronous RPC thread, otherwise
  635. * we might find ourselves hanging on a dead portmapper.
  636. * - Some lockd implementations (e.g. HP) don't react to
  637. * RPC_GRANTED calls; they seem to insist on RPC_GRANTED_MSG calls.
  638. */
  639. static void
  640. nlmsvc_grant_blocked(struct nlm_block *block)
  641. {
  642. struct nlm_file *file = block->b_file;
  643. struct nlm_lock *lock = &block->b_call->a_args.lock;
  644. int error;
  645. loff_t fl_start, fl_end;
  646. dprintk("lockd: grant blocked lock %p\n", block);
  647. kref_get(&block->b_count);
  648. /* Unlink block request from list */
  649. nlmsvc_unlink_block(block);
  650. /* If b_granted is true this means we've been here before.
  651. * Just retry the grant callback, possibly refreshing the RPC
  652. * binding */
  653. if (block->b_granted) {
  654. nlm_rebind_host(block->b_host);
  655. goto callback;
  656. }
  657. /* Try the lock operation again */
  658. /* vfs_lock_file() can mangle fl_start and fl_end, but we need
  659. * them unchanged for the GRANT_MSG
  660. */
  661. lock->fl.fl_flags |= FL_SLEEP;
  662. fl_start = lock->fl.fl_start;
  663. fl_end = lock->fl.fl_end;
  664. error = vfs_lock_file(file->f_file, F_SETLK, &lock->fl, NULL);
  665. lock->fl.fl_flags &= ~FL_SLEEP;
  666. lock->fl.fl_start = fl_start;
  667. lock->fl.fl_end = fl_end;
  668. switch (error) {
  669. case 0:
  670. break;
  671. case FILE_LOCK_DEFERRED:
  672. dprintk("lockd: lock still blocked error %d\n", error);
  673. nlmsvc_insert_block(block, NLM_NEVER);
  674. nlmsvc_release_block(block);
  675. return;
  676. default:
  677. printk(KERN_WARNING "lockd: unexpected error %d in %s!\n",
  678. -error, __func__);
  679. nlmsvc_insert_block(block, 10 * HZ);
  680. nlmsvc_release_block(block);
  681. return;
  682. }
  683. callback:
  684. /* Lock was granted by VFS. */
  685. dprintk("lockd: GRANTing blocked lock.\n");
  686. block->b_granted = 1;
  687. /* keep block on the list, but don't reattempt until the RPC
  688. * completes or the submission fails
  689. */
  690. nlmsvc_insert_block(block, NLM_NEVER);
  691. /* Call the client -- use a soft RPC task since nlmsvc_retry_blocked
  692. * will queue up a new one if this one times out
  693. */
  694. error = nlm_async_call(block->b_call, NLMPROC_GRANTED_MSG,
  695. &nlmsvc_grant_ops);
  696. /* RPC submission failed, wait a bit and retry */
  697. if (error < 0)
  698. nlmsvc_insert_block(block, 10 * HZ);
  699. }
  700. /*
  701. * This is the callback from the RPC layer when the NLM_GRANTED_MSG
  702. * RPC call has succeeded or timed out.
  703. * Like all RPC callbacks, it is invoked by the rpciod process, so it
  704. * better not sleep. Therefore, we put the blocked lock on the nlm_blocked
  705. * chain once more in order to have it removed by lockd itself (which can
  706. * then sleep on the file semaphore without disrupting e.g. the nfs client).
  707. */
  708. static void nlmsvc_grant_callback(struct rpc_task *task, void *data)
  709. {
  710. struct nlm_rqst *call = data;
  711. struct nlm_block *block = call->a_block;
  712. unsigned long timeout;
  713. dprintk("lockd: GRANT_MSG RPC callback\n");
  714. spin_lock(&nlm_blocked_lock);
  715. /* if the block is not on a list at this point then it has
  716. * been invalidated. Don't try to requeue it.
  717. *
  718. * FIXME: it's possible that the block is removed from the list
  719. * after this check but before the nlmsvc_insert_block. In that
  720. * case it will be added back. Perhaps we need better locking
  721. * for nlm_blocked?
  722. */
  723. if (list_empty(&block->b_list))
  724. goto out;
  725. /* Technically, we should down the file semaphore here. Since we
  726. * move the block towards the head of the queue only, no harm
  727. * can be done, though. */
  728. if (task->tk_status < 0) {
  729. /* RPC error: Re-insert for retransmission */
  730. timeout = 10 * HZ;
  731. } else {
  732. /* Call was successful, now wait for client callback */
  733. timeout = 60 * HZ;
  734. }
  735. nlmsvc_insert_block_locked(block, timeout);
  736. svc_wake_up(block->b_daemon);
  737. out:
  738. spin_unlock(&nlm_blocked_lock);
  739. }
  740. /*
  741. * FIXME: nlmsvc_release_block() grabs a mutex. This is not allowed for an
  742. * .rpc_release rpc_call_op
  743. */
  744. static void nlmsvc_grant_release(void *data)
  745. {
  746. struct nlm_rqst *call = data;
  747. nlmsvc_release_block(call->a_block);
  748. }
  749. static const struct rpc_call_ops nlmsvc_grant_ops = {
  750. .rpc_call_done = nlmsvc_grant_callback,
  751. .rpc_release = nlmsvc_grant_release,
  752. };
  753. /*
  754. * We received a GRANT_RES callback. Try to find the corresponding
  755. * block.
  756. */
  757. void
  758. nlmsvc_grant_reply(struct nlm_cookie *cookie, __be32 status)
  759. {
  760. struct nlm_block *block;
  761. dprintk("grant_reply: looking for cookie %x, s=%d \n",
  762. *(unsigned int *)(cookie->data), status);
  763. if (!(block = nlmsvc_find_block(cookie)))
  764. return;
  765. if (block) {
  766. if (status == nlm_lck_denied_grace_period) {
  767. /* Try again in a couple of seconds */
  768. nlmsvc_insert_block(block, 10 * HZ);
  769. } else {
  770. /* Lock is now held by client, or has been rejected.
  771. * In both cases, the block should be removed. */
  772. nlmsvc_unlink_block(block);
  773. }
  774. }
  775. nlmsvc_release_block(block);
  776. }
  777. /* Helper function to handle retry of a deferred block.
  778. * If it is a blocking lock, call grant_blocked.
  779. * For a non-blocking lock or test lock, revisit the request.
  780. */
  781. static void
  782. retry_deferred_block(struct nlm_block *block)
  783. {
  784. if (!(block->b_flags & B_GOT_CALLBACK))
  785. block->b_flags |= B_TIMED_OUT;
  786. nlmsvc_insert_block(block, NLM_TIMEOUT);
  787. dprintk("revisit block %p flags %d\n", block, block->b_flags);
  788. if (block->b_deferred_req) {
  789. block->b_deferred_req->revisit(block->b_deferred_req, 0);
  790. block->b_deferred_req = NULL;
  791. }
  792. }
  793. /*
  794. * Retry all blocked locks that have been notified. This is where lockd
  795. * picks up locks that can be granted, or grant notifications that must
  796. * be retransmitted.
  797. */
  798. unsigned long
  799. nlmsvc_retry_blocked(void)
  800. {
  801. unsigned long timeout = MAX_SCHEDULE_TIMEOUT;
  802. struct nlm_block *block;
  803. spin_lock(&nlm_blocked_lock);
  804. while (!list_empty(&nlm_blocked) && !kthread_should_stop()) {
  805. block = list_entry(nlm_blocked.next, struct nlm_block, b_list);
  806. if (block->b_when == NLM_NEVER)
  807. break;
  808. if (time_after(block->b_when, jiffies)) {
  809. timeout = block->b_when - jiffies;
  810. break;
  811. }
  812. spin_unlock(&nlm_blocked_lock);
  813. dprintk("nlmsvc_retry_blocked(%p, when=%ld)\n",
  814. block, block->b_when);
  815. if (block->b_flags & B_QUEUED) {
  816. dprintk("nlmsvc_retry_blocked delete block (%p, granted=%d, flags=%d)\n",
  817. block, block->b_granted, block->b_flags);
  818. retry_deferred_block(block);
  819. } else
  820. nlmsvc_grant_blocked(block);
  821. spin_lock(&nlm_blocked_lock);
  822. }
  823. spin_unlock(&nlm_blocked_lock);
  824. return timeout;
  825. }