svcproc.c 15 KB

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