svcproc.c 15 KB

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