svclock.c 27 KB

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