clntproc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/fs/lockd/clntproc.c
  4. *
  5. * RPC procedures for the client side NLM implementation
  6. *
  7. * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
  8. */
  9. #include <linux/module.h>
  10. #include <linux/slab.h>
  11. #include <linux/types.h>
  12. #include <linux/errno.h>
  13. #include <linux/fs.h>
  14. #include <linux/filelock.h>
  15. #include <linux/nfs_fs.h>
  16. #include <linux/utsname.h>
  17. #include <linux/freezer.h>
  18. #include <linux/sunrpc/clnt.h>
  19. #include <linux/sunrpc/svc.h>
  20. #include <linux/lockd/lockd.h>
  21. #include "trace.h"
  22. #define NLMDBG_FACILITY NLMDBG_CLIENT
  23. #define NLMCLNT_GRACE_WAIT (5*HZ)
  24. #define NLMCLNT_POLL_TIMEOUT (30*HZ)
  25. #define NLMCLNT_MAX_RETRIES 3
  26. static int nlmclnt_test(struct nlm_rqst *, struct file_lock *);
  27. static int nlmclnt_lock(struct nlm_rqst *, struct file_lock *);
  28. static int nlmclnt_unlock(struct nlm_rqst *, struct file_lock *);
  29. static int nlm_stat_to_errno(__be32 stat);
  30. static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host);
  31. static int nlmclnt_cancel(struct nlm_host *, int , struct file_lock *);
  32. static const struct rpc_call_ops nlmclnt_unlock_ops;
  33. static const struct rpc_call_ops nlmclnt_cancel_ops;
  34. /*
  35. * Cookie counter for NLM requests
  36. */
  37. static atomic_t nlm_cookie = ATOMIC_INIT(0x1234);
  38. void nlmclnt_next_cookie(struct nlm_cookie *c)
  39. {
  40. u32 cookie = atomic_inc_return(&nlm_cookie);
  41. memcpy(c->data, &cookie, 4);
  42. c->len=4;
  43. }
  44. static struct nlm_lockowner *
  45. nlmclnt_get_lockowner(struct nlm_lockowner *lockowner)
  46. {
  47. refcount_inc(&lockowner->count);
  48. return lockowner;
  49. }
  50. static void nlmclnt_put_lockowner(struct nlm_lockowner *lockowner)
  51. {
  52. if (!refcount_dec_and_lock(&lockowner->count, &lockowner->host->h_lock))
  53. return;
  54. list_del(&lockowner->list);
  55. spin_unlock(&lockowner->host->h_lock);
  56. nlmclnt_release_host(lockowner->host);
  57. kfree(lockowner);
  58. }
  59. static inline int nlm_pidbusy(struct nlm_host *host, uint32_t pid)
  60. {
  61. struct nlm_lockowner *lockowner;
  62. list_for_each_entry(lockowner, &host->h_lockowners, list) {
  63. if (lockowner->pid == pid)
  64. return -EBUSY;
  65. }
  66. return 0;
  67. }
  68. static inline uint32_t __nlm_alloc_pid(struct nlm_host *host)
  69. {
  70. uint32_t res;
  71. do {
  72. res = host->h_pidcount++;
  73. } while (nlm_pidbusy(host, res) < 0);
  74. return res;
  75. }
  76. static struct nlm_lockowner *__nlmclnt_find_lockowner(struct nlm_host *host, fl_owner_t owner)
  77. {
  78. struct nlm_lockowner *lockowner;
  79. list_for_each_entry(lockowner, &host->h_lockowners, list) {
  80. if (lockowner->owner != owner)
  81. continue;
  82. return nlmclnt_get_lockowner(lockowner);
  83. }
  84. return NULL;
  85. }
  86. static struct nlm_lockowner *nlmclnt_find_lockowner(struct nlm_host *host, fl_owner_t owner)
  87. {
  88. struct nlm_lockowner *res, *new = NULL;
  89. spin_lock(&host->h_lock);
  90. res = __nlmclnt_find_lockowner(host, owner);
  91. if (res == NULL) {
  92. spin_unlock(&host->h_lock);
  93. new = kmalloc_obj(*new);
  94. spin_lock(&host->h_lock);
  95. res = __nlmclnt_find_lockowner(host, owner);
  96. if (res == NULL && new != NULL) {
  97. res = new;
  98. refcount_set(&new->count, 1);
  99. new->owner = owner;
  100. new->pid = __nlm_alloc_pid(host);
  101. new->host = nlm_get_host(host);
  102. list_add(&new->list, &host->h_lockowners);
  103. new = NULL;
  104. }
  105. }
  106. spin_unlock(&host->h_lock);
  107. kfree(new);
  108. return res;
  109. }
  110. /*
  111. * Initialize arguments for TEST/LOCK/UNLOCK/CANCEL calls
  112. */
  113. static void nlmclnt_setlockargs(struct nlm_rqst *req, struct file_lock *fl)
  114. {
  115. struct nlm_args *argp = &req->a_args;
  116. struct nlm_lock *lock = &argp->lock;
  117. char *nodename = req->a_host->h_rpcclnt->cl_nodename;
  118. nlmclnt_next_cookie(&argp->cookie);
  119. memcpy(&lock->fh, NFS_FH(file_inode(fl->c.flc_file)),
  120. sizeof(struct nfs_fh));
  121. lock->caller = nodename;
  122. lock->oh.data = req->a_owner;
  123. lock->oh.len = snprintf(req->a_owner, sizeof(req->a_owner), "%u@%s",
  124. (unsigned int)fl->fl_u.nfs_fl.owner->pid,
  125. nodename);
  126. lock->svid = fl->fl_u.nfs_fl.owner->pid;
  127. lock->fl.fl_start = fl->fl_start;
  128. lock->fl.fl_end = fl->fl_end;
  129. lock->fl.c.flc_type = fl->c.flc_type;
  130. }
  131. static void nlmclnt_release_lockargs(struct nlm_rqst *req)
  132. {
  133. WARN_ON_ONCE(req->a_args.lock.fl.fl_ops != NULL);
  134. }
  135. /**
  136. * nlmclnt_proc - Perform a single client-side lock request
  137. * @host: address of a valid nlm_host context representing the NLM server
  138. * @cmd: fcntl-style file lock operation to perform
  139. * @fl: address of arguments for the lock operation
  140. * @data: address of data to be sent to callback operations
  141. *
  142. */
  143. int nlmclnt_proc(struct nlm_host *host, int cmd, struct file_lock *fl, void *data)
  144. {
  145. struct nlm_rqst *call;
  146. int status;
  147. const struct nlmclnt_operations *nlmclnt_ops = host->h_nlmclnt_ops;
  148. call = nlm_alloc_call(host);
  149. if (call == NULL)
  150. return -ENOMEM;
  151. if (nlmclnt_ops && nlmclnt_ops->nlmclnt_alloc_call)
  152. nlmclnt_ops->nlmclnt_alloc_call(data);
  153. nlmclnt_locks_init_private(fl, host);
  154. if (!fl->fl_u.nfs_fl.owner) {
  155. /* lockowner allocation has failed */
  156. nlmclnt_release_call(call);
  157. return -ENOMEM;
  158. }
  159. /* Set up the argument struct */
  160. nlmclnt_setlockargs(call, fl);
  161. call->a_callback_data = data;
  162. if (IS_SETLK(cmd) || IS_SETLKW(cmd)) {
  163. if (fl->c.flc_type != F_UNLCK) {
  164. call->a_args.block = IS_SETLKW(cmd) ? 1 : 0;
  165. status = nlmclnt_lock(call, fl);
  166. } else
  167. status = nlmclnt_unlock(call, fl);
  168. } else if (IS_GETLK(cmd))
  169. status = nlmclnt_test(call, fl);
  170. else
  171. status = -EINVAL;
  172. fl->fl_ops->fl_release_private(fl);
  173. fl->fl_ops = NULL;
  174. dprintk("lockd: clnt proc returns %d\n", status);
  175. return status;
  176. }
  177. EXPORT_SYMBOL_GPL(nlmclnt_proc);
  178. /*
  179. * Allocate an NLM RPC call struct
  180. */
  181. struct nlm_rqst *nlm_alloc_call(struct nlm_host *host)
  182. {
  183. struct nlm_rqst *call;
  184. for(;;) {
  185. call = kzalloc_obj(*call);
  186. if (call != NULL) {
  187. refcount_set(&call->a_count, 1);
  188. locks_init_lock(&call->a_args.lock.fl);
  189. locks_init_lock(&call->a_res.lock.fl);
  190. call->a_host = nlm_get_host(host);
  191. return call;
  192. }
  193. if (signalled())
  194. break;
  195. printk("nlm_alloc_call: failed, waiting for memory\n");
  196. schedule_timeout_interruptible(5*HZ);
  197. }
  198. return NULL;
  199. }
  200. void nlmclnt_release_call(struct nlm_rqst *call)
  201. {
  202. const struct nlmclnt_operations *nlmclnt_ops = call->a_host->h_nlmclnt_ops;
  203. if (!refcount_dec_and_test(&call->a_count))
  204. return;
  205. if (nlmclnt_ops && nlmclnt_ops->nlmclnt_release_call)
  206. nlmclnt_ops->nlmclnt_release_call(call->a_callback_data);
  207. nlmclnt_release_host(call->a_host);
  208. nlmclnt_release_lockargs(call);
  209. kfree(call);
  210. }
  211. static void nlmclnt_rpc_release(void *data)
  212. {
  213. nlmclnt_release_call(data);
  214. }
  215. static int nlm_wait_on_grace(wait_queue_head_t *queue)
  216. {
  217. DEFINE_WAIT(wait);
  218. int status = -EINTR;
  219. prepare_to_wait(queue, &wait, TASK_INTERRUPTIBLE);
  220. if (!signalled ()) {
  221. schedule_timeout(NLMCLNT_GRACE_WAIT);
  222. try_to_freeze();
  223. if (!signalled ())
  224. status = 0;
  225. }
  226. finish_wait(queue, &wait);
  227. return status;
  228. }
  229. /*
  230. * Generic NLM call
  231. */
  232. static int
  233. nlmclnt_call(const struct cred *cred, struct nlm_rqst *req, u32 proc)
  234. {
  235. struct nlm_host *host = req->a_host;
  236. struct rpc_clnt *clnt;
  237. struct nlm_args *argp = &req->a_args;
  238. struct nlm_res *resp = &req->a_res;
  239. struct rpc_message msg = {
  240. .rpc_argp = argp,
  241. .rpc_resp = resp,
  242. .rpc_cred = cred,
  243. };
  244. int status;
  245. dprintk("lockd: call procedure %d on %s\n",
  246. (int)proc, host->h_name);
  247. do {
  248. if (host->h_reclaiming && !argp->reclaim)
  249. goto in_grace_period;
  250. /* If we have no RPC client yet, create one. */
  251. if ((clnt = nlm_bind_host(host)) == NULL)
  252. return -ENOLCK;
  253. msg.rpc_proc = &clnt->cl_procinfo[proc];
  254. /* Perform the RPC call. If an error occurs, try again */
  255. if ((status = rpc_call_sync(clnt, &msg, 0)) < 0) {
  256. dprintk("lockd: rpc_call returned error %d\n", -status);
  257. switch (status) {
  258. case -EPROTONOSUPPORT:
  259. status = -EINVAL;
  260. break;
  261. case -ECONNREFUSED:
  262. case -ETIMEDOUT:
  263. case -ENOTCONN:
  264. nlm_rebind_host(host);
  265. status = -EAGAIN;
  266. break;
  267. case -ERESTARTSYS:
  268. return signalled () ? -EINTR : status;
  269. default:
  270. break;
  271. }
  272. break;
  273. } else
  274. if (resp->status == nlm_lck_denied_grace_period) {
  275. dprintk("lockd: server in grace period\n");
  276. if (argp->reclaim) {
  277. printk(KERN_WARNING
  278. "lockd: spurious grace period reject?!\n");
  279. return -ENOLCK;
  280. }
  281. } else {
  282. if (!argp->reclaim) {
  283. /* We appear to be out of the grace period */
  284. wake_up_all(&host->h_gracewait);
  285. }
  286. dprintk("lockd: server returns status %d\n",
  287. ntohl(resp->status));
  288. return 0; /* Okay, call complete */
  289. }
  290. in_grace_period:
  291. /*
  292. * The server has rebooted and appears to be in the grace
  293. * period during which locks are only allowed to be
  294. * reclaimed.
  295. * We can only back off and try again later.
  296. */
  297. status = nlm_wait_on_grace(&host->h_gracewait);
  298. } while (status == 0);
  299. return status;
  300. }
  301. /*
  302. * Generic NLM call, async version.
  303. */
  304. static struct rpc_task *__nlm_async_call(struct nlm_rqst *req, u32 proc, struct rpc_message *msg, const struct rpc_call_ops *tk_ops)
  305. {
  306. struct nlm_host *host = req->a_host;
  307. struct rpc_clnt *clnt;
  308. struct rpc_task_setup task_setup_data = {
  309. .rpc_message = msg,
  310. .callback_ops = tk_ops,
  311. .callback_data = req,
  312. .flags = RPC_TASK_ASYNC,
  313. };
  314. dprintk("lockd: call procedure %d on %s (async)\n",
  315. (int)proc, host->h_name);
  316. /* If we have no RPC client yet, create one. */
  317. clnt = nlm_bind_host(host);
  318. if (clnt == NULL)
  319. goto out_err;
  320. msg->rpc_proc = &clnt->cl_procinfo[proc];
  321. task_setup_data.rpc_client = clnt;
  322. /* bootstrap and kick off the async RPC call */
  323. return rpc_run_task(&task_setup_data);
  324. out_err:
  325. tk_ops->rpc_release(req);
  326. return ERR_PTR(-ENOLCK);
  327. }
  328. static int nlm_do_async_call(struct nlm_rqst *req, u32 proc, struct rpc_message *msg, const struct rpc_call_ops *tk_ops)
  329. {
  330. struct rpc_task *task;
  331. task = __nlm_async_call(req, proc, msg, tk_ops);
  332. if (IS_ERR(task))
  333. return PTR_ERR(task);
  334. rpc_put_task(task);
  335. return 0;
  336. }
  337. /*
  338. * NLM asynchronous call.
  339. */
  340. int nlm_async_call(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
  341. {
  342. struct rpc_message msg = {
  343. .rpc_argp = &req->a_args,
  344. .rpc_resp = &req->a_res,
  345. };
  346. return nlm_do_async_call(req, proc, &msg, tk_ops);
  347. }
  348. int nlm_async_reply(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
  349. {
  350. struct rpc_message msg = {
  351. .rpc_argp = &req->a_res,
  352. };
  353. return nlm_do_async_call(req, proc, &msg, tk_ops);
  354. }
  355. /*
  356. * NLM client asynchronous call.
  357. *
  358. * Note that although the calls are asynchronous, and are therefore
  359. * guaranteed to complete, we still always attempt to wait for
  360. * completion in order to be able to correctly track the lock
  361. * state.
  362. */
  363. static int nlmclnt_async_call(const struct cred *cred, struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
  364. {
  365. struct rpc_message msg = {
  366. .rpc_argp = &req->a_args,
  367. .rpc_resp = &req->a_res,
  368. .rpc_cred = cred,
  369. };
  370. struct rpc_task *task;
  371. int err;
  372. task = __nlm_async_call(req, proc, &msg, tk_ops);
  373. if (IS_ERR(task))
  374. return PTR_ERR(task);
  375. err = rpc_wait_for_completion_task(task);
  376. rpc_put_task(task);
  377. return err;
  378. }
  379. /*
  380. * TEST for the presence of a conflicting lock
  381. */
  382. static int
  383. nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl)
  384. {
  385. int status;
  386. status = nlmclnt_call(nfs_file_cred(fl->c.flc_file), req,
  387. NLMPROC_TEST);
  388. if (status < 0)
  389. goto out;
  390. switch (req->a_res.status) {
  391. case nlm_granted:
  392. fl->c.flc_type = F_UNLCK;
  393. break;
  394. case nlm_lck_denied:
  395. /*
  396. * Report the conflicting lock back to the application.
  397. */
  398. fl->fl_start = req->a_res.lock.fl.fl_start;
  399. fl->fl_end = req->a_res.lock.fl.fl_end;
  400. fl->c.flc_type = req->a_res.lock.fl.c.flc_type;
  401. fl->c.flc_pid = -req->a_res.lock.fl.c.flc_pid;
  402. break;
  403. default:
  404. status = nlm_stat_to_errno(req->a_res.status);
  405. }
  406. out:
  407. trace_nlmclnt_test(&req->a_args.lock,
  408. (const struct sockaddr *)&req->a_host->h_addr,
  409. req->a_host->h_addrlen, req->a_res.status);
  410. nlmclnt_release_call(req);
  411. return status;
  412. }
  413. static void nlmclnt_locks_copy_lock(struct file_lock *new, struct file_lock *fl)
  414. {
  415. spin_lock(&fl->fl_u.nfs_fl.owner->host->h_lock);
  416. new->fl_u.nfs_fl.state = fl->fl_u.nfs_fl.state;
  417. new->fl_u.nfs_fl.owner = nlmclnt_get_lockowner(fl->fl_u.nfs_fl.owner);
  418. list_add_tail(&new->fl_u.nfs_fl.list, &fl->fl_u.nfs_fl.owner->host->h_granted);
  419. spin_unlock(&fl->fl_u.nfs_fl.owner->host->h_lock);
  420. }
  421. static void nlmclnt_locks_release_private(struct file_lock *fl)
  422. {
  423. spin_lock(&fl->fl_u.nfs_fl.owner->host->h_lock);
  424. list_del(&fl->fl_u.nfs_fl.list);
  425. spin_unlock(&fl->fl_u.nfs_fl.owner->host->h_lock);
  426. nlmclnt_put_lockowner(fl->fl_u.nfs_fl.owner);
  427. }
  428. static const struct file_lock_operations nlmclnt_lock_ops = {
  429. .fl_copy_lock = nlmclnt_locks_copy_lock,
  430. .fl_release_private = nlmclnt_locks_release_private,
  431. };
  432. static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host)
  433. {
  434. fl->fl_u.nfs_fl.state = 0;
  435. fl->fl_u.nfs_fl.owner = nlmclnt_find_lockowner(host,
  436. fl->c.flc_owner);
  437. INIT_LIST_HEAD(&fl->fl_u.nfs_fl.list);
  438. fl->fl_ops = &nlmclnt_lock_ops;
  439. }
  440. static int do_vfs_lock(struct file_lock *fl)
  441. {
  442. return locks_lock_file_wait(fl->c.flc_file, fl);
  443. }
  444. /*
  445. * LOCK: Try to create a lock
  446. *
  447. * Programmer Harassment Alert
  448. *
  449. * When given a blocking lock request in a sync RPC call, the HPUX lockd
  450. * will faithfully return LCK_BLOCKED but never cares to notify us when
  451. * the lock could be granted. This way, our local process could hang
  452. * around forever waiting for the callback.
  453. *
  454. * Solution A: Implement busy-waiting
  455. * Solution B: Use the async version of the call (NLM_LOCK_{MSG,RES})
  456. *
  457. * For now I am implementing solution A, because I hate the idea of
  458. * re-implementing lockd for a third time in two months. The async
  459. * calls shouldn't be too hard to do, however.
  460. *
  461. * This is one of the lovely things about standards in the NFS area:
  462. * they're so soft and squishy you can't really blame HP for doing this.
  463. */
  464. static int
  465. nlmclnt_lock(struct nlm_rqst *req, struct file_lock *fl)
  466. {
  467. const struct cred *cred = nfs_file_cred(fl->c.flc_file);
  468. struct nlm_host *host = req->a_host;
  469. struct nlm_res *resp = &req->a_res;
  470. struct nlm_wait block;
  471. unsigned char flags = fl->c.flc_flags;
  472. unsigned char type;
  473. __be32 b_status;
  474. int status = -ENOLCK;
  475. if (nsm_monitor(host) < 0)
  476. goto out;
  477. req->a_args.state = nsm_local_state;
  478. fl->c.flc_flags |= FL_ACCESS;
  479. status = do_vfs_lock(fl);
  480. fl->c.flc_flags = flags;
  481. if (status < 0)
  482. goto out;
  483. nlmclnt_prepare_block(&block, host, fl);
  484. again:
  485. /*
  486. * Initialise resp->status to a valid non-zero value,
  487. * since 0 == nlm_lck_granted
  488. */
  489. resp->status = nlm_lck_blocked;
  490. /*
  491. * A GRANTED callback can come at any time -- even before the reply
  492. * to the LOCK request arrives, so we queue the wait before
  493. * requesting the lock.
  494. */
  495. nlmclnt_queue_block(&block);
  496. for (;;) {
  497. /* Reboot protection */
  498. fl->fl_u.nfs_fl.state = host->h_state;
  499. status = nlmclnt_call(cred, req, NLMPROC_LOCK);
  500. if (status < 0)
  501. break;
  502. /* Did a reclaimer thread notify us of a server reboot? */
  503. if (resp->status == nlm_lck_denied_grace_period)
  504. continue;
  505. if (resp->status != nlm_lck_blocked)
  506. break;
  507. /* Wait on an NLM blocking lock */
  508. status = nlmclnt_wait(&block, req, NLMCLNT_POLL_TIMEOUT);
  509. if (status < 0)
  510. break;
  511. if (block.b_status != nlm_lck_blocked)
  512. break;
  513. }
  514. b_status = nlmclnt_dequeue_block(&block);
  515. if (resp->status == nlm_lck_blocked)
  516. resp->status = b_status;
  517. /* if we were interrupted while blocking, then cancel the lock request
  518. * and exit
  519. */
  520. if (resp->status == nlm_lck_blocked) {
  521. if (!req->a_args.block)
  522. goto out_unlock;
  523. if (nlmclnt_cancel(host, req->a_args.block, fl) == 0)
  524. goto out;
  525. }
  526. if (resp->status == nlm_granted) {
  527. down_read(&host->h_rwsem);
  528. /* Check whether or not the server has rebooted */
  529. if (fl->fl_u.nfs_fl.state != host->h_state) {
  530. up_read(&host->h_rwsem);
  531. goto again;
  532. }
  533. /* Ensure the resulting lock will get added to granted list */
  534. fl->c.flc_flags |= FL_SLEEP;
  535. if (do_vfs_lock(fl) < 0)
  536. printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
  537. up_read(&host->h_rwsem);
  538. fl->c.flc_flags = flags;
  539. status = 0;
  540. }
  541. if (status < 0)
  542. goto out_unlock;
  543. /*
  544. * EAGAIN doesn't make sense for sleeping locks, and in some
  545. * cases NLM_LCK_DENIED is returned for a permanent error. So
  546. * turn it into an ENOLCK.
  547. */
  548. if (resp->status == nlm_lck_denied && (flags & FL_SLEEP))
  549. status = -ENOLCK;
  550. else
  551. status = nlm_stat_to_errno(resp->status);
  552. out:
  553. trace_nlmclnt_lock(&req->a_args.lock,
  554. (const struct sockaddr *)&req->a_host->h_addr,
  555. req->a_host->h_addrlen, req->a_res.status);
  556. nlmclnt_release_call(req);
  557. return status;
  558. out_unlock:
  559. /* Fatal error: ensure that we remove the lock altogether */
  560. trace_nlmclnt_lock(&req->a_args.lock,
  561. (const struct sockaddr *)&req->a_host->h_addr,
  562. req->a_host->h_addrlen, req->a_res.status);
  563. dprintk("lockd: lock attempt ended in fatal error.\n"
  564. " Attempting to unlock.\n");
  565. type = fl->c.flc_type;
  566. fl->c.flc_type = F_UNLCK;
  567. down_read(&host->h_rwsem);
  568. do_vfs_lock(fl);
  569. up_read(&host->h_rwsem);
  570. fl->c.flc_type = type;
  571. fl->c.flc_flags = flags;
  572. nlmclnt_async_call(cred, req, NLMPROC_UNLOCK, &nlmclnt_unlock_ops);
  573. return status;
  574. }
  575. /*
  576. * RECLAIM: Try to reclaim a lock
  577. */
  578. int
  579. nlmclnt_reclaim(struct nlm_host *host, struct file_lock *fl,
  580. struct nlm_rqst *req)
  581. {
  582. int status;
  583. memset(req, 0, sizeof(*req));
  584. locks_init_lock(&req->a_args.lock.fl);
  585. locks_init_lock(&req->a_res.lock.fl);
  586. req->a_host = host;
  587. /* Set up the argument struct */
  588. nlmclnt_setlockargs(req, fl);
  589. req->a_args.reclaim = 1;
  590. status = nlmclnt_call(nfs_file_cred(fl->c.flc_file), req,
  591. NLMPROC_LOCK);
  592. if (status >= 0 && req->a_res.status == nlm_granted)
  593. return 0;
  594. printk(KERN_WARNING "lockd: failed to reclaim lock for pid %d "
  595. "(errno %d, status %d)\n",
  596. fl->c.flc_pid,
  597. status, ntohl(req->a_res.status));
  598. /*
  599. * FIXME: This is a serious failure. We can
  600. *
  601. * a. Ignore the problem
  602. * b. Send the owning process some signal (Linux doesn't have
  603. * SIGLOST, though...)
  604. * c. Retry the operation
  605. *
  606. * Until someone comes up with a simple implementation
  607. * for b or c, I'll choose option a.
  608. */
  609. return -ENOLCK;
  610. }
  611. /*
  612. * UNLOCK: remove an existing lock
  613. */
  614. static int
  615. nlmclnt_unlock(struct nlm_rqst *req, struct file_lock *fl)
  616. {
  617. struct nlm_host *host = req->a_host;
  618. struct nlm_res *resp = &req->a_res;
  619. int status;
  620. unsigned char flags = fl->c.flc_flags;
  621. /*
  622. * Note: the server is supposed to either grant us the unlock
  623. * request, or to deny it with NLM_LCK_DENIED_GRACE_PERIOD. In either
  624. * case, we want to unlock.
  625. */
  626. fl->c.flc_flags |= FL_EXISTS;
  627. down_read(&host->h_rwsem);
  628. status = do_vfs_lock(fl);
  629. up_read(&host->h_rwsem);
  630. fl->c.flc_flags = flags;
  631. if (status == -ENOENT) {
  632. status = 0;
  633. goto out;
  634. }
  635. refcount_inc(&req->a_count);
  636. status = nlmclnt_async_call(nfs_file_cred(fl->c.flc_file), req,
  637. NLMPROC_UNLOCK, &nlmclnt_unlock_ops);
  638. if (status < 0)
  639. goto out;
  640. if (resp->status == nlm_granted)
  641. goto out;
  642. if (resp->status != nlm_lck_denied_nolocks)
  643. printk("lockd: unexpected unlock status: %d\n",
  644. ntohl(resp->status));
  645. /* What to do now? I'm out of my depth... */
  646. status = -ENOLCK;
  647. out:
  648. trace_nlmclnt_unlock(&req->a_args.lock,
  649. (const struct sockaddr *)&req->a_host->h_addr,
  650. req->a_host->h_addrlen, req->a_res.status);
  651. nlmclnt_release_call(req);
  652. return status;
  653. }
  654. static void nlmclnt_unlock_prepare(struct rpc_task *task, void *data)
  655. {
  656. struct nlm_rqst *req = data;
  657. const struct nlmclnt_operations *nlmclnt_ops = req->a_host->h_nlmclnt_ops;
  658. bool defer_call = false;
  659. if (nlmclnt_ops && nlmclnt_ops->nlmclnt_unlock_prepare)
  660. defer_call = nlmclnt_ops->nlmclnt_unlock_prepare(task, req->a_callback_data);
  661. if (!defer_call)
  662. rpc_call_start(task);
  663. }
  664. static void nlmclnt_unlock_callback(struct rpc_task *task, void *data)
  665. {
  666. struct nlm_rqst *req = data;
  667. u32 status = ntohl(req->a_res.status);
  668. if (RPC_SIGNALLED(task))
  669. goto die;
  670. if (task->tk_status < 0) {
  671. dprintk("lockd: unlock failed (err = %d)\n", -task->tk_status);
  672. switch (task->tk_status) {
  673. case -EACCES:
  674. case -EIO:
  675. goto die;
  676. default:
  677. goto retry_rebind;
  678. }
  679. }
  680. if (status == NLM_LCK_DENIED_GRACE_PERIOD) {
  681. rpc_delay(task, NLMCLNT_GRACE_WAIT);
  682. goto retry_unlock;
  683. }
  684. if (status != NLM_LCK_GRANTED)
  685. printk(KERN_WARNING "lockd: unexpected unlock status: %d\n", status);
  686. die:
  687. return;
  688. retry_rebind:
  689. nlm_rebind_host(req->a_host);
  690. retry_unlock:
  691. rpc_restart_call(task);
  692. }
  693. static const struct rpc_call_ops nlmclnt_unlock_ops = {
  694. .rpc_call_prepare = nlmclnt_unlock_prepare,
  695. .rpc_call_done = nlmclnt_unlock_callback,
  696. .rpc_release = nlmclnt_rpc_release,
  697. };
  698. /*
  699. * Cancel a blocked lock request.
  700. * We always use an async RPC call for this in order not to hang a
  701. * process that has been Ctrl-C'ed.
  702. */
  703. static int nlmclnt_cancel(struct nlm_host *host, int block, struct file_lock *fl)
  704. {
  705. struct nlm_rqst *req;
  706. int status;
  707. dprintk("lockd: blocking lock attempt was interrupted by a signal.\n"
  708. " Attempting to cancel lock.\n");
  709. req = nlm_alloc_call(host);
  710. if (!req)
  711. return -ENOMEM;
  712. req->a_flags = RPC_TASK_ASYNC;
  713. nlmclnt_setlockargs(req, fl);
  714. req->a_args.block = block;
  715. refcount_inc(&req->a_count);
  716. status = nlmclnt_async_call(nfs_file_cred(fl->c.flc_file), req,
  717. NLMPROC_CANCEL, &nlmclnt_cancel_ops);
  718. if (status == 0 && req->a_res.status == nlm_lck_denied)
  719. status = -ENOLCK;
  720. nlmclnt_release_call(req);
  721. return status;
  722. }
  723. static void nlmclnt_cancel_callback(struct rpc_task *task, void *data)
  724. {
  725. struct nlm_rqst *req = data;
  726. u32 status = ntohl(req->a_res.status);
  727. if (RPC_SIGNALLED(task))
  728. goto die;
  729. if (task->tk_status < 0) {
  730. dprintk("lockd: CANCEL call error %d, retrying.\n",
  731. task->tk_status);
  732. goto retry_cancel;
  733. }
  734. switch (status) {
  735. case NLM_LCK_GRANTED:
  736. case NLM_LCK_DENIED_GRACE_PERIOD:
  737. case NLM_LCK_DENIED:
  738. /* Everything's good */
  739. break;
  740. case NLM_LCK_DENIED_NOLOCKS:
  741. dprintk("lockd: CANCEL failed (server has no locks)\n");
  742. goto retry_cancel;
  743. default:
  744. printk(KERN_NOTICE "lockd: weird return %d for CANCEL call\n",
  745. status);
  746. }
  747. die:
  748. return;
  749. retry_cancel:
  750. /* Don't ever retry more than 3 times */
  751. if (req->a_retries++ >= NLMCLNT_MAX_RETRIES)
  752. goto die;
  753. nlm_rebind_host(req->a_host);
  754. rpc_restart_call(task);
  755. rpc_delay(task, 30 * HZ);
  756. }
  757. static const struct rpc_call_ops nlmclnt_cancel_ops = {
  758. .rpc_call_done = nlmclnt_cancel_callback,
  759. .rpc_release = nlmclnt_rpc_release,
  760. };
  761. /*
  762. * Convert an NLM status code to a generic kernel errno
  763. */
  764. static int
  765. nlm_stat_to_errno(__be32 status)
  766. {
  767. switch(ntohl(status)) {
  768. case NLM_LCK_GRANTED:
  769. return 0;
  770. case NLM_LCK_DENIED:
  771. return -EAGAIN;
  772. case NLM_LCK_DENIED_NOLOCKS:
  773. case NLM_LCK_DENIED_GRACE_PERIOD:
  774. return -ENOLCK;
  775. case NLM_LCK_BLOCKED:
  776. printk(KERN_NOTICE "lockd: unexpected status NLM_BLOCKED\n");
  777. return -ENOLCK;
  778. #ifdef CONFIG_LOCKD_V4
  779. case NLM_DEADLCK:
  780. return -EDEADLK;
  781. case NLM_ROFS:
  782. return -EROFS;
  783. case NLM_STALE_FH:
  784. return -ESTALE;
  785. case NLM_FBIG:
  786. return -EOVERFLOW;
  787. case NLM_FAILED:
  788. return -ENOLCK;
  789. #endif
  790. }
  791. printk(KERN_NOTICE "lockd: unexpected server status %d\n",
  792. ntohl(status));
  793. return -ENOLCK;
  794. }