svcproc.c 15 KB

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