svcproc.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:15k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

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