xdr4.c 7.5 KB

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