clntproc.c 21 KB

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