callback_xdr.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. __be32 status;
  114. status = decode_string(xdr, &hdr->taglen, &hdr->tag);
  115. if (unlikely(status != 0))
  116. return status;
  117. /* We do not like overly long tags! */
  118. if (hdr->taglen > CB_OP_TAGLEN_MAXSZ - 12) {
  119. printk("NFSv4 CALLBACK %s: client sent tag of length %u\n",
  120. __func__, hdr->taglen);
  121. return htonl(NFS4ERR_RESOURCE);
  122. }
  123. p = read_buf(xdr, 12);
  124. if (unlikely(p == NULL))
  125. return htonl(NFS4ERR_RESOURCE);
  126. hdr->minorversion = ntohl(*p++);
  127. /* Check minor version is zero. */
  128. if (hdr->minorversion != 0) {
  129. printk(KERN_WARNING "%s: NFSv4 server callback with "
  130. "illegal minor version %u!\n",
  131. __func__, hdr->minorversion);
  132. return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
  133. }
  134. hdr->callback_ident = ntohl(*p++);
  135. hdr->nops = ntohl(*p);
  136. dprintk("%s: minorversion %d nops %d\n", __func__,
  137. hdr->minorversion, hdr->nops);
  138. return 0;
  139. }
  140. static __be32 decode_op_hdr(struct xdr_stream *xdr, unsigned int *op)
  141. {
  142. __be32 *p;
  143. p = read_buf(xdr, 4);
  144. if (unlikely(p == NULL))
  145. return htonl(NFS4ERR_RESOURCE);
  146. *op = ntohl(*p);
  147. return 0;
  148. }
  149. static __be32 decode_getattr_args(struct svc_rqst *rqstp, struct xdr_stream *xdr, struct cb_getattrargs *args)
  150. {
  151. __be32 status;
  152. status = decode_fh(xdr, &args->fh);
  153. if (unlikely(status != 0))
  154. goto out;
  155. args->addr = svc_addr(rqstp);
  156. status = decode_bitmap(xdr, args->bitmap);
  157. out:
  158. dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
  159. return status;
  160. }
  161. static __be32 decode_recall_args(struct svc_rqst *rqstp, struct xdr_stream *xdr, struct cb_recallargs *args)
  162. {
  163. __be32 *p;
  164. __be32 status;
  165. args->addr = svc_addr(rqstp);
  166. status = decode_stateid(xdr, &args->stateid);
  167. if (unlikely(status != 0))
  168. goto out;
  169. p = read_buf(xdr, 4);
  170. if (unlikely(p == NULL)) {
  171. status = htonl(NFS4ERR_RESOURCE);
  172. goto out;
  173. }
  174. args->truncate = ntohl(*p);
  175. status = decode_fh(xdr, &args->fh);
  176. out:
  177. dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
  178. return status;
  179. }
  180. static __be32 encode_string(struct xdr_stream *xdr, unsigned int len, const char *str)
  181. {
  182. __be32 *p;
  183. p = xdr_reserve_space(xdr, 4 + len);
  184. if (unlikely(p == NULL))
  185. return htonl(NFS4ERR_RESOURCE);
  186. xdr_encode_opaque(p, str, len);
  187. return 0;
  188. }
  189. #define CB_SUPPORTED_ATTR0 (FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE)
  190. #define CB_SUPPORTED_ATTR1 (FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY)
  191. static __be32 encode_attr_bitmap(struct xdr_stream *xdr, const uint32_t *bitmap, __be32 **savep)
  192. {
  193. __be32 bm[2];
  194. __be32 *p;
  195. bm[0] = htonl(bitmap[0] & CB_SUPPORTED_ATTR0);
  196. bm[1] = htonl(bitmap[1] & CB_SUPPORTED_ATTR1);
  197. if (bm[1] != 0) {
  198. p = xdr_reserve_space(xdr, 16);
  199. if (unlikely(p == NULL))
  200. return htonl(NFS4ERR_RESOURCE);
  201. *p++ = htonl(2);
  202. *p++ = bm[0];
  203. *p++ = bm[1];
  204. } else if (bm[0] != 0) {
  205. p = xdr_reserve_space(xdr, 12);
  206. if (unlikely(p == NULL))
  207. return htonl(NFS4ERR_RESOURCE);
  208. *p++ = htonl(1);
  209. *p++ = bm[0];
  210. } else {
  211. p = xdr_reserve_space(xdr, 8);
  212. if (unlikely(p == NULL))
  213. return htonl(NFS4ERR_RESOURCE);
  214. *p++ = htonl(0);
  215. }
  216. *savep = p;
  217. return 0;
  218. }
  219. static __be32 encode_attr_change(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t change)
  220. {
  221. __be32 *p;
  222. if (!(bitmap[0] & FATTR4_WORD0_CHANGE))
  223. return 0;
  224. p = xdr_reserve_space(xdr, 8);
  225. if (unlikely(!p))
  226. return htonl(NFS4ERR_RESOURCE);
  227. p = xdr_encode_hyper(p, change);
  228. return 0;
  229. }
  230. static __be32 encode_attr_size(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t size)
  231. {
  232. __be32 *p;
  233. if (!(bitmap[0] & FATTR4_WORD0_SIZE))
  234. return 0;
  235. p = xdr_reserve_space(xdr, 8);
  236. if (unlikely(!p))
  237. return htonl(NFS4ERR_RESOURCE);
  238. p = xdr_encode_hyper(p, size);
  239. return 0;
  240. }
  241. static __be32 encode_attr_time(struct xdr_stream *xdr, const struct timespec *time)
  242. {
  243. __be32 *p;
  244. p = xdr_reserve_space(xdr, 12);
  245. if (unlikely(!p))
  246. return htonl(NFS4ERR_RESOURCE);
  247. p = xdr_encode_hyper(p, time->tv_sec);
  248. *p = htonl(time->tv_nsec);
  249. return 0;
  250. }
  251. static __be32 encode_attr_ctime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
  252. {
  253. if (!(bitmap[1] & FATTR4_WORD1_TIME_METADATA))
  254. return 0;
  255. return encode_attr_time(xdr,time);
  256. }
  257. static __be32 encode_attr_mtime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
  258. {
  259. if (!(bitmap[1] & FATTR4_WORD1_TIME_MODIFY))
  260. return 0;
  261. return encode_attr_time(xdr,time);
  262. }
  263. static __be32 encode_compound_hdr_res(struct xdr_stream *xdr, struct cb_compound_hdr_res *hdr)
  264. {
  265. __be32 status;
  266. hdr->status = xdr_reserve_space(xdr, 4);
  267. if (unlikely(hdr->status == NULL))
  268. return htonl(NFS4ERR_RESOURCE);
  269. status = encode_string(xdr, hdr->taglen, hdr->tag);
  270. if (unlikely(status != 0))
  271. return status;
  272. hdr->nops = xdr_reserve_space(xdr, 4);
  273. if (unlikely(hdr->nops == NULL))
  274. return htonl(NFS4ERR_RESOURCE);
  275. return 0;
  276. }
  277. static __be32 encode_op_hdr(struct xdr_stream *xdr, uint32_t op, __be32 res)
  278. {
  279. __be32 *p;
  280. p = xdr_reserve_space(xdr, 8);
  281. if (unlikely(p == NULL))
  282. return htonl(NFS4ERR_RESOURCE);
  283. *p++ = htonl(op);
  284. *p = res;
  285. return 0;
  286. }
  287. static __be32 encode_getattr_res(struct svc_rqst *rqstp, struct xdr_stream *xdr, const struct cb_getattrres *res)
  288. {
  289. __be32 *savep = NULL;
  290. __be32 status = res->status;
  291. if (unlikely(status != 0))
  292. goto out;
  293. status = encode_attr_bitmap(xdr, res->bitmap, &savep);
  294. if (unlikely(status != 0))
  295. goto out;
  296. status = encode_attr_change(xdr, res->bitmap, res->change_attr);
  297. if (unlikely(status != 0))
  298. goto out;
  299. status = encode_attr_size(xdr, res->bitmap, res->size);
  300. if (unlikely(status != 0))
  301. goto out;
  302. status = encode_attr_ctime(xdr, res->bitmap, &res->ctime);
  303. if (unlikely(status != 0))
  304. goto out;
  305. status = encode_attr_mtime(xdr, res->bitmap, &res->mtime);
  306. *savep = htonl((unsigned int)((char *)xdr->p - (char *)(savep+1)));
  307. out:
  308. dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
  309. return status;
  310. }
  311. static __be32 process_op(struct svc_rqst *rqstp,
  312. struct xdr_stream *xdr_in, void *argp,
  313. struct xdr_stream *xdr_out, void *resp)
  314. {
  315. struct callback_op *op = &callback_ops[0];
  316. unsigned int op_nr = OP_CB_ILLEGAL;
  317. __be32 status = 0;
  318. long maxlen;
  319. __be32 res;
  320. dprintk("%s: start\n", __func__);
  321. status = decode_op_hdr(xdr_in, &op_nr);
  322. if (likely(status == 0)) {
  323. switch (op_nr) {
  324. case OP_CB_GETATTR:
  325. case OP_CB_RECALL:
  326. op = &callback_ops[op_nr];
  327. break;
  328. default:
  329. op_nr = OP_CB_ILLEGAL;
  330. op = &callback_ops[0];
  331. status = htonl(NFS4ERR_OP_ILLEGAL);
  332. }
  333. }
  334. maxlen = xdr_out->end - xdr_out->p;
  335. if (maxlen > 0 && maxlen < PAGE_SIZE) {
  336. if (likely(status == 0 && op->decode_args != NULL))
  337. status = op->decode_args(rqstp, xdr_in, argp);
  338. if (likely(status == 0 && op->process_op != NULL))
  339. status = op->process_op(argp, resp);
  340. } else
  341. status = htonl(NFS4ERR_RESOURCE);
  342. res = encode_op_hdr(xdr_out, op_nr, status);
  343. if (status == 0)
  344. status = res;
  345. if (op->encode_res != NULL && status == 0)
  346. status = op->encode_res(rqstp, xdr_out, resp);
  347. dprintk("%s: done, status = %d\n", __func__, ntohl(status));
  348. return status;
  349. }
  350. /*
  351. * Decode, process and encode a COMPOUND
  352. */
  353. static __be32 nfs4_callback_compound(struct svc_rqst *rqstp, void *argp, void *resp)
  354. {
  355. struct cb_compound_hdr_arg hdr_arg = { 0 };
  356. struct cb_compound_hdr_res hdr_res = { NULL };
  357. struct xdr_stream xdr_in, xdr_out;
  358. __be32 *p;
  359. __be32 status;
  360. unsigned int nops = 0;
  361. dprintk("%s: start\n", __func__);
  362. xdr_init_decode(&xdr_in, &rqstp->rq_arg, rqstp->rq_arg.head[0].iov_base);
  363. p = (__be32*)((char *)rqstp->rq_res.head[0].iov_base + rqstp->rq_res.head[0].iov_len);
  364. xdr_init_encode(&xdr_out, &rqstp->rq_res, p);
  365. status = decode_compound_hdr_arg(&xdr_in, &hdr_arg);
  366. if (status == __constant_htonl(NFS4ERR_RESOURCE))
  367. return rpc_garbage_args;
  368. hdr_res.taglen = hdr_arg.taglen;
  369. hdr_res.tag = hdr_arg.tag;
  370. if (encode_compound_hdr_res(&xdr_out, &hdr_res) != 0)
  371. return rpc_system_err;
  372. while (status == 0 && nops != hdr_arg.nops) {
  373. status = process_op(rqstp, &xdr_in, argp, &xdr_out, resp);
  374. nops++;
  375. }
  376. *hdr_res.status = status;
  377. *hdr_res.nops = htonl(nops);
  378. dprintk("%s: done, status = %u\n", __func__, ntohl(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. };