clntproc.c 19 KB

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