clntproc.c 21 KB

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