svclock.c 25 KB

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