svclock.c 27 KB

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