svcproc.c 16 KB

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