nfs3xdr.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/nfs/nfs3xdr.c
  4. *
  5. * XDR functions to encode/decode NFSv3 RPC arguments and results.
  6. *
  7. * Copyright (C) 1996, 1997 Olaf Kirch
  8. */
  9. #include <linux/param.h>
  10. #include <linux/time.h>
  11. #include <linux/mm.h>
  12. #include <linux/errno.h>
  13. #include <linux/string.h>
  14. #include <linux/in.h>
  15. #include <linux/pagemap.h>
  16. #include <linux/proc_fs.h>
  17. #include <linux/kdev_t.h>
  18. #include <linux/sunrpc/clnt.h>
  19. #include <linux/nfs.h>
  20. #include <linux/nfs3.h>
  21. #include <linux/nfs_fs.h>
  22. #include <linux/nfsacl.h>
  23. #include <linux/nfs_common.h>
  24. #include "nfstrace.h"
  25. #include "internal.h"
  26. #define NFSDBG_FACILITY NFSDBG_XDR
  27. /*
  28. * Declare the space requirements for NFS arguments and replies as
  29. * number of 32bit-words
  30. */
  31. #define NFS3_pagepad_sz (1) /* Page padding */
  32. #define NFS3_fhandle_sz (1+16)
  33. #define NFS3_fh_sz (NFS3_fhandle_sz) /* shorthand */
  34. #define NFS3_post_op_fh_sz (1+NFS3_fh_sz)
  35. #define NFS3_sattr_sz (15)
  36. #define NFS3_filename_sz (1+(NFS3_MAXNAMLEN>>2))
  37. #define NFS3_path_sz (1+(NFS3_MAXPATHLEN>>2))
  38. #define NFS3_fattr_sz (21)
  39. #define NFS3_cookieverf_sz (NFS3_COOKIEVERFSIZE>>2)
  40. #define NFS3_wcc_attr_sz (6)
  41. #define NFS3_pre_op_attr_sz (1+NFS3_wcc_attr_sz)
  42. #define NFS3_post_op_attr_sz (1+NFS3_fattr_sz)
  43. #define NFS3_wcc_data_sz (NFS3_pre_op_attr_sz+NFS3_post_op_attr_sz)
  44. #define NFS3_diropargs_sz (NFS3_fh_sz+NFS3_filename_sz)
  45. #define NFS3_getattrargs_sz (NFS3_fh_sz)
  46. #define NFS3_setattrargs_sz (NFS3_fh_sz+NFS3_sattr_sz+3)
  47. #define NFS3_lookupargs_sz (NFS3_fh_sz+NFS3_filename_sz)
  48. #define NFS3_accessargs_sz (NFS3_fh_sz+1)
  49. #define NFS3_readlinkargs_sz (NFS3_fh_sz)
  50. #define NFS3_readargs_sz (NFS3_fh_sz+3)
  51. #define NFS3_writeargs_sz (NFS3_fh_sz+5)
  52. #define NFS3_createargs_sz (NFS3_diropargs_sz+NFS3_sattr_sz)
  53. #define NFS3_mkdirargs_sz (NFS3_diropargs_sz+NFS3_sattr_sz)
  54. #define NFS3_symlinkargs_sz (NFS3_diropargs_sz+1+NFS3_sattr_sz)
  55. #define NFS3_mknodargs_sz (NFS3_diropargs_sz+2+NFS3_sattr_sz)
  56. #define NFS3_removeargs_sz (NFS3_fh_sz+NFS3_filename_sz)
  57. #define NFS3_renameargs_sz (NFS3_diropargs_sz+NFS3_diropargs_sz)
  58. #define NFS3_linkargs_sz (NFS3_fh_sz+NFS3_diropargs_sz)
  59. #define NFS3_readdirargs_sz (NFS3_fh_sz+NFS3_cookieverf_sz+3)
  60. #define NFS3_readdirplusargs_sz (NFS3_fh_sz+NFS3_cookieverf_sz+4)
  61. #define NFS3_commitargs_sz (NFS3_fh_sz+3)
  62. #define NFS3_getattrres_sz (1+NFS3_fattr_sz)
  63. #define NFS3_setattrres_sz (1+NFS3_wcc_data_sz)
  64. #define NFS3_removeres_sz (NFS3_setattrres_sz)
  65. #define NFS3_lookupres_sz (1+NFS3_fh_sz+(2 * NFS3_post_op_attr_sz))
  66. #define NFS3_accessres_sz (1+NFS3_post_op_attr_sz+1)
  67. #define NFS3_readlinkres_sz (1+NFS3_post_op_attr_sz+1+NFS3_pagepad_sz)
  68. #define NFS3_readres_sz (1+NFS3_post_op_attr_sz+3+NFS3_pagepad_sz)
  69. #define NFS3_writeres_sz (1+NFS3_wcc_data_sz+4)
  70. #define NFS3_createres_sz (1+NFS3_post_op_fh_sz+NFS3_post_op_attr_sz+NFS3_wcc_data_sz)
  71. #define NFS3_renameres_sz (1+(2 * NFS3_wcc_data_sz))
  72. #define NFS3_linkres_sz (1+NFS3_post_op_attr_sz+NFS3_wcc_data_sz)
  73. #define NFS3_readdirres_sz (1+NFS3_post_op_attr_sz+2+NFS3_pagepad_sz)
  74. #define NFS3_fsstatres_sz (1+NFS3_post_op_attr_sz+13)
  75. #define NFS3_fsinfores_sz (1+NFS3_post_op_attr_sz+12)
  76. #define NFS3_pathconfres_sz (1+NFS3_post_op_attr_sz+6)
  77. #define NFS3_commitres_sz (1+NFS3_wcc_data_sz+2)
  78. #define ACL3_getaclargs_sz (NFS3_fh_sz+1)
  79. #define ACL3_setaclargs_sz (NFS3_fh_sz+1+ \
  80. XDR_QUADLEN(NFS_ACL_INLINE_BUFSIZE))
  81. #define ACL3_getaclres_sz (1+NFS3_post_op_attr_sz+1+ \
  82. XDR_QUADLEN(NFS_ACL_INLINE_BUFSIZE)+\
  83. NFS3_pagepad_sz)
  84. #define ACL3_setaclres_sz (1+NFS3_post_op_attr_sz)
  85. /*
  86. * Map file type to S_IFMT bits
  87. */
  88. static const umode_t nfs_type2fmt[] = {
  89. [NF3BAD] = 0,
  90. [NF3REG] = S_IFREG,
  91. [NF3DIR] = S_IFDIR,
  92. [NF3BLK] = S_IFBLK,
  93. [NF3CHR] = S_IFCHR,
  94. [NF3LNK] = S_IFLNK,
  95. [NF3SOCK] = S_IFSOCK,
  96. [NF3FIFO] = S_IFIFO,
  97. };
  98. static struct user_namespace *rpc_userns(const struct rpc_clnt *clnt)
  99. {
  100. if (clnt && clnt->cl_cred)
  101. return clnt->cl_cred->user_ns;
  102. return &init_user_ns;
  103. }
  104. static struct user_namespace *rpc_rqst_userns(const struct rpc_rqst *rqstp)
  105. {
  106. if (rqstp->rq_task)
  107. return rpc_userns(rqstp->rq_task->tk_client);
  108. return &init_user_ns;
  109. }
  110. /*
  111. * Encode/decode NFSv3 basic data types
  112. *
  113. * Basic NFSv3 data types are defined in section 2.5 of RFC 1813:
  114. * "NFS Version 3 Protocol Specification".
  115. *
  116. * Not all basic data types have their own encoding and decoding
  117. * functions. For run-time efficiency, some data types are encoded
  118. * or decoded inline.
  119. */
  120. static void encode_uint32(struct xdr_stream *xdr, u32 value)
  121. {
  122. __be32 *p = xdr_reserve_space(xdr, 4);
  123. *p = cpu_to_be32(value);
  124. }
  125. static int decode_uint32(struct xdr_stream *xdr, u32 *value)
  126. {
  127. __be32 *p;
  128. p = xdr_inline_decode(xdr, 4);
  129. if (unlikely(!p))
  130. return -EIO;
  131. *value = be32_to_cpup(p);
  132. return 0;
  133. }
  134. static int decode_uint64(struct xdr_stream *xdr, u64 *value)
  135. {
  136. __be32 *p;
  137. p = xdr_inline_decode(xdr, 8);
  138. if (unlikely(!p))
  139. return -EIO;
  140. xdr_decode_hyper(p, value);
  141. return 0;
  142. }
  143. /*
  144. * fileid3
  145. *
  146. * typedef uint64 fileid3;
  147. */
  148. static __be32 *xdr_decode_fileid3(__be32 *p, u64 *fileid)
  149. {
  150. return xdr_decode_hyper(p, fileid);
  151. }
  152. static int decode_fileid3(struct xdr_stream *xdr, u64 *fileid)
  153. {
  154. return decode_uint64(xdr, fileid);
  155. }
  156. /*
  157. * filename3
  158. *
  159. * typedef string filename3<>;
  160. */
  161. static void encode_filename3(struct xdr_stream *xdr,
  162. const char *name, u32 length)
  163. {
  164. __be32 *p;
  165. WARN_ON_ONCE(length > NFS3_MAXNAMLEN);
  166. p = xdr_reserve_space(xdr, 4 + length);
  167. xdr_encode_opaque(p, name, length);
  168. }
  169. static int decode_inline_filename3(struct xdr_stream *xdr,
  170. const char **name, u32 *length)
  171. {
  172. __be32 *p;
  173. u32 count;
  174. p = xdr_inline_decode(xdr, 4);
  175. if (unlikely(!p))
  176. return -EIO;
  177. count = be32_to_cpup(p);
  178. if (count > NFS3_MAXNAMLEN)
  179. goto out_nametoolong;
  180. p = xdr_inline_decode(xdr, count);
  181. if (unlikely(!p))
  182. return -EIO;
  183. *name = (const char *)p;
  184. *length = count;
  185. return 0;
  186. out_nametoolong:
  187. dprintk("NFS: returned filename too long: %u\n", count);
  188. return -ENAMETOOLONG;
  189. }
  190. /*
  191. * nfspath3
  192. *
  193. * typedef string nfspath3<>;
  194. */
  195. static void encode_nfspath3(struct xdr_stream *xdr, struct page **pages,
  196. const u32 length)
  197. {
  198. encode_uint32(xdr, length);
  199. xdr_write_pages(xdr, pages, 0, length);
  200. }
  201. static int decode_nfspath3(struct xdr_stream *xdr)
  202. {
  203. u32 recvd, count;
  204. __be32 *p;
  205. p = xdr_inline_decode(xdr, 4);
  206. if (unlikely(!p))
  207. return -EIO;
  208. count = be32_to_cpup(p);
  209. if (unlikely(count >= xdr->buf->page_len || count > NFS3_MAXPATHLEN))
  210. goto out_nametoolong;
  211. recvd = xdr_read_pages(xdr, count);
  212. if (unlikely(count > recvd))
  213. goto out_cheating;
  214. xdr_terminate_string(xdr->buf, count);
  215. return 0;
  216. out_nametoolong:
  217. dprintk("NFS: returned pathname too long: %u\n", count);
  218. return -ENAMETOOLONG;
  219. out_cheating:
  220. dprintk("NFS: server cheating in pathname result: "
  221. "count %u > recvd %u\n", count, recvd);
  222. return -EIO;
  223. }
  224. /*
  225. * cookie3
  226. *
  227. * typedef uint64 cookie3
  228. */
  229. static __be32 *xdr_encode_cookie3(__be32 *p, u64 cookie)
  230. {
  231. return xdr_encode_hyper(p, cookie);
  232. }
  233. static int decode_cookie3(struct xdr_stream *xdr, u64 *cookie)
  234. {
  235. return decode_uint64(xdr, cookie);
  236. }
  237. /*
  238. * cookieverf3
  239. *
  240. * typedef opaque cookieverf3[NFS3_COOKIEVERFSIZE];
  241. */
  242. static __be32 *xdr_encode_cookieverf3(__be32 *p, const __be32 *verifier)
  243. {
  244. memcpy(p, verifier, NFS3_COOKIEVERFSIZE);
  245. return p + XDR_QUADLEN(NFS3_COOKIEVERFSIZE);
  246. }
  247. static int decode_cookieverf3(struct xdr_stream *xdr, __be32 *verifier)
  248. {
  249. __be32 *p;
  250. p = xdr_inline_decode(xdr, NFS3_COOKIEVERFSIZE);
  251. if (unlikely(!p))
  252. return -EIO;
  253. memcpy(verifier, p, NFS3_COOKIEVERFSIZE);
  254. return 0;
  255. }
  256. /*
  257. * createverf3
  258. *
  259. * typedef opaque createverf3[NFS3_CREATEVERFSIZE];
  260. */
  261. static void encode_createverf3(struct xdr_stream *xdr, const __be32 *verifier)
  262. {
  263. __be32 *p;
  264. p = xdr_reserve_space(xdr, NFS3_CREATEVERFSIZE);
  265. memcpy(p, verifier, NFS3_CREATEVERFSIZE);
  266. }
  267. static int decode_writeverf3(struct xdr_stream *xdr, struct nfs_write_verifier *verifier)
  268. {
  269. __be32 *p;
  270. p = xdr_inline_decode(xdr, NFS3_WRITEVERFSIZE);
  271. if (unlikely(!p))
  272. return -EIO;
  273. memcpy(verifier->data, p, NFS3_WRITEVERFSIZE);
  274. return 0;
  275. }
  276. /*
  277. * size3
  278. *
  279. * typedef uint64 size3;
  280. */
  281. static __be32 *xdr_decode_size3(__be32 *p, u64 *size)
  282. {
  283. return xdr_decode_hyper(p, size);
  284. }
  285. /*
  286. * nfsstat3
  287. *
  288. * enum nfsstat3 {
  289. * NFS3_OK = 0,
  290. * ...
  291. * }
  292. */
  293. #define NFS3_OK NFS_OK
  294. static int decode_nfsstat3(struct xdr_stream *xdr, enum nfs_stat *status)
  295. {
  296. __be32 *p;
  297. p = xdr_inline_decode(xdr, 4);
  298. if (unlikely(!p))
  299. return -EIO;
  300. if (unlikely(*p != cpu_to_be32(NFS3_OK)))
  301. goto out_status;
  302. *status = 0;
  303. return 0;
  304. out_status:
  305. *status = be32_to_cpup(p);
  306. trace_nfs_xdr_status(xdr, (int)*status);
  307. return 0;
  308. }
  309. /*
  310. * ftype3
  311. *
  312. * enum ftype3 {
  313. * NF3REG = 1,
  314. * NF3DIR = 2,
  315. * NF3BLK = 3,
  316. * NF3CHR = 4,
  317. * NF3LNK = 5,
  318. * NF3SOCK = 6,
  319. * NF3FIFO = 7
  320. * };
  321. */
  322. static void encode_ftype3(struct xdr_stream *xdr, const u32 type)
  323. {
  324. encode_uint32(xdr, type);
  325. }
  326. static __be32 *xdr_decode_ftype3(__be32 *p, umode_t *mode)
  327. {
  328. u32 type;
  329. type = be32_to_cpup(p++);
  330. if (type > NF3FIFO)
  331. type = NF3NON;
  332. *mode = nfs_type2fmt[type];
  333. return p;
  334. }
  335. /*
  336. * specdata3
  337. *
  338. * struct specdata3 {
  339. * uint32 specdata1;
  340. * uint32 specdata2;
  341. * };
  342. */
  343. static void encode_specdata3(struct xdr_stream *xdr, const dev_t rdev)
  344. {
  345. __be32 *p;
  346. p = xdr_reserve_space(xdr, 8);
  347. *p++ = cpu_to_be32(MAJOR(rdev));
  348. *p = cpu_to_be32(MINOR(rdev));
  349. }
  350. static __be32 *xdr_decode_specdata3(__be32 *p, dev_t *rdev)
  351. {
  352. unsigned int major, minor;
  353. major = be32_to_cpup(p++);
  354. minor = be32_to_cpup(p++);
  355. *rdev = MKDEV(major, minor);
  356. if (MAJOR(*rdev) != major || MINOR(*rdev) != minor)
  357. *rdev = 0;
  358. return p;
  359. }
  360. /*
  361. * nfs_fh3
  362. *
  363. * struct nfs_fh3 {
  364. * opaque data<NFS3_FHSIZE>;
  365. * };
  366. */
  367. static void encode_nfs_fh3(struct xdr_stream *xdr, const struct nfs_fh *fh)
  368. {
  369. __be32 *p;
  370. WARN_ON_ONCE(fh->size > NFS3_FHSIZE);
  371. p = xdr_reserve_space(xdr, 4 + fh->size);
  372. xdr_encode_opaque(p, fh->data, fh->size);
  373. }
  374. static int decode_nfs_fh3(struct xdr_stream *xdr, struct nfs_fh *fh)
  375. {
  376. u32 length;
  377. __be32 *p;
  378. p = xdr_inline_decode(xdr, 4);
  379. if (unlikely(!p))
  380. return -EIO;
  381. length = be32_to_cpup(p++);
  382. if (unlikely(length > NFS3_FHSIZE || length == 0))
  383. goto out_toobig;
  384. p = xdr_inline_decode(xdr, length);
  385. if (unlikely(!p))
  386. return -EIO;
  387. fh->size = length;
  388. memcpy(fh->data, p, length);
  389. return 0;
  390. out_toobig:
  391. trace_nfs_xdr_bad_filehandle(xdr, NFSERR_BADHANDLE);
  392. return -E2BIG;
  393. }
  394. static void zero_nfs_fh3(struct nfs_fh *fh)
  395. {
  396. memset(fh, 0, sizeof(*fh));
  397. }
  398. /*
  399. * nfstime3
  400. *
  401. * struct nfstime3 {
  402. * uint32 seconds;
  403. * uint32 nseconds;
  404. * };
  405. */
  406. static __be32 *xdr_encode_nfstime3(__be32 *p, const struct timespec64 *timep)
  407. {
  408. *p++ = cpu_to_be32((u32)timep->tv_sec);
  409. *p++ = cpu_to_be32(timep->tv_nsec);
  410. return p;
  411. }
  412. static __be32 *xdr_decode_nfstime3(__be32 *p, struct timespec64 *timep)
  413. {
  414. timep->tv_sec = be32_to_cpup(p++);
  415. timep->tv_nsec = be32_to_cpup(p++);
  416. return p;
  417. }
  418. /*
  419. * sattr3
  420. *
  421. * enum time_how {
  422. * DONT_CHANGE = 0,
  423. * SET_TO_SERVER_TIME = 1,
  424. * SET_TO_CLIENT_TIME = 2
  425. * };
  426. *
  427. * union set_mode3 switch (bool set_it) {
  428. * case TRUE:
  429. * mode3 mode;
  430. * default:
  431. * void;
  432. * };
  433. *
  434. * union set_uid3 switch (bool set_it) {
  435. * case TRUE:
  436. * uid3 uid;
  437. * default:
  438. * void;
  439. * };
  440. *
  441. * union set_gid3 switch (bool set_it) {
  442. * case TRUE:
  443. * gid3 gid;
  444. * default:
  445. * void;
  446. * };
  447. *
  448. * union set_size3 switch (bool set_it) {
  449. * case TRUE:
  450. * size3 size;
  451. * default:
  452. * void;
  453. * };
  454. *
  455. * union set_atime switch (time_how set_it) {
  456. * case SET_TO_CLIENT_TIME:
  457. * nfstime3 atime;
  458. * default:
  459. * void;
  460. * };
  461. *
  462. * union set_mtime switch (time_how set_it) {
  463. * case SET_TO_CLIENT_TIME:
  464. * nfstime3 mtime;
  465. * default:
  466. * void;
  467. * };
  468. *
  469. * struct sattr3 {
  470. * set_mode3 mode;
  471. * set_uid3 uid;
  472. * set_gid3 gid;
  473. * set_size3 size;
  474. * set_atime atime;
  475. * set_mtime mtime;
  476. * };
  477. */
  478. static void encode_sattr3(struct xdr_stream *xdr, const struct iattr *attr,
  479. struct user_namespace *userns)
  480. {
  481. u32 nbytes;
  482. __be32 *p;
  483. /*
  484. * In order to make only a single xdr_reserve_space() call,
  485. * pre-compute the total number of bytes to be reserved.
  486. * Six boolean values, one for each set_foo field, are always
  487. * present in the encoded result, so start there.
  488. */
  489. nbytes = 6 * 4;
  490. if (attr->ia_valid & ATTR_MODE)
  491. nbytes += 4;
  492. if (attr->ia_valid & ATTR_UID)
  493. nbytes += 4;
  494. if (attr->ia_valid & ATTR_GID)
  495. nbytes += 4;
  496. if (attr->ia_valid & ATTR_SIZE)
  497. nbytes += 8;
  498. if (attr->ia_valid & ATTR_ATIME_SET)
  499. nbytes += 8;
  500. if (attr->ia_valid & ATTR_MTIME_SET)
  501. nbytes += 8;
  502. p = xdr_reserve_space(xdr, nbytes);
  503. if (attr->ia_valid & ATTR_MODE) {
  504. *p++ = xdr_one;
  505. *p++ = cpu_to_be32(attr->ia_mode & S_IALLUGO);
  506. } else
  507. *p++ = xdr_zero;
  508. if (attr->ia_valid & ATTR_UID) {
  509. *p++ = xdr_one;
  510. *p++ = cpu_to_be32(from_kuid_munged(userns, attr->ia_uid));
  511. } else
  512. *p++ = xdr_zero;
  513. if (attr->ia_valid & ATTR_GID) {
  514. *p++ = xdr_one;
  515. *p++ = cpu_to_be32(from_kgid_munged(userns, attr->ia_gid));
  516. } else
  517. *p++ = xdr_zero;
  518. if (attr->ia_valid & ATTR_SIZE) {
  519. *p++ = xdr_one;
  520. p = xdr_encode_hyper(p, (u64)attr->ia_size);
  521. } else
  522. *p++ = xdr_zero;
  523. if (attr->ia_valid & ATTR_ATIME_SET) {
  524. *p++ = xdr_two;
  525. p = xdr_encode_nfstime3(p, &attr->ia_atime);
  526. } else if (attr->ia_valid & ATTR_ATIME) {
  527. *p++ = xdr_one;
  528. } else
  529. *p++ = xdr_zero;
  530. if (attr->ia_valid & ATTR_MTIME_SET) {
  531. *p++ = xdr_two;
  532. xdr_encode_nfstime3(p, &attr->ia_mtime);
  533. } else if (attr->ia_valid & ATTR_MTIME) {
  534. *p = xdr_one;
  535. } else
  536. *p = xdr_zero;
  537. }
  538. /*
  539. * fattr3
  540. *
  541. * struct fattr3 {
  542. * ftype3 type;
  543. * mode3 mode;
  544. * uint32 nlink;
  545. * uid3 uid;
  546. * gid3 gid;
  547. * size3 size;
  548. * size3 used;
  549. * specdata3 rdev;
  550. * uint64 fsid;
  551. * fileid3 fileid;
  552. * nfstime3 atime;
  553. * nfstime3 mtime;
  554. * nfstime3 ctime;
  555. * };
  556. */
  557. static int decode_fattr3(struct xdr_stream *xdr, struct nfs_fattr *fattr,
  558. struct user_namespace *userns)
  559. {
  560. umode_t fmode;
  561. __be32 *p;
  562. p = xdr_inline_decode(xdr, NFS3_fattr_sz << 2);
  563. if (unlikely(!p))
  564. return -EIO;
  565. p = xdr_decode_ftype3(p, &fmode);
  566. fattr->mode = (be32_to_cpup(p++) & ~S_IFMT) | fmode;
  567. fattr->nlink = be32_to_cpup(p++);
  568. fattr->uid = make_kuid(userns, be32_to_cpup(p++));
  569. if (!uid_valid(fattr->uid))
  570. goto out_uid;
  571. fattr->gid = make_kgid(userns, be32_to_cpup(p++));
  572. if (!gid_valid(fattr->gid))
  573. goto out_gid;
  574. p = xdr_decode_size3(p, &fattr->size);
  575. p = xdr_decode_size3(p, &fattr->du.nfs3.used);
  576. p = xdr_decode_specdata3(p, &fattr->rdev);
  577. p = xdr_decode_hyper(p, &fattr->fsid.major);
  578. fattr->fsid.minor = 0;
  579. p = xdr_decode_fileid3(p, &fattr->fileid);
  580. p = xdr_decode_nfstime3(p, &fattr->atime);
  581. p = xdr_decode_nfstime3(p, &fattr->mtime);
  582. xdr_decode_nfstime3(p, &fattr->ctime);
  583. fattr->change_attr = nfs_timespec_to_change_attr(&fattr->ctime);
  584. fattr->valid |= NFS_ATTR_FATTR_V3;
  585. return 0;
  586. out_uid:
  587. dprintk("NFS: returned invalid uid\n");
  588. return -EINVAL;
  589. out_gid:
  590. dprintk("NFS: returned invalid gid\n");
  591. return -EINVAL;
  592. }
  593. /*
  594. * post_op_attr
  595. *
  596. * union post_op_attr switch (bool attributes_follow) {
  597. * case TRUE:
  598. * fattr3 attributes;
  599. * case FALSE:
  600. * void;
  601. * };
  602. */
  603. static int decode_post_op_attr(struct xdr_stream *xdr, struct nfs_fattr *fattr,
  604. struct user_namespace *userns)
  605. {
  606. __be32 *p;
  607. p = xdr_inline_decode(xdr, 4);
  608. if (unlikely(!p))
  609. return -EIO;
  610. if (*p != xdr_zero)
  611. return decode_fattr3(xdr, fattr, userns);
  612. return 0;
  613. }
  614. /*
  615. * wcc_attr
  616. * struct wcc_attr {
  617. * size3 size;
  618. * nfstime3 mtime;
  619. * nfstime3 ctime;
  620. * };
  621. */
  622. static int decode_wcc_attr(struct xdr_stream *xdr, struct nfs_fattr *fattr)
  623. {
  624. __be32 *p;
  625. p = xdr_inline_decode(xdr, NFS3_wcc_attr_sz << 2);
  626. if (unlikely(!p))
  627. return -EIO;
  628. fattr->valid |= NFS_ATTR_FATTR_PRESIZE
  629. | NFS_ATTR_FATTR_PRECHANGE
  630. | NFS_ATTR_FATTR_PREMTIME
  631. | NFS_ATTR_FATTR_PRECTIME;
  632. p = xdr_decode_size3(p, &fattr->pre_size);
  633. p = xdr_decode_nfstime3(p, &fattr->pre_mtime);
  634. xdr_decode_nfstime3(p, &fattr->pre_ctime);
  635. fattr->pre_change_attr = nfs_timespec_to_change_attr(&fattr->pre_ctime);
  636. return 0;
  637. }
  638. /*
  639. * pre_op_attr
  640. * union pre_op_attr switch (bool attributes_follow) {
  641. * case TRUE:
  642. * wcc_attr attributes;
  643. * case FALSE:
  644. * void;
  645. * };
  646. *
  647. * wcc_data
  648. *
  649. * struct wcc_data {
  650. * pre_op_attr before;
  651. * post_op_attr after;
  652. * };
  653. */
  654. static int decode_pre_op_attr(struct xdr_stream *xdr, struct nfs_fattr *fattr)
  655. {
  656. __be32 *p;
  657. p = xdr_inline_decode(xdr, 4);
  658. if (unlikely(!p))
  659. return -EIO;
  660. if (*p != xdr_zero)
  661. return decode_wcc_attr(xdr, fattr);
  662. return 0;
  663. }
  664. static int decode_wcc_data(struct xdr_stream *xdr, struct nfs_fattr *fattr,
  665. struct user_namespace *userns)
  666. {
  667. int error;
  668. error = decode_pre_op_attr(xdr, fattr);
  669. if (unlikely(error))
  670. goto out;
  671. error = decode_post_op_attr(xdr, fattr, userns);
  672. out:
  673. return error;
  674. }
  675. /*
  676. * post_op_fh3
  677. *
  678. * union post_op_fh3 switch (bool handle_follows) {
  679. * case TRUE:
  680. * nfs_fh3 handle;
  681. * case FALSE:
  682. * void;
  683. * };
  684. */
  685. static int decode_post_op_fh3(struct xdr_stream *xdr, struct nfs_fh *fh)
  686. {
  687. __be32 *p = xdr_inline_decode(xdr, 4);
  688. if (unlikely(!p))
  689. return -EIO;
  690. if (*p != xdr_zero)
  691. return decode_nfs_fh3(xdr, fh);
  692. zero_nfs_fh3(fh);
  693. return 0;
  694. }
  695. /*
  696. * diropargs3
  697. *
  698. * struct diropargs3 {
  699. * nfs_fh3 dir;
  700. * filename3 name;
  701. * };
  702. */
  703. static void encode_diropargs3(struct xdr_stream *xdr, const struct nfs_fh *fh,
  704. const char *name, u32 length)
  705. {
  706. encode_nfs_fh3(xdr, fh);
  707. encode_filename3(xdr, name, length);
  708. }
  709. /*
  710. * NFSv3 XDR encode functions
  711. *
  712. * NFSv3 argument types are defined in section 3.3 of RFC 1813:
  713. * "NFS Version 3 Protocol Specification".
  714. */
  715. /*
  716. * 3.3.1 GETATTR3args
  717. *
  718. * struct GETATTR3args {
  719. * nfs_fh3 object;
  720. * };
  721. */
  722. static void nfs3_xdr_enc_getattr3args(struct rpc_rqst *req,
  723. struct xdr_stream *xdr,
  724. const void *data)
  725. {
  726. const struct nfs_fh *fh = data;
  727. encode_nfs_fh3(xdr, fh);
  728. }
  729. /*
  730. * 3.3.2 SETATTR3args
  731. *
  732. * union sattrguard3 switch (bool check) {
  733. * case TRUE:
  734. * nfstime3 obj_ctime;
  735. * case FALSE:
  736. * void;
  737. * };
  738. *
  739. * struct SETATTR3args {
  740. * nfs_fh3 object;
  741. * sattr3 new_attributes;
  742. * sattrguard3 guard;
  743. * };
  744. */
  745. static void encode_sattrguard3(struct xdr_stream *xdr,
  746. const struct nfs3_sattrargs *args)
  747. {
  748. __be32 *p;
  749. if (args->guard) {
  750. p = xdr_reserve_space(xdr, 4 + 8);
  751. *p++ = xdr_one;
  752. xdr_encode_nfstime3(p, &args->guardtime);
  753. } else {
  754. p = xdr_reserve_space(xdr, 4);
  755. *p = xdr_zero;
  756. }
  757. }
  758. static void nfs3_xdr_enc_setattr3args(struct rpc_rqst *req,
  759. struct xdr_stream *xdr,
  760. const void *data)
  761. {
  762. const struct nfs3_sattrargs *args = data;
  763. encode_nfs_fh3(xdr, args->fh);
  764. encode_sattr3(xdr, args->sattr, rpc_rqst_userns(req));
  765. encode_sattrguard3(xdr, args);
  766. }
  767. /*
  768. * 3.3.3 LOOKUP3args
  769. *
  770. * struct LOOKUP3args {
  771. * diropargs3 what;
  772. * };
  773. */
  774. static void nfs3_xdr_enc_lookup3args(struct rpc_rqst *req,
  775. struct xdr_stream *xdr,
  776. const void *data)
  777. {
  778. const struct nfs3_diropargs *args = data;
  779. encode_diropargs3(xdr, args->fh, args->name, args->len);
  780. }
  781. /*
  782. * 3.3.4 ACCESS3args
  783. *
  784. * struct ACCESS3args {
  785. * nfs_fh3 object;
  786. * uint32 access;
  787. * };
  788. */
  789. static void encode_access3args(struct xdr_stream *xdr,
  790. const struct nfs3_accessargs *args)
  791. {
  792. encode_nfs_fh3(xdr, args->fh);
  793. encode_uint32(xdr, args->access);
  794. }
  795. static void nfs3_xdr_enc_access3args(struct rpc_rqst *req,
  796. struct xdr_stream *xdr,
  797. const void *data)
  798. {
  799. const struct nfs3_accessargs *args = data;
  800. encode_access3args(xdr, args);
  801. }
  802. /*
  803. * 3.3.5 READLINK3args
  804. *
  805. * struct READLINK3args {
  806. * nfs_fh3 symlink;
  807. * };
  808. */
  809. static void nfs3_xdr_enc_readlink3args(struct rpc_rqst *req,
  810. struct xdr_stream *xdr,
  811. const void *data)
  812. {
  813. const struct nfs3_readlinkargs *args = data;
  814. encode_nfs_fh3(xdr, args->fh);
  815. rpc_prepare_reply_pages(req, args->pages, args->pgbase, args->pglen,
  816. NFS3_readlinkres_sz - NFS3_pagepad_sz);
  817. }
  818. /*
  819. * 3.3.6 READ3args
  820. *
  821. * struct READ3args {
  822. * nfs_fh3 file;
  823. * offset3 offset;
  824. * count3 count;
  825. * };
  826. */
  827. static void encode_read3args(struct xdr_stream *xdr,
  828. const struct nfs_pgio_args *args)
  829. {
  830. __be32 *p;
  831. encode_nfs_fh3(xdr, args->fh);
  832. p = xdr_reserve_space(xdr, 8 + 4);
  833. p = xdr_encode_hyper(p, args->offset);
  834. *p = cpu_to_be32(args->count);
  835. }
  836. static void nfs3_xdr_enc_read3args(struct rpc_rqst *req,
  837. struct xdr_stream *xdr,
  838. const void *data)
  839. {
  840. const struct nfs_pgio_args *args = data;
  841. unsigned int replen = args->replen ? args->replen :
  842. NFS3_readres_sz - NFS3_pagepad_sz;
  843. encode_read3args(xdr, args);
  844. rpc_prepare_reply_pages(req, args->pages, args->pgbase,
  845. args->count, replen);
  846. req->rq_rcv_buf.flags |= XDRBUF_READ;
  847. }
  848. /*
  849. * 3.3.7 WRITE3args
  850. *
  851. * enum stable_how {
  852. * UNSTABLE = 0,
  853. * DATA_SYNC = 1,
  854. * FILE_SYNC = 2
  855. * };
  856. *
  857. * struct WRITE3args {
  858. * nfs_fh3 file;
  859. * offset3 offset;
  860. * count3 count;
  861. * stable_how stable;
  862. * opaque data<>;
  863. * };
  864. */
  865. static void encode_write3args(struct xdr_stream *xdr,
  866. const struct nfs_pgio_args *args)
  867. {
  868. __be32 *p;
  869. encode_nfs_fh3(xdr, args->fh);
  870. p = xdr_reserve_space(xdr, 8 + 4 + 4 + 4);
  871. p = xdr_encode_hyper(p, args->offset);
  872. *p++ = cpu_to_be32(args->count);
  873. *p++ = cpu_to_be32(args->stable);
  874. *p = cpu_to_be32(args->count);
  875. xdr_write_pages(xdr, args->pages, args->pgbase, args->count);
  876. }
  877. static void nfs3_xdr_enc_write3args(struct rpc_rqst *req,
  878. struct xdr_stream *xdr,
  879. const void *data)
  880. {
  881. const struct nfs_pgio_args *args = data;
  882. encode_write3args(xdr, args);
  883. xdr->buf->flags |= XDRBUF_WRITE;
  884. }
  885. /*
  886. * 3.3.8 CREATE3args
  887. *
  888. * enum createmode3 {
  889. * UNCHECKED = 0,
  890. * GUARDED = 1,
  891. * EXCLUSIVE = 2
  892. * };
  893. *
  894. * union createhow3 switch (createmode3 mode) {
  895. * case UNCHECKED:
  896. * case GUARDED:
  897. * sattr3 obj_attributes;
  898. * case EXCLUSIVE:
  899. * createverf3 verf;
  900. * };
  901. *
  902. * struct CREATE3args {
  903. * diropargs3 where;
  904. * createhow3 how;
  905. * };
  906. */
  907. static void encode_createhow3(struct xdr_stream *xdr,
  908. const struct nfs3_createargs *args,
  909. struct user_namespace *userns)
  910. {
  911. encode_uint32(xdr, args->createmode);
  912. switch (args->createmode) {
  913. case NFS3_CREATE_UNCHECKED:
  914. case NFS3_CREATE_GUARDED:
  915. encode_sattr3(xdr, args->sattr, userns);
  916. break;
  917. case NFS3_CREATE_EXCLUSIVE:
  918. encode_createverf3(xdr, args->verifier);
  919. break;
  920. default:
  921. BUG();
  922. }
  923. }
  924. static void nfs3_xdr_enc_create3args(struct rpc_rqst *req,
  925. struct xdr_stream *xdr,
  926. const void *data)
  927. {
  928. const struct nfs3_createargs *args = data;
  929. encode_diropargs3(xdr, args->fh, args->name, args->len);
  930. encode_createhow3(xdr, args, rpc_rqst_userns(req));
  931. }
  932. /*
  933. * 3.3.9 MKDIR3args
  934. *
  935. * struct MKDIR3args {
  936. * diropargs3 where;
  937. * sattr3 attributes;
  938. * };
  939. */
  940. static void nfs3_xdr_enc_mkdir3args(struct rpc_rqst *req,
  941. struct xdr_stream *xdr,
  942. const void *data)
  943. {
  944. const struct nfs3_mkdirargs *args = data;
  945. encode_diropargs3(xdr, args->fh, args->name, args->len);
  946. encode_sattr3(xdr, args->sattr, rpc_rqst_userns(req));
  947. }
  948. /*
  949. * 3.3.10 SYMLINK3args
  950. *
  951. * struct symlinkdata3 {
  952. * sattr3 symlink_attributes;
  953. * nfspath3 symlink_data;
  954. * };
  955. *
  956. * struct SYMLINK3args {
  957. * diropargs3 where;
  958. * symlinkdata3 symlink;
  959. * };
  960. */
  961. static void encode_symlinkdata3(struct xdr_stream *xdr,
  962. const void *data,
  963. struct user_namespace *userns)
  964. {
  965. const struct nfs3_symlinkargs *args = data;
  966. encode_sattr3(xdr, args->sattr, userns);
  967. encode_nfspath3(xdr, args->pages, args->pathlen);
  968. }
  969. static void nfs3_xdr_enc_symlink3args(struct rpc_rqst *req,
  970. struct xdr_stream *xdr,
  971. const void *data)
  972. {
  973. const struct nfs3_symlinkargs *args = data;
  974. encode_diropargs3(xdr, args->fromfh, args->fromname, args->fromlen);
  975. encode_symlinkdata3(xdr, args, rpc_rqst_userns(req));
  976. xdr->buf->flags |= XDRBUF_WRITE;
  977. }
  978. /*
  979. * 3.3.11 MKNOD3args
  980. *
  981. * struct devicedata3 {
  982. * sattr3 dev_attributes;
  983. * specdata3 spec;
  984. * };
  985. *
  986. * union mknoddata3 switch (ftype3 type) {
  987. * case NF3CHR:
  988. * case NF3BLK:
  989. * devicedata3 device;
  990. * case NF3SOCK:
  991. * case NF3FIFO:
  992. * sattr3 pipe_attributes;
  993. * default:
  994. * void;
  995. * };
  996. *
  997. * struct MKNOD3args {
  998. * diropargs3 where;
  999. * mknoddata3 what;
  1000. * };
  1001. */
  1002. static void encode_devicedata3(struct xdr_stream *xdr,
  1003. const struct nfs3_mknodargs *args,
  1004. struct user_namespace *userns)
  1005. {
  1006. encode_sattr3(xdr, args->sattr, userns);
  1007. encode_specdata3(xdr, args->rdev);
  1008. }
  1009. static void encode_mknoddata3(struct xdr_stream *xdr,
  1010. const struct nfs3_mknodargs *args,
  1011. struct user_namespace *userns)
  1012. {
  1013. encode_ftype3(xdr, args->type);
  1014. switch (args->type) {
  1015. case NF3CHR:
  1016. case NF3BLK:
  1017. encode_devicedata3(xdr, args, userns);
  1018. break;
  1019. case NF3SOCK:
  1020. case NF3FIFO:
  1021. encode_sattr3(xdr, args->sattr, userns);
  1022. break;
  1023. case NF3REG:
  1024. case NF3DIR:
  1025. break;
  1026. default:
  1027. BUG();
  1028. }
  1029. }
  1030. static void nfs3_xdr_enc_mknod3args(struct rpc_rqst *req,
  1031. struct xdr_stream *xdr,
  1032. const void *data)
  1033. {
  1034. const struct nfs3_mknodargs *args = data;
  1035. encode_diropargs3(xdr, args->fh, args->name, args->len);
  1036. encode_mknoddata3(xdr, args, rpc_rqst_userns(req));
  1037. }
  1038. /*
  1039. * 3.3.12 REMOVE3args
  1040. *
  1041. * struct REMOVE3args {
  1042. * diropargs3 object;
  1043. * };
  1044. */
  1045. static void nfs3_xdr_enc_remove3args(struct rpc_rqst *req,
  1046. struct xdr_stream *xdr,
  1047. const void *data)
  1048. {
  1049. const struct nfs_removeargs *args = data;
  1050. encode_diropargs3(xdr, args->fh, args->name.name, args->name.len);
  1051. }
  1052. /*
  1053. * 3.3.14 RENAME3args
  1054. *
  1055. * struct RENAME3args {
  1056. * diropargs3 from;
  1057. * diropargs3 to;
  1058. * };
  1059. */
  1060. static void nfs3_xdr_enc_rename3args(struct rpc_rqst *req,
  1061. struct xdr_stream *xdr,
  1062. const void *data)
  1063. {
  1064. const struct nfs_renameargs *args = data;
  1065. const struct qstr *old = args->old_name;
  1066. const struct qstr *new = args->new_name;
  1067. encode_diropargs3(xdr, args->old_dir, old->name, old->len);
  1068. encode_diropargs3(xdr, args->new_dir, new->name, new->len);
  1069. }
  1070. /*
  1071. * 3.3.15 LINK3args
  1072. *
  1073. * struct LINK3args {
  1074. * nfs_fh3 file;
  1075. * diropargs3 link;
  1076. * };
  1077. */
  1078. static void nfs3_xdr_enc_link3args(struct rpc_rqst *req,
  1079. struct xdr_stream *xdr,
  1080. const void *data)
  1081. {
  1082. const struct nfs3_linkargs *args = data;
  1083. encode_nfs_fh3(xdr, args->fromfh);
  1084. encode_diropargs3(xdr, args->tofh, args->toname, args->tolen);
  1085. }
  1086. /*
  1087. * 3.3.16 READDIR3args
  1088. *
  1089. * struct READDIR3args {
  1090. * nfs_fh3 dir;
  1091. * cookie3 cookie;
  1092. * cookieverf3 cookieverf;
  1093. * count3 count;
  1094. * };
  1095. */
  1096. static void encode_readdir3args(struct xdr_stream *xdr,
  1097. const struct nfs3_readdirargs *args)
  1098. {
  1099. __be32 *p;
  1100. encode_nfs_fh3(xdr, args->fh);
  1101. p = xdr_reserve_space(xdr, 8 + NFS3_COOKIEVERFSIZE + 4);
  1102. p = xdr_encode_cookie3(p, args->cookie);
  1103. p = xdr_encode_cookieverf3(p, args->verf);
  1104. *p = cpu_to_be32(args->count);
  1105. }
  1106. static void nfs3_xdr_enc_readdir3args(struct rpc_rqst *req,
  1107. struct xdr_stream *xdr,
  1108. const void *data)
  1109. {
  1110. const struct nfs3_readdirargs *args = data;
  1111. encode_readdir3args(xdr, args);
  1112. rpc_prepare_reply_pages(req, args->pages, 0, args->count,
  1113. NFS3_readdirres_sz - NFS3_pagepad_sz);
  1114. }
  1115. /*
  1116. * 3.3.17 READDIRPLUS3args
  1117. *
  1118. * struct READDIRPLUS3args {
  1119. * nfs_fh3 dir;
  1120. * cookie3 cookie;
  1121. * cookieverf3 cookieverf;
  1122. * count3 dircount;
  1123. * count3 maxcount;
  1124. * };
  1125. */
  1126. static void encode_readdirplus3args(struct xdr_stream *xdr,
  1127. const struct nfs3_readdirargs *args)
  1128. {
  1129. uint32_t dircount = args->count;
  1130. uint32_t maxcount = args->count;
  1131. __be32 *p;
  1132. encode_nfs_fh3(xdr, args->fh);
  1133. p = xdr_reserve_space(xdr, 8 + NFS3_COOKIEVERFSIZE + 4 + 4);
  1134. p = xdr_encode_cookie3(p, args->cookie);
  1135. p = xdr_encode_cookieverf3(p, args->verf);
  1136. /*
  1137. * readdirplus: need dircount + buffer size.
  1138. * We just make sure we make dircount big enough
  1139. */
  1140. *p++ = cpu_to_be32(dircount);
  1141. *p = cpu_to_be32(maxcount);
  1142. }
  1143. static void nfs3_xdr_enc_readdirplus3args(struct rpc_rqst *req,
  1144. struct xdr_stream *xdr,
  1145. const void *data)
  1146. {
  1147. const struct nfs3_readdirargs *args = data;
  1148. encode_readdirplus3args(xdr, args);
  1149. rpc_prepare_reply_pages(req, args->pages, 0, args->count,
  1150. NFS3_readdirres_sz - NFS3_pagepad_sz);
  1151. }
  1152. /*
  1153. * 3.3.21 COMMIT3args
  1154. *
  1155. * struct COMMIT3args {
  1156. * nfs_fh3 file;
  1157. * offset3 offset;
  1158. * count3 count;
  1159. * };
  1160. */
  1161. static void encode_commit3args(struct xdr_stream *xdr,
  1162. const struct nfs_commitargs *args)
  1163. {
  1164. __be32 *p;
  1165. encode_nfs_fh3(xdr, args->fh);
  1166. p = xdr_reserve_space(xdr, 8 + 4);
  1167. p = xdr_encode_hyper(p, args->offset);
  1168. *p = cpu_to_be32(args->count);
  1169. }
  1170. static void nfs3_xdr_enc_commit3args(struct rpc_rqst *req,
  1171. struct xdr_stream *xdr,
  1172. const void *data)
  1173. {
  1174. const struct nfs_commitargs *args = data;
  1175. encode_commit3args(xdr, args);
  1176. }
  1177. #ifdef CONFIG_NFS_V3_ACL
  1178. static void nfs3_xdr_enc_getacl3args(struct rpc_rqst *req,
  1179. struct xdr_stream *xdr,
  1180. const void *data)
  1181. {
  1182. const struct nfs3_getaclargs *args = data;
  1183. encode_nfs_fh3(xdr, args->fh);
  1184. encode_uint32(xdr, args->mask);
  1185. if (args->mask & (NFS_ACL | NFS_DFACL)) {
  1186. rpc_prepare_reply_pages(req, args->pages, 0,
  1187. NFSACL_MAXPAGES << PAGE_SHIFT,
  1188. ACL3_getaclres_sz - NFS3_pagepad_sz);
  1189. req->rq_rcv_buf.flags |= XDRBUF_SPARSE_PAGES;
  1190. }
  1191. }
  1192. static void nfs3_xdr_enc_setacl3args(struct rpc_rqst *req,
  1193. struct xdr_stream *xdr,
  1194. const void *data)
  1195. {
  1196. const struct nfs3_setaclargs *args = data;
  1197. unsigned int base;
  1198. int error;
  1199. encode_nfs_fh3(xdr, NFS_FH(args->inode));
  1200. encode_uint32(xdr, args->mask);
  1201. base = req->rq_slen;
  1202. if (args->npages != 0)
  1203. xdr_write_pages(xdr, args->pages, 0, args->len);
  1204. else
  1205. xdr_reserve_space(xdr, args->len);
  1206. error = nfsacl_encode(xdr->buf, base, args->inode,
  1207. (args->mask & NFS_ACL) ?
  1208. args->acl_access : NULL, 1, 0);
  1209. /* FIXME: this is just broken */
  1210. BUG_ON(error < 0);
  1211. error = nfsacl_encode(xdr->buf, base + error, args->inode,
  1212. (args->mask & NFS_DFACL) ?
  1213. args->acl_default : NULL, 1,
  1214. NFS_ACL_DEFAULT);
  1215. BUG_ON(error < 0);
  1216. }
  1217. #endif /* CONFIG_NFS_V3_ACL */
  1218. /*
  1219. * NFSv3 XDR decode functions
  1220. *
  1221. * NFSv3 result types are defined in section 3.3 of RFC 1813:
  1222. * "NFS Version 3 Protocol Specification".
  1223. */
  1224. /*
  1225. * 3.3.1 GETATTR3res
  1226. *
  1227. * struct GETATTR3resok {
  1228. * fattr3 obj_attributes;
  1229. * };
  1230. *
  1231. * union GETATTR3res switch (nfsstat3 status) {
  1232. * case NFS3_OK:
  1233. * GETATTR3resok resok;
  1234. * default:
  1235. * void;
  1236. * };
  1237. */
  1238. static int nfs3_xdr_dec_getattr3res(struct rpc_rqst *req,
  1239. struct xdr_stream *xdr,
  1240. void *result)
  1241. {
  1242. enum nfs_stat status;
  1243. int error;
  1244. error = decode_nfsstat3(xdr, &status);
  1245. if (unlikely(error))
  1246. goto out;
  1247. if (status != NFS3_OK)
  1248. goto out_default;
  1249. error = decode_fattr3(xdr, result, rpc_rqst_userns(req));
  1250. out:
  1251. return error;
  1252. out_default:
  1253. return nfs_stat_to_errno(status);
  1254. }
  1255. /*
  1256. * 3.3.2 SETATTR3res
  1257. *
  1258. * struct SETATTR3resok {
  1259. * wcc_data obj_wcc;
  1260. * };
  1261. *
  1262. * struct SETATTR3resfail {
  1263. * wcc_data obj_wcc;
  1264. * };
  1265. *
  1266. * union SETATTR3res switch (nfsstat3 status) {
  1267. * case NFS3_OK:
  1268. * SETATTR3resok resok;
  1269. * default:
  1270. * SETATTR3resfail resfail;
  1271. * };
  1272. */
  1273. static int nfs3_xdr_dec_setattr3res(struct rpc_rqst *req,
  1274. struct xdr_stream *xdr,
  1275. void *result)
  1276. {
  1277. enum nfs_stat status;
  1278. int error;
  1279. error = decode_nfsstat3(xdr, &status);
  1280. if (unlikely(error))
  1281. goto out;
  1282. error = decode_wcc_data(xdr, result, rpc_rqst_userns(req));
  1283. if (unlikely(error))
  1284. goto out;
  1285. if (status != NFS3_OK)
  1286. goto out_status;
  1287. out:
  1288. return error;
  1289. out_status:
  1290. return nfs_stat_to_errno(status);
  1291. }
  1292. /*
  1293. * 3.3.3 LOOKUP3res
  1294. *
  1295. * struct LOOKUP3resok {
  1296. * nfs_fh3 object;
  1297. * post_op_attr obj_attributes;
  1298. * post_op_attr dir_attributes;
  1299. * };
  1300. *
  1301. * struct LOOKUP3resfail {
  1302. * post_op_attr dir_attributes;
  1303. * };
  1304. *
  1305. * union LOOKUP3res switch (nfsstat3 status) {
  1306. * case NFS3_OK:
  1307. * LOOKUP3resok resok;
  1308. * default:
  1309. * LOOKUP3resfail resfail;
  1310. * };
  1311. */
  1312. static int nfs3_xdr_dec_lookup3res(struct rpc_rqst *req,
  1313. struct xdr_stream *xdr,
  1314. void *data)
  1315. {
  1316. struct user_namespace *userns = rpc_rqst_userns(req);
  1317. struct nfs3_diropres *result = data;
  1318. enum nfs_stat status;
  1319. int error;
  1320. error = decode_nfsstat3(xdr, &status);
  1321. if (unlikely(error))
  1322. goto out;
  1323. if (status != NFS3_OK)
  1324. goto out_default;
  1325. error = decode_nfs_fh3(xdr, result->fh);
  1326. if (unlikely(error))
  1327. goto out;
  1328. error = decode_post_op_attr(xdr, result->fattr, userns);
  1329. if (unlikely(error))
  1330. goto out;
  1331. error = decode_post_op_attr(xdr, result->dir_attr, userns);
  1332. out:
  1333. return error;
  1334. out_default:
  1335. error = decode_post_op_attr(xdr, result->dir_attr, userns);
  1336. if (unlikely(error))
  1337. goto out;
  1338. return nfs_stat_to_errno(status);
  1339. }
  1340. /*
  1341. * 3.3.4 ACCESS3res
  1342. *
  1343. * struct ACCESS3resok {
  1344. * post_op_attr obj_attributes;
  1345. * uint32 access;
  1346. * };
  1347. *
  1348. * struct ACCESS3resfail {
  1349. * post_op_attr obj_attributes;
  1350. * };
  1351. *
  1352. * union ACCESS3res switch (nfsstat3 status) {
  1353. * case NFS3_OK:
  1354. * ACCESS3resok resok;
  1355. * default:
  1356. * ACCESS3resfail resfail;
  1357. * };
  1358. */
  1359. static int nfs3_xdr_dec_access3res(struct rpc_rqst *req,
  1360. struct xdr_stream *xdr,
  1361. void *data)
  1362. {
  1363. struct nfs3_accessres *result = data;
  1364. enum nfs_stat status;
  1365. int error;
  1366. error = decode_nfsstat3(xdr, &status);
  1367. if (unlikely(error))
  1368. goto out;
  1369. error = decode_post_op_attr(xdr, result->fattr, rpc_rqst_userns(req));
  1370. if (unlikely(error))
  1371. goto out;
  1372. if (status != NFS3_OK)
  1373. goto out_default;
  1374. error = decode_uint32(xdr, &result->access);
  1375. out:
  1376. return error;
  1377. out_default:
  1378. return nfs_stat_to_errno(status);
  1379. }
  1380. /*
  1381. * 3.3.5 READLINK3res
  1382. *
  1383. * struct READLINK3resok {
  1384. * post_op_attr symlink_attributes;
  1385. * nfspath3 data;
  1386. * };
  1387. *
  1388. * struct READLINK3resfail {
  1389. * post_op_attr symlink_attributes;
  1390. * };
  1391. *
  1392. * union READLINK3res switch (nfsstat3 status) {
  1393. * case NFS3_OK:
  1394. * READLINK3resok resok;
  1395. * default:
  1396. * READLINK3resfail resfail;
  1397. * };
  1398. */
  1399. static int nfs3_xdr_dec_readlink3res(struct rpc_rqst *req,
  1400. struct xdr_stream *xdr,
  1401. void *result)
  1402. {
  1403. enum nfs_stat status;
  1404. int error;
  1405. error = decode_nfsstat3(xdr, &status);
  1406. if (unlikely(error))
  1407. goto out;
  1408. error = decode_post_op_attr(xdr, result, rpc_rqst_userns(req));
  1409. if (unlikely(error))
  1410. goto out;
  1411. if (status != NFS3_OK)
  1412. goto out_default;
  1413. error = decode_nfspath3(xdr);
  1414. out:
  1415. return error;
  1416. out_default:
  1417. return nfs_stat_to_errno(status);
  1418. }
  1419. /*
  1420. * 3.3.6 READ3res
  1421. *
  1422. * struct READ3resok {
  1423. * post_op_attr file_attributes;
  1424. * count3 count;
  1425. * bool eof;
  1426. * opaque data<>;
  1427. * };
  1428. *
  1429. * struct READ3resfail {
  1430. * post_op_attr file_attributes;
  1431. * };
  1432. *
  1433. * union READ3res switch (nfsstat3 status) {
  1434. * case NFS3_OK:
  1435. * READ3resok resok;
  1436. * default:
  1437. * READ3resfail resfail;
  1438. * };
  1439. */
  1440. static int decode_read3resok(struct xdr_stream *xdr,
  1441. struct nfs_pgio_res *result)
  1442. {
  1443. u32 eof, count, ocount, recvd;
  1444. __be32 *p;
  1445. p = xdr_inline_decode(xdr, 4 + 4 + 4);
  1446. if (unlikely(!p))
  1447. return -EIO;
  1448. count = be32_to_cpup(p++);
  1449. eof = be32_to_cpup(p++);
  1450. ocount = be32_to_cpup(p++);
  1451. if (unlikely(ocount != count))
  1452. goto out_mismatch;
  1453. recvd = xdr_read_pages(xdr, count);
  1454. if (unlikely(count > recvd))
  1455. goto out_cheating;
  1456. out:
  1457. result->eof = eof;
  1458. result->count = count;
  1459. return count;
  1460. out_mismatch:
  1461. dprintk("NFS: READ count doesn't match length of opaque: "
  1462. "count %u != ocount %u\n", count, ocount);
  1463. return -EIO;
  1464. out_cheating:
  1465. dprintk("NFS: server cheating in read result: "
  1466. "count %u > recvd %u\n", count, recvd);
  1467. count = recvd;
  1468. eof = 0;
  1469. goto out;
  1470. }
  1471. static int nfs3_xdr_dec_read3res(struct rpc_rqst *req, struct xdr_stream *xdr,
  1472. void *data)
  1473. {
  1474. struct nfs_pgio_res *result = data;
  1475. unsigned int pos;
  1476. enum nfs_stat status;
  1477. int error;
  1478. pos = xdr_stream_pos(xdr);
  1479. error = decode_nfsstat3(xdr, &status);
  1480. if (unlikely(error))
  1481. goto out;
  1482. error = decode_post_op_attr(xdr, result->fattr, rpc_rqst_userns(req));
  1483. if (unlikely(error))
  1484. goto out;
  1485. result->op_status = status;
  1486. if (status != NFS3_OK)
  1487. goto out_status;
  1488. result->replen = 3 + ((xdr_stream_pos(xdr) - pos) >> 2);
  1489. error = decode_read3resok(xdr, result);
  1490. out:
  1491. return error;
  1492. out_status:
  1493. return nfs_stat_to_errno(status);
  1494. }
  1495. /*
  1496. * 3.3.7 WRITE3res
  1497. *
  1498. * enum stable_how {
  1499. * UNSTABLE = 0,
  1500. * DATA_SYNC = 1,
  1501. * FILE_SYNC = 2
  1502. * };
  1503. *
  1504. * struct WRITE3resok {
  1505. * wcc_data file_wcc;
  1506. * count3 count;
  1507. * stable_how committed;
  1508. * writeverf3 verf;
  1509. * };
  1510. *
  1511. * struct WRITE3resfail {
  1512. * wcc_data file_wcc;
  1513. * };
  1514. *
  1515. * union WRITE3res switch (nfsstat3 status) {
  1516. * case NFS3_OK:
  1517. * WRITE3resok resok;
  1518. * default:
  1519. * WRITE3resfail resfail;
  1520. * };
  1521. */
  1522. static int decode_write3resok(struct xdr_stream *xdr,
  1523. struct nfs_pgio_res *result)
  1524. {
  1525. __be32 *p;
  1526. p = xdr_inline_decode(xdr, 4 + 4);
  1527. if (unlikely(!p))
  1528. return -EIO;
  1529. result->count = be32_to_cpup(p++);
  1530. result->verf->committed = be32_to_cpup(p++);
  1531. if (unlikely(result->verf->committed > NFS_FILE_SYNC))
  1532. goto out_badvalue;
  1533. if (decode_writeverf3(xdr, &result->verf->verifier))
  1534. return -EIO;
  1535. return result->count;
  1536. out_badvalue:
  1537. dprintk("NFS: bad stable_how value: %u\n", result->verf->committed);
  1538. return -EIO;
  1539. }
  1540. static int nfs3_xdr_dec_write3res(struct rpc_rqst *req, struct xdr_stream *xdr,
  1541. void *data)
  1542. {
  1543. struct nfs_pgio_res *result = data;
  1544. enum nfs_stat status;
  1545. int error;
  1546. error = decode_nfsstat3(xdr, &status);
  1547. if (unlikely(error))
  1548. goto out;
  1549. error = decode_wcc_data(xdr, result->fattr, rpc_rqst_userns(req));
  1550. if (unlikely(error))
  1551. goto out;
  1552. result->op_status = status;
  1553. if (status != NFS3_OK)
  1554. goto out_status;
  1555. error = decode_write3resok(xdr, result);
  1556. out:
  1557. return error;
  1558. out_status:
  1559. return nfs_stat_to_errno(status);
  1560. }
  1561. /*
  1562. * 3.3.8 CREATE3res
  1563. *
  1564. * struct CREATE3resok {
  1565. * post_op_fh3 obj;
  1566. * post_op_attr obj_attributes;
  1567. * wcc_data dir_wcc;
  1568. * };
  1569. *
  1570. * struct CREATE3resfail {
  1571. * wcc_data dir_wcc;
  1572. * };
  1573. *
  1574. * union CREATE3res switch (nfsstat3 status) {
  1575. * case NFS3_OK:
  1576. * CREATE3resok resok;
  1577. * default:
  1578. * CREATE3resfail resfail;
  1579. * };
  1580. */
  1581. static int decode_create3resok(struct xdr_stream *xdr,
  1582. struct nfs3_diropres *result,
  1583. struct user_namespace *userns)
  1584. {
  1585. int error;
  1586. error = decode_post_op_fh3(xdr, result->fh);
  1587. if (unlikely(error))
  1588. goto out;
  1589. error = decode_post_op_attr(xdr, result->fattr, userns);
  1590. if (unlikely(error))
  1591. goto out;
  1592. /* The server isn't required to return a file handle.
  1593. * If it didn't, force the client to perform a LOOKUP
  1594. * to determine the correct file handle and attribute
  1595. * values for the new object. */
  1596. if (result->fh->size == 0)
  1597. result->fattr->valid = 0;
  1598. error = decode_wcc_data(xdr, result->dir_attr, userns);
  1599. out:
  1600. return error;
  1601. }
  1602. static int nfs3_xdr_dec_create3res(struct rpc_rqst *req,
  1603. struct xdr_stream *xdr,
  1604. void *data)
  1605. {
  1606. struct user_namespace *userns = rpc_rqst_userns(req);
  1607. struct nfs3_diropres *result = data;
  1608. enum nfs_stat status;
  1609. int error;
  1610. error = decode_nfsstat3(xdr, &status);
  1611. if (unlikely(error))
  1612. goto out;
  1613. if (status != NFS3_OK)
  1614. goto out_default;
  1615. error = decode_create3resok(xdr, result, userns);
  1616. out:
  1617. return error;
  1618. out_default:
  1619. error = decode_wcc_data(xdr, result->dir_attr, userns);
  1620. if (unlikely(error))
  1621. goto out;
  1622. return nfs_stat_to_errno(status);
  1623. }
  1624. /*
  1625. * 3.3.12 REMOVE3res
  1626. *
  1627. * struct REMOVE3resok {
  1628. * wcc_data dir_wcc;
  1629. * };
  1630. *
  1631. * struct REMOVE3resfail {
  1632. * wcc_data dir_wcc;
  1633. * };
  1634. *
  1635. * union REMOVE3res switch (nfsstat3 status) {
  1636. * case NFS3_OK:
  1637. * REMOVE3resok resok;
  1638. * default:
  1639. * REMOVE3resfail resfail;
  1640. * };
  1641. */
  1642. static int nfs3_xdr_dec_remove3res(struct rpc_rqst *req,
  1643. struct xdr_stream *xdr,
  1644. void *data)
  1645. {
  1646. struct nfs_removeres *result = data;
  1647. enum nfs_stat status;
  1648. int error;
  1649. error = decode_nfsstat3(xdr, &status);
  1650. if (unlikely(error))
  1651. goto out;
  1652. error = decode_wcc_data(xdr, result->dir_attr, rpc_rqst_userns(req));
  1653. if (unlikely(error))
  1654. goto out;
  1655. if (status != NFS3_OK)
  1656. goto out_status;
  1657. out:
  1658. return error;
  1659. out_status:
  1660. return nfs_stat_to_errno(status);
  1661. }
  1662. /*
  1663. * 3.3.14 RENAME3res
  1664. *
  1665. * struct RENAME3resok {
  1666. * wcc_data fromdir_wcc;
  1667. * wcc_data todir_wcc;
  1668. * };
  1669. *
  1670. * struct RENAME3resfail {
  1671. * wcc_data fromdir_wcc;
  1672. * wcc_data todir_wcc;
  1673. * };
  1674. *
  1675. * union RENAME3res switch (nfsstat3 status) {
  1676. * case NFS3_OK:
  1677. * RENAME3resok resok;
  1678. * default:
  1679. * RENAME3resfail resfail;
  1680. * };
  1681. */
  1682. static int nfs3_xdr_dec_rename3res(struct rpc_rqst *req,
  1683. struct xdr_stream *xdr,
  1684. void *data)
  1685. {
  1686. struct user_namespace *userns = rpc_rqst_userns(req);
  1687. struct nfs_renameres *result = data;
  1688. enum nfs_stat status;
  1689. int error;
  1690. error = decode_nfsstat3(xdr, &status);
  1691. if (unlikely(error))
  1692. goto out;
  1693. error = decode_wcc_data(xdr, result->old_fattr, userns);
  1694. if (unlikely(error))
  1695. goto out;
  1696. error = decode_wcc_data(xdr, result->new_fattr, userns);
  1697. if (unlikely(error))
  1698. goto out;
  1699. if (status != NFS3_OK)
  1700. goto out_status;
  1701. out:
  1702. return error;
  1703. out_status:
  1704. return nfs_stat_to_errno(status);
  1705. }
  1706. /*
  1707. * 3.3.15 LINK3res
  1708. *
  1709. * struct LINK3resok {
  1710. * post_op_attr file_attributes;
  1711. * wcc_data linkdir_wcc;
  1712. * };
  1713. *
  1714. * struct LINK3resfail {
  1715. * post_op_attr file_attributes;
  1716. * wcc_data linkdir_wcc;
  1717. * };
  1718. *
  1719. * union LINK3res switch (nfsstat3 status) {
  1720. * case NFS3_OK:
  1721. * LINK3resok resok;
  1722. * default:
  1723. * LINK3resfail resfail;
  1724. * };
  1725. */
  1726. static int nfs3_xdr_dec_link3res(struct rpc_rqst *req, struct xdr_stream *xdr,
  1727. void *data)
  1728. {
  1729. struct user_namespace *userns = rpc_rqst_userns(req);
  1730. struct nfs3_linkres *result = data;
  1731. enum nfs_stat status;
  1732. int error;
  1733. error = decode_nfsstat3(xdr, &status);
  1734. if (unlikely(error))
  1735. goto out;
  1736. error = decode_post_op_attr(xdr, result->fattr, userns);
  1737. if (unlikely(error))
  1738. goto out;
  1739. error = decode_wcc_data(xdr, result->dir_attr, userns);
  1740. if (unlikely(error))
  1741. goto out;
  1742. if (status != NFS3_OK)
  1743. goto out_status;
  1744. out:
  1745. return error;
  1746. out_status:
  1747. return nfs_stat_to_errno(status);
  1748. }
  1749. /**
  1750. * nfs3_decode_dirent - Decode a single NFSv3 directory entry stored in
  1751. * the local page cache
  1752. * @xdr: XDR stream where entry resides
  1753. * @entry: buffer to fill in with entry data
  1754. * @plus: boolean indicating whether this should be a readdirplus entry
  1755. *
  1756. * Returns zero if successful, otherwise a negative errno value is
  1757. * returned.
  1758. *
  1759. * This function is not invoked during READDIR reply decoding, but
  1760. * rather whenever an application invokes the getdents(2) system call
  1761. * on a directory already in our cache.
  1762. *
  1763. * 3.3.16 entry3
  1764. *
  1765. * struct entry3 {
  1766. * fileid3 fileid;
  1767. * filename3 name;
  1768. * cookie3 cookie;
  1769. * fhandle3 filehandle;
  1770. * post_op_attr3 attributes;
  1771. * entry3 *nextentry;
  1772. * };
  1773. *
  1774. * 3.3.17 entryplus3
  1775. * struct entryplus3 {
  1776. * fileid3 fileid;
  1777. * filename3 name;
  1778. * cookie3 cookie;
  1779. * post_op_attr name_attributes;
  1780. * post_op_fh3 name_handle;
  1781. * entryplus3 *nextentry;
  1782. * };
  1783. */
  1784. int nfs3_decode_dirent(struct xdr_stream *xdr, struct nfs_entry *entry,
  1785. bool plus)
  1786. {
  1787. struct user_namespace *userns = rpc_userns(entry->server->client);
  1788. __be32 *p;
  1789. int error;
  1790. u64 new_cookie;
  1791. p = xdr_inline_decode(xdr, 4);
  1792. if (unlikely(!p))
  1793. return -EAGAIN;
  1794. if (*p == xdr_zero) {
  1795. p = xdr_inline_decode(xdr, 4);
  1796. if (unlikely(!p))
  1797. return -EAGAIN;
  1798. if (*p == xdr_zero)
  1799. return -EAGAIN;
  1800. entry->eof = 1;
  1801. return -EBADCOOKIE;
  1802. }
  1803. error = decode_fileid3(xdr, &entry->ino);
  1804. if (unlikely(error))
  1805. return -EAGAIN;
  1806. error = decode_inline_filename3(xdr, &entry->name, &entry->len);
  1807. if (unlikely(error))
  1808. return error == -ENAMETOOLONG ? -ENAMETOOLONG : -EAGAIN;
  1809. error = decode_cookie3(xdr, &new_cookie);
  1810. if (unlikely(error))
  1811. return -EAGAIN;
  1812. entry->d_type = DT_UNKNOWN;
  1813. if (plus) {
  1814. entry->fattr->valid = 0;
  1815. error = decode_post_op_attr(xdr, entry->fattr, userns);
  1816. if (unlikely(error))
  1817. return -EAGAIN;
  1818. if (entry->fattr->valid & NFS_ATTR_FATTR_V3)
  1819. entry->d_type = nfs_umode_to_dtype(entry->fattr->mode);
  1820. if (entry->fattr->fileid != entry->ino) {
  1821. entry->fattr->mounted_on_fileid = entry->ino;
  1822. entry->fattr->valid |= NFS_ATTR_FATTR_MOUNTED_ON_FILEID;
  1823. }
  1824. /* In fact, a post_op_fh3: */
  1825. p = xdr_inline_decode(xdr, 4);
  1826. if (unlikely(!p))
  1827. return -EAGAIN;
  1828. if (*p != xdr_zero) {
  1829. error = decode_nfs_fh3(xdr, entry->fh);
  1830. if (unlikely(error))
  1831. return -EAGAIN;
  1832. } else
  1833. zero_nfs_fh3(entry->fh);
  1834. }
  1835. entry->cookie = new_cookie;
  1836. return 0;
  1837. }
  1838. /*
  1839. * 3.3.16 READDIR3res
  1840. *
  1841. * struct dirlist3 {
  1842. * entry3 *entries;
  1843. * bool eof;
  1844. * };
  1845. *
  1846. * struct READDIR3resok {
  1847. * post_op_attr dir_attributes;
  1848. * cookieverf3 cookieverf;
  1849. * dirlist3 reply;
  1850. * };
  1851. *
  1852. * struct READDIR3resfail {
  1853. * post_op_attr dir_attributes;
  1854. * };
  1855. *
  1856. * union READDIR3res switch (nfsstat3 status) {
  1857. * case NFS3_OK:
  1858. * READDIR3resok resok;
  1859. * default:
  1860. * READDIR3resfail resfail;
  1861. * };
  1862. *
  1863. * Read the directory contents into the page cache, but otherwise
  1864. * don't touch them. The actual decoding is done by nfs3_decode_entry()
  1865. * during subsequent nfs_readdir() calls.
  1866. */
  1867. static int decode_dirlist3(struct xdr_stream *xdr)
  1868. {
  1869. return xdr_read_pages(xdr, xdr->buf->page_len);
  1870. }
  1871. static int decode_readdir3resok(struct xdr_stream *xdr,
  1872. struct nfs3_readdirres *result,
  1873. struct user_namespace *userns)
  1874. {
  1875. int error;
  1876. error = decode_post_op_attr(xdr, result->dir_attr, userns);
  1877. if (unlikely(error))
  1878. goto out;
  1879. /* XXX: do we need to check if result->verf != NULL ? */
  1880. error = decode_cookieverf3(xdr, result->verf);
  1881. if (unlikely(error))
  1882. goto out;
  1883. error = decode_dirlist3(xdr);
  1884. out:
  1885. return error;
  1886. }
  1887. static int nfs3_xdr_dec_readdir3res(struct rpc_rqst *req,
  1888. struct xdr_stream *xdr,
  1889. void *data)
  1890. {
  1891. struct nfs3_readdirres *result = data;
  1892. enum nfs_stat status;
  1893. int error;
  1894. error = decode_nfsstat3(xdr, &status);
  1895. if (unlikely(error))
  1896. goto out;
  1897. if (status != NFS3_OK)
  1898. goto out_default;
  1899. error = decode_readdir3resok(xdr, result, rpc_rqst_userns(req));
  1900. out:
  1901. return error;
  1902. out_default:
  1903. error = decode_post_op_attr(xdr, result->dir_attr, rpc_rqst_userns(req));
  1904. if (unlikely(error))
  1905. goto out;
  1906. return nfs_stat_to_errno(status);
  1907. }
  1908. /*
  1909. * 3.3.18 FSSTAT3res
  1910. *
  1911. * struct FSSTAT3resok {
  1912. * post_op_attr obj_attributes;
  1913. * size3 tbytes;
  1914. * size3 fbytes;
  1915. * size3 abytes;
  1916. * size3 tfiles;
  1917. * size3 ffiles;
  1918. * size3 afiles;
  1919. * uint32 invarsec;
  1920. * };
  1921. *
  1922. * struct FSSTAT3resfail {
  1923. * post_op_attr obj_attributes;
  1924. * };
  1925. *
  1926. * union FSSTAT3res switch (nfsstat3 status) {
  1927. * case NFS3_OK:
  1928. * FSSTAT3resok resok;
  1929. * default:
  1930. * FSSTAT3resfail resfail;
  1931. * };
  1932. */
  1933. static int decode_fsstat3resok(struct xdr_stream *xdr,
  1934. struct nfs_fsstat *result)
  1935. {
  1936. __be32 *p;
  1937. p = xdr_inline_decode(xdr, 8 * 6 + 4);
  1938. if (unlikely(!p))
  1939. return -EIO;
  1940. p = xdr_decode_size3(p, &result->tbytes);
  1941. p = xdr_decode_size3(p, &result->fbytes);
  1942. p = xdr_decode_size3(p, &result->abytes);
  1943. p = xdr_decode_size3(p, &result->tfiles);
  1944. p = xdr_decode_size3(p, &result->ffiles);
  1945. xdr_decode_size3(p, &result->afiles);
  1946. /* ignore invarsec */
  1947. return 0;
  1948. }
  1949. static int nfs3_xdr_dec_fsstat3res(struct rpc_rqst *req,
  1950. struct xdr_stream *xdr,
  1951. void *data)
  1952. {
  1953. struct nfs_fsstat *result = data;
  1954. enum nfs_stat status;
  1955. int error;
  1956. error = decode_nfsstat3(xdr, &status);
  1957. if (unlikely(error))
  1958. goto out;
  1959. error = decode_post_op_attr(xdr, result->fattr, rpc_rqst_userns(req));
  1960. if (unlikely(error))
  1961. goto out;
  1962. if (status != NFS3_OK)
  1963. goto out_status;
  1964. error = decode_fsstat3resok(xdr, result);
  1965. out:
  1966. return error;
  1967. out_status:
  1968. return nfs_stat_to_errno(status);
  1969. }
  1970. /*
  1971. * 3.3.19 FSINFO3res
  1972. *
  1973. * struct FSINFO3resok {
  1974. * post_op_attr obj_attributes;
  1975. * uint32 rtmax;
  1976. * uint32 rtpref;
  1977. * uint32 rtmult;
  1978. * uint32 wtmax;
  1979. * uint32 wtpref;
  1980. * uint32 wtmult;
  1981. * uint32 dtpref;
  1982. * size3 maxfilesize;
  1983. * nfstime3 time_delta;
  1984. * uint32 properties;
  1985. * };
  1986. *
  1987. * struct FSINFO3resfail {
  1988. * post_op_attr obj_attributes;
  1989. * };
  1990. *
  1991. * union FSINFO3res switch (nfsstat3 status) {
  1992. * case NFS3_OK:
  1993. * FSINFO3resok resok;
  1994. * default:
  1995. * FSINFO3resfail resfail;
  1996. * };
  1997. */
  1998. static int decode_fsinfo3resok(struct xdr_stream *xdr,
  1999. struct nfs_fsinfo *result)
  2000. {
  2001. __be32 *p;
  2002. p = xdr_inline_decode(xdr, 4 * 7 + 8 + 8 + 4);
  2003. if (unlikely(!p))
  2004. return -EIO;
  2005. result->rtmax = be32_to_cpup(p++);
  2006. result->rtpref = be32_to_cpup(p++);
  2007. result->rtmult = be32_to_cpup(p++);
  2008. result->wtmax = be32_to_cpup(p++);
  2009. result->wtpref = be32_to_cpup(p++);
  2010. result->wtmult = be32_to_cpup(p++);
  2011. result->dtpref = be32_to_cpup(p++);
  2012. p = xdr_decode_size3(p, &result->maxfilesize);
  2013. xdr_decode_nfstime3(p, &result->time_delta);
  2014. /* ignore properties */
  2015. result->lease_time = 0;
  2016. result->change_attr_type = NFS4_CHANGE_TYPE_IS_UNDEFINED;
  2017. result->xattr_support = 0;
  2018. return 0;
  2019. }
  2020. static int nfs3_xdr_dec_fsinfo3res(struct rpc_rqst *req,
  2021. struct xdr_stream *xdr,
  2022. void *data)
  2023. {
  2024. struct nfs_fsinfo *result = data;
  2025. enum nfs_stat status;
  2026. int error;
  2027. error = decode_nfsstat3(xdr, &status);
  2028. if (unlikely(error))
  2029. goto out;
  2030. error = decode_post_op_attr(xdr, result->fattr, rpc_rqst_userns(req));
  2031. if (unlikely(error))
  2032. goto out;
  2033. if (status != NFS3_OK)
  2034. goto out_status;
  2035. error = decode_fsinfo3resok(xdr, result);
  2036. out:
  2037. return error;
  2038. out_status:
  2039. return nfs_stat_to_errno(status);
  2040. }
  2041. /*
  2042. * 3.3.20 PATHCONF3res
  2043. *
  2044. * struct PATHCONF3resok {
  2045. * post_op_attr obj_attributes;
  2046. * uint32 linkmax;
  2047. * uint32 name_max;
  2048. * bool no_trunc;
  2049. * bool chown_restricted;
  2050. * bool case_insensitive;
  2051. * bool case_preserving;
  2052. * };
  2053. *
  2054. * struct PATHCONF3resfail {
  2055. * post_op_attr obj_attributes;
  2056. * };
  2057. *
  2058. * union PATHCONF3res switch (nfsstat3 status) {
  2059. * case NFS3_OK:
  2060. * PATHCONF3resok resok;
  2061. * default:
  2062. * PATHCONF3resfail resfail;
  2063. * };
  2064. */
  2065. static int decode_pathconf3resok(struct xdr_stream *xdr,
  2066. struct nfs_pathconf *result)
  2067. {
  2068. __be32 *p;
  2069. p = xdr_inline_decode(xdr, 4 * 6);
  2070. if (unlikely(!p))
  2071. return -EIO;
  2072. result->max_link = be32_to_cpup(p++);
  2073. result->max_namelen = be32_to_cpup(p);
  2074. /* ignore remaining fields */
  2075. return 0;
  2076. }
  2077. static int nfs3_xdr_dec_pathconf3res(struct rpc_rqst *req,
  2078. struct xdr_stream *xdr,
  2079. void *data)
  2080. {
  2081. struct nfs_pathconf *result = data;
  2082. enum nfs_stat status;
  2083. int error;
  2084. error = decode_nfsstat3(xdr, &status);
  2085. if (unlikely(error))
  2086. goto out;
  2087. error = decode_post_op_attr(xdr, result->fattr, rpc_rqst_userns(req));
  2088. if (unlikely(error))
  2089. goto out;
  2090. if (status != NFS3_OK)
  2091. goto out_status;
  2092. error = decode_pathconf3resok(xdr, result);
  2093. out:
  2094. return error;
  2095. out_status:
  2096. return nfs_stat_to_errno(status);
  2097. }
  2098. /*
  2099. * 3.3.21 COMMIT3res
  2100. *
  2101. * struct COMMIT3resok {
  2102. * wcc_data file_wcc;
  2103. * writeverf3 verf;
  2104. * };
  2105. *
  2106. * struct COMMIT3resfail {
  2107. * wcc_data file_wcc;
  2108. * };
  2109. *
  2110. * union COMMIT3res switch (nfsstat3 status) {
  2111. * case NFS3_OK:
  2112. * COMMIT3resok resok;
  2113. * default:
  2114. * COMMIT3resfail resfail;
  2115. * };
  2116. */
  2117. static int nfs3_xdr_dec_commit3res(struct rpc_rqst *req,
  2118. struct xdr_stream *xdr,
  2119. void *data)
  2120. {
  2121. struct nfs_commitres *result = data;
  2122. struct nfs_writeverf *verf = result->verf;
  2123. enum nfs_stat status;
  2124. int error;
  2125. error = decode_nfsstat3(xdr, &status);
  2126. if (unlikely(error))
  2127. goto out;
  2128. error = decode_wcc_data(xdr, result->fattr, rpc_rqst_userns(req));
  2129. if (unlikely(error))
  2130. goto out;
  2131. result->op_status = status;
  2132. if (status != NFS3_OK)
  2133. goto out_status;
  2134. error = decode_writeverf3(xdr, &verf->verifier);
  2135. if (!error)
  2136. verf->committed = NFS_FILE_SYNC;
  2137. out:
  2138. return error;
  2139. out_status:
  2140. return nfs_stat_to_errno(status);
  2141. }
  2142. #ifdef CONFIG_NFS_V3_ACL
  2143. static inline int decode_getacl3resok(struct xdr_stream *xdr,
  2144. struct nfs3_getaclres *result,
  2145. struct user_namespace *userns)
  2146. {
  2147. struct posix_acl **acl;
  2148. unsigned int *aclcnt;
  2149. size_t hdrlen;
  2150. int error;
  2151. error = decode_post_op_attr(xdr, result->fattr, userns);
  2152. if (unlikely(error))
  2153. goto out;
  2154. error = decode_uint32(xdr, &result->mask);
  2155. if (unlikely(error))
  2156. goto out;
  2157. error = -EINVAL;
  2158. if (result->mask & ~(NFS_ACL|NFS_ACLCNT|NFS_DFACL|NFS_DFACLCNT))
  2159. goto out;
  2160. hdrlen = xdr_stream_pos(xdr);
  2161. acl = NULL;
  2162. if (result->mask & NFS_ACL)
  2163. acl = &result->acl_access;
  2164. aclcnt = NULL;
  2165. if (result->mask & NFS_ACLCNT)
  2166. aclcnt = &result->acl_access_count;
  2167. error = nfsacl_decode(xdr->buf, hdrlen, aclcnt, acl);
  2168. if (unlikely(error <= 0))
  2169. goto out;
  2170. acl = NULL;
  2171. if (result->mask & NFS_DFACL)
  2172. acl = &result->acl_default;
  2173. aclcnt = NULL;
  2174. if (result->mask & NFS_DFACLCNT)
  2175. aclcnt = &result->acl_default_count;
  2176. error = nfsacl_decode(xdr->buf, hdrlen + error, aclcnt, acl);
  2177. if (unlikely(error <= 0))
  2178. return error;
  2179. error = 0;
  2180. out:
  2181. return error;
  2182. }
  2183. static int nfs3_xdr_dec_getacl3res(struct rpc_rqst *req,
  2184. struct xdr_stream *xdr,
  2185. void *result)
  2186. {
  2187. enum nfs_stat status;
  2188. int error;
  2189. error = decode_nfsstat3(xdr, &status);
  2190. if (unlikely(error))
  2191. goto out;
  2192. if (status != NFS3_OK)
  2193. goto out_default;
  2194. error = decode_getacl3resok(xdr, result, rpc_rqst_userns(req));
  2195. out:
  2196. return error;
  2197. out_default:
  2198. return nfs_stat_to_errno(status);
  2199. }
  2200. static int nfs3_xdr_dec_setacl3res(struct rpc_rqst *req,
  2201. struct xdr_stream *xdr,
  2202. void *result)
  2203. {
  2204. enum nfs_stat status;
  2205. int error;
  2206. error = decode_nfsstat3(xdr, &status);
  2207. if (unlikely(error))
  2208. goto out;
  2209. if (status != NFS3_OK)
  2210. goto out_default;
  2211. error = decode_post_op_attr(xdr, result, rpc_rqst_userns(req));
  2212. out:
  2213. return error;
  2214. out_default:
  2215. return nfs_stat_to_errno(status);
  2216. }
  2217. #endif /* CONFIG_NFS_V3_ACL */
  2218. #define PROC(proc, argtype, restype, timer) \
  2219. [NFS3PROC_##proc] = { \
  2220. .p_proc = NFS3PROC_##proc, \
  2221. .p_encode = nfs3_xdr_enc_##argtype##3args, \
  2222. .p_decode = nfs3_xdr_dec_##restype##3res, \
  2223. .p_arglen = NFS3_##argtype##args_sz, \
  2224. .p_replen = NFS3_##restype##res_sz, \
  2225. .p_timer = timer, \
  2226. .p_statidx = NFS3PROC_##proc, \
  2227. .p_name = #proc, \
  2228. }
  2229. const struct rpc_procinfo nfs3_procedures[] = {
  2230. PROC(GETATTR, getattr, getattr, 1),
  2231. PROC(SETATTR, setattr, setattr, 0),
  2232. PROC(LOOKUP, lookup, lookup, 2),
  2233. PROC(ACCESS, access, access, 1),
  2234. PROC(READLINK, readlink, readlink, 3),
  2235. PROC(READ, read, read, 3),
  2236. PROC(WRITE, write, write, 4),
  2237. PROC(CREATE, create, create, 0),
  2238. PROC(MKDIR, mkdir, create, 0),
  2239. PROC(SYMLINK, symlink, create, 0),
  2240. PROC(MKNOD, mknod, create, 0),
  2241. PROC(REMOVE, remove, remove, 0),
  2242. PROC(RMDIR, lookup, setattr, 0),
  2243. PROC(RENAME, rename, rename, 0),
  2244. PROC(LINK, link, link, 0),
  2245. PROC(READDIR, readdir, readdir, 3),
  2246. PROC(READDIRPLUS, readdirplus, readdir, 3),
  2247. PROC(FSSTAT, getattr, fsstat, 0),
  2248. PROC(FSINFO, getattr, fsinfo, 0),
  2249. PROC(PATHCONF, getattr, pathconf, 0),
  2250. PROC(COMMIT, commit, commit, 5),
  2251. };
  2252. static unsigned int nfs_version3_counts[ARRAY_SIZE(nfs3_procedures)];
  2253. const struct rpc_version nfs_version3 = {
  2254. .number = 3,
  2255. .nrprocs = ARRAY_SIZE(nfs3_procedures),
  2256. .procs = nfs3_procedures,
  2257. .counts = nfs_version3_counts,
  2258. };
  2259. #ifdef CONFIG_NFS_V3_ACL
  2260. static const struct rpc_procinfo nfs3_acl_procedures[] = {
  2261. [ACLPROC3_GETACL] = {
  2262. .p_proc = ACLPROC3_GETACL,
  2263. .p_encode = nfs3_xdr_enc_getacl3args,
  2264. .p_decode = nfs3_xdr_dec_getacl3res,
  2265. .p_arglen = ACL3_getaclargs_sz,
  2266. .p_replen = ACL3_getaclres_sz,
  2267. .p_timer = 1,
  2268. .p_name = "GETACL",
  2269. },
  2270. [ACLPROC3_SETACL] = {
  2271. .p_proc = ACLPROC3_SETACL,
  2272. .p_encode = nfs3_xdr_enc_setacl3args,
  2273. .p_decode = nfs3_xdr_dec_setacl3res,
  2274. .p_arglen = ACL3_setaclargs_sz,
  2275. .p_replen = ACL3_setaclres_sz,
  2276. .p_timer = 0,
  2277. .p_name = "SETACL",
  2278. },
  2279. };
  2280. static unsigned int nfs3_acl_counts[ARRAY_SIZE(nfs3_acl_procedures)];
  2281. const struct rpc_version nfsacl_version3 = {
  2282. .number = 3,
  2283. .nrprocs = ARRAY_SIZE(nfs3_acl_procedures),
  2284. .procs = nfs3_acl_procedures,
  2285. .counts = nfs3_acl_counts,
  2286. };
  2287. #endif /* CONFIG_NFS_V3_ACL */