xdr4.c 7.5 KB

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