svclock.c 25 KB

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