svclock.c 29 KB

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