xdr.c 7.2 KB

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