nfs42xdr.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. /*
  2. * Copyright (c) 2014 Anna Schumaker <Anna.Schumaker@Netapp.com>
  3. */
  4. #ifndef __LINUX_FS_NFS_NFS4_2XDR_H
  5. #define __LINUX_FS_NFS_NFS4_2XDR_H
  6. #include "nfs42.h"
  7. #define encode_fallocate_maxsz (encode_stateid_maxsz + \
  8. 2 /* offset */ + \
  9. 2 /* length */)
  10. #define NFS42_WRITE_RES_SIZE (1 /* wr_callback_id size */ +\
  11. XDR_QUADLEN(NFS4_STATEID_SIZE) + \
  12. 2 /* wr_count */ + \
  13. 1 /* wr_committed */ + \
  14. XDR_QUADLEN(NFS4_VERIFIER_SIZE))
  15. #define encode_allocate_maxsz (op_encode_hdr_maxsz + \
  16. encode_fallocate_maxsz)
  17. #define decode_allocate_maxsz (op_decode_hdr_maxsz)
  18. #define encode_copy_maxsz (op_encode_hdr_maxsz + \
  19. XDR_QUADLEN(NFS4_STATEID_SIZE) + \
  20. XDR_QUADLEN(NFS4_STATEID_SIZE) + \
  21. 2 + 2 + 2 + 1 + 1 + 1)
  22. #define decode_copy_maxsz (op_decode_hdr_maxsz + \
  23. NFS42_WRITE_RES_SIZE + \
  24. 1 /* cr_consecutive */ + \
  25. 1 /* cr_synchronous */)
  26. #define encode_deallocate_maxsz (op_encode_hdr_maxsz + \
  27. encode_fallocate_maxsz)
  28. #define decode_deallocate_maxsz (op_decode_hdr_maxsz)
  29. #define encode_seek_maxsz (op_encode_hdr_maxsz + \
  30. encode_stateid_maxsz + \
  31. 2 /* offset */ + \
  32. 1 /* whence */)
  33. #define decode_seek_maxsz (op_decode_hdr_maxsz + \
  34. 1 /* eof */ + \
  35. 1 /* whence */ + \
  36. 2 /* offset */ + \
  37. 2 /* length */)
  38. #define encode_io_info_maxsz 4
  39. #define encode_layoutstats_maxsz (op_decode_hdr_maxsz + \
  40. 2 /* offset */ + \
  41. 2 /* length */ + \
  42. encode_stateid_maxsz + \
  43. encode_io_info_maxsz + \
  44. encode_io_info_maxsz + \
  45. 1 /* opaque devaddr4 length */ + \
  46. XDR_QUADLEN(PNFS_LAYOUTSTATS_MAXSIZE))
  47. #define decode_layoutstats_maxsz (op_decode_hdr_maxsz)
  48. #define encode_clone_maxsz (encode_stateid_maxsz + \
  49. encode_stateid_maxsz + \
  50. 2 /* src offset */ + \
  51. 2 /* dst offset */ + \
  52. 2 /* count */)
  53. #define decode_clone_maxsz (op_decode_hdr_maxsz)
  54. #define NFS4_enc_allocate_sz (compound_encode_hdr_maxsz + \
  55. encode_putfh_maxsz + \
  56. encode_allocate_maxsz + \
  57. encode_getattr_maxsz)
  58. #define NFS4_dec_allocate_sz (compound_decode_hdr_maxsz + \
  59. decode_putfh_maxsz + \
  60. decode_allocate_maxsz + \
  61. decode_getattr_maxsz)
  62. #define NFS4_enc_copy_sz (compound_encode_hdr_maxsz + \
  63. encode_putfh_maxsz + \
  64. encode_savefh_maxsz + \
  65. encode_putfh_maxsz + \
  66. encode_copy_maxsz)
  67. #define NFS4_dec_copy_sz (compound_decode_hdr_maxsz + \
  68. decode_putfh_maxsz + \
  69. decode_savefh_maxsz + \
  70. decode_putfh_maxsz + \
  71. decode_copy_maxsz)
  72. #define NFS4_enc_deallocate_sz (compound_encode_hdr_maxsz + \
  73. encode_putfh_maxsz + \
  74. encode_deallocate_maxsz + \
  75. encode_getattr_maxsz)
  76. #define NFS4_dec_deallocate_sz (compound_decode_hdr_maxsz + \
  77. decode_putfh_maxsz + \
  78. decode_deallocate_maxsz + \
  79. decode_getattr_maxsz)
  80. #define NFS4_enc_seek_sz (compound_encode_hdr_maxsz + \
  81. encode_putfh_maxsz + \
  82. encode_seek_maxsz)
  83. #define NFS4_dec_seek_sz (compound_decode_hdr_maxsz + \
  84. decode_putfh_maxsz + \
  85. decode_seek_maxsz)
  86. #define NFS4_enc_layoutstats_sz (compound_encode_hdr_maxsz + \
  87. encode_sequence_maxsz + \
  88. encode_putfh_maxsz + \
  89. PNFS_LAYOUTSTATS_MAXDEV * encode_layoutstats_maxsz)
  90. #define NFS4_dec_layoutstats_sz (compound_decode_hdr_maxsz + \
  91. decode_sequence_maxsz + \
  92. decode_putfh_maxsz + \
  93. PNFS_LAYOUTSTATS_MAXDEV * decode_layoutstats_maxsz)
  94. #define NFS4_enc_clone_sz (compound_encode_hdr_maxsz + \
  95. encode_sequence_maxsz + \
  96. encode_putfh_maxsz + \
  97. encode_savefh_maxsz + \
  98. encode_putfh_maxsz + \
  99. encode_clone_maxsz + \
  100. encode_getattr_maxsz)
  101. #define NFS4_dec_clone_sz (compound_decode_hdr_maxsz + \
  102. decode_sequence_maxsz + \
  103. decode_putfh_maxsz + \
  104. decode_savefh_maxsz + \
  105. decode_putfh_maxsz + \
  106. decode_clone_maxsz + \
  107. decode_getattr_maxsz)
  108. static void encode_fallocate(struct xdr_stream *xdr,
  109. struct nfs42_falloc_args *args)
  110. {
  111. encode_nfs4_stateid(xdr, &args->falloc_stateid);
  112. encode_uint64(xdr, args->falloc_offset);
  113. encode_uint64(xdr, args->falloc_length);
  114. }
  115. static void encode_allocate(struct xdr_stream *xdr,
  116. struct nfs42_falloc_args *args,
  117. struct compound_hdr *hdr)
  118. {
  119. encode_op_hdr(xdr, OP_ALLOCATE, decode_allocate_maxsz, hdr);
  120. encode_fallocate(xdr, args);
  121. }
  122. static void encode_copy(struct xdr_stream *xdr,
  123. struct nfs42_copy_args *args,
  124. struct compound_hdr *hdr)
  125. {
  126. encode_op_hdr(xdr, OP_COPY, decode_copy_maxsz, hdr);
  127. encode_nfs4_stateid(xdr, &args->src_stateid);
  128. encode_nfs4_stateid(xdr, &args->dst_stateid);
  129. encode_uint64(xdr, args->src_pos);
  130. encode_uint64(xdr, args->dst_pos);
  131. encode_uint64(xdr, args->count);
  132. encode_uint32(xdr, 1); /* consecutive = true */
  133. encode_uint32(xdr, 1); /* synchronous = true */
  134. encode_uint32(xdr, 0); /* src server list */
  135. }
  136. static void encode_deallocate(struct xdr_stream *xdr,
  137. struct nfs42_falloc_args *args,
  138. struct compound_hdr *hdr)
  139. {
  140. encode_op_hdr(xdr, OP_DEALLOCATE, decode_deallocate_maxsz, hdr);
  141. encode_fallocate(xdr, args);
  142. }
  143. static void encode_seek(struct xdr_stream *xdr,
  144. struct nfs42_seek_args *args,
  145. struct compound_hdr *hdr)
  146. {
  147. encode_op_hdr(xdr, OP_SEEK, decode_seek_maxsz, hdr);
  148. encode_nfs4_stateid(xdr, &args->sa_stateid);
  149. encode_uint64(xdr, args->sa_offset);
  150. encode_uint32(xdr, args->sa_what);
  151. }
  152. static void encode_layoutstats(struct xdr_stream *xdr,
  153. struct nfs42_layoutstat_args *args,
  154. struct nfs42_layoutstat_devinfo *devinfo,
  155. struct compound_hdr *hdr)
  156. {
  157. __be32 *p;
  158. encode_op_hdr(xdr, OP_LAYOUTSTATS, decode_layoutstats_maxsz, hdr);
  159. p = reserve_space(xdr, 8 + 8);
  160. p = xdr_encode_hyper(p, devinfo->offset);
  161. p = xdr_encode_hyper(p, devinfo->length);
  162. encode_nfs4_stateid(xdr, &args->stateid);
  163. p = reserve_space(xdr, 4*8 + NFS4_DEVICEID4_SIZE + 4);
  164. p = xdr_encode_hyper(p, devinfo->read_count);
  165. p = xdr_encode_hyper(p, devinfo->read_bytes);
  166. p = xdr_encode_hyper(p, devinfo->write_count);
  167. p = xdr_encode_hyper(p, devinfo->write_bytes);
  168. p = xdr_encode_opaque_fixed(p, devinfo->dev_id.data,
  169. NFS4_DEVICEID4_SIZE);
  170. /* Encode layoutupdate4 */
  171. *p++ = cpu_to_be32(devinfo->layout_type);
  172. if (devinfo->layoutstats_encode != NULL)
  173. devinfo->layoutstats_encode(xdr, args, devinfo);
  174. else
  175. encode_uint32(xdr, 0);
  176. }
  177. static void encode_clone(struct xdr_stream *xdr,
  178. struct nfs42_clone_args *args,
  179. struct compound_hdr *hdr)
  180. {
  181. __be32 *p;
  182. encode_op_hdr(xdr, OP_CLONE, decode_clone_maxsz, hdr);
  183. encode_nfs4_stateid(xdr, &args->src_stateid);
  184. encode_nfs4_stateid(xdr, &args->dst_stateid);
  185. p = reserve_space(xdr, 3*8);
  186. p = xdr_encode_hyper(p, args->src_offset);
  187. p = xdr_encode_hyper(p, args->dst_offset);
  188. xdr_encode_hyper(p, args->count);
  189. }
  190. /*
  191. * Encode ALLOCATE request
  192. */
  193. static void nfs4_xdr_enc_allocate(struct rpc_rqst *req,
  194. struct xdr_stream *xdr,
  195. struct nfs42_falloc_args *args)
  196. {
  197. struct compound_hdr hdr = {
  198. .minorversion = nfs4_xdr_minorversion(&args->seq_args),
  199. };
  200. encode_compound_hdr(xdr, req, &hdr);
  201. encode_sequence(xdr, &args->seq_args, &hdr);
  202. encode_putfh(xdr, args->falloc_fh, &hdr);
  203. encode_allocate(xdr, args, &hdr);
  204. encode_getfattr(xdr, args->falloc_bitmask, &hdr);
  205. encode_nops(&hdr);
  206. }
  207. /*
  208. * Encode COPY request
  209. */
  210. static void nfs4_xdr_enc_copy(struct rpc_rqst *req,
  211. struct xdr_stream *xdr,
  212. struct nfs42_copy_args *args)
  213. {
  214. struct compound_hdr hdr = {
  215. .minorversion = nfs4_xdr_minorversion(&args->seq_args),
  216. };
  217. encode_compound_hdr(xdr, req, &hdr);
  218. encode_sequence(xdr, &args->seq_args, &hdr);
  219. encode_putfh(xdr, args->src_fh, &hdr);
  220. encode_savefh(xdr, &hdr);
  221. encode_putfh(xdr, args->dst_fh, &hdr);
  222. encode_copy(xdr, args, &hdr);
  223. encode_nops(&hdr);
  224. }
  225. /*
  226. * Encode DEALLOCATE request
  227. */
  228. static void nfs4_xdr_enc_deallocate(struct rpc_rqst *req,
  229. struct xdr_stream *xdr,
  230. struct nfs42_falloc_args *args)
  231. {
  232. struct compound_hdr hdr = {
  233. .minorversion = nfs4_xdr_minorversion(&args->seq_args),
  234. };
  235. encode_compound_hdr(xdr, req, &hdr);
  236. encode_sequence(xdr, &args->seq_args, &hdr);
  237. encode_putfh(xdr, args->falloc_fh, &hdr);
  238. encode_deallocate(xdr, args, &hdr);
  239. encode_getfattr(xdr, args->falloc_bitmask, &hdr);
  240. encode_nops(&hdr);
  241. }
  242. /*
  243. * Encode SEEK request
  244. */
  245. static void nfs4_xdr_enc_seek(struct rpc_rqst *req,
  246. struct xdr_stream *xdr,
  247. struct nfs42_seek_args *args)
  248. {
  249. struct compound_hdr hdr = {
  250. .minorversion = nfs4_xdr_minorversion(&args->seq_args),
  251. };
  252. encode_compound_hdr(xdr, req, &hdr);
  253. encode_sequence(xdr, &args->seq_args, &hdr);
  254. encode_putfh(xdr, args->sa_fh, &hdr);
  255. encode_seek(xdr, args, &hdr);
  256. encode_nops(&hdr);
  257. }
  258. /*
  259. * Encode LAYOUTSTATS request
  260. */
  261. static void nfs4_xdr_enc_layoutstats(struct rpc_rqst *req,
  262. struct xdr_stream *xdr,
  263. struct nfs42_layoutstat_args *args)
  264. {
  265. int i;
  266. struct compound_hdr hdr = {
  267. .minorversion = nfs4_xdr_minorversion(&args->seq_args),
  268. };
  269. encode_compound_hdr(xdr, req, &hdr);
  270. encode_sequence(xdr, &args->seq_args, &hdr);
  271. encode_putfh(xdr, args->fh, &hdr);
  272. WARN_ON(args->num_dev > PNFS_LAYOUTSTATS_MAXDEV);
  273. for (i = 0; i < args->num_dev; i++)
  274. encode_layoutstats(xdr, args, &args->devinfo[i], &hdr);
  275. encode_nops(&hdr);
  276. }
  277. /*
  278. * Encode CLONE request
  279. */
  280. static void nfs4_xdr_enc_clone(struct rpc_rqst *req,
  281. struct xdr_stream *xdr,
  282. struct nfs42_clone_args *args)
  283. {
  284. struct compound_hdr hdr = {
  285. .minorversion = nfs4_xdr_minorversion(&args->seq_args),
  286. };
  287. encode_compound_hdr(xdr, req, &hdr);
  288. encode_sequence(xdr, &args->seq_args, &hdr);
  289. encode_putfh(xdr, args->src_fh, &hdr);
  290. encode_savefh(xdr, &hdr);
  291. encode_putfh(xdr, args->dst_fh, &hdr);
  292. encode_clone(xdr, args, &hdr);
  293. encode_getfattr(xdr, args->dst_bitmask, &hdr);
  294. encode_nops(&hdr);
  295. }
  296. static int decode_allocate(struct xdr_stream *xdr, struct nfs42_falloc_res *res)
  297. {
  298. return decode_op_hdr(xdr, OP_ALLOCATE);
  299. }
  300. static int decode_write_response(struct xdr_stream *xdr,
  301. struct nfs42_write_res *res)
  302. {
  303. __be32 *p;
  304. p = xdr_inline_decode(xdr, 4 + 8 + 4);
  305. if (unlikely(!p))
  306. goto out_overflow;
  307. /*
  308. * We never use asynchronous mode, so warn if a server returns
  309. * a stateid.
  310. */
  311. if (unlikely(*p != 0)) {
  312. pr_err_once("%s: server has set unrequested "
  313. "asynchronous mode\n", __func__);
  314. return -EREMOTEIO;
  315. }
  316. p++;
  317. p = xdr_decode_hyper(p, &res->count);
  318. res->verifier.committed = be32_to_cpup(p);
  319. return decode_verifier(xdr, &res->verifier.verifier);
  320. out_overflow:
  321. print_overflow_msg(__func__, xdr);
  322. return -EIO;
  323. }
  324. static int decode_copy_requirements(struct xdr_stream *xdr,
  325. struct nfs42_copy_res *res) {
  326. __be32 *p;
  327. p = xdr_inline_decode(xdr, 4 + 4);
  328. if (unlikely(!p))
  329. goto out_overflow;
  330. res->consecutive = be32_to_cpup(p++);
  331. res->synchronous = be32_to_cpup(p++);
  332. return 0;
  333. out_overflow:
  334. print_overflow_msg(__func__, xdr);
  335. return -EIO;
  336. }
  337. static int decode_copy(struct xdr_stream *xdr, struct nfs42_copy_res *res)
  338. {
  339. int status;
  340. status = decode_op_hdr(xdr, OP_COPY);
  341. if (status == NFS4ERR_OFFLOAD_NO_REQS) {
  342. status = decode_copy_requirements(xdr, res);
  343. if (status)
  344. return status;
  345. return NFS4ERR_OFFLOAD_NO_REQS;
  346. } else if (status)
  347. return status;
  348. status = decode_write_response(xdr, &res->write_res);
  349. if (status)
  350. return status;
  351. return decode_copy_requirements(xdr, res);
  352. }
  353. static int decode_deallocate(struct xdr_stream *xdr, struct nfs42_falloc_res *res)
  354. {
  355. return decode_op_hdr(xdr, OP_DEALLOCATE);
  356. }
  357. static int decode_seek(struct xdr_stream *xdr, struct nfs42_seek_res *res)
  358. {
  359. int status;
  360. __be32 *p;
  361. status = decode_op_hdr(xdr, OP_SEEK);
  362. if (status)
  363. return status;
  364. p = xdr_inline_decode(xdr, 4 + 8);
  365. if (unlikely(!p))
  366. goto out_overflow;
  367. res->sr_eof = be32_to_cpup(p++);
  368. p = xdr_decode_hyper(p, &res->sr_offset);
  369. return 0;
  370. out_overflow:
  371. print_overflow_msg(__func__, xdr);
  372. return -EIO;
  373. }
  374. static int decode_layoutstats(struct xdr_stream *xdr)
  375. {
  376. return decode_op_hdr(xdr, OP_LAYOUTSTATS);
  377. }
  378. static int decode_clone(struct xdr_stream *xdr)
  379. {
  380. return decode_op_hdr(xdr, OP_CLONE);
  381. }
  382. /*
  383. * Decode ALLOCATE request
  384. */
  385. static int nfs4_xdr_dec_allocate(struct rpc_rqst *rqstp,
  386. struct xdr_stream *xdr,
  387. struct nfs42_falloc_res *res)
  388. {
  389. struct compound_hdr hdr;
  390. int status;
  391. status = decode_compound_hdr(xdr, &hdr);
  392. if (status)
  393. goto out;
  394. status = decode_sequence(xdr, &res->seq_res, rqstp);
  395. if (status)
  396. goto out;
  397. status = decode_putfh(xdr);
  398. if (status)
  399. goto out;
  400. status = decode_allocate(xdr, res);
  401. if (status)
  402. goto out;
  403. decode_getfattr(xdr, res->falloc_fattr, res->falloc_server);
  404. out:
  405. return status;
  406. }
  407. /*
  408. * Decode COPY response
  409. */
  410. static int nfs4_xdr_dec_copy(struct rpc_rqst *rqstp,
  411. struct xdr_stream *xdr,
  412. struct nfs42_copy_res *res)
  413. {
  414. struct compound_hdr hdr;
  415. int status;
  416. status = decode_compound_hdr(xdr, &hdr);
  417. if (status)
  418. goto out;
  419. status = decode_sequence(xdr, &res->seq_res, rqstp);
  420. if (status)
  421. goto out;
  422. status = decode_putfh(xdr);
  423. if (status)
  424. goto out;
  425. status = decode_savefh(xdr);
  426. if (status)
  427. goto out;
  428. status = decode_putfh(xdr);
  429. if (status)
  430. goto out;
  431. status = decode_copy(xdr, res);
  432. out:
  433. return status;
  434. }
  435. /*
  436. * Decode DEALLOCATE request
  437. */
  438. static int nfs4_xdr_dec_deallocate(struct rpc_rqst *rqstp,
  439. struct xdr_stream *xdr,
  440. struct nfs42_falloc_res *res)
  441. {
  442. struct compound_hdr hdr;
  443. int status;
  444. status = decode_compound_hdr(xdr, &hdr);
  445. if (status)
  446. goto out;
  447. status = decode_sequence(xdr, &res->seq_res, rqstp);
  448. if (status)
  449. goto out;
  450. status = decode_putfh(xdr);
  451. if (status)
  452. goto out;
  453. status = decode_deallocate(xdr, res);
  454. if (status)
  455. goto out;
  456. decode_getfattr(xdr, res->falloc_fattr, res->falloc_server);
  457. out:
  458. return status;
  459. }
  460. /*
  461. * Decode SEEK request
  462. */
  463. static int nfs4_xdr_dec_seek(struct rpc_rqst *rqstp,
  464. struct xdr_stream *xdr,
  465. struct nfs42_seek_res *res)
  466. {
  467. struct compound_hdr hdr;
  468. int status;
  469. status = decode_compound_hdr(xdr, &hdr);
  470. if (status)
  471. goto out;
  472. status = decode_sequence(xdr, &res->seq_res, rqstp);
  473. if (status)
  474. goto out;
  475. status = decode_putfh(xdr);
  476. if (status)
  477. goto out;
  478. status = decode_seek(xdr, res);
  479. out:
  480. return status;
  481. }
  482. /*
  483. * Decode LAYOUTSTATS request
  484. */
  485. static int nfs4_xdr_dec_layoutstats(struct rpc_rqst *rqstp,
  486. struct xdr_stream *xdr,
  487. struct nfs42_layoutstat_res *res)
  488. {
  489. struct compound_hdr hdr;
  490. int status, i;
  491. status = decode_compound_hdr(xdr, &hdr);
  492. if (status)
  493. goto out;
  494. status = decode_sequence(xdr, &res->seq_res, rqstp);
  495. if (status)
  496. goto out;
  497. status = decode_putfh(xdr);
  498. if (status)
  499. goto out;
  500. WARN_ON(res->num_dev > PNFS_LAYOUTSTATS_MAXDEV);
  501. for (i = 0; i < res->num_dev; i++) {
  502. status = decode_layoutstats(xdr);
  503. if (status)
  504. goto out;
  505. }
  506. out:
  507. res->rpc_status = status;
  508. return status;
  509. }
  510. /*
  511. * Decode CLONE request
  512. */
  513. static int nfs4_xdr_dec_clone(struct rpc_rqst *rqstp,
  514. struct xdr_stream *xdr,
  515. struct nfs42_clone_res *res)
  516. {
  517. struct compound_hdr hdr;
  518. int status;
  519. status = decode_compound_hdr(xdr, &hdr);
  520. if (status)
  521. goto out;
  522. status = decode_sequence(xdr, &res->seq_res, rqstp);
  523. if (status)
  524. goto out;
  525. status = decode_putfh(xdr);
  526. if (status)
  527. goto out;
  528. status = decode_savefh(xdr);
  529. if (status)
  530. goto out;
  531. status = decode_putfh(xdr);
  532. if (status)
  533. goto out;
  534. status = decode_clone(xdr);
  535. if (status)
  536. goto out;
  537. status = decode_getfattr(xdr, res->dst_fattr, res->server);
  538. out:
  539. res->rpc_status = status;
  540. return status;
  541. }
  542. #endif /* __LINUX_FS_NFS_NFS4_2XDR_H */