xdr4.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. #include "svcxdr.h"
  19. static inline s64
  20. loff_t_to_s64(loff_t offset)
  21. {
  22. s64 res;
  23. if (offset > NLM4_OFFSET_MAX)
  24. res = NLM4_OFFSET_MAX;
  25. else if (offset < -NLM4_OFFSET_MAX)
  26. res = -NLM4_OFFSET_MAX;
  27. else
  28. res = offset;
  29. return res;
  30. }
  31. void nlm4svc_set_file_lock_range(struct file_lock *fl, u64 off, u64 len)
  32. {
  33. s64 end = off + len - 1;
  34. fl->fl_start = off;
  35. if (len == 0 || end < 0)
  36. fl->fl_end = OFFSET_MAX;
  37. else
  38. fl->fl_end = end;
  39. }
  40. /*
  41. * NLM file handles are defined by specification to be a variable-length
  42. * XDR opaque no longer than 1024 bytes. However, this implementation
  43. * limits their length to the size of an NFSv3 file handle.
  44. */
  45. static bool
  46. svcxdr_decode_fhandle(struct xdr_stream *xdr, struct nfs_fh *fh)
  47. {
  48. __be32 *p;
  49. u32 len;
  50. if (xdr_stream_decode_u32(xdr, &len) < 0)
  51. return false;
  52. if (len > NFS_MAXFHSIZE)
  53. return false;
  54. p = xdr_inline_decode(xdr, len);
  55. if (!p)
  56. return false;
  57. fh->size = len;
  58. memcpy(fh->data, p, len);
  59. memset(fh->data + len, 0, sizeof(fh->data) - len);
  60. return true;
  61. }
  62. static bool
  63. svcxdr_decode_lock(struct xdr_stream *xdr, struct nlm_lock *lock)
  64. {
  65. struct file_lock *fl = &lock->fl;
  66. if (!svcxdr_decode_string(xdr, &lock->caller, &lock->len))
  67. return false;
  68. if (!svcxdr_decode_fhandle(xdr, &lock->fh))
  69. return false;
  70. if (!svcxdr_decode_owner(xdr, &lock->oh))
  71. return false;
  72. if (xdr_stream_decode_u32(xdr, &lock->svid) < 0)
  73. return false;
  74. if (xdr_stream_decode_u64(xdr, &lock->lock_start) < 0)
  75. return false;
  76. if (xdr_stream_decode_u64(xdr, &lock->lock_len) < 0)
  77. return false;
  78. locks_init_lock(fl);
  79. fl->c.flc_type = F_RDLCK;
  80. nlm4svc_set_file_lock_range(fl, lock->lock_start, lock->lock_len);
  81. return true;
  82. }
  83. static bool
  84. svcxdr_encode_holder(struct xdr_stream *xdr, const struct nlm_lock *lock)
  85. {
  86. const struct file_lock *fl = &lock->fl;
  87. s64 start, len;
  88. /* exclusive */
  89. if (xdr_stream_encode_bool(xdr, fl->c.flc_type != F_RDLCK) < 0)
  90. return false;
  91. if (xdr_stream_encode_u32(xdr, lock->svid) < 0)
  92. return false;
  93. if (!svcxdr_encode_owner(xdr, &lock->oh))
  94. return false;
  95. start = loff_t_to_s64(fl->fl_start);
  96. if (fl->fl_end == OFFSET_MAX)
  97. len = 0;
  98. else
  99. len = loff_t_to_s64(fl->fl_end - fl->fl_start + 1);
  100. if (xdr_stream_encode_u64(xdr, start) < 0)
  101. return false;
  102. if (xdr_stream_encode_u64(xdr, len) < 0)
  103. return false;
  104. return true;
  105. }
  106. static bool
  107. svcxdr_encode_testrply(struct xdr_stream *xdr, const struct nlm_res *resp)
  108. {
  109. if (!svcxdr_encode_stats(xdr, resp->status))
  110. return false;
  111. switch (resp->status) {
  112. case nlm_lck_denied:
  113. if (!svcxdr_encode_holder(xdr, &resp->lock))
  114. return false;
  115. }
  116. return true;
  117. }
  118. /*
  119. * Decode Call arguments
  120. */
  121. bool
  122. nlm4svc_decode_void(struct svc_rqst *rqstp, struct xdr_stream *xdr)
  123. {
  124. return true;
  125. }
  126. bool
  127. nlm4svc_decode_testargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
  128. {
  129. struct nlm_args *argp = rqstp->rq_argp;
  130. u32 exclusive;
  131. if (!svcxdr_decode_cookie(xdr, &argp->cookie))
  132. return false;
  133. if (xdr_stream_decode_bool(xdr, &exclusive) < 0)
  134. return false;
  135. if (!svcxdr_decode_lock(xdr, &argp->lock))
  136. return false;
  137. if (exclusive)
  138. argp->lock.fl.c.flc_type = F_WRLCK;
  139. return true;
  140. }
  141. bool
  142. nlm4svc_decode_lockargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
  143. {
  144. struct nlm_args *argp = rqstp->rq_argp;
  145. u32 exclusive;
  146. if (!svcxdr_decode_cookie(xdr, &argp->cookie))
  147. return false;
  148. if (xdr_stream_decode_bool(xdr, &argp->block) < 0)
  149. return false;
  150. if (xdr_stream_decode_bool(xdr, &exclusive) < 0)
  151. return false;
  152. if (!svcxdr_decode_lock(xdr, &argp->lock))
  153. return false;
  154. if (exclusive)
  155. argp->lock.fl.c.flc_type = F_WRLCK;
  156. if (xdr_stream_decode_bool(xdr, &argp->reclaim) < 0)
  157. return false;
  158. if (xdr_stream_decode_u32(xdr, &argp->state) < 0)
  159. return false;
  160. argp->monitor = 1; /* monitor client by default */
  161. return true;
  162. }
  163. bool
  164. nlm4svc_decode_cancargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
  165. {
  166. struct nlm_args *argp = rqstp->rq_argp;
  167. u32 exclusive;
  168. if (!svcxdr_decode_cookie(xdr, &argp->cookie))
  169. return false;
  170. if (xdr_stream_decode_bool(xdr, &argp->block) < 0)
  171. return false;
  172. if (xdr_stream_decode_bool(xdr, &exclusive) < 0)
  173. return false;
  174. if (!svcxdr_decode_lock(xdr, &argp->lock))
  175. return false;
  176. if (exclusive)
  177. argp->lock.fl.c.flc_type = F_WRLCK;
  178. return true;
  179. }
  180. bool
  181. nlm4svc_decode_unlockargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
  182. {
  183. struct nlm_args *argp = rqstp->rq_argp;
  184. if (!svcxdr_decode_cookie(xdr, &argp->cookie))
  185. return false;
  186. if (!svcxdr_decode_lock(xdr, &argp->lock))
  187. return false;
  188. argp->lock.fl.c.flc_type = F_UNLCK;
  189. return true;
  190. }
  191. bool
  192. nlm4svc_decode_res(struct svc_rqst *rqstp, struct xdr_stream *xdr)
  193. {
  194. struct nlm_res *resp = rqstp->rq_argp;
  195. if (!svcxdr_decode_cookie(xdr, &resp->cookie))
  196. return false;
  197. if (!svcxdr_decode_stats(xdr, &resp->status))
  198. return false;
  199. return true;
  200. }
  201. bool
  202. nlm4svc_decode_reboot(struct svc_rqst *rqstp, struct xdr_stream *xdr)
  203. {
  204. struct nlm_reboot *argp = rqstp->rq_argp;
  205. __be32 *p;
  206. u32 len;
  207. if (xdr_stream_decode_u32(xdr, &len) < 0)
  208. return false;
  209. if (len > SM_MAXSTRLEN)
  210. return false;
  211. p = xdr_inline_decode(xdr, len);
  212. if (!p)
  213. return false;
  214. argp->len = len;
  215. argp->mon = (char *)p;
  216. if (xdr_stream_decode_u32(xdr, &argp->state) < 0)
  217. return false;
  218. p = xdr_inline_decode(xdr, SM_PRIV_SIZE);
  219. if (!p)
  220. return false;
  221. memcpy(&argp->priv.data, p, sizeof(argp->priv.data));
  222. return true;
  223. }
  224. bool
  225. nlm4svc_decode_shareargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
  226. {
  227. struct nlm_args *argp = rqstp->rq_argp;
  228. struct nlm_lock *lock = &argp->lock;
  229. locks_init_lock(&lock->fl);
  230. lock->svid = ~(u32)0;
  231. if (!svcxdr_decode_cookie(xdr, &argp->cookie))
  232. return false;
  233. if (!svcxdr_decode_string(xdr, &lock->caller, &lock->len))
  234. return false;
  235. if (!svcxdr_decode_fhandle(xdr, &lock->fh))
  236. return false;
  237. if (!svcxdr_decode_owner(xdr, &lock->oh))
  238. return false;
  239. /* XXX: Range checks are missing in the original code */
  240. if (xdr_stream_decode_u32(xdr, &argp->fsm_mode) < 0)
  241. return false;
  242. if (xdr_stream_decode_u32(xdr, &argp->fsm_access) < 0)
  243. return false;
  244. return true;
  245. }
  246. bool
  247. nlm4svc_decode_notify(struct svc_rqst *rqstp, struct xdr_stream *xdr)
  248. {
  249. struct nlm_args *argp = rqstp->rq_argp;
  250. struct nlm_lock *lock = &argp->lock;
  251. if (!svcxdr_decode_string(xdr, &lock->caller, &lock->len))
  252. return false;
  253. if (xdr_stream_decode_u32(xdr, &argp->state) < 0)
  254. return false;
  255. return true;
  256. }
  257. /*
  258. * Encode Reply results
  259. */
  260. bool
  261. nlm4svc_encode_void(struct svc_rqst *rqstp, struct xdr_stream *xdr)
  262. {
  263. return true;
  264. }
  265. bool
  266. nlm4svc_encode_testres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
  267. {
  268. struct nlm_res *resp = rqstp->rq_resp;
  269. return svcxdr_encode_cookie(xdr, &resp->cookie) &&
  270. svcxdr_encode_testrply(xdr, resp);
  271. }
  272. bool
  273. nlm4svc_encode_res(struct svc_rqst *rqstp, struct xdr_stream *xdr)
  274. {
  275. struct nlm_res *resp = rqstp->rq_resp;
  276. return svcxdr_encode_cookie(xdr, &resp->cookie) &&
  277. svcxdr_encode_stats(xdr, resp->status);
  278. }
  279. bool
  280. nlm4svc_encode_shareres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
  281. {
  282. struct nlm_res *resp = rqstp->rq_resp;
  283. if (!svcxdr_encode_cookie(xdr, &resp->cookie))
  284. return false;
  285. if (!svcxdr_encode_stats(xdr, resp->status))
  286. return false;
  287. /* sequence */
  288. if (xdr_stream_encode_u32(xdr, 0) < 0)
  289. return false;
  290. return true;
  291. }