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 = 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 rpc_clnt *client = NFS_CLIENT(inode);
  138. struct sockaddr_in addr;
  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. if (host == NULL)
  152. return -ENOLCK;
  153. call = nlm_alloc_call(host);
  154. if (call == NULL)
  155. return -ENOMEM;
  156. nlmclnt_locks_init_private(fl, host);
  157. /* Set up the argument struct */
  158. nlmclnt_setlockargs(call, fl);
  159. /* Keep the old signal mask */
  160. spin_lock_irqsave(&current->sighand->siglock, flags);
  161. oldset = current->blocked;
  162. /* If we're cleaning up locks because the process is exiting,
  163. * perform the RPC call asynchronously. */
  164. if ((IS_SETLK(cmd) || IS_SETLKW(cmd))
  165. && fl->fl_type == F_UNLCK
  166. && (current->flags & PF_EXITING)) {
  167. sigfillset(&current->blocked); /* Mask all signals */
  168. recalc_sigpending();
  169. call->a_flags = RPC_TASK_ASYNC;
  170. }
  171. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  172. if (IS_SETLK(cmd) || IS_SETLKW(cmd)) {
  173. if (fl->fl_type != F_UNLCK) {
  174. call->a_args.block = IS_SETLKW(cmd) ? 1 : 0;
  175. status = nlmclnt_lock(call, fl);
  176. } else
  177. status = nlmclnt_unlock(call, fl);
  178. } else if (IS_GETLK(cmd))
  179. status = nlmclnt_test(call, fl);
  180. else
  181. status = -EINVAL;
  182. fl->fl_ops->fl_release_private(fl);
  183. fl->fl_ops = NULL;
  184. spin_lock_irqsave(&current->sighand->siglock, flags);
  185. current->blocked = oldset;
  186. recalc_sigpending();
  187. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  188. dprintk("lockd: clnt proc returns %d\n", status);
  189. return status;
  190. }
  191. EXPORT_SYMBOL(nlmclnt_proc);
  192. /*
  193. * Allocate an NLM RPC call struct
  194. *
  195. * Note: the caller must hold a reference to host. In case of failure,
  196. * this reference will be released.
  197. */
  198. struct nlm_rqst *nlm_alloc_call(struct nlm_host *host)
  199. {
  200. struct nlm_rqst *call;
  201. for(;;) {
  202. call = kzalloc(sizeof(*call), GFP_KERNEL);
  203. if (call != NULL) {
  204. locks_init_lock(&call->a_args.lock.fl);
  205. locks_init_lock(&call->a_res.lock.fl);
  206. call->a_host = host;
  207. return call;
  208. }
  209. if (signalled())
  210. break;
  211. printk("nlm_alloc_call: failed, waiting for memory\n");
  212. schedule_timeout_interruptible(5*HZ);
  213. }
  214. nlm_release_host(host);
  215. return NULL;
  216. }
  217. void nlm_release_call(struct nlm_rqst *call)
  218. {
  219. nlm_release_host(call->a_host);
  220. nlmclnt_release_lockargs(call);
  221. kfree(call);
  222. }
  223. static void nlmclnt_rpc_release(void *data)
  224. {
  225. return nlm_release_call(data);
  226. }
  227. static int nlm_wait_on_grace(wait_queue_head_t *queue)
  228. {
  229. DEFINE_WAIT(wait);
  230. int status = -EINTR;
  231. prepare_to_wait(queue, &wait, TASK_INTERRUPTIBLE);
  232. if (!signalled ()) {
  233. schedule_timeout(NLMCLNT_GRACE_WAIT);
  234. try_to_freeze();
  235. if (!signalled ())
  236. status = 0;
  237. }
  238. finish_wait(queue, &wait);
  239. return status;
  240. }
  241. /*
  242. * Generic NLM call
  243. */
  244. static int
  245. nlmclnt_call(struct nlm_rqst *req, u32 proc)
  246. {
  247. struct nlm_host *host = req->a_host;
  248. struct rpc_clnt *clnt;
  249. struct nlm_args *argp = &req->a_args;
  250. struct nlm_res *resp = &req->a_res;
  251. struct rpc_message msg = {
  252. .rpc_argp = argp,
  253. .rpc_resp = resp,
  254. };
  255. int status;
  256. dprintk("lockd: call procedure %d on %s\n",
  257. (int)proc, host->h_name);
  258. do {
  259. if (host->h_reclaiming && !argp->reclaim)
  260. goto in_grace_period;
  261. /* If we have no RPC client yet, create one. */
  262. if ((clnt = nlm_bind_host(host)) == NULL)
  263. return -ENOLCK;
  264. msg.rpc_proc = &clnt->cl_procinfo[proc];
  265. /* Perform the RPC call. If an error occurs, try again */
  266. if ((status = rpc_call_sync(clnt, &msg, 0)) < 0) {
  267. dprintk("lockd: rpc_call returned error %d\n", -status);
  268. switch (status) {
  269. case -EPROTONOSUPPORT:
  270. status = -EINVAL;
  271. break;
  272. case -ECONNREFUSED:
  273. case -ETIMEDOUT:
  274. case -ENOTCONN:
  275. nlm_rebind_host(host);
  276. status = -EAGAIN;
  277. break;
  278. case -ERESTARTSYS:
  279. return signalled () ? -EINTR : status;
  280. default:
  281. break;
  282. }
  283. break;
  284. } else
  285. if (resp->status == NLM_LCK_DENIED_GRACE_PERIOD) {
  286. dprintk("lockd: server in grace period\n");
  287. if (argp->reclaim) {
  288. printk(KERN_WARNING
  289. "lockd: spurious grace period reject?!\n");
  290. return -ENOLCK;
  291. }
  292. } else {
  293. if (!argp->reclaim) {
  294. /* We appear to be out of the grace period */
  295. wake_up_all(&host->h_gracewait);
  296. }
  297. dprintk("lockd: server returns status %d\n", resp->status);
  298. return 0; /* Okay, call complete */
  299. }
  300. in_grace_period:
  301. /*
  302. * The server has rebooted and appears to be in the grace
  303. * period during which locks are only allowed to be
  304. * reclaimed.
  305. * We can only back off and try again later.
  306. */
  307. status = nlm_wait_on_grace(&host->h_gracewait);
  308. } while (status == 0);
  309. return status;
  310. }
  311. /*
  312. * Generic NLM call, async version.
  313. */
  314. static int __nlm_async_call(struct nlm_rqst *req, u32 proc, struct rpc_message *msg, const struct rpc_call_ops *tk_ops)
  315. {
  316. struct nlm_host *host = req->a_host;
  317. struct rpc_clnt *clnt;
  318. int status = -ENOLCK;
  319. dprintk("lockd: call procedure %d on %s (async)\n",
  320. (int)proc, host->h_name);
  321. /* If we have no RPC client yet, create one. */
  322. clnt = nlm_bind_host(host);
  323. if (clnt == NULL)
  324. goto out_err;
  325. msg->rpc_proc = &clnt->cl_procinfo[proc];
  326. /* bootstrap and kick off the async RPC call */
  327. status = rpc_call_async(clnt, msg, RPC_TASK_ASYNC, tk_ops, req);
  328. if (status == 0)
  329. return 0;
  330. out_err:
  331. nlm_release_call(req);
  332. return status;
  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_LCK_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 (!host->h_monitored && 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_LCK_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_LCK_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, 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_LCK_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. int status = 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. 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 %d (task %d)\n",
  643. req->a_res.status, task->tk_pid);
  644. switch (req->a_res.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. req->a_res.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(u32 status)
  676. {
  677. switch(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. }