clntproc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  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/freezer.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(__be32 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_path.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. dprintk("lockd: call procedure %d on %s (async)\n",
  321. (int)proc, host->h_name);
  322. /* If we have no RPC client yet, create one. */
  323. clnt = nlm_bind_host(host);
  324. if (clnt == NULL)
  325. goto out_err;
  326. msg->rpc_proc = &clnt->cl_procinfo[proc];
  327. /* bootstrap and kick off the async RPC call */
  328. return rpc_call_async(clnt, msg, RPC_TASK_ASYNC, tk_ops, req);
  329. out_err:
  330. tk_ops->rpc_release(req);
  331. return -ENOLCK;
  332. }
  333. int nlm_async_call(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
  334. {
  335. struct rpc_message msg = {
  336. .rpc_argp = &req->a_args,
  337. .rpc_resp = &req->a_res,
  338. };
  339. return __nlm_async_call(req, proc, &msg, tk_ops);
  340. }
  341. int nlm_async_reply(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
  342. {
  343. struct rpc_message msg = {
  344. .rpc_argp = &req->a_res,
  345. };
  346. return __nlm_async_call(req, proc, &msg, tk_ops);
  347. }
  348. /*
  349. * TEST for the presence of a conflicting lock
  350. */
  351. static int
  352. nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl)
  353. {
  354. int status;
  355. status = nlmclnt_call(req, NLMPROC_TEST);
  356. if (status < 0)
  357. goto out;
  358. switch (req->a_res.status) {
  359. case nlm_granted:
  360. fl->fl_type = F_UNLCK;
  361. break;
  362. case nlm_lck_denied:
  363. /*
  364. * Report the conflicting lock back to the application.
  365. */
  366. fl->fl_start = req->a_res.lock.fl.fl_start;
  367. fl->fl_end = req->a_res.lock.fl.fl_start;
  368. fl->fl_type = req->a_res.lock.fl.fl_type;
  369. fl->fl_pid = 0;
  370. break;
  371. default:
  372. status = nlm_stat_to_errno(req->a_res.status);
  373. }
  374. out:
  375. nlm_release_call(req);
  376. return status;
  377. }
  378. static void nlmclnt_locks_copy_lock(struct file_lock *new, struct file_lock *fl)
  379. {
  380. new->fl_u.nfs_fl.state = fl->fl_u.nfs_fl.state;
  381. new->fl_u.nfs_fl.owner = nlm_get_lockowner(fl->fl_u.nfs_fl.owner);
  382. list_add_tail(&new->fl_u.nfs_fl.list, &fl->fl_u.nfs_fl.owner->host->h_granted);
  383. }
  384. static void nlmclnt_locks_release_private(struct file_lock *fl)
  385. {
  386. list_del(&fl->fl_u.nfs_fl.list);
  387. nlm_put_lockowner(fl->fl_u.nfs_fl.owner);
  388. }
  389. static struct file_lock_operations nlmclnt_lock_ops = {
  390. .fl_copy_lock = nlmclnt_locks_copy_lock,
  391. .fl_release_private = nlmclnt_locks_release_private,
  392. };
  393. static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host)
  394. {
  395. BUG_ON(fl->fl_ops != NULL);
  396. fl->fl_u.nfs_fl.state = 0;
  397. fl->fl_u.nfs_fl.owner = nlm_find_lockowner(host, fl->fl_owner);
  398. INIT_LIST_HEAD(&fl->fl_u.nfs_fl.list);
  399. fl->fl_ops = &nlmclnt_lock_ops;
  400. }
  401. static int do_vfs_lock(struct file_lock *fl)
  402. {
  403. int res = 0;
  404. switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
  405. case FL_POSIX:
  406. res = posix_lock_file_wait(fl->fl_file, fl);
  407. break;
  408. case FL_FLOCK:
  409. res = flock_lock_file_wait(fl->fl_file, fl);
  410. break;
  411. default:
  412. BUG();
  413. }
  414. return res;
  415. }
  416. /*
  417. * LOCK: Try to create a lock
  418. *
  419. * Programmer Harassment Alert
  420. *
  421. * When given a blocking lock request in a sync RPC call, the HPUX lockd
  422. * will faithfully return LCK_BLOCKED but never cares to notify us when
  423. * the lock could be granted. This way, our local process could hang
  424. * around forever waiting for the callback.
  425. *
  426. * Solution A: Implement busy-waiting
  427. * Solution B: Use the async version of the call (NLM_LOCK_{MSG,RES})
  428. *
  429. * For now I am implementing solution A, because I hate the idea of
  430. * re-implementing lockd for a third time in two months. The async
  431. * calls shouldn't be too hard to do, however.
  432. *
  433. * This is one of the lovely things about standards in the NFS area:
  434. * they're so soft and squishy you can't really blame HP for doing this.
  435. */
  436. static int
  437. nlmclnt_lock(struct nlm_rqst *req, struct file_lock *fl)
  438. {
  439. struct nlm_host *host = req->a_host;
  440. struct nlm_res *resp = &req->a_res;
  441. struct nlm_wait *block = NULL;
  442. unsigned char fl_flags = fl->fl_flags;
  443. int status = -ENOLCK;
  444. if (nsm_monitor(host) < 0) {
  445. printk(KERN_NOTICE "lockd: failed to monitor %s\n",
  446. host->h_name);
  447. goto out;
  448. }
  449. fl->fl_flags |= FL_ACCESS;
  450. status = do_vfs_lock(fl);
  451. if (status < 0)
  452. goto out;
  453. block = nlmclnt_prepare_block(host, fl);
  454. again:
  455. for(;;) {
  456. /* Reboot protection */
  457. fl->fl_u.nfs_fl.state = host->h_state;
  458. status = nlmclnt_call(req, NLMPROC_LOCK);
  459. if (status < 0)
  460. goto out_unblock;
  461. if (!req->a_args.block)
  462. break;
  463. /* Did a reclaimer thread notify us of a server reboot? */
  464. if (resp->status == nlm_lck_denied_grace_period)
  465. continue;
  466. if (resp->status != nlm_lck_blocked)
  467. break;
  468. /* Wait on an NLM blocking lock */
  469. status = nlmclnt_block(block, req, NLMCLNT_POLL_TIMEOUT);
  470. /* if we were interrupted. Send a CANCEL request to the server
  471. * and exit
  472. */
  473. if (status < 0)
  474. goto out_unblock;
  475. if (resp->status != nlm_lck_blocked)
  476. break;
  477. }
  478. if (resp->status == nlm_granted) {
  479. down_read(&host->h_rwsem);
  480. /* Check whether or not the server has rebooted */
  481. if (fl->fl_u.nfs_fl.state != host->h_state) {
  482. up_read(&host->h_rwsem);
  483. goto again;
  484. }
  485. /* Ensure the resulting lock will get added to granted list */
  486. fl->fl_flags = fl_flags | FL_SLEEP;
  487. if (do_vfs_lock(fl) < 0)
  488. printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
  489. up_read(&host->h_rwsem);
  490. }
  491. status = nlm_stat_to_errno(resp->status);
  492. out_unblock:
  493. nlmclnt_finish_block(block);
  494. /* Cancel the blocked request if it is still pending */
  495. if (resp->status == nlm_lck_blocked)
  496. nlmclnt_cancel(host, req->a_args.block, fl);
  497. out:
  498. nlm_release_call(req);
  499. fl->fl_flags = fl_flags;
  500. return status;
  501. }
  502. /*
  503. * RECLAIM: Try to reclaim a lock
  504. */
  505. int
  506. nlmclnt_reclaim(struct nlm_host *host, struct file_lock *fl)
  507. {
  508. struct nlm_rqst reqst, *req;
  509. int status;
  510. req = &reqst;
  511. memset(req, 0, sizeof(*req));
  512. locks_init_lock(&req->a_args.lock.fl);
  513. locks_init_lock(&req->a_res.lock.fl);
  514. req->a_host = host;
  515. req->a_flags = 0;
  516. /* Set up the argument struct */
  517. nlmclnt_setlockargs(req, fl);
  518. req->a_args.reclaim = 1;
  519. if ((status = nlmclnt_call(req, NLMPROC_LOCK)) >= 0
  520. && req->a_res.status == nlm_granted)
  521. return 0;
  522. printk(KERN_WARNING "lockd: failed to reclaim lock for pid %d "
  523. "(errno %d, status %d)\n", fl->fl_pid,
  524. status, ntohl(req->a_res.status));
  525. /*
  526. * FIXME: This is a serious failure. We can
  527. *
  528. * a. Ignore the problem
  529. * b. Send the owning process some signal (Linux doesn't have
  530. * SIGLOST, though...)
  531. * c. Retry the operation
  532. *
  533. * Until someone comes up with a simple implementation
  534. * for b or c, I'll choose option a.
  535. */
  536. return -ENOLCK;
  537. }
  538. /*
  539. * UNLOCK: remove an existing lock
  540. */
  541. static int
  542. nlmclnt_unlock(struct nlm_rqst *req, struct file_lock *fl)
  543. {
  544. struct nlm_host *host = req->a_host;
  545. struct nlm_res *resp = &req->a_res;
  546. int status = 0;
  547. /*
  548. * Note: the server is supposed to either grant us the unlock
  549. * request, or to deny it with NLM_LCK_DENIED_GRACE_PERIOD. In either
  550. * case, we want to unlock.
  551. */
  552. fl->fl_flags |= FL_EXISTS;
  553. down_read(&host->h_rwsem);
  554. if (do_vfs_lock(fl) == -ENOENT) {
  555. up_read(&host->h_rwsem);
  556. goto out;
  557. }
  558. up_read(&host->h_rwsem);
  559. if (req->a_flags & RPC_TASK_ASYNC)
  560. return nlm_async_call(req, NLMPROC_UNLOCK, &nlmclnt_unlock_ops);
  561. status = nlmclnt_call(req, NLMPROC_UNLOCK);
  562. if (status < 0)
  563. goto out;
  564. if (resp->status == nlm_granted)
  565. goto out;
  566. if (resp->status != nlm_lck_denied_nolocks)
  567. printk("lockd: unexpected unlock status: %d\n", resp->status);
  568. /* What to do now? I'm out of my depth... */
  569. status = -ENOLCK;
  570. out:
  571. nlm_release_call(req);
  572. return status;
  573. }
  574. static void nlmclnt_unlock_callback(struct rpc_task *task, void *data)
  575. {
  576. struct nlm_rqst *req = data;
  577. u32 status = ntohl(req->a_res.status);
  578. if (RPC_ASSASSINATED(task))
  579. goto die;
  580. if (task->tk_status < 0) {
  581. dprintk("lockd: unlock failed (err = %d)\n", -task->tk_status);
  582. goto retry_rebind;
  583. }
  584. if (status == NLM_LCK_DENIED_GRACE_PERIOD) {
  585. rpc_delay(task, NLMCLNT_GRACE_WAIT);
  586. goto retry_unlock;
  587. }
  588. if (status != NLM_LCK_GRANTED)
  589. printk(KERN_WARNING "lockd: unexpected unlock status: %d\n", status);
  590. die:
  591. return;
  592. retry_rebind:
  593. nlm_rebind_host(req->a_host);
  594. retry_unlock:
  595. rpc_restart_call(task);
  596. }
  597. static const struct rpc_call_ops nlmclnt_unlock_ops = {
  598. .rpc_call_done = nlmclnt_unlock_callback,
  599. .rpc_release = nlmclnt_rpc_release,
  600. };
  601. /*
  602. * Cancel a blocked lock request.
  603. * We always use an async RPC call for this in order not to hang a
  604. * process that has been Ctrl-C'ed.
  605. */
  606. static int nlmclnt_cancel(struct nlm_host *host, int block, struct file_lock *fl)
  607. {
  608. struct nlm_rqst *req;
  609. unsigned long flags;
  610. sigset_t oldset;
  611. int status;
  612. /* Block all signals while setting up call */
  613. spin_lock_irqsave(&current->sighand->siglock, flags);
  614. oldset = current->blocked;
  615. sigfillset(&current->blocked);
  616. recalc_sigpending();
  617. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  618. req = nlm_alloc_call(nlm_get_host(host));
  619. if (!req)
  620. return -ENOMEM;
  621. req->a_flags = RPC_TASK_ASYNC;
  622. nlmclnt_setlockargs(req, fl);
  623. req->a_args.block = block;
  624. status = nlm_async_call(req, NLMPROC_CANCEL, &nlmclnt_cancel_ops);
  625. spin_lock_irqsave(&current->sighand->siglock, flags);
  626. current->blocked = oldset;
  627. recalc_sigpending();
  628. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  629. return status;
  630. }
  631. static void nlmclnt_cancel_callback(struct rpc_task *task, void *data)
  632. {
  633. struct nlm_rqst *req = data;
  634. u32 status = ntohl(req->a_res.status);
  635. if (RPC_ASSASSINATED(task))
  636. goto die;
  637. if (task->tk_status < 0) {
  638. dprintk("lockd: CANCEL call error %d, retrying.\n",
  639. task->tk_status);
  640. goto retry_cancel;
  641. }
  642. dprintk("lockd: cancel status %u (task %u)\n",
  643. status, task->tk_pid);
  644. switch (status) {
  645. case NLM_LCK_GRANTED:
  646. case NLM_LCK_DENIED_GRACE_PERIOD:
  647. case NLM_LCK_DENIED:
  648. /* Everything's good */
  649. break;
  650. case NLM_LCK_DENIED_NOLOCKS:
  651. dprintk("lockd: CANCEL failed (server has no locks)\n");
  652. goto retry_cancel;
  653. default:
  654. printk(KERN_NOTICE "lockd: weird return %d for CANCEL call\n",
  655. status);
  656. }
  657. die:
  658. return;
  659. retry_cancel:
  660. /* Don't ever retry more than 3 times */
  661. if (req->a_retries++ >= NLMCLNT_MAX_RETRIES)
  662. goto die;
  663. nlm_rebind_host(req->a_host);
  664. rpc_restart_call(task);
  665. rpc_delay(task, 30 * HZ);
  666. }
  667. static const struct rpc_call_ops nlmclnt_cancel_ops = {
  668. .rpc_call_done = nlmclnt_cancel_callback,
  669. .rpc_release = nlmclnt_rpc_release,
  670. };
  671. /*
  672. * Convert an NLM status code to a generic kernel errno
  673. */
  674. static int
  675. nlm_stat_to_errno(__be32 status)
  676. {
  677. switch(ntohl(status)) {
  678. case NLM_LCK_GRANTED:
  679. return 0;
  680. case NLM_LCK_DENIED:
  681. return -EAGAIN;
  682. case NLM_LCK_DENIED_NOLOCKS:
  683. case NLM_LCK_DENIED_GRACE_PERIOD:
  684. return -ENOLCK;
  685. case NLM_LCK_BLOCKED:
  686. printk(KERN_NOTICE "lockd: unexpected status NLM_BLOCKED\n");
  687. return -ENOLCK;
  688. #ifdef CONFIG_LOCKD_V4
  689. case NLM_DEADLCK:
  690. return -EDEADLK;
  691. case NLM_ROFS:
  692. return -EROFS;
  693. case NLM_STALE_FH:
  694. return -ESTALE;
  695. case NLM_FBIG:
  696. return -EOVERFLOW;
  697. case NLM_FAILED:
  698. return -ENOLCK;
  699. #endif
  700. }
  701. printk(KERN_NOTICE "lockd: unexpected server status %d\n", status);
  702. return -ENOLCK;
  703. }