clntproc.c 20 KB

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