svcproc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/lockd/svcproc.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. #ifdef CONFIG_LOCKD_V4
  17. static __be32
  18. cast_to_nlm(__be32 status, u32 vers)
  19. {
  20. /* Note: status is assumed to be in network byte order !!! */
  21. if (vers != 4){
  22. switch (status) {
  23. case nlm_granted:
  24. case nlm_lck_denied:
  25. case nlm_lck_denied_nolocks:
  26. case nlm_lck_blocked:
  27. case nlm_lck_denied_grace_period:
  28. case nlm_drop_reply:
  29. break;
  30. case nlm4_deadlock:
  31. status = nlm_lck_denied;
  32. break;
  33. default:
  34. status = nlm_lck_denied_nolocks;
  35. }
  36. }
  37. return (status);
  38. }
  39. #define cast_status(status) (cast_to_nlm(status, rqstp->rq_vers))
  40. #else
  41. #define cast_status(status) (status)
  42. #endif
  43. /*
  44. * Obtain client and file from arguments
  45. */
  46. static __be32
  47. nlmsvc_retrieve_args(struct svc_rqst *rqstp, struct nlm_args *argp,
  48. struct nlm_host **hostp, struct nlm_file **filp)
  49. {
  50. struct nlm_host *host = NULL;
  51. struct nlm_file *file = NULL;
  52. struct nlm_lock *lock = &argp->lock;
  53. __be32 error = 0;
  54. /* nfsd callbacks must have been installed for this procedure */
  55. if (!nlmsvc_ops)
  56. return nlm_lck_denied_nolocks;
  57. /* Obtain host handle */
  58. if (!(host = nlmsvc_lookup_host(rqstp, lock->caller, lock->len))
  59. || (argp->monitor && nsm_monitor(host) < 0))
  60. goto no_locks;
  61. *hostp = host;
  62. /* Obtain file pointer. Not used by FREE_ALL call. */
  63. if (filp != NULL) {
  64. error = cast_status(nlm_lookup_file(rqstp, &file, &lock->fh));
  65. if (error != 0)
  66. goto no_locks;
  67. *filp = file;
  68. /* Set up the missing parts of the file_lock structure */
  69. lock->fl.fl_file = file->f_file;
  70. lock->fl.fl_owner = (fl_owner_t) host;
  71. lock->fl.fl_lmops = &nlmsvc_lock_operations;
  72. }
  73. return 0;
  74. no_locks:
  75. nlmsvc_release_host(host);
  76. if (error)
  77. return error;
  78. return nlm_lck_denied_nolocks;
  79. }
  80. /*
  81. * NULL: Test for presence of service
  82. */
  83. static __be32
  84. nlmsvc_proc_null(struct svc_rqst *rqstp)
  85. {
  86. dprintk("lockd: NULL called\n");
  87. return rpc_success;
  88. }
  89. /*
  90. * TEST: Check for conflicting lock
  91. */
  92. static __be32
  93. __nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_res *resp)
  94. {
  95. struct nlm_args *argp = rqstp->rq_argp;
  96. struct nlm_host *host;
  97. struct nlm_file *file;
  98. __be32 rc = rpc_success;
  99. dprintk("lockd: TEST called\n");
  100. resp->cookie = argp->cookie;
  101. /* Obtain client and file */
  102. if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
  103. return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
  104. /* Now check for conflicting locks */
  105. resp->status = cast_status(nlmsvc_testlock(rqstp, file, host, &argp->lock, &resp->lock, &resp->cookie));
  106. if (resp->status == nlm_drop_reply)
  107. rc = rpc_drop_reply;
  108. else
  109. dprintk("lockd: TEST status %d vers %d\n",
  110. ntohl(resp->status), rqstp->rq_vers);
  111. nlmsvc_release_host(host);
  112. nlm_release_file(file);
  113. return rc;
  114. }
  115. static __be32
  116. nlmsvc_proc_test(struct svc_rqst *rqstp)
  117. {
  118. return __nlmsvc_proc_test(rqstp, rqstp->rq_resp);
  119. }
  120. static __be32
  121. __nlmsvc_proc_lock(struct svc_rqst *rqstp, struct nlm_res *resp)
  122. {
  123. struct nlm_args *argp = rqstp->rq_argp;
  124. struct nlm_host *host;
  125. struct nlm_file *file;
  126. __be32 rc = rpc_success;
  127. dprintk("lockd: LOCK called\n");
  128. resp->cookie = argp->cookie;
  129. /* Obtain client and file */
  130. if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
  131. return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
  132. #if 0
  133. /* If supplied state doesn't match current state, we assume it's
  134. * an old request that time-warped somehow. Any error return would
  135. * do in this case because it's irrelevant anyway.
  136. *
  137. * NB: We don't retrieve the remote host's state yet.
  138. */
  139. if (host->h_nsmstate && host->h_nsmstate != argp->state) {
  140. resp->status = nlm_lck_denied_nolocks;
  141. } else
  142. #endif
  143. /* Now try to lock the file */
  144. resp->status = cast_status(nlmsvc_lock(rqstp, file, host, &argp->lock,
  145. argp->block, &argp->cookie,
  146. argp->reclaim));
  147. if (resp->status == nlm_drop_reply)
  148. rc = rpc_drop_reply;
  149. else
  150. dprintk("lockd: LOCK status %d\n", ntohl(resp->status));
  151. nlmsvc_release_host(host);
  152. nlm_release_file(file);
  153. return rc;
  154. }
  155. static __be32
  156. nlmsvc_proc_lock(struct svc_rqst *rqstp)
  157. {
  158. return __nlmsvc_proc_lock(rqstp, rqstp->rq_resp);
  159. }
  160. static __be32
  161. __nlmsvc_proc_cancel(struct svc_rqst *rqstp, struct nlm_res *resp)
  162. {
  163. struct nlm_args *argp = rqstp->rq_argp;
  164. struct nlm_host *host;
  165. struct nlm_file *file;
  166. struct net *net = SVC_NET(rqstp);
  167. dprintk("lockd: CANCEL called\n");
  168. resp->cookie = argp->cookie;
  169. /* Don't accept requests during grace period */
  170. if (locks_in_grace(net)) {
  171. resp->status = nlm_lck_denied_grace_period;
  172. return rpc_success;
  173. }
  174. /* Obtain client and file */
  175. if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
  176. return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
  177. /* Try to cancel request. */
  178. resp->status = cast_status(nlmsvc_cancel_blocked(net, file, &argp->lock));
  179. dprintk("lockd: CANCEL status %d\n", ntohl(resp->status));
  180. nlmsvc_release_host(host);
  181. nlm_release_file(file);
  182. return rpc_success;
  183. }
  184. static __be32
  185. nlmsvc_proc_cancel(struct svc_rqst *rqstp)
  186. {
  187. return __nlmsvc_proc_cancel(rqstp, rqstp->rq_resp);
  188. }
  189. /*
  190. * UNLOCK: release a lock
  191. */
  192. static __be32
  193. __nlmsvc_proc_unlock(struct svc_rqst *rqstp, struct nlm_res *resp)
  194. {
  195. struct nlm_args *argp = rqstp->rq_argp;
  196. struct nlm_host *host;
  197. struct nlm_file *file;
  198. struct net *net = SVC_NET(rqstp);
  199. dprintk("lockd: UNLOCK called\n");
  200. resp->cookie = argp->cookie;
  201. /* Don't accept new lock requests during grace period */
  202. if (locks_in_grace(net)) {
  203. resp->status = nlm_lck_denied_grace_period;
  204. return rpc_success;
  205. }
  206. /* Obtain client and file */
  207. if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
  208. return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
  209. /* Now try to remove the lock */
  210. resp->status = cast_status(nlmsvc_unlock(net, file, &argp->lock));
  211. dprintk("lockd: UNLOCK status %d\n", ntohl(resp->status));
  212. nlmsvc_release_host(host);
  213. nlm_release_file(file);
  214. return rpc_success;
  215. }
  216. static __be32
  217. nlmsvc_proc_unlock(struct svc_rqst *rqstp)
  218. {
  219. return __nlmsvc_proc_unlock(rqstp, rqstp->rq_resp);
  220. }
  221. /*
  222. * GRANTED: A server calls us to tell that a process' lock request
  223. * was granted
  224. */
  225. static __be32
  226. __nlmsvc_proc_granted(struct svc_rqst *rqstp, struct nlm_res *resp)
  227. {
  228. struct nlm_args *argp = rqstp->rq_argp;
  229. resp->cookie = argp->cookie;
  230. dprintk("lockd: GRANTED called\n");
  231. resp->status = nlmclnt_grant(svc_addr(rqstp), &argp->lock);
  232. dprintk("lockd: GRANTED status %d\n", ntohl(resp->status));
  233. return rpc_success;
  234. }
  235. static __be32
  236. nlmsvc_proc_granted(struct svc_rqst *rqstp)
  237. {
  238. return __nlmsvc_proc_granted(rqstp, rqstp->rq_resp);
  239. }
  240. /*
  241. * This is the generic lockd callback for async RPC calls
  242. */
  243. static void nlmsvc_callback_exit(struct rpc_task *task, void *data)
  244. {
  245. dprintk("lockd: %5u callback returned %d\n", task->tk_pid,
  246. -task->tk_status);
  247. }
  248. void nlmsvc_release_call(struct nlm_rqst *call)
  249. {
  250. if (!refcount_dec_and_test(&call->a_count))
  251. return;
  252. nlmsvc_release_host(call->a_host);
  253. kfree(call);
  254. }
  255. static void nlmsvc_callback_release(void *data)
  256. {
  257. nlmsvc_release_call(data);
  258. }
  259. static const struct rpc_call_ops nlmsvc_callback_ops = {
  260. .rpc_call_done = nlmsvc_callback_exit,
  261. .rpc_release = nlmsvc_callback_release,
  262. };
  263. /*
  264. * `Async' versions of the above service routines. They aren't really,
  265. * because we send the callback before the reply proper. I hope this
  266. * doesn't break any clients.
  267. */
  268. static __be32 nlmsvc_callback(struct svc_rqst *rqstp, u32 proc,
  269. __be32 (*func)(struct svc_rqst *, struct nlm_res *))
  270. {
  271. struct nlm_args *argp = rqstp->rq_argp;
  272. struct nlm_host *host;
  273. struct nlm_rqst *call;
  274. __be32 stat;
  275. host = nlmsvc_lookup_host(rqstp,
  276. argp->lock.caller,
  277. argp->lock.len);
  278. if (host == NULL)
  279. return rpc_system_err;
  280. call = nlm_alloc_call(host);
  281. nlmsvc_release_host(host);
  282. if (call == NULL)
  283. return rpc_system_err;
  284. stat = func(rqstp, &call->a_res);
  285. if (stat != 0) {
  286. nlmsvc_release_call(call);
  287. return stat;
  288. }
  289. call->a_flags = RPC_TASK_ASYNC;
  290. if (nlm_async_reply(call, proc, &nlmsvc_callback_ops) < 0)
  291. return rpc_system_err;
  292. return rpc_success;
  293. }
  294. static __be32 nlmsvc_proc_test_msg(struct svc_rqst *rqstp)
  295. {
  296. dprintk("lockd: TEST_MSG called\n");
  297. return nlmsvc_callback(rqstp, NLMPROC_TEST_RES, __nlmsvc_proc_test);
  298. }
  299. static __be32 nlmsvc_proc_lock_msg(struct svc_rqst *rqstp)
  300. {
  301. dprintk("lockd: LOCK_MSG called\n");
  302. return nlmsvc_callback(rqstp, NLMPROC_LOCK_RES, __nlmsvc_proc_lock);
  303. }
  304. static __be32 nlmsvc_proc_cancel_msg(struct svc_rqst *rqstp)
  305. {
  306. dprintk("lockd: CANCEL_MSG called\n");
  307. return nlmsvc_callback(rqstp, NLMPROC_CANCEL_RES, __nlmsvc_proc_cancel);
  308. }
  309. static __be32
  310. nlmsvc_proc_unlock_msg(struct svc_rqst *rqstp)
  311. {
  312. dprintk("lockd: UNLOCK_MSG called\n");
  313. return nlmsvc_callback(rqstp, NLMPROC_UNLOCK_RES, __nlmsvc_proc_unlock);
  314. }
  315. static __be32
  316. nlmsvc_proc_granted_msg(struct svc_rqst *rqstp)
  317. {
  318. dprintk("lockd: GRANTED_MSG called\n");
  319. return nlmsvc_callback(rqstp, NLMPROC_GRANTED_RES, __nlmsvc_proc_granted);
  320. }
  321. /*
  322. * SHARE: create a DOS share or alter existing share.
  323. */
  324. static __be32
  325. nlmsvc_proc_share(struct svc_rqst *rqstp)
  326. {
  327. struct nlm_args *argp = rqstp->rq_argp;
  328. struct nlm_res *resp = rqstp->rq_resp;
  329. struct nlm_host *host;
  330. struct nlm_file *file;
  331. dprintk("lockd: SHARE called\n");
  332. resp->cookie = argp->cookie;
  333. /* Don't accept new lock requests during grace period */
  334. if (locks_in_grace(SVC_NET(rqstp)) && !argp->reclaim) {
  335. resp->status = nlm_lck_denied_grace_period;
  336. return rpc_success;
  337. }
  338. /* Obtain client and file */
  339. if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
  340. return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
  341. /* Now try to create the share */
  342. resp->status = cast_status(nlmsvc_share_file(host, file, argp));
  343. dprintk("lockd: SHARE status %d\n", ntohl(resp->status));
  344. nlmsvc_release_host(host);
  345. nlm_release_file(file);
  346. return rpc_success;
  347. }
  348. /*
  349. * UNSHARE: Release a DOS share.
  350. */
  351. static __be32
  352. nlmsvc_proc_unshare(struct svc_rqst *rqstp)
  353. {
  354. struct nlm_args *argp = rqstp->rq_argp;
  355. struct nlm_res *resp = rqstp->rq_resp;
  356. struct nlm_host *host;
  357. struct nlm_file *file;
  358. dprintk("lockd: UNSHARE called\n");
  359. resp->cookie = argp->cookie;
  360. /* Don't accept requests during grace period */
  361. if (locks_in_grace(SVC_NET(rqstp))) {
  362. resp->status = nlm_lck_denied_grace_period;
  363. return rpc_success;
  364. }
  365. /* Obtain client and file */
  366. if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
  367. return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
  368. /* Now try to unshare the file */
  369. resp->status = cast_status(nlmsvc_unshare_file(host, file, argp));
  370. dprintk("lockd: UNSHARE status %d\n", ntohl(resp->status));
  371. nlmsvc_release_host(host);
  372. nlm_release_file(file);
  373. return rpc_success;
  374. }
  375. /*
  376. * NM_LOCK: Create an unmonitored lock
  377. */
  378. static __be32
  379. nlmsvc_proc_nm_lock(struct svc_rqst *rqstp)
  380. {
  381. struct nlm_args *argp = rqstp->rq_argp;
  382. dprintk("lockd: NM_LOCK called\n");
  383. argp->monitor = 0; /* just clean the monitor flag */
  384. return nlmsvc_proc_lock(rqstp);
  385. }
  386. /*
  387. * FREE_ALL: Release all locks and shares held by client
  388. */
  389. static __be32
  390. nlmsvc_proc_free_all(struct svc_rqst *rqstp)
  391. {
  392. struct nlm_args *argp = rqstp->rq_argp;
  393. struct nlm_host *host;
  394. /* Obtain client */
  395. if (nlmsvc_retrieve_args(rqstp, argp, &host, NULL))
  396. return rpc_success;
  397. nlmsvc_free_host_resources(host);
  398. nlmsvc_release_host(host);
  399. return rpc_success;
  400. }
  401. /*
  402. * SM_NOTIFY: private callback from statd (not part of official NLM proto)
  403. */
  404. static __be32
  405. nlmsvc_proc_sm_notify(struct svc_rqst *rqstp)
  406. {
  407. struct nlm_reboot *argp = rqstp->rq_argp;
  408. dprintk("lockd: SM_NOTIFY called\n");
  409. if (!nlm_privileged_requester(rqstp)) {
  410. char buf[RPC_MAX_ADDRBUFLEN];
  411. printk(KERN_WARNING "lockd: rejected NSM callback from %s\n",
  412. svc_print_addr(rqstp, buf, sizeof(buf)));
  413. return rpc_system_err;
  414. }
  415. nlm_host_rebooted(SVC_NET(rqstp), argp);
  416. return rpc_success;
  417. }
  418. /*
  419. * client sent a GRANTED_RES, let's remove the associated block
  420. */
  421. static __be32
  422. nlmsvc_proc_granted_res(struct svc_rqst *rqstp)
  423. {
  424. struct nlm_res *argp = rqstp->rq_argp;
  425. if (!nlmsvc_ops)
  426. return rpc_success;
  427. dprintk("lockd: GRANTED_RES called\n");
  428. nlmsvc_grant_reply(&argp->cookie, argp->status);
  429. return rpc_success;
  430. }
  431. /*
  432. * NLM Server procedures.
  433. */
  434. #define nlmsvc_encode_norep nlmsvc_encode_void
  435. #define nlmsvc_decode_norep nlmsvc_decode_void
  436. #define nlmsvc_decode_testres nlmsvc_decode_void
  437. #define nlmsvc_decode_lockres nlmsvc_decode_void
  438. #define nlmsvc_decode_unlockres nlmsvc_decode_void
  439. #define nlmsvc_decode_cancelres nlmsvc_decode_void
  440. #define nlmsvc_decode_grantedres nlmsvc_decode_void
  441. #define nlmsvc_proc_none nlmsvc_proc_null
  442. #define nlmsvc_proc_test_res nlmsvc_proc_null
  443. #define nlmsvc_proc_lock_res nlmsvc_proc_null
  444. #define nlmsvc_proc_cancel_res nlmsvc_proc_null
  445. #define nlmsvc_proc_unlock_res nlmsvc_proc_null
  446. struct nlm_void { int dummy; };
  447. #define PROC(name, xargt, xrest, argt, rest, respsize) \
  448. { .pc_func = nlmsvc_proc_##name, \
  449. .pc_decode = nlmsvc_decode_##xargt, \
  450. .pc_encode = nlmsvc_encode_##xrest, \
  451. .pc_release = NULL, \
  452. .pc_argsize = sizeof(struct nlm_##argt), \
  453. .pc_ressize = sizeof(struct nlm_##rest), \
  454. .pc_xdrressize = respsize, \
  455. }
  456. #define Ck (1+XDR_QUADLEN(NLM_MAXCOOKIELEN)) /* cookie */
  457. #define St 1 /* status */
  458. #define No (1+1024/4) /* Net Obj */
  459. #define Rg 2 /* range - offset + size */
  460. const struct svc_procedure nlmsvc_procedures[] = {
  461. PROC(null, void, void, void, void, 1),
  462. PROC(test, testargs, testres, args, res, Ck+St+2+No+Rg),
  463. PROC(lock, lockargs, res, args, res, Ck+St),
  464. PROC(cancel, cancargs, res, args, res, Ck+St),
  465. PROC(unlock, unlockargs, res, args, res, Ck+St),
  466. PROC(granted, testargs, res, args, res, Ck+St),
  467. PROC(test_msg, testargs, norep, args, void, 1),
  468. PROC(lock_msg, lockargs, norep, args, void, 1),
  469. PROC(cancel_msg, cancargs, norep, args, void, 1),
  470. PROC(unlock_msg, unlockargs, norep, args, void, 1),
  471. PROC(granted_msg, testargs, norep, args, void, 1),
  472. PROC(test_res, testres, norep, res, void, 1),
  473. PROC(lock_res, lockres, norep, res, void, 1),
  474. PROC(cancel_res, cancelres, norep, res, void, 1),
  475. PROC(unlock_res, unlockres, norep, res, void, 1),
  476. PROC(granted_res, res, norep, res, void, 1),
  477. /* statd callback */
  478. PROC(sm_notify, reboot, void, reboot, void, 1),
  479. PROC(none, void, void, void, void, 1),
  480. PROC(none, void, void, void, void, 1),
  481. PROC(none, void, void, void, void, 1),
  482. PROC(share, shareargs, shareres, args, res, Ck+St+1),
  483. PROC(unshare, shareargs, shareres, args, res, Ck+St+1),
  484. PROC(nm_lock, lockargs, res, args, res, Ck+St),
  485. PROC(free_all, notify, void, args, void, 0),
  486. };