svc4proc.c 15 KB

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