svc4proc.c 15 KB

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