svcproc.c 14 KB

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