svc4proc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /*
  2. * linux/fs/lockd/svc4proc.c
  3. *
  4. * Lockd server procedures. We don't implement the NLM_*_RES
  5. * procedures because we don't use the async procedures.
  6. *
  7. * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
  8. */
  9. #include <linux/types.h>
  10. #include <linux/time.h>
  11. #include <linux/slab.h>
  12. #include <linux/in.h>
  13. #include <linux/sunrpc/svc.h>
  14. #include <linux/sunrpc/clnt.h>
  15. #include <linux/nfsd/nfsd.h>
  16. #include <linux/lockd/lockd.h>
  17. #include <linux/lockd/share.h>
  18. #define NLMDBG_FACILITY NLMDBG_CLIENT
  19. /*
  20. * Obtain client and file from arguments
  21. */
  22. static __be32
  23. nlm4svc_retrieve_args(struct svc_rqst *rqstp, struct nlm_args *argp,
  24. struct nlm_host **hostp, struct nlm_file **filp)
  25. {
  26. struct nlm_host *host = NULL;
  27. struct nlm_file *file = NULL;
  28. struct nlm_lock *lock = &argp->lock;
  29. __be32 error = 0;
  30. /* nfsd callbacks must have been installed for this procedure */
  31. if (!nlmsvc_ops)
  32. return nlm_lck_denied_nolocks;
  33. /* Obtain host handle */
  34. if (!(host = nlmsvc_lookup_host(rqstp, lock->caller, lock->len))
  35. || (argp->monitor && nsm_monitor(host) < 0))
  36. goto no_locks;
  37. *hostp = host;
  38. /* Obtain file pointer. Not used by FREE_ALL call. */
  39. if (filp != NULL) {
  40. if ((error = nlm_lookup_file(rqstp, &file, &lock->fh)) != 0)
  41. goto no_locks;
  42. *filp = file;
  43. /* Set up the missing parts of the file_lock structure */
  44. lock->fl.fl_file = file->f_file;
  45. lock->fl.fl_owner = (fl_owner_t) host;
  46. lock->fl.fl_lmops = &nlmsvc_lock_operations;
  47. }
  48. return 0;
  49. no_locks:
  50. nlm_release_host(host);
  51. if (error)
  52. return error;
  53. return nlm_lck_denied_nolocks;
  54. }
  55. /*
  56. * NULL: Test for presence of service
  57. */
  58. static __be32
  59. nlm4svc_proc_null(struct svc_rqst *rqstp, void *argp, void *resp)
  60. {
  61. dprintk("lockd: NULL called\n");
  62. return rpc_success;
  63. }
  64. /*
  65. * TEST: Check for conflicting lock
  66. */
  67. static __be32
  68. nlm4svc_proc_test(struct svc_rqst *rqstp, struct nlm_args *argp,
  69. struct nlm_res *resp)
  70. {
  71. struct nlm_host *host;
  72. struct nlm_file *file;
  73. __be32 rc = rpc_success;
  74. dprintk("lockd: TEST4 called\n");
  75. resp->cookie = argp->cookie;
  76. /* Obtain client and file */
  77. if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
  78. return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
  79. /* Now check for conflicting locks */
  80. resp->status = nlmsvc_testlock(rqstp, file, host, &argp->lock, &resp->lock, &resp->cookie);
  81. if (resp->status == nlm_drop_reply)
  82. rc = rpc_drop_reply;
  83. else
  84. dprintk("lockd: TEST4 status %d\n", ntohl(resp->status));
  85. nlm_release_host(host);
  86. nlm_release_file(file);
  87. return rc;
  88. }
  89. static __be32
  90. nlm4svc_proc_lock(struct svc_rqst *rqstp, struct nlm_args *argp,
  91. struct nlm_res *resp)
  92. {
  93. struct nlm_host *host;
  94. struct nlm_file *file;
  95. __be32 rc = rpc_success;
  96. dprintk("lockd: LOCK called\n");
  97. resp->cookie = argp->cookie;
  98. /* Obtain client and file */
  99. if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
  100. return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
  101. #if 0
  102. /* If supplied state doesn't match current state, we assume it's
  103. * an old request that time-warped somehow. Any error return would
  104. * do in this case because it's irrelevant anyway.
  105. *
  106. * NB: We don't retrieve the remote host's state yet.
  107. */
  108. if (host->h_nsmstate && host->h_nsmstate != argp->state) {
  109. resp->status = nlm_lck_denied_nolocks;
  110. } else
  111. #endif
  112. /* Now try to lock the file */
  113. resp->status = nlmsvc_lock(rqstp, file, host, &argp->lock,
  114. argp->block, &argp->cookie,
  115. argp->reclaim);
  116. if (resp->status == nlm_drop_reply)
  117. rc = rpc_drop_reply;
  118. else
  119. dprintk("lockd: LOCK status %d\n", ntohl(resp->status));
  120. nlm_release_host(host);
  121. nlm_release_file(file);
  122. return rc;
  123. }
  124. static __be32
  125. nlm4svc_proc_cancel(struct svc_rqst *rqstp, struct nlm_args *argp,
  126. struct nlm_res *resp)
  127. {
  128. struct nlm_host *host;
  129. struct nlm_file *file;
  130. dprintk("lockd: CANCEL called\n");
  131. resp->cookie = argp->cookie;
  132. /* Don't accept requests during grace period */
  133. if (locks_in_grace()) {
  134. resp->status = nlm_lck_denied_grace_period;
  135. return rpc_success;
  136. }
  137. /* Obtain client and file */
  138. if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
  139. return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
  140. /* Try to cancel request. */
  141. resp->status = nlmsvc_cancel_blocked(file, &argp->lock);
  142. dprintk("lockd: CANCEL status %d\n", ntohl(resp->status));
  143. nlm_release_host(host);
  144. nlm_release_file(file);
  145. return rpc_success;
  146. }
  147. /*
  148. * UNLOCK: release a lock
  149. */
  150. static __be32
  151. nlm4svc_proc_unlock(struct svc_rqst *rqstp, struct nlm_args *argp,
  152. struct nlm_res *resp)
  153. {
  154. struct nlm_host *host;
  155. struct nlm_file *file;
  156. dprintk("lockd: UNLOCK called\n");
  157. resp->cookie = argp->cookie;
  158. /* Don't accept new lock requests during grace period */
  159. if (locks_in_grace()) {
  160. resp->status = nlm_lck_denied_grace_period;
  161. return rpc_success;
  162. }
  163. /* Obtain client and file */
  164. if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
  165. return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
  166. /* Now try to remove the lock */
  167. resp->status = nlmsvc_unlock(file, &argp->lock);
  168. dprintk("lockd: UNLOCK status %d\n", ntohl(resp->status));
  169. nlm_release_host(host);
  170. nlm_release_file(file);
  171. return rpc_success;
  172. }
  173. /*
  174. * GRANTED: A server calls us to tell that a process' lock request
  175. * was granted
  176. */
  177. static __be32
  178. nlm4svc_proc_granted(struct svc_rqst *rqstp, struct nlm_args *argp,
  179. struct nlm_res *resp)
  180. {
  181. resp->cookie = argp->cookie;
  182. dprintk("lockd: GRANTED called\n");
  183. resp->status = nlmclnt_grant(svc_addr(rqstp), &argp->lock);
  184. dprintk("lockd: GRANTED status %d\n", ntohl(resp->status));
  185. return rpc_success;
  186. }
  187. /*
  188. * This is the generic lockd callback for async RPC calls
  189. */
  190. static void nlm4svc_callback_exit(struct rpc_task *task, void *data)
  191. {
  192. dprintk("lockd: %5u callback returned %d\n", task->tk_pid,
  193. -task->tk_status);
  194. }
  195. static void nlm4svc_callback_release(void *data)
  196. {
  197. lock_kernel();
  198. nlm_release_call(data);
  199. unlock_kernel();
  200. }
  201. static const struct rpc_call_ops nlm4svc_callback_ops = {
  202. .rpc_call_done = nlm4svc_callback_exit,
  203. .rpc_release = nlm4svc_callback_release,
  204. };
  205. /*
  206. * `Async' versions of the above service routines. They aren't really,
  207. * because we send the callback before the reply proper. I hope this
  208. * doesn't break any clients.
  209. */
  210. static __be32 nlm4svc_callback(struct svc_rqst *rqstp, u32 proc, struct nlm_args *argp,
  211. __be32 (*func)(struct svc_rqst *, struct nlm_args *, struct nlm_res *))
  212. {
  213. struct nlm_host *host;
  214. struct nlm_rqst *call;
  215. __be32 stat;
  216. host = nlmsvc_lookup_host(rqstp,
  217. argp->lock.caller,
  218. argp->lock.len);
  219. if (host == NULL)
  220. return rpc_system_err;
  221. call = nlm_alloc_call(host);
  222. if (call == NULL)
  223. return rpc_system_err;
  224. stat = func(rqstp, argp, &call->a_res);
  225. if (stat != 0) {
  226. nlm_release_call(call);
  227. return stat;
  228. }
  229. call->a_flags = RPC_TASK_ASYNC;
  230. if (nlm_async_reply(call, proc, &nlm4svc_callback_ops) < 0)
  231. return rpc_system_err;
  232. return rpc_success;
  233. }
  234. static __be32 nlm4svc_proc_test_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
  235. void *resp)
  236. {
  237. dprintk("lockd: TEST_MSG called\n");
  238. return nlm4svc_callback(rqstp, NLMPROC_TEST_RES, argp, nlm4svc_proc_test);
  239. }
  240. static __be32 nlm4svc_proc_lock_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
  241. void *resp)
  242. {
  243. dprintk("lockd: LOCK_MSG called\n");
  244. return nlm4svc_callback(rqstp, NLMPROC_LOCK_RES, argp, nlm4svc_proc_lock);
  245. }
  246. static __be32 nlm4svc_proc_cancel_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
  247. void *resp)
  248. {
  249. dprintk("lockd: CANCEL_MSG called\n");
  250. return nlm4svc_callback(rqstp, NLMPROC_CANCEL_RES, argp, nlm4svc_proc_cancel);
  251. }
  252. static __be32 nlm4svc_proc_unlock_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
  253. void *resp)
  254. {
  255. dprintk("lockd: UNLOCK_MSG called\n");
  256. return nlm4svc_callback(rqstp, NLMPROC_UNLOCK_RES, argp, nlm4svc_proc_unlock);
  257. }
  258. static __be32 nlm4svc_proc_granted_msg(struct svc_rqst *rqstp, struct nlm_args *argp,
  259. void *resp)
  260. {
  261. dprintk("lockd: GRANTED_MSG called\n");
  262. return nlm4svc_callback(rqstp, NLMPROC_GRANTED_RES, argp, nlm4svc_proc_granted);
  263. }
  264. /*
  265. * SHARE: create a DOS share or alter existing share.
  266. */
  267. static __be32
  268. nlm4svc_proc_share(struct svc_rqst *rqstp, struct nlm_args *argp,
  269. struct nlm_res *resp)
  270. {
  271. struct nlm_host *host;
  272. struct nlm_file *file;
  273. dprintk("lockd: SHARE called\n");
  274. resp->cookie = argp->cookie;
  275. /* Don't accept new lock requests during grace period */
  276. if (locks_in_grace() && !argp->reclaim) {
  277. resp->status = nlm_lck_denied_grace_period;
  278. return rpc_success;
  279. }
  280. /* Obtain client and file */
  281. if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
  282. return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
  283. /* Now try to create the share */
  284. resp->status = nlmsvc_share_file(host, file, argp);
  285. dprintk("lockd: SHARE status %d\n", ntohl(resp->status));
  286. nlm_release_host(host);
  287. nlm_release_file(file);
  288. return rpc_success;
  289. }
  290. /*
  291. * UNSHARE: Release a DOS share.
  292. */
  293. static __be32
  294. nlm4svc_proc_unshare(struct svc_rqst *rqstp, struct nlm_args *argp,
  295. struct nlm_res *resp)
  296. {
  297. struct nlm_host *host;
  298. struct nlm_file *file;
  299. dprintk("lockd: UNSHARE called\n");
  300. resp->cookie = argp->cookie;
  301. /* Don't accept requests during grace period */
  302. if (locks_in_grace()) {
  303. resp->status = nlm_lck_denied_grace_period;
  304. return rpc_success;
  305. }
  306. /* Obtain client and file */
  307. if ((resp->status = nlm4svc_retrieve_args(rqstp, argp, &host, &file)))
  308. return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
  309. /* Now try to lock the file */
  310. resp->status = nlmsvc_unshare_file(host, file, argp);
  311. dprintk("lockd: UNSHARE status %d\n", ntohl(resp->status));
  312. nlm_release_host(host);
  313. nlm_release_file(file);
  314. return rpc_success;
  315. }
  316. /*
  317. * NM_LOCK: Create an unmonitored lock
  318. */
  319. static __be32
  320. nlm4svc_proc_nm_lock(struct svc_rqst *rqstp, struct nlm_args *argp,
  321. struct nlm_res *resp)
  322. {
  323. dprintk("lockd: NM_LOCK called\n");
  324. argp->monitor = 0; /* just clean the monitor flag */
  325. return nlm4svc_proc_lock(rqstp, argp, resp);
  326. }
  327. /*
  328. * FREE_ALL: Release all locks and shares held by client
  329. */
  330. static __be32
  331. nlm4svc_proc_free_all(struct svc_rqst *rqstp, struct nlm_args *argp,
  332. void *resp)
  333. {
  334. struct nlm_host *host;
  335. /* Obtain client */
  336. if (nlm4svc_retrieve_args(rqstp, argp, &host, NULL))
  337. return rpc_success;
  338. nlmsvc_free_host_resources(host);
  339. nlm_release_host(host);
  340. return rpc_success;
  341. }
  342. /*
  343. * SM_NOTIFY: private callback from statd (not part of official NLM proto)
  344. */
  345. static __be32
  346. nlm4svc_proc_sm_notify(struct svc_rqst *rqstp, struct nlm_reboot *argp,
  347. void *resp)
  348. {
  349. dprintk("lockd: SM_NOTIFY called\n");
  350. if (!nlm_privileged_requester(rqstp)) {
  351. char buf[RPC_MAX_ADDRBUFLEN];
  352. printk(KERN_WARNING "lockd: rejected NSM callback from %s\n",
  353. svc_print_addr(rqstp, buf, sizeof(buf)));
  354. return rpc_system_err;
  355. }
  356. nlm_host_rebooted(argp);
  357. return rpc_success;
  358. }
  359. /*
  360. * client sent a GRANTED_RES, let's remove the associated block
  361. */
  362. static __be32
  363. nlm4svc_proc_granted_res(struct svc_rqst *rqstp, struct nlm_res *argp,
  364. void *resp)
  365. {
  366. if (!nlmsvc_ops)
  367. return rpc_success;
  368. dprintk("lockd: GRANTED_RES called\n");
  369. nlmsvc_grant_reply(&argp->cookie, argp->status);
  370. return rpc_success;
  371. }
  372. /*
  373. * NLM Server procedures.
  374. */
  375. #define nlm4svc_encode_norep nlm4svc_encode_void
  376. #define nlm4svc_decode_norep nlm4svc_decode_void
  377. #define nlm4svc_decode_testres nlm4svc_decode_void
  378. #define nlm4svc_decode_lockres nlm4svc_decode_void
  379. #define nlm4svc_decode_unlockres nlm4svc_decode_void
  380. #define nlm4svc_decode_cancelres nlm4svc_decode_void
  381. #define nlm4svc_decode_grantedres nlm4svc_decode_void
  382. #define nlm4svc_proc_none nlm4svc_proc_null
  383. #define nlm4svc_proc_test_res nlm4svc_proc_null
  384. #define nlm4svc_proc_lock_res nlm4svc_proc_null
  385. #define nlm4svc_proc_cancel_res nlm4svc_proc_null
  386. #define nlm4svc_proc_unlock_res nlm4svc_proc_null
  387. struct nlm_void { int dummy; };
  388. #define PROC(name, xargt, xrest, argt, rest, respsize) \
  389. { .pc_func = (svc_procfunc) nlm4svc_proc_##name, \
  390. .pc_decode = (kxdrproc_t) nlm4svc_decode_##xargt, \
  391. .pc_encode = (kxdrproc_t) nlm4svc_encode_##xrest, \
  392. .pc_release = NULL, \
  393. .pc_argsize = sizeof(struct nlm_##argt), \
  394. .pc_ressize = sizeof(struct nlm_##rest), \
  395. .pc_xdrressize = respsize, \
  396. }
  397. #define Ck (1+XDR_QUADLEN(NLM_MAXCOOKIELEN)) /* cookie */
  398. #define No (1+1024/4) /* netobj */
  399. #define St 1 /* status */
  400. #define Rg 4 /* range (offset + length) */
  401. struct svc_procedure nlmsvc_procedures4[] = {
  402. PROC(null, void, void, void, void, 1),
  403. PROC(test, testargs, testres, args, res, Ck+St+2+No+Rg),
  404. PROC(lock, lockargs, res, args, res, Ck+St),
  405. PROC(cancel, cancargs, res, args, res, Ck+St),
  406. PROC(unlock, unlockargs, res, args, res, Ck+St),
  407. PROC(granted, testargs, res, args, res, Ck+St),
  408. PROC(test_msg, testargs, norep, args, void, 1),
  409. PROC(lock_msg, lockargs, norep, args, void, 1),
  410. PROC(cancel_msg, cancargs, norep, args, void, 1),
  411. PROC(unlock_msg, unlockargs, norep, args, void, 1),
  412. PROC(granted_msg, testargs, norep, args, void, 1),
  413. PROC(test_res, testres, norep, res, void, 1),
  414. PROC(lock_res, lockres, norep, res, void, 1),
  415. PROC(cancel_res, cancelres, norep, res, void, 1),
  416. PROC(unlock_res, unlockres, norep, res, void, 1),
  417. PROC(granted_res, res, norep, res, void, 1),
  418. /* statd callback */
  419. PROC(sm_notify, reboot, void, reboot, void, 1),
  420. PROC(none, void, void, void, void, 0),
  421. PROC(none, void, void, void, void, 0),
  422. PROC(none, void, void, void, void, 0),
  423. PROC(share, shareargs, shareres, args, res, Ck+St+1),
  424. PROC(unshare, shareargs, shareres, args, res, Ck+St+1),
  425. PROC(nm_lock, lockargs, res, args, res, Ck+St),
  426. PROC(free_all, notify, void, args, void, 1),
  427. };