callback_xdr.c 20 KB

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