clntproc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  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. 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
  357. nlmsvc_async_call(struct nlm_rqst *req, u32 proc, rpc_action callback)
  358. {
  359. struct nlm_host *host = req->a_host;
  360. struct rpc_clnt *clnt;
  361. struct rpc_message msg = {
  362. .rpc_argp = &req->a_args,
  363. .rpc_resp = &req->a_res,
  364. };
  365. int status;
  366. dprintk("lockd: call procedure %d on %s (async)\n",
  367. (int)proc, host->h_name);
  368. /* If we have no RPC client yet, create one. */
  369. if ((clnt = nlm_bind_host(host)) == NULL)
  370. return -ENOLCK;
  371. msg.rpc_proc = &clnt->cl_procinfo[proc];
  372. /* bootstrap and kick off the async RPC call */
  373. status = rpc_call_async(clnt, &msg, RPC_TASK_ASYNC, callback, req);
  374. return status;
  375. }
  376. static int
  377. nlmclnt_async_call(struct nlm_rqst *req, u32 proc, rpc_action callback)
  378. {
  379. struct nlm_host *host = req->a_host;
  380. struct rpc_clnt *clnt;
  381. struct nlm_args *argp = &req->a_args;
  382. struct nlm_res *resp = &req->a_res;
  383. struct rpc_message msg = {
  384. .rpc_argp = argp,
  385. .rpc_resp = resp,
  386. };
  387. int status;
  388. dprintk("lockd: call procedure %d on %s (async)\n",
  389. (int)proc, host->h_name);
  390. /* If we have no RPC client yet, create one. */
  391. if ((clnt = nlm_bind_host(host)) == NULL)
  392. return -ENOLCK;
  393. msg.rpc_proc = &clnt->cl_procinfo[proc];
  394. /* Increment host refcount */
  395. nlm_get_host(host);
  396. /* bootstrap and kick off the async RPC call */
  397. status = rpc_call_async(clnt, &msg, RPC_TASK_ASYNC, callback, req);
  398. if (status < 0)
  399. nlm_release_host(host);
  400. return status;
  401. }
  402. /*
  403. * TEST for the presence of a conflicting lock
  404. */
  405. static int
  406. nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl)
  407. {
  408. int status;
  409. status = nlmclnt_call(req, NLMPROC_TEST);
  410. nlmclnt_release_lockargs(req);
  411. if (status < 0)
  412. return status;
  413. status = req->a_res.status;
  414. if (status == NLM_LCK_GRANTED) {
  415. fl->fl_type = F_UNLCK;
  416. } if (status == NLM_LCK_DENIED) {
  417. /*
  418. * Report the conflicting lock back to the application.
  419. */
  420. locks_copy_lock(fl, &req->a_res.lock.fl);
  421. fl->fl_pid = 0;
  422. } else {
  423. return nlm_stat_to_errno(req->a_res.status);
  424. }
  425. return 0;
  426. }
  427. static void nlmclnt_locks_copy_lock(struct file_lock *new, struct file_lock *fl)
  428. {
  429. memcpy(&new->fl_u.nfs_fl, &fl->fl_u.nfs_fl, sizeof(new->fl_u.nfs_fl));
  430. nlm_get_lockowner(new->fl_u.nfs_fl.owner);
  431. }
  432. static void nlmclnt_locks_release_private(struct file_lock *fl)
  433. {
  434. nlm_put_lockowner(fl->fl_u.nfs_fl.owner);
  435. fl->fl_ops = NULL;
  436. }
  437. static struct file_lock_operations nlmclnt_lock_ops = {
  438. .fl_copy_lock = nlmclnt_locks_copy_lock,
  439. .fl_release_private = nlmclnt_locks_release_private,
  440. };
  441. static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host)
  442. {
  443. BUG_ON(fl->fl_ops != NULL);
  444. fl->fl_u.nfs_fl.state = 0;
  445. fl->fl_u.nfs_fl.flags = 0;
  446. fl->fl_u.nfs_fl.owner = nlm_find_lockowner(host, fl->fl_owner);
  447. fl->fl_ops = &nlmclnt_lock_ops;
  448. }
  449. static void do_vfs_lock(struct file_lock *fl)
  450. {
  451. int res = 0;
  452. switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
  453. case FL_POSIX:
  454. res = posix_lock_file_wait(fl->fl_file, fl);
  455. break;
  456. case FL_FLOCK:
  457. res = flock_lock_file_wait(fl->fl_file, fl);
  458. break;
  459. default:
  460. BUG();
  461. }
  462. if (res < 0)
  463. printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n",
  464. __FUNCTION__);
  465. }
  466. /*
  467. * LOCK: Try to create a lock
  468. *
  469. * Programmer Harassment Alert
  470. *
  471. * When given a blocking lock request in a sync RPC call, the HPUX lockd
  472. * will faithfully return LCK_BLOCKED but never cares to notify us when
  473. * the lock could be granted. This way, our local process could hang
  474. * around forever waiting for the callback.
  475. *
  476. * Solution A: Implement busy-waiting
  477. * Solution B: Use the async version of the call (NLM_LOCK_{MSG,RES})
  478. *
  479. * For now I am implementing solution A, because I hate the idea of
  480. * re-implementing lockd for a third time in two months. The async
  481. * calls shouldn't be too hard to do, however.
  482. *
  483. * This is one of the lovely things about standards in the NFS area:
  484. * they're so soft and squishy you can't really blame HP for doing this.
  485. */
  486. static int
  487. nlmclnt_lock(struct nlm_rqst *req, struct file_lock *fl)
  488. {
  489. struct nlm_host *host = req->a_host;
  490. struct nlm_res *resp = &req->a_res;
  491. long timeout;
  492. int status;
  493. if (!host->h_monitored && nsm_monitor(host) < 0) {
  494. printk(KERN_NOTICE "lockd: failed to monitor %s\n",
  495. host->h_name);
  496. status = -ENOLCK;
  497. goto out;
  498. }
  499. if (req->a_args.block) {
  500. status = nlmclnt_prepare_block(req, host, fl);
  501. if (status < 0)
  502. goto out;
  503. }
  504. for(;;) {
  505. status = nlmclnt_call(req, NLMPROC_LOCK);
  506. if (status < 0)
  507. goto out_unblock;
  508. if (resp->status != NLM_LCK_BLOCKED)
  509. break;
  510. /* Wait on an NLM blocking lock */
  511. timeout = nlmclnt_block(req, NLMCLNT_POLL_TIMEOUT);
  512. /* Did a reclaimer thread notify us of a server reboot? */
  513. if (resp->status == NLM_LCK_DENIED_GRACE_PERIOD)
  514. continue;
  515. if (resp->status != NLM_LCK_BLOCKED)
  516. break;
  517. if (timeout >= 0)
  518. continue;
  519. /* We were interrupted. Send a CANCEL request to the server
  520. * and exit
  521. */
  522. status = (int)timeout;
  523. goto out_unblock;
  524. }
  525. if (resp->status == NLM_LCK_GRANTED) {
  526. fl->fl_u.nfs_fl.state = host->h_state;
  527. fl->fl_u.nfs_fl.flags |= NFS_LCK_GRANTED;
  528. fl->fl_flags |= FL_SLEEP;
  529. do_vfs_lock(fl);
  530. }
  531. status = nlm_stat_to_errno(resp->status);
  532. out_unblock:
  533. nlmclnt_finish_block(req);
  534. /* Cancel the blocked request if it is still pending */
  535. if (resp->status == NLM_LCK_BLOCKED)
  536. nlmclnt_cancel(host, fl);
  537. out:
  538. nlmclnt_release_lockargs(req);
  539. return status;
  540. }
  541. /*
  542. * RECLAIM: Try to reclaim a lock
  543. */
  544. int
  545. nlmclnt_reclaim(struct nlm_host *host, struct file_lock *fl)
  546. {
  547. struct nlm_rqst reqst, *req;
  548. int status;
  549. req = &reqst;
  550. memset(req, 0, sizeof(*req));
  551. locks_init_lock(&req->a_args.lock.fl);
  552. locks_init_lock(&req->a_res.lock.fl);
  553. req->a_host = host;
  554. req->a_flags = 0;
  555. /* Set up the argument struct */
  556. nlmclnt_setlockargs(req, fl);
  557. req->a_args.reclaim = 1;
  558. if ((status = nlmclnt_call(req, NLMPROC_LOCK)) >= 0
  559. && req->a_res.status == NLM_LCK_GRANTED)
  560. return 0;
  561. printk(KERN_WARNING "lockd: failed to reclaim lock for pid %d "
  562. "(errno %d, status %d)\n", fl->fl_pid,
  563. status, req->a_res.status);
  564. /*
  565. * FIXME: This is a serious failure. We can
  566. *
  567. * a. Ignore the problem
  568. * b. Send the owning process some signal (Linux doesn't have
  569. * SIGLOST, though...)
  570. * c. Retry the operation
  571. *
  572. * Until someone comes up with a simple implementation
  573. * for b or c, I'll choose option a.
  574. */
  575. return -ENOLCK;
  576. }
  577. /*
  578. * UNLOCK: remove an existing lock
  579. */
  580. static int
  581. nlmclnt_unlock(struct nlm_rqst *req, struct file_lock *fl)
  582. {
  583. struct nlm_res *resp = &req->a_res;
  584. int status;
  585. /* Clean the GRANTED flag now so the lock doesn't get
  586. * reclaimed while we're stuck in the unlock call. */
  587. fl->fl_u.nfs_fl.flags &= ~NFS_LCK_GRANTED;
  588. if (req->a_flags & RPC_TASK_ASYNC) {
  589. status = nlmclnt_async_call(req, NLMPROC_UNLOCK,
  590. nlmclnt_unlock_callback);
  591. /* Hrmf... Do the unlock early since locks_remove_posix()
  592. * really expects us to free the lock synchronously */
  593. do_vfs_lock(fl);
  594. if (status < 0) {
  595. nlmclnt_release_lockargs(req);
  596. kfree(req);
  597. }
  598. return status;
  599. }
  600. status = nlmclnt_call(req, NLMPROC_UNLOCK);
  601. nlmclnt_release_lockargs(req);
  602. if (status < 0)
  603. return status;
  604. do_vfs_lock(fl);
  605. if (resp->status == NLM_LCK_GRANTED)
  606. return 0;
  607. if (resp->status != NLM_LCK_DENIED_NOLOCKS)
  608. printk("lockd: unexpected unlock status: %d\n", resp->status);
  609. /* What to do now? I'm out of my depth... */
  610. return -ENOLCK;
  611. }
  612. static void
  613. nlmclnt_unlock_callback(struct rpc_task *task)
  614. {
  615. struct nlm_rqst *req = (struct nlm_rqst *) task->tk_calldata;
  616. int status = req->a_res.status;
  617. if (RPC_ASSASSINATED(task))
  618. goto die;
  619. if (task->tk_status < 0) {
  620. dprintk("lockd: unlock failed (err = %d)\n", -task->tk_status);
  621. goto retry_rebind;
  622. }
  623. if (status == NLM_LCK_DENIED_GRACE_PERIOD) {
  624. rpc_delay(task, NLMCLNT_GRACE_WAIT);
  625. goto retry_unlock;
  626. }
  627. if (status != NLM_LCK_GRANTED)
  628. printk(KERN_WARNING "lockd: unexpected unlock status: %d\n", status);
  629. die:
  630. nlm_release_host(req->a_host);
  631. nlmclnt_release_lockargs(req);
  632. kfree(req);
  633. return;
  634. retry_rebind:
  635. nlm_rebind_host(req->a_host);
  636. retry_unlock:
  637. rpc_restart_call(task);
  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. int
  645. nlmclnt_cancel(struct nlm_host *host, struct file_lock *fl)
  646. {
  647. struct nlm_rqst *req;
  648. unsigned long flags;
  649. sigset_t oldset;
  650. int status;
  651. /* Block all signals while setting up call */
  652. spin_lock_irqsave(&current->sighand->siglock, flags);
  653. oldset = current->blocked;
  654. sigfillset(&current->blocked);
  655. recalc_sigpending();
  656. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  657. req = nlmclnt_alloc_call();
  658. if (!req)
  659. return -ENOMEM;
  660. req->a_host = host;
  661. req->a_flags = RPC_TASK_ASYNC;
  662. nlmclnt_setlockargs(req, fl);
  663. status = nlmclnt_async_call(req, NLMPROC_CANCEL,
  664. nlmclnt_cancel_callback);
  665. if (status < 0) {
  666. nlmclnt_release_lockargs(req);
  667. kfree(req);
  668. }
  669. spin_lock_irqsave(&current->sighand->siglock, flags);
  670. current->blocked = oldset;
  671. recalc_sigpending();
  672. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  673. return status;
  674. }
  675. static void
  676. nlmclnt_cancel_callback(struct rpc_task *task)
  677. {
  678. struct nlm_rqst *req = (struct nlm_rqst *) task->tk_calldata;
  679. if (RPC_ASSASSINATED(task))
  680. goto die;
  681. if (task->tk_status < 0) {
  682. dprintk("lockd: CANCEL call error %d, retrying.\n",
  683. task->tk_status);
  684. goto retry_cancel;
  685. }
  686. dprintk("lockd: cancel status %d (task %d)\n",
  687. req->a_res.status, task->tk_pid);
  688. switch (req->a_res.status) {
  689. case NLM_LCK_GRANTED:
  690. case NLM_LCK_DENIED_GRACE_PERIOD:
  691. /* Everything's good */
  692. break;
  693. case NLM_LCK_DENIED_NOLOCKS:
  694. dprintk("lockd: CANCEL failed (server has no locks)\n");
  695. goto retry_cancel;
  696. default:
  697. printk(KERN_NOTICE "lockd: weird return %d for CANCEL call\n",
  698. req->a_res.status);
  699. }
  700. die:
  701. nlm_release_host(req->a_host);
  702. nlmclnt_release_lockargs(req);
  703. kfree(req);
  704. return;
  705. retry_cancel:
  706. nlm_rebind_host(req->a_host);
  707. rpc_restart_call(task);
  708. rpc_delay(task, 30 * HZ);
  709. }
  710. /*
  711. * Convert an NLM status code to a generic kernel errno
  712. */
  713. static int
  714. nlm_stat_to_errno(u32 status)
  715. {
  716. switch(status) {
  717. case NLM_LCK_GRANTED:
  718. return 0;
  719. case NLM_LCK_DENIED:
  720. return -EAGAIN;
  721. case NLM_LCK_DENIED_NOLOCKS:
  722. case NLM_LCK_DENIED_GRACE_PERIOD:
  723. return -ENOLCK;
  724. case NLM_LCK_BLOCKED:
  725. printk(KERN_NOTICE "lockd: unexpected status NLM_BLOCKED\n");
  726. return -ENOLCK;
  727. #ifdef CONFIG_LOCKD_V4
  728. case NLM_DEADLCK:
  729. return -EDEADLK;
  730. case NLM_ROFS:
  731. return -EROFS;
  732. case NLM_STALE_FH:
  733. return -ESTALE;
  734. case NLM_FBIG:
  735. return -EOVERFLOW;
  736. case NLM_FAILED:
  737. return -ENOLCK;
  738. #endif
  739. }
  740. printk(KERN_NOTICE "lockd: unexpected server status %d\n", status);
  741. return -ENOLCK;
  742. }