nfs3xdr.c 53 KB

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