clntproc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  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 void nlmclnt_unlock_callback(struct rpc_task *);
  27. static void nlmclnt_cancel_callback(struct rpc_task *);
  28. static int nlm_stat_to_errno(u32 stat);
  29. static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host);
  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. clnt->cl_chatty = nfssrv->client->cl_chatty;
  199. }
  200. /* Keep the old signal mask */
  201. spin_lock_irqsave(&current->sighand->siglock, flags);
  202. oldset = current->blocked;
  203. /* If we're cleaning up locks because the process is exiting,
  204. * perform the RPC call asynchronously. */
  205. if ((IS_SETLK(cmd) || IS_SETLKW(cmd))
  206. && fl->fl_type == F_UNLCK
  207. && (current->flags & PF_EXITING)) {
  208. sigfillset(&current->blocked); /* Mask all signals */
  209. recalc_sigpending();
  210. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  211. call = nlmclnt_alloc_call();
  212. if (!call) {
  213. status = -ENOMEM;
  214. goto out_restore;
  215. }
  216. call->a_flags = RPC_TASK_ASYNC;
  217. } else {
  218. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  219. memset(call, 0, sizeof(*call));
  220. locks_init_lock(&call->a_args.lock.fl);
  221. locks_init_lock(&call->a_res.lock.fl);
  222. }
  223. call->a_host = host;
  224. nlmclnt_locks_init_private(fl, host);
  225. /* Set up the argument struct */
  226. nlmclnt_setlockargs(call, fl);
  227. if (IS_SETLK(cmd) || IS_SETLKW(cmd)) {
  228. if (fl->fl_type != F_UNLCK) {
  229. call->a_args.block = IS_SETLKW(cmd) ? 1 : 0;
  230. status = nlmclnt_lock(call, fl);
  231. } else
  232. status = nlmclnt_unlock(call, fl);
  233. } else if (IS_GETLK(cmd))
  234. status = nlmclnt_test(call, fl);
  235. else
  236. status = -EINVAL;
  237. out_restore:
  238. spin_lock_irqsave(&current->sighand->siglock, flags);
  239. current->blocked = oldset;
  240. recalc_sigpending();
  241. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  242. done:
  243. dprintk("lockd: clnt proc returns %d\n", status);
  244. nlm_release_host(host);
  245. return status;
  246. }
  247. EXPORT_SYMBOL(nlmclnt_proc);
  248. /*
  249. * Allocate an NLM RPC call struct
  250. */
  251. struct nlm_rqst *
  252. nlmclnt_alloc_call(void)
  253. {
  254. struct nlm_rqst *call;
  255. while (!signalled()) {
  256. call = (struct nlm_rqst *) kmalloc(sizeof(struct nlm_rqst), GFP_KERNEL);
  257. if (call) {
  258. memset(call, 0, sizeof(*call));
  259. locks_init_lock(&call->a_args.lock.fl);
  260. locks_init_lock(&call->a_res.lock.fl);
  261. return call;
  262. }
  263. printk("nlmclnt_alloc_call: failed, waiting for memory\n");
  264. schedule_timeout_interruptible(5*HZ);
  265. }
  266. return NULL;
  267. }
  268. static int nlm_wait_on_grace(wait_queue_head_t *queue)
  269. {
  270. DEFINE_WAIT(wait);
  271. int status = -EINTR;
  272. prepare_to_wait(queue, &wait, TASK_INTERRUPTIBLE);
  273. if (!signalled ()) {
  274. schedule_timeout(NLMCLNT_GRACE_WAIT);
  275. try_to_freeze();
  276. if (!signalled ())
  277. status = 0;
  278. }
  279. finish_wait(queue, &wait);
  280. return status;
  281. }
  282. /*
  283. * Generic NLM call
  284. */
  285. static int
  286. nlmclnt_call(struct nlm_rqst *req, u32 proc)
  287. {
  288. struct nlm_host *host = req->a_host;
  289. struct rpc_clnt *clnt;
  290. struct nlm_args *argp = &req->a_args;
  291. struct nlm_res *resp = &req->a_res;
  292. struct rpc_message msg = {
  293. .rpc_argp = argp,
  294. .rpc_resp = resp,
  295. };
  296. int status;
  297. dprintk("lockd: call procedure %d on %s\n",
  298. (int)proc, host->h_name);
  299. do {
  300. if (host->h_reclaiming && !argp->reclaim)
  301. goto in_grace_period;
  302. /* If we have no RPC client yet, create one. */
  303. if ((clnt = nlm_bind_host(host)) == NULL)
  304. return -ENOLCK;
  305. msg.rpc_proc = &clnt->cl_procinfo[proc];
  306. /* Perform the RPC call. If an error occurs, try again */
  307. if ((status = rpc_call_sync(clnt, &msg, 0)) < 0) {
  308. dprintk("lockd: rpc_call returned error %d\n", -status);
  309. switch (status) {
  310. case -EPROTONOSUPPORT:
  311. status = -EINVAL;
  312. break;
  313. case -ECONNREFUSED:
  314. case -ETIMEDOUT:
  315. case -ENOTCONN:
  316. nlm_rebind_host(host);
  317. status = -EAGAIN;
  318. break;
  319. case -ERESTARTSYS:
  320. return signalled () ? -EINTR : status;
  321. default:
  322. break;
  323. }
  324. break;
  325. } else
  326. if (resp->status == NLM_LCK_DENIED_GRACE_PERIOD) {
  327. dprintk("lockd: server in grace period\n");
  328. if (argp->reclaim) {
  329. printk(KERN_WARNING
  330. "lockd: spurious grace period reject?!\n");
  331. return -ENOLCK;
  332. }
  333. } else {
  334. if (!argp->reclaim) {
  335. /* We appear to be out of the grace period */
  336. wake_up_all(&host->h_gracewait);
  337. }
  338. dprintk("lockd: server returns status %d\n", resp->status);
  339. return 0; /* Okay, call complete */
  340. }
  341. in_grace_period:
  342. /*
  343. * The server has rebooted and appears to be in the grace
  344. * period during which locks are only allowed to be
  345. * reclaimed.
  346. * We can only back off and try again later.
  347. */
  348. status = nlm_wait_on_grace(&host->h_gracewait);
  349. } while (status == 0);
  350. return status;
  351. }
  352. /*
  353. * Generic NLM call, async version.
  354. */
  355. int
  356. nlmsvc_async_call(struct nlm_rqst *req, u32 proc, rpc_action callback)
  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, callback, req);
  373. return status;
  374. }
  375. static int
  376. nlmclnt_async_call(struct nlm_rqst *req, u32 proc, rpc_action callback)
  377. {
  378. struct nlm_host *host = req->a_host;
  379. struct rpc_clnt *clnt;
  380. struct nlm_args *argp = &req->a_args;
  381. struct nlm_res *resp = &req->a_res;
  382. struct rpc_message msg = {
  383. .rpc_argp = argp,
  384. .rpc_resp = resp,
  385. };
  386. int status;
  387. dprintk("lockd: call procedure %d on %s (async)\n",
  388. (int)proc, host->h_name);
  389. /* If we have no RPC client yet, create one. */
  390. if ((clnt = nlm_bind_host(host)) == NULL)
  391. return -ENOLCK;
  392. msg.rpc_proc = &clnt->cl_procinfo[proc];
  393. /* Increment host refcount */
  394. nlm_get_host(host);
  395. /* bootstrap and kick off the async RPC call */
  396. status = rpc_call_async(clnt, &msg, RPC_TASK_ASYNC, callback, req);
  397. if (status < 0)
  398. nlm_release_host(host);
  399. return status;
  400. }
  401. /*
  402. * TEST for the presence of a conflicting lock
  403. */
  404. static int
  405. nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl)
  406. {
  407. int status;
  408. status = nlmclnt_call(req, NLMPROC_TEST);
  409. nlmclnt_release_lockargs(req);
  410. if (status < 0)
  411. return status;
  412. status = req->a_res.status;
  413. if (status == NLM_LCK_GRANTED) {
  414. fl->fl_type = F_UNLCK;
  415. } if (status == NLM_LCK_DENIED) {
  416. /*
  417. * Report the conflicting lock back to the application.
  418. */
  419. locks_copy_lock(fl, &req->a_res.lock.fl);
  420. fl->fl_pid = 0;
  421. } else {
  422. return nlm_stat_to_errno(req->a_res.status);
  423. }
  424. return 0;
  425. }
  426. static void nlmclnt_locks_copy_lock(struct file_lock *new, struct file_lock *fl)
  427. {
  428. memcpy(&new->fl_u.nfs_fl, &fl->fl_u.nfs_fl, sizeof(new->fl_u.nfs_fl));
  429. nlm_get_lockowner(new->fl_u.nfs_fl.owner);
  430. }
  431. static void nlmclnt_locks_release_private(struct file_lock *fl)
  432. {
  433. nlm_put_lockowner(fl->fl_u.nfs_fl.owner);
  434. fl->fl_ops = NULL;
  435. }
  436. static struct file_lock_operations nlmclnt_lock_ops = {
  437. .fl_copy_lock = nlmclnt_locks_copy_lock,
  438. .fl_release_private = nlmclnt_locks_release_private,
  439. };
  440. static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host)
  441. {
  442. BUG_ON(fl->fl_ops != NULL);
  443. fl->fl_u.nfs_fl.state = 0;
  444. fl->fl_u.nfs_fl.flags = 0;
  445. fl->fl_u.nfs_fl.owner = nlm_find_lockowner(host, fl->fl_owner);
  446. fl->fl_ops = &nlmclnt_lock_ops;
  447. }
  448. static void do_vfs_lock(struct file_lock *fl)
  449. {
  450. int res = 0;
  451. switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
  452. case FL_POSIX:
  453. res = posix_lock_file_wait(fl->fl_file, fl);
  454. break;
  455. case FL_FLOCK:
  456. res = flock_lock_file_wait(fl->fl_file, fl);
  457. break;
  458. default:
  459. BUG();
  460. }
  461. if (res < 0)
  462. printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n",
  463. __FUNCTION__);
  464. }
  465. /*
  466. * LOCK: Try to create a lock
  467. *
  468. * Programmer Harassment Alert
  469. *
  470. * When given a blocking lock request in a sync RPC call, the HPUX lockd
  471. * will faithfully return LCK_BLOCKED but never cares to notify us when
  472. * the lock could be granted. This way, our local process could hang
  473. * around forever waiting for the callback.
  474. *
  475. * Solution A: Implement busy-waiting
  476. * Solution B: Use the async version of the call (NLM_LOCK_{MSG,RES})
  477. *
  478. * For now I am implementing solution A, because I hate the idea of
  479. * re-implementing lockd for a third time in two months. The async
  480. * calls shouldn't be too hard to do, however.
  481. *
  482. * This is one of the lovely things about standards in the NFS area:
  483. * they're so soft and squishy you can't really blame HP for doing this.
  484. */
  485. static int
  486. nlmclnt_lock(struct nlm_rqst *req, struct file_lock *fl)
  487. {
  488. struct nlm_host *host = req->a_host;
  489. struct nlm_res *resp = &req->a_res;
  490. long timeout;
  491. int status;
  492. if (!host->h_monitored && nsm_monitor(host) < 0) {
  493. printk(KERN_NOTICE "lockd: failed to monitor %s\n",
  494. host->h_name);
  495. status = -ENOLCK;
  496. goto out;
  497. }
  498. if (req->a_args.block) {
  499. status = nlmclnt_prepare_block(req, host, fl);
  500. if (status < 0)
  501. goto out;
  502. }
  503. for(;;) {
  504. status = nlmclnt_call(req, NLMPROC_LOCK);
  505. if (status < 0)
  506. goto out_unblock;
  507. if (resp->status != NLM_LCK_BLOCKED)
  508. break;
  509. /* Wait on an NLM blocking lock */
  510. timeout = nlmclnt_block(req, NLMCLNT_POLL_TIMEOUT);
  511. /* Did a reclaimer thread notify us of a server reboot? */
  512. if (resp->status == NLM_LCK_DENIED_GRACE_PERIOD)
  513. continue;
  514. if (resp->status != NLM_LCK_BLOCKED)
  515. break;
  516. if (timeout >= 0)
  517. continue;
  518. /* We were interrupted. Send a CANCEL request to the server
  519. * and exit
  520. */
  521. status = (int)timeout;
  522. goto out_unblock;
  523. }
  524. if (resp->status == NLM_LCK_GRANTED) {
  525. fl->fl_u.nfs_fl.state = host->h_state;
  526. fl->fl_u.nfs_fl.flags |= NFS_LCK_GRANTED;
  527. fl->fl_flags |= FL_SLEEP;
  528. do_vfs_lock(fl);
  529. }
  530. status = nlm_stat_to_errno(resp->status);
  531. out_unblock:
  532. nlmclnt_finish_block(req);
  533. /* Cancel the blocked request if it is still pending */
  534. if (resp->status == NLM_LCK_BLOCKED)
  535. nlmclnt_cancel(host, fl);
  536. out:
  537. nlmclnt_release_lockargs(req);
  538. return status;
  539. }
  540. /*
  541. * RECLAIM: Try to reclaim a lock
  542. */
  543. int
  544. nlmclnt_reclaim(struct nlm_host *host, struct file_lock *fl)
  545. {
  546. struct nlm_rqst reqst, *req;
  547. int status;
  548. req = &reqst;
  549. memset(req, 0, sizeof(*req));
  550. locks_init_lock(&req->a_args.lock.fl);
  551. locks_init_lock(&req->a_res.lock.fl);
  552. req->a_host = host;
  553. req->a_flags = 0;
  554. /* Set up the argument struct */
  555. nlmclnt_setlockargs(req, fl);
  556. req->a_args.reclaim = 1;
  557. if ((status = nlmclnt_call(req, NLMPROC_LOCK)) >= 0
  558. && req->a_res.status == NLM_LCK_GRANTED)
  559. return 0;
  560. printk(KERN_WARNING "lockd: failed to reclaim lock for pid %d "
  561. "(errno %d, status %d)\n", fl->fl_pid,
  562. status, req->a_res.status);
  563. /*
  564. * FIXME: This is a serious failure. We can
  565. *
  566. * a. Ignore the problem
  567. * b. Send the owning process some signal (Linux doesn't have
  568. * SIGLOST, though...)
  569. * c. Retry the operation
  570. *
  571. * Until someone comes up with a simple implementation
  572. * for b or c, I'll choose option a.
  573. */
  574. return -ENOLCK;
  575. }
  576. /*
  577. * UNLOCK: remove an existing lock
  578. */
  579. static int
  580. nlmclnt_unlock(struct nlm_rqst *req, struct file_lock *fl)
  581. {
  582. struct nlm_res *resp = &req->a_res;
  583. int status;
  584. /* Clean the GRANTED flag now so the lock doesn't get
  585. * reclaimed while we're stuck in the unlock call. */
  586. fl->fl_u.nfs_fl.flags &= ~NFS_LCK_GRANTED;
  587. if (req->a_flags & RPC_TASK_ASYNC) {
  588. status = nlmclnt_async_call(req, NLMPROC_UNLOCK,
  589. nlmclnt_unlock_callback);
  590. /* Hrmf... Do the unlock early since locks_remove_posix()
  591. * really expects us to free the lock synchronously */
  592. do_vfs_lock(fl);
  593. if (status < 0) {
  594. nlmclnt_release_lockargs(req);
  595. kfree(req);
  596. }
  597. return status;
  598. }
  599. status = nlmclnt_call(req, NLMPROC_UNLOCK);
  600. nlmclnt_release_lockargs(req);
  601. if (status < 0)
  602. return status;
  603. do_vfs_lock(fl);
  604. if (resp->status == NLM_LCK_GRANTED)
  605. return 0;
  606. if (resp->status != NLM_LCK_DENIED_NOLOCKS)
  607. printk("lockd: unexpected unlock status: %d\n", resp->status);
  608. /* What to do now? I'm out of my depth... */
  609. return -ENOLCK;
  610. }
  611. static void
  612. nlmclnt_unlock_callback(struct rpc_task *task)
  613. {
  614. struct nlm_rqst *req = (struct nlm_rqst *) task->tk_calldata;
  615. int status = req->a_res.status;
  616. if (RPC_ASSASSINATED(task))
  617. goto die;
  618. if (task->tk_status < 0) {
  619. dprintk("lockd: unlock failed (err = %d)\n", -task->tk_status);
  620. goto retry_rebind;
  621. }
  622. if (status == NLM_LCK_DENIED_GRACE_PERIOD) {
  623. rpc_delay(task, NLMCLNT_GRACE_WAIT);
  624. goto retry_unlock;
  625. }
  626. if (status != NLM_LCK_GRANTED)
  627. printk(KERN_WARNING "lockd: unexpected unlock status: %d\n", status);
  628. die:
  629. nlm_release_host(req->a_host);
  630. nlmclnt_release_lockargs(req);
  631. kfree(req);
  632. return;
  633. retry_rebind:
  634. nlm_rebind_host(req->a_host);
  635. retry_unlock:
  636. rpc_restart_call(task);
  637. }
  638. /*
  639. * Cancel a blocked lock request.
  640. * We always use an async RPC call for this in order not to hang a
  641. * process that has been Ctrl-C'ed.
  642. */
  643. int
  644. nlmclnt_cancel(struct nlm_host *host, 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. status = nlmclnt_async_call(req, NLMPROC_CANCEL,
  663. nlmclnt_cancel_callback);
  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
  675. nlmclnt_cancel_callback(struct rpc_task *task)
  676. {
  677. struct nlm_rqst *req = (struct nlm_rqst *) task->tk_calldata;
  678. if (RPC_ASSASSINATED(task))
  679. goto die;
  680. if (task->tk_status < 0) {
  681. dprintk("lockd: CANCEL call error %d, retrying.\n",
  682. task->tk_status);
  683. goto retry_cancel;
  684. }
  685. dprintk("lockd: cancel status %d (task %d)\n",
  686. req->a_res.status, task->tk_pid);
  687. switch (req->a_res.status) {
  688. case NLM_LCK_GRANTED:
  689. case NLM_LCK_DENIED_GRACE_PERIOD:
  690. /* Everything's good */
  691. break;
  692. case NLM_LCK_DENIED_NOLOCKS:
  693. dprintk("lockd: CANCEL failed (server has no locks)\n");
  694. goto retry_cancel;
  695. default:
  696. printk(KERN_NOTICE "lockd: weird return %d for CANCEL call\n",
  697. req->a_res.status);
  698. }
  699. die:
  700. nlm_release_host(req->a_host);
  701. nlmclnt_release_lockargs(req);
  702. kfree(req);
  703. return;
  704. retry_cancel:
  705. nlm_rebind_host(req->a_host);
  706. rpc_restart_call(task);
  707. rpc_delay(task, 30 * HZ);
  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. }