clntproc.c 21 KB

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