svcproc.c 14 KB

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