svc4proc.c 14 KB

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