xdr4.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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, nlm_args *argp)
  156. {
  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, struct nlm_res *resp)
  169. {
  170. if (!(p = nlm4_encode_testres(p, resp)))
  171. return 0;
  172. return xdr_ressize_check(rqstp, p);
  173. }
  174. int
  175. nlm4svc_decode_lockargs(struct svc_rqst *rqstp, __be32 *p, nlm_args *argp)
  176. {
  177. u32 exclusive;
  178. if (!(p = nlm4_decode_cookie(p, &argp->cookie)))
  179. return 0;
  180. argp->block = ntohl(*p++);
  181. exclusive = ntohl(*p++);
  182. if (!(p = nlm4_decode_lock(p, &argp->lock)))
  183. return 0;
  184. if (exclusive)
  185. argp->lock.fl.fl_type = F_WRLCK;
  186. argp->reclaim = ntohl(*p++);
  187. argp->state = ntohl(*p++);
  188. argp->monitor = 1; /* monitor client by default */
  189. return xdr_argsize_check(rqstp, p);
  190. }
  191. int
  192. nlm4svc_decode_cancargs(struct svc_rqst *rqstp, __be32 *p, nlm_args *argp)
  193. {
  194. u32 exclusive;
  195. if (!(p = nlm4_decode_cookie(p, &argp->cookie)))
  196. return 0;
  197. argp->block = ntohl(*p++);
  198. exclusive = ntohl(*p++);
  199. if (!(p = nlm4_decode_lock(p, &argp->lock)))
  200. return 0;
  201. if (exclusive)
  202. argp->lock.fl.fl_type = F_WRLCK;
  203. return xdr_argsize_check(rqstp, p);
  204. }
  205. int
  206. nlm4svc_decode_unlockargs(struct svc_rqst *rqstp, __be32 *p, nlm_args *argp)
  207. {
  208. if (!(p = nlm4_decode_cookie(p, &argp->cookie))
  209. || !(p = nlm4_decode_lock(p, &argp->lock)))
  210. return 0;
  211. argp->lock.fl.fl_type = F_UNLCK;
  212. return xdr_argsize_check(rqstp, p);
  213. }
  214. int
  215. nlm4svc_decode_shareargs(struct svc_rqst *rqstp, __be32 *p, nlm_args *argp)
  216. {
  217. struct nlm_lock *lock = &argp->lock;
  218. memset(lock, 0, sizeof(*lock));
  219. locks_init_lock(&lock->fl);
  220. lock->svid = ~(u32) 0;
  221. lock->fl.fl_pid = (pid_t)lock->svid;
  222. if (!(p = nlm4_decode_cookie(p, &argp->cookie))
  223. || !(p = xdr_decode_string_inplace(p, &lock->caller,
  224. &lock->len, NLM_MAXSTRLEN))
  225. || !(p = nlm4_decode_fh(p, &lock->fh))
  226. || !(p = nlm4_decode_oh(p, &lock->oh)))
  227. return 0;
  228. argp->fsm_mode = ntohl(*p++);
  229. argp->fsm_access = ntohl(*p++);
  230. return xdr_argsize_check(rqstp, p);
  231. }
  232. int
  233. nlm4svc_encode_shareres(struct svc_rqst *rqstp, __be32 *p, struct nlm_res *resp)
  234. {
  235. if (!(p = nlm4_encode_cookie(p, &resp->cookie)))
  236. return 0;
  237. *p++ = resp->status;
  238. *p++ = xdr_zero; /* sequence argument */
  239. return xdr_ressize_check(rqstp, p);
  240. }
  241. int
  242. nlm4svc_encode_res(struct svc_rqst *rqstp, __be32 *p, struct nlm_res *resp)
  243. {
  244. if (!(p = nlm4_encode_cookie(p, &resp->cookie)))
  245. return 0;
  246. *p++ = resp->status;
  247. return xdr_ressize_check(rqstp, p);
  248. }
  249. int
  250. nlm4svc_decode_notify(struct svc_rqst *rqstp, __be32 *p, struct nlm_args *argp)
  251. {
  252. struct nlm_lock *lock = &argp->lock;
  253. if (!(p = xdr_decode_string_inplace(p, &lock->caller,
  254. &lock->len, NLM_MAXSTRLEN)))
  255. return 0;
  256. argp->state = ntohl(*p++);
  257. return xdr_argsize_check(rqstp, p);
  258. }
  259. int
  260. nlm4svc_decode_reboot(struct svc_rqst *rqstp, __be32 *p, struct nlm_reboot *argp)
  261. {
  262. if (!(p = xdr_decode_string_inplace(p, &argp->mon, &argp->len, SM_MAXSTRLEN)))
  263. return 0;
  264. argp->state = ntohl(*p++);
  265. memcpy(&argp->priv.data, p, sizeof(argp->priv.data));
  266. p += XDR_QUADLEN(SM_PRIV_SIZE);
  267. return xdr_argsize_check(rqstp, p);
  268. }
  269. int
  270. nlm4svc_decode_res(struct svc_rqst *rqstp, __be32 *p, struct nlm_res *resp)
  271. {
  272. if (!(p = nlm4_decode_cookie(p, &resp->cookie)))
  273. return 0;
  274. resp->status = *p++;
  275. return xdr_argsize_check(rqstp, p);
  276. }
  277. int
  278. nlm4svc_decode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
  279. {
  280. return xdr_argsize_check(rqstp, p);
  281. }
  282. int
  283. nlm4svc_encode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
  284. {
  285. return xdr_ressize_check(rqstp, p);
  286. }