clntproc.c 20 KB

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