svcproc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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/smp_lock.h>
  12. #include <linux/lockd/lockd.h>
  13. #include <linux/lockd/share.h>
  14. #define NLMDBG_FACILITY NLMDBG_CLIENT
  15. #ifdef CONFIG_LOCKD_V4
  16. static __be32
  17. cast_to_nlm(__be32 status, u32 vers)
  18. {
  19. /* Note: status is assumed to be in network byte order !!! */
  20. if (vers != 4){
  21. switch (status) {
  22. case nlm_granted:
  23. case nlm_lck_denied:
  24. case nlm_lck_denied_nolocks:
  25. case nlm_lck_blocked:
  26. case nlm_lck_denied_grace_period:
  27. case nlm_drop_reply:
  28. break;
  29. case nlm4_deadlock:
  30. status = nlm_lck_denied;
  31. break;
  32. default:
  33. status = nlm_lck_denied_nolocks;
  34. }
  35. }
  36. return (status);
  37. }
  38. #define cast_status(status) (cast_to_nlm(status, rqstp->rq_vers))
  39. #else
  40. #define cast_status(status) (status)
  41. #endif
  42. /*
  43. * Obtain client and file from arguments
  44. */
  45. static __be32
  46. nlmsvc_retrieve_args(struct svc_rqst *rqstp, struct nlm_args *argp,
  47. struct nlm_host **hostp, struct nlm_file **filp)
  48. {
  49. struct nlm_host *host = NULL;
  50. struct nlm_file *file = NULL;
  51. struct nlm_lock *lock = &argp->lock;
  52. __be32 error = 0;
  53. /* nfsd callbacks must have been installed for this procedure */
  54. if (!nlmsvc_ops)
  55. return nlm_lck_denied_nolocks;
  56. /* Obtain host handle */
  57. if (!(host = nlmsvc_lookup_host(rqstp, lock->caller, lock->len))
  58. || (argp->monitor && nsm_monitor(host) < 0))
  59. goto no_locks;
  60. *hostp = host;
  61. /* Obtain file pointer. Not used by FREE_ALL call. */
  62. if (filp != NULL) {
  63. if ((error = nlm_lookup_file(rqstp, &file, &lock->fh)) != 0)
  64. goto no_locks;
  65. *filp = file;
  66. /* Set up the missing parts of the file_lock structure */
  67. lock->fl.fl_file = file->f_file;
  68. lock->fl.fl_owner = (fl_owner_t) host;
  69. lock->fl.fl_lmops = &nlmsvc_lock_operations;
  70. }
  71. return 0;
  72. no_locks:
  73. nlm_release_host(host);
  74. if (error)
  75. return error;
  76. return nlm_lck_denied_nolocks;
  77. }
  78. /*
  79. * NULL: Test for presence of service
  80. */
  81. static __be32
  82. nlmsvc_proc_null(struct svc_rqst *rqstp, void *argp, void *resp)
  83. {
  84. dprintk("lockd: NULL called\n");
  85. return rpc_success;
  86. }
  87. /*
  88. * TEST: Check for conflicting lock
  89. */
  90. static __be32
  91. nlmsvc_proc_test(struct svc_rqst *rqstp, struct nlm_args *argp,
  92. struct nlm_res *resp)
  93. {
  94. struct nlm_host *host;
  95. struct nlm_file *file;
  96. __be32 rc = rpc_success;
  97. dprintk("lockd: TEST called\n");
  98. resp->cookie = argp->cookie;
  99. /* Obtain client and file */
  100. if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
  101. return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
  102. /* Now check for conflicting locks */
  103. resp->status = cast_status(nlmsvc_testlock(rqstp, file, host, &argp->lock, &resp->lock, &resp->cookie));
  104. if (resp->status == nlm_drop_reply)
  105. rc = rpc_drop_reply;
  106. else
  107. dprintk("lockd: TEST status %d vers %d\n",
  108. ntohl(resp->status), rqstp->rq_vers);
  109. nlm_release_host(host);
  110. nlm_release_file(file);
  111. return rc;
  112. }
  113. static __be32
  114. nlmsvc_proc_lock(struct svc_rqst *rqstp, struct nlm_args *argp,
  115. struct nlm_res *resp)
  116. {
  117. struct nlm_host *host;
  118. struct nlm_file *file;
  119. __be32 rc = rpc_success;
  120. dprintk("lockd: LOCK called\n");
  121. resp->cookie = argp->cookie;
  122. /* Obtain client and file */
  123. if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
  124. return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
  125. #if 0
  126. /* If supplied state doesn't match current state, we assume it's
  127. * an old request that time-warped somehow. Any error return would
  128. * do in this case because it's irrelevant anyway.
  129. *
  130. * NB: We don't retrieve the remote host's state yet.
  131. */
  132. if (host->h_nsmstate && host->h_nsmstate != argp->state) {
  133. resp->status = nlm_lck_denied_nolocks;
  134. } else
  135. #endif
  136. /* Now try to lock the file */
  137. resp->status = cast_status(nlmsvc_lock(rqstp, file, host, &argp->lock,
  138. argp->block, &argp->cookie,
  139. argp->reclaim));
  140. if (resp->status == nlm_drop_reply)
  141. rc = rpc_drop_reply;
  142. else
  143. dprintk("lockd: LOCK status %d\n", ntohl(resp->status));
  144. nlm_release_host(host);
  145. nlm_release_file(file);
  146. return rc;
  147. }
  148. static __be32
  149. nlmsvc_proc_cancel(struct svc_rqst *rqstp, struct nlm_args *argp,
  150. struct nlm_res *resp)
  151. {
  152. struct nlm_host *host;
  153. struct nlm_file *file;
  154. dprintk("lockd: CANCEL called\n");
  155. resp->cookie = argp->cookie;
  156. /* Don't accept requests during grace period */
  157. if (locks_in_grace()) {
  158. resp->status = nlm_lck_denied_grace_period;
  159. return rpc_success;
  160. }
  161. /* Obtain client and file */
  162. if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
  163. return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
  164. /* Try to cancel request. */
  165. resp->status = cast_status(nlmsvc_cancel_blocked(file, &argp->lock));
  166. dprintk("lockd: CANCEL status %d\n", ntohl(resp->status));
  167. nlm_release_host(host);
  168. nlm_release_file(file);
  169. return rpc_success;
  170. }
  171. /*
  172. * UNLOCK: release a lock
  173. */
  174. static __be32
  175. nlmsvc_proc_unlock(struct svc_rqst *rqstp, struct nlm_args *argp,
  176. struct nlm_res *resp)
  177. {
  178. struct nlm_host *host;
  179. struct nlm_file *file;
  180. dprintk("lockd: UNLOCK called\n");
  181. resp->cookie = argp->cookie;
  182. /* Don't accept new lock requests during grace period */
  183. if (locks_in_grace()) {
  184. resp->status = nlm_lck_denied_grace_period;
  185. return rpc_success;
  186. }
  187. /* Obtain client and file */
  188. if ((resp->status = nlmsvc_retrieve_args(rqstp, argp, &host, &file)))
  189. return resp->status == nlm_drop_reply ? rpc_drop_reply :rpc_success;
  190. /* Now try to remove the lock */
  191. resp->status = cast_status(nlmsvc_unlock(file, &argp->lock));
  192. dprintk("lockd: UNLOCK status %d\n", ntohl(resp->status));
  193. nlm_release_host(host);
  194. nlm_release_file(file);
  195. return rpc_success;
  196. }
  197. /*
  198. * GRANTED: A server calls us to tell that a process' lock request
  199. * was granted
  200. */
  201. static __be32
  202. nlmsvc_proc_granted(struct svc_rqst *rqstp, struct nlm_args *argp,
  203. struct nlm_res *resp)
  204. {
  205. resp->cookie = argp->cookie;
  206. dprintk("lockd: GRANTED called\n");
  207. resp->status = nlmclnt_grant(svc_addr(rqstp), &argp->lock);
  208. dprintk("lockd: GRANTED status %d\n", ntohl(resp->status));
  209. return rpc_success;
  210. }
  211. /*
  212. * This is the generic lockd callback for async RPC calls
  213. */
  214. static void nlmsvc_callback_exit(struct rpc_task *task, void *data)
  215. {
  216. dprintk("lockd: %5u callback returned %d\n", task->tk_pid,
  217. -task->tk_status);
  218. }
  219. static void nlmsvc_callback_release(void *data)
  220. {
  221. lock_kernel();
  222. nlm_release_call(data);
  223. unlock_kernel();
  224. }
  225. static const struct rpc_call_ops nlmsvc_callback_ops = {
  226. .rpc_call_done = nlmsvc_callback_exit,
  227. .rpc_release = nlmsvc_callback_release,
  228. };
  229. /*
  230. * `Async' versions of the above service routines. They aren't really,
  231. * because we send the callback before the reply proper. I hope this
  232. * doesn't break any clients.
  233. */
  234. static __be32 nlmsvc_callback(struct svc_rqst *rqstp, u32 proc, struct nlm_args *argp,
  235. __be32 (*func)(struct svc_rqst *, struct nlm_args *, struct nlm_res *))
  236. {
  237. struct nlm_host *host;
  238. struct nlm_rqst *call;
  239. __be32 stat;
  240. host = nlmsvc_lookup_host(rqstp,
  241. argp->lock.caller,
  242. argp->lock.len);
  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 __be32 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 __be32 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 __be32 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 __be32
  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 __be32
  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 __be32
  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 (locks_in_grace() && !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 resp->status == nlm_drop_reply ? rpc_drop_reply :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 __be32
  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 (locks_in_grace()) {
  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 resp->status == nlm_drop_reply ? rpc_drop_reply :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 __be32
  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 __be32
  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 __be32
  372. nlmsvc_proc_sm_notify(struct svc_rqst *rqstp, struct nlm_reboot *argp,
  373. void *resp)
  374. {
  375. dprintk("lockd: SM_NOTIFY called\n");
  376. if (!nlm_privileged_requester(rqstp)) {
  377. char buf[RPC_MAX_ADDRBUFLEN];
  378. printk(KERN_WARNING "lockd: rejected NSM callback from %s\n",
  379. svc_print_addr(rqstp, buf, sizeof(buf)));
  380. return rpc_system_err;
  381. }
  382. nlm_host_rebooted(argp);
  383. return rpc_success;
  384. }
  385. /*
  386. * client sent a GRANTED_RES, let's remove the associated block
  387. */
  388. static __be32
  389. nlmsvc_proc_granted_res(struct svc_rqst *rqstp, struct nlm_res *argp,
  390. void *resp)
  391. {
  392. if (!nlmsvc_ops)
  393. return rpc_success;
  394. dprintk("lockd: GRANTED_RES called\n");
  395. nlmsvc_grant_reply(&argp->cookie, argp->status);
  396. return rpc_success;
  397. }
  398. /*
  399. * NLM Server procedures.
  400. */
  401. #define nlmsvc_encode_norep nlmsvc_encode_void
  402. #define nlmsvc_decode_norep nlmsvc_decode_void
  403. #define nlmsvc_decode_testres nlmsvc_decode_void
  404. #define nlmsvc_decode_lockres nlmsvc_decode_void
  405. #define nlmsvc_decode_unlockres nlmsvc_decode_void
  406. #define nlmsvc_decode_cancelres nlmsvc_decode_void
  407. #define nlmsvc_decode_grantedres nlmsvc_decode_void
  408. #define nlmsvc_proc_none nlmsvc_proc_null
  409. #define nlmsvc_proc_test_res nlmsvc_proc_null
  410. #define nlmsvc_proc_lock_res nlmsvc_proc_null
  411. #define nlmsvc_proc_cancel_res nlmsvc_proc_null
  412. #define nlmsvc_proc_unlock_res nlmsvc_proc_null
  413. struct nlm_void { int dummy; };
  414. #define PROC(name, xargt, xrest, argt, rest, respsize) \
  415. { .pc_func = (svc_procfunc) nlmsvc_proc_##name, \
  416. .pc_decode = (kxdrproc_t) nlmsvc_decode_##xargt, \
  417. .pc_encode = (kxdrproc_t) nlmsvc_encode_##xrest, \
  418. .pc_release = NULL, \
  419. .pc_argsize = sizeof(struct nlm_##argt), \
  420. .pc_ressize = sizeof(struct nlm_##rest), \
  421. .pc_xdrressize = respsize, \
  422. }
  423. #define Ck (1+XDR_QUADLEN(NLM_MAXCOOKIELEN)) /* cookie */
  424. #define St 1 /* status */
  425. #define No (1+1024/4) /* Net Obj */
  426. #define Rg 2 /* range - offset + size */
  427. struct svc_procedure nlmsvc_procedures[] = {
  428. PROC(null, void, void, void, void, 1),
  429. PROC(test, testargs, testres, args, res, Ck+St+2+No+Rg),
  430. PROC(lock, lockargs, res, args, res, Ck+St),
  431. PROC(cancel, cancargs, res, args, res, Ck+St),
  432. PROC(unlock, unlockargs, res, args, res, Ck+St),
  433. PROC(granted, testargs, res, args, res, Ck+St),
  434. PROC(test_msg, testargs, norep, args, void, 1),
  435. PROC(lock_msg, lockargs, norep, args, void, 1),
  436. PROC(cancel_msg, cancargs, norep, args, void, 1),
  437. PROC(unlock_msg, unlockargs, norep, args, void, 1),
  438. PROC(granted_msg, testargs, norep, args, void, 1),
  439. PROC(test_res, testres, norep, res, void, 1),
  440. PROC(lock_res, lockres, norep, res, void, 1),
  441. PROC(cancel_res, cancelres, norep, res, void, 1),
  442. PROC(unlock_res, unlockres, norep, res, void, 1),
  443. PROC(granted_res, res, norep, res, void, 1),
  444. /* statd callback */
  445. PROC(sm_notify, reboot, void, reboot, void, 1),
  446. PROC(none, void, void, void, void, 1),
  447. PROC(none, void, void, void, void, 1),
  448. PROC(none, void, void, void, void, 1),
  449. PROC(share, shareargs, shareres, args, res, Ck+St+1),
  450. PROC(unshare, shareargs, shareres, args, res, Ck+St+1),
  451. PROC(nm_lock, lockargs, res, args, res, Ck+St),
  452. PROC(free_all, notify, void, args, void, 0),
  453. };