clntproc.c 20 KB

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