svclock.c 24 KB

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