clntproc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  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",
  273. ntohl(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_end;
  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. nlmclnt_release_call(req);
  393. return status;
  394. }
  395. static void nlmclnt_locks_copy_lock(struct file_lock *new, struct file_lock *fl)
  396. {
  397. spin_lock(&fl->fl_u.nfs_fl.owner->host->h_lock);
  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. spin_unlock(&fl->fl_u.nfs_fl.owner->host->h_lock);
  402. }
  403. static void nlmclnt_locks_release_private(struct file_lock *fl)
  404. {
  405. spin_lock(&fl->fl_u.nfs_fl.owner->host->h_lock);
  406. list_del(&fl->fl_u.nfs_fl.list);
  407. spin_unlock(&fl->fl_u.nfs_fl.owner->host->h_lock);
  408. nlm_put_lockowner(fl->fl_u.nfs_fl.owner);
  409. }
  410. static const struct file_lock_operations nlmclnt_lock_ops = {
  411. .fl_copy_lock = nlmclnt_locks_copy_lock,
  412. .fl_release_private = nlmclnt_locks_release_private,
  413. };
  414. static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host)
  415. {
  416. BUG_ON(fl->fl_ops != NULL);
  417. fl->fl_u.nfs_fl.state = 0;
  418. fl->fl_u.nfs_fl.owner = nlm_find_lockowner(host, fl->fl_owner);
  419. INIT_LIST_HEAD(&fl->fl_u.nfs_fl.list);
  420. fl->fl_ops = &nlmclnt_lock_ops;
  421. }
  422. static int do_vfs_lock(struct file_lock *fl)
  423. {
  424. int res = 0;
  425. switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
  426. case FL_POSIX:
  427. res = posix_lock_file_wait(fl->fl_file, fl);
  428. break;
  429. case FL_FLOCK:
  430. res = flock_lock_file_wait(fl->fl_file, fl);
  431. break;
  432. default:
  433. BUG();
  434. }
  435. return res;
  436. }
  437. /*
  438. * LOCK: Try to create a lock
  439. *
  440. * Programmer Harassment Alert
  441. *
  442. * When given a blocking lock request in a sync RPC call, the HPUX lockd
  443. * will faithfully return LCK_BLOCKED but never cares to notify us when
  444. * the lock could be granted. This way, our local process could hang
  445. * around forever waiting for the callback.
  446. *
  447. * Solution A: Implement busy-waiting
  448. * Solution B: Use the async version of the call (NLM_LOCK_{MSG,RES})
  449. *
  450. * For now I am implementing solution A, because I hate the idea of
  451. * re-implementing lockd for a third time in two months. The async
  452. * calls shouldn't be too hard to do, however.
  453. *
  454. * This is one of the lovely things about standards in the NFS area:
  455. * they're so soft and squishy you can't really blame HP for doing this.
  456. */
  457. static int
  458. nlmclnt_lock(struct nlm_rqst *req, struct file_lock *fl)
  459. {
  460. struct rpc_cred *cred = nfs_file_cred(fl->fl_file);
  461. struct nlm_host *host = req->a_host;
  462. struct nlm_res *resp = &req->a_res;
  463. struct nlm_wait *block = NULL;
  464. unsigned char fl_flags = fl->fl_flags;
  465. unsigned char fl_type;
  466. int status = -ENOLCK;
  467. if (nsm_monitor(host) < 0)
  468. goto out;
  469. req->a_args.state = nsm_local_state;
  470. fl->fl_flags |= FL_ACCESS;
  471. status = do_vfs_lock(fl);
  472. fl->fl_flags = fl_flags;
  473. if (status < 0)
  474. goto out;
  475. block = nlmclnt_prepare_block(host, fl);
  476. again:
  477. /*
  478. * Initialise resp->status to a valid non-zero value,
  479. * since 0 == nlm_lck_granted
  480. */
  481. resp->status = nlm_lck_blocked;
  482. for(;;) {
  483. /* Reboot protection */
  484. fl->fl_u.nfs_fl.state = host->h_state;
  485. status = nlmclnt_call(cred, req, NLMPROC_LOCK);
  486. if (status < 0)
  487. break;
  488. /* Did a reclaimer thread notify us of a server reboot? */
  489. if (resp->status == nlm_lck_denied_grace_period)
  490. continue;
  491. if (resp->status != nlm_lck_blocked)
  492. break;
  493. /* Wait on an NLM blocking lock */
  494. status = nlmclnt_block(block, req, NLMCLNT_POLL_TIMEOUT);
  495. if (status < 0)
  496. break;
  497. if (resp->status != nlm_lck_blocked)
  498. break;
  499. }
  500. /* if we were interrupted while blocking, then cancel the lock request
  501. * and exit
  502. */
  503. if (resp->status == nlm_lck_blocked) {
  504. if (!req->a_args.block)
  505. goto out_unlock;
  506. if (nlmclnt_cancel(host, req->a_args.block, fl) == 0)
  507. goto out_unblock;
  508. }
  509. if (resp->status == nlm_granted) {
  510. down_read(&host->h_rwsem);
  511. /* Check whether or not the server has rebooted */
  512. if (fl->fl_u.nfs_fl.state != host->h_state) {
  513. up_read(&host->h_rwsem);
  514. goto again;
  515. }
  516. /* Ensure the resulting lock will get added to granted list */
  517. fl->fl_flags |= FL_SLEEP;
  518. if (do_vfs_lock(fl) < 0)
  519. printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
  520. up_read(&host->h_rwsem);
  521. fl->fl_flags = fl_flags;
  522. status = 0;
  523. }
  524. if (status < 0)
  525. goto out_unlock;
  526. /*
  527. * EAGAIN doesn't make sense for sleeping locks, and in some
  528. * cases NLM_LCK_DENIED is returned for a permanent error. So
  529. * turn it into an ENOLCK.
  530. */
  531. if (resp->status == nlm_lck_denied && (fl_flags & FL_SLEEP))
  532. status = -ENOLCK;
  533. else
  534. status = nlm_stat_to_errno(resp->status);
  535. out_unblock:
  536. nlmclnt_finish_block(block);
  537. out:
  538. nlmclnt_release_call(req);
  539. return status;
  540. out_unlock:
  541. /* Fatal error: ensure that we remove the lock altogether */
  542. dprintk("lockd: lock attempt ended in fatal error.\n"
  543. " Attempting to unlock.\n");
  544. nlmclnt_finish_block(block);
  545. fl_type = fl->fl_type;
  546. fl->fl_type = F_UNLCK;
  547. down_read(&host->h_rwsem);
  548. do_vfs_lock(fl);
  549. up_read(&host->h_rwsem);
  550. fl->fl_type = fl_type;
  551. fl->fl_flags = fl_flags;
  552. nlmclnt_async_call(cred, req, NLMPROC_UNLOCK, &nlmclnt_unlock_ops);
  553. return status;
  554. }
  555. /*
  556. * RECLAIM: Try to reclaim a lock
  557. */
  558. int
  559. nlmclnt_reclaim(struct nlm_host *host, struct file_lock *fl)
  560. {
  561. struct nlm_rqst reqst, *req;
  562. int status;
  563. req = &reqst;
  564. memset(req, 0, sizeof(*req));
  565. locks_init_lock(&req->a_args.lock.fl);
  566. locks_init_lock(&req->a_res.lock.fl);
  567. req->a_host = host;
  568. req->a_flags = 0;
  569. /* Set up the argument struct */
  570. nlmclnt_setlockargs(req, fl);
  571. req->a_args.reclaim = 1;
  572. status = nlmclnt_call(nfs_file_cred(fl->fl_file), req, NLMPROC_LOCK);
  573. if (status >= 0 && req->a_res.status == nlm_granted)
  574. return 0;
  575. printk(KERN_WARNING "lockd: failed to reclaim lock for pid %d "
  576. "(errno %d, status %d)\n", fl->fl_pid,
  577. status, ntohl(req->a_res.status));
  578. /*
  579. * FIXME: This is a serious failure. We can
  580. *
  581. * a. Ignore the problem
  582. * b. Send the owning process some signal (Linux doesn't have
  583. * SIGLOST, though...)
  584. * c. Retry the operation
  585. *
  586. * Until someone comes up with a simple implementation
  587. * for b or c, I'll choose option a.
  588. */
  589. return -ENOLCK;
  590. }
  591. /*
  592. * UNLOCK: remove an existing lock
  593. */
  594. static int
  595. nlmclnt_unlock(struct nlm_rqst *req, struct file_lock *fl)
  596. {
  597. struct nlm_host *host = req->a_host;
  598. struct nlm_res *resp = &req->a_res;
  599. int status;
  600. unsigned char fl_flags = fl->fl_flags;
  601. /*
  602. * Note: the server is supposed to either grant us the unlock
  603. * request, or to deny it with NLM_LCK_DENIED_GRACE_PERIOD. In either
  604. * case, we want to unlock.
  605. */
  606. fl->fl_flags |= FL_EXISTS;
  607. down_read(&host->h_rwsem);
  608. status = do_vfs_lock(fl);
  609. up_read(&host->h_rwsem);
  610. fl->fl_flags = fl_flags;
  611. if (status == -ENOENT) {
  612. status = 0;
  613. goto out;
  614. }
  615. atomic_inc(&req->a_count);
  616. status = nlmclnt_async_call(nfs_file_cred(fl->fl_file), req,
  617. NLMPROC_UNLOCK, &nlmclnt_unlock_ops);
  618. if (status < 0)
  619. goto out;
  620. if (resp->status == nlm_granted)
  621. goto out;
  622. if (resp->status != nlm_lck_denied_nolocks)
  623. printk("lockd: unexpected unlock status: %d\n",
  624. ntohl(resp->status));
  625. /* What to do now? I'm out of my depth... */
  626. status = -ENOLCK;
  627. out:
  628. nlmclnt_release_call(req);
  629. return status;
  630. }
  631. static void nlmclnt_unlock_callback(struct rpc_task *task, void *data)
  632. {
  633. struct nlm_rqst *req = data;
  634. u32 status = ntohl(req->a_res.status);
  635. if (RPC_ASSASSINATED(task))
  636. goto die;
  637. if (task->tk_status < 0) {
  638. dprintk("lockd: unlock failed (err = %d)\n", -task->tk_status);
  639. switch (task->tk_status) {
  640. case -EACCES:
  641. case -EIO:
  642. goto die;
  643. default:
  644. goto retry_rebind;
  645. }
  646. }
  647. if (status == NLM_LCK_DENIED_GRACE_PERIOD) {
  648. rpc_delay(task, NLMCLNT_GRACE_WAIT);
  649. goto retry_unlock;
  650. }
  651. if (status != NLM_LCK_GRANTED)
  652. printk(KERN_WARNING "lockd: unexpected unlock status: %d\n", status);
  653. die:
  654. return;
  655. retry_rebind:
  656. nlm_rebind_host(req->a_host);
  657. retry_unlock:
  658. rpc_restart_call(task);
  659. }
  660. static const struct rpc_call_ops nlmclnt_unlock_ops = {
  661. .rpc_call_done = nlmclnt_unlock_callback,
  662. .rpc_release = nlmclnt_rpc_release,
  663. };
  664. /*
  665. * Cancel a blocked lock request.
  666. * We always use an async RPC call for this in order not to hang a
  667. * process that has been Ctrl-C'ed.
  668. */
  669. static int nlmclnt_cancel(struct nlm_host *host, int block, struct file_lock *fl)
  670. {
  671. struct nlm_rqst *req;
  672. int status;
  673. dprintk("lockd: blocking lock attempt was interrupted by a signal.\n"
  674. " Attempting to cancel lock.\n");
  675. req = nlm_alloc_call(nlm_get_host(host));
  676. if (!req)
  677. return -ENOMEM;
  678. req->a_flags = RPC_TASK_ASYNC;
  679. nlmclnt_setlockargs(req, fl);
  680. req->a_args.block = block;
  681. atomic_inc(&req->a_count);
  682. status = nlmclnt_async_call(nfs_file_cred(fl->fl_file), req,
  683. NLMPROC_CANCEL, &nlmclnt_cancel_ops);
  684. if (status == 0 && req->a_res.status == nlm_lck_denied)
  685. status = -ENOLCK;
  686. nlmclnt_release_call(req);
  687. return status;
  688. }
  689. static void nlmclnt_cancel_callback(struct rpc_task *task, void *data)
  690. {
  691. struct nlm_rqst *req = data;
  692. u32 status = ntohl(req->a_res.status);
  693. if (RPC_ASSASSINATED(task))
  694. goto die;
  695. if (task->tk_status < 0) {
  696. dprintk("lockd: CANCEL call error %d, retrying.\n",
  697. task->tk_status);
  698. goto retry_cancel;
  699. }
  700. dprintk("lockd: cancel status %u (task %u)\n",
  701. status, task->tk_pid);
  702. switch (status) {
  703. case NLM_LCK_GRANTED:
  704. case NLM_LCK_DENIED_GRACE_PERIOD:
  705. case NLM_LCK_DENIED:
  706. /* Everything's good */
  707. break;
  708. case NLM_LCK_DENIED_NOLOCKS:
  709. dprintk("lockd: CANCEL failed (server has no locks)\n");
  710. goto retry_cancel;
  711. default:
  712. printk(KERN_NOTICE "lockd: weird return %d for CANCEL call\n",
  713. status);
  714. }
  715. die:
  716. return;
  717. retry_cancel:
  718. /* Don't ever retry more than 3 times */
  719. if (req->a_retries++ >= NLMCLNT_MAX_RETRIES)
  720. goto die;
  721. nlm_rebind_host(req->a_host);
  722. rpc_restart_call(task);
  723. rpc_delay(task, 30 * HZ);
  724. }
  725. static const struct rpc_call_ops nlmclnt_cancel_ops = {
  726. .rpc_call_done = nlmclnt_cancel_callback,
  727. .rpc_release = nlmclnt_rpc_release,
  728. };
  729. /*
  730. * Convert an NLM status code to a generic kernel errno
  731. */
  732. static int
  733. nlm_stat_to_errno(__be32 status)
  734. {
  735. switch(ntohl(status)) {
  736. case NLM_LCK_GRANTED:
  737. return 0;
  738. case NLM_LCK_DENIED:
  739. return -EAGAIN;
  740. case NLM_LCK_DENIED_NOLOCKS:
  741. case NLM_LCK_DENIED_GRACE_PERIOD:
  742. return -ENOLCK;
  743. case NLM_LCK_BLOCKED:
  744. printk(KERN_NOTICE "lockd: unexpected status NLM_BLOCKED\n");
  745. return -ENOLCK;
  746. #ifdef CONFIG_LOCKD_V4
  747. case NLM_DEADLCK:
  748. return -EDEADLK;
  749. case NLM_ROFS:
  750. return -EROFS;
  751. case NLM_STALE_FH:
  752. return -ESTALE;
  753. case NLM_FBIG:
  754. return -EOVERFLOW;
  755. case NLM_FAILED:
  756. return -ENOLCK;
  757. #endif
  758. }
  759. printk(KERN_NOTICE "lockd: unexpected server status %d\n",
  760. ntohl(status));
  761. return -ENOLCK;
  762. }