svc4proc.c 15 KB

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