clntproc.c 21 KB

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