svc4proc.c 14 KB

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