svclock.c 24 KB

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