svcproc.c 15 KB

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