xdr.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/lockd/xdr.c
  4. *
  5. * XDR support for lockd and the lock client.
  6. *
  7. * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
  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. #include <uapi/linux/nfs2.h>
  18. #define NLMDBG_FACILITY NLMDBG_XDR
  19. static inline loff_t
  20. s32_to_loff_t(__s32 offset)
  21. {
  22. return (loff_t)offset;
  23. }
  24. static inline __s32
  25. loff_t_to_s32(loff_t offset)
  26. {
  27. __s32 res;
  28. if (offset >= NLM_OFFSET_MAX)
  29. res = NLM_OFFSET_MAX;
  30. else if (offset <= -NLM_OFFSET_MAX)
  31. res = -NLM_OFFSET_MAX;
  32. else
  33. res = offset;
  34. return res;
  35. }
  36. /*
  37. * XDR functions for basic NLM types
  38. */
  39. static __be32 *nlm_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 inline __be32 *
  64. nlm_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. nlm_decode_fh(__be32 *p, struct nfs_fh *f)
  73. {
  74. unsigned int len;
  75. if ((len = ntohl(*p++)) != NFS2_FHSIZE) {
  76. dprintk("lockd: bad fhandle size %d (should be %d)\n",
  77. len, NFS2_FHSIZE);
  78. return NULL;
  79. }
  80. f->size = NFS2_FHSIZE;
  81. memset(f->data, 0, sizeof(f->data));
  82. memcpy(f->data, p, NFS2_FHSIZE);
  83. return p + XDR_QUADLEN(NFS2_FHSIZE);
  84. }
  85. /*
  86. * Encode and decode owner handle
  87. */
  88. static inline __be32 *
  89. nlm_decode_oh(__be32 *p, struct xdr_netobj *oh)
  90. {
  91. return xdr_decode_netobj(p, oh);
  92. }
  93. static inline __be32 *
  94. nlm_encode_oh(__be32 *p, struct xdr_netobj *oh)
  95. {
  96. return xdr_encode_netobj(p, oh);
  97. }
  98. static __be32 *
  99. nlm_decode_lock(__be32 *p, struct nlm_lock *lock)
  100. {
  101. struct file_lock *fl = &lock->fl;
  102. s32 start, len, end;
  103. if (!(p = xdr_decode_string_inplace(p, &lock->caller,
  104. &lock->len,
  105. NLM_MAXSTRLEN))
  106. || !(p = nlm_decode_fh(p, &lock->fh))
  107. || !(p = nlm_decode_oh(p, &lock->oh)))
  108. return NULL;
  109. lock->svid = ntohl(*p++);
  110. locks_init_lock(fl);
  111. fl->fl_owner = current->files;
  112. fl->fl_pid = (pid_t)lock->svid;
  113. fl->fl_flags = FL_POSIX;
  114. fl->fl_type = F_RDLCK; /* as good as anything else */
  115. start = ntohl(*p++);
  116. len = ntohl(*p++);
  117. end = start + len - 1;
  118. fl->fl_start = s32_to_loff_t(start);
  119. if (len == 0 || end < 0)
  120. fl->fl_end = OFFSET_MAX;
  121. else
  122. fl->fl_end = s32_to_loff_t(end);
  123. return p;
  124. }
  125. /*
  126. * Encode result of a TEST/TEST_MSG call
  127. */
  128. static __be32 *
  129. nlm_encode_testres(__be32 *p, struct nlm_res *resp)
  130. {
  131. s32 start, len;
  132. if (!(p = nlm_encode_cookie(p, &resp->cookie)))
  133. return NULL;
  134. *p++ = resp->status;
  135. if (resp->status == nlm_lck_denied) {
  136. struct file_lock *fl = &resp->lock.fl;
  137. *p++ = (fl->fl_type == F_RDLCK)? xdr_zero : xdr_one;
  138. *p++ = htonl(resp->lock.svid);
  139. /* Encode owner handle. */
  140. if (!(p = xdr_encode_netobj(p, &resp->lock.oh)))
  141. return NULL;
  142. start = loff_t_to_s32(fl->fl_start);
  143. if (fl->fl_end == OFFSET_MAX)
  144. len = 0;
  145. else
  146. len = loff_t_to_s32(fl->fl_end - fl->fl_start + 1);
  147. *p++ = htonl(start);
  148. *p++ = htonl(len);
  149. }
  150. return p;
  151. }
  152. /*
  153. * First, the server side XDR functions
  154. */
  155. int
  156. nlmsvc_decode_testargs(struct svc_rqst *rqstp, __be32 *p)
  157. {
  158. struct nlm_args *argp = rqstp->rq_argp;
  159. u32 exclusive;
  160. if (!(p = nlm_decode_cookie(p, &argp->cookie)))
  161. return 0;
  162. exclusive = ntohl(*p++);
  163. if (!(p = nlm_decode_lock(p, &argp->lock)))
  164. return 0;
  165. if (exclusive)
  166. argp->lock.fl.fl_type = F_WRLCK;
  167. return xdr_argsize_check(rqstp, p);
  168. }
  169. int
  170. nlmsvc_encode_testres(struct svc_rqst *rqstp, __be32 *p)
  171. {
  172. struct nlm_res *resp = rqstp->rq_resp;
  173. if (!(p = nlm_encode_testres(p, resp)))
  174. return 0;
  175. return xdr_ressize_check(rqstp, p);
  176. }
  177. int
  178. nlmsvc_decode_lockargs(struct svc_rqst *rqstp, __be32 *p)
  179. {
  180. struct nlm_args *argp = rqstp->rq_argp;
  181. u32 exclusive;
  182. if (!(p = nlm_decode_cookie(p, &argp->cookie)))
  183. return 0;
  184. argp->block = ntohl(*p++);
  185. exclusive = ntohl(*p++);
  186. if (!(p = nlm_decode_lock(p, &argp->lock)))
  187. return 0;
  188. if (exclusive)
  189. argp->lock.fl.fl_type = F_WRLCK;
  190. argp->reclaim = ntohl(*p++);
  191. argp->state = ntohl(*p++);
  192. argp->monitor = 1; /* monitor client by default */
  193. return xdr_argsize_check(rqstp, p);
  194. }
  195. int
  196. nlmsvc_decode_cancargs(struct svc_rqst *rqstp, __be32 *p)
  197. {
  198. struct nlm_args *argp = rqstp->rq_argp;
  199. u32 exclusive;
  200. if (!(p = nlm_decode_cookie(p, &argp->cookie)))
  201. return 0;
  202. argp->block = ntohl(*p++);
  203. exclusive = ntohl(*p++);
  204. if (!(p = nlm_decode_lock(p, &argp->lock)))
  205. return 0;
  206. if (exclusive)
  207. argp->lock.fl.fl_type = F_WRLCK;
  208. return xdr_argsize_check(rqstp, p);
  209. }
  210. int
  211. nlmsvc_decode_unlockargs(struct svc_rqst *rqstp, __be32 *p)
  212. {
  213. struct nlm_args *argp = rqstp->rq_argp;
  214. if (!(p = nlm_decode_cookie(p, &argp->cookie))
  215. || !(p = nlm_decode_lock(p, &argp->lock)))
  216. return 0;
  217. argp->lock.fl.fl_type = F_UNLCK;
  218. return xdr_argsize_check(rqstp, p);
  219. }
  220. int
  221. nlmsvc_decode_shareargs(struct svc_rqst *rqstp, __be32 *p)
  222. {
  223. struct nlm_args *argp = rqstp->rq_argp;
  224. struct nlm_lock *lock = &argp->lock;
  225. memset(lock, 0, sizeof(*lock));
  226. locks_init_lock(&lock->fl);
  227. lock->svid = ~(u32) 0;
  228. lock->fl.fl_pid = (pid_t)lock->svid;
  229. if (!(p = nlm_decode_cookie(p, &argp->cookie))
  230. || !(p = xdr_decode_string_inplace(p, &lock->caller,
  231. &lock->len, NLM_MAXSTRLEN))
  232. || !(p = nlm_decode_fh(p, &lock->fh))
  233. || !(p = nlm_decode_oh(p, &lock->oh)))
  234. return 0;
  235. argp->fsm_mode = ntohl(*p++);
  236. argp->fsm_access = ntohl(*p++);
  237. return xdr_argsize_check(rqstp, p);
  238. }
  239. int
  240. nlmsvc_encode_shareres(struct svc_rqst *rqstp, __be32 *p)
  241. {
  242. struct nlm_res *resp = rqstp->rq_resp;
  243. if (!(p = nlm_encode_cookie(p, &resp->cookie)))
  244. return 0;
  245. *p++ = resp->status;
  246. *p++ = xdr_zero; /* sequence argument */
  247. return xdr_ressize_check(rqstp, p);
  248. }
  249. int
  250. nlmsvc_encode_res(struct svc_rqst *rqstp, __be32 *p)
  251. {
  252. struct nlm_res *resp = rqstp->rq_resp;
  253. if (!(p = nlm_encode_cookie(p, &resp->cookie)))
  254. return 0;
  255. *p++ = resp->status;
  256. return xdr_ressize_check(rqstp, p);
  257. }
  258. int
  259. nlmsvc_decode_notify(struct svc_rqst *rqstp, __be32 *p)
  260. {
  261. struct nlm_args *argp = rqstp->rq_argp;
  262. struct nlm_lock *lock = &argp->lock;
  263. if (!(p = xdr_decode_string_inplace(p, &lock->caller,
  264. &lock->len, NLM_MAXSTRLEN)))
  265. return 0;
  266. argp->state = ntohl(*p++);
  267. return xdr_argsize_check(rqstp, p);
  268. }
  269. int
  270. nlmsvc_decode_reboot(struct svc_rqst *rqstp, __be32 *p)
  271. {
  272. struct nlm_reboot *argp = rqstp->rq_argp;
  273. if (!(p = xdr_decode_string_inplace(p, &argp->mon, &argp->len, SM_MAXSTRLEN)))
  274. return 0;
  275. argp->state = ntohl(*p++);
  276. memcpy(&argp->priv.data, p, sizeof(argp->priv.data));
  277. p += XDR_QUADLEN(SM_PRIV_SIZE);
  278. return xdr_argsize_check(rqstp, p);
  279. }
  280. int
  281. nlmsvc_decode_res(struct svc_rqst *rqstp, __be32 *p)
  282. {
  283. struct nlm_res *resp = rqstp->rq_argp;
  284. if (!(p = nlm_decode_cookie(p, &resp->cookie)))
  285. return 0;
  286. resp->status = *p++;
  287. return xdr_argsize_check(rqstp, p);
  288. }
  289. int
  290. nlmsvc_decode_void(struct svc_rqst *rqstp, __be32 *p)
  291. {
  292. return xdr_argsize_check(rqstp, p);
  293. }
  294. int
  295. nlmsvc_encode_void(struct svc_rqst *rqstp, __be32 *p)
  296. {
  297. return xdr_ressize_check(rqstp, p);
  298. }