callback_xdr.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /*
  2. * linux/fs/nfs/callback_xdr.c
  3. *
  4. * Copyright (C) 2004 Trond Myklebust
  5. *
  6. * NFSv4 callback encode/decode procedures
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/sunrpc/svc.h>
  10. #include <linux/nfs4.h>
  11. #include <linux/nfs_fs.h>
  12. #include "nfs4_fs.h"
  13. #include "callback.h"
  14. #define CB_OP_TAGLEN_MAXSZ (512)
  15. #define CB_OP_HDR_RES_MAXSZ (2 + CB_OP_TAGLEN_MAXSZ)
  16. #define CB_OP_GETATTR_BITMAP_MAXSZ (4)
  17. #define CB_OP_GETATTR_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ + \
  18. CB_OP_GETATTR_BITMAP_MAXSZ + \
  19. 2 + 2 + 3 + 3)
  20. #define CB_OP_RECALL_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
  21. #define NFSDBG_FACILITY NFSDBG_CALLBACK
  22. typedef __be32 (*callback_process_op_t)(void *, void *);
  23. typedef __be32 (*callback_decode_arg_t)(struct svc_rqst *, struct xdr_stream *, void *);
  24. typedef __be32 (*callback_encode_res_t)(struct svc_rqst *, struct xdr_stream *, void *);
  25. struct callback_op {
  26. callback_process_op_t process_op;
  27. callback_decode_arg_t decode_args;
  28. callback_encode_res_t encode_res;
  29. long res_maxsize;
  30. };
  31. static struct callback_op callback_ops[];
  32. static __be32 nfs4_callback_null(struct svc_rqst *rqstp, void *argp, void *resp)
  33. {
  34. return htonl(NFS4_OK);
  35. }
  36. static int nfs4_decode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
  37. {
  38. return xdr_argsize_check(rqstp, p);
  39. }
  40. static int nfs4_encode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
  41. {
  42. return xdr_ressize_check(rqstp, p);
  43. }
  44. static __be32 *read_buf(struct xdr_stream *xdr, int nbytes)
  45. {
  46. __be32 *p;
  47. p = xdr_inline_decode(xdr, nbytes);
  48. if (unlikely(p == NULL))
  49. printk(KERN_WARNING "NFSv4 callback reply buffer overflowed!\n");
  50. return p;
  51. }
  52. static __be32 decode_string(struct xdr_stream *xdr, unsigned int *len, const char **str)
  53. {
  54. __be32 *p;
  55. p = read_buf(xdr, 4);
  56. if (unlikely(p == NULL))
  57. return htonl(NFS4ERR_RESOURCE);
  58. *len = ntohl(*p);
  59. if (*len != 0) {
  60. p = read_buf(xdr, *len);
  61. if (unlikely(p == NULL))
  62. return htonl(NFS4ERR_RESOURCE);
  63. *str = (const char *)p;
  64. } else
  65. *str = NULL;
  66. return 0;
  67. }
  68. static __be32 decode_fh(struct xdr_stream *xdr, struct nfs_fh *fh)
  69. {
  70. __be32 *p;
  71. p = read_buf(xdr, 4);
  72. if (unlikely(p == NULL))
  73. return htonl(NFS4ERR_RESOURCE);
  74. fh->size = ntohl(*p);
  75. if (fh->size > NFS4_FHSIZE)
  76. return htonl(NFS4ERR_BADHANDLE);
  77. p = read_buf(xdr, fh->size);
  78. if (unlikely(p == NULL))
  79. return htonl(NFS4ERR_RESOURCE);
  80. memcpy(&fh->data[0], p, fh->size);
  81. memset(&fh->data[fh->size], 0, sizeof(fh->data) - fh->size);
  82. return 0;
  83. }
  84. static __be32 decode_bitmap(struct xdr_stream *xdr, uint32_t *bitmap)
  85. {
  86. __be32 *p;
  87. unsigned int attrlen;
  88. p = read_buf(xdr, 4);
  89. if (unlikely(p == NULL))
  90. return htonl(NFS4ERR_RESOURCE);
  91. attrlen = ntohl(*p);
  92. p = read_buf(xdr, attrlen << 2);
  93. if (unlikely(p == NULL))
  94. return htonl(NFS4ERR_RESOURCE);
  95. if (likely(attrlen > 0))
  96. bitmap[0] = ntohl(*p++);
  97. if (attrlen > 1)
  98. bitmap[1] = ntohl(*p);
  99. return 0;
  100. }
  101. static __be32 decode_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
  102. {
  103. __be32 *p;
  104. p = read_buf(xdr, 16);
  105. if (unlikely(p == NULL))
  106. return htonl(NFS4ERR_RESOURCE);
  107. memcpy(stateid->data, p, 16);
  108. return 0;
  109. }
  110. static __be32 decode_compound_hdr_arg(struct xdr_stream *xdr, struct cb_compound_hdr_arg *hdr)
  111. {
  112. __be32 *p;
  113. unsigned int minor_version;
  114. __be32 status;
  115. status = decode_string(xdr, &hdr->taglen, &hdr->tag);
  116. if (unlikely(status != 0))
  117. return status;
  118. /* We do not like overly long tags! */
  119. if (hdr->taglen > CB_OP_TAGLEN_MAXSZ-12 || hdr->taglen < 0) {
  120. printk("NFSv4 CALLBACK %s: client sent tag of length %u\n",
  121. __FUNCTION__, hdr->taglen);
  122. return htonl(NFS4ERR_RESOURCE);
  123. }
  124. p = read_buf(xdr, 12);
  125. if (unlikely(p == NULL))
  126. return htonl(NFS4ERR_RESOURCE);
  127. minor_version = ntohl(*p++);
  128. /* Check minor version is zero. */
  129. if (minor_version != 0) {
  130. printk(KERN_WARNING "%s: NFSv4 server callback with illegal minor version %u!\n",
  131. __FUNCTION__, minor_version);
  132. return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
  133. }
  134. hdr->callback_ident = ntohl(*p++);
  135. hdr->nops = ntohl(*p);
  136. return 0;
  137. }
  138. static __be32 decode_op_hdr(struct xdr_stream *xdr, unsigned int *op)
  139. {
  140. __be32 *p;
  141. p = read_buf(xdr, 4);
  142. if (unlikely(p == NULL))
  143. return htonl(NFS4ERR_RESOURCE);
  144. *op = ntohl(*p);
  145. return 0;
  146. }
  147. static __be32 decode_getattr_args(struct svc_rqst *rqstp, struct xdr_stream *xdr, struct cb_getattrargs *args)
  148. {
  149. __be32 status;
  150. status = decode_fh(xdr, &args->fh);
  151. if (unlikely(status != 0))
  152. goto out;
  153. args->addr = svc_addr_in(rqstp);
  154. status = decode_bitmap(xdr, args->bitmap);
  155. out:
  156. dprintk("%s: exit with status = %d\n", __FUNCTION__, status);
  157. return status;
  158. }
  159. static __be32 decode_recall_args(struct svc_rqst *rqstp, struct xdr_stream *xdr, struct cb_recallargs *args)
  160. {
  161. __be32 *p;
  162. __be32 status;
  163. args->addr = svc_addr_in(rqstp);
  164. status = decode_stateid(xdr, &args->stateid);
  165. if (unlikely(status != 0))
  166. goto out;
  167. p = read_buf(xdr, 4);
  168. if (unlikely(p == NULL)) {
  169. status = htonl(NFS4ERR_RESOURCE);
  170. goto out;
  171. }
  172. args->truncate = ntohl(*p);
  173. status = decode_fh(xdr, &args->fh);
  174. out:
  175. dprintk("%s: exit with status = %d\n", __FUNCTION__, status);
  176. return status;
  177. }
  178. static __be32 encode_string(struct xdr_stream *xdr, unsigned int len, const char *str)
  179. {
  180. __be32 *p;
  181. p = xdr_reserve_space(xdr, 4 + len);
  182. if (unlikely(p == NULL))
  183. return htonl(NFS4ERR_RESOURCE);
  184. xdr_encode_opaque(p, str, len);
  185. return 0;
  186. }
  187. #define CB_SUPPORTED_ATTR0 (FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE)
  188. #define CB_SUPPORTED_ATTR1 (FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY)
  189. static __be32 encode_attr_bitmap(struct xdr_stream *xdr, const uint32_t *bitmap, __be32 **savep)
  190. {
  191. __be32 bm[2];
  192. __be32 *p;
  193. bm[0] = htonl(bitmap[0] & CB_SUPPORTED_ATTR0);
  194. bm[1] = htonl(bitmap[1] & CB_SUPPORTED_ATTR1);
  195. if (bm[1] != 0) {
  196. p = xdr_reserve_space(xdr, 16);
  197. if (unlikely(p == NULL))
  198. return htonl(NFS4ERR_RESOURCE);
  199. *p++ = htonl(2);
  200. *p++ = bm[0];
  201. *p++ = bm[1];
  202. } else if (bm[0] != 0) {
  203. p = xdr_reserve_space(xdr, 12);
  204. if (unlikely(p == NULL))
  205. return htonl(NFS4ERR_RESOURCE);
  206. *p++ = htonl(1);
  207. *p++ = bm[0];
  208. } else {
  209. p = xdr_reserve_space(xdr, 8);
  210. if (unlikely(p == NULL))
  211. return htonl(NFS4ERR_RESOURCE);
  212. *p++ = htonl(0);
  213. }
  214. *savep = p;
  215. return 0;
  216. }
  217. static __be32 encode_attr_change(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t change)
  218. {
  219. __be32 *p;
  220. if (!(bitmap[0] & FATTR4_WORD0_CHANGE))
  221. return 0;
  222. p = xdr_reserve_space(xdr, 8);
  223. if (unlikely(p == 0))
  224. return htonl(NFS4ERR_RESOURCE);
  225. p = xdr_encode_hyper(p, change);
  226. return 0;
  227. }
  228. static __be32 encode_attr_size(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t size)
  229. {
  230. __be32 *p;
  231. if (!(bitmap[0] & FATTR4_WORD0_SIZE))
  232. return 0;
  233. p = xdr_reserve_space(xdr, 8);
  234. if (unlikely(p == 0))
  235. return htonl(NFS4ERR_RESOURCE);
  236. p = xdr_encode_hyper(p, size);
  237. return 0;
  238. }
  239. static __be32 encode_attr_time(struct xdr_stream *xdr, const struct timespec *time)
  240. {
  241. __be32 *p;
  242. p = xdr_reserve_space(xdr, 12);
  243. if (unlikely(p == 0))
  244. return htonl(NFS4ERR_RESOURCE);
  245. p = xdr_encode_hyper(p, time->tv_sec);
  246. *p = htonl(time->tv_nsec);
  247. return 0;
  248. }
  249. static __be32 encode_attr_ctime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
  250. {
  251. if (!(bitmap[1] & FATTR4_WORD1_TIME_METADATA))
  252. return 0;
  253. return encode_attr_time(xdr,time);
  254. }
  255. static __be32 encode_attr_mtime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
  256. {
  257. if (!(bitmap[1] & FATTR4_WORD1_TIME_MODIFY))
  258. return 0;
  259. return encode_attr_time(xdr,time);
  260. }
  261. static __be32 encode_compound_hdr_res(struct xdr_stream *xdr, struct cb_compound_hdr_res *hdr)
  262. {
  263. __be32 status;
  264. hdr->status = xdr_reserve_space(xdr, 4);
  265. if (unlikely(hdr->status == NULL))
  266. return htonl(NFS4ERR_RESOURCE);
  267. status = encode_string(xdr, hdr->taglen, hdr->tag);
  268. if (unlikely(status != 0))
  269. return status;
  270. hdr->nops = xdr_reserve_space(xdr, 4);
  271. if (unlikely(hdr->nops == NULL))
  272. return htonl(NFS4ERR_RESOURCE);
  273. return 0;
  274. }
  275. static __be32 encode_op_hdr(struct xdr_stream *xdr, uint32_t op, __be32 res)
  276. {
  277. __be32 *p;
  278. p = xdr_reserve_space(xdr, 8);
  279. if (unlikely(p == NULL))
  280. return htonl(NFS4ERR_RESOURCE);
  281. *p++ = htonl(op);
  282. *p = res;
  283. return 0;
  284. }
  285. static __be32 encode_getattr_res(struct svc_rqst *rqstp, struct xdr_stream *xdr, const struct cb_getattrres *res)
  286. {
  287. __be32 *savep = NULL;
  288. __be32 status = res->status;
  289. if (unlikely(status != 0))
  290. goto out;
  291. status = encode_attr_bitmap(xdr, res->bitmap, &savep);
  292. if (unlikely(status != 0))
  293. goto out;
  294. status = encode_attr_change(xdr, res->bitmap, res->change_attr);
  295. if (unlikely(status != 0))
  296. goto out;
  297. status = encode_attr_size(xdr, res->bitmap, res->size);
  298. if (unlikely(status != 0))
  299. goto out;
  300. status = encode_attr_ctime(xdr, res->bitmap, &res->ctime);
  301. if (unlikely(status != 0))
  302. goto out;
  303. status = encode_attr_mtime(xdr, res->bitmap, &res->mtime);
  304. *savep = htonl((unsigned int)((char *)xdr->p - (char *)(savep+1)));
  305. out:
  306. dprintk("%s: exit with status = %d\n", __FUNCTION__, status);
  307. return status;
  308. }
  309. static __be32 process_op(struct svc_rqst *rqstp,
  310. struct xdr_stream *xdr_in, void *argp,
  311. struct xdr_stream *xdr_out, void *resp)
  312. {
  313. struct callback_op *op = &callback_ops[0];
  314. unsigned int op_nr = OP_CB_ILLEGAL;
  315. __be32 status = 0;
  316. long maxlen;
  317. __be32 res;
  318. dprintk("%s: start\n", __FUNCTION__);
  319. status = decode_op_hdr(xdr_in, &op_nr);
  320. if (likely(status == 0)) {
  321. switch (op_nr) {
  322. case OP_CB_GETATTR:
  323. case OP_CB_RECALL:
  324. op = &callback_ops[op_nr];
  325. break;
  326. default:
  327. op_nr = OP_CB_ILLEGAL;
  328. op = &callback_ops[0];
  329. status = htonl(NFS4ERR_OP_ILLEGAL);
  330. }
  331. }
  332. maxlen = xdr_out->end - xdr_out->p;
  333. if (maxlen > 0 && maxlen < PAGE_SIZE) {
  334. if (likely(status == 0 && op->decode_args != NULL))
  335. status = op->decode_args(rqstp, xdr_in, argp);
  336. if (likely(status == 0 && op->process_op != NULL))
  337. status = op->process_op(argp, resp);
  338. } else
  339. status = htonl(NFS4ERR_RESOURCE);
  340. res = encode_op_hdr(xdr_out, op_nr, status);
  341. if (status == 0)
  342. status = res;
  343. if (op->encode_res != NULL && status == 0)
  344. status = op->encode_res(rqstp, xdr_out, resp);
  345. dprintk("%s: done, status = %d\n", __FUNCTION__, status);
  346. return status;
  347. }
  348. /*
  349. * Decode, process and encode a COMPOUND
  350. */
  351. static __be32 nfs4_callback_compound(struct svc_rqst *rqstp, void *argp, void *resp)
  352. {
  353. struct cb_compound_hdr_arg hdr_arg;
  354. struct cb_compound_hdr_res hdr_res;
  355. struct xdr_stream xdr_in, xdr_out;
  356. __be32 *p;
  357. __be32 status;
  358. unsigned int nops = 1;
  359. dprintk("%s: start\n", __FUNCTION__);
  360. xdr_init_decode(&xdr_in, &rqstp->rq_arg, rqstp->rq_arg.head[0].iov_base);
  361. p = (__be32*)((char *)rqstp->rq_res.head[0].iov_base + rqstp->rq_res.head[0].iov_len);
  362. xdr_init_encode(&xdr_out, &rqstp->rq_res, p);
  363. decode_compound_hdr_arg(&xdr_in, &hdr_arg);
  364. hdr_res.taglen = hdr_arg.taglen;
  365. hdr_res.tag = hdr_arg.tag;
  366. hdr_res.nops = NULL;
  367. encode_compound_hdr_res(&xdr_out, &hdr_res);
  368. for (;;) {
  369. status = process_op(rqstp, &xdr_in, argp, &xdr_out, resp);
  370. if (status != 0)
  371. break;
  372. if (nops == hdr_arg.nops)
  373. break;
  374. nops++;
  375. }
  376. *hdr_res.status = status;
  377. *hdr_res.nops = htonl(nops);
  378. dprintk("%s: done, status = %u\n", __FUNCTION__, status);
  379. return rpc_success;
  380. }
  381. /*
  382. * Define NFS4 callback COMPOUND ops.
  383. */
  384. static struct callback_op callback_ops[] = {
  385. [0] = {
  386. .res_maxsize = CB_OP_HDR_RES_MAXSZ,
  387. },
  388. [OP_CB_GETATTR] = {
  389. .process_op = (callback_process_op_t)nfs4_callback_getattr,
  390. .decode_args = (callback_decode_arg_t)decode_getattr_args,
  391. .encode_res = (callback_encode_res_t)encode_getattr_res,
  392. .res_maxsize = CB_OP_GETATTR_RES_MAXSZ,
  393. },
  394. [OP_CB_RECALL] = {
  395. .process_op = (callback_process_op_t)nfs4_callback_recall,
  396. .decode_args = (callback_decode_arg_t)decode_recall_args,
  397. .res_maxsize = CB_OP_RECALL_RES_MAXSZ,
  398. }
  399. };
  400. /*
  401. * Define NFS4 callback procedures
  402. */
  403. static struct svc_procedure nfs4_callback_procedures1[] = {
  404. [CB_NULL] = {
  405. .pc_func = nfs4_callback_null,
  406. .pc_decode = (kxdrproc_t)nfs4_decode_void,
  407. .pc_encode = (kxdrproc_t)nfs4_encode_void,
  408. .pc_xdrressize = 1,
  409. },
  410. [CB_COMPOUND] = {
  411. .pc_func = nfs4_callback_compound,
  412. .pc_encode = (kxdrproc_t)nfs4_encode_void,
  413. .pc_argsize = 256,
  414. .pc_ressize = 256,
  415. .pc_xdrressize = NFS4_CALLBACK_BUFSIZE,
  416. }
  417. };
  418. struct svc_version nfs4_callback_version1 = {
  419. .vs_vers = 1,
  420. .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
  421. .vs_proc = nfs4_callback_procedures1,
  422. .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
  423. .vs_dispatch = NULL,
  424. };