svc4proc.c 14 KB

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