clntproc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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/config.h>
  9. #include <linux/module.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/smp_lock.h>
  16. #include <linux/sunrpc/clnt.h>
  17. #include <linux/sunrpc/svc.h>
  18. #include <linux/lockd/lockd.h>
  19. #include <linux/lockd/sm_inter.h>
  20. #define NLMDBG_FACILITY NLMDBG_CLIENT
  21. #define NLMCLNT_GRACE_WAIT (5*HZ)
  22. #define NLMCLNT_POLL_TIMEOUT (30*HZ)
  23. #define NLMCLNT_MAX_RETRIES 3
  24. static int nlmclnt_test(struct nlm_rqst *, struct file_lock *);
  25. static int nlmclnt_lock(struct nlm_rqst *, struct file_lock *);
  26. static int nlmclnt_unlock(struct nlm_rqst *, struct file_lock *);
  27. static int nlm_stat_to_errno(u32 stat);
  28. static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host);
  29. static int nlmclnt_cancel(struct nlm_host *, int , struct file_lock *);
  30. static const struct rpc_call_ops nlmclnt_unlock_ops;
  31. static const struct rpc_call_ops nlmclnt_cancel_ops;
  32. /*
  33. * Cookie counter for NLM requests
  34. */
  35. static u32 nlm_cookie = 0x1234;
  36. static inline void nlmclnt_next_cookie(struct nlm_cookie *c)
  37. {
  38. memcpy(c->data, &nlm_cookie, 4);
  39. memset(c->data+4, 0, 4);
  40. c->len=4;
  41. nlm_cookie++;
  42. }
  43. static struct nlm_lockowner *nlm_get_lockowner(struct nlm_lockowner *lockowner)
  44. {
  45. atomic_inc(&lockowner->count);
  46. return lockowner;
  47. }
  48. static void nlm_put_lockowner(struct nlm_lockowner *lockowner)
  49. {
  50. if (!atomic_dec_and_lock(&lockowner->count, &lockowner->host->h_lock))
  51. return;
  52. list_del(&lockowner->list);
  53. spin_unlock(&lockowner->host->h_lock);
  54. nlm_release_host(lockowner->host);
  55. kfree(lockowner);
  56. }
  57. static inline int nlm_pidbusy(struct nlm_host *host, uint32_t pid)
  58. {
  59. struct nlm_lockowner *lockowner;
  60. list_for_each_entry(lockowner, &host->h_lockowners, list) {
  61. if (lockowner->pid == pid)
  62. return -EBUSY;
  63. }
  64. return 0;
  65. }
  66. static inline uint32_t __nlm_alloc_pid(struct nlm_host *host)
  67. {
  68. uint32_t res;
  69. do {
  70. res = host->h_pidcount++;
  71. } while (nlm_pidbusy(host, res) < 0);
  72. return res;
  73. }
  74. static struct nlm_lockowner *__nlm_find_lockowner(struct nlm_host *host, fl_owner_t owner)
  75. {
  76. struct nlm_lockowner *lockowner;
  77. list_for_each_entry(lockowner, &host->h_lockowners, list) {
  78. if (lockowner->owner != owner)
  79. continue;
  80. return nlm_get_lockowner(lockowner);
  81. }
  82. return NULL;
  83. }
  84. static struct nlm_lockowner *nlm_find_lockowner(struct nlm_host *host, fl_owner_t owner)
  85. {
  86. struct nlm_lockowner *res, *new = NULL;
  87. spin_lock(&host->h_lock);
  88. res = __nlm_find_lockowner(host, owner);
  89. if (res == NULL) {
  90. spin_unlock(&host->h_lock);
  91. new = (struct nlm_lockowner *)kmalloc(sizeof(*new), GFP_KERNEL);
  92. spin_lock(&host->h_lock);
  93. res = __nlm_find_lockowner(host, owner);
  94. if (res == NULL && new != NULL) {
  95. res = new;
  96. atomic_set(&new->count, 1);
  97. new->owner = owner;
  98. new->pid = __nlm_alloc_pid(host);
  99. new->host = nlm_get_host(host);
  100. list_add(&new->list, &host->h_lockowners);
  101. new = NULL;
  102. }
  103. }
  104. spin_unlock(&host->h_lock);
  105. kfree(new);
  106. return res;
  107. }
  108. /*
  109. * Initialize arguments for TEST/LOCK/UNLOCK/CANCEL calls
  110. */
  111. static void nlmclnt_setlockargs(struct nlm_rqst *req, struct file_lock *fl)
  112. {
  113. struct nlm_args *argp = &req->a_args;
  114. struct nlm_lock *lock = &argp->lock;
  115. nlmclnt_next_cookie(&argp->cookie);
  116. argp->state = nsm_local_state;
  117. memcpy(&lock->fh, NFS_FH(fl->fl_file->f_dentry->d_inode), sizeof(struct nfs_fh));
  118. lock->caller = system_utsname.nodename;
  119. lock->oh.data = req->a_owner;
  120. lock->oh.len = snprintf(req->a_owner, sizeof(req->a_owner), "%u@%s",
  121. (unsigned int)fl->fl_u.nfs_fl.owner->pid,
  122. system_utsname.nodename);
  123. lock->svid = fl->fl_u.nfs_fl.owner->pid;
  124. lock->fl.fl_start = fl->fl_start;
  125. lock->fl.fl_end = fl->fl_end;
  126. lock->fl.fl_type = fl->fl_type;
  127. }
  128. static void nlmclnt_release_lockargs(struct nlm_rqst *req)
  129. {
  130. BUG_ON(req->a_args.lock.fl.fl_ops != NULL);
  131. }
  132. /*
  133. * This is the main entry point for the NLM client.
  134. */
  135. int
  136. nlmclnt_proc(struct inode *inode, int cmd, struct file_lock *fl)
  137. {
  138. struct nlm_host *host;
  139. struct nlm_rqst *call;
  140. sigset_t oldset;
  141. unsigned long flags;
  142. int status, proto, vers;
  143. vers = (NFS_PROTO(inode)->version == 3) ? 4 : 1;
  144. if (NFS_PROTO(inode)->version > 3) {
  145. printk(KERN_NOTICE "NFSv4 file locking not implemented!\n");
  146. return -ENOLCK;
  147. }
  148. /* Retrieve transport protocol from NFS client */
  149. proto = NFS_CLIENT(inode)->cl_xprt->prot;
  150. host = nlmclnt_lookup_host(NFS_ADDR(inode), proto, vers);
  151. if (host == NULL)
  152. return -ENOLCK;
  153. call = nlm_alloc_call(host);
  154. if (call == NULL)
  155. return -ENOMEM;
  156. nlmclnt_locks_init_private(fl, host);
  157. /* Set up the argument struct */
  158. nlmclnt_setlockargs(call, fl);
  159. /* Keep the old signal mask */
  160. spin_lock_irqsave(&current->sighand->siglock, flags);
  161. oldset = current->blocked;
  162. /* If we're cleaning up locks because the process is exiting,
  163. * perform the RPC call asynchronously. */
  164. if ((IS_SETLK(cmd) || IS_SETLKW(cmd))
  165. && fl->fl_type == F_UNLCK
  166. && (current->flags & PF_EXITING)) {
  167. sigfillset(&current->blocked); /* Mask all signals */
  168. recalc_sigpending();
  169. call->a_flags = RPC_TASK_ASYNC;
  170. }
  171. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  172. if (IS_SETLK(cmd) || IS_SETLKW(cmd)) {
  173. if (fl->fl_type != F_UNLCK) {
  174. call->a_args.block = IS_SETLKW(cmd) ? 1 : 0;
  175. status = nlmclnt_lock(call, fl);
  176. } else
  177. status = nlmclnt_unlock(call, fl);
  178. } else if (IS_GETLK(cmd))
  179. status = nlmclnt_test(call, fl);
  180. else
  181. status = -EINVAL;
  182. fl->fl_ops->fl_release_private(fl);
  183. fl->fl_ops = NULL;
  184. spin_lock_irqsave(&current->sighand->siglock, flags);
  185. current->blocked = oldset;
  186. recalc_sigpending();
  187. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  188. dprintk("lockd: clnt proc returns %d\n", status);
  189. return status;
  190. }
  191. EXPORT_SYMBOL(nlmclnt_proc);
  192. /*
  193. * Allocate an NLM RPC call struct
  194. *
  195. * Note: the caller must hold a reference to host. In case of failure,
  196. * this reference will be released.
  197. */
  198. struct nlm_rqst *nlm_alloc_call(struct nlm_host *host)
  199. {
  200. struct nlm_rqst *call;
  201. for(;;) {
  202. call = kzalloc(sizeof(*call), GFP_KERNEL);
  203. if (call != NULL) {
  204. locks_init_lock(&call->a_args.lock.fl);
  205. locks_init_lock(&call->a_res.lock.fl);
  206. call->a_host = host;
  207. return call;
  208. }
  209. if (signalled())
  210. break;
  211. printk("nlm_alloc_call: failed, waiting for memory\n");
  212. schedule_timeout_interruptible(5*HZ);
  213. }
  214. nlm_release_host(host);
  215. return NULL;
  216. }
  217. void nlm_release_call(struct nlm_rqst *call)
  218. {
  219. nlm_release_host(call->a_host);
  220. nlmclnt_release_lockargs(call);
  221. kfree(call);
  222. }
  223. static void nlmclnt_rpc_release(void *data)
  224. {
  225. return nlm_release_call(data);
  226. }
  227. static int nlm_wait_on_grace(wait_queue_head_t *queue)
  228. {
  229. DEFINE_WAIT(wait);
  230. int status = -EINTR;
  231. prepare_to_wait(queue, &wait, TASK_INTERRUPTIBLE);
  232. if (!signalled ()) {
  233. schedule_timeout(NLMCLNT_GRACE_WAIT);
  234. try_to_freeze();
  235. if (!signalled ())
  236. status = 0;
  237. }
  238. finish_wait(queue, &wait);
  239. return status;
  240. }
  241. /*
  242. * Generic NLM call
  243. */
  244. static int
  245. nlmclnt_call(struct nlm_rqst *req, u32 proc)
  246. {
  247. struct nlm_host *host = req->a_host;
  248. struct rpc_clnt *clnt;
  249. struct nlm_args *argp = &req->a_args;
  250. struct nlm_res *resp = &req->a_res;
  251. struct rpc_message msg = {
  252. .rpc_argp = argp,
  253. .rpc_resp = resp,
  254. };
  255. int status;
  256. dprintk("lockd: call procedure %d on %s\n",
  257. (int)proc, host->h_name);
  258. do {
  259. if (host->h_reclaiming && !argp->reclaim)
  260. goto in_grace_period;
  261. /* If we have no RPC client yet, create one. */
  262. if ((clnt = nlm_bind_host(host)) == NULL)
  263. return -ENOLCK;
  264. msg.rpc_proc = &clnt->cl_procinfo[proc];
  265. /* Perform the RPC call. If an error occurs, try again */
  266. if ((status = rpc_call_sync(clnt, &msg, 0)) < 0) {
  267. dprintk("lockd: rpc_call returned error %d\n", -status);
  268. switch (status) {
  269. case -EPROTONOSUPPORT:
  270. status = -EINVAL;
  271. break;
  272. case -ECONNREFUSED:
  273. case -ETIMEDOUT:
  274. case -ENOTCONN:
  275. nlm_rebind_host(host);
  276. status = -EAGAIN;
  277. break;
  278. case -ERESTARTSYS:
  279. return signalled () ? -EINTR : status;
  280. default:
  281. break;
  282. }
  283. break;
  284. } else
  285. if (resp->status == NLM_LCK_DENIED_GRACE_PERIOD) {
  286. dprintk("lockd: server in grace period\n");
  287. if (argp->reclaim) {
  288. printk(KERN_WARNING
  289. "lockd: spurious grace period reject?!\n");
  290. return -ENOLCK;
  291. }
  292. } else {
  293. if (!argp->reclaim) {
  294. /* We appear to be out of the grace period */
  295. wake_up_all(&host->h_gracewait);
  296. }
  297. dprintk("lockd: server returns status %d\n", resp->status);
  298. return 0; /* Okay, call complete */
  299. }
  300. in_grace_period:
  301. /*
  302. * The server has rebooted and appears to be in the grace
  303. * period during which locks are only allowed to be
  304. * reclaimed.
  305. * We can only back off and try again later.
  306. */
  307. status = nlm_wait_on_grace(&host->h_gracewait);
  308. } while (status == 0);
  309. return status;
  310. }
  311. /*
  312. * Generic NLM call, async version.
  313. */
  314. int nlm_async_call(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
  315. {
  316. struct nlm_host *host = req->a_host;
  317. struct rpc_clnt *clnt;
  318. struct rpc_message msg = {
  319. .rpc_argp = &req->a_args,
  320. .rpc_resp = &req->a_res,
  321. };
  322. int status = -ENOLCK;
  323. dprintk("lockd: call procedure %d on %s (async)\n",
  324. (int)proc, host->h_name);
  325. /* If we have no RPC client yet, create one. */
  326. clnt = nlm_bind_host(host);
  327. if (clnt == NULL)
  328. goto out_err;
  329. msg.rpc_proc = &clnt->cl_procinfo[proc];
  330. /* bootstrap and kick off the async RPC call */
  331. status = rpc_call_async(clnt, &msg, RPC_TASK_ASYNC, tk_ops, req);
  332. if (status == 0)
  333. return 0;
  334. out_err:
  335. nlm_release_call(req);
  336. return status;
  337. }
  338. /*
  339. * TEST for the presence of a conflicting lock
  340. */
  341. static int
  342. nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl)
  343. {
  344. int status;
  345. status = nlmclnt_call(req, NLMPROC_TEST);
  346. if (status < 0)
  347. goto out;
  348. switch (req->a_res.status) {
  349. case NLM_LCK_GRANTED:
  350. fl->fl_type = F_UNLCK;
  351. break;
  352. case NLM_LCK_DENIED:
  353. /*
  354. * Report the conflicting lock back to the application.
  355. */
  356. fl->fl_start = req->a_res.lock.fl.fl_start;
  357. fl->fl_end = req->a_res.lock.fl.fl_start;
  358. fl->fl_type = req->a_res.lock.fl.fl_type;
  359. fl->fl_pid = 0;
  360. break;
  361. default:
  362. status = nlm_stat_to_errno(req->a_res.status);
  363. }
  364. out:
  365. nlm_release_call(req);
  366. return status;
  367. }
  368. static void nlmclnt_locks_copy_lock(struct file_lock *new, struct file_lock *fl)
  369. {
  370. new->fl_u.nfs_fl.state = fl->fl_u.nfs_fl.state;
  371. new->fl_u.nfs_fl.owner = nlm_get_lockowner(fl->fl_u.nfs_fl.owner);
  372. list_add_tail(&new->fl_u.nfs_fl.list, &fl->fl_u.nfs_fl.owner->host->h_granted);
  373. }
  374. static void nlmclnt_locks_release_private(struct file_lock *fl)
  375. {
  376. list_del(&fl->fl_u.nfs_fl.list);
  377. nlm_put_lockowner(fl->fl_u.nfs_fl.owner);
  378. }
  379. static struct file_lock_operations nlmclnt_lock_ops = {
  380. .fl_copy_lock = nlmclnt_locks_copy_lock,
  381. .fl_release_private = nlmclnt_locks_release_private,
  382. };
  383. static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host)
  384. {
  385. BUG_ON(fl->fl_ops != NULL);
  386. fl->fl_u.nfs_fl.state = 0;
  387. fl->fl_u.nfs_fl.owner = nlm_find_lockowner(host, fl->fl_owner);
  388. INIT_LIST_HEAD(&fl->fl_u.nfs_fl.list);
  389. fl->fl_ops = &nlmclnt_lock_ops;
  390. }
  391. static void do_vfs_lock(struct file_lock *fl)
  392. {
  393. int res = 0;
  394. switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
  395. case FL_POSIX:
  396. res = posix_lock_file_wait(fl->fl_file, fl);
  397. break;
  398. case FL_FLOCK:
  399. res = flock_lock_file_wait(fl->fl_file, fl);
  400. break;
  401. default:
  402. BUG();
  403. }
  404. if (res < 0)
  405. printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n",
  406. __FUNCTION__);
  407. }
  408. /*
  409. * LOCK: Try to create a lock
  410. *
  411. * Programmer Harassment Alert
  412. *
  413. * When given a blocking lock request in a sync RPC call, the HPUX lockd
  414. * will faithfully return LCK_BLOCKED but never cares to notify us when
  415. * the lock could be granted. This way, our local process could hang
  416. * around forever waiting for the callback.
  417. *
  418. * Solution A: Implement busy-waiting
  419. * Solution B: Use the async version of the call (NLM_LOCK_{MSG,RES})
  420. *
  421. * For now I am implementing solution A, because I hate the idea of
  422. * re-implementing lockd for a third time in two months. The async
  423. * calls shouldn't be too hard to do, however.
  424. *
  425. * This is one of the lovely things about standards in the NFS area:
  426. * they're so soft and squishy you can't really blame HP for doing this.
  427. */
  428. static int
  429. nlmclnt_lock(struct nlm_rqst *req, struct file_lock *fl)
  430. {
  431. struct nlm_host *host = req->a_host;
  432. struct nlm_res *resp = &req->a_res;
  433. struct nlm_wait *block = NULL;
  434. int status = -ENOLCK;
  435. if (!host->h_monitored && nsm_monitor(host) < 0) {
  436. printk(KERN_NOTICE "lockd: failed to monitor %s\n",
  437. host->h_name);
  438. goto out;
  439. }
  440. block = nlmclnt_prepare_block(host, fl);
  441. for(;;) {
  442. status = nlmclnt_call(req, NLMPROC_LOCK);
  443. if (status < 0)
  444. goto out_unblock;
  445. if (!req->a_args.block)
  446. break;
  447. /* Did a reclaimer thread notify us of a server reboot? */
  448. if (resp->status == NLM_LCK_DENIED_GRACE_PERIOD)
  449. continue;
  450. if (resp->status != NLM_LCK_BLOCKED)
  451. break;
  452. /* Wait on an NLM blocking lock */
  453. status = nlmclnt_block(block, req, NLMCLNT_POLL_TIMEOUT);
  454. /* if we were interrupted. Send a CANCEL request to the server
  455. * and exit
  456. */
  457. if (status < 0)
  458. goto out_unblock;
  459. if (resp->status != NLM_LCK_BLOCKED)
  460. break;
  461. }
  462. if (resp->status == NLM_LCK_GRANTED) {
  463. fl->fl_u.nfs_fl.state = host->h_state;
  464. fl->fl_flags |= FL_SLEEP;
  465. /* Ensure the resulting lock will get added to granted list */
  466. do_vfs_lock(fl);
  467. }
  468. status = nlm_stat_to_errno(resp->status);
  469. out_unblock:
  470. nlmclnt_finish_block(block);
  471. /* Cancel the blocked request if it is still pending */
  472. if (resp->status == NLM_LCK_BLOCKED)
  473. nlmclnt_cancel(host, req->a_args.block, fl);
  474. out:
  475. nlm_release_call(req);
  476. return status;
  477. }
  478. /*
  479. * RECLAIM: Try to reclaim a lock
  480. */
  481. int
  482. nlmclnt_reclaim(struct nlm_host *host, struct file_lock *fl)
  483. {
  484. struct nlm_rqst reqst, *req;
  485. int status;
  486. req = &reqst;
  487. memset(req, 0, sizeof(*req));
  488. locks_init_lock(&req->a_args.lock.fl);
  489. locks_init_lock(&req->a_res.lock.fl);
  490. req->a_host = host;
  491. req->a_flags = 0;
  492. /* Set up the argument struct */
  493. nlmclnt_setlockargs(req, fl);
  494. req->a_args.reclaim = 1;
  495. if ((status = nlmclnt_call(req, NLMPROC_LOCK)) >= 0
  496. && req->a_res.status == NLM_LCK_GRANTED)
  497. return 0;
  498. printk(KERN_WARNING "lockd: failed to reclaim lock for pid %d "
  499. "(errno %d, status %d)\n", fl->fl_pid,
  500. status, req->a_res.status);
  501. /*
  502. * FIXME: This is a serious failure. We can
  503. *
  504. * a. Ignore the problem
  505. * b. Send the owning process some signal (Linux doesn't have
  506. * SIGLOST, though...)
  507. * c. Retry the operation
  508. *
  509. * Until someone comes up with a simple implementation
  510. * for b or c, I'll choose option a.
  511. */
  512. return -ENOLCK;
  513. }
  514. /*
  515. * UNLOCK: remove an existing lock
  516. */
  517. static int
  518. nlmclnt_unlock(struct nlm_rqst *req, struct file_lock *fl)
  519. {
  520. struct nlm_res *resp = &req->a_res;
  521. int status;
  522. /*
  523. * Note: the server is supposed to either grant us the unlock
  524. * request, or to deny it with NLM_LCK_DENIED_GRACE_PERIOD. In either
  525. * case, we want to unlock.
  526. */
  527. do_vfs_lock(fl);
  528. if (req->a_flags & RPC_TASK_ASYNC)
  529. return nlm_async_call(req, NLMPROC_UNLOCK, &nlmclnt_unlock_ops);
  530. status = nlmclnt_call(req, NLMPROC_UNLOCK);
  531. if (status < 0)
  532. goto out;
  533. status = 0;
  534. if (resp->status == NLM_LCK_GRANTED)
  535. goto out;
  536. if (resp->status != NLM_LCK_DENIED_NOLOCKS)
  537. printk("lockd: unexpected unlock status: %d\n", resp->status);
  538. /* What to do now? I'm out of my depth... */
  539. status = -ENOLCK;
  540. out:
  541. nlm_release_call(req);
  542. return status;
  543. }
  544. static void nlmclnt_unlock_callback(struct rpc_task *task, void *data)
  545. {
  546. struct nlm_rqst *req = data;
  547. int status = req->a_res.status;
  548. if (RPC_ASSASSINATED(task))
  549. goto die;
  550. if (task->tk_status < 0) {
  551. dprintk("lockd: unlock failed (err = %d)\n", -task->tk_status);
  552. goto retry_rebind;
  553. }
  554. if (status == NLM_LCK_DENIED_GRACE_PERIOD) {
  555. rpc_delay(task, NLMCLNT_GRACE_WAIT);
  556. goto retry_unlock;
  557. }
  558. if (status != NLM_LCK_GRANTED)
  559. printk(KERN_WARNING "lockd: unexpected unlock status: %d\n", status);
  560. die:
  561. return;
  562. retry_rebind:
  563. nlm_rebind_host(req->a_host);
  564. retry_unlock:
  565. rpc_restart_call(task);
  566. }
  567. static const struct rpc_call_ops nlmclnt_unlock_ops = {
  568. .rpc_call_done = nlmclnt_unlock_callback,
  569. .rpc_release = nlmclnt_rpc_release,
  570. };
  571. /*
  572. * Cancel a blocked lock request.
  573. * We always use an async RPC call for this in order not to hang a
  574. * process that has been Ctrl-C'ed.
  575. */
  576. static int nlmclnt_cancel(struct nlm_host *host, int block, struct file_lock *fl)
  577. {
  578. struct nlm_rqst *req;
  579. unsigned long flags;
  580. sigset_t oldset;
  581. int status;
  582. /* Block all signals while setting up call */
  583. spin_lock_irqsave(&current->sighand->siglock, flags);
  584. oldset = current->blocked;
  585. sigfillset(&current->blocked);
  586. recalc_sigpending();
  587. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  588. req = nlm_alloc_call(nlm_get_host(host));
  589. if (!req)
  590. return -ENOMEM;
  591. req->a_flags = RPC_TASK_ASYNC;
  592. nlmclnt_setlockargs(req, fl);
  593. req->a_args.block = block;
  594. status = nlm_async_call(req, NLMPROC_CANCEL, &nlmclnt_cancel_ops);
  595. spin_lock_irqsave(&current->sighand->siglock, flags);
  596. current->blocked = oldset;
  597. recalc_sigpending();
  598. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  599. return status;
  600. }
  601. static void nlmclnt_cancel_callback(struct rpc_task *task, void *data)
  602. {
  603. struct nlm_rqst *req = data;
  604. if (RPC_ASSASSINATED(task))
  605. goto die;
  606. if (task->tk_status < 0) {
  607. dprintk("lockd: CANCEL call error %d, retrying.\n",
  608. task->tk_status);
  609. goto retry_cancel;
  610. }
  611. dprintk("lockd: cancel status %d (task %d)\n",
  612. req->a_res.status, task->tk_pid);
  613. switch (req->a_res.status) {
  614. case NLM_LCK_GRANTED:
  615. case NLM_LCK_DENIED_GRACE_PERIOD:
  616. case NLM_LCK_DENIED:
  617. /* Everything's good */
  618. break;
  619. case NLM_LCK_DENIED_NOLOCKS:
  620. dprintk("lockd: CANCEL failed (server has no locks)\n");
  621. goto retry_cancel;
  622. default:
  623. printk(KERN_NOTICE "lockd: weird return %d for CANCEL call\n",
  624. req->a_res.status);
  625. }
  626. die:
  627. return;
  628. retry_cancel:
  629. /* Don't ever retry more than 3 times */
  630. if (req->a_retries++ >= NLMCLNT_MAX_RETRIES)
  631. goto die;
  632. nlm_rebind_host(req->a_host);
  633. rpc_restart_call(task);
  634. rpc_delay(task, 30 * HZ);
  635. }
  636. static const struct rpc_call_ops nlmclnt_cancel_ops = {
  637. .rpc_call_done = nlmclnt_cancel_callback,
  638. .rpc_release = nlmclnt_rpc_release,
  639. };
  640. /*
  641. * Convert an NLM status code to a generic kernel errno
  642. */
  643. static int
  644. nlm_stat_to_errno(u32 status)
  645. {
  646. switch(status) {
  647. case NLM_LCK_GRANTED:
  648. return 0;
  649. case NLM_LCK_DENIED:
  650. return -EAGAIN;
  651. case NLM_LCK_DENIED_NOLOCKS:
  652. case NLM_LCK_DENIED_GRACE_PERIOD:
  653. return -ENOLCK;
  654. case NLM_LCK_BLOCKED:
  655. printk(KERN_NOTICE "lockd: unexpected status NLM_BLOCKED\n");
  656. return -ENOLCK;
  657. #ifdef CONFIG_LOCKD_V4
  658. case NLM_DEADLCK:
  659. return -EDEADLK;
  660. case NLM_ROFS:
  661. return -EROFS;
  662. case NLM_STALE_FH:
  663. return -ESTALE;
  664. case NLM_FBIG:
  665. return -EOVERFLOW;
  666. case NLM_FAILED:
  667. return -ENOLCK;
  668. #endif
  669. }
  670. printk(KERN_NOTICE "lockd: unexpected server status %d\n", status);
  671. return -ENOLCK;
  672. }