clntproc.c 22 KB

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