clntproc.c 21 KB

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