callback_xdr.c 20 KB

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