clntproc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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 nfs_server *nfssrv = NFS_SERVER(inode);
  139. struct nlm_host *host;
  140. struct nlm_rqst reqst, *call = &reqst;
  141. sigset_t oldset;
  142. unsigned long flags;
  143. int status, proto, vers;
  144. vers = (NFS_PROTO(inode)->version == 3) ? 4 : 1;
  145. if (NFS_PROTO(inode)->version > 3) {
  146. printk(KERN_NOTICE "NFSv4 file locking not implemented!\n");
  147. return -ENOLCK;
  148. }
  149. /* Retrieve transport protocol from NFS client */
  150. proto = NFS_CLIENT(inode)->cl_xprt->prot;
  151. if (!(host = nlmclnt_lookup_host(NFS_ADDR(inode), proto, vers)))
  152. return -ENOLCK;
  153. /* Create RPC client handle if not there, and copy soft
  154. * and intr flags from NFS client. */
  155. if (host->h_rpcclnt == NULL) {
  156. struct rpc_clnt *clnt;
  157. /* Bind an rpc client to this host handle (does not
  158. * perform a portmapper lookup) */
  159. if (!(clnt = nlm_bind_host(host))) {
  160. status = -ENOLCK;
  161. goto done;
  162. }
  163. clnt->cl_softrtry = nfssrv->client->cl_softrtry;
  164. clnt->cl_intr = nfssrv->client->cl_intr;
  165. }
  166. /* Keep the old signal mask */
  167. spin_lock_irqsave(&current->sighand->siglock, flags);
  168. oldset = current->blocked;
  169. /* If we're cleaning up locks because the process is exiting,
  170. * perform the RPC call asynchronously. */
  171. if ((IS_SETLK(cmd) || IS_SETLKW(cmd))
  172. && fl->fl_type == F_UNLCK
  173. && (current->flags & PF_EXITING)) {
  174. sigfillset(&current->blocked); /* Mask all signals */
  175. recalc_sigpending();
  176. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  177. call = nlmclnt_alloc_call();
  178. if (!call) {
  179. status = -ENOMEM;
  180. goto out_restore;
  181. }
  182. call->a_flags = RPC_TASK_ASYNC;
  183. } else {
  184. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  185. memset(call, 0, sizeof(*call));
  186. locks_init_lock(&call->a_args.lock.fl);
  187. locks_init_lock(&call->a_res.lock.fl);
  188. }
  189. call->a_host = host;
  190. nlmclnt_locks_init_private(fl, host);
  191. /* Set up the argument struct */
  192. nlmclnt_setlockargs(call, fl);
  193. if (IS_SETLK(cmd) || IS_SETLKW(cmd)) {
  194. if (fl->fl_type != F_UNLCK) {
  195. call->a_args.block = IS_SETLKW(cmd) ? 1 : 0;
  196. status = nlmclnt_lock(call, fl);
  197. } else
  198. status = nlmclnt_unlock(call, fl);
  199. } else if (IS_GETLK(cmd))
  200. status = nlmclnt_test(call, fl);
  201. else
  202. status = -EINVAL;
  203. out_restore:
  204. spin_lock_irqsave(&current->sighand->siglock, flags);
  205. current->blocked = oldset;
  206. recalc_sigpending();
  207. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  208. done:
  209. dprintk("lockd: clnt proc returns %d\n", status);
  210. nlm_release_host(host);
  211. return status;
  212. }
  213. EXPORT_SYMBOL(nlmclnt_proc);
  214. /*
  215. * Allocate an NLM RPC call struct
  216. */
  217. struct nlm_rqst *
  218. nlmclnt_alloc_call(void)
  219. {
  220. struct nlm_rqst *call;
  221. for(;;) {
  222. call = kzalloc(sizeof(*call), GFP_KERNEL);
  223. if (call != NULL) {
  224. locks_init_lock(&call->a_args.lock.fl);
  225. locks_init_lock(&call->a_res.lock.fl);
  226. return call;
  227. }
  228. if (signalled())
  229. break;
  230. printk("nlmclnt_alloc_call: failed, waiting for memory\n");
  231. schedule_timeout_interruptible(5*HZ);
  232. }
  233. return NULL;
  234. }
  235. static int nlm_wait_on_grace(wait_queue_head_t *queue)
  236. {
  237. DEFINE_WAIT(wait);
  238. int status = -EINTR;
  239. prepare_to_wait(queue, &wait, TASK_INTERRUPTIBLE);
  240. if (!signalled ()) {
  241. schedule_timeout(NLMCLNT_GRACE_WAIT);
  242. try_to_freeze();
  243. if (!signalled ())
  244. status = 0;
  245. }
  246. finish_wait(queue, &wait);
  247. return status;
  248. }
  249. /*
  250. * Generic NLM call
  251. */
  252. static int
  253. nlmclnt_call(struct nlm_rqst *req, u32 proc)
  254. {
  255. struct nlm_host *host = req->a_host;
  256. struct rpc_clnt *clnt;
  257. struct nlm_args *argp = &req->a_args;
  258. struct nlm_res *resp = &req->a_res;
  259. struct rpc_message msg = {
  260. .rpc_argp = argp,
  261. .rpc_resp = resp,
  262. };
  263. int status;
  264. dprintk("lockd: call procedure %d on %s\n",
  265. (int)proc, host->h_name);
  266. do {
  267. if (host->h_reclaiming && !argp->reclaim)
  268. goto in_grace_period;
  269. /* If we have no RPC client yet, create one. */
  270. if ((clnt = nlm_bind_host(host)) == NULL)
  271. return -ENOLCK;
  272. msg.rpc_proc = &clnt->cl_procinfo[proc];
  273. /* Perform the RPC call. If an error occurs, try again */
  274. if ((status = rpc_call_sync(clnt, &msg, 0)) < 0) {
  275. dprintk("lockd: rpc_call returned error %d\n", -status);
  276. switch (status) {
  277. case -EPROTONOSUPPORT:
  278. status = -EINVAL;
  279. break;
  280. case -ECONNREFUSED:
  281. case -ETIMEDOUT:
  282. case -ENOTCONN:
  283. nlm_rebind_host(host);
  284. status = -EAGAIN;
  285. break;
  286. case -ERESTARTSYS:
  287. return signalled () ? -EINTR : status;
  288. default:
  289. break;
  290. }
  291. break;
  292. } else
  293. if (resp->status == NLM_LCK_DENIED_GRACE_PERIOD) {
  294. dprintk("lockd: server in grace period\n");
  295. if (argp->reclaim) {
  296. printk(KERN_WARNING
  297. "lockd: spurious grace period reject?!\n");
  298. return -ENOLCK;
  299. }
  300. } else {
  301. if (!argp->reclaim) {
  302. /* We appear to be out of the grace period */
  303. wake_up_all(&host->h_gracewait);
  304. }
  305. dprintk("lockd: server returns status %d\n", resp->status);
  306. return 0; /* Okay, call complete */
  307. }
  308. in_grace_period:
  309. /*
  310. * The server has rebooted and appears to be in the grace
  311. * period during which locks are only allowed to be
  312. * reclaimed.
  313. * We can only back off and try again later.
  314. */
  315. status = nlm_wait_on_grace(&host->h_gracewait);
  316. } while (status == 0);
  317. return status;
  318. }
  319. /*
  320. * Generic NLM call, async version.
  321. */
  322. int nlmsvc_async_call(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
  323. {
  324. struct nlm_host *host = req->a_host;
  325. struct rpc_clnt *clnt;
  326. struct rpc_message msg = {
  327. .rpc_argp = &req->a_args,
  328. .rpc_resp = &req->a_res,
  329. };
  330. int status;
  331. dprintk("lockd: call procedure %d on %s (async)\n",
  332. (int)proc, host->h_name);
  333. /* If we have no RPC client yet, create one. */
  334. if ((clnt = nlm_bind_host(host)) == NULL)
  335. return -ENOLCK;
  336. msg.rpc_proc = &clnt->cl_procinfo[proc];
  337. /* bootstrap and kick off the async RPC call */
  338. status = rpc_call_async(clnt, &msg, RPC_TASK_ASYNC, tk_ops, req);
  339. return status;
  340. }
  341. static int nlmclnt_async_call(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
  342. {
  343. struct nlm_host *host = req->a_host;
  344. struct rpc_clnt *clnt;
  345. struct nlm_args *argp = &req->a_args;
  346. struct nlm_res *resp = &req->a_res;
  347. struct rpc_message msg = {
  348. .rpc_argp = argp,
  349. .rpc_resp = resp,
  350. };
  351. int status;
  352. dprintk("lockd: call procedure %d on %s (async)\n",
  353. (int)proc, host->h_name);
  354. /* If we have no RPC client yet, create one. */
  355. if ((clnt = nlm_bind_host(host)) == NULL)
  356. return -ENOLCK;
  357. msg.rpc_proc = &clnt->cl_procinfo[proc];
  358. /* Increment host refcount */
  359. nlm_get_host(host);
  360. /* bootstrap and kick off the async RPC call */
  361. status = rpc_call_async(clnt, &msg, RPC_TASK_ASYNC, tk_ops, req);
  362. if (status < 0)
  363. nlm_release_host(host);
  364. return status;
  365. }
  366. /*
  367. * TEST for the presence of a conflicting lock
  368. */
  369. static int
  370. nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl)
  371. {
  372. int status;
  373. status = nlmclnt_call(req, NLMPROC_TEST);
  374. nlmclnt_release_lockargs(req);
  375. if (status < 0)
  376. return status;
  377. status = req->a_res.status;
  378. if (status == NLM_LCK_GRANTED) {
  379. fl->fl_type = F_UNLCK;
  380. } if (status == NLM_LCK_DENIED) {
  381. /*
  382. * Report the conflicting lock back to the application.
  383. */
  384. locks_copy_lock(fl, &req->a_res.lock.fl);
  385. fl->fl_pid = 0;
  386. } else {
  387. return nlm_stat_to_errno(req->a_res.status);
  388. }
  389. return 0;
  390. }
  391. static void nlmclnt_locks_copy_lock(struct file_lock *new, struct file_lock *fl)
  392. {
  393. new->fl_u.nfs_fl.state = fl->fl_u.nfs_fl.state;
  394. new->fl_u.nfs_fl.owner = nlm_get_lockowner(fl->fl_u.nfs_fl.owner);
  395. list_add_tail(&new->fl_u.nfs_fl.list, &fl->fl_u.nfs_fl.owner->host->h_granted);
  396. }
  397. static void nlmclnt_locks_release_private(struct file_lock *fl)
  398. {
  399. list_del(&fl->fl_u.nfs_fl.list);
  400. nlm_put_lockowner(fl->fl_u.nfs_fl.owner);
  401. }
  402. static struct file_lock_operations nlmclnt_lock_ops = {
  403. .fl_copy_lock = nlmclnt_locks_copy_lock,
  404. .fl_release_private = nlmclnt_locks_release_private,
  405. };
  406. static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host)
  407. {
  408. BUG_ON(fl->fl_ops != NULL);
  409. fl->fl_u.nfs_fl.state = 0;
  410. fl->fl_u.nfs_fl.owner = nlm_find_lockowner(host, fl->fl_owner);
  411. INIT_LIST_HEAD(&fl->fl_u.nfs_fl.list);
  412. fl->fl_ops = &nlmclnt_lock_ops;
  413. }
  414. static void do_vfs_lock(struct file_lock *fl)
  415. {
  416. int res = 0;
  417. switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
  418. case FL_POSIX:
  419. res = posix_lock_file_wait(fl->fl_file, fl);
  420. break;
  421. case FL_FLOCK:
  422. res = flock_lock_file_wait(fl->fl_file, fl);
  423. break;
  424. default:
  425. BUG();
  426. }
  427. if (res < 0)
  428. printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n",
  429. __FUNCTION__);
  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. int status = -ENOLCK;
  458. if (!host->h_monitored && nsm_monitor(host) < 0) {
  459. printk(KERN_NOTICE "lockd: failed to monitor %s\n",
  460. host->h_name);
  461. goto out;
  462. }
  463. block = nlmclnt_prepare_block(host, fl);
  464. for(;;) {
  465. status = nlmclnt_call(req, NLMPROC_LOCK);
  466. if (status < 0)
  467. goto out_unblock;
  468. if (!req->a_args.block)
  469. break;
  470. /* Did a reclaimer thread notify us of a server reboot? */
  471. if (resp->status == NLM_LCK_DENIED_GRACE_PERIOD)
  472. continue;
  473. if (resp->status != NLM_LCK_BLOCKED)
  474. break;
  475. /* Wait on an NLM blocking lock */
  476. status = nlmclnt_block(block, req, NLMCLNT_POLL_TIMEOUT);
  477. /* if we were interrupted. Send a CANCEL request to the server
  478. * and exit
  479. */
  480. if (status < 0)
  481. goto out_unblock;
  482. if (resp->status != NLM_LCK_BLOCKED)
  483. break;
  484. }
  485. if (resp->status == NLM_LCK_GRANTED) {
  486. fl->fl_u.nfs_fl.state = host->h_state;
  487. fl->fl_flags |= FL_SLEEP;
  488. /* Ensure the resulting lock will get added to granted list */
  489. do_vfs_lock(fl);
  490. }
  491. status = nlm_stat_to_errno(resp->status);
  492. out_unblock:
  493. nlmclnt_finish_block(block);
  494. /* Cancel the blocked request if it is still pending */
  495. if (resp->status == NLM_LCK_BLOCKED)
  496. nlmclnt_cancel(host, req->a_args.block, fl);
  497. out:
  498. nlmclnt_release_lockargs(req);
  499. return status;
  500. }
  501. /*
  502. * RECLAIM: Try to reclaim a lock
  503. */
  504. int
  505. nlmclnt_reclaim(struct nlm_host *host, struct file_lock *fl)
  506. {
  507. struct nlm_rqst reqst, *req;
  508. int status;
  509. req = &reqst;
  510. memset(req, 0, sizeof(*req));
  511. locks_init_lock(&req->a_args.lock.fl);
  512. locks_init_lock(&req->a_res.lock.fl);
  513. req->a_host = host;
  514. req->a_flags = 0;
  515. /* Set up the argument struct */
  516. nlmclnt_setlockargs(req, fl);
  517. req->a_args.reclaim = 1;
  518. if ((status = nlmclnt_call(req, NLMPROC_LOCK)) >= 0
  519. && req->a_res.status == NLM_LCK_GRANTED)
  520. return 0;
  521. printk(KERN_WARNING "lockd: failed to reclaim lock for pid %d "
  522. "(errno %d, status %d)\n", fl->fl_pid,
  523. status, req->a_res.status);
  524. /*
  525. * FIXME: This is a serious failure. We can
  526. *
  527. * a. Ignore the problem
  528. * b. Send the owning process some signal (Linux doesn't have
  529. * SIGLOST, though...)
  530. * c. Retry the operation
  531. *
  532. * Until someone comes up with a simple implementation
  533. * for b or c, I'll choose option a.
  534. */
  535. return -ENOLCK;
  536. }
  537. /*
  538. * UNLOCK: remove an existing lock
  539. */
  540. static int
  541. nlmclnt_unlock(struct nlm_rqst *req, struct file_lock *fl)
  542. {
  543. struct nlm_res *resp = &req->a_res;
  544. int status;
  545. /*
  546. * Note: the server is supposed to either grant us the unlock
  547. * request, or to deny it with NLM_LCK_DENIED_GRACE_PERIOD. In either
  548. * case, we want to unlock.
  549. */
  550. do_vfs_lock(fl);
  551. if (req->a_flags & RPC_TASK_ASYNC) {
  552. status = nlmclnt_async_call(req, NLMPROC_UNLOCK,
  553. &nlmclnt_unlock_ops);
  554. /* Hrmf... Do the unlock early since locks_remove_posix()
  555. * really expects us to free the lock synchronously */
  556. if (status < 0) {
  557. nlmclnt_release_lockargs(req);
  558. kfree(req);
  559. }
  560. return status;
  561. }
  562. status = nlmclnt_call(req, NLMPROC_UNLOCK);
  563. nlmclnt_release_lockargs(req);
  564. if (status < 0)
  565. return status;
  566. if (resp->status == NLM_LCK_GRANTED)
  567. return 0;
  568. if (resp->status != NLM_LCK_DENIED_NOLOCKS)
  569. printk("lockd: unexpected unlock status: %d\n", resp->status);
  570. /* What to do now? I'm out of my depth... */
  571. return -ENOLCK;
  572. }
  573. static void nlmclnt_unlock_callback(struct rpc_task *task, void *data)
  574. {
  575. struct nlm_rqst *req = data;
  576. int status = req->a_res.status;
  577. if (RPC_ASSASSINATED(task))
  578. goto die;
  579. if (task->tk_status < 0) {
  580. dprintk("lockd: unlock failed (err = %d)\n", -task->tk_status);
  581. goto retry_rebind;
  582. }
  583. if (status == NLM_LCK_DENIED_GRACE_PERIOD) {
  584. rpc_delay(task, NLMCLNT_GRACE_WAIT);
  585. goto retry_unlock;
  586. }
  587. if (status != NLM_LCK_GRANTED)
  588. printk(KERN_WARNING "lockd: unexpected unlock status: %d\n", status);
  589. die:
  590. nlm_release_host(req->a_host);
  591. nlmclnt_release_lockargs(req);
  592. kfree(req);
  593. return;
  594. retry_rebind:
  595. nlm_rebind_host(req->a_host);
  596. retry_unlock:
  597. rpc_restart_call(task);
  598. }
  599. static const struct rpc_call_ops nlmclnt_unlock_ops = {
  600. .rpc_call_done = nlmclnt_unlock_callback,
  601. };
  602. /*
  603. * Cancel a blocked lock request.
  604. * We always use an async RPC call for this in order not to hang a
  605. * process that has been Ctrl-C'ed.
  606. */
  607. static int nlmclnt_cancel(struct nlm_host *host, int block, struct file_lock *fl)
  608. {
  609. struct nlm_rqst *req;
  610. unsigned long flags;
  611. sigset_t oldset;
  612. int status;
  613. /* Block all signals while setting up call */
  614. spin_lock_irqsave(&current->sighand->siglock, flags);
  615. oldset = current->blocked;
  616. sigfillset(&current->blocked);
  617. recalc_sigpending();
  618. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  619. req = nlmclnt_alloc_call();
  620. if (!req)
  621. return -ENOMEM;
  622. req->a_host = host;
  623. req->a_flags = RPC_TASK_ASYNC;
  624. nlmclnt_setlockargs(req, fl);
  625. req->a_args.block = block;
  626. status = nlmclnt_async_call(req, NLMPROC_CANCEL, &nlmclnt_cancel_ops);
  627. if (status < 0) {
  628. nlmclnt_release_lockargs(req);
  629. kfree(req);
  630. }
  631. spin_lock_irqsave(&current->sighand->siglock, flags);
  632. current->blocked = oldset;
  633. recalc_sigpending();
  634. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  635. return status;
  636. }
  637. static void nlmclnt_cancel_callback(struct rpc_task *task, void *data)
  638. {
  639. struct nlm_rqst *req = data;
  640. if (RPC_ASSASSINATED(task))
  641. goto die;
  642. if (task->tk_status < 0) {
  643. dprintk("lockd: CANCEL call error %d, retrying.\n",
  644. task->tk_status);
  645. goto retry_cancel;
  646. }
  647. dprintk("lockd: cancel status %d (task %d)\n",
  648. req->a_res.status, task->tk_pid);
  649. switch (req->a_res.status) {
  650. case NLM_LCK_GRANTED:
  651. case NLM_LCK_DENIED_GRACE_PERIOD:
  652. case NLM_LCK_DENIED:
  653. /* Everything's good */
  654. break;
  655. case NLM_LCK_DENIED_NOLOCKS:
  656. dprintk("lockd: CANCEL failed (server has no locks)\n");
  657. goto retry_cancel;
  658. default:
  659. printk(KERN_NOTICE "lockd: weird return %d for CANCEL call\n",
  660. req->a_res.status);
  661. }
  662. die:
  663. nlm_release_host(req->a_host);
  664. nlmclnt_release_lockargs(req);
  665. kfree(req);
  666. return;
  667. retry_cancel:
  668. /* Don't ever retry more than 3 times */
  669. if (req->a_retries++ >= NLMCLNT_MAX_RETRIES)
  670. goto die;
  671. nlm_rebind_host(req->a_host);
  672. rpc_restart_call(task);
  673. rpc_delay(task, 30 * HZ);
  674. }
  675. static const struct rpc_call_ops nlmclnt_cancel_ops = {
  676. .rpc_call_done = nlmclnt_cancel_callback,
  677. };
  678. /*
  679. * Convert an NLM status code to a generic kernel errno
  680. */
  681. static int
  682. nlm_stat_to_errno(u32 status)
  683. {
  684. switch(status) {
  685. case NLM_LCK_GRANTED:
  686. return 0;
  687. case NLM_LCK_DENIED:
  688. return -EAGAIN;
  689. case NLM_LCK_DENIED_NOLOCKS:
  690. case NLM_LCK_DENIED_GRACE_PERIOD:
  691. return -ENOLCK;
  692. case NLM_LCK_BLOCKED:
  693. printk(KERN_NOTICE "lockd: unexpected status NLM_BLOCKED\n");
  694. return -ENOLCK;
  695. #ifdef CONFIG_LOCKD_V4
  696. case NLM_DEADLCK:
  697. return -EDEADLK;
  698. case NLM_ROFS:
  699. return -EROFS;
  700. case NLM_STALE_FH:
  701. return -ESTALE;
  702. case NLM_FBIG:
  703. return -EOVERFLOW;
  704. case NLM_FAILED:
  705. return -ENOLCK;
  706. #endif
  707. }
  708. printk(KERN_NOTICE "lockd: unexpected server status %d\n", status);
  709. return -ENOLCK;
  710. }