svclock.c 28 KB

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