clntproc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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/config.h>
  9. #include <linux/module.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/smp_lock.h>
  16. #include <linux/sunrpc/clnt.h>
  17. #include <linux/sunrpc/svc.h>
  18. #include <linux/lockd/lockd.h>
  19. #include <linux/lockd/sm_inter.h>
  20. #define NLMDBG_FACILITY NLMDBG_CLIENT
  21. #define NLMCLNT_GRACE_WAIT (5*HZ)
  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 void nlmclnt_unlock_callback(struct rpc_task *);
  26. static void nlmclnt_cancel_callback(struct rpc_task *);
  27. static int nlm_stat_to_errno(u32 stat);
  28. static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host);
  29. /*
  30. * Cookie counter for NLM requests
  31. */
  32. static u32 nlm_cookie = 0x1234;
  33. static inline void nlmclnt_next_cookie(struct nlm_cookie *c)
  34. {
  35. memcpy(c->data, &nlm_cookie, 4);
  36. memset(c->data+4, 0, 4);
  37. c->len=4;
  38. nlm_cookie++;
  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 = (struct nlm_lockowner *)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. if (new != NULL)
  103. kfree(new);
  104. return res;
  105. }
  106. /*
  107. * Initialize arguments for TEST/LOCK/UNLOCK/CANCEL calls
  108. */
  109. static void nlmclnt_setlockargs(struct nlm_rqst *req, struct file_lock *fl)
  110. {
  111. struct nlm_args *argp = &req->a_args;
  112. struct nlm_lock *lock = &argp->lock;
  113. nlmclnt_next_cookie(&argp->cookie);
  114. argp->state = nsm_local_state;
  115. memcpy(&lock->fh, NFS_FH(fl->fl_file->f_dentry->d_inode), sizeof(struct nfs_fh));
  116. lock->caller = system_utsname.nodename;
  117. lock->oh.data = req->a_owner;
  118. lock->oh.len = sprintf(req->a_owner, "%d@%s",
  119. current->pid, system_utsname.nodename);
  120. locks_copy_lock(&lock->fl, fl);
  121. }
  122. static void nlmclnt_release_lockargs(struct nlm_rqst *req)
  123. {
  124. struct file_lock *fl = &req->a_args.lock.fl;
  125. if (fl->fl_ops && fl->fl_ops->fl_release_private)
  126. fl->fl_ops->fl_release_private(fl);
  127. }
  128. /*
  129. * Initialize arguments for GRANTED call. The nlm_rqst structure
  130. * has been cleared already.
  131. */
  132. int
  133. nlmclnt_setgrantargs(struct nlm_rqst *call, struct nlm_lock *lock)
  134. {
  135. locks_copy_lock(&call->a_args.lock.fl, &lock->fl);
  136. memcpy(&call->a_args.lock.fh, &lock->fh, sizeof(call->a_args.lock.fh));
  137. call->a_args.lock.caller = system_utsname.nodename;
  138. call->a_args.lock.oh.len = lock->oh.len;
  139. /* set default data area */
  140. call->a_args.lock.oh.data = call->a_owner;
  141. if (lock->oh.len > NLMCLNT_OHSIZE) {
  142. void *data = kmalloc(lock->oh.len, GFP_KERNEL);
  143. if (!data) {
  144. nlmclnt_freegrantargs(call);
  145. return 0;
  146. }
  147. call->a_args.lock.oh.data = (u8 *) data;
  148. }
  149. memcpy(call->a_args.lock.oh.data, lock->oh.data, lock->oh.len);
  150. return 1;
  151. }
  152. void
  153. nlmclnt_freegrantargs(struct nlm_rqst *call)
  154. {
  155. struct file_lock *fl = &call->a_args.lock.fl;
  156. /*
  157. * Check whether we allocated memory for the owner.
  158. */
  159. if (call->a_args.lock.oh.data != (u8 *) call->a_owner) {
  160. kfree(call->a_args.lock.oh.data);
  161. }
  162. if (fl->fl_ops && fl->fl_ops->fl_release_private)
  163. fl->fl_ops->fl_release_private(fl);
  164. }
  165. /*
  166. * This is the main entry point for the NLM client.
  167. */
  168. int
  169. nlmclnt_proc(struct inode *inode, int cmd, struct file_lock *fl)
  170. {
  171. struct nfs_server *nfssrv = NFS_SERVER(inode);
  172. struct nlm_host *host;
  173. struct nlm_rqst reqst, *call = &reqst;
  174. sigset_t oldset;
  175. unsigned long flags;
  176. int status, proto, vers;
  177. vers = (NFS_PROTO(inode)->version == 3) ? 4 : 1;
  178. if (NFS_PROTO(inode)->version > 3) {
  179. printk(KERN_NOTICE "NFSv4 file locking not implemented!\n");
  180. return -ENOLCK;
  181. }
  182. /* Retrieve transport protocol from NFS client */
  183. proto = NFS_CLIENT(inode)->cl_xprt->prot;
  184. if (!(host = nlmclnt_lookup_host(NFS_ADDR(inode), proto, vers)))
  185. return -ENOLCK;
  186. /* Create RPC client handle if not there, and copy soft
  187. * and intr flags from NFS client. */
  188. if (host->h_rpcclnt == NULL) {
  189. struct rpc_clnt *clnt;
  190. /* Bind an rpc client to this host handle (does not
  191. * perform a portmapper lookup) */
  192. if (!(clnt = nlm_bind_host(host))) {
  193. status = -ENOLCK;
  194. goto done;
  195. }
  196. clnt->cl_softrtry = nfssrv->client->cl_softrtry;
  197. clnt->cl_intr = nfssrv->client->cl_intr;
  198. clnt->cl_chatty = nfssrv->client->cl_chatty;
  199. }
  200. /* Keep the old signal mask */
  201. spin_lock_irqsave(&current->sighand->siglock, flags);
  202. oldset = current->blocked;
  203. /* If we're cleaning up locks because the process is exiting,
  204. * perform the RPC call asynchronously. */
  205. if ((IS_SETLK(cmd) || IS_SETLKW(cmd))
  206. && fl->fl_type == F_UNLCK
  207. && (current->flags & PF_EXITING)) {
  208. sigfillset(&current->blocked); /* Mask all signals */
  209. recalc_sigpending();
  210. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  211. call = nlmclnt_alloc_call();
  212. if (!call) {
  213. status = -ENOMEM;
  214. goto out_restore;
  215. }
  216. call->a_flags = RPC_TASK_ASYNC;
  217. } else {
  218. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  219. memset(call, 0, sizeof(*call));
  220. locks_init_lock(&call->a_args.lock.fl);
  221. locks_init_lock(&call->a_res.lock.fl);
  222. }
  223. call->a_host = host;
  224. nlmclnt_locks_init_private(fl, host);
  225. /* Set up the argument struct */
  226. nlmclnt_setlockargs(call, fl);
  227. if (IS_SETLK(cmd) || IS_SETLKW(cmd)) {
  228. if (fl->fl_type != F_UNLCK) {
  229. call->a_args.block = IS_SETLKW(cmd) ? 1 : 0;
  230. status = nlmclnt_lock(call, fl);
  231. } else
  232. status = nlmclnt_unlock(call, fl);
  233. } else if (IS_GETLK(cmd))
  234. status = nlmclnt_test(call, fl);
  235. else
  236. status = -EINVAL;
  237. out_restore:
  238. spin_lock_irqsave(&current->sighand->siglock, flags);
  239. current->blocked = oldset;
  240. recalc_sigpending();
  241. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  242. done:
  243. dprintk("lockd: clnt proc returns %d\n", status);
  244. nlm_release_host(host);
  245. return status;
  246. }
  247. EXPORT_SYMBOL(nlmclnt_proc);
  248. /*
  249. * Allocate an NLM RPC call struct
  250. */
  251. struct nlm_rqst *
  252. nlmclnt_alloc_call(void)
  253. {
  254. struct nlm_rqst *call;
  255. while (!signalled()) {
  256. call = (struct nlm_rqst *) kmalloc(sizeof(struct nlm_rqst), GFP_KERNEL);
  257. if (call) {
  258. memset(call, 0, sizeof(*call));
  259. locks_init_lock(&call->a_args.lock.fl);
  260. locks_init_lock(&call->a_res.lock.fl);
  261. return call;
  262. }
  263. printk("nlmclnt_alloc_call: failed, waiting for memory\n");
  264. current->state = TASK_INTERRUPTIBLE;
  265. schedule_timeout(5*HZ);
  266. }
  267. return NULL;
  268. }
  269. static int nlm_wait_on_grace(wait_queue_head_t *queue)
  270. {
  271. DEFINE_WAIT(wait);
  272. int status = -EINTR;
  273. prepare_to_wait(queue, &wait, TASK_INTERRUPTIBLE);
  274. if (!signalled ()) {
  275. schedule_timeout(NLMCLNT_GRACE_WAIT);
  276. try_to_freeze(PF_FREEZE);
  277. if (!signalled ())
  278. status = 0;
  279. }
  280. finish_wait(queue, &wait);
  281. return status;
  282. }
  283. /*
  284. * Generic NLM call
  285. */
  286. static int
  287. nlmclnt_call(struct nlm_rqst *req, u32 proc)
  288. {
  289. struct nlm_host *host = req->a_host;
  290. struct rpc_clnt *clnt;
  291. struct nlm_args *argp = &req->a_args;
  292. struct nlm_res *resp = &req->a_res;
  293. struct rpc_message msg = {
  294. .rpc_argp = argp,
  295. .rpc_resp = resp,
  296. };
  297. int status;
  298. dprintk("lockd: call procedure %d on %s\n",
  299. (int)proc, host->h_name);
  300. do {
  301. if (host->h_reclaiming && !argp->reclaim)
  302. goto in_grace_period;
  303. /* If we have no RPC client yet, create one. */
  304. if ((clnt = nlm_bind_host(host)) == NULL)
  305. return -ENOLCK;
  306. msg.rpc_proc = &clnt->cl_procinfo[proc];
  307. /* Perform the RPC call. If an error occurs, try again */
  308. if ((status = rpc_call_sync(clnt, &msg, 0)) < 0) {
  309. dprintk("lockd: rpc_call returned error %d\n", -status);
  310. switch (status) {
  311. case -EPROTONOSUPPORT:
  312. status = -EINVAL;
  313. break;
  314. case -ECONNREFUSED:
  315. case -ETIMEDOUT:
  316. case -ENOTCONN:
  317. nlm_rebind_host(host);
  318. status = -EAGAIN;
  319. break;
  320. case -ERESTARTSYS:
  321. return signalled () ? -EINTR : status;
  322. default:
  323. break;
  324. }
  325. break;
  326. } else
  327. if (resp->status == NLM_LCK_DENIED_GRACE_PERIOD) {
  328. dprintk("lockd: server in grace period\n");
  329. if (argp->reclaim) {
  330. printk(KERN_WARNING
  331. "lockd: spurious grace period reject?!\n");
  332. return -ENOLCK;
  333. }
  334. } else {
  335. if (!argp->reclaim) {
  336. /* We appear to be out of the grace period */
  337. wake_up_all(&host->h_gracewait);
  338. }
  339. dprintk("lockd: server returns status %d\n", resp->status);
  340. return 0; /* Okay, call complete */
  341. }
  342. in_grace_period:
  343. /*
  344. * The server has rebooted and appears to be in the grace
  345. * period during which locks are only allowed to be
  346. * reclaimed.
  347. * We can only back off and try again later.
  348. */
  349. status = nlm_wait_on_grace(&host->h_gracewait);
  350. } while (status == 0);
  351. return status;
  352. }
  353. /*
  354. * Generic NLM call, async version.
  355. */
  356. int
  357. nlmsvc_async_call(struct nlm_rqst *req, u32 proc, rpc_action callback)
  358. {
  359. struct nlm_host *host = req->a_host;
  360. struct rpc_clnt *clnt;
  361. struct rpc_message msg = {
  362. .rpc_argp = &req->a_args,
  363. .rpc_resp = &req->a_res,
  364. };
  365. int status;
  366. dprintk("lockd: call procedure %d on %s (async)\n",
  367. (int)proc, host->h_name);
  368. /* If we have no RPC client yet, create one. */
  369. if ((clnt = nlm_bind_host(host)) == NULL)
  370. return -ENOLCK;
  371. msg.rpc_proc = &clnt->cl_procinfo[proc];
  372. /* bootstrap and kick off the async RPC call */
  373. status = rpc_call_async(clnt, &msg, RPC_TASK_ASYNC, callback, req);
  374. return status;
  375. }
  376. static int
  377. nlmclnt_async_call(struct nlm_rqst *req, u32 proc, rpc_action callback)
  378. {
  379. struct nlm_host *host = req->a_host;
  380. struct rpc_clnt *clnt;
  381. struct nlm_args *argp = &req->a_args;
  382. struct nlm_res *resp = &req->a_res;
  383. struct rpc_message msg = {
  384. .rpc_argp = argp,
  385. .rpc_resp = resp,
  386. };
  387. int status;
  388. dprintk("lockd: call procedure %d on %s (async)\n",
  389. (int)proc, host->h_name);
  390. /* If we have no RPC client yet, create one. */
  391. if ((clnt = nlm_bind_host(host)) == NULL)
  392. return -ENOLCK;
  393. msg.rpc_proc = &clnt->cl_procinfo[proc];
  394. /* Increment host refcount */
  395. nlm_get_host(host);
  396. /* bootstrap and kick off the async RPC call */
  397. status = rpc_call_async(clnt, &msg, RPC_TASK_ASYNC, callback, req);
  398. if (status < 0)
  399. nlm_release_host(host);
  400. return status;
  401. }
  402. /*
  403. * TEST for the presence of a conflicting lock
  404. */
  405. static int
  406. nlmclnt_test(struct nlm_rqst *req, struct file_lock *fl)
  407. {
  408. int status;
  409. status = nlmclnt_call(req, NLMPROC_TEST);
  410. nlmclnt_release_lockargs(req);
  411. if (status < 0)
  412. return status;
  413. status = req->a_res.status;
  414. if (status == NLM_LCK_GRANTED) {
  415. fl->fl_type = F_UNLCK;
  416. } if (status == NLM_LCK_DENIED) {
  417. /*
  418. * Report the conflicting lock back to the application.
  419. */
  420. locks_copy_lock(fl, &req->a_res.lock.fl);
  421. fl->fl_pid = 0;
  422. } else {
  423. return nlm_stat_to_errno(req->a_res.status);
  424. }
  425. return 0;
  426. }
  427. static void nlmclnt_locks_copy_lock(struct file_lock *new, struct file_lock *fl)
  428. {
  429. memcpy(&new->fl_u.nfs_fl, &fl->fl_u.nfs_fl, sizeof(new->fl_u.nfs_fl));
  430. nlm_get_lockowner(new->fl_u.nfs_fl.owner);
  431. }
  432. static void nlmclnt_locks_release_private(struct file_lock *fl)
  433. {
  434. nlm_put_lockowner(fl->fl_u.nfs_fl.owner);
  435. fl->fl_ops = NULL;
  436. }
  437. static struct file_lock_operations nlmclnt_lock_ops = {
  438. .fl_copy_lock = nlmclnt_locks_copy_lock,
  439. .fl_release_private = nlmclnt_locks_release_private,
  440. };
  441. static void nlmclnt_locks_init_private(struct file_lock *fl, struct nlm_host *host)
  442. {
  443. BUG_ON(fl->fl_ops != NULL);
  444. fl->fl_u.nfs_fl.state = 0;
  445. fl->fl_u.nfs_fl.flags = 0;
  446. fl->fl_u.nfs_fl.owner = nlm_find_lockowner(host, fl->fl_owner);
  447. fl->fl_ops = &nlmclnt_lock_ops;
  448. }
  449. static void do_vfs_lock(struct file_lock *fl)
  450. {
  451. int res = 0;
  452. switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
  453. case FL_POSIX:
  454. res = posix_lock_file_wait(fl->fl_file, fl);
  455. break;
  456. case FL_FLOCK:
  457. res = flock_lock_file_wait(fl->fl_file, fl);
  458. break;
  459. default:
  460. BUG();
  461. }
  462. if (res < 0)
  463. printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n",
  464. __FUNCTION__);
  465. }
  466. /*
  467. * LOCK: Try to create a lock
  468. *
  469. * Programmer Harassment Alert
  470. *
  471. * When given a blocking lock request in a sync RPC call, the HPUX lockd
  472. * will faithfully return LCK_BLOCKED but never cares to notify us when
  473. * the lock could be granted. This way, our local process could hang
  474. * around forever waiting for the callback.
  475. *
  476. * Solution A: Implement busy-waiting
  477. * Solution B: Use the async version of the call (NLM_LOCK_{MSG,RES})
  478. *
  479. * For now I am implementing solution A, because I hate the idea of
  480. * re-implementing lockd for a third time in two months. The async
  481. * calls shouldn't be too hard to do, however.
  482. *
  483. * This is one of the lovely things about standards in the NFS area:
  484. * they're so soft and squishy you can't really blame HP for doing this.
  485. */
  486. static int
  487. nlmclnt_lock(struct nlm_rqst *req, struct file_lock *fl)
  488. {
  489. struct nlm_host *host = req->a_host;
  490. struct nlm_res *resp = &req->a_res;
  491. int status;
  492. if (!host->h_monitored && nsm_monitor(host) < 0) {
  493. printk(KERN_NOTICE "lockd: failed to monitor %s\n",
  494. host->h_name);
  495. status = -ENOLCK;
  496. goto out;
  497. }
  498. do {
  499. if ((status = nlmclnt_call(req, NLMPROC_LOCK)) >= 0) {
  500. if (resp->status != NLM_LCK_BLOCKED)
  501. break;
  502. status = nlmclnt_block(host, fl, &resp->status);
  503. }
  504. if (status < 0)
  505. goto out;
  506. } while (resp->status == NLM_LCK_BLOCKED && req->a_args.block);
  507. if (resp->status == NLM_LCK_GRANTED) {
  508. fl->fl_u.nfs_fl.state = host->h_state;
  509. fl->fl_u.nfs_fl.flags |= NFS_LCK_GRANTED;
  510. fl->fl_flags |= FL_SLEEP;
  511. do_vfs_lock(fl);
  512. }
  513. status = nlm_stat_to_errno(resp->status);
  514. out:
  515. nlmclnt_release_lockargs(req);
  516. return status;
  517. }
  518. /*
  519. * RECLAIM: Try to reclaim a lock
  520. */
  521. int
  522. nlmclnt_reclaim(struct nlm_host *host, struct file_lock *fl)
  523. {
  524. struct nlm_rqst reqst, *req;
  525. int status;
  526. req = &reqst;
  527. memset(req, 0, sizeof(*req));
  528. locks_init_lock(&req->a_args.lock.fl);
  529. locks_init_lock(&req->a_res.lock.fl);
  530. req->a_host = host;
  531. req->a_flags = 0;
  532. /* Set up the argument struct */
  533. nlmclnt_setlockargs(req, fl);
  534. req->a_args.reclaim = 1;
  535. if ((status = nlmclnt_call(req, NLMPROC_LOCK)) >= 0
  536. && req->a_res.status == NLM_LCK_GRANTED)
  537. return 0;
  538. printk(KERN_WARNING "lockd: failed to reclaim lock for pid %d "
  539. "(errno %d, status %d)\n", fl->fl_pid,
  540. status, req->a_res.status);
  541. /*
  542. * FIXME: This is a serious failure. We can
  543. *
  544. * a. Ignore the problem
  545. * b. Send the owning process some signal (Linux doesn't have
  546. * SIGLOST, though...)
  547. * c. Retry the operation
  548. *
  549. * Until someone comes up with a simple implementation
  550. * for b or c, I'll choose option a.
  551. */
  552. return -ENOLCK;
  553. }
  554. /*
  555. * UNLOCK: remove an existing lock
  556. */
  557. static int
  558. nlmclnt_unlock(struct nlm_rqst *req, struct file_lock *fl)
  559. {
  560. struct nlm_res *resp = &req->a_res;
  561. int status;
  562. /* Clean the GRANTED flag now so the lock doesn't get
  563. * reclaimed while we're stuck in the unlock call. */
  564. fl->fl_u.nfs_fl.flags &= ~NFS_LCK_GRANTED;
  565. if (req->a_flags & RPC_TASK_ASYNC) {
  566. status = nlmclnt_async_call(req, NLMPROC_UNLOCK,
  567. nlmclnt_unlock_callback);
  568. /* Hrmf... Do the unlock early since locks_remove_posix()
  569. * really expects us to free the lock synchronously */
  570. do_vfs_lock(fl);
  571. if (status < 0) {
  572. nlmclnt_release_lockargs(req);
  573. kfree(req);
  574. }
  575. return status;
  576. }
  577. status = nlmclnt_call(req, NLMPROC_UNLOCK);
  578. nlmclnt_release_lockargs(req);
  579. if (status < 0)
  580. return status;
  581. do_vfs_lock(fl);
  582. if (resp->status == NLM_LCK_GRANTED)
  583. return 0;
  584. if (resp->status != NLM_LCK_DENIED_NOLOCKS)
  585. printk("lockd: unexpected unlock status: %d\n", resp->status);
  586. /* What to do now? I'm out of my depth... */
  587. return -ENOLCK;
  588. }
  589. static void
  590. nlmclnt_unlock_callback(struct rpc_task *task)
  591. {
  592. struct nlm_rqst *req = (struct nlm_rqst *) task->tk_calldata;
  593. int status = req->a_res.status;
  594. if (RPC_ASSASSINATED(task))
  595. goto die;
  596. if (task->tk_status < 0) {
  597. dprintk("lockd: unlock failed (err = %d)\n", -task->tk_status);
  598. goto retry_rebind;
  599. }
  600. if (status == NLM_LCK_DENIED_GRACE_PERIOD) {
  601. rpc_delay(task, NLMCLNT_GRACE_WAIT);
  602. goto retry_unlock;
  603. }
  604. if (status != NLM_LCK_GRANTED)
  605. printk(KERN_WARNING "lockd: unexpected unlock status: %d\n", status);
  606. die:
  607. nlm_release_host(req->a_host);
  608. nlmclnt_release_lockargs(req);
  609. kfree(req);
  610. return;
  611. retry_rebind:
  612. nlm_rebind_host(req->a_host);
  613. retry_unlock:
  614. rpc_restart_call(task);
  615. }
  616. /*
  617. * Cancel a blocked lock request.
  618. * We always use an async RPC call for this in order not to hang a
  619. * process that has been Ctrl-C'ed.
  620. */
  621. int
  622. nlmclnt_cancel(struct nlm_host *host, struct file_lock *fl)
  623. {
  624. struct nlm_rqst *req;
  625. unsigned long flags;
  626. sigset_t oldset;
  627. int status;
  628. /* Block all signals while setting up call */
  629. spin_lock_irqsave(&current->sighand->siglock, flags);
  630. oldset = current->blocked;
  631. sigfillset(&current->blocked);
  632. recalc_sigpending();
  633. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  634. req = nlmclnt_alloc_call();
  635. if (!req)
  636. return -ENOMEM;
  637. req->a_host = host;
  638. req->a_flags = RPC_TASK_ASYNC;
  639. nlmclnt_setlockargs(req, fl);
  640. status = nlmclnt_async_call(req, NLMPROC_CANCEL,
  641. nlmclnt_cancel_callback);
  642. if (status < 0) {
  643. nlmclnt_release_lockargs(req);
  644. kfree(req);
  645. }
  646. spin_lock_irqsave(&current->sighand->siglock, flags);
  647. current->blocked = oldset;
  648. recalc_sigpending();
  649. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  650. return status;
  651. }
  652. static void
  653. nlmclnt_cancel_callback(struct rpc_task *task)
  654. {
  655. struct nlm_rqst *req = (struct nlm_rqst *) task->tk_calldata;
  656. if (RPC_ASSASSINATED(task))
  657. goto die;
  658. if (task->tk_status < 0) {
  659. dprintk("lockd: CANCEL call error %d, retrying.\n",
  660. task->tk_status);
  661. goto retry_cancel;
  662. }
  663. dprintk("lockd: cancel status %d (task %d)\n",
  664. req->a_res.status, task->tk_pid);
  665. switch (req->a_res.status) {
  666. case NLM_LCK_GRANTED:
  667. case NLM_LCK_DENIED_GRACE_PERIOD:
  668. /* Everything's good */
  669. break;
  670. case NLM_LCK_DENIED_NOLOCKS:
  671. dprintk("lockd: CANCEL failed (server has no locks)\n");
  672. goto retry_cancel;
  673. default:
  674. printk(KERN_NOTICE "lockd: weird return %d for CANCEL call\n",
  675. req->a_res.status);
  676. }
  677. die:
  678. nlm_release_host(req->a_host);
  679. nlmclnt_release_lockargs(req);
  680. kfree(req);
  681. return;
  682. retry_cancel:
  683. nlm_rebind_host(req->a_host);
  684. rpc_restart_call(task);
  685. rpc_delay(task, 30 * HZ);
  686. }
  687. /*
  688. * Convert an NLM status code to a generic kernel errno
  689. */
  690. static int
  691. nlm_stat_to_errno(u32 status)
  692. {
  693. switch(status) {
  694. case NLM_LCK_GRANTED:
  695. return 0;
  696. case NLM_LCK_DENIED:
  697. return -EAGAIN;
  698. case NLM_LCK_DENIED_NOLOCKS:
  699. case NLM_LCK_DENIED_GRACE_PERIOD:
  700. return -ENOLCK;
  701. case NLM_LCK_BLOCKED:
  702. printk(KERN_NOTICE "lockd: unexpected status NLM_BLOCKED\n");
  703. return -ENOLCK;
  704. #ifdef CONFIG_LOCKD_V4
  705. case NLM_DEADLCK:
  706. return -EDEADLK;
  707. case NLM_ROFS:
  708. return -EROFS;
  709. case NLM_STALE_FH:
  710. return -ESTALE;
  711. case NLM_FBIG:
  712. return -EOVERFLOW;
  713. case NLM_FAILED:
  714. return -ENOLCK;
  715. #endif
  716. }
  717. printk(KERN_NOTICE "lockd: unexpected server status %d\n", status);
  718. return -ENOLCK;
  719. }