callback_xdr.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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. #if defined(CONFIG_NFS_V4_1)
  22. #define CB_OP_SEQUENCE_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ + \
  23. 4 + 1 + 3)
  24. #define CB_OP_RECALLANY_RES_MAXSZ (CB_OP_HDR_RES_MAXSZ)
  25. #endif /* CONFIG_NFS_V4_1 */
  26. #define NFSDBG_FACILITY NFSDBG_CALLBACK
  27. /* Internal error code */
  28. #define NFS4ERR_RESOURCE_HDR 11050
  29. typedef __be32 (*callback_process_op_t)(void *, void *);
  30. typedef __be32 (*callback_decode_arg_t)(struct svc_rqst *, struct xdr_stream *, void *);
  31. typedef __be32 (*callback_encode_res_t)(struct svc_rqst *, struct xdr_stream *, void *);
  32. struct callback_op {
  33. callback_process_op_t process_op;
  34. callback_decode_arg_t decode_args;
  35. callback_encode_res_t encode_res;
  36. long res_maxsize;
  37. };
  38. static struct callback_op callback_ops[];
  39. static __be32 nfs4_callback_null(struct svc_rqst *rqstp, void *argp, void *resp)
  40. {
  41. return htonl(NFS4_OK);
  42. }
  43. static int nfs4_decode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
  44. {
  45. return xdr_argsize_check(rqstp, p);
  46. }
  47. static int nfs4_encode_void(struct svc_rqst *rqstp, __be32 *p, void *dummy)
  48. {
  49. return xdr_ressize_check(rqstp, p);
  50. }
  51. static __be32 *read_buf(struct xdr_stream *xdr, int nbytes)
  52. {
  53. __be32 *p;
  54. p = xdr_inline_decode(xdr, nbytes);
  55. if (unlikely(p == NULL))
  56. printk(KERN_WARNING "NFSv4 callback reply buffer overflowed!\n");
  57. return p;
  58. }
  59. static __be32 decode_string(struct xdr_stream *xdr, unsigned int *len, const char **str)
  60. {
  61. __be32 *p;
  62. p = read_buf(xdr, 4);
  63. if (unlikely(p == NULL))
  64. return htonl(NFS4ERR_RESOURCE);
  65. *len = ntohl(*p);
  66. if (*len != 0) {
  67. p = read_buf(xdr, *len);
  68. if (unlikely(p == NULL))
  69. return htonl(NFS4ERR_RESOURCE);
  70. *str = (const char *)p;
  71. } else
  72. *str = NULL;
  73. return 0;
  74. }
  75. static __be32 decode_fh(struct xdr_stream *xdr, struct nfs_fh *fh)
  76. {
  77. __be32 *p;
  78. p = read_buf(xdr, 4);
  79. if (unlikely(p == NULL))
  80. return htonl(NFS4ERR_RESOURCE);
  81. fh->size = ntohl(*p);
  82. if (fh->size > NFS4_FHSIZE)
  83. return htonl(NFS4ERR_BADHANDLE);
  84. p = read_buf(xdr, fh->size);
  85. if (unlikely(p == NULL))
  86. return htonl(NFS4ERR_RESOURCE);
  87. memcpy(&fh->data[0], p, fh->size);
  88. memset(&fh->data[fh->size], 0, sizeof(fh->data) - fh->size);
  89. return 0;
  90. }
  91. static __be32 decode_bitmap(struct xdr_stream *xdr, uint32_t *bitmap)
  92. {
  93. __be32 *p;
  94. unsigned int attrlen;
  95. p = read_buf(xdr, 4);
  96. if (unlikely(p == NULL))
  97. return htonl(NFS4ERR_RESOURCE);
  98. attrlen = ntohl(*p);
  99. p = read_buf(xdr, attrlen << 2);
  100. if (unlikely(p == NULL))
  101. return htonl(NFS4ERR_RESOURCE);
  102. if (likely(attrlen > 0))
  103. bitmap[0] = ntohl(*p++);
  104. if (attrlen > 1)
  105. bitmap[1] = ntohl(*p);
  106. return 0;
  107. }
  108. static __be32 decode_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
  109. {
  110. __be32 *p;
  111. p = read_buf(xdr, 16);
  112. if (unlikely(p == NULL))
  113. return htonl(NFS4ERR_RESOURCE);
  114. memcpy(stateid->data, p, 16);
  115. return 0;
  116. }
  117. static __be32 decode_compound_hdr_arg(struct xdr_stream *xdr, struct cb_compound_hdr_arg *hdr)
  118. {
  119. __be32 *p;
  120. __be32 status;
  121. status = decode_string(xdr, &hdr->taglen, &hdr->tag);
  122. if (unlikely(status != 0))
  123. return status;
  124. /* We do not like overly long tags! */
  125. if (hdr->taglen > CB_OP_TAGLEN_MAXSZ - 12) {
  126. printk("NFSv4 CALLBACK %s: client sent tag of length %u\n",
  127. __func__, hdr->taglen);
  128. return htonl(NFS4ERR_RESOURCE);
  129. }
  130. p = read_buf(xdr, 12);
  131. if (unlikely(p == NULL))
  132. return htonl(NFS4ERR_RESOURCE);
  133. hdr->minorversion = ntohl(*p++);
  134. /* Check minor version is zero or one. */
  135. if (hdr->minorversion <= 1) {
  136. p++; /* skip callback_ident */
  137. } else {
  138. printk(KERN_WARNING "%s: NFSv4 server callback with "
  139. "illegal minor version %u!\n",
  140. __func__, hdr->minorversion);
  141. return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
  142. }
  143. hdr->nops = ntohl(*p);
  144. dprintk("%s: minorversion %d nops %d\n", __func__,
  145. hdr->minorversion, hdr->nops);
  146. return 0;
  147. }
  148. static __be32 decode_op_hdr(struct xdr_stream *xdr, unsigned int *op)
  149. {
  150. __be32 *p;
  151. p = read_buf(xdr, 4);
  152. if (unlikely(p == NULL))
  153. return htonl(NFS4ERR_RESOURCE_HDR);
  154. *op = ntohl(*p);
  155. return 0;
  156. }
  157. static __be32 decode_getattr_args(struct svc_rqst *rqstp, struct xdr_stream *xdr, struct cb_getattrargs *args)
  158. {
  159. __be32 status;
  160. status = decode_fh(xdr, &args->fh);
  161. if (unlikely(status != 0))
  162. goto out;
  163. args->addr = svc_addr(rqstp);
  164. status = decode_bitmap(xdr, args->bitmap);
  165. out:
  166. dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
  167. return status;
  168. }
  169. static __be32 decode_recall_args(struct svc_rqst *rqstp, struct xdr_stream *xdr, struct cb_recallargs *args)
  170. {
  171. __be32 *p;
  172. __be32 status;
  173. args->addr = svc_addr(rqstp);
  174. status = decode_stateid(xdr, &args->stateid);
  175. if (unlikely(status != 0))
  176. goto out;
  177. p = read_buf(xdr, 4);
  178. if (unlikely(p == NULL)) {
  179. status = htonl(NFS4ERR_RESOURCE);
  180. goto out;
  181. }
  182. args->truncate = ntohl(*p);
  183. status = decode_fh(xdr, &args->fh);
  184. out:
  185. dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
  186. return status;
  187. }
  188. #if defined(CONFIG_NFS_V4_1)
  189. static unsigned decode_sessionid(struct xdr_stream *xdr,
  190. struct nfs4_sessionid *sid)
  191. {
  192. uint32_t *p;
  193. int len = NFS4_MAX_SESSIONID_LEN;
  194. p = read_buf(xdr, len);
  195. if (unlikely(p == NULL))
  196. return htonl(NFS4ERR_RESOURCE);
  197. memcpy(sid->data, p, len);
  198. return 0;
  199. }
  200. static unsigned decode_rc_list(struct xdr_stream *xdr,
  201. struct referring_call_list *rc_list)
  202. {
  203. uint32_t *p;
  204. int i;
  205. unsigned status;
  206. status = decode_sessionid(xdr, &rc_list->rcl_sessionid);
  207. if (status)
  208. goto out;
  209. status = htonl(NFS4ERR_RESOURCE);
  210. p = read_buf(xdr, sizeof(uint32_t));
  211. if (unlikely(p == NULL))
  212. goto out;
  213. rc_list->rcl_nrefcalls = ntohl(*p++);
  214. if (rc_list->rcl_nrefcalls) {
  215. p = read_buf(xdr,
  216. rc_list->rcl_nrefcalls * 2 * sizeof(uint32_t));
  217. if (unlikely(p == NULL))
  218. goto out;
  219. rc_list->rcl_refcalls = kmalloc(rc_list->rcl_nrefcalls *
  220. sizeof(*rc_list->rcl_refcalls),
  221. GFP_KERNEL);
  222. if (unlikely(rc_list->rcl_refcalls == NULL))
  223. goto out;
  224. for (i = 0; i < rc_list->rcl_nrefcalls; i++) {
  225. rc_list->rcl_refcalls[i].rc_sequenceid = ntohl(*p++);
  226. rc_list->rcl_refcalls[i].rc_slotid = ntohl(*p++);
  227. }
  228. }
  229. status = 0;
  230. out:
  231. return status;
  232. }
  233. static unsigned decode_cb_sequence_args(struct svc_rqst *rqstp,
  234. struct xdr_stream *xdr,
  235. struct cb_sequenceargs *args)
  236. {
  237. uint32_t *p;
  238. int i;
  239. unsigned status;
  240. status = decode_sessionid(xdr, &args->csa_sessionid);
  241. if (status)
  242. goto out;
  243. status = htonl(NFS4ERR_RESOURCE);
  244. p = read_buf(xdr, 5 * sizeof(uint32_t));
  245. if (unlikely(p == NULL))
  246. goto out;
  247. args->csa_addr = svc_addr(rqstp);
  248. args->csa_sequenceid = ntohl(*p++);
  249. args->csa_slotid = ntohl(*p++);
  250. args->csa_highestslotid = ntohl(*p++);
  251. args->csa_cachethis = ntohl(*p++);
  252. args->csa_nrclists = ntohl(*p++);
  253. args->csa_rclists = NULL;
  254. if (args->csa_nrclists) {
  255. args->csa_rclists = kmalloc(args->csa_nrclists *
  256. sizeof(*args->csa_rclists),
  257. GFP_KERNEL);
  258. if (unlikely(args->csa_rclists == NULL))
  259. goto out;
  260. for (i = 0; i < args->csa_nrclists; i++) {
  261. status = decode_rc_list(xdr, &args->csa_rclists[i]);
  262. if (status)
  263. goto out_free;
  264. }
  265. }
  266. status = 0;
  267. dprintk("%s: sessionid %x:%x:%x:%x sequenceid %u slotid %u "
  268. "highestslotid %u cachethis %d nrclists %u\n",
  269. __func__,
  270. ((u32 *)&args->csa_sessionid)[0],
  271. ((u32 *)&args->csa_sessionid)[1],
  272. ((u32 *)&args->csa_sessionid)[2],
  273. ((u32 *)&args->csa_sessionid)[3],
  274. args->csa_sequenceid, args->csa_slotid,
  275. args->csa_highestslotid, args->csa_cachethis,
  276. args->csa_nrclists);
  277. out:
  278. dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
  279. return status;
  280. out_free:
  281. for (i = 0; i < args->csa_nrclists; i++)
  282. kfree(args->csa_rclists[i].rcl_refcalls);
  283. kfree(args->csa_rclists);
  284. goto out;
  285. }
  286. static unsigned decode_recallany_args(struct svc_rqst *rqstp,
  287. struct xdr_stream *xdr,
  288. struct cb_recallanyargs *args)
  289. {
  290. uint32_t *p;
  291. args->craa_addr = svc_addr(rqstp);
  292. p = read_buf(xdr, 4);
  293. if (unlikely(p == NULL))
  294. return htonl(NFS4ERR_BADXDR);
  295. args->craa_objs_to_keep = ntohl(*p++);
  296. p = read_buf(xdr, 4);
  297. if (unlikely(p == NULL))
  298. return htonl(NFS4ERR_BADXDR);
  299. args->craa_type_mask = ntohl(*p);
  300. return 0;
  301. }
  302. #endif /* CONFIG_NFS_V4_1 */
  303. static __be32 encode_string(struct xdr_stream *xdr, unsigned int len, const char *str)
  304. {
  305. __be32 *p;
  306. p = xdr_reserve_space(xdr, 4 + len);
  307. if (unlikely(p == NULL))
  308. return htonl(NFS4ERR_RESOURCE);
  309. xdr_encode_opaque(p, str, len);
  310. return 0;
  311. }
  312. #define CB_SUPPORTED_ATTR0 (FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE)
  313. #define CB_SUPPORTED_ATTR1 (FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY)
  314. static __be32 encode_attr_bitmap(struct xdr_stream *xdr, const uint32_t *bitmap, __be32 **savep)
  315. {
  316. __be32 bm[2];
  317. __be32 *p;
  318. bm[0] = htonl(bitmap[0] & CB_SUPPORTED_ATTR0);
  319. bm[1] = htonl(bitmap[1] & CB_SUPPORTED_ATTR1);
  320. if (bm[1] != 0) {
  321. p = xdr_reserve_space(xdr, 16);
  322. if (unlikely(p == NULL))
  323. return htonl(NFS4ERR_RESOURCE);
  324. *p++ = htonl(2);
  325. *p++ = bm[0];
  326. *p++ = bm[1];
  327. } else if (bm[0] != 0) {
  328. p = xdr_reserve_space(xdr, 12);
  329. if (unlikely(p == NULL))
  330. return htonl(NFS4ERR_RESOURCE);
  331. *p++ = htonl(1);
  332. *p++ = bm[0];
  333. } else {
  334. p = xdr_reserve_space(xdr, 8);
  335. if (unlikely(p == NULL))
  336. return htonl(NFS4ERR_RESOURCE);
  337. *p++ = htonl(0);
  338. }
  339. *savep = p;
  340. return 0;
  341. }
  342. static __be32 encode_attr_change(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t change)
  343. {
  344. __be32 *p;
  345. if (!(bitmap[0] & FATTR4_WORD0_CHANGE))
  346. return 0;
  347. p = xdr_reserve_space(xdr, 8);
  348. if (unlikely(!p))
  349. return htonl(NFS4ERR_RESOURCE);
  350. p = xdr_encode_hyper(p, change);
  351. return 0;
  352. }
  353. static __be32 encode_attr_size(struct xdr_stream *xdr, const uint32_t *bitmap, uint64_t size)
  354. {
  355. __be32 *p;
  356. if (!(bitmap[0] & FATTR4_WORD0_SIZE))
  357. return 0;
  358. p = xdr_reserve_space(xdr, 8);
  359. if (unlikely(!p))
  360. return htonl(NFS4ERR_RESOURCE);
  361. p = xdr_encode_hyper(p, size);
  362. return 0;
  363. }
  364. static __be32 encode_attr_time(struct xdr_stream *xdr, const struct timespec *time)
  365. {
  366. __be32 *p;
  367. p = xdr_reserve_space(xdr, 12);
  368. if (unlikely(!p))
  369. return htonl(NFS4ERR_RESOURCE);
  370. p = xdr_encode_hyper(p, time->tv_sec);
  371. *p = htonl(time->tv_nsec);
  372. return 0;
  373. }
  374. static __be32 encode_attr_ctime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
  375. {
  376. if (!(bitmap[1] & FATTR4_WORD1_TIME_METADATA))
  377. return 0;
  378. return encode_attr_time(xdr,time);
  379. }
  380. static __be32 encode_attr_mtime(struct xdr_stream *xdr, const uint32_t *bitmap, const struct timespec *time)
  381. {
  382. if (!(bitmap[1] & FATTR4_WORD1_TIME_MODIFY))
  383. return 0;
  384. return encode_attr_time(xdr,time);
  385. }
  386. static __be32 encode_compound_hdr_res(struct xdr_stream *xdr, struct cb_compound_hdr_res *hdr)
  387. {
  388. __be32 status;
  389. hdr->status = xdr_reserve_space(xdr, 4);
  390. if (unlikely(hdr->status == NULL))
  391. return htonl(NFS4ERR_RESOURCE);
  392. status = encode_string(xdr, hdr->taglen, hdr->tag);
  393. if (unlikely(status != 0))
  394. return status;
  395. hdr->nops = xdr_reserve_space(xdr, 4);
  396. if (unlikely(hdr->nops == NULL))
  397. return htonl(NFS4ERR_RESOURCE);
  398. return 0;
  399. }
  400. static __be32 encode_op_hdr(struct xdr_stream *xdr, uint32_t op, __be32 res)
  401. {
  402. __be32 *p;
  403. p = xdr_reserve_space(xdr, 8);
  404. if (unlikely(p == NULL))
  405. return htonl(NFS4ERR_RESOURCE_HDR);
  406. *p++ = htonl(op);
  407. *p = res;
  408. return 0;
  409. }
  410. static __be32 encode_getattr_res(struct svc_rqst *rqstp, struct xdr_stream *xdr, const struct cb_getattrres *res)
  411. {
  412. __be32 *savep = NULL;
  413. __be32 status = res->status;
  414. if (unlikely(status != 0))
  415. goto out;
  416. status = encode_attr_bitmap(xdr, res->bitmap, &savep);
  417. if (unlikely(status != 0))
  418. goto out;
  419. status = encode_attr_change(xdr, res->bitmap, res->change_attr);
  420. if (unlikely(status != 0))
  421. goto out;
  422. status = encode_attr_size(xdr, res->bitmap, res->size);
  423. if (unlikely(status != 0))
  424. goto out;
  425. status = encode_attr_ctime(xdr, res->bitmap, &res->ctime);
  426. if (unlikely(status != 0))
  427. goto out;
  428. status = encode_attr_mtime(xdr, res->bitmap, &res->mtime);
  429. *savep = htonl((unsigned int)((char *)xdr->p - (char *)(savep+1)));
  430. out:
  431. dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
  432. return status;
  433. }
  434. #if defined(CONFIG_NFS_V4_1)
  435. static unsigned encode_sessionid(struct xdr_stream *xdr,
  436. const struct nfs4_sessionid *sid)
  437. {
  438. uint32_t *p;
  439. int len = NFS4_MAX_SESSIONID_LEN;
  440. p = xdr_reserve_space(xdr, len);
  441. if (unlikely(p == NULL))
  442. return htonl(NFS4ERR_RESOURCE);
  443. memcpy(p, sid, len);
  444. return 0;
  445. }
  446. static unsigned encode_cb_sequence_res(struct svc_rqst *rqstp,
  447. struct xdr_stream *xdr,
  448. const struct cb_sequenceres *res)
  449. {
  450. uint32_t *p;
  451. unsigned status = res->csr_status;
  452. if (unlikely(status != 0))
  453. goto out;
  454. encode_sessionid(xdr, &res->csr_sessionid);
  455. p = xdr_reserve_space(xdr, 4 * sizeof(uint32_t));
  456. if (unlikely(p == NULL))
  457. return htonl(NFS4ERR_RESOURCE);
  458. *p++ = htonl(res->csr_sequenceid);
  459. *p++ = htonl(res->csr_slotid);
  460. *p++ = htonl(res->csr_highestslotid);
  461. *p++ = htonl(res->csr_target_highestslotid);
  462. out:
  463. dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
  464. return status;
  465. }
  466. static __be32
  467. preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
  468. {
  469. if (op_nr == OP_CB_SEQUENCE) {
  470. if (nop != 0)
  471. return htonl(NFS4ERR_SEQUENCE_POS);
  472. } else {
  473. if (nop == 0)
  474. return htonl(NFS4ERR_OP_NOT_IN_SESSION);
  475. }
  476. switch (op_nr) {
  477. case OP_CB_GETATTR:
  478. case OP_CB_RECALL:
  479. case OP_CB_SEQUENCE:
  480. case OP_CB_RECALL_ANY:
  481. *op = &callback_ops[op_nr];
  482. break;
  483. case OP_CB_LAYOUTRECALL:
  484. case OP_CB_NOTIFY_DEVICEID:
  485. case OP_CB_NOTIFY:
  486. case OP_CB_PUSH_DELEG:
  487. case OP_CB_RECALLABLE_OBJ_AVAIL:
  488. case OP_CB_RECALL_SLOT:
  489. case OP_CB_WANTS_CANCELLED:
  490. case OP_CB_NOTIFY_LOCK:
  491. return htonl(NFS4ERR_NOTSUPP);
  492. default:
  493. return htonl(NFS4ERR_OP_ILLEGAL);
  494. }
  495. return htonl(NFS_OK);
  496. }
  497. #else /* CONFIG_NFS_V4_1 */
  498. static __be32
  499. preprocess_nfs41_op(int nop, unsigned int op_nr, struct callback_op **op)
  500. {
  501. return htonl(NFS4ERR_MINOR_VERS_MISMATCH);
  502. }
  503. #endif /* CONFIG_NFS_V4_1 */
  504. static __be32
  505. preprocess_nfs4_op(unsigned int op_nr, struct callback_op **op)
  506. {
  507. switch (op_nr) {
  508. case OP_CB_GETATTR:
  509. case OP_CB_RECALL:
  510. *op = &callback_ops[op_nr];
  511. break;
  512. default:
  513. return htonl(NFS4ERR_OP_ILLEGAL);
  514. }
  515. return htonl(NFS_OK);
  516. }
  517. static __be32 process_op(uint32_t minorversion, int nop,
  518. struct svc_rqst *rqstp,
  519. struct xdr_stream *xdr_in, void *argp,
  520. struct xdr_stream *xdr_out, void *resp)
  521. {
  522. struct callback_op *op = &callback_ops[0];
  523. unsigned int op_nr;
  524. __be32 status;
  525. long maxlen;
  526. __be32 res;
  527. dprintk("%s: start\n", __func__);
  528. status = decode_op_hdr(xdr_in, &op_nr);
  529. if (unlikely(status))
  530. return status;
  531. dprintk("%s: minorversion=%d nop=%d op_nr=%u\n",
  532. __func__, minorversion, nop, op_nr);
  533. status = minorversion ? preprocess_nfs41_op(nop, op_nr, &op) :
  534. preprocess_nfs4_op(op_nr, &op);
  535. if (status == htonl(NFS4ERR_OP_ILLEGAL))
  536. op_nr = OP_CB_ILLEGAL;
  537. if (status)
  538. goto encode_hdr;
  539. maxlen = xdr_out->end - xdr_out->p;
  540. if (maxlen > 0 && maxlen < PAGE_SIZE) {
  541. status = op->decode_args(rqstp, xdr_in, argp);
  542. if (likely(status == 0))
  543. status = op->process_op(argp, resp);
  544. } else
  545. status = htonl(NFS4ERR_RESOURCE);
  546. encode_hdr:
  547. res = encode_op_hdr(xdr_out, op_nr, status);
  548. if (unlikely(res))
  549. return res;
  550. if (op->encode_res != NULL && status == 0)
  551. status = op->encode_res(rqstp, xdr_out, resp);
  552. dprintk("%s: done, status = %d\n", __func__, ntohl(status));
  553. return status;
  554. }
  555. /*
  556. * Decode, process and encode a COMPOUND
  557. */
  558. static __be32 nfs4_callback_compound(struct svc_rqst *rqstp, void *argp, void *resp)
  559. {
  560. struct cb_compound_hdr_arg hdr_arg = { 0 };
  561. struct cb_compound_hdr_res hdr_res = { NULL };
  562. struct xdr_stream xdr_in, xdr_out;
  563. __be32 *p;
  564. __be32 status;
  565. unsigned int nops = 0;
  566. dprintk("%s: start\n", __func__);
  567. xdr_init_decode(&xdr_in, &rqstp->rq_arg, rqstp->rq_arg.head[0].iov_base);
  568. p = (__be32*)((char *)rqstp->rq_res.head[0].iov_base + rqstp->rq_res.head[0].iov_len);
  569. xdr_init_encode(&xdr_out, &rqstp->rq_res, p);
  570. status = decode_compound_hdr_arg(&xdr_in, &hdr_arg);
  571. if (status == __constant_htonl(NFS4ERR_RESOURCE))
  572. return rpc_garbage_args;
  573. hdr_res.taglen = hdr_arg.taglen;
  574. hdr_res.tag = hdr_arg.tag;
  575. if (encode_compound_hdr_res(&xdr_out, &hdr_res) != 0)
  576. return rpc_system_err;
  577. while (status == 0 && nops != hdr_arg.nops) {
  578. status = process_op(hdr_arg.minorversion, nops,
  579. rqstp, &xdr_in, argp, &xdr_out, resp);
  580. nops++;
  581. }
  582. /* Buffer overflow in decode_ops_hdr or encode_ops_hdr. Return
  583. * resource error in cb_compound status without returning op */
  584. if (unlikely(status == htonl(NFS4ERR_RESOURCE_HDR))) {
  585. status = htonl(NFS4ERR_RESOURCE);
  586. nops--;
  587. }
  588. *hdr_res.status = status;
  589. *hdr_res.nops = htonl(nops);
  590. dprintk("%s: done, status = %u\n", __func__, ntohl(status));
  591. return rpc_success;
  592. }
  593. /*
  594. * Define NFS4 callback COMPOUND ops.
  595. */
  596. static struct callback_op callback_ops[] = {
  597. [0] = {
  598. .res_maxsize = CB_OP_HDR_RES_MAXSZ,
  599. },
  600. [OP_CB_GETATTR] = {
  601. .process_op = (callback_process_op_t)nfs4_callback_getattr,
  602. .decode_args = (callback_decode_arg_t)decode_getattr_args,
  603. .encode_res = (callback_encode_res_t)encode_getattr_res,
  604. .res_maxsize = CB_OP_GETATTR_RES_MAXSZ,
  605. },
  606. [OP_CB_RECALL] = {
  607. .process_op = (callback_process_op_t)nfs4_callback_recall,
  608. .decode_args = (callback_decode_arg_t)decode_recall_args,
  609. .res_maxsize = CB_OP_RECALL_RES_MAXSZ,
  610. },
  611. #if defined(CONFIG_NFS_V4_1)
  612. [OP_CB_SEQUENCE] = {
  613. .process_op = (callback_process_op_t)nfs4_callback_sequence,
  614. .decode_args = (callback_decode_arg_t)decode_cb_sequence_args,
  615. .encode_res = (callback_encode_res_t)encode_cb_sequence_res,
  616. .res_maxsize = CB_OP_SEQUENCE_RES_MAXSZ,
  617. },
  618. [OP_CB_RECALL_ANY] = {
  619. .process_op = (callback_process_op_t)nfs4_callback_recallany,
  620. .decode_args = (callback_decode_arg_t)decode_recallany_args,
  621. .res_maxsize = CB_OP_RECALLANY_RES_MAXSZ,
  622. },
  623. #endif /* CONFIG_NFS_V4_1 */
  624. };
  625. /*
  626. * Define NFS4 callback procedures
  627. */
  628. static struct svc_procedure nfs4_callback_procedures1[] = {
  629. [CB_NULL] = {
  630. .pc_func = nfs4_callback_null,
  631. .pc_decode = (kxdrproc_t)nfs4_decode_void,
  632. .pc_encode = (kxdrproc_t)nfs4_encode_void,
  633. .pc_xdrressize = 1,
  634. },
  635. [CB_COMPOUND] = {
  636. .pc_func = nfs4_callback_compound,
  637. .pc_encode = (kxdrproc_t)nfs4_encode_void,
  638. .pc_argsize = 256,
  639. .pc_ressize = 256,
  640. .pc_xdrressize = NFS4_CALLBACK_BUFSIZE,
  641. }
  642. };
  643. struct svc_version nfs4_callback_version1 = {
  644. .vs_vers = 1,
  645. .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
  646. .vs_proc = nfs4_callback_procedures1,
  647. .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
  648. .vs_dispatch = NULL,
  649. };
  650. struct svc_version nfs4_callback_version4 = {
  651. .vs_vers = 4,
  652. .vs_nproc = ARRAY_SIZE(nfs4_callback_procedures1),
  653. .vs_proc = nfs4_callback_procedures1,
  654. .vs_xdrsize = NFS4_CALLBACK_XDRSIZE,
  655. .vs_dispatch = NULL,
  656. };