clntproc.c 21 KB

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