clntproc.c 22 KB

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