svclock.c 26 KB

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