xdr.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * linux/fs/lockd/xdr.c
  3. *
  4. * XDR support for lockd and the lock client.
  5. *
  6. * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
  7. */
  8. #include <linux/types.h>
  9. #include <linux/sched.h>
  10. #include <linux/nfs.h>
  11. #include <linux/sunrpc/xdr.h>
  12. #include <linux/sunrpc/clnt.h>
  13. #include <linux/sunrpc/svc.h>
  14. #include <linux/sunrpc/stats.h>
  15. #include <linux/lockd/lockd.h>
  16. #define NLMDBG_FACILITY NLMDBG_XDR
  17. static inline loff_t
  18. s32_to_loff_t(__s32 offset)
  19. {
  20. return (loff_t)offset;
  21. }
  22. static inline __s32
  23. loff_t_to_s32(loff_t offset)
  24. {
  25. __s32 res;
  26. if (offset >= NLM_OFFSET_MAX)
  27. res = NLM_OFFSET_MAX;
  28. else if (offset <= -NLM_OFFSET_MAX)
  29. res = -NLM_OFFSET_MAX;
  30. else
  31. res = offset;
  32. return res;
  33. }
  34. /*
  35. * XDR functions for basic NLM types
  36. */
  37. static __be32 *nlm_decode_cookie(__be32 *p, struct nlm_cookie *c)
  38. {
  39. unsigned int len;
  40. len = ntohl(*p++);
  41. if(len==0)
  42. {
  43. c->len=4;
  44. memset(c->data, 0, 4); /* hockeypux brain damage */
  45. }
  46. else if(len<=NLM_MAXCOOKIELEN)
  47. {
  48. c->len=len;
  49. memcpy(c->data, p, len);
  50. p+=XDR_QUADLEN(len);
  51. }
  52. else
  53. {
  54. dprintk("lockd: bad cookie size %d (only cookies under "
  55. "%d bytes are supported.)\n",
  56. len, NLM_MAXCOOKIELEN);
  57. return NULL;
  58. }
  59. return p;
  60. }
  61. static inline __be32 *
  62. nlm_encode_cookie(__be32 *p, struct nlm_cookie *c)
  63. {
  64. *p++ = htonl(c->len);
  65. memcpy(p, c->data, c->len);
  66. p+=XDR_QUADLEN(c->len);
  67. return p;
  68. }
  69. static __be32 *
  70. nlm_decode_fh(__be32 *p, struct nfs_fh *f)
  71. {
  72. unsigned int len;
  73. if ((len = ntohl(*p++)) != NFS2_FHSIZE) {
  74. dprintk("lockd: bad fhandle size %d (should be %d)\n",
  75. len, NFS2_FHSIZE);
  76. return NULL;
  77. }
  78. f->size = NFS2_FHSIZE;
  79. memset(f->data, 0, sizeof(f->data));
  80. memcpy(f->data, p, NFS2_FHSIZE);
  81. return p + XDR_QUADLEN(NFS2_FHSIZE);
  82. }
  83. static inline __be32 *
  84. nlm_encode_fh(__be32 *p, struct nfs_fh *f)
  85. {
  86. *p++ = htonl(NFS2_FHSIZE);
  87. memcpy(p, f->data, NFS2_FHSIZE);
  88. return p + XDR_QUADLEN(NFS2_FHSIZE);
  89. }
  90. /*
  91. * Encode and decode owner handle
  92. */
  93. static inline __be32 *
  94. nlm_decode_oh(__be32 *p, struct xdr_netobj *oh)
  95. {
  96. return xdr_decode_netobj(p, oh);
  97. }
  98. static inline __be32 *
  99. nlm_encode_oh(__be32 *p, struct xdr_netobj *oh)
  100. {
  101. return xdr_encode_netobj(p, oh);
  102. }
  103. static __be32 *
  104. nlm_decode_lock(__be32 *p, struct nlm_lock *lock)
  105. {
  106. struct file_lock *fl = &lock->fl;
  107. s32 start, len, end;
  108. if (!(p = xdr_decode_string_inplace(p, &lock->caller,
  109. &lock->len,
  110. NLM_MAXSTRLEN))
  111. || !(p = nlm_decode_fh(p, &lock->fh))
  112. || !(p = nlm_decode_oh(p, &lock->oh)))
  113. return NULL;
  114. lock->svid = ntohl(*p++);
  115. locks_init_lock(fl);
  116. fl->fl_owner = current->files;
  117. fl->fl_pid = (pid_t)lock->svid;
  118. fl->fl_flags = FL_POSIX;
  119. fl->fl_type = F_RDLCK; /* as good as anything else */
  120. start = ntohl(*p++);
  121. len = ntohl(*p++);
  122. end = start + len - 1;
  123. fl->fl_start = s32_to_loff_t(start);
  124. if (len == 0 || end < 0)
  125. fl->fl_end = OFFSET_MAX;
  126. else
  127. fl->fl_end = s32_to_loff_t(end);
  128. return p;
  129. }
  130. /*
  131. * Encode result of a TEST/TEST_MSG call
  132. */
  133. static __be32 *
  134. nlm_encode_testres(__be32 *p, struct nlm_res *resp)
  135. {
  136. s32 start, len;
  137. if (!(p = nlm_encode_cookie(p, &resp->cookie)))
  138. return NULL;
  139. *p++ = resp->status;
  140. if (resp->status == nlm_lck_denied) {
  141. struct file_lock *fl = &resp->lock.fl;
  142. *p++ = (fl->fl_type == F_RDLCK)? xdr_zero : xdr_one;
  143. *p++ = htonl(resp->lock.svid);
  144. /* Encode owner handle. */
  145. if (!(p = xdr_encode_netobj(p, &resp->lock.oh)))
  146. return NULL;
  147. start = loff_t_to_s32(fl->fl_start);
  148. if (fl->fl_end == OFFSET_MAX)
  149. len = 0;
  150. else
  151. len = loff_t_to_s32(fl->fl_end - fl->fl_start + 1);
  152. *p++ = htonl(start);
  153. *p++ = htonl(len);
  154. }
  155. return p;
  156. }
  157. /*
  158. * First, the server side XDR functions
  159. */
  160. int
  161. nlmsvc_decode_testargs(struct svc_rqst *rqstp, __be32 *p, nlm_args *argp)
  162. {
  163. u32 exclusive;
  164. if (!(p = nlm_decode_cookie(p, &argp->cookie)))
  165. return 0;
  166. exclusive = ntohl(*p++);
  167. if (!(p = nlm_decode_lock(p, &argp->lock)))
  168. return 0;
  169. if (exclusive)
  170. argp->lock.fl.fl_type = F_WRLCK;
  171. return xdr_argsize_check(rqstp, p);
  172. }
  173. int
  174. nlmsvc_encode_testres(struct svc_rqst *rqstp, __be32 *p, struct nlm_res *resp)
  175. {
  176. if (!(p = nlm_encode_testres(p, resp)))
  177. return 0;
  178. return xdr_ressize_check(rqstp, p);
  179. }
  180. int
  181. nlmsvc_decode_lockargs(struct svc_rqst *rqstp, __be32 *p, nlm_args *argp)
  182. {
  183. u32 exclusive;
  184. if (!(p = nlm_decode_cookie(p, &argp->cookie)))
  185. return 0;
  186. argp->block = ntohl(*p++);
  187. exclusive = ntohl(*p++);
  188. if (!(p = nlm_decode_lock(p, &argp->lock)))
  189. return 0;
  190. if (exclusive)
  191. argp->lock.fl.fl_type = F_WRLCK;
  192. argp->reclaim = ntohl(*p++);
  193. argp->state = ntohl(*p++);
  194. argp->monitor = 1; /* monitor client by default */
  195. return xdr_argsize_check(rqstp, p);
  196. }
  197. int
  198. nlmsvc_decode_cancargs(struct svc_rqst *rqstp, __be32 *p, nlm_args *argp)
  199. {
  200. u32 exclusive;
  201. if (!(p = nlm_decode_cookie(p, &argp->cookie)))
  202. return 0;
  203. argp->block = ntohl(*p++);
  204. exclusive = ntohl(*p++);
  205. if (!(p = nlm_decode_lock(p, &argp->lock)))
  206. return 0;
  207. if (exclusive)
  208. argp->lock.fl.fl_type = F_WRLCK;
  209. return xdr_argsize_check(rqstp, p);
  210. }
  211. int
  212. nlmsvc_decode_unlockargs(struct svc_rqst *rqstp, __be32 *p, nlm_args *argp)
  213. {
  214. if (!(p = nlm_decode_cookie(p, &argp->cookie))
  215. || !(p = nlm_decode_lock(p, &argp->lock)))
  216. return 0;
  217. argp->lock.fl.fl_type = F_UNLCK;
  218. return xdr_argsize_check(rqstp, p);
  219. }
  220. int
  221. nlmsvc_decode_shareargs(struct svc_rqst *rqstp, __be32 *p, nlm_args *argp)
  222. {
  223. struct nlm_lock *lock = &argp->lock;
  224. memset(lock, 0, sizeof(*lock));
  225. locks_init_lock(&lock->fl);
  226. lock->svid = ~(u32) 0;
  227. lock->fl.fl_pid = (pid_t)lock->svid;
  228. if (!(p = nlm_decode_cookie(p, &argp->cookie))
  229. || !(p = xdr_decode_string_inplace(p, &lock->caller,
  230. &lock->len, NLM_MAXSTRLEN))
  231. || !(p = nlm_decode_fh(p, &lock->fh))
  232. || !(p = nlm_decode_oh(p, &lock->oh)))
  233. return 0;
  234. argp->fsm_mode = ntohl(*p++);
  235. argp->fsm_access = ntohl(*p++);
  236. return xdr_argsize_check(rqstp, p);
  237. }
  238. int
  239. nlmsvc_encode_shareres(struct svc_rqst *rqstp, __be32 *p, struct nlm_res *resp)
  240. {
  241. if (!(p = nlm_encode_cookie(p, &resp->cookie)))
  242. return 0;
  243. *p++ = resp->status;
  244. *p++ = xdr_zero; /* sequence argument */
  245. return xdr_ressize_check(rqstp, p);
  246. }
  247. int
  248. nlmsvc_encode_res(struct svc_rqst *rqstp, __be32 *p, struct nlm_res *resp)
  249. {
  250. if (!(p = nlm_encode_cookie(p, &resp->cookie)))
  251. return 0;
  252. *p++ = resp->status;
  253. return xdr_ressize_check(rqstp, p);
  254. }
  255. int
  256. nlmsvc_decode_notify(struct svc_rqst *rqstp, __be32 *p, struct nlm_args *argp)
  257. {
  258. struct nlm_lock *lock = &argp->lock;
  259. if (!(p = xdr_decode_string_inplace(p, &lock->caller,
  260. &lock->len, NLM_MAXSTRLEN)))
  261. return 0;
  262. argp->state = ntohl(*p++);
  263. return xdr_argsize_check(rqstp, p);
  264. }
  265. int
  266. nlmsvc_decode_reboot(struct svc_rqst *rqstp, __be32 *p, struct nlm_reboot *argp)
  267. {
  268. if (!(p = xdr_decode_string_inplace(p, &argp->mon, &argp->len, SM_MAXSTRLEN)))
  269. return 0;
  270. argp->state = ntohl(*p++);
  271. memcpy(&argp->priv.data, p, sizeof(argp->priv.data));
  272. p += XDR_QUADLEN(SM_PRIV_SIZE);
  273. return xdr_argsize_check(rqstp, p);
  274. }
  275. int
  276. nlmsvc_decode_res(struct svc_rqst *rqstp, __be32 *p, struct nlm_res *resp)
  277. {
  278. if (!(p = nlm_decode_cookie(p, &resp->cookie)))
  279. return 0;
  280. resp->status = *p++;
  281. return xdr_argsize_check(rqstp, p);
  282. }
  283. int
  284. nlmsvc_decode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
  285. {
  286. return xdr_argsize_check(rqstp, p);
  287. }
  288. int
  289. nlmsvc_encode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
  290. {
  291. return xdr_ressize_check(rqstp, p);
  292. }