clntproc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. /*
  2. * linux/fs/lockd/clntproc.c
  3. *
  4. * RPC procedures for the client side NLM implementation
  5. *
  6. * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
  7. */
  8. #include <linux/module.h>
  9. #include <linux/types.h>
  10. #include <linux/errno.h>
  11. #include <linux/fs.h>
  12. #include <linux/nfs_fs.h>
  13. #include <linux/utsname.h>
  14. #include <linux/smp_lock.h>
  15. #include <linux/sunrpc/clnt.h>
  16. #include <linux/sunrpc/svc.h>
  17. #include <linux/lockd/lockd.h>
  18. #include <linux/lockd/sm_inter.h>
  19. #define NLMDBG_FACILITY NLMDBG_CLIENT
  20. #define NLMCLNT_GRACE_WAIT (5*HZ)
  21. #define NLMCLNT_POLL_TIMEOUT (30*HZ)
  22. #define NLMCLNT_MAX_RETRIES 3
  23. static int nlmclnt_test(struct nlm_rqst *, struct file_lock *);
  24. static int nlmclnt_lock(struct nlm_rqst *, struct file_lock *);
  25. static int nlmclnt_unlock(struct nlm_rqst *, struct file_lock *);
  26. static int nlm_stat_to_errno(u32 stat);
  27. static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host);
  28. static int nlmclnt_cancel(struct nlm_host *, int , struct file_lock *);
  29. static const struct rpc_call_ops nlmclnt_unlock_ops;
  30. static const struct rpc_call_ops nlmclnt_cancel_ops;
  31. /*
  32. * Cookie counter for NLM requests
  33. */
  34. static u32 nlm_cookie = 0x1234;
  35. static inline void nlmclnt_next_cookie(struct nlm_cookie *c)
  36. {
  37. memcpy(c->data, &nlm_cookie, 4);
  38. memset(c->data+4, 0, 4);
  39. c->len=4;
  40. nlm_cookie++;
  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 = (struct nlm_lockowner *)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_dentry->d_inode), sizeof(struct nfs_fh));
  117. lock->caller = system_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. system_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 nlm_host *host;
  138. struct nlm_rqst *call;
  139. sigset_t oldset;
  140. unsigned long flags;
  141. int status, proto, vers;
  142. vers = (NFS_PROTO(inode)->version == 3) ? 4 : 1;
  143. if (NFS_PROTO(inode)->version > 3) {
  144. printk(KERN_NOTICE "NFSv4 file locking not implemented!\n");
  145. return -ENOLCK;
  146. }
  147. /* Retrieve transport protocol from NFS client */
  148. proto = NFS_CLIENT(inode)->cl_xprt->prot;
  149. host = nlmclnt_lookup_host(NFS_ADDR(inode), proto, vers);
  150. if (host == NULL)
  151. return -ENOLCK;
  152. call = nlm_alloc_call(host);
  153. if (call == NULL)
  154. return -ENOMEM;
  155. nlmclnt_locks_init_private(fl, host);
  156. /* Set up the argument struct */
  157. nlmclnt_setlockargs(call, fl);
  158. /* Keep the old signal mask */
  159. spin_lock_irqsave(&current->sighand->siglock, flags);
  160. oldset = current->blocked;
  161. /* If we're cleaning up locks because the process is exiting,
  162. * perform the RPC call asynchronously. */
  163. if ((IS_SETLK(cmd) || IS_SETLKW(cmd))
  164. && fl->fl_type == F_UNLCK
  165. && (current->flags & PF_EXITING)) {
  166. sigfillset(&current->blocked); /* Mask all signals */
  167. recalc_sigpending();
  168. call->a_flags = RPC_TASK_ASYNC;
  169. }
  170. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  171. if (IS_SETLK(cmd) || IS_SETLKW(cmd)) {
  172. if (fl->fl_type != F_UNLCK) {
  173. call->a_args.block = IS_SETLKW(cmd) ? 1 : 0;
  174. status = nlmclnt_lock(call, fl);
  175. } else
  176. status = nlmclnt_unlock(call, fl);
  177. } else if (IS_GETLK(cmd))
  178. status = nlmclnt_test(call, fl);
  179. else
  180. status = -EINVAL;
  181. fl->fl_ops->fl_release_private(fl);
  182. fl->fl_ops = NULL;
  183. spin_lock_irqsave(&current->sighand->siglock, flags);
  184. current->blocked = oldset;
  185. recalc_sigpending();
  186. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  187. dprintk("lockd: clnt proc returns %d\n", status);
  188. return status;
  189. }
  190. EXPORT_SYMBOL(nlmclnt_proc);
  191. /*
  192. * Allocate an NLM RPC call struct
  193. *
  194. * Note: the caller must hold a reference to host. In case of failure,
  195. * this reference will be released.
  196. */
  197. struct nlm_rqst *nlm_alloc_call(struct nlm_host *host)
  198. {
  199. struct nlm_rqst *call;
  200. for(;;) {
  201. call = kzalloc(sizeof(*call), GFP_KERNEL);
  202. if (call != NULL) {
  203. locks_init_lock(&call->a_args.lock.fl);
  204. locks_init_lock(&call->a_res.lock.fl);
  205. call->a_host = host;
  206. return call;
  207. }
  208. if (signalled())
  209. break;
  210. printk("nlm_alloc_call: failed, waiting for memory\n");
  211. schedule_timeout_interruptible(5*HZ);
  212. }
  213. nlm_release_host(host);
  214. return NULL;
  215. }
  216. void nlm_release_call(struct nlm_rqst *call)
  217. {
  218. nlm_release_host(call->a_host);
  219. nlmclnt_release_lockargs(call);
  220. kfree(call);
  221. }
  222. static void nlmclnt_rpc_release(void *data)
  223. {
  224. return nlm_release_call(data);
  225. }
  226. static int nlm_wait_on_grace(wait_queue_head_t *queue)
  227. {
  228. DEFINE_WAIT(wait);
  229. int status = -EINTR;
  230. prepare_to_wait(queue, &wait, TASK_INTERRUPTIBLE);
  231. if (!signalled ()) {
  232. schedule_timeout(NLMCLNT_GRACE_WAIT);
  233. try_to_freeze();
  234. if (!signalled ())
  235. status = 0;
  236. }
  237. finish_wait(queue, &wait);
  238. return status;
  239. }
  240. /*
  241. * Generic NLM call
  242. */
  243. static int
  244. nlmclnt_call(struct nlm_rqst *req, u32 proc)
  245. {
  246. struct nlm_host *host = req->a_host;
  247. struct rpc_clnt *clnt;
  248. struct nlm_args *argp = &req->a_args;
  249. struct nlm_res *resp = &req->a_res;
  250. struct rpc_message msg = {
  251. .rpc_argp = argp,
  252. .rpc_resp = resp,
  253. };
  254. int status;
  255. dprintk("lockd: call procedure %d on %s\n",
  256. (int)proc, host->h_name);
  257. do {
  258. if (host->h_reclaiming && !argp->reclaim)
  259. goto in_grace_period;
  260. /* If we have no RPC client yet, create one. */
  261. if ((clnt = nlm_bind_host(host)) == NULL)
  262. return -ENOLCK;
  263. msg.rpc_proc = &clnt->cl_procinfo[proc];
  264. /* Perform the RPC call. If an error occurs, try again */
  265. if ((status = rpc_call_sync(clnt, &msg, 0)) < 0) {
  266. dprintk("lockd: rpc_call returned error %d\n", -status);
  267. switch (status) {
  268. case -EPROTONOSUPPORT:
  269. status = -EINVAL;
  270. break;
  271. case -ECONNREFUSED:
  272. case -ETIMEDOUT:
  273. case -ENOTCONN:
  274. nlm_rebind_host(host);
  275. status = -EAGAIN;
  276. break;
  277. case -ERESTARTSYS:
  278. return signalled () ? -EINTR : status;
  279. default:
  280. break;
  281. }
  282. break;
  283. } else
  284. if (resp->status == NLM_LCK_DENIED_GRACE_PERIOD) {
  285. dprintk("lockd: server in grace period\n");
  286. if (argp->reclaim) {
  287. printk(KERN_WARNING
  288. "lockd: spurious grace period reject?!\n");
  289. return -ENOLCK;
  290. }
  291. } else {
  292. if (!argp->reclaim) {
  293. /* We appear to be out of the grace period */
  294. wake_up_all(&host->h_gracewait);
  295. }
  296. dprintk("lockd: server returns status %d\n", resp->status);
  297. return 0; /* Okay, call complete */
  298. }
  299. in_grace_period:
  300. /*
  301. * The server has rebooted and appears to be in the grace
  302. * period during which locks are only allowed to be
  303. * reclaimed.
  304. * We can only back off and try again later.
  305. */
  306. status = nlm_wait_on_grace(&host->h_gracewait);
  307. } while (status == 0);
  308. return status;
  309. }
  310. /*
  311. * Generic NLM call, async version.
  312. */
  313. static int __nlm_async_call(struct nlm_rqst *req, u32 proc, struct rpc_message *msg, const struct rpc_call_ops *tk_ops)
  314. {
  315. struct nlm_host *host = req->a_host;
  316. struct rpc_clnt *clnt;
  317. int status = -ENOLCK;
  318. dprintk("lockd: call procedure %d on %s (async)\n",
  319. (int)proc, host->h_name);
  320. /* If we have no RPC client yet, create one. */
  321. clnt = nlm_bind_host(host);
  322. if (clnt == NULL)
  323. goto out_err;
  324. msg->rpc_proc = &clnt->cl_procinfo[proc];
  325. /* bootstrap and kick off the async RPC call */
  326. status = rpc_call_async(clnt, msg, RPC_TASK_ASYNC, tk_ops, req);
  327. if (status == 0)
  328. return 0;
  329. out_err:
  330. nlm_release_call(req);
  331. return status;
  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_LCK_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 (!host->h_monitored && 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_LCK_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_LCK_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, 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_LCK_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. int status = 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. if (RPC_ASSASSINATED(task))
  635. goto die;
  636. if (task->tk_status < 0) {
  637. dprintk("lockd: CANCEL call error %d, retrying.\n",
  638. task->tk_status);
  639. goto retry_cancel;
  640. }
  641. dprintk("lockd: cancel status %d (task %d)\n",
  642. req->a_res.status, task->tk_pid);
  643. switch (req->a_res.status) {
  644. case NLM_LCK_GRANTED:
  645. case NLM_LCK_DENIED_GRACE_PERIOD:
  646. case NLM_LCK_DENIED:
  647. /* Everything's good */
  648. break;
  649. case NLM_LCK_DENIED_NOLOCKS:
  650. dprintk("lockd: CANCEL failed (server has no locks)\n");
  651. goto retry_cancel;
  652. default:
  653. printk(KERN_NOTICE "lockd: weird return %d for CANCEL call\n",
  654. req->a_res.status);
  655. }
  656. die:
  657. return;
  658. retry_cancel:
  659. /* Don't ever retry more than 3 times */
  660. if (req->a_retries++ >= NLMCLNT_MAX_RETRIES)
  661. goto die;
  662. nlm_rebind_host(req->a_host);
  663. rpc_restart_call(task);
  664. rpc_delay(task, 30 * HZ);
  665. }
  666. static const struct rpc_call_ops nlmclnt_cancel_ops = {
  667. .rpc_call_done = nlmclnt_cancel_callback,
  668. .rpc_release = nlmclnt_rpc_release,
  669. };
  670. /*
  671. * Convert an NLM status code to a generic kernel errno
  672. */
  673. static int
  674. nlm_stat_to_errno(u32 status)
  675. {
  676. switch(status) {
  677. case NLM_LCK_GRANTED:
  678. return 0;
  679. case NLM_LCK_DENIED:
  680. return -EAGAIN;
  681. case NLM_LCK_DENIED_NOLOCKS:
  682. case NLM_LCK_DENIED_GRACE_PERIOD:
  683. return -ENOLCK;
  684. case NLM_LCK_BLOCKED:
  685. printk(KERN_NOTICE "lockd: unexpected status NLM_BLOCKED\n");
  686. return -ENOLCK;
  687. #ifdef CONFIG_LOCKD_V4
  688. case NLM_DEADLCK:
  689. return -EDEADLK;
  690. case NLM_ROFS:
  691. return -EROFS;
  692. case NLM_STALE_FH:
  693. return -ESTALE;
  694. case NLM_FBIG:
  695. return -EOVERFLOW;
  696. case NLM_FAILED:
  697. return -ENOLCK;
  698. #endif
  699. }
  700. printk(KERN_NOTICE "lockd: unexpected server status %d\n", status);
  701. return -ENOLCK;
  702. }