clntproc.c 21 KB

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