svclock.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  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. #ifdef LOCKD_DEBUG
  50. static const char *nlmdbg_cookie2a(const struct nlm_cookie *cookie)
  51. {
  52. /*
  53. * We can get away with a static buffer because we're only
  54. * called with BKL held.
  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. block->b_fl = NULL;
  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->b_fl);
  259. kfree(block);
  260. }
  261. static void nlmsvc_release_block(struct nlm_block *block)
  262. {
  263. if (block != NULL)
  264. kref_put_mutex(&block->b_count, nlmsvc_free_block, &block->b_file->f_mutex);
  265. }
  266. /*
  267. * Loop over all blocks and delete blocks held by
  268. * a matching host.
  269. */
  270. void nlmsvc_traverse_blocks(struct nlm_host *host,
  271. struct nlm_file *file,
  272. nlm_host_match_fn_t match)
  273. {
  274. struct nlm_block *block, *next;
  275. restart:
  276. mutex_lock(&file->f_mutex);
  277. list_for_each_entry_safe(block, next, &file->f_blocks, b_flist) {
  278. if (!match(block->b_host, host))
  279. continue;
  280. /* Do not destroy blocks that are not on
  281. * the global retry list - why? */
  282. if (list_empty(&block->b_list))
  283. continue;
  284. kref_get(&block->b_count);
  285. mutex_unlock(&file->f_mutex);
  286. nlmsvc_unlink_block(block);
  287. nlmsvc_release_block(block);
  288. goto restart;
  289. }
  290. mutex_unlock(&file->f_mutex);
  291. }
  292. /*
  293. * Initialize arguments for GRANTED call. The nlm_rqst structure
  294. * has been cleared already.
  295. */
  296. static int nlmsvc_setgrantargs(struct nlm_rqst *call, struct nlm_lock *lock)
  297. {
  298. locks_copy_lock(&call->a_args.lock.fl, &lock->fl);
  299. memcpy(&call->a_args.lock.fh, &lock->fh, sizeof(call->a_args.lock.fh));
  300. call->a_args.lock.caller = utsname()->nodename;
  301. call->a_args.lock.oh.len = lock->oh.len;
  302. /* set default data area */
  303. call->a_args.lock.oh.data = call->a_owner;
  304. call->a_args.lock.svid = lock->fl.fl_pid;
  305. if (lock->oh.len > NLMCLNT_OHSIZE) {
  306. void *data = kmalloc(lock->oh.len, GFP_KERNEL);
  307. if (!data)
  308. return 0;
  309. call->a_args.lock.oh.data = (u8 *) data;
  310. }
  311. memcpy(call->a_args.lock.oh.data, lock->oh.data, lock->oh.len);
  312. return 1;
  313. }
  314. static void nlmsvc_freegrantargs(struct nlm_rqst *call)
  315. {
  316. if (call->a_args.lock.oh.data != call->a_owner)
  317. kfree(call->a_args.lock.oh.data);
  318. locks_release_private(&call->a_args.lock.fl);
  319. }
  320. /*
  321. * Deferred lock request handling for non-blocking lock
  322. */
  323. static __be32
  324. nlmsvc_defer_lock_rqst(struct svc_rqst *rqstp, struct nlm_block *block)
  325. {
  326. __be32 status = nlm_lck_denied_nolocks;
  327. block->b_flags |= B_QUEUED;
  328. nlmsvc_insert_block(block, NLM_TIMEOUT);
  329. block->b_cache_req = &rqstp->rq_chandle;
  330. if (rqstp->rq_chandle.defer) {
  331. block->b_deferred_req =
  332. rqstp->rq_chandle.defer(block->b_cache_req);
  333. if (block->b_deferred_req != NULL)
  334. status = nlm_drop_reply;
  335. }
  336. dprintk("lockd: nlmsvc_defer_lock_rqst block %p flags %d status %d\n",
  337. block, block->b_flags, ntohl(status));
  338. return status;
  339. }
  340. /*
  341. * Attempt to establish a lock, and if it can't be granted, block it
  342. * if required.
  343. */
  344. __be32
  345. nlmsvc_lock(struct svc_rqst *rqstp, struct nlm_file *file,
  346. struct nlm_host *host, struct nlm_lock *lock, int wait,
  347. struct nlm_cookie *cookie, int reclaim)
  348. {
  349. struct nlm_block *block = NULL;
  350. int error;
  351. __be32 ret;
  352. dprintk("lockd: nlmsvc_lock(%s/%ld, ty=%d, pi=%d, %Ld-%Ld, bl=%d)\n",
  353. file_inode(file->f_file)->i_sb->s_id,
  354. file_inode(file->f_file)->i_ino,
  355. lock->fl.fl_type, lock->fl.fl_pid,
  356. (long long)lock->fl.fl_start,
  357. (long long)lock->fl.fl_end,
  358. wait);
  359. /* Lock file against concurrent access */
  360. mutex_lock(&file->f_mutex);
  361. /* Get existing block (in case client is busy-waiting)
  362. * or create new block
  363. */
  364. block = nlmsvc_lookup_block(file, lock);
  365. if (block == NULL) {
  366. block = nlmsvc_create_block(rqstp, host, file, lock, cookie);
  367. ret = nlm_lck_denied_nolocks;
  368. if (block == NULL)
  369. goto out;
  370. lock = &block->b_call->a_args.lock;
  371. } else
  372. lock->fl.fl_flags &= ~FL_SLEEP;
  373. if (block->b_flags & B_QUEUED) {
  374. dprintk("lockd: nlmsvc_lock deferred block %p flags %d\n",
  375. block, block->b_flags);
  376. if (block->b_granted) {
  377. nlmsvc_unlink_block(block);
  378. ret = nlm_granted;
  379. goto out;
  380. }
  381. if (block->b_flags & B_TIMED_OUT) {
  382. nlmsvc_unlink_block(block);
  383. ret = nlm_lck_denied;
  384. goto out;
  385. }
  386. ret = nlm_drop_reply;
  387. goto out;
  388. }
  389. if (locks_in_grace(SVC_NET(rqstp)) && !reclaim) {
  390. ret = nlm_lck_denied_grace_period;
  391. goto out;
  392. }
  393. if (reclaim && !locks_in_grace(SVC_NET(rqstp))) {
  394. ret = nlm_lck_denied_grace_period;
  395. goto out;
  396. }
  397. if (!wait)
  398. lock->fl.fl_flags &= ~FL_SLEEP;
  399. error = vfs_lock_file(file->f_file, F_SETLK, &lock->fl, NULL);
  400. lock->fl.fl_flags &= ~FL_SLEEP;
  401. dprintk("lockd: vfs_lock_file returned %d\n", error);
  402. switch (error) {
  403. case 0:
  404. ret = nlm_granted;
  405. goto out;
  406. case -EAGAIN:
  407. /*
  408. * If this is a blocking request for an
  409. * already pending lock request then we need
  410. * to put it back on lockd's block list
  411. */
  412. if (wait)
  413. break;
  414. ret = nlm_lck_denied;
  415. goto out;
  416. case FILE_LOCK_DEFERRED:
  417. if (wait)
  418. break;
  419. /* Filesystem lock operation is in progress
  420. Add it to the queue waiting for callback */
  421. ret = nlmsvc_defer_lock_rqst(rqstp, block);
  422. goto out;
  423. case -EDEADLK:
  424. ret = nlm_deadlock;
  425. goto out;
  426. default: /* includes ENOLCK */
  427. ret = nlm_lck_denied_nolocks;
  428. goto out;
  429. }
  430. ret = nlm_lck_blocked;
  431. /* Append to list of blocked */
  432. nlmsvc_insert_block(block, NLM_NEVER);
  433. out:
  434. mutex_unlock(&file->f_mutex);
  435. nlmsvc_release_block(block);
  436. dprintk("lockd: nlmsvc_lock returned %u\n", ret);
  437. return ret;
  438. }
  439. /*
  440. * Test for presence of a conflicting lock.
  441. */
  442. __be32
  443. nlmsvc_testlock(struct svc_rqst *rqstp, struct nlm_file *file,
  444. struct nlm_host *host, struct nlm_lock *lock,
  445. struct nlm_lock *conflock, struct nlm_cookie *cookie)
  446. {
  447. struct nlm_block *block = NULL;
  448. int error;
  449. __be32 ret;
  450. dprintk("lockd: nlmsvc_testlock(%s/%ld, ty=%d, %Ld-%Ld)\n",
  451. file_inode(file->f_file)->i_sb->s_id,
  452. file_inode(file->f_file)->i_ino,
  453. lock->fl.fl_type,
  454. (long long)lock->fl.fl_start,
  455. (long long)lock->fl.fl_end);
  456. /* Get existing block (in case client is busy-waiting) */
  457. block = nlmsvc_lookup_block(file, lock);
  458. if (block == NULL) {
  459. struct file_lock *conf = kzalloc(sizeof(*conf), GFP_KERNEL);
  460. if (conf == NULL)
  461. return nlm_granted;
  462. block = nlmsvc_create_block(rqstp, host, file, lock, cookie);
  463. if (block == NULL) {
  464. kfree(conf);
  465. return nlm_granted;
  466. }
  467. block->b_fl = conf;
  468. }
  469. if (block->b_flags & B_QUEUED) {
  470. dprintk("lockd: nlmsvc_testlock deferred block %p flags %d fl %p\n",
  471. block, block->b_flags, block->b_fl);
  472. if (block->b_flags & B_TIMED_OUT) {
  473. nlmsvc_unlink_block(block);
  474. ret = nlm_lck_denied;
  475. goto out;
  476. }
  477. if (block->b_flags & B_GOT_CALLBACK) {
  478. nlmsvc_unlink_block(block);
  479. if (block->b_fl != NULL
  480. && block->b_fl->fl_type != F_UNLCK) {
  481. lock->fl = *block->b_fl;
  482. goto conf_lock;
  483. } else {
  484. ret = nlm_granted;
  485. goto out;
  486. }
  487. }
  488. ret = nlm_drop_reply;
  489. goto out;
  490. }
  491. if (locks_in_grace(SVC_NET(rqstp))) {
  492. ret = nlm_lck_denied_grace_period;
  493. goto out;
  494. }
  495. error = vfs_test_lock(file->f_file, &lock->fl);
  496. if (error == FILE_LOCK_DEFERRED) {
  497. ret = nlmsvc_defer_lock_rqst(rqstp, block);
  498. goto out;
  499. }
  500. if (error) {
  501. ret = nlm_lck_denied_nolocks;
  502. goto out;
  503. }
  504. if (lock->fl.fl_type == F_UNLCK) {
  505. ret = nlm_granted;
  506. goto out;
  507. }
  508. conf_lock:
  509. dprintk("lockd: conflicting lock(ty=%d, %Ld-%Ld)\n",
  510. lock->fl.fl_type, (long long)lock->fl.fl_start,
  511. (long long)lock->fl.fl_end);
  512. conflock->caller = "somehost"; /* FIXME */
  513. conflock->len = strlen(conflock->caller);
  514. conflock->oh.len = 0; /* don't return OH info */
  515. conflock->svid = lock->fl.fl_pid;
  516. conflock->fl.fl_type = lock->fl.fl_type;
  517. conflock->fl.fl_start = lock->fl.fl_start;
  518. conflock->fl.fl_end = lock->fl.fl_end;
  519. ret = nlm_lck_denied;
  520. out:
  521. if (block)
  522. nlmsvc_release_block(block);
  523. return ret;
  524. }
  525. /*
  526. * Remove a lock.
  527. * This implies a CANCEL call: We send a GRANT_MSG, the client replies
  528. * with a GRANT_RES call which gets lost, and calls UNLOCK immediately
  529. * afterwards. In this case the block will still be there, and hence
  530. * must be removed.
  531. */
  532. __be32
  533. nlmsvc_unlock(struct net *net, struct nlm_file *file, struct nlm_lock *lock)
  534. {
  535. int error;
  536. dprintk("lockd: nlmsvc_unlock(%s/%ld, pi=%d, %Ld-%Ld)\n",
  537. file_inode(file->f_file)->i_sb->s_id,
  538. file_inode(file->f_file)->i_ino,
  539. lock->fl.fl_pid,
  540. (long long)lock->fl.fl_start,
  541. (long long)lock->fl.fl_end);
  542. /* First, cancel any lock that might be there */
  543. nlmsvc_cancel_blocked(net, file, lock);
  544. lock->fl.fl_type = F_UNLCK;
  545. error = vfs_lock_file(file->f_file, F_SETLK, &lock->fl, NULL);
  546. return (error < 0)? nlm_lck_denied_nolocks : nlm_granted;
  547. }
  548. /*
  549. * Cancel a previously blocked request.
  550. *
  551. * A cancel request always overrides any grant that may currently
  552. * be in progress.
  553. * The calling procedure must check whether the file can be closed.
  554. */
  555. __be32
  556. nlmsvc_cancel_blocked(struct net *net, struct nlm_file *file, struct nlm_lock *lock)
  557. {
  558. struct nlm_block *block;
  559. int status = 0;
  560. dprintk("lockd: nlmsvc_cancel(%s/%ld, pi=%d, %Ld-%Ld)\n",
  561. file_inode(file->f_file)->i_sb->s_id,
  562. file_inode(file->f_file)->i_ino,
  563. lock->fl.fl_pid,
  564. (long long)lock->fl.fl_start,
  565. (long long)lock->fl.fl_end);
  566. if (locks_in_grace(net))
  567. return nlm_lck_denied_grace_period;
  568. mutex_lock(&file->f_mutex);
  569. block = nlmsvc_lookup_block(file, lock);
  570. mutex_unlock(&file->f_mutex);
  571. if (block != NULL) {
  572. vfs_cancel_lock(block->b_file->f_file,
  573. &block->b_call->a_args.lock.fl);
  574. status = nlmsvc_unlink_block(block);
  575. nlmsvc_release_block(block);
  576. }
  577. return status ? nlm_lck_denied : nlm_granted;
  578. }
  579. /*
  580. * This is a callback from the filesystem for VFS file lock requests.
  581. * It will be used if lm_grant is defined and the filesystem can not
  582. * respond to the request immediately.
  583. * For GETLK request it will copy the reply to the nlm_block.
  584. * For SETLK or SETLKW request it will get the local posix lock.
  585. * In all cases it will move the block to the head of nlm_blocked q where
  586. * nlmsvc_retry_blocked() can send back a reply for SETLKW or revisit the
  587. * deferred rpc for GETLK and SETLK.
  588. */
  589. static void
  590. nlmsvc_update_deferred_block(struct nlm_block *block, struct file_lock *conf,
  591. int result)
  592. {
  593. block->b_flags |= B_GOT_CALLBACK;
  594. if (result == 0)
  595. block->b_granted = 1;
  596. else
  597. block->b_flags |= B_TIMED_OUT;
  598. if (conf) {
  599. if (block->b_fl)
  600. __locks_copy_lock(block->b_fl, conf);
  601. }
  602. }
  603. static int nlmsvc_grant_deferred(struct file_lock *fl, struct file_lock *conf,
  604. int result)
  605. {
  606. struct nlm_block *block;
  607. int rc = -ENOENT;
  608. spin_lock(&nlm_blocked_lock);
  609. list_for_each_entry(block, &nlm_blocked, b_list) {
  610. if (nlm_compare_locks(&block->b_call->a_args.lock.fl, fl)) {
  611. dprintk("lockd: nlmsvc_notify_blocked block %p flags %d\n",
  612. block, block->b_flags);
  613. if (block->b_flags & B_QUEUED) {
  614. if (block->b_flags & B_TIMED_OUT) {
  615. rc = -ENOLCK;
  616. break;
  617. }
  618. nlmsvc_update_deferred_block(block, conf, result);
  619. } else if (result == 0)
  620. block->b_granted = 1;
  621. nlmsvc_insert_block_locked(block, 0);
  622. svc_wake_up(block->b_daemon);
  623. rc = 0;
  624. break;
  625. }
  626. }
  627. spin_unlock(&nlm_blocked_lock);
  628. if (rc == -ENOENT)
  629. printk(KERN_WARNING "lockd: grant for unknown block\n");
  630. return rc;
  631. }
  632. /*
  633. * Unblock a blocked lock request. This is a callback invoked from the
  634. * VFS layer when a lock on which we blocked is removed.
  635. *
  636. * This function doesn't grant the blocked lock instantly, but rather moves
  637. * the block to the head of nlm_blocked where it can be picked up by lockd.
  638. */
  639. static void
  640. nlmsvc_notify_blocked(struct file_lock *fl)
  641. {
  642. struct nlm_block *block;
  643. dprintk("lockd: VFS unblock notification for block %p\n", fl);
  644. spin_lock(&nlm_blocked_lock);
  645. list_for_each_entry(block, &nlm_blocked, b_list) {
  646. if (nlm_compare_locks(&block->b_call->a_args.lock.fl, fl)) {
  647. nlmsvc_insert_block_locked(block, 0);
  648. spin_unlock(&nlm_blocked_lock);
  649. svc_wake_up(block->b_daemon);
  650. return;
  651. }
  652. }
  653. spin_unlock(&nlm_blocked_lock);
  654. printk(KERN_WARNING "lockd: notification for unknown block!\n");
  655. }
  656. static int nlmsvc_same_owner(struct file_lock *fl1, struct file_lock *fl2)
  657. {
  658. return fl1->fl_owner == fl2->fl_owner && fl1->fl_pid == fl2->fl_pid;
  659. }
  660. /*
  661. * Since NLM uses two "keys" for tracking locks, we need to hash them down
  662. * to one for the blocked_hash. Here, we're just xor'ing the host address
  663. * with the pid in order to create a key value for picking a hash bucket.
  664. */
  665. static unsigned long
  666. nlmsvc_owner_key(struct file_lock *fl)
  667. {
  668. return (unsigned long)fl->fl_owner ^ (unsigned long)fl->fl_pid;
  669. }
  670. const struct lock_manager_operations nlmsvc_lock_operations = {
  671. .lm_compare_owner = nlmsvc_same_owner,
  672. .lm_owner_key = nlmsvc_owner_key,
  673. .lm_notify = nlmsvc_notify_blocked,
  674. .lm_grant = nlmsvc_grant_deferred,
  675. };
  676. /*
  677. * Try to claim a lock that was previously blocked.
  678. *
  679. * Note that we use both the RPC_GRANTED_MSG call _and_ an async
  680. * RPC thread when notifying the client. This seems like overkill...
  681. * Here's why:
  682. * - we don't want to use a synchronous RPC thread, otherwise
  683. * we might find ourselves hanging on a dead portmapper.
  684. * - Some lockd implementations (e.g. HP) don't react to
  685. * RPC_GRANTED calls; they seem to insist on RPC_GRANTED_MSG calls.
  686. */
  687. static void
  688. nlmsvc_grant_blocked(struct nlm_block *block)
  689. {
  690. struct nlm_file *file = block->b_file;
  691. struct nlm_lock *lock = &block->b_call->a_args.lock;
  692. int error;
  693. dprintk("lockd: grant blocked lock %p\n", block);
  694. kref_get(&block->b_count);
  695. /* Unlink block request from list */
  696. nlmsvc_unlink_block(block);
  697. /* If b_granted is true this means we've been here before.
  698. * Just retry the grant callback, possibly refreshing the RPC
  699. * binding */
  700. if (block->b_granted) {
  701. nlm_rebind_host(block->b_host);
  702. goto callback;
  703. }
  704. /* Try the lock operation again */
  705. lock->fl.fl_flags |= FL_SLEEP;
  706. error = vfs_lock_file(file->f_file, F_SETLK, &lock->fl, NULL);
  707. lock->fl.fl_flags &= ~FL_SLEEP;
  708. switch (error) {
  709. case 0:
  710. break;
  711. case FILE_LOCK_DEFERRED:
  712. dprintk("lockd: lock still blocked error %d\n", error);
  713. nlmsvc_insert_block(block, NLM_NEVER);
  714. nlmsvc_release_block(block);
  715. return;
  716. default:
  717. printk(KERN_WARNING "lockd: unexpected error %d in %s!\n",
  718. -error, __func__);
  719. nlmsvc_insert_block(block, 10 * HZ);
  720. nlmsvc_release_block(block);
  721. return;
  722. }
  723. callback:
  724. /* Lock was granted by VFS. */
  725. dprintk("lockd: GRANTing blocked lock.\n");
  726. block->b_granted = 1;
  727. /* keep block on the list, but don't reattempt until the RPC
  728. * completes or the submission fails
  729. */
  730. nlmsvc_insert_block(block, NLM_NEVER);
  731. /* Call the client -- use a soft RPC task since nlmsvc_retry_blocked
  732. * will queue up a new one if this one times out
  733. */
  734. error = nlm_async_call(block->b_call, NLMPROC_GRANTED_MSG,
  735. &nlmsvc_grant_ops);
  736. /* RPC submission failed, wait a bit and retry */
  737. if (error < 0)
  738. nlmsvc_insert_block(block, 10 * HZ);
  739. }
  740. /*
  741. * This is the callback from the RPC layer when the NLM_GRANTED_MSG
  742. * RPC call has succeeded or timed out.
  743. * Like all RPC callbacks, it is invoked by the rpciod process, so it
  744. * better not sleep. Therefore, we put the blocked lock on the nlm_blocked
  745. * chain once more in order to have it removed by lockd itself (which can
  746. * then sleep on the file semaphore without disrupting e.g. the nfs client).
  747. */
  748. static void nlmsvc_grant_callback(struct rpc_task *task, void *data)
  749. {
  750. struct nlm_rqst *call = data;
  751. struct nlm_block *block = call->a_block;
  752. unsigned long timeout;
  753. dprintk("lockd: GRANT_MSG RPC callback\n");
  754. spin_lock(&nlm_blocked_lock);
  755. /* if the block is not on a list at this point then it has
  756. * been invalidated. Don't try to requeue it.
  757. *
  758. * FIXME: it's possible that the block is removed from the list
  759. * after this check but before the nlmsvc_insert_block. In that
  760. * case it will be added back. Perhaps we need better locking
  761. * for nlm_blocked?
  762. */
  763. if (list_empty(&block->b_list))
  764. goto out;
  765. /* Technically, we should down the file semaphore here. Since we
  766. * move the block towards the head of the queue only, no harm
  767. * can be done, though. */
  768. if (task->tk_status < 0) {
  769. /* RPC error: Re-insert for retransmission */
  770. timeout = 10 * HZ;
  771. } else {
  772. /* Call was successful, now wait for client callback */
  773. timeout = 60 * HZ;
  774. }
  775. nlmsvc_insert_block_locked(block, timeout);
  776. svc_wake_up(block->b_daemon);
  777. out:
  778. spin_unlock(&nlm_blocked_lock);
  779. }
  780. /*
  781. * FIXME: nlmsvc_release_block() grabs a mutex. This is not allowed for an
  782. * .rpc_release rpc_call_op
  783. */
  784. static void nlmsvc_grant_release(void *data)
  785. {
  786. struct nlm_rqst *call = data;
  787. nlmsvc_release_block(call->a_block);
  788. }
  789. static const struct rpc_call_ops nlmsvc_grant_ops = {
  790. .rpc_call_done = nlmsvc_grant_callback,
  791. .rpc_release = nlmsvc_grant_release,
  792. };
  793. /*
  794. * We received a GRANT_RES callback. Try to find the corresponding
  795. * block.
  796. */
  797. void
  798. nlmsvc_grant_reply(struct nlm_cookie *cookie, __be32 status)
  799. {
  800. struct nlm_block *block;
  801. dprintk("grant_reply: looking for cookie %x, s=%d \n",
  802. *(unsigned int *)(cookie->data), status);
  803. if (!(block = nlmsvc_find_block(cookie)))
  804. return;
  805. if (block) {
  806. if (status == nlm_lck_denied_grace_period) {
  807. /* Try again in a couple of seconds */
  808. nlmsvc_insert_block(block, 10 * HZ);
  809. } else {
  810. /* Lock is now held by client, or has been rejected.
  811. * In both cases, the block should be removed. */
  812. nlmsvc_unlink_block(block);
  813. }
  814. }
  815. nlmsvc_release_block(block);
  816. }
  817. /* Helper function to handle retry of a deferred block.
  818. * If it is a blocking lock, call grant_blocked.
  819. * For a non-blocking lock or test lock, revisit the request.
  820. */
  821. static void
  822. retry_deferred_block(struct nlm_block *block)
  823. {
  824. if (!(block->b_flags & B_GOT_CALLBACK))
  825. block->b_flags |= B_TIMED_OUT;
  826. nlmsvc_insert_block(block, NLM_TIMEOUT);
  827. dprintk("revisit block %p flags %d\n", block, block->b_flags);
  828. if (block->b_deferred_req) {
  829. block->b_deferred_req->revisit(block->b_deferred_req, 0);
  830. block->b_deferred_req = NULL;
  831. }
  832. }
  833. /*
  834. * Retry all blocked locks that have been notified. This is where lockd
  835. * picks up locks that can be granted, or grant notifications that must
  836. * be retransmitted.
  837. */
  838. unsigned long
  839. nlmsvc_retry_blocked(void)
  840. {
  841. unsigned long timeout = MAX_SCHEDULE_TIMEOUT;
  842. struct nlm_block *block;
  843. spin_lock(&nlm_blocked_lock);
  844. while (!list_empty(&nlm_blocked) && !kthread_should_stop()) {
  845. block = list_entry(nlm_blocked.next, struct nlm_block, b_list);
  846. if (block->b_when == NLM_NEVER)
  847. break;
  848. if (time_after(block->b_when, jiffies)) {
  849. timeout = block->b_when - jiffies;
  850. break;
  851. }
  852. spin_unlock(&nlm_blocked_lock);
  853. dprintk("nlmsvc_retry_blocked(%p, when=%ld)\n",
  854. block, block->b_when);
  855. if (block->b_flags & B_QUEUED) {
  856. dprintk("nlmsvc_retry_blocked delete block (%p, granted=%d, flags=%d)\n",
  857. block, block->b_granted, block->b_flags);
  858. retry_deferred_block(block);
  859. } else
  860. nlmsvc_grant_blocked(block);
  861. spin_lock(&nlm_blocked_lock);
  862. }
  863. spin_unlock(&nlm_blocked_lock);
  864. return timeout;
  865. }