svclock.c 26 KB

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