svclock.c 24 KB

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