callback_xdr.c 12 KB

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