clntproc.c 21 KB

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