callback_xdr.c 19 KB

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