clntproc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  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. #define NLMDBG_FACILITY NLMDBG_CLIENT
  19. #define NLMCLNT_GRACE_WAIT (5*HZ)
  20. #define NLMCLNT_POLL_TIMEOUT (30*HZ)
  21. #define NLMCLNT_MAX_RETRIES 3
  22. static int nlmclnt_test(struct nlm_rqst *, struct file_lock *);
  23. static int nlmclnt_lock(struct nlm_rqst *, struct file_lock *);
  24. static int nlmclnt_unlock(struct nlm_rqst *, struct file_lock *);
  25. static int nlm_stat_to_errno(__be32 stat);
  26. static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host);
  27. static int nlmclnt_cancel(struct nlm_host *, int , struct file_lock *);
  28. static const struct rpc_call_ops nlmclnt_unlock_ops;
  29. static const struct rpc_call_ops nlmclnt_cancel_ops;
  30. /*
  31. * Cookie counter for NLM requests
  32. */
  33. static atomic_t nlm_cookie = ATOMIC_INIT(0x1234);
  34. void nlmclnt_next_cookie(struct nlm_cookie *c)
  35. {
  36. u32 cookie = atomic_inc_return(&nlm_cookie);
  37. memcpy(c->data, &cookie, 4);
  38. c->len=4;
  39. }
  40. static struct nlm_lockowner *nlm_get_lockowner(struct nlm_lockowner *lockowner)
  41. {
  42. atomic_inc(&lockowner->count);
  43. return lockowner;
  44. }
  45. static void nlm_put_lockowner(struct nlm_lockowner *lockowner)
  46. {
  47. if (!atomic_dec_and_lock(&lockowner->count, &lockowner->host->h_lock))
  48. return;
  49. list_del(&lockowner->list);
  50. spin_unlock(&lockowner->host->h_lock);
  51. nlm_release_host(lockowner->host);
  52. kfree(lockowner);
  53. }
  54. static inline int nlm_pidbusy(struct nlm_host *host, uint32_t pid)
  55. {
  56. struct nlm_lockowner *lockowner;
  57. list_for_each_entry(lockowner, &host->h_lockowners, list) {
  58. if (lockowner->pid == pid)
  59. return -EBUSY;
  60. }
  61. return 0;
  62. }
  63. static inline uint32_t __nlm_alloc_pid(struct nlm_host *host)
  64. {
  65. uint32_t res;
  66. do {
  67. res = host->h_pidcount++;
  68. } while (nlm_pidbusy(host, res) < 0);
  69. return res;
  70. }
  71. static struct nlm_lockowner *__nlm_find_lockowner(struct nlm_host *host, fl_owner_t owner)
  72. {
  73. struct nlm_lockowner *lockowner;
  74. list_for_each_entry(lockowner, &host->h_lockowners, list) {
  75. if (lockowner->owner != owner)
  76. continue;
  77. return nlm_get_lockowner(lockowner);
  78. }
  79. return NULL;
  80. }
  81. static struct nlm_lockowner *nlm_find_lockowner(struct nlm_host *host, fl_owner_t owner)
  82. {
  83. struct nlm_lockowner *res, *new = NULL;
  84. spin_lock(&host->h_lock);
  85. res = __nlm_find_lockowner(host, owner);
  86. if (res == NULL) {
  87. spin_unlock(&host->h_lock);
  88. new = kmalloc(sizeof(*new), GFP_KERNEL);
  89. spin_lock(&host->h_lock);
  90. res = __nlm_find_lockowner(host, owner);
  91. if (res == NULL && new != NULL) {
  92. res = new;
  93. atomic_set(&new->count, 1);
  94. new->owner = owner;
  95. new->pid = __nlm_alloc_pid(host);
  96. new->host = nlm_get_host(host);
  97. list_add(&new->list, &host->h_lockowners);
  98. new = NULL;
  99. }
  100. }
  101. spin_unlock(&host->h_lock);
  102. kfree(new);
  103. return res;
  104. }
  105. /*
  106. * Initialize arguments for TEST/LOCK/UNLOCK/CANCEL calls
  107. */
  108. static void nlmclnt_setlockargs(struct nlm_rqst *req, struct file_lock *fl)
  109. {
  110. struct nlm_args *argp = &req->a_args;
  111. struct nlm_lock *lock = &argp->lock;
  112. nlmclnt_next_cookie(&argp->cookie);
  113. memcpy(&lock->fh, NFS_FH(fl->fl_file->f_path.dentry->d_inode), sizeof(struct nfs_fh));
  114. lock->caller = utsname()->nodename;
  115. lock->oh.data = req->a_owner;
  116. lock->oh.len = snprintf(req->a_owner, sizeof(req->a_owner), "%u@%s",
  117. (unsigned int)fl->fl_u.nfs_fl.owner->pid,
  118. utsname()->nodename);
  119. lock->svid = fl->fl_u.nfs_fl.owner->pid;
  120. lock->fl.fl_start = fl->fl_start;
  121. lock->fl.fl_end = fl->fl_end;
  122. lock->fl.fl_type = fl->fl_type;
  123. }
  124. static void nlmclnt_release_lockargs(struct nlm_rqst *req)
  125. {
  126. BUG_ON(req->a_args.lock.fl.fl_ops != NULL);
  127. }
  128. /**
  129. * nlmclnt_proc - Perform a single client-side lock request
  130. * @host: address of a valid nlm_host context representing the NLM server
  131. * @cmd: fcntl-style file lock operation to perform
  132. * @fl: address of arguments for the lock operation
  133. *
  134. */
  135. int nlmclnt_proc(struct nlm_host *host, int cmd, struct file_lock *fl)
  136. {
  137. struct nlm_rqst *call;
  138. int status;
  139. nlm_get_host(host);
  140. call = nlm_alloc_call(host);
  141. if (call == NULL)
  142. return -ENOMEM;
  143. nlmclnt_locks_init_private(fl, host);
  144. /* Set up the argument struct */
  145. nlmclnt_setlockargs(call, fl);
  146. lock_kernel();
  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. unlock_kernel();
  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. lock_kernel();
  201. nlm_release_call(data);
  202. unlock_kernel();
  203. }
  204. static int nlm_wait_on_grace(wait_queue_head_t *queue)
  205. {
  206. DEFINE_WAIT(wait);
  207. int status = -EINTR;
  208. prepare_to_wait(queue, &wait, TASK_INTERRUPTIBLE);
  209. if (!signalled ()) {
  210. schedule_timeout(NLMCLNT_GRACE_WAIT);
  211. try_to_freeze();
  212. if (!signalled ())
  213. status = 0;
  214. }
  215. finish_wait(queue, &wait);
  216. return status;
  217. }
  218. /*
  219. * Generic NLM call
  220. */
  221. static int
  222. nlmclnt_call(struct rpc_cred *cred, struct nlm_rqst *req, u32 proc)
  223. {
  224. struct nlm_host *host = req->a_host;
  225. struct rpc_clnt *clnt;
  226. struct nlm_args *argp = &req->a_args;
  227. struct nlm_res *resp = &req->a_res;
  228. struct rpc_message msg = {
  229. .rpc_argp = argp,
  230. .rpc_resp = resp,
  231. .rpc_cred = cred,
  232. };
  233. int status;
  234. dprintk("lockd: call procedure %d on %s\n",
  235. (int)proc, host->h_name);
  236. do {
  237. if (host->h_reclaiming && !argp->reclaim)
  238. goto in_grace_period;
  239. /* If we have no RPC client yet, create one. */
  240. if ((clnt = nlm_bind_host(host)) == NULL)
  241. return -ENOLCK;
  242. msg.rpc_proc = &clnt->cl_procinfo[proc];
  243. /* Perform the RPC call. If an error occurs, try again */
  244. if ((status = rpc_call_sync(clnt, &msg, 0)) < 0) {
  245. dprintk("lockd: rpc_call returned error %d\n", -status);
  246. switch (status) {
  247. case -EPROTONOSUPPORT:
  248. status = -EINVAL;
  249. break;
  250. case -ECONNREFUSED:
  251. case -ETIMEDOUT:
  252. case -ENOTCONN:
  253. nlm_rebind_host(host);
  254. status = -EAGAIN;
  255. break;
  256. case -ERESTARTSYS:
  257. return signalled () ? -EINTR : status;
  258. default:
  259. break;
  260. }
  261. break;
  262. } else
  263. if (resp->status == nlm_lck_denied_grace_period) {
  264. dprintk("lockd: server in grace period\n");
  265. if (argp->reclaim) {
  266. printk(KERN_WARNING
  267. "lockd: spurious grace period reject?!\n");
  268. return -ENOLCK;
  269. }
  270. } else {
  271. if (!argp->reclaim) {
  272. /* We appear to be out of the grace period */
  273. wake_up_all(&host->h_gracewait);
  274. }
  275. dprintk("lockd: server returns status %d\n", resp->status);
  276. return 0; /* Okay, call complete */
  277. }
  278. in_grace_period:
  279. /*
  280. * The server has rebooted and appears to be in the grace
  281. * period during which locks are only allowed to be
  282. * reclaimed.
  283. * We can only back off and try again later.
  284. */
  285. status = nlm_wait_on_grace(&host->h_gracewait);
  286. } while (status == 0);
  287. return status;
  288. }
  289. /*
  290. * Generic NLM call, async version.
  291. */
  292. static struct rpc_task *__nlm_async_call(struct nlm_rqst *req, u32 proc, struct rpc_message *msg, const struct rpc_call_ops *tk_ops)
  293. {
  294. struct nlm_host *host = req->a_host;
  295. struct rpc_clnt *clnt;
  296. struct rpc_task_setup task_setup_data = {
  297. .rpc_message = msg,
  298. .callback_ops = tk_ops,
  299. .callback_data = req,
  300. .flags = RPC_TASK_ASYNC,
  301. };
  302. dprintk("lockd: call procedure %d on %s (async)\n",
  303. (int)proc, host->h_name);
  304. /* If we have no RPC client yet, create one. */
  305. clnt = nlm_bind_host(host);
  306. if (clnt == NULL)
  307. goto out_err;
  308. msg->rpc_proc = &clnt->cl_procinfo[proc];
  309. task_setup_data.rpc_client = clnt;
  310. /* bootstrap and kick off the async RPC call */
  311. return rpc_run_task(&task_setup_data);
  312. out_err:
  313. tk_ops->rpc_release(req);
  314. return ERR_PTR(-ENOLCK);
  315. }
  316. static int nlm_do_async_call(struct nlm_rqst *req, u32 proc, struct rpc_message *msg, const struct rpc_call_ops *tk_ops)
  317. {
  318. struct rpc_task *task;
  319. task = __nlm_async_call(req, proc, msg, tk_ops);
  320. if (IS_ERR(task))
  321. return PTR_ERR(task);
  322. rpc_put_task(task);
  323. return 0;
  324. }
  325. /*
  326. * NLM asynchronous call.
  327. */
  328. int nlm_async_call(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
  329. {
  330. struct rpc_message msg = {
  331. .rpc_argp = &req->a_args,
  332. .rpc_resp = &req->a_res,
  333. };
  334. return nlm_do_async_call(req, proc, &msg, tk_ops);
  335. }
  336. int nlm_async_reply(struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
  337. {
  338. struct rpc_message msg = {
  339. .rpc_argp = &req->a_res,
  340. };
  341. return nlm_do_async_call(req, proc, &msg, tk_ops);
  342. }
  343. /*
  344. * NLM client asynchronous call.
  345. *
  346. * Note that although the calls are asynchronous, and are therefore
  347. * guaranteed to complete, we still always attempt to wait for
  348. * completion in order to be able to correctly track the lock
  349. * state.
  350. */
  351. static int nlmclnt_async_call(struct rpc_cred *cred, struct nlm_rqst *req, u32 proc, const struct rpc_call_ops *tk_ops)
  352. {
  353. struct rpc_message msg = {
  354. .rpc_argp = &req->a_args,
  355. .rpc_resp = &req->a_res,
  356. .rpc_cred = cred,
  357. };
  358. struct rpc_task *task;
  359. int err;
  360. task = __nlm_async_call(req, proc, &msg, tk_ops);
  361. if (IS_ERR(task))
  362. return PTR_ERR(task);
  363. err = rpc_wait_for_completion_task(task);
  364. rpc_put_task(task);
  365. return err;
  366. }
  367. /*
  368. * TEST for the presence of a conflicting lock
  369. */
  370. static int
  371. nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl)
  372. {
  373. int status;
  374. status = nlmclnt_call(nfs_file_cred(fl->fl_file), req, NLMPROC_TEST);
  375. if (status < 0)
  376. goto out;
  377. switch (req->a_res.status) {
  378. case nlm_granted:
  379. fl->fl_type = F_UNLCK;
  380. break;
  381. case nlm_lck_denied:
  382. /*
  383. * Report the conflicting lock back to the application.
  384. */
  385. fl->fl_start = req->a_res.lock.fl.fl_start;
  386. fl->fl_end = req->a_res.lock.fl.fl_end;
  387. fl->fl_type = req->a_res.lock.fl.fl_type;
  388. fl->fl_pid = 0;
  389. break;
  390. default:
  391. status = nlm_stat_to_errno(req->a_res.status);
  392. }
  393. out:
  394. nlm_release_call(req);
  395. return status;
  396. }
  397. static void nlmclnt_locks_copy_lock(struct file_lock *new, struct file_lock *fl)
  398. {
  399. new->fl_u.nfs_fl.state = fl->fl_u.nfs_fl.state;
  400. new->fl_u.nfs_fl.owner = nlm_get_lockowner(fl->fl_u.nfs_fl.owner);
  401. list_add_tail(&new->fl_u.nfs_fl.list, &fl->fl_u.nfs_fl.owner->host->h_granted);
  402. }
  403. static void nlmclnt_locks_release_private(struct file_lock *fl)
  404. {
  405. list_del(&fl->fl_u.nfs_fl.list);
  406. nlm_put_lockowner(fl->fl_u.nfs_fl.owner);
  407. }
  408. static struct file_lock_operations nlmclnt_lock_ops = {
  409. .fl_copy_lock = nlmclnt_locks_copy_lock,
  410. .fl_release_private = nlmclnt_locks_release_private,
  411. };
  412. static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host)
  413. {
  414. BUG_ON(fl->fl_ops != NULL);
  415. fl->fl_u.nfs_fl.state = 0;
  416. fl->fl_u.nfs_fl.owner = nlm_find_lockowner(host, fl->fl_owner);
  417. INIT_LIST_HEAD(&fl->fl_u.nfs_fl.list);
  418. fl->fl_ops = &nlmclnt_lock_ops;
  419. }
  420. static int do_vfs_lock(struct file_lock *fl)
  421. {
  422. int res = 0;
  423. switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
  424. case FL_POSIX:
  425. res = posix_lock_file_wait(fl->fl_file, fl);
  426. break;
  427. case FL_FLOCK:
  428. res = flock_lock_file_wait(fl->fl_file, fl);
  429. break;
  430. default:
  431. BUG();
  432. }
  433. return res;
  434. }
  435. /*
  436. * LOCK: Try to create a lock
  437. *
  438. * Programmer Harassment Alert
  439. *
  440. * When given a blocking lock request in a sync RPC call, the HPUX lockd
  441. * will faithfully return LCK_BLOCKED but never cares to notify us when
  442. * the lock could be granted. This way, our local process could hang
  443. * around forever waiting for the callback.
  444. *
  445. * Solution A: Implement busy-waiting
  446. * Solution B: Use the async version of the call (NLM_LOCK_{MSG,RES})
  447. *
  448. * For now I am implementing solution A, because I hate the idea of
  449. * re-implementing lockd for a third time in two months. The async
  450. * calls shouldn't be too hard to do, however.
  451. *
  452. * This is one of the lovely things about standards in the NFS area:
  453. * they're so soft and squishy you can't really blame HP for doing this.
  454. */
  455. static int
  456. nlmclnt_lock(struct nlm_rqst *req, struct file_lock *fl)
  457. {
  458. struct rpc_cred *cred = nfs_file_cred(fl->fl_file);
  459. struct nlm_host *host = req->a_host;
  460. struct nlm_res *resp = &req->a_res;
  461. struct nlm_wait *block = NULL;
  462. unsigned char fl_flags = fl->fl_flags;
  463. unsigned char fl_type;
  464. int status = -ENOLCK;
  465. if (nsm_monitor(host) < 0)
  466. goto out;
  467. req->a_args.state = nsm_local_state;
  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", __func__);
  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. /*
  525. * EAGAIN doesn't make sense for sleeping locks, and in some
  526. * cases NLM_LCK_DENIED is returned for a permanent error. So
  527. * turn it into an ENOLCK.
  528. */
  529. if (resp->status == nlm_lck_denied && (fl_flags & FL_SLEEP))
  530. status = -ENOLCK;
  531. else
  532. status = nlm_stat_to_errno(resp->status);
  533. out_unblock:
  534. nlmclnt_finish_block(block);
  535. out:
  536. nlm_release_call(req);
  537. return status;
  538. out_unlock:
  539. /* Fatal error: ensure that we remove the lock altogether */
  540. dprintk("lockd: lock attempt ended in fatal error.\n"
  541. " Attempting to unlock.\n");
  542. nlmclnt_finish_block(block);
  543. fl_type = fl->fl_type;
  544. fl->fl_type = F_UNLCK;
  545. down_read(&host->h_rwsem);
  546. do_vfs_lock(fl);
  547. up_read(&host->h_rwsem);
  548. fl->fl_type = fl_type;
  549. fl->fl_flags = fl_flags;
  550. nlmclnt_async_call(cred, req, NLMPROC_UNLOCK, &nlmclnt_unlock_ops);
  551. return status;
  552. }
  553. /*
  554. * RECLAIM: Try to reclaim a lock
  555. */
  556. int
  557. nlmclnt_reclaim(struct nlm_host *host, struct file_lock *fl)
  558. {
  559. struct nlm_rqst reqst, *req;
  560. int status;
  561. req = &reqst;
  562. memset(req, 0, sizeof(*req));
  563. locks_init_lock(&req->a_args.lock.fl);
  564. locks_init_lock(&req->a_res.lock.fl);
  565. req->a_host = host;
  566. req->a_flags = 0;
  567. /* Set up the argument struct */
  568. nlmclnt_setlockargs(req, fl);
  569. req->a_args.reclaim = 1;
  570. status = nlmclnt_call(nfs_file_cred(fl->fl_file), req, NLMPROC_LOCK);
  571. if (status >= 0 && req->a_res.status == nlm_granted)
  572. return 0;
  573. printk(KERN_WARNING "lockd: failed to reclaim lock for pid %d "
  574. "(errno %d, status %d)\n", fl->fl_pid,
  575. status, ntohl(req->a_res.status));
  576. /*
  577. * FIXME: This is a serious failure. We can
  578. *
  579. * a. Ignore the problem
  580. * b. Send the owning process some signal (Linux doesn't have
  581. * SIGLOST, though...)
  582. * c. Retry the operation
  583. *
  584. * Until someone comes up with a simple implementation
  585. * for b or c, I'll choose option a.
  586. */
  587. return -ENOLCK;
  588. }
  589. /*
  590. * UNLOCK: remove an existing lock
  591. */
  592. static int
  593. nlmclnt_unlock(struct nlm_rqst *req, struct file_lock *fl)
  594. {
  595. struct nlm_host *host = req->a_host;
  596. struct nlm_res *resp = &req->a_res;
  597. int status;
  598. unsigned char fl_flags = fl->fl_flags;
  599. /*
  600. * Note: the server is supposed to either grant us the unlock
  601. * request, or to deny it with NLM_LCK_DENIED_GRACE_PERIOD. In either
  602. * case, we want to unlock.
  603. */
  604. fl->fl_flags |= FL_EXISTS;
  605. down_read(&host->h_rwsem);
  606. status = do_vfs_lock(fl);
  607. up_read(&host->h_rwsem);
  608. fl->fl_flags = fl_flags;
  609. if (status == -ENOENT) {
  610. status = 0;
  611. goto out;
  612. }
  613. atomic_inc(&req->a_count);
  614. status = nlmclnt_async_call(nfs_file_cred(fl->fl_file), req,
  615. NLMPROC_UNLOCK, &nlmclnt_unlock_ops);
  616. if (status < 0)
  617. goto out;
  618. if (resp->status == nlm_granted)
  619. goto out;
  620. if (resp->status != nlm_lck_denied_nolocks)
  621. printk("lockd: unexpected unlock status: %d\n", resp->status);
  622. /* What to do now? I'm out of my depth... */
  623. status = -ENOLCK;
  624. out:
  625. nlm_release_call(req);
  626. return status;
  627. }
  628. static void nlmclnt_unlock_callback(struct rpc_task *task, void *data)
  629. {
  630. struct nlm_rqst *req = data;
  631. u32 status = ntohl(req->a_res.status);
  632. if (RPC_ASSASSINATED(task))
  633. goto die;
  634. if (task->tk_status < 0) {
  635. dprintk("lockd: unlock failed (err = %d)\n", -task->tk_status);
  636. goto retry_rebind;
  637. }
  638. if (status == NLM_LCK_DENIED_GRACE_PERIOD) {
  639. rpc_delay(task, NLMCLNT_GRACE_WAIT);
  640. goto retry_unlock;
  641. }
  642. if (status != NLM_LCK_GRANTED)
  643. printk(KERN_WARNING "lockd: unexpected unlock status: %d\n", status);
  644. die:
  645. return;
  646. retry_rebind:
  647. lock_kernel();
  648. nlm_rebind_host(req->a_host);
  649. unlock_kernel();
  650. retry_unlock:
  651. rpc_restart_call(task);
  652. }
  653. static const struct rpc_call_ops nlmclnt_unlock_ops = {
  654. .rpc_call_done = nlmclnt_unlock_callback,
  655. .rpc_release = nlmclnt_rpc_release,
  656. };
  657. /*
  658. * Cancel a blocked lock request.
  659. * We always use an async RPC call for this in order not to hang a
  660. * process that has been Ctrl-C'ed.
  661. */
  662. static int nlmclnt_cancel(struct nlm_host *host, int block, struct file_lock *fl)
  663. {
  664. struct nlm_rqst *req;
  665. int status;
  666. dprintk("lockd: blocking lock attempt was interrupted by a signal.\n"
  667. " Attempting to cancel lock.\n");
  668. req = nlm_alloc_call(nlm_get_host(host));
  669. if (!req)
  670. return -ENOMEM;
  671. req->a_flags = RPC_TASK_ASYNC;
  672. nlmclnt_setlockargs(req, fl);
  673. req->a_args.block = block;
  674. atomic_inc(&req->a_count);
  675. status = nlmclnt_async_call(nfs_file_cred(fl->fl_file), req,
  676. NLMPROC_CANCEL, &nlmclnt_cancel_ops);
  677. if (status == 0 && req->a_res.status == nlm_lck_denied)
  678. status = -ENOLCK;
  679. nlm_release_call(req);
  680. return status;
  681. }
  682. static void nlmclnt_cancel_callback(struct rpc_task *task, void *data)
  683. {
  684. struct nlm_rqst *req = data;
  685. u32 status = ntohl(req->a_res.status);
  686. if (RPC_ASSASSINATED(task))
  687. goto die;
  688. if (task->tk_status < 0) {
  689. dprintk("lockd: CANCEL call error %d, retrying.\n",
  690. task->tk_status);
  691. goto retry_cancel;
  692. }
  693. dprintk("lockd: cancel status %u (task %u)\n",
  694. status, task->tk_pid);
  695. switch (status) {
  696. case NLM_LCK_GRANTED:
  697. case NLM_LCK_DENIED_GRACE_PERIOD:
  698. case NLM_LCK_DENIED:
  699. /* Everything's good */
  700. break;
  701. case NLM_LCK_DENIED_NOLOCKS:
  702. dprintk("lockd: CANCEL failed (server has no locks)\n");
  703. goto retry_cancel;
  704. default:
  705. printk(KERN_NOTICE "lockd: weird return %d for CANCEL call\n",
  706. status);
  707. }
  708. die:
  709. return;
  710. retry_cancel:
  711. /* Don't ever retry more than 3 times */
  712. if (req->a_retries++ >= NLMCLNT_MAX_RETRIES)
  713. goto die;
  714. lock_kernel();
  715. nlm_rebind_host(req->a_host);
  716. unlock_kernel();
  717. rpc_restart_call(task);
  718. rpc_delay(task, 30 * HZ);
  719. }
  720. static const struct rpc_call_ops nlmclnt_cancel_ops = {
  721. .rpc_call_done = nlmclnt_cancel_callback,
  722. .rpc_release = nlmclnt_rpc_release,
  723. };
  724. /*
  725. * Convert an NLM status code to a generic kernel errno
  726. */
  727. static int
  728. nlm_stat_to_errno(__be32 status)
  729. {
  730. switch(ntohl(status)) {
  731. case NLM_LCK_GRANTED:
  732. return 0;
  733. case NLM_LCK_DENIED:
  734. return -EAGAIN;
  735. case NLM_LCK_DENIED_NOLOCKS:
  736. case NLM_LCK_DENIED_GRACE_PERIOD:
  737. return -ENOLCK;
  738. case NLM_LCK_BLOCKED:
  739. printk(KERN_NOTICE "lockd: unexpected status NLM_BLOCKED\n");
  740. return -ENOLCK;
  741. #ifdef CONFIG_LOCKD_V4
  742. case NLM_DEADLCK:
  743. return -EDEADLK;
  744. case NLM_ROFS:
  745. return -EROFS;
  746. case NLM_STALE_FH:
  747. return -ESTALE;
  748. case NLM_FBIG:
  749. return -EOVERFLOW;
  750. case NLM_FAILED:
  751. return -ENOLCK;
  752. #endif
  753. }
  754. printk(KERN_NOTICE "lockd: unexpected server status %d\n", status);
  755. return -ENOLCK;
  756. }