clntproc.c 20 KB

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