nfs42xdr.c 42 KB

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