xdr.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. /*
  85. * Encode and decode owner handle
  86. */
  87. static inline __be32 *
  88. nlm_decode_oh(__be32 *p, struct xdr_netobj *oh)
  89. {
  90. return xdr_decode_netobj(p, oh);
  91. }
  92. static inline __be32 *
  93. nlm_encode_oh(__be32 *p, struct xdr_netobj *oh)
  94. {
  95. return xdr_encode_netobj(p, oh);
  96. }
  97. static __be32 *
  98. nlm_decode_lock(__be32 *p, struct nlm_lock *lock)
  99. {
  100. struct file_lock *fl = &lock->fl;
  101. s32 start, len, end;
  102. if (!(p = xdr_decode_string_inplace(p, &lock->caller,
  103. &lock->len,
  104. NLM_MAXSTRLEN))
  105. || !(p = nlm_decode_fh(p, &lock->fh))
  106. || !(p = nlm_decode_oh(p, &lock->oh)))
  107. return NULL;
  108. lock->svid = ntohl(*p++);
  109. locks_init_lock(fl);
  110. fl->fl_owner = current->files;
  111. fl->fl_pid = (pid_t)lock->svid;
  112. fl->fl_flags = FL_POSIX;
  113. fl->fl_type = F_RDLCK; /* as good as anything else */
  114. start = ntohl(*p++);
  115. len = ntohl(*p++);
  116. end = start + len - 1;
  117. fl->fl_start = s32_to_loff_t(start);
  118. if (len == 0 || end < 0)
  119. fl->fl_end = OFFSET_MAX;
  120. else
  121. fl->fl_end = s32_to_loff_t(end);
  122. return p;
  123. }
  124. /*
  125. * Encode result of a TEST/TEST_MSG call
  126. */
  127. static __be32 *
  128. nlm_encode_testres(__be32 *p, struct nlm_res *resp)
  129. {
  130. s32 start, len;
  131. if (!(p = nlm_encode_cookie(p, &resp->cookie)))
  132. return NULL;
  133. *p++ = resp->status;
  134. if (resp->status == nlm_lck_denied) {
  135. struct file_lock *fl = &resp->lock.fl;
  136. *p++ = (fl->fl_type == F_RDLCK)? xdr_zero : xdr_one;
  137. *p++ = htonl(resp->lock.svid);
  138. /* Encode owner handle. */
  139. if (!(p = xdr_encode_netobj(p, &resp->lock.oh)))
  140. return NULL;
  141. start = loff_t_to_s32(fl->fl_start);
  142. if (fl->fl_end == OFFSET_MAX)
  143. len = 0;
  144. else
  145. len = loff_t_to_s32(fl->fl_end - fl->fl_start + 1);
  146. *p++ = htonl(start);
  147. *p++ = htonl(len);
  148. }
  149. return p;
  150. }
  151. /*
  152. * First, the server side XDR functions
  153. */
  154. int
  155. nlmsvc_decode_testargs(struct svc_rqst *rqstp, __be32 *p, nlm_args *argp)
  156. {
  157. u32 exclusive;
  158. if (!(p = nlm_decode_cookie(p, &argp->cookie)))
  159. return 0;
  160. exclusive = ntohl(*p++);
  161. if (!(p = nlm_decode_lock(p, &argp->lock)))
  162. return 0;
  163. if (exclusive)
  164. argp->lock.fl.fl_type = F_WRLCK;
  165. return xdr_argsize_check(rqstp, p);
  166. }
  167. int
  168. nlmsvc_encode_testres(struct svc_rqst *rqstp, __be32 *p, struct nlm_res *resp)
  169. {
  170. if (!(p = nlm_encode_testres(p, resp)))
  171. return 0;
  172. return xdr_ressize_check(rqstp, p);
  173. }
  174. int
  175. nlmsvc_decode_lockargs(struct svc_rqst *rqstp, __be32 *p, nlm_args *argp)
  176. {
  177. u32 exclusive;
  178. if (!(p = nlm_decode_cookie(p, &argp->cookie)))
  179. return 0;
  180. argp->block = ntohl(*p++);
  181. exclusive = ntohl(*p++);
  182. if (!(p = nlm_decode_lock(p, &argp->lock)))
  183. return 0;
  184. if (exclusive)
  185. argp->lock.fl.fl_type = F_WRLCK;
  186. argp->reclaim = ntohl(*p++);
  187. argp->state = ntohl(*p++);
  188. argp->monitor = 1; /* monitor client by default */
  189. return xdr_argsize_check(rqstp, p);
  190. }
  191. int
  192. nlmsvc_decode_cancargs(struct svc_rqst *rqstp, __be32 *p, nlm_args *argp)
  193. {
  194. u32 exclusive;
  195. if (!(p = nlm_decode_cookie(p, &argp->cookie)))
  196. return 0;
  197. argp->block = ntohl(*p++);
  198. exclusive = ntohl(*p++);
  199. if (!(p = nlm_decode_lock(p, &argp->lock)))
  200. return 0;
  201. if (exclusive)
  202. argp->lock.fl.fl_type = F_WRLCK;
  203. return xdr_argsize_check(rqstp, p);
  204. }
  205. int
  206. nlmsvc_decode_unlockargs(struct svc_rqst *rqstp, __be32 *p, nlm_args *argp)
  207. {
  208. if (!(p = nlm_decode_cookie(p, &argp->cookie))
  209. || !(p = nlm_decode_lock(p, &argp->lock)))
  210. return 0;
  211. argp->lock.fl.fl_type = F_UNLCK;
  212. return xdr_argsize_check(rqstp, p);
  213. }
  214. int
  215. nlmsvc_decode_shareargs(struct svc_rqst *rqstp, __be32 *p, nlm_args *argp)
  216. {
  217. struct nlm_lock *lock = &argp->lock;
  218. memset(lock, 0, sizeof(*lock));
  219. locks_init_lock(&lock->fl);
  220. lock->svid = ~(u32) 0;
  221. lock->fl.fl_pid = (pid_t)lock->svid;
  222. if (!(p = nlm_decode_cookie(p, &argp->cookie))
  223. || !(p = xdr_decode_string_inplace(p, &lock->caller,
  224. &lock->len, NLM_MAXSTRLEN))
  225. || !(p = nlm_decode_fh(p, &lock->fh))
  226. || !(p = nlm_decode_oh(p, &lock->oh)))
  227. return 0;
  228. argp->fsm_mode = ntohl(*p++);
  229. argp->fsm_access = ntohl(*p++);
  230. return xdr_argsize_check(rqstp, p);
  231. }
  232. int
  233. nlmsvc_encode_shareres(struct svc_rqst *rqstp, __be32 *p, struct nlm_res *resp)
  234. {
  235. if (!(p = nlm_encode_cookie(p, &resp->cookie)))
  236. return 0;
  237. *p++ = resp->status;
  238. *p++ = xdr_zero; /* sequence argument */
  239. return xdr_ressize_check(rqstp, p);
  240. }
  241. int
  242. nlmsvc_encode_res(struct svc_rqst *rqstp, __be32 *p, struct nlm_res *resp)
  243. {
  244. if (!(p = nlm_encode_cookie(p, &resp->cookie)))
  245. return 0;
  246. *p++ = resp->status;
  247. return xdr_ressize_check(rqstp, p);
  248. }
  249. int
  250. nlmsvc_decode_notify(struct svc_rqst *rqstp, __be32 *p, struct nlm_args *argp)
  251. {
  252. struct nlm_lock *lock = &argp->lock;
  253. if (!(p = xdr_decode_string_inplace(p, &lock->caller,
  254. &lock->len, NLM_MAXSTRLEN)))
  255. return 0;
  256. argp->state = ntohl(*p++);
  257. return xdr_argsize_check(rqstp, p);
  258. }
  259. int
  260. nlmsvc_decode_reboot(struct svc_rqst *rqstp, __be32 *p, struct nlm_reboot *argp)
  261. {
  262. if (!(p = xdr_decode_string_inplace(p, &argp->mon, &argp->len, SM_MAXSTRLEN)))
  263. return 0;
  264. argp->state = ntohl(*p++);
  265. memcpy(&argp->priv.data, p, sizeof(argp->priv.data));
  266. p += XDR_QUADLEN(SM_PRIV_SIZE);
  267. return xdr_argsize_check(rqstp, p);
  268. }
  269. int
  270. nlmsvc_decode_res(struct svc_rqst *rqstp, __be32 *p, struct nlm_res *resp)
  271. {
  272. if (!(p = nlm_decode_cookie(p, &resp->cookie)))
  273. return 0;
  274. resp->status = *p++;
  275. return xdr_argsize_check(rqstp, p);
  276. }
  277. int
  278. nlmsvc_decode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
  279. {
  280. return xdr_argsize_check(rqstp, p);
  281. }
  282. int
  283. nlmsvc_encode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
  284. {
  285. return xdr_ressize_check(rqstp, p);
  286. }