svcproc.c 15 KB

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