clntproc.c 19 KB

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