xdr4.c 7.6 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. #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_flags = FL_POSIX;
  80. fl->c.flc_type = F_RDLCK;
  81. nlm4svc_set_file_lock_range(fl, lock->lock_start, lock->lock_len);
  82. return true;
  83. }
  84. static bool
  85. svcxdr_encode_holder(struct xdr_stream *xdr, const struct nlm_lock *lock)
  86. {
  87. const struct file_lock *fl = &lock->fl;
  88. s64 start, len;
  89. /* exclusive */
  90. if (xdr_stream_encode_bool(xdr, fl->c.flc_type != F_RDLCK) < 0)
  91. return false;
  92. if (xdr_stream_encode_u32(xdr, lock->svid) < 0)
  93. return false;
  94. if (!svcxdr_encode_owner(xdr, &lock->oh))
  95. return false;
  96. start = loff_t_to_s64(fl->fl_start);
  97. if (fl->fl_end == OFFSET_MAX)
  98. len = 0;
  99. else
  100. len = loff_t_to_s64(fl->fl_end - fl->fl_start + 1);
  101. if (xdr_stream_encode_u64(xdr, start) < 0)
  102. return false;
  103. if (xdr_stream_encode_u64(xdr, len) < 0)
  104. return false;
  105. return true;
  106. }
  107. static bool
  108. svcxdr_encode_testrply(struct xdr_stream *xdr, const struct nlm_res *resp)
  109. {
  110. if (!svcxdr_encode_stats(xdr, resp->status))
  111. return false;
  112. switch (resp->status) {
  113. case nlm_lck_denied:
  114. if (!svcxdr_encode_holder(xdr, &resp->lock))
  115. return false;
  116. }
  117. return true;
  118. }
  119. /*
  120. * Decode Call arguments
  121. */
  122. bool
  123. nlm4svc_decode_void(struct svc_rqst *rqstp, struct xdr_stream *xdr)
  124. {
  125. return true;
  126. }
  127. bool
  128. nlm4svc_decode_testargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
  129. {
  130. struct nlm_args *argp = rqstp->rq_argp;
  131. u32 exclusive;
  132. if (!svcxdr_decode_cookie(xdr, &argp->cookie))
  133. return false;
  134. if (xdr_stream_decode_bool(xdr, &exclusive) < 0)
  135. return false;
  136. if (!svcxdr_decode_lock(xdr, &argp->lock))
  137. return false;
  138. if (exclusive)
  139. argp->lock.fl.c.flc_type = F_WRLCK;
  140. return true;
  141. }
  142. bool
  143. nlm4svc_decode_lockargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
  144. {
  145. struct nlm_args *argp = rqstp->rq_argp;
  146. u32 exclusive;
  147. if (!svcxdr_decode_cookie(xdr, &argp->cookie))
  148. return false;
  149. if (xdr_stream_decode_bool(xdr, &argp->block) < 0)
  150. return false;
  151. if (xdr_stream_decode_bool(xdr, &exclusive) < 0)
  152. return false;
  153. if (!svcxdr_decode_lock(xdr, &argp->lock))
  154. return false;
  155. if (exclusive)
  156. argp->lock.fl.c.flc_type = F_WRLCK;
  157. if (xdr_stream_decode_bool(xdr, &argp->reclaim) < 0)
  158. return false;
  159. if (xdr_stream_decode_u32(xdr, &argp->state) < 0)
  160. return false;
  161. argp->monitor = 1; /* monitor client by default */
  162. return true;
  163. }
  164. bool
  165. nlm4svc_decode_cancargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
  166. {
  167. struct nlm_args *argp = rqstp->rq_argp;
  168. u32 exclusive;
  169. if (!svcxdr_decode_cookie(xdr, &argp->cookie))
  170. return false;
  171. if (xdr_stream_decode_bool(xdr, &argp->block) < 0)
  172. return false;
  173. if (xdr_stream_decode_bool(xdr, &exclusive) < 0)
  174. return false;
  175. if (!svcxdr_decode_lock(xdr, &argp->lock))
  176. return false;
  177. if (exclusive)
  178. argp->lock.fl.c.flc_type = F_WRLCK;
  179. return true;
  180. }
  181. bool
  182. nlm4svc_decode_unlockargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
  183. {
  184. struct nlm_args *argp = rqstp->rq_argp;
  185. if (!svcxdr_decode_cookie(xdr, &argp->cookie))
  186. return false;
  187. if (!svcxdr_decode_lock(xdr, &argp->lock))
  188. return false;
  189. argp->lock.fl.c.flc_type = F_UNLCK;
  190. return true;
  191. }
  192. bool
  193. nlm4svc_decode_res(struct svc_rqst *rqstp, struct xdr_stream *xdr)
  194. {
  195. struct nlm_res *resp = rqstp->rq_argp;
  196. if (!svcxdr_decode_cookie(xdr, &resp->cookie))
  197. return false;
  198. if (!svcxdr_decode_stats(xdr, &resp->status))
  199. return false;
  200. return true;
  201. }
  202. bool
  203. nlm4svc_decode_reboot(struct svc_rqst *rqstp, struct xdr_stream *xdr)
  204. {
  205. struct nlm_reboot *argp = rqstp->rq_argp;
  206. __be32 *p;
  207. u32 len;
  208. if (xdr_stream_decode_u32(xdr, &len) < 0)
  209. return false;
  210. if (len > SM_MAXSTRLEN)
  211. return false;
  212. p = xdr_inline_decode(xdr, len);
  213. if (!p)
  214. return false;
  215. argp->len = len;
  216. argp->mon = (char *)p;
  217. if (xdr_stream_decode_u32(xdr, &argp->state) < 0)
  218. return false;
  219. p = xdr_inline_decode(xdr, SM_PRIV_SIZE);
  220. if (!p)
  221. return false;
  222. memcpy(&argp->priv.data, p, sizeof(argp->priv.data));
  223. return true;
  224. }
  225. bool
  226. nlm4svc_decode_shareargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
  227. {
  228. struct nlm_args *argp = rqstp->rq_argp;
  229. struct nlm_lock *lock = &argp->lock;
  230. memset(lock, 0, sizeof(*lock));
  231. locks_init_lock(&lock->fl);
  232. lock->svid = ~(u32)0;
  233. if (!svcxdr_decode_cookie(xdr, &argp->cookie))
  234. return false;
  235. if (!svcxdr_decode_string(xdr, &lock->caller, &lock->len))
  236. return false;
  237. if (!svcxdr_decode_fhandle(xdr, &lock->fh))
  238. return false;
  239. if (!svcxdr_decode_owner(xdr, &lock->oh))
  240. return false;
  241. /* XXX: Range checks are missing in the original code */
  242. if (xdr_stream_decode_u32(xdr, &argp->fsm_mode) < 0)
  243. return false;
  244. if (xdr_stream_decode_u32(xdr, &argp->fsm_access) < 0)
  245. return false;
  246. return true;
  247. }
  248. bool
  249. nlm4svc_decode_notify(struct svc_rqst *rqstp, struct xdr_stream *xdr)
  250. {
  251. struct nlm_args *argp = rqstp->rq_argp;
  252. struct nlm_lock *lock = &argp->lock;
  253. if (!svcxdr_decode_string(xdr, &lock->caller, &lock->len))
  254. return false;
  255. if (xdr_stream_decode_u32(xdr, &argp->state) < 0)
  256. return false;
  257. return true;
  258. }
  259. /*
  260. * Encode Reply results
  261. */
  262. bool
  263. nlm4svc_encode_void(struct svc_rqst *rqstp, struct xdr_stream *xdr)
  264. {
  265. return true;
  266. }
  267. bool
  268. nlm4svc_encode_testres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
  269. {
  270. struct nlm_res *resp = rqstp->rq_resp;
  271. return svcxdr_encode_cookie(xdr, &resp->cookie) &&
  272. svcxdr_encode_testrply(xdr, resp);
  273. }
  274. bool
  275. nlm4svc_encode_res(struct svc_rqst *rqstp, struct xdr_stream *xdr)
  276. {
  277. struct nlm_res *resp = rqstp->rq_resp;
  278. return svcxdr_encode_cookie(xdr, &resp->cookie) &&
  279. svcxdr_encode_stats(xdr, resp->status);
  280. }
  281. bool
  282. nlm4svc_encode_shareres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
  283. {
  284. struct nlm_res *resp = rqstp->rq_resp;
  285. if (!svcxdr_encode_cookie(xdr, &resp->cookie))
  286. return false;
  287. if (!svcxdr_encode_stats(xdr, resp->status))
  288. return false;
  289. /* sequence */
  290. if (xdr_stream_encode_u32(xdr, 0) < 0)
  291. return false;
  292. return true;
  293. }