nfs42xdr.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2014 Anna Schumaker <Anna.Schumaker@Netapp.com>
  4. */
  5. #ifndef __LINUX_FS_NFS_NFS4_2XDR_H
  6. #define __LINUX_FS_NFS_NFS4_2XDR_H
  7. #include "nfs42.h"
  8. /* Not limited by NFS itself, limited by the generic xattr code */
  9. #define nfs4_xattr_name_maxsz XDR_QUADLEN(XATTR_NAME_MAX)
  10. #define encode_fallocate_maxsz (encode_stateid_maxsz + \
  11. 2 /* offset */ + \
  12. 2 /* length */)
  13. #define NFS42_WRITE_RES_SIZE (1 /* wr_callback_id size */ +\
  14. XDR_QUADLEN(NFS4_STATEID_SIZE) + \
  15. 2 /* wr_count */ + \
  16. 1 /* wr_committed */ + \
  17. XDR_QUADLEN(NFS4_VERIFIER_SIZE))
  18. #define encode_allocate_maxsz (op_encode_hdr_maxsz + \
  19. encode_fallocate_maxsz)
  20. #define decode_allocate_maxsz (op_decode_hdr_maxsz)
  21. #define encode_copy_maxsz (op_encode_hdr_maxsz + \
  22. XDR_QUADLEN(NFS4_STATEID_SIZE) + \
  23. XDR_QUADLEN(NFS4_STATEID_SIZE) + \
  24. 2 + 2 + 2 + 1 + 1 + 1 +\
  25. 1 + /* One cnr_source_server */\
  26. 1 + /* nl4_type */ \
  27. 1 + XDR_QUADLEN(NFS4_OPAQUE_LIMIT))
  28. #define decode_copy_maxsz (op_decode_hdr_maxsz + \
  29. NFS42_WRITE_RES_SIZE + \
  30. 1 /* cr_consecutive */ + \
  31. 1 /* cr_synchronous */)
  32. #define encode_offload_cancel_maxsz (op_encode_hdr_maxsz + \
  33. XDR_QUADLEN(NFS4_STATEID_SIZE))
  34. #define decode_offload_cancel_maxsz (op_decode_hdr_maxsz)
  35. #define encode_copy_notify_maxsz (op_encode_hdr_maxsz + \
  36. XDR_QUADLEN(NFS4_STATEID_SIZE) + \
  37. 1 + /* nl4_type */ \
  38. 1 + XDR_QUADLEN(NFS4_OPAQUE_LIMIT))
  39. #define decode_copy_notify_maxsz (op_decode_hdr_maxsz + \
  40. 3 + /* cnr_lease_time */\
  41. XDR_QUADLEN(NFS4_STATEID_SIZE) + \
  42. 1 + /* Support 1 cnr_source_server */\
  43. 1 + /* nl4_type */ \
  44. 1 + XDR_QUADLEN(NFS4_OPAQUE_LIMIT))
  45. #define encode_deallocate_maxsz (op_encode_hdr_maxsz + \
  46. encode_fallocate_maxsz)
  47. #define decode_deallocate_maxsz (op_decode_hdr_maxsz)
  48. #define encode_read_plus_maxsz (op_encode_hdr_maxsz + \
  49. encode_stateid_maxsz + 3)
  50. #define NFS42_READ_PLUS_DATA_SEGMENT_SIZE \
  51. (1 /* data_content4 */ + \
  52. 2 /* data_info4.di_offset */ + \
  53. 1 /* data_info4.di_length */)
  54. #define NFS42_READ_PLUS_HOLE_SEGMENT_SIZE \
  55. (1 /* data_content4 */ + \
  56. 2 /* data_info4.di_offset */ + \
  57. 2 /* data_info4.di_length */)
  58. #define READ_PLUS_SEGMENT_SIZE_DIFF (NFS42_READ_PLUS_HOLE_SEGMENT_SIZE - \
  59. NFS42_READ_PLUS_DATA_SEGMENT_SIZE)
  60. #define decode_read_plus_maxsz (op_decode_hdr_maxsz + \
  61. 1 /* rpr_eof */ + \
  62. 1 /* rpr_contents count */ + \
  63. NFS42_READ_PLUS_HOLE_SEGMENT_SIZE)
  64. #define encode_seek_maxsz (op_encode_hdr_maxsz + \
  65. encode_stateid_maxsz + \
  66. 2 /* offset */ + \
  67. 1 /* whence */)
  68. #define decode_seek_maxsz (op_decode_hdr_maxsz + \
  69. 1 /* eof */ + \
  70. 1 /* whence */ + \
  71. 2 /* offset */ + \
  72. 2 /* length */)
  73. #define encode_io_info_maxsz 4
  74. #define encode_layoutstats_maxsz (op_decode_hdr_maxsz + \
  75. 2 /* offset */ + \
  76. 2 /* length */ + \
  77. encode_stateid_maxsz + \
  78. encode_io_info_maxsz + \
  79. encode_io_info_maxsz + \
  80. 1 /* opaque devaddr4 length */ + \
  81. XDR_QUADLEN(PNFS_LAYOUTSTATS_MAXSIZE))
  82. #define decode_layoutstats_maxsz (op_decode_hdr_maxsz)
  83. #define encode_device_error_maxsz (XDR_QUADLEN(NFS4_DEVICEID4_SIZE) + \
  84. 1 /* status */ + 1 /* opnum */)
  85. #define encode_layouterror_maxsz (op_decode_hdr_maxsz + \
  86. 2 /* offset */ + \
  87. 2 /* length */ + \
  88. encode_stateid_maxsz + \
  89. 1 /* Array size */ + \
  90. encode_device_error_maxsz)
  91. #define decode_layouterror_maxsz (op_decode_hdr_maxsz)
  92. #define encode_clone_maxsz (encode_stateid_maxsz + \
  93. encode_stateid_maxsz + \
  94. 2 /* src offset */ + \
  95. 2 /* dst offset */ + \
  96. 2 /* count */)
  97. #define decode_clone_maxsz (op_decode_hdr_maxsz)
  98. #define encode_getxattr_maxsz (op_encode_hdr_maxsz + 1 + \
  99. nfs4_xattr_name_maxsz)
  100. #define decode_getxattr_maxsz (op_decode_hdr_maxsz + 1 + pagepad_maxsz)
  101. #define encode_setxattr_maxsz (op_encode_hdr_maxsz + \
  102. 1 + nfs4_xattr_name_maxsz + 1)
  103. #define decode_setxattr_maxsz (op_decode_hdr_maxsz + decode_change_info_maxsz)
  104. #define encode_listxattrs_maxsz (op_encode_hdr_maxsz + 2 + 1)
  105. #define decode_listxattrs_maxsz (op_decode_hdr_maxsz + 2 + 1 + 1 + 1)
  106. #define encode_removexattr_maxsz (op_encode_hdr_maxsz + 1 + \
  107. nfs4_xattr_name_maxsz)
  108. #define decode_removexattr_maxsz (op_decode_hdr_maxsz + \
  109. decode_change_info_maxsz)
  110. #define NFS4_enc_allocate_sz (compound_encode_hdr_maxsz + \
  111. encode_sequence_maxsz + \
  112. encode_putfh_maxsz + \
  113. encode_allocate_maxsz + \
  114. encode_getattr_maxsz)
  115. #define NFS4_dec_allocate_sz (compound_decode_hdr_maxsz + \
  116. decode_sequence_maxsz + \
  117. decode_putfh_maxsz + \
  118. decode_allocate_maxsz + \
  119. decode_getattr_maxsz)
  120. #define NFS4_enc_copy_sz (compound_encode_hdr_maxsz + \
  121. encode_sequence_maxsz + \
  122. encode_putfh_maxsz + \
  123. encode_savefh_maxsz + \
  124. encode_putfh_maxsz + \
  125. encode_copy_maxsz + \
  126. encode_commit_maxsz)
  127. #define NFS4_dec_copy_sz (compound_decode_hdr_maxsz + \
  128. decode_sequence_maxsz + \
  129. decode_putfh_maxsz + \
  130. decode_savefh_maxsz + \
  131. decode_putfh_maxsz + \
  132. decode_copy_maxsz + \
  133. decode_commit_maxsz)
  134. #define NFS4_enc_offload_cancel_sz (compound_encode_hdr_maxsz + \
  135. encode_sequence_maxsz + \
  136. encode_putfh_maxsz + \
  137. encode_offload_cancel_maxsz)
  138. #define NFS4_dec_offload_cancel_sz (compound_decode_hdr_maxsz + \
  139. decode_sequence_maxsz + \
  140. decode_putfh_maxsz + \
  141. decode_offload_cancel_maxsz)
  142. #define NFS4_enc_copy_notify_sz (compound_encode_hdr_maxsz + \
  143. encode_sequence_maxsz + \
  144. encode_putfh_maxsz + \
  145. encode_copy_notify_maxsz)
  146. #define NFS4_dec_copy_notify_sz (compound_decode_hdr_maxsz + \
  147. decode_sequence_maxsz + \
  148. decode_putfh_maxsz + \
  149. decode_copy_notify_maxsz)
  150. #define NFS4_enc_deallocate_sz (compound_encode_hdr_maxsz + \
  151. encode_sequence_maxsz + \
  152. encode_putfh_maxsz + \
  153. encode_deallocate_maxsz + \
  154. encode_getattr_maxsz)
  155. #define NFS4_dec_deallocate_sz (compound_decode_hdr_maxsz + \
  156. decode_sequence_maxsz + \
  157. decode_putfh_maxsz + \
  158. decode_deallocate_maxsz + \
  159. decode_getattr_maxsz)
  160. #define NFS4_enc_read_plus_sz (compound_encode_hdr_maxsz + \
  161. encode_sequence_maxsz + \
  162. encode_putfh_maxsz + \
  163. encode_read_plus_maxsz)
  164. #define NFS4_dec_read_plus_sz (compound_decode_hdr_maxsz + \
  165. decode_sequence_maxsz + \
  166. decode_putfh_maxsz + \
  167. decode_read_plus_maxsz)
  168. #define NFS4_enc_seek_sz (compound_encode_hdr_maxsz + \
  169. encode_sequence_maxsz + \
  170. encode_putfh_maxsz + \
  171. encode_seek_maxsz)
  172. #define NFS4_dec_seek_sz (compound_decode_hdr_maxsz + \
  173. decode_sequence_maxsz + \
  174. decode_putfh_maxsz + \
  175. decode_seek_maxsz)
  176. #define NFS4_enc_layoutstats_sz (compound_encode_hdr_maxsz + \
  177. encode_sequence_maxsz + \
  178. encode_putfh_maxsz + \
  179. PNFS_LAYOUTSTATS_MAXDEV * encode_layoutstats_maxsz)
  180. #define NFS4_dec_layoutstats_sz (compound_decode_hdr_maxsz + \
  181. decode_sequence_maxsz + \
  182. decode_putfh_maxsz + \
  183. PNFS_LAYOUTSTATS_MAXDEV * decode_layoutstats_maxsz)
  184. #define NFS4_enc_layouterror_sz (compound_encode_hdr_maxsz + \
  185. encode_sequence_maxsz + \
  186. encode_putfh_maxsz + \
  187. NFS42_LAYOUTERROR_MAX * \
  188. encode_layouterror_maxsz)
  189. #define NFS4_dec_layouterror_sz (compound_decode_hdr_maxsz + \
  190. decode_sequence_maxsz + \
  191. decode_putfh_maxsz + \
  192. NFS42_LAYOUTERROR_MAX * \
  193. decode_layouterror_maxsz)
  194. #define NFS4_enc_clone_sz (compound_encode_hdr_maxsz + \
  195. encode_sequence_maxsz + \
  196. encode_putfh_maxsz + \
  197. encode_savefh_maxsz + \
  198. encode_putfh_maxsz + \
  199. encode_clone_maxsz + \
  200. encode_getattr_maxsz)
  201. #define NFS4_dec_clone_sz (compound_decode_hdr_maxsz + \
  202. decode_sequence_maxsz + \
  203. decode_putfh_maxsz + \
  204. decode_savefh_maxsz + \
  205. decode_putfh_maxsz + \
  206. decode_clone_maxsz + \
  207. decode_getattr_maxsz)
  208. #define NFS4_enc_getxattr_sz (compound_encode_hdr_maxsz + \
  209. encode_sequence_maxsz + \
  210. encode_putfh_maxsz + \
  211. encode_getxattr_maxsz)
  212. #define NFS4_dec_getxattr_sz (compound_decode_hdr_maxsz + \
  213. decode_sequence_maxsz + \
  214. decode_putfh_maxsz + \
  215. decode_getxattr_maxsz)
  216. #define NFS4_enc_setxattr_sz (compound_encode_hdr_maxsz + \
  217. encode_sequence_maxsz + \
  218. encode_putfh_maxsz + \
  219. encode_setxattr_maxsz + \
  220. encode_getattr_maxsz)
  221. #define NFS4_dec_setxattr_sz (compound_decode_hdr_maxsz + \
  222. decode_sequence_maxsz + \
  223. decode_putfh_maxsz + \
  224. decode_setxattr_maxsz + \
  225. decode_getattr_maxsz)
  226. #define NFS4_enc_listxattrs_sz (compound_encode_hdr_maxsz + \
  227. encode_sequence_maxsz + \
  228. encode_putfh_maxsz + \
  229. encode_listxattrs_maxsz)
  230. #define NFS4_dec_listxattrs_sz (compound_decode_hdr_maxsz + \
  231. decode_sequence_maxsz + \
  232. decode_putfh_maxsz + \
  233. decode_listxattrs_maxsz)
  234. #define NFS4_enc_removexattr_sz (compound_encode_hdr_maxsz + \
  235. encode_sequence_maxsz + \
  236. encode_putfh_maxsz + \
  237. encode_removexattr_maxsz)
  238. #define NFS4_dec_removexattr_sz (compound_decode_hdr_maxsz + \
  239. decode_sequence_maxsz + \
  240. decode_putfh_maxsz + \
  241. decode_removexattr_maxsz)
  242. /*
  243. * These values specify the maximum amount of data that is not
  244. * associated with the extended attribute name or extended
  245. * attribute list in the SETXATTR, GETXATTR and LISTXATTR
  246. * respectively.
  247. */
  248. const u32 nfs42_maxsetxattr_overhead = ((RPC_MAX_HEADER_WITH_AUTH +
  249. compound_encode_hdr_maxsz +
  250. encode_sequence_maxsz +
  251. encode_putfh_maxsz + 1 +
  252. nfs4_xattr_name_maxsz)
  253. * XDR_UNIT);
  254. const u32 nfs42_maxgetxattr_overhead = ((RPC_MAX_HEADER_WITH_AUTH +
  255. compound_decode_hdr_maxsz +
  256. decode_sequence_maxsz +
  257. decode_putfh_maxsz + 1) * XDR_UNIT);
  258. const u32 nfs42_maxlistxattrs_overhead = ((RPC_MAX_HEADER_WITH_AUTH +
  259. compound_decode_hdr_maxsz +
  260. decode_sequence_maxsz +
  261. decode_putfh_maxsz + 3) * XDR_UNIT);
  262. static void encode_fallocate(struct xdr_stream *xdr,
  263. const struct nfs42_falloc_args *args)
  264. {
  265. encode_nfs4_stateid(xdr, &args->falloc_stateid);
  266. encode_uint64(xdr, args->falloc_offset);
  267. encode_uint64(xdr, args->falloc_length);
  268. }
  269. static void encode_allocate(struct xdr_stream *xdr,
  270. const struct nfs42_falloc_args *args,
  271. struct compound_hdr *hdr)
  272. {
  273. encode_op_hdr(xdr, OP_ALLOCATE, decode_allocate_maxsz, hdr);
  274. encode_fallocate(xdr, args);
  275. }
  276. static void encode_nl4_server(struct xdr_stream *xdr,
  277. const struct nl4_server *ns)
  278. {
  279. encode_uint32(xdr, ns->nl4_type);
  280. switch (ns->nl4_type) {
  281. case NL4_NAME:
  282. case NL4_URL:
  283. encode_string(xdr, ns->u.nl4_str_sz, ns->u.nl4_str);
  284. break;
  285. case NL4_NETADDR:
  286. encode_string(xdr, ns->u.nl4_addr.netid_len,
  287. ns->u.nl4_addr.netid);
  288. encode_string(xdr, ns->u.nl4_addr.addr_len,
  289. ns->u.nl4_addr.addr);
  290. break;
  291. default:
  292. WARN_ON_ONCE(1);
  293. }
  294. }
  295. static void encode_copy(struct xdr_stream *xdr,
  296. const struct nfs42_copy_args *args,
  297. struct compound_hdr *hdr)
  298. {
  299. encode_op_hdr(xdr, OP_COPY, decode_copy_maxsz, hdr);
  300. encode_nfs4_stateid(xdr, &args->src_stateid);
  301. encode_nfs4_stateid(xdr, &args->dst_stateid);
  302. encode_uint64(xdr, args->src_pos);
  303. encode_uint64(xdr, args->dst_pos);
  304. encode_uint64(xdr, args->count);
  305. encode_uint32(xdr, 1); /* consecutive = true */
  306. encode_uint32(xdr, args->sync);
  307. if (args->cp_src == NULL) { /* intra-ssc */
  308. encode_uint32(xdr, 0); /* no src server list */
  309. return;
  310. }
  311. encode_uint32(xdr, 1); /* supporting 1 server */
  312. encode_nl4_server(xdr, args->cp_src);
  313. }
  314. static void encode_copy_commit(struct xdr_stream *xdr,
  315. const struct nfs42_copy_args *args,
  316. struct compound_hdr *hdr)
  317. {
  318. __be32 *p;
  319. encode_op_hdr(xdr, OP_COMMIT, decode_commit_maxsz, hdr);
  320. p = reserve_space(xdr, 12);
  321. p = xdr_encode_hyper(p, args->dst_pos);
  322. *p = cpu_to_be32(args->count);
  323. }
  324. static void encode_offload_cancel(struct xdr_stream *xdr,
  325. const struct nfs42_offload_status_args *args,
  326. struct compound_hdr *hdr)
  327. {
  328. encode_op_hdr(xdr, OP_OFFLOAD_CANCEL, decode_offload_cancel_maxsz, hdr);
  329. encode_nfs4_stateid(xdr, &args->osa_stateid);
  330. }
  331. static void encode_copy_notify(struct xdr_stream *xdr,
  332. const struct nfs42_copy_notify_args *args,
  333. struct compound_hdr *hdr)
  334. {
  335. encode_op_hdr(xdr, OP_COPY_NOTIFY, decode_copy_notify_maxsz, hdr);
  336. encode_nfs4_stateid(xdr, &args->cna_src_stateid);
  337. encode_nl4_server(xdr, &args->cna_dst);
  338. }
  339. static void encode_deallocate(struct xdr_stream *xdr,
  340. const struct nfs42_falloc_args *args,
  341. struct compound_hdr *hdr)
  342. {
  343. encode_op_hdr(xdr, OP_DEALLOCATE, decode_deallocate_maxsz, hdr);
  344. encode_fallocate(xdr, args);
  345. }
  346. static void encode_read_plus(struct xdr_stream *xdr,
  347. const struct nfs_pgio_args *args,
  348. struct compound_hdr *hdr)
  349. {
  350. encode_op_hdr(xdr, OP_READ_PLUS, decode_read_plus_maxsz, hdr);
  351. encode_nfs4_stateid(xdr, &args->stateid);
  352. encode_uint64(xdr, args->offset);
  353. encode_uint32(xdr, args->count);
  354. }
  355. static void encode_seek(struct xdr_stream *xdr,
  356. const struct nfs42_seek_args *args,
  357. struct compound_hdr *hdr)
  358. {
  359. encode_op_hdr(xdr, OP_SEEK, decode_seek_maxsz, hdr);
  360. encode_nfs4_stateid(xdr, &args->sa_stateid);
  361. encode_uint64(xdr, args->sa_offset);
  362. encode_uint32(xdr, args->sa_what);
  363. }
  364. static void encode_layoutstats(struct xdr_stream *xdr,
  365. const struct nfs42_layoutstat_args *args,
  366. struct nfs42_layoutstat_devinfo *devinfo,
  367. struct compound_hdr *hdr)
  368. {
  369. __be32 *p;
  370. encode_op_hdr(xdr, OP_LAYOUTSTATS, decode_layoutstats_maxsz, hdr);
  371. p = reserve_space(xdr, 8 + 8);
  372. p = xdr_encode_hyper(p, devinfo->offset);
  373. p = xdr_encode_hyper(p, devinfo->length);
  374. encode_nfs4_stateid(xdr, &args->stateid);
  375. p = reserve_space(xdr, 4*8 + NFS4_DEVICEID4_SIZE + 4);
  376. p = xdr_encode_hyper(p, devinfo->read_count);
  377. p = xdr_encode_hyper(p, devinfo->read_bytes);
  378. p = xdr_encode_hyper(p, devinfo->write_count);
  379. p = xdr_encode_hyper(p, devinfo->write_bytes);
  380. p = xdr_encode_opaque_fixed(p, devinfo->dev_id.data,
  381. NFS4_DEVICEID4_SIZE);
  382. /* Encode layoutupdate4 */
  383. *p++ = cpu_to_be32(devinfo->layout_type);
  384. if (devinfo->ld_private.ops)
  385. devinfo->ld_private.ops->encode(xdr, args,
  386. &devinfo->ld_private);
  387. else
  388. encode_uint32(xdr, 0);
  389. }
  390. static void encode_clone(struct xdr_stream *xdr,
  391. const struct nfs42_clone_args *args,
  392. struct compound_hdr *hdr)
  393. {
  394. __be32 *p;
  395. encode_op_hdr(xdr, OP_CLONE, decode_clone_maxsz, hdr);
  396. encode_nfs4_stateid(xdr, &args->src_stateid);
  397. encode_nfs4_stateid(xdr, &args->dst_stateid);
  398. p = reserve_space(xdr, 3*8);
  399. p = xdr_encode_hyper(p, args->src_offset);
  400. p = xdr_encode_hyper(p, args->dst_offset);
  401. xdr_encode_hyper(p, args->count);
  402. }
  403. static void encode_device_error(struct xdr_stream *xdr,
  404. const struct nfs42_device_error *error)
  405. {
  406. __be32 *p;
  407. p = reserve_space(xdr, NFS4_DEVICEID4_SIZE + 2*4);
  408. p = xdr_encode_opaque_fixed(p, error->dev_id.data,
  409. NFS4_DEVICEID4_SIZE);
  410. *p++ = cpu_to_be32(error->status);
  411. *p = cpu_to_be32(error->opnum);
  412. }
  413. static void encode_layouterror(struct xdr_stream *xdr,
  414. const struct nfs42_layout_error *args,
  415. struct compound_hdr *hdr)
  416. {
  417. __be32 *p;
  418. encode_op_hdr(xdr, OP_LAYOUTERROR, decode_layouterror_maxsz, hdr);
  419. p = reserve_space(xdr, 8 + 8);
  420. p = xdr_encode_hyper(p, args->offset);
  421. p = xdr_encode_hyper(p, args->length);
  422. encode_nfs4_stateid(xdr, &args->stateid);
  423. p = reserve_space(xdr, 4);
  424. *p = cpu_to_be32(1);
  425. encode_device_error(xdr, &args->errors[0]);
  426. }
  427. static void encode_setxattr(struct xdr_stream *xdr,
  428. const struct nfs42_setxattrargs *arg,
  429. struct compound_hdr *hdr)
  430. {
  431. __be32 *p;
  432. BUILD_BUG_ON(XATTR_CREATE != SETXATTR4_CREATE);
  433. BUILD_BUG_ON(XATTR_REPLACE != SETXATTR4_REPLACE);
  434. encode_op_hdr(xdr, OP_SETXATTR, decode_setxattr_maxsz, hdr);
  435. p = reserve_space(xdr, 4);
  436. *p = cpu_to_be32(arg->xattr_flags);
  437. encode_string(xdr, strlen(arg->xattr_name), arg->xattr_name);
  438. p = reserve_space(xdr, 4);
  439. *p = cpu_to_be32(arg->xattr_len);
  440. if (arg->xattr_len)
  441. xdr_write_pages(xdr, arg->xattr_pages, 0, arg->xattr_len);
  442. }
  443. static void encode_getxattr(struct xdr_stream *xdr, const char *name,
  444. struct compound_hdr *hdr)
  445. {
  446. encode_op_hdr(xdr, OP_GETXATTR, decode_getxattr_maxsz, hdr);
  447. encode_string(xdr, strlen(name), name);
  448. }
  449. static void encode_removexattr(struct xdr_stream *xdr, const char *name,
  450. struct compound_hdr *hdr)
  451. {
  452. encode_op_hdr(xdr, OP_REMOVEXATTR, decode_removexattr_maxsz, hdr);
  453. encode_string(xdr, strlen(name), name);
  454. }
  455. static void encode_listxattrs(struct xdr_stream *xdr,
  456. const struct nfs42_listxattrsargs *arg,
  457. struct compound_hdr *hdr)
  458. {
  459. __be32 *p;
  460. encode_op_hdr(xdr, OP_LISTXATTRS, decode_listxattrs_maxsz, hdr);
  461. p = reserve_space(xdr, 12);
  462. if (unlikely(!p))
  463. return;
  464. p = xdr_encode_hyper(p, arg->cookie);
  465. /*
  466. * RFC 8276 says to specify the full max length of the LISTXATTRS
  467. * XDR reply. Count is set to the XDR length of the names array
  468. * plus the EOF marker. So, add the cookie and the names count.
  469. */
  470. *p = cpu_to_be32(arg->count + 8 + 4);
  471. }
  472. /*
  473. * Encode ALLOCATE request
  474. */
  475. static void nfs4_xdr_enc_allocate(struct rpc_rqst *req,
  476. struct xdr_stream *xdr,
  477. const void *data)
  478. {
  479. const struct nfs42_falloc_args *args = data;
  480. struct compound_hdr hdr = {
  481. .minorversion = nfs4_xdr_minorversion(&args->seq_args),
  482. };
  483. encode_compound_hdr(xdr, req, &hdr);
  484. encode_sequence(xdr, &args->seq_args, &hdr);
  485. encode_putfh(xdr, args->falloc_fh, &hdr);
  486. encode_allocate(xdr, args, &hdr);
  487. encode_getfattr(xdr, args->falloc_bitmask, &hdr);
  488. encode_nops(&hdr);
  489. }
  490. /*
  491. * Encode COPY request
  492. */
  493. static void nfs4_xdr_enc_copy(struct rpc_rqst *req,
  494. struct xdr_stream *xdr,
  495. const void *data)
  496. {
  497. const struct nfs42_copy_args *args = data;
  498. struct compound_hdr hdr = {
  499. .minorversion = nfs4_xdr_minorversion(&args->seq_args),
  500. };
  501. encode_compound_hdr(xdr, req, &hdr);
  502. encode_sequence(xdr, &args->seq_args, &hdr);
  503. encode_putfh(xdr, args->src_fh, &hdr);
  504. encode_savefh(xdr, &hdr);
  505. encode_putfh(xdr, args->dst_fh, &hdr);
  506. encode_copy(xdr, args, &hdr);
  507. if (args->sync)
  508. encode_copy_commit(xdr, args, &hdr);
  509. encode_nops(&hdr);
  510. }
  511. /*
  512. * Encode OFFLOAD_CANEL request
  513. */
  514. static void nfs4_xdr_enc_offload_cancel(struct rpc_rqst *req,
  515. struct xdr_stream *xdr,
  516. const void *data)
  517. {
  518. const struct nfs42_offload_status_args *args = data;
  519. struct compound_hdr hdr = {
  520. .minorversion = nfs4_xdr_minorversion(&args->osa_seq_args),
  521. };
  522. encode_compound_hdr(xdr, req, &hdr);
  523. encode_sequence(xdr, &args->osa_seq_args, &hdr);
  524. encode_putfh(xdr, args->osa_src_fh, &hdr);
  525. encode_offload_cancel(xdr, args, &hdr);
  526. encode_nops(&hdr);
  527. }
  528. /*
  529. * Encode COPY_NOTIFY request
  530. */
  531. static void nfs4_xdr_enc_copy_notify(struct rpc_rqst *req,
  532. struct xdr_stream *xdr,
  533. const void *data)
  534. {
  535. const struct nfs42_copy_notify_args *args = data;
  536. struct compound_hdr hdr = {
  537. .minorversion = nfs4_xdr_minorversion(&args->cna_seq_args),
  538. };
  539. encode_compound_hdr(xdr, req, &hdr);
  540. encode_sequence(xdr, &args->cna_seq_args, &hdr);
  541. encode_putfh(xdr, args->cna_src_fh, &hdr);
  542. encode_copy_notify(xdr, args, &hdr);
  543. encode_nops(&hdr);
  544. }
  545. /*
  546. * Encode DEALLOCATE request
  547. */
  548. static void nfs4_xdr_enc_deallocate(struct rpc_rqst *req,
  549. struct xdr_stream *xdr,
  550. const void *data)
  551. {
  552. const struct nfs42_falloc_args *args = data;
  553. struct compound_hdr hdr = {
  554. .minorversion = nfs4_xdr_minorversion(&args->seq_args),
  555. };
  556. encode_compound_hdr(xdr, req, &hdr);
  557. encode_sequence(xdr, &args->seq_args, &hdr);
  558. encode_putfh(xdr, args->falloc_fh, &hdr);
  559. encode_deallocate(xdr, args, &hdr);
  560. encode_getfattr(xdr, args->falloc_bitmask, &hdr);
  561. encode_nops(&hdr);
  562. }
  563. /*
  564. * Encode READ_PLUS request
  565. */
  566. static void nfs4_xdr_enc_read_plus(struct rpc_rqst *req,
  567. struct xdr_stream *xdr,
  568. const void *data)
  569. {
  570. const struct nfs_pgio_args *args = data;
  571. struct compound_hdr hdr = {
  572. .minorversion = nfs4_xdr_minorversion(&args->seq_args),
  573. };
  574. encode_compound_hdr(xdr, req, &hdr);
  575. encode_sequence(xdr, &args->seq_args, &hdr);
  576. encode_putfh(xdr, args->fh, &hdr);
  577. encode_read_plus(xdr, args, &hdr);
  578. rpc_prepare_reply_pages(req, args->pages, args->pgbase, args->count,
  579. hdr.replen - READ_PLUS_SEGMENT_SIZE_DIFF);
  580. encode_nops(&hdr);
  581. }
  582. /*
  583. * Encode SEEK request
  584. */
  585. static void nfs4_xdr_enc_seek(struct rpc_rqst *req,
  586. struct xdr_stream *xdr,
  587. const void *data)
  588. {
  589. const struct nfs42_seek_args *args = data;
  590. struct compound_hdr hdr = {
  591. .minorversion = nfs4_xdr_minorversion(&args->seq_args),
  592. };
  593. encode_compound_hdr(xdr, req, &hdr);
  594. encode_sequence(xdr, &args->seq_args, &hdr);
  595. encode_putfh(xdr, args->sa_fh, &hdr);
  596. encode_seek(xdr, args, &hdr);
  597. encode_nops(&hdr);
  598. }
  599. /*
  600. * Encode LAYOUTSTATS request
  601. */
  602. static void nfs4_xdr_enc_layoutstats(struct rpc_rqst *req,
  603. struct xdr_stream *xdr,
  604. const void *data)
  605. {
  606. const struct nfs42_layoutstat_args *args = data;
  607. int i;
  608. struct compound_hdr hdr = {
  609. .minorversion = nfs4_xdr_minorversion(&args->seq_args),
  610. };
  611. encode_compound_hdr(xdr, req, &hdr);
  612. encode_sequence(xdr, &args->seq_args, &hdr);
  613. encode_putfh(xdr, args->fh, &hdr);
  614. WARN_ON(args->num_dev > PNFS_LAYOUTSTATS_MAXDEV);
  615. for (i = 0; i < args->num_dev; i++)
  616. encode_layoutstats(xdr, args, &args->devinfo[i], &hdr);
  617. encode_nops(&hdr);
  618. }
  619. /*
  620. * Encode CLONE request
  621. */
  622. static void nfs4_xdr_enc_clone(struct rpc_rqst *req,
  623. struct xdr_stream *xdr,
  624. const void *data)
  625. {
  626. const struct nfs42_clone_args *args = data;
  627. struct compound_hdr hdr = {
  628. .minorversion = nfs4_xdr_minorversion(&args->seq_args),
  629. };
  630. encode_compound_hdr(xdr, req, &hdr);
  631. encode_sequence(xdr, &args->seq_args, &hdr);
  632. encode_putfh(xdr, args->src_fh, &hdr);
  633. encode_savefh(xdr, &hdr);
  634. encode_putfh(xdr, args->dst_fh, &hdr);
  635. encode_clone(xdr, args, &hdr);
  636. encode_getfattr(xdr, args->dst_bitmask, &hdr);
  637. encode_nops(&hdr);
  638. }
  639. /*
  640. * Encode LAYOUTERROR request
  641. */
  642. static void nfs4_xdr_enc_layouterror(struct rpc_rqst *req,
  643. struct xdr_stream *xdr,
  644. const void *data)
  645. {
  646. const struct nfs42_layouterror_args *args = data;
  647. struct compound_hdr hdr = {
  648. .minorversion = nfs4_xdr_minorversion(&args->seq_args),
  649. };
  650. int i;
  651. encode_compound_hdr(xdr, req, &hdr);
  652. encode_sequence(xdr, &args->seq_args, &hdr);
  653. encode_putfh(xdr, NFS_FH(args->inode), &hdr);
  654. for (i = 0; i < args->num_errors; i++)
  655. encode_layouterror(xdr, &args->errors[i], &hdr);
  656. encode_nops(&hdr);
  657. }
  658. /*
  659. * Encode SETXATTR request
  660. */
  661. static void nfs4_xdr_enc_setxattr(struct rpc_rqst *req, struct xdr_stream *xdr,
  662. const void *data)
  663. {
  664. const struct nfs42_setxattrargs *args = data;
  665. struct compound_hdr hdr = {
  666. .minorversion = nfs4_xdr_minorversion(&args->seq_args),
  667. };
  668. encode_compound_hdr(xdr, req, &hdr);
  669. encode_sequence(xdr, &args->seq_args, &hdr);
  670. encode_putfh(xdr, args->fh, &hdr);
  671. encode_setxattr(xdr, args, &hdr);
  672. encode_getfattr(xdr, args->bitmask, &hdr);
  673. encode_nops(&hdr);
  674. }
  675. /*
  676. * Encode GETXATTR request
  677. */
  678. static void nfs4_xdr_enc_getxattr(struct rpc_rqst *req, struct xdr_stream *xdr,
  679. const void *data)
  680. {
  681. const struct nfs42_getxattrargs *args = data;
  682. struct compound_hdr hdr = {
  683. .minorversion = nfs4_xdr_minorversion(&args->seq_args),
  684. };
  685. uint32_t replen;
  686. encode_compound_hdr(xdr, req, &hdr);
  687. encode_sequence(xdr, &args->seq_args, &hdr);
  688. encode_putfh(xdr, args->fh, &hdr);
  689. replen = hdr.replen + op_decode_hdr_maxsz + 1;
  690. encode_getxattr(xdr, args->xattr_name, &hdr);
  691. rpc_prepare_reply_pages(req, args->xattr_pages, 0, args->xattr_len,
  692. replen);
  693. encode_nops(&hdr);
  694. }
  695. /*
  696. * Encode LISTXATTR request
  697. */
  698. static void nfs4_xdr_enc_listxattrs(struct rpc_rqst *req,
  699. struct xdr_stream *xdr, const void *data)
  700. {
  701. const struct nfs42_listxattrsargs *args = data;
  702. struct compound_hdr hdr = {
  703. .minorversion = nfs4_xdr_minorversion(&args->seq_args),
  704. };
  705. uint32_t replen;
  706. encode_compound_hdr(xdr, req, &hdr);
  707. encode_sequence(xdr, &args->seq_args, &hdr);
  708. encode_putfh(xdr, args->fh, &hdr);
  709. replen = hdr.replen + op_decode_hdr_maxsz + 2 + 1;
  710. encode_listxattrs(xdr, args, &hdr);
  711. rpc_prepare_reply_pages(req, args->xattr_pages, 0, args->count, replen);
  712. encode_nops(&hdr);
  713. }
  714. /*
  715. * Encode REMOVEXATTR request
  716. */
  717. static void nfs4_xdr_enc_removexattr(struct rpc_rqst *req,
  718. struct xdr_stream *xdr, const void *data)
  719. {
  720. const struct nfs42_removexattrargs *args = data;
  721. struct compound_hdr hdr = {
  722. .minorversion = nfs4_xdr_minorversion(&args->seq_args),
  723. };
  724. encode_compound_hdr(xdr, req, &hdr);
  725. encode_sequence(xdr, &args->seq_args, &hdr);
  726. encode_putfh(xdr, args->fh, &hdr);
  727. encode_removexattr(xdr, args->xattr_name, &hdr);
  728. encode_nops(&hdr);
  729. }
  730. static int decode_allocate(struct xdr_stream *xdr, struct nfs42_falloc_res *res)
  731. {
  732. return decode_op_hdr(xdr, OP_ALLOCATE);
  733. }
  734. static int decode_write_response(struct xdr_stream *xdr,
  735. struct nfs42_write_res *res)
  736. {
  737. __be32 *p;
  738. int status, count;
  739. p = xdr_inline_decode(xdr, 4);
  740. if (unlikely(!p))
  741. return -EIO;
  742. count = be32_to_cpup(p);
  743. if (count > 1)
  744. return -EREMOTEIO;
  745. else if (count == 1) {
  746. status = decode_opaque_fixed(xdr, &res->stateid,
  747. NFS4_STATEID_SIZE);
  748. if (unlikely(status))
  749. return -EIO;
  750. }
  751. p = xdr_inline_decode(xdr, 8 + 4);
  752. if (unlikely(!p))
  753. return -EIO;
  754. p = xdr_decode_hyper(p, &res->count);
  755. res->verifier.committed = be32_to_cpup(p);
  756. return decode_verifier(xdr, &res->verifier.verifier);
  757. }
  758. static int decode_nl4_server(struct xdr_stream *xdr, struct nl4_server *ns)
  759. {
  760. struct nfs42_netaddr *naddr;
  761. uint32_t dummy;
  762. char *dummy_str;
  763. __be32 *p;
  764. int status;
  765. /* nl_type */
  766. p = xdr_inline_decode(xdr, 4);
  767. if (unlikely(!p))
  768. return -EIO;
  769. ns->nl4_type = be32_to_cpup(p);
  770. switch (ns->nl4_type) {
  771. case NL4_NAME:
  772. case NL4_URL:
  773. status = decode_opaque_inline(xdr, &dummy, &dummy_str);
  774. if (unlikely(status))
  775. return status;
  776. if (unlikely(dummy > NFS4_OPAQUE_LIMIT))
  777. return -EIO;
  778. memcpy(&ns->u.nl4_str, dummy_str, dummy);
  779. ns->u.nl4_str_sz = dummy;
  780. break;
  781. case NL4_NETADDR:
  782. naddr = &ns->u.nl4_addr;
  783. /* netid string */
  784. status = decode_opaque_inline(xdr, &dummy, &dummy_str);
  785. if (unlikely(status))
  786. return status;
  787. if (unlikely(dummy > RPCBIND_MAXNETIDLEN))
  788. return -EIO;
  789. naddr->netid_len = dummy;
  790. memcpy(naddr->netid, dummy_str, naddr->netid_len);
  791. /* uaddr string */
  792. status = decode_opaque_inline(xdr, &dummy, &dummy_str);
  793. if (unlikely(status))
  794. return status;
  795. if (unlikely(dummy > RPCBIND_MAXUADDRLEN))
  796. return -EIO;
  797. naddr->addr_len = dummy;
  798. memcpy(naddr->addr, dummy_str, naddr->addr_len);
  799. break;
  800. default:
  801. WARN_ON_ONCE(1);
  802. return -EIO;
  803. }
  804. return 0;
  805. }
  806. static int decode_copy_requirements(struct xdr_stream *xdr,
  807. struct nfs42_copy_res *res) {
  808. __be32 *p;
  809. p = xdr_inline_decode(xdr, 4 + 4);
  810. if (unlikely(!p))
  811. return -EIO;
  812. res->consecutive = be32_to_cpup(p++);
  813. res->synchronous = be32_to_cpup(p++);
  814. return 0;
  815. }
  816. static int decode_copy(struct xdr_stream *xdr, struct nfs42_copy_res *res)
  817. {
  818. int status;
  819. status = decode_op_hdr(xdr, OP_COPY);
  820. if (status == NFS4ERR_OFFLOAD_NO_REQS) {
  821. status = decode_copy_requirements(xdr, res);
  822. if (status)
  823. return status;
  824. return NFS4ERR_OFFLOAD_NO_REQS;
  825. } else if (status)
  826. return status;
  827. status = decode_write_response(xdr, &res->write_res);
  828. if (status)
  829. return status;
  830. return decode_copy_requirements(xdr, res);
  831. }
  832. static int decode_offload_cancel(struct xdr_stream *xdr,
  833. struct nfs42_offload_status_res *res)
  834. {
  835. return decode_op_hdr(xdr, OP_OFFLOAD_CANCEL);
  836. }
  837. static int decode_copy_notify(struct xdr_stream *xdr,
  838. struct nfs42_copy_notify_res *res)
  839. {
  840. __be32 *p;
  841. int status, count;
  842. status = decode_op_hdr(xdr, OP_COPY_NOTIFY);
  843. if (status)
  844. return status;
  845. /* cnr_lease_time */
  846. p = xdr_inline_decode(xdr, 12);
  847. if (unlikely(!p))
  848. return -EIO;
  849. p = xdr_decode_hyper(p, &res->cnr_lease_time.seconds);
  850. res->cnr_lease_time.nseconds = be32_to_cpup(p);
  851. status = decode_opaque_fixed(xdr, &res->cnr_stateid, NFS4_STATEID_SIZE);
  852. if (unlikely(status))
  853. return -EIO;
  854. /* number of source addresses */
  855. p = xdr_inline_decode(xdr, 4);
  856. if (unlikely(!p))
  857. return -EIO;
  858. count = be32_to_cpup(p);
  859. if (count > 1)
  860. pr_warn("NFS: %s: nsvr %d > Supported. Use first servers\n",
  861. __func__, count);
  862. status = decode_nl4_server(xdr, &res->cnr_src);
  863. if (unlikely(status))
  864. return -EIO;
  865. return 0;
  866. }
  867. static int decode_deallocate(struct xdr_stream *xdr, struct nfs42_falloc_res *res)
  868. {
  869. return decode_op_hdr(xdr, OP_DEALLOCATE);
  870. }
  871. struct read_plus_segment {
  872. enum data_content4 type;
  873. uint64_t offset;
  874. union {
  875. struct {
  876. uint64_t length;
  877. } hole;
  878. struct {
  879. uint32_t length;
  880. unsigned int from;
  881. } data;
  882. };
  883. };
  884. static inline uint64_t read_plus_segment_length(struct read_plus_segment *seg)
  885. {
  886. return seg->type == NFS4_CONTENT_DATA ? seg->data.length : seg->hole.length;
  887. }
  888. static int decode_read_plus_segment(struct xdr_stream *xdr,
  889. struct read_plus_segment *seg)
  890. {
  891. __be32 *p;
  892. p = xdr_inline_decode(xdr, 4);
  893. if (!p)
  894. return -EIO;
  895. seg->type = be32_to_cpup(p++);
  896. p = xdr_inline_decode(xdr, seg->type == NFS4_CONTENT_DATA ? 12 : 16);
  897. if (!p)
  898. return -EIO;
  899. p = xdr_decode_hyper(p, &seg->offset);
  900. if (seg->type == NFS4_CONTENT_DATA) {
  901. struct xdr_buf buf;
  902. uint32_t len = be32_to_cpup(p);
  903. seg->data.length = len;
  904. seg->data.from = xdr_stream_pos(xdr);
  905. if (!xdr_stream_subsegment(xdr, &buf, xdr_align_size(len)))
  906. return -EIO;
  907. } else if (seg->type == NFS4_CONTENT_HOLE) {
  908. xdr_decode_hyper(p, &seg->hole.length);
  909. } else
  910. return -EINVAL;
  911. return 0;
  912. }
  913. static int process_read_plus_segment(struct xdr_stream *xdr,
  914. struct nfs_pgio_args *args,
  915. struct nfs_pgio_res *res,
  916. struct read_plus_segment *seg)
  917. {
  918. unsigned long offset = seg->offset;
  919. unsigned long length = read_plus_segment_length(seg);
  920. unsigned int bufpos;
  921. if (offset + length < args->offset)
  922. return 0;
  923. else if (offset > args->offset + args->count) {
  924. res->eof = 0;
  925. return 0;
  926. } else if (offset < args->offset) {
  927. length -= (args->offset - offset);
  928. offset = args->offset;
  929. } else if (offset + length > args->offset + args->count) {
  930. length = (args->offset + args->count) - offset;
  931. res->eof = 0;
  932. }
  933. bufpos = xdr->buf->head[0].iov_len + (offset - args->offset);
  934. if (seg->type == NFS4_CONTENT_HOLE)
  935. return xdr_stream_zero(xdr, bufpos, length);
  936. else
  937. return xdr_stream_move_subsegment(xdr, seg->data.from, bufpos, length);
  938. }
  939. static int decode_read_plus(struct xdr_stream *xdr, struct nfs_pgio_res *res)
  940. {
  941. struct nfs_pgio_header *hdr =
  942. container_of(res, struct nfs_pgio_header, res);
  943. struct nfs_pgio_args *args = &hdr->args;
  944. uint32_t segments;
  945. struct read_plus_segment *segs;
  946. int status, i;
  947. __be32 *p;
  948. status = decode_op_hdr(xdr, OP_READ_PLUS);
  949. if (status)
  950. return status;
  951. p = xdr_inline_decode(xdr, 4 + 4);
  952. if (unlikely(!p))
  953. return -EIO;
  954. res->count = 0;
  955. res->eof = be32_to_cpup(p++);
  956. segments = be32_to_cpup(p++);
  957. if (segments == 0)
  958. return 0;
  959. segs = kmalloc_array(segments, sizeof(*segs), GFP_KERNEL);
  960. if (!segs)
  961. return -ENOMEM;
  962. for (i = 0; i < segments; i++) {
  963. status = decode_read_plus_segment(xdr, &segs[i]);
  964. if (status < 0)
  965. goto out;
  966. }
  967. xdr_set_pagelen(xdr, xdr_align_size(args->count));
  968. for (i = segments; i > 0; i--)
  969. res->count += process_read_plus_segment(xdr, args, res, &segs[i-1]);
  970. status = 0;
  971. out:
  972. kfree(segs);
  973. return status;
  974. }
  975. static int decode_seek(struct xdr_stream *xdr, struct nfs42_seek_res *res)
  976. {
  977. int status;
  978. __be32 *p;
  979. status = decode_op_hdr(xdr, OP_SEEK);
  980. if (status)
  981. return status;
  982. p = xdr_inline_decode(xdr, 4 + 8);
  983. if (unlikely(!p))
  984. return -EIO;
  985. res->sr_eof = be32_to_cpup(p++);
  986. p = xdr_decode_hyper(p, &res->sr_offset);
  987. return 0;
  988. }
  989. static int decode_layoutstats(struct xdr_stream *xdr)
  990. {
  991. return decode_op_hdr(xdr, OP_LAYOUTSTATS);
  992. }
  993. static int decode_clone(struct xdr_stream *xdr)
  994. {
  995. return decode_op_hdr(xdr, OP_CLONE);
  996. }
  997. static int decode_layouterror(struct xdr_stream *xdr)
  998. {
  999. return decode_op_hdr(xdr, OP_LAYOUTERROR);
  1000. }
  1001. static int decode_setxattr(struct xdr_stream *xdr,
  1002. struct nfs4_change_info *cinfo)
  1003. {
  1004. int status;
  1005. status = decode_op_hdr(xdr, OP_SETXATTR);
  1006. if (status)
  1007. goto out;
  1008. status = decode_change_info(xdr, cinfo);
  1009. out:
  1010. return status;
  1011. }
  1012. static int decode_getxattr(struct xdr_stream *xdr,
  1013. struct nfs42_getxattrres *res,
  1014. struct rpc_rqst *req)
  1015. {
  1016. int status;
  1017. __be32 *p;
  1018. u32 len, rdlen;
  1019. status = decode_op_hdr(xdr, OP_GETXATTR);
  1020. if (status)
  1021. return status;
  1022. p = xdr_inline_decode(xdr, 4);
  1023. if (unlikely(!p))
  1024. return -EIO;
  1025. len = be32_to_cpup(p);
  1026. /*
  1027. * Only check against the page length here. The actual
  1028. * requested length may be smaller, but that is only
  1029. * checked against after possibly caching a valid reply.
  1030. */
  1031. if (len > req->rq_rcv_buf.page_len)
  1032. return -ERANGE;
  1033. res->xattr_len = len;
  1034. if (len > 0) {
  1035. rdlen = xdr_read_pages(xdr, len);
  1036. if (rdlen < len)
  1037. return -EIO;
  1038. }
  1039. return 0;
  1040. }
  1041. static int decode_removexattr(struct xdr_stream *xdr,
  1042. struct nfs4_change_info *cinfo)
  1043. {
  1044. int status;
  1045. status = decode_op_hdr(xdr, OP_REMOVEXATTR);
  1046. if (status)
  1047. goto out;
  1048. status = decode_change_info(xdr, cinfo);
  1049. out:
  1050. return status;
  1051. }
  1052. static int decode_listxattrs(struct xdr_stream *xdr,
  1053. struct nfs42_listxattrsres *res)
  1054. {
  1055. int status;
  1056. __be32 *p;
  1057. u32 count, len, ulen;
  1058. size_t left, copied;
  1059. char *buf;
  1060. status = decode_op_hdr(xdr, OP_LISTXATTRS);
  1061. if (status) {
  1062. /*
  1063. * Special case: for LISTXATTRS, NFS4ERR_TOOSMALL
  1064. * should be translated to ERANGE.
  1065. */
  1066. if (status == -ETOOSMALL)
  1067. status = -ERANGE;
  1068. /*
  1069. * Special case: for LISTXATTRS, NFS4ERR_NOXATTR
  1070. * should be translated to success with zero-length reply.
  1071. */
  1072. if (status == -ENODATA) {
  1073. res->eof = true;
  1074. status = 0;
  1075. }
  1076. goto out;
  1077. }
  1078. p = xdr_inline_decode(xdr, 8);
  1079. if (unlikely(!p))
  1080. return -EIO;
  1081. xdr_decode_hyper(p, &res->cookie);
  1082. p = xdr_inline_decode(xdr, 4);
  1083. if (unlikely(!p))
  1084. return -EIO;
  1085. left = res->xattr_len;
  1086. buf = res->xattr_buf;
  1087. count = be32_to_cpup(p);
  1088. copied = 0;
  1089. /*
  1090. * We have asked for enough room to encode the maximum number
  1091. * of possible attribute names, so everything should fit.
  1092. *
  1093. * But, don't rely on that assumption. Just decode entries
  1094. * until they don't fit anymore, just in case the server did
  1095. * something odd.
  1096. */
  1097. while (count--) {
  1098. p = xdr_inline_decode(xdr, 4);
  1099. if (unlikely(!p))
  1100. return -EIO;
  1101. len = be32_to_cpup(p);
  1102. if (len > (XATTR_NAME_MAX - XATTR_USER_PREFIX_LEN)) {
  1103. status = -ERANGE;
  1104. goto out;
  1105. }
  1106. p = xdr_inline_decode(xdr, len);
  1107. if (unlikely(!p))
  1108. return -EIO;
  1109. ulen = len + XATTR_USER_PREFIX_LEN + 1;
  1110. if (buf) {
  1111. if (ulen > left) {
  1112. status = -ERANGE;
  1113. goto out;
  1114. }
  1115. memcpy(buf, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
  1116. memcpy(buf + XATTR_USER_PREFIX_LEN, p, len);
  1117. buf[ulen - 1] = 0;
  1118. buf += ulen;
  1119. left -= ulen;
  1120. }
  1121. copied += ulen;
  1122. }
  1123. p = xdr_inline_decode(xdr, 4);
  1124. if (unlikely(!p))
  1125. return -EIO;
  1126. res->eof = be32_to_cpup(p);
  1127. res->copied = copied;
  1128. out:
  1129. if (status == -ERANGE && res->xattr_len == XATTR_LIST_MAX)
  1130. status = -E2BIG;
  1131. return status;
  1132. }
  1133. /*
  1134. * Decode ALLOCATE request
  1135. */
  1136. static int nfs4_xdr_dec_allocate(struct rpc_rqst *rqstp,
  1137. struct xdr_stream *xdr,
  1138. void *data)
  1139. {
  1140. struct nfs42_falloc_res *res = data;
  1141. struct compound_hdr hdr;
  1142. int status;
  1143. status = decode_compound_hdr(xdr, &hdr);
  1144. if (status)
  1145. goto out;
  1146. status = decode_sequence(xdr, &res->seq_res, rqstp);
  1147. if (status)
  1148. goto out;
  1149. status = decode_putfh(xdr);
  1150. if (status)
  1151. goto out;
  1152. status = decode_allocate(xdr, res);
  1153. if (status)
  1154. goto out;
  1155. decode_getfattr(xdr, res->falloc_fattr, res->falloc_server);
  1156. out:
  1157. return status;
  1158. }
  1159. /*
  1160. * Decode COPY response
  1161. */
  1162. static int nfs4_xdr_dec_copy(struct rpc_rqst *rqstp,
  1163. struct xdr_stream *xdr,
  1164. void *data)
  1165. {
  1166. struct nfs42_copy_res *res = data;
  1167. struct compound_hdr hdr;
  1168. int status;
  1169. status = decode_compound_hdr(xdr, &hdr);
  1170. if (status)
  1171. goto out;
  1172. status = decode_sequence(xdr, &res->seq_res, rqstp);
  1173. if (status)
  1174. goto out;
  1175. status = decode_putfh(xdr);
  1176. if (status)
  1177. goto out;
  1178. status = decode_savefh(xdr);
  1179. if (status)
  1180. goto out;
  1181. status = decode_putfh(xdr);
  1182. if (status)
  1183. goto out;
  1184. status = decode_copy(xdr, res);
  1185. if (status)
  1186. goto out;
  1187. if (res->commit_res.verf)
  1188. status = decode_commit(xdr, &res->commit_res);
  1189. out:
  1190. return status;
  1191. }
  1192. /*
  1193. * Decode OFFLOAD_CANCEL response
  1194. */
  1195. static int nfs4_xdr_dec_offload_cancel(struct rpc_rqst *rqstp,
  1196. struct xdr_stream *xdr,
  1197. void *data)
  1198. {
  1199. struct nfs42_offload_status_res *res = data;
  1200. struct compound_hdr hdr;
  1201. int status;
  1202. status = decode_compound_hdr(xdr, &hdr);
  1203. if (status)
  1204. goto out;
  1205. status = decode_sequence(xdr, &res->osr_seq_res, rqstp);
  1206. if (status)
  1207. goto out;
  1208. status = decode_putfh(xdr);
  1209. if (status)
  1210. goto out;
  1211. status = decode_offload_cancel(xdr, res);
  1212. out:
  1213. return status;
  1214. }
  1215. /*
  1216. * Decode COPY_NOTIFY response
  1217. */
  1218. static int nfs4_xdr_dec_copy_notify(struct rpc_rqst *rqstp,
  1219. struct xdr_stream *xdr,
  1220. void *data)
  1221. {
  1222. struct nfs42_copy_notify_res *res = data;
  1223. struct compound_hdr hdr;
  1224. int status;
  1225. status = decode_compound_hdr(xdr, &hdr);
  1226. if (status)
  1227. goto out;
  1228. status = decode_sequence(xdr, &res->cnr_seq_res, rqstp);
  1229. if (status)
  1230. goto out;
  1231. status = decode_putfh(xdr);
  1232. if (status)
  1233. goto out;
  1234. status = decode_copy_notify(xdr, res);
  1235. out:
  1236. return status;
  1237. }
  1238. /*
  1239. * Decode DEALLOCATE request
  1240. */
  1241. static int nfs4_xdr_dec_deallocate(struct rpc_rqst *rqstp,
  1242. struct xdr_stream *xdr,
  1243. void *data)
  1244. {
  1245. struct nfs42_falloc_res *res = data;
  1246. struct compound_hdr hdr;
  1247. int status;
  1248. status = decode_compound_hdr(xdr, &hdr);
  1249. if (status)
  1250. goto out;
  1251. status = decode_sequence(xdr, &res->seq_res, rqstp);
  1252. if (status)
  1253. goto out;
  1254. status = decode_putfh(xdr);
  1255. if (status)
  1256. goto out;
  1257. status = decode_deallocate(xdr, res);
  1258. if (status)
  1259. goto out;
  1260. decode_getfattr(xdr, res->falloc_fattr, res->falloc_server);
  1261. out:
  1262. return status;
  1263. }
  1264. /*
  1265. * Decode READ_PLUS request
  1266. */
  1267. static int nfs4_xdr_dec_read_plus(struct rpc_rqst *rqstp,
  1268. struct xdr_stream *xdr,
  1269. void *data)
  1270. {
  1271. struct nfs_pgio_res *res = data;
  1272. struct compound_hdr hdr;
  1273. int status;
  1274. xdr_set_scratch_buffer(xdr, res->scratch, READ_PLUS_SCRATCH_SIZE);
  1275. status = decode_compound_hdr(xdr, &hdr);
  1276. if (status)
  1277. goto out;
  1278. status = decode_sequence(xdr, &res->seq_res, rqstp);
  1279. if (status)
  1280. goto out;
  1281. status = decode_putfh(xdr);
  1282. if (status)
  1283. goto out;
  1284. status = decode_read_plus(xdr, res);
  1285. if (!status)
  1286. status = res->count;
  1287. out:
  1288. return status;
  1289. }
  1290. /*
  1291. * Decode SEEK request
  1292. */
  1293. static int nfs4_xdr_dec_seek(struct rpc_rqst *rqstp,
  1294. struct xdr_stream *xdr,
  1295. void *data)
  1296. {
  1297. struct nfs42_seek_res *res = data;
  1298. struct compound_hdr hdr;
  1299. int status;
  1300. status = decode_compound_hdr(xdr, &hdr);
  1301. if (status)
  1302. goto out;
  1303. status = decode_sequence(xdr, &res->seq_res, rqstp);
  1304. if (status)
  1305. goto out;
  1306. status = decode_putfh(xdr);
  1307. if (status)
  1308. goto out;
  1309. status = decode_seek(xdr, res);
  1310. out:
  1311. return status;
  1312. }
  1313. /*
  1314. * Decode LAYOUTSTATS request
  1315. */
  1316. static int nfs4_xdr_dec_layoutstats(struct rpc_rqst *rqstp,
  1317. struct xdr_stream *xdr,
  1318. void *data)
  1319. {
  1320. struct nfs42_layoutstat_res *res = data;
  1321. struct compound_hdr hdr;
  1322. int status, i;
  1323. status = decode_compound_hdr(xdr, &hdr);
  1324. if (status)
  1325. goto out;
  1326. status = decode_sequence(xdr, &res->seq_res, rqstp);
  1327. if (status)
  1328. goto out;
  1329. status = decode_putfh(xdr);
  1330. if (status)
  1331. goto out;
  1332. WARN_ON(res->num_dev > PNFS_LAYOUTSTATS_MAXDEV);
  1333. for (i = 0; i < res->num_dev; i++) {
  1334. status = decode_layoutstats(xdr);
  1335. if (status)
  1336. goto out;
  1337. }
  1338. out:
  1339. res->rpc_status = status;
  1340. return status;
  1341. }
  1342. /*
  1343. * Decode CLONE request
  1344. */
  1345. static int nfs4_xdr_dec_clone(struct rpc_rqst *rqstp,
  1346. struct xdr_stream *xdr,
  1347. void *data)
  1348. {
  1349. struct nfs42_clone_res *res = data;
  1350. struct compound_hdr hdr;
  1351. int status;
  1352. status = decode_compound_hdr(xdr, &hdr);
  1353. if (status)
  1354. goto out;
  1355. status = decode_sequence(xdr, &res->seq_res, rqstp);
  1356. if (status)
  1357. goto out;
  1358. status = decode_putfh(xdr);
  1359. if (status)
  1360. goto out;
  1361. status = decode_savefh(xdr);
  1362. if (status)
  1363. goto out;
  1364. status = decode_putfh(xdr);
  1365. if (status)
  1366. goto out;
  1367. status = decode_clone(xdr);
  1368. if (status)
  1369. goto out;
  1370. decode_getfattr(xdr, res->dst_fattr, res->server);
  1371. out:
  1372. res->rpc_status = status;
  1373. return status;
  1374. }
  1375. /*
  1376. * Decode LAYOUTERROR request
  1377. */
  1378. static int nfs4_xdr_dec_layouterror(struct rpc_rqst *rqstp,
  1379. struct xdr_stream *xdr,
  1380. void *data)
  1381. {
  1382. struct nfs42_layouterror_res *res = data;
  1383. struct compound_hdr hdr;
  1384. int status, i;
  1385. status = decode_compound_hdr(xdr, &hdr);
  1386. if (status)
  1387. goto out;
  1388. status = decode_sequence(xdr, &res->seq_res, rqstp);
  1389. if (status)
  1390. goto out;
  1391. status = decode_putfh(xdr);
  1392. for (i = 0; i < res->num_errors && status == 0; i++)
  1393. status = decode_layouterror(xdr);
  1394. out:
  1395. res->rpc_status = status;
  1396. return status;
  1397. }
  1398. /*
  1399. * Decode SETXATTR request
  1400. */
  1401. static int nfs4_xdr_dec_setxattr(struct rpc_rqst *req, struct xdr_stream *xdr,
  1402. void *data)
  1403. {
  1404. struct nfs42_setxattrres *res = data;
  1405. struct compound_hdr hdr;
  1406. int status;
  1407. status = decode_compound_hdr(xdr, &hdr);
  1408. if (status)
  1409. goto out;
  1410. status = decode_sequence(xdr, &res->seq_res, req);
  1411. if (status)
  1412. goto out;
  1413. status = decode_putfh(xdr);
  1414. if (status)
  1415. goto out;
  1416. status = decode_setxattr(xdr, &res->cinfo);
  1417. if (status)
  1418. goto out;
  1419. status = decode_getfattr(xdr, res->fattr, res->server);
  1420. out:
  1421. return status;
  1422. }
  1423. /*
  1424. * Decode GETXATTR request
  1425. */
  1426. static int nfs4_xdr_dec_getxattr(struct rpc_rqst *rqstp,
  1427. struct xdr_stream *xdr, void *data)
  1428. {
  1429. struct nfs42_getxattrres *res = data;
  1430. struct compound_hdr hdr;
  1431. int status;
  1432. status = decode_compound_hdr(xdr, &hdr);
  1433. if (status)
  1434. goto out;
  1435. status = decode_sequence(xdr, &res->seq_res, rqstp);
  1436. if (status)
  1437. goto out;
  1438. status = decode_putfh(xdr);
  1439. if (status)
  1440. goto out;
  1441. status = decode_getxattr(xdr, res, rqstp);
  1442. out:
  1443. return status;
  1444. }
  1445. /*
  1446. * Decode LISTXATTR request
  1447. */
  1448. static int nfs4_xdr_dec_listxattrs(struct rpc_rqst *rqstp,
  1449. struct xdr_stream *xdr, void *data)
  1450. {
  1451. struct nfs42_listxattrsres *res = data;
  1452. struct compound_hdr hdr;
  1453. int status;
  1454. xdr_set_scratch_page(xdr, res->scratch);
  1455. status = decode_compound_hdr(xdr, &hdr);
  1456. if (status)
  1457. goto out;
  1458. status = decode_sequence(xdr, &res->seq_res, rqstp);
  1459. if (status)
  1460. goto out;
  1461. status = decode_putfh(xdr);
  1462. if (status)
  1463. goto out;
  1464. status = decode_listxattrs(xdr, res);
  1465. out:
  1466. return status;
  1467. }
  1468. /*
  1469. * Decode REMOVEXATTR request
  1470. */
  1471. static int nfs4_xdr_dec_removexattr(struct rpc_rqst *req,
  1472. struct xdr_stream *xdr, void *data)
  1473. {
  1474. struct nfs42_removexattrres *res = data;
  1475. struct compound_hdr hdr;
  1476. int status;
  1477. status = decode_compound_hdr(xdr, &hdr);
  1478. if (status)
  1479. goto out;
  1480. status = decode_sequence(xdr, &res->seq_res, req);
  1481. if (status)
  1482. goto out;
  1483. status = decode_putfh(xdr);
  1484. if (status)
  1485. goto out;
  1486. status = decode_removexattr(xdr, &res->cinfo);
  1487. out:
  1488. return status;
  1489. }
  1490. #endif /* __LINUX_FS_NFS_NFS4_2XDR_H */