clntproc.c 19 KB

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