nfs3xdr.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039
  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_fsstat_sz
  42. #define NFS3_fsinfo_sz
  43. #define NFS3_pathconf_sz
  44. #define NFS3_entry_sz (NFS3_filename_sz+3)
  45. #define NFS3_sattrargs_sz (NFS3_fh_sz+NFS3_sattr_sz+3)
  46. #define NFS3_diropargs_sz (NFS3_fh_sz+NFS3_filename_sz)
  47. #define NFS3_removeargs_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_renameargs_sz (NFS3_diropargs_sz+NFS3_diropargs_sz)
  57. #define NFS3_linkargs_sz (NFS3_fh_sz+NFS3_diropargs_sz)
  58. #define NFS3_readdirargs_sz (NFS3_fh_sz+NFS3_cookieverf_sz+3)
  59. #define NFS3_readdirplusargs_sz (NFS3_fh_sz+NFS3_cookieverf_sz+4)
  60. #define NFS3_commitargs_sz (NFS3_fh_sz+3)
  61. #define NFS3_attrstat_sz (1+NFS3_fattr_sz)
  62. #define NFS3_wccstat_sz (1+NFS3_wcc_data_sz)
  63. #define NFS3_removeres_sz (NFS3_wccstat_sz)
  64. #define NFS3_lookupres_sz (1+NFS3_fh_sz+(2 * NFS3_post_op_attr_sz))
  65. #define NFS3_accessres_sz (1+NFS3_post_op_attr_sz+1)
  66. #define NFS3_readlinkres_sz (1+NFS3_post_op_attr_sz+1)
  67. #define NFS3_readres_sz (1+NFS3_post_op_attr_sz+3)
  68. #define NFS3_writeres_sz (1+NFS3_wcc_data_sz+4)
  69. #define NFS3_createres_sz (1+NFS3_fh_sz+NFS3_post_op_attr_sz+NFS3_wcc_data_sz)
  70. #define NFS3_renameres_sz (1+(2 * NFS3_wcc_data_sz))
  71. #define NFS3_linkres_sz (1+NFS3_post_op_attr_sz+NFS3_wcc_data_sz)
  72. #define NFS3_readdirres_sz (1+NFS3_post_op_attr_sz+2)
  73. #define NFS3_fsstatres_sz (1+NFS3_post_op_attr_sz+13)
  74. #define NFS3_fsinfores_sz (1+NFS3_post_op_attr_sz+12)
  75. #define NFS3_pathconfres_sz (1+NFS3_post_op_attr_sz+6)
  76. #define NFS3_commitres_sz (1+NFS3_wcc_data_sz+2)
  77. #define ACL3_getaclargs_sz (NFS3_fh_sz+1)
  78. #define ACL3_setaclargs_sz (NFS3_fh_sz+1+ \
  79. XDR_QUADLEN(NFS_ACL_INLINE_BUFSIZE))
  80. #define ACL3_getaclres_sz (1+NFS3_post_op_attr_sz+1+ \
  81. XDR_QUADLEN(NFS_ACL_INLINE_BUFSIZE))
  82. #define ACL3_setaclres_sz (1+NFS3_post_op_attr_sz)
  83. /*
  84. * Map file type to S_IFMT bits
  85. */
  86. static const umode_t nfs_type2fmt[] = {
  87. [NF3BAD] = 0,
  88. [NF3REG] = S_IFREG,
  89. [NF3DIR] = S_IFDIR,
  90. [NF3BLK] = S_IFBLK,
  91. [NF3CHR] = S_IFCHR,
  92. [NF3LNK] = S_IFLNK,
  93. [NF3SOCK] = S_IFSOCK,
  94. [NF3FIFO] = S_IFIFO,
  95. };
  96. static void print_overflow_msg(const char *func, const struct xdr_stream *xdr)
  97. {
  98. dprintk("nfs: %s: prematurely hit end of receive buffer. "
  99. "Remaining buffer length is %tu words.\n",
  100. func, xdr->end - xdr->p);
  101. }
  102. /*
  103. * While encoding arguments, set up the reply buffer in advance to
  104. * receive reply data directly into the page cache.
  105. */
  106. static void prepare_reply_buffer(struct rpc_rqst *req, struct page **pages,
  107. unsigned int base, unsigned int len,
  108. unsigned int bufsize)
  109. {
  110. struct rpc_auth *auth = req->rq_cred->cr_auth;
  111. unsigned int replen;
  112. replen = RPC_REPHDRSIZE + auth->au_rslack + bufsize;
  113. xdr_inline_pages(&req->rq_rcv_buf, replen << 2, pages, base, len);
  114. }
  115. /*
  116. * Common NFS XDR functions as inlines
  117. */
  118. static inline __be32 *
  119. xdr_encode_fhandle(__be32 *p, const struct nfs_fh *fh)
  120. {
  121. return xdr_encode_array(p, fh->data, fh->size);
  122. }
  123. static inline __be32 *
  124. xdr_decode_fhandle(__be32 *p, struct nfs_fh *fh)
  125. {
  126. if ((fh->size = ntohl(*p++)) <= NFS3_FHSIZE) {
  127. memcpy(fh->data, p, fh->size);
  128. return p + XDR_QUADLEN(fh->size);
  129. }
  130. return NULL;
  131. }
  132. static inline __be32 *
  133. xdr_decode_fhandle_stream(struct xdr_stream *xdr, struct nfs_fh *fh)
  134. {
  135. __be32 *p;
  136. p = xdr_inline_decode(xdr, 4);
  137. if (unlikely(!p))
  138. goto out_overflow;
  139. fh->size = ntohl(*p++);
  140. if (fh->size <= NFS3_FHSIZE) {
  141. p = xdr_inline_decode(xdr, fh->size);
  142. if (unlikely(!p))
  143. goto out_overflow;
  144. memcpy(fh->data, p, fh->size);
  145. return p + XDR_QUADLEN(fh->size);
  146. }
  147. return NULL;
  148. out_overflow:
  149. print_overflow_msg(__func__, xdr);
  150. return ERR_PTR(-EIO);
  151. }
  152. /*
  153. * Encode/decode time.
  154. */
  155. static inline __be32 *
  156. xdr_encode_time3(__be32 *p, const struct timespec *timep)
  157. {
  158. *p++ = htonl(timep->tv_sec);
  159. *p++ = htonl(timep->tv_nsec);
  160. return p;
  161. }
  162. static inline __be32 *
  163. xdr_decode_time3(__be32 *p, struct timespec *timep)
  164. {
  165. timep->tv_sec = ntohl(*p++);
  166. timep->tv_nsec = ntohl(*p++);
  167. return p;
  168. }
  169. static __be32 *
  170. xdr_decode_fattr(__be32 *p, struct nfs_fattr *fattr)
  171. {
  172. unsigned int type, major, minor;
  173. umode_t fmode;
  174. type = ntohl(*p++);
  175. if (type > NF3FIFO)
  176. type = NF3NON;
  177. fmode = nfs_type2fmt[type];
  178. fattr->mode = (ntohl(*p++) & ~S_IFMT) | fmode;
  179. fattr->nlink = ntohl(*p++);
  180. fattr->uid = ntohl(*p++);
  181. fattr->gid = ntohl(*p++);
  182. p = xdr_decode_hyper(p, &fattr->size);
  183. p = xdr_decode_hyper(p, &fattr->du.nfs3.used);
  184. /* Turn remote device info into Linux-specific dev_t */
  185. major = ntohl(*p++);
  186. minor = ntohl(*p++);
  187. fattr->rdev = MKDEV(major, minor);
  188. if (MAJOR(fattr->rdev) != major || MINOR(fattr->rdev) != minor)
  189. fattr->rdev = 0;
  190. p = xdr_decode_hyper(p, &fattr->fsid.major);
  191. fattr->fsid.minor = 0;
  192. p = xdr_decode_hyper(p, &fattr->fileid);
  193. p = xdr_decode_time3(p, &fattr->atime);
  194. p = xdr_decode_time3(p, &fattr->mtime);
  195. p = xdr_decode_time3(p, &fattr->ctime);
  196. /* Update the mode bits */
  197. fattr->valid |= NFS_ATTR_FATTR_V3;
  198. return p;
  199. }
  200. static inline __be32 *
  201. xdr_encode_sattr(__be32 *p, const struct iattr *attr)
  202. {
  203. if (attr->ia_valid & ATTR_MODE) {
  204. *p++ = xdr_one;
  205. *p++ = htonl(attr->ia_mode & S_IALLUGO);
  206. } else {
  207. *p++ = xdr_zero;
  208. }
  209. if (attr->ia_valid & ATTR_UID) {
  210. *p++ = xdr_one;
  211. *p++ = htonl(attr->ia_uid);
  212. } else {
  213. *p++ = xdr_zero;
  214. }
  215. if (attr->ia_valid & ATTR_GID) {
  216. *p++ = xdr_one;
  217. *p++ = htonl(attr->ia_gid);
  218. } else {
  219. *p++ = xdr_zero;
  220. }
  221. if (attr->ia_valid & ATTR_SIZE) {
  222. *p++ = xdr_one;
  223. p = xdr_encode_hyper(p, (__u64) attr->ia_size);
  224. } else {
  225. *p++ = xdr_zero;
  226. }
  227. if (attr->ia_valid & ATTR_ATIME_SET) {
  228. *p++ = xdr_two;
  229. p = xdr_encode_time3(p, &attr->ia_atime);
  230. } else if (attr->ia_valid & ATTR_ATIME) {
  231. *p++ = xdr_one;
  232. } else {
  233. *p++ = xdr_zero;
  234. }
  235. if (attr->ia_valid & ATTR_MTIME_SET) {
  236. *p++ = xdr_two;
  237. p = xdr_encode_time3(p, &attr->ia_mtime);
  238. } else if (attr->ia_valid & ATTR_MTIME) {
  239. *p++ = xdr_one;
  240. } else {
  241. *p++ = xdr_zero;
  242. }
  243. return p;
  244. }
  245. static inline __be32 *
  246. xdr_decode_wcc_attr(__be32 *p, struct nfs_fattr *fattr)
  247. {
  248. p = xdr_decode_hyper(p, &fattr->pre_size);
  249. p = xdr_decode_time3(p, &fattr->pre_mtime);
  250. p = xdr_decode_time3(p, &fattr->pre_ctime);
  251. fattr->valid |= NFS_ATTR_FATTR_PRESIZE
  252. | NFS_ATTR_FATTR_PREMTIME
  253. | NFS_ATTR_FATTR_PRECTIME;
  254. return p;
  255. }
  256. static inline __be32 *
  257. xdr_decode_post_op_attr(__be32 *p, struct nfs_fattr *fattr)
  258. {
  259. if (*p++)
  260. p = xdr_decode_fattr(p, fattr);
  261. return p;
  262. }
  263. static inline __be32 *
  264. xdr_decode_post_op_attr_stream(struct xdr_stream *xdr, struct nfs_fattr *fattr)
  265. {
  266. __be32 *p;
  267. p = xdr_inline_decode(xdr, 4);
  268. if (unlikely(!p))
  269. goto out_overflow;
  270. if (ntohl(*p++)) {
  271. p = xdr_inline_decode(xdr, 84);
  272. if (unlikely(!p))
  273. goto out_overflow;
  274. p = xdr_decode_fattr(p, fattr);
  275. }
  276. return p;
  277. out_overflow:
  278. print_overflow_msg(__func__, xdr);
  279. return ERR_PTR(-EIO);
  280. }
  281. static inline __be32 *
  282. xdr_decode_pre_op_attr(__be32 *p, struct nfs_fattr *fattr)
  283. {
  284. if (*p++)
  285. return xdr_decode_wcc_attr(p, fattr);
  286. return p;
  287. }
  288. static inline __be32 *
  289. xdr_decode_wcc_data(__be32 *p, struct nfs_fattr *fattr)
  290. {
  291. p = xdr_decode_pre_op_attr(p, fattr);
  292. return xdr_decode_post_op_attr(p, fattr);
  293. }
  294. /*
  295. * Encode/decode NFSv3 basic data types
  296. *
  297. * Basic NFSv3 data types are defined in section 2.5 of RFC 1813:
  298. * "NFS Version 3 Protocol Specification".
  299. *
  300. * Not all basic data types have their own encoding and decoding
  301. * functions. For run-time efficiency, some data types are encoded
  302. * or decoded inline.
  303. */
  304. static void encode_uint32(struct xdr_stream *xdr, u32 value)
  305. {
  306. __be32 *p = xdr_reserve_space(xdr, 4);
  307. *p = cpu_to_be32(value);
  308. }
  309. /*
  310. * filename3
  311. *
  312. * typedef string filename3<>;
  313. */
  314. static void encode_filename3(struct xdr_stream *xdr,
  315. const char *name, u32 length)
  316. {
  317. __be32 *p;
  318. BUG_ON(length > NFS3_MAXNAMLEN);
  319. p = xdr_reserve_space(xdr, 4 + length);
  320. xdr_encode_opaque(p, name, length);
  321. }
  322. /*
  323. * nfspath3
  324. *
  325. * typedef string nfspath3<>;
  326. */
  327. static void encode_nfspath3(struct xdr_stream *xdr, struct page **pages,
  328. const u32 length)
  329. {
  330. BUG_ON(length > NFS3_MAXPATHLEN);
  331. encode_uint32(xdr, length);
  332. xdr_write_pages(xdr, pages, 0, length);
  333. }
  334. /*
  335. * cookie3
  336. *
  337. * typedef uint64 cookie3
  338. */
  339. static __be32 *xdr_encode_cookie3(__be32 *p, u64 cookie)
  340. {
  341. return xdr_encode_hyper(p, cookie);
  342. }
  343. /*
  344. * cookieverf3
  345. *
  346. * typedef opaque cookieverf3[NFS3_COOKIEVERFSIZE];
  347. */
  348. static __be32 *xdr_encode_cookieverf3(__be32 *p, const __be32 *verifier)
  349. {
  350. memcpy(p, verifier, NFS3_COOKIEVERFSIZE);
  351. return p + XDR_QUADLEN(NFS3_COOKIEVERFSIZE);
  352. }
  353. /*
  354. * createverf3
  355. *
  356. * typedef opaque createverf3[NFS3_CREATEVERFSIZE];
  357. */
  358. static void encode_createverf3(struct xdr_stream *xdr, const __be32 *verifier)
  359. {
  360. __be32 *p;
  361. p = xdr_reserve_space(xdr, NFS3_CREATEVERFSIZE);
  362. memcpy(p, verifier, NFS3_CREATEVERFSIZE);
  363. }
  364. /*
  365. * ftype3
  366. *
  367. * enum ftype3 {
  368. * NF3REG = 1,
  369. * NF3DIR = 2,
  370. * NF3BLK = 3,
  371. * NF3CHR = 4,
  372. * NF3LNK = 5,
  373. * NF3SOCK = 6,
  374. * NF3FIFO = 7
  375. * };
  376. */
  377. static void encode_ftype3(struct xdr_stream *xdr, const u32 type)
  378. {
  379. BUG_ON(type > NF3FIFO);
  380. encode_uint32(xdr, type);
  381. }
  382. /*
  383. * specdata3
  384. *
  385. * struct specdata3 {
  386. * uint32 specdata1;
  387. * uint32 specdata2;
  388. * };
  389. */
  390. static void encode_specdata3(struct xdr_stream *xdr, const dev_t rdev)
  391. {
  392. __be32 *p;
  393. p = xdr_reserve_space(xdr, 8);
  394. *p++ = cpu_to_be32(MAJOR(rdev));
  395. *p = cpu_to_be32(MINOR(rdev));
  396. }
  397. /*
  398. * nfs_fh3
  399. *
  400. * struct nfs_fh3 {
  401. * opaque data<NFS3_FHSIZE>;
  402. * };
  403. */
  404. static void encode_nfs_fh3(struct xdr_stream *xdr, const struct nfs_fh *fh)
  405. {
  406. __be32 *p;
  407. BUG_ON(fh->size > NFS3_FHSIZE);
  408. p = xdr_reserve_space(xdr, 4 + fh->size);
  409. xdr_encode_opaque(p, fh->data, fh->size);
  410. }
  411. /*
  412. * sattr3
  413. *
  414. * enum time_how {
  415. * DONT_CHANGE = 0,
  416. * SET_TO_SERVER_TIME = 1,
  417. * SET_TO_CLIENT_TIME = 2
  418. * };
  419. *
  420. * union set_mode3 switch (bool set_it) {
  421. * case TRUE:
  422. * mode3 mode;
  423. * default:
  424. * void;
  425. * };
  426. *
  427. * union set_uid3 switch (bool set_it) {
  428. * case TRUE:
  429. * uid3 uid;
  430. * default:
  431. * void;
  432. * };
  433. *
  434. * union set_gid3 switch (bool set_it) {
  435. * case TRUE:
  436. * gid3 gid;
  437. * default:
  438. * void;
  439. * };
  440. *
  441. * union set_size3 switch (bool set_it) {
  442. * case TRUE:
  443. * size3 size;
  444. * default:
  445. * void;
  446. * };
  447. *
  448. * union set_atime switch (time_how set_it) {
  449. * case SET_TO_CLIENT_TIME:
  450. * nfstime3 atime;
  451. * default:
  452. * void;
  453. * };
  454. *
  455. * union set_mtime switch (time_how set_it) {
  456. * case SET_TO_CLIENT_TIME:
  457. * nfstime3 mtime;
  458. * default:
  459. * void;
  460. * };
  461. *
  462. * struct sattr3 {
  463. * set_mode3 mode;
  464. * set_uid3 uid;
  465. * set_gid3 gid;
  466. * set_size3 size;
  467. * set_atime atime;
  468. * set_mtime mtime;
  469. * };
  470. */
  471. static void encode_sattr3(struct xdr_stream *xdr, const struct iattr *attr)
  472. {
  473. u32 nbytes;
  474. __be32 *p;
  475. /*
  476. * In order to make only a single xdr_reserve_space() call,
  477. * pre-compute the total number of bytes to be reserved.
  478. * Six boolean values, one for each set_foo field, are always
  479. * present in the encoded result, so start there.
  480. */
  481. nbytes = 6 * 4;
  482. if (attr->ia_valid & ATTR_MODE)
  483. nbytes += 4;
  484. if (attr->ia_valid & ATTR_UID)
  485. nbytes += 4;
  486. if (attr->ia_valid & ATTR_GID)
  487. nbytes += 4;
  488. if (attr->ia_valid & ATTR_SIZE)
  489. nbytes += 8;
  490. if (attr->ia_valid & ATTR_ATIME_SET)
  491. nbytes += 8;
  492. if (attr->ia_valid & ATTR_MTIME_SET)
  493. nbytes += 8;
  494. p = xdr_reserve_space(xdr, nbytes);
  495. xdr_encode_sattr(p, attr);
  496. }
  497. /*
  498. * diropargs3
  499. *
  500. * struct diropargs3 {
  501. * nfs_fh3 dir;
  502. * filename3 name;
  503. * };
  504. */
  505. static void encode_diropargs3(struct xdr_stream *xdr, const struct nfs_fh *fh,
  506. const char *name, u32 length)
  507. {
  508. encode_nfs_fh3(xdr, fh);
  509. encode_filename3(xdr, name, length);
  510. }
  511. /*
  512. * NFS encode functions
  513. */
  514. /*
  515. * Encode file handle argument
  516. */
  517. static int
  518. nfs3_xdr_fhandle(struct rpc_rqst *req, __be32 *p, struct nfs_fh *fh)
  519. {
  520. p = xdr_encode_fhandle(p, fh);
  521. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  522. return 0;
  523. }
  524. /*
  525. * 3.3.1 GETATTR3args
  526. *
  527. * struct GETATTR3args {
  528. * nfs_fh3 object;
  529. * };
  530. */
  531. static int nfs3_xdr_enc_getattr3args(struct rpc_rqst *req, __be32 *p,
  532. const struct nfs_fh *fh)
  533. {
  534. struct xdr_stream xdr;
  535. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  536. encode_nfs_fh3(&xdr, fh);
  537. return 0;
  538. }
  539. /*
  540. * Encode SETATTR arguments
  541. */
  542. static int
  543. nfs3_xdr_sattrargs(struct rpc_rqst *req, __be32 *p, struct nfs3_sattrargs *args)
  544. {
  545. p = xdr_encode_fhandle(p, args->fh);
  546. p = xdr_encode_sattr(p, args->sattr);
  547. *p++ = htonl(args->guard);
  548. if (args->guard)
  549. p = xdr_encode_time3(p, &args->guardtime);
  550. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  551. return 0;
  552. }
  553. /*
  554. * 3.3.2 SETATTR3args
  555. *
  556. * union sattrguard3 switch (bool check) {
  557. * case TRUE:
  558. * nfstime3 obj_ctime;
  559. * case FALSE:
  560. * void;
  561. * };
  562. *
  563. * struct SETATTR3args {
  564. * nfs_fh3 object;
  565. * sattr3 new_attributes;
  566. * sattrguard3 guard;
  567. * };
  568. */
  569. static void encode_sattrguard3(struct xdr_stream *xdr,
  570. const struct nfs3_sattrargs *args)
  571. {
  572. __be32 *p;
  573. if (args->guard) {
  574. p = xdr_reserve_space(xdr, 4 + 8);
  575. *p++ = xdr_one;
  576. xdr_encode_time3(p, &args->guardtime);
  577. } else {
  578. p = xdr_reserve_space(xdr, 4);
  579. *p = xdr_zero;
  580. }
  581. }
  582. static int nfs3_xdr_enc_setattr3args(struct rpc_rqst *req, __be32 *p,
  583. const struct nfs3_sattrargs *args)
  584. {
  585. struct xdr_stream xdr;
  586. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  587. encode_nfs_fh3(&xdr, args->fh);
  588. encode_sattr3(&xdr, args->sattr);
  589. encode_sattrguard3(&xdr, args);
  590. return 0;
  591. }
  592. /*
  593. * Encode directory ops argument
  594. */
  595. static int
  596. nfs3_xdr_diropargs(struct rpc_rqst *req, __be32 *p, struct nfs3_diropargs *args)
  597. {
  598. p = xdr_encode_fhandle(p, args->fh);
  599. p = xdr_encode_array(p, args->name, args->len);
  600. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  601. return 0;
  602. }
  603. /*
  604. * 3.3.3 LOOKUP3args
  605. *
  606. * struct LOOKUP3args {
  607. * diropargs3 what;
  608. * };
  609. */
  610. static int nfs3_xdr_enc_lookup3args(struct rpc_rqst *req, __be32 *p,
  611. const struct nfs3_diropargs *args)
  612. {
  613. struct xdr_stream xdr;
  614. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  615. encode_diropargs3(&xdr, args->fh, args->name, args->len);
  616. return 0;
  617. }
  618. /*
  619. * Encode REMOVE argument
  620. */
  621. static int
  622. nfs3_xdr_removeargs(struct rpc_rqst *req, __be32 *p, const struct nfs_removeargs *args)
  623. {
  624. p = xdr_encode_fhandle(p, args->fh);
  625. p = xdr_encode_array(p, args->name.name, args->name.len);
  626. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  627. return 0;
  628. }
  629. /*
  630. * Encode access() argument
  631. */
  632. static int
  633. nfs3_xdr_accessargs(struct rpc_rqst *req, __be32 *p, struct nfs3_accessargs *args)
  634. {
  635. p = xdr_encode_fhandle(p, args->fh);
  636. *p++ = htonl(args->access);
  637. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  638. return 0;
  639. }
  640. /*
  641. * 3.3.4 ACCESS3args
  642. *
  643. * struct ACCESS3args {
  644. * nfs_fh3 object;
  645. * uint32 access;
  646. * };
  647. */
  648. static void encode_access3args(struct xdr_stream *xdr,
  649. const struct nfs3_accessargs *args)
  650. {
  651. encode_nfs_fh3(xdr, args->fh);
  652. encode_uint32(xdr, args->access);
  653. }
  654. static int nfs3_xdr_enc_access3args(struct rpc_rqst *req, __be32 *p,
  655. const struct nfs3_accessargs *args)
  656. {
  657. struct xdr_stream xdr;
  658. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  659. encode_access3args(&xdr, args);
  660. return 0;
  661. }
  662. /*
  663. * 3.3.5 READLINK3args
  664. *
  665. * struct READLINK3args {
  666. * nfs_fh3 symlink;
  667. * };
  668. */
  669. static int nfs3_xdr_enc_readlink3args(struct rpc_rqst *req, __be32 *p,
  670. const struct nfs3_readlinkargs *args)
  671. {
  672. struct xdr_stream xdr;
  673. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  674. encode_nfs_fh3(&xdr, args->fh);
  675. prepare_reply_buffer(req, args->pages, args->pgbase,
  676. args->pglen, NFS3_readlinkres_sz);
  677. return 0;
  678. }
  679. /*
  680. * Arguments to a READ call. Since we read data directly into the page
  681. * cache, we also set up the reply iovec here so that iov[1] points
  682. * exactly to the page we want to fetch.
  683. */
  684. static int
  685. nfs3_xdr_readargs(struct rpc_rqst *req, __be32 *p, struct nfs_readargs *args)
  686. {
  687. struct rpc_auth *auth = req->rq_cred->cr_auth;
  688. unsigned int replen;
  689. u32 count = args->count;
  690. p = xdr_encode_fhandle(p, args->fh);
  691. p = xdr_encode_hyper(p, args->offset);
  692. *p++ = htonl(count);
  693. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  694. /* Inline the page array */
  695. replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS3_readres_sz) << 2;
  696. xdr_inline_pages(&req->rq_rcv_buf, replen,
  697. args->pages, args->pgbase, count);
  698. req->rq_rcv_buf.flags |= XDRBUF_READ;
  699. return 0;
  700. }
  701. /*
  702. * 3.3.6 READ3args
  703. *
  704. * struct READ3args {
  705. * nfs_fh3 file;
  706. * offset3 offset;
  707. * count3 count;
  708. * };
  709. */
  710. static void encode_read3args(struct xdr_stream *xdr,
  711. const struct nfs_readargs *args)
  712. {
  713. __be32 *p;
  714. encode_nfs_fh3(xdr, args->fh);
  715. p = xdr_reserve_space(xdr, 8 + 4);
  716. p = xdr_encode_hyper(p, args->offset);
  717. *p = cpu_to_be32(args->count);
  718. }
  719. static int nfs3_xdr_enc_read3args(struct rpc_rqst *req, __be32 *p,
  720. const struct nfs_readargs *args)
  721. {
  722. struct xdr_stream xdr;
  723. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  724. encode_read3args(&xdr, args);
  725. prepare_reply_buffer(req, args->pages, args->pgbase,
  726. args->count, NFS3_readres_sz);
  727. req->rq_rcv_buf.flags |= XDRBUF_READ;
  728. return 0;
  729. }
  730. /*
  731. * Write arguments. Splice the buffer to be written into the iovec.
  732. */
  733. static int
  734. nfs3_xdr_writeargs(struct rpc_rqst *req, __be32 *p, struct nfs_writeargs *args)
  735. {
  736. struct xdr_buf *sndbuf = &req->rq_snd_buf;
  737. u32 count = args->count;
  738. p = xdr_encode_fhandle(p, args->fh);
  739. p = xdr_encode_hyper(p, args->offset);
  740. *p++ = htonl(count);
  741. *p++ = htonl(args->stable);
  742. *p++ = htonl(count);
  743. sndbuf->len = xdr_adjust_iovec(sndbuf->head, p);
  744. /* Copy the page array */
  745. xdr_encode_pages(sndbuf, args->pages, args->pgbase, count);
  746. sndbuf->flags |= XDRBUF_WRITE;
  747. return 0;
  748. }
  749. /*
  750. * 3.3.7 WRITE3args
  751. *
  752. * enum stable_how {
  753. * UNSTABLE = 0,
  754. * DATA_SYNC = 1,
  755. * FILE_SYNC = 2
  756. * };
  757. *
  758. * struct WRITE3args {
  759. * nfs_fh3 file;
  760. * offset3 offset;
  761. * count3 count;
  762. * stable_how stable;
  763. * opaque data<>;
  764. * };
  765. */
  766. static void encode_write3args(struct xdr_stream *xdr,
  767. const struct nfs_writeargs *args)
  768. {
  769. __be32 *p;
  770. encode_nfs_fh3(xdr, args->fh);
  771. p = xdr_reserve_space(xdr, 8 + 4 + 4 + 4);
  772. p = xdr_encode_hyper(p, args->offset);
  773. *p++ = cpu_to_be32(args->count);
  774. BUG_ON(args->stable > NFS_FILE_SYNC);
  775. *p++ = cpu_to_be32(args->stable);
  776. *p = cpu_to_be32(args->count);
  777. xdr_write_pages(xdr, args->pages, args->pgbase, args->count);
  778. }
  779. static int nfs3_xdr_enc_write3args(struct rpc_rqst *req, __be32 *p,
  780. const struct nfs_writeargs *args)
  781. {
  782. struct xdr_stream xdr;
  783. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  784. encode_write3args(&xdr, args);
  785. xdr.buf->flags |= XDRBUF_WRITE;
  786. return 0;
  787. }
  788. /*
  789. * Encode CREATE arguments
  790. */
  791. static int
  792. nfs3_xdr_createargs(struct rpc_rqst *req, __be32 *p, struct nfs3_createargs *args)
  793. {
  794. p = xdr_encode_fhandle(p, args->fh);
  795. p = xdr_encode_array(p, args->name, args->len);
  796. *p++ = htonl(args->createmode);
  797. if (args->createmode == NFS3_CREATE_EXCLUSIVE) {
  798. *p++ = args->verifier[0];
  799. *p++ = args->verifier[1];
  800. } else
  801. p = xdr_encode_sattr(p, args->sattr);
  802. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  803. return 0;
  804. }
  805. /*
  806. * 3.3.8 CREATE3args
  807. *
  808. * enum createmode3 {
  809. * UNCHECKED = 0,
  810. * GUARDED = 1,
  811. * EXCLUSIVE = 2
  812. * };
  813. *
  814. * union createhow3 switch (createmode3 mode) {
  815. * case UNCHECKED:
  816. * case GUARDED:
  817. * sattr3 obj_attributes;
  818. * case EXCLUSIVE:
  819. * createverf3 verf;
  820. * };
  821. *
  822. * struct CREATE3args {
  823. * diropargs3 where;
  824. * createhow3 how;
  825. * };
  826. */
  827. static void encode_createhow3(struct xdr_stream *xdr,
  828. const struct nfs3_createargs *args)
  829. {
  830. encode_uint32(xdr, args->createmode);
  831. switch (args->createmode) {
  832. case NFS3_CREATE_UNCHECKED:
  833. case NFS3_CREATE_GUARDED:
  834. encode_sattr3(xdr, args->sattr);
  835. break;
  836. case NFS3_CREATE_EXCLUSIVE:
  837. encode_createverf3(xdr, args->verifier);
  838. break;
  839. default:
  840. BUG();
  841. }
  842. }
  843. static int nfs3_xdr_enc_create3args(struct rpc_rqst *req, __be32 *p,
  844. const struct nfs3_createargs *args)
  845. {
  846. struct xdr_stream xdr;
  847. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  848. encode_diropargs3(&xdr, args->fh, args->name, args->len);
  849. encode_createhow3(&xdr, args);
  850. return 0;
  851. }
  852. /*
  853. * Encode MKDIR arguments
  854. */
  855. static int
  856. nfs3_xdr_mkdirargs(struct rpc_rqst *req, __be32 *p, struct nfs3_mkdirargs *args)
  857. {
  858. p = xdr_encode_fhandle(p, args->fh);
  859. p = xdr_encode_array(p, args->name, args->len);
  860. p = xdr_encode_sattr(p, args->sattr);
  861. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  862. return 0;
  863. }
  864. /*
  865. * 3.3.9 MKDIR3args
  866. *
  867. * struct MKDIR3args {
  868. * diropargs3 where;
  869. * sattr3 attributes;
  870. * };
  871. */
  872. static int nfs3_xdr_enc_mkdir3args(struct rpc_rqst *req, __be32 *p,
  873. const struct nfs3_mkdirargs *args)
  874. {
  875. struct xdr_stream xdr;
  876. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  877. encode_diropargs3(&xdr, args->fh, args->name, args->len);
  878. encode_sattr3(&xdr, args->sattr);
  879. return 0;
  880. }
  881. /*
  882. * Encode SYMLINK arguments
  883. */
  884. static int
  885. nfs3_xdr_symlinkargs(struct rpc_rqst *req, __be32 *p, struct nfs3_symlinkargs *args)
  886. {
  887. p = xdr_encode_fhandle(p, args->fromfh);
  888. p = xdr_encode_array(p, args->fromname, args->fromlen);
  889. p = xdr_encode_sattr(p, args->sattr);
  890. *p++ = htonl(args->pathlen);
  891. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  892. /* Copy the page */
  893. xdr_encode_pages(&req->rq_snd_buf, args->pages, 0, args->pathlen);
  894. return 0;
  895. }
  896. /*
  897. * 3.3.10 SYMLINK3args
  898. *
  899. * struct symlinkdata3 {
  900. * sattr3 symlink_attributes;
  901. * nfspath3 symlink_data;
  902. * };
  903. *
  904. * struct SYMLINK3args {
  905. * diropargs3 where;
  906. * symlinkdata3 symlink;
  907. * };
  908. */
  909. static void encode_symlinkdata3(struct xdr_stream *xdr,
  910. const struct nfs3_symlinkargs *args)
  911. {
  912. encode_sattr3(xdr, args->sattr);
  913. encode_nfspath3(xdr, args->pages, args->pathlen);
  914. }
  915. static int nfs3_xdr_enc_symlink3args(struct rpc_rqst *req, __be32 *p,
  916. const struct nfs3_symlinkargs *args)
  917. {
  918. struct xdr_stream xdr;
  919. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  920. encode_diropargs3(&xdr, args->fromfh, args->fromname, args->fromlen);
  921. encode_symlinkdata3(&xdr, args);
  922. return 0;
  923. }
  924. /*
  925. * Encode MKNOD arguments
  926. */
  927. static int
  928. nfs3_xdr_mknodargs(struct rpc_rqst *req, __be32 *p, struct nfs3_mknodargs *args)
  929. {
  930. p = xdr_encode_fhandle(p, args->fh);
  931. p = xdr_encode_array(p, args->name, args->len);
  932. *p++ = htonl(args->type);
  933. p = xdr_encode_sattr(p, args->sattr);
  934. if (args->type == NF3CHR || args->type == NF3BLK) {
  935. *p++ = htonl(MAJOR(args->rdev));
  936. *p++ = htonl(MINOR(args->rdev));
  937. }
  938. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  939. return 0;
  940. }
  941. /*
  942. * 3.3.11 MKNOD3args
  943. *
  944. * struct devicedata3 {
  945. * sattr3 dev_attributes;
  946. * specdata3 spec;
  947. * };
  948. *
  949. * union mknoddata3 switch (ftype3 type) {
  950. * case NF3CHR:
  951. * case NF3BLK:
  952. * devicedata3 device;
  953. * case NF3SOCK:
  954. * case NF3FIFO:
  955. * sattr3 pipe_attributes;
  956. * default:
  957. * void;
  958. * };
  959. *
  960. * struct MKNOD3args {
  961. * diropargs3 where;
  962. * mknoddata3 what;
  963. * };
  964. */
  965. static void encode_devicedata3(struct xdr_stream *xdr,
  966. const struct nfs3_mknodargs *args)
  967. {
  968. encode_sattr3(xdr, args->sattr);
  969. encode_specdata3(xdr, args->rdev);
  970. }
  971. static void encode_mknoddata3(struct xdr_stream *xdr,
  972. const struct nfs3_mknodargs *args)
  973. {
  974. encode_ftype3(xdr, args->type);
  975. switch (args->type) {
  976. case NF3CHR:
  977. case NF3BLK:
  978. encode_devicedata3(xdr, args);
  979. break;
  980. case NF3SOCK:
  981. case NF3FIFO:
  982. encode_sattr3(xdr, args->sattr);
  983. break;
  984. case NF3REG:
  985. case NF3DIR:
  986. break;
  987. default:
  988. BUG();
  989. }
  990. }
  991. static int nfs3_xdr_enc_mknod3args(struct rpc_rqst *req, __be32 *p,
  992. const struct nfs3_mknodargs *args)
  993. {
  994. struct xdr_stream xdr;
  995. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  996. encode_diropargs3(&xdr, args->fh, args->name, args->len);
  997. encode_mknoddata3(&xdr, args);
  998. return 0;
  999. }
  1000. /*
  1001. * 3.3.12 REMOVE3args
  1002. *
  1003. * struct REMOVE3args {
  1004. * diropargs3 object;
  1005. * };
  1006. */
  1007. static int nfs3_xdr_enc_remove3args(struct rpc_rqst *req, __be32 *p,
  1008. const struct nfs_removeargs *args)
  1009. {
  1010. struct xdr_stream xdr;
  1011. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  1012. encode_diropargs3(&xdr, args->fh, args->name.name, args->name.len);
  1013. return 0;
  1014. }
  1015. /*
  1016. * Encode RENAME arguments
  1017. */
  1018. static int
  1019. nfs3_xdr_renameargs(struct rpc_rqst *req, __be32 *p, struct nfs_renameargs *args)
  1020. {
  1021. p = xdr_encode_fhandle(p, args->old_dir);
  1022. p = xdr_encode_array(p, args->old_name->name, args->old_name->len);
  1023. p = xdr_encode_fhandle(p, args->new_dir);
  1024. p = xdr_encode_array(p, args->new_name->name, args->new_name->len);
  1025. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  1026. return 0;
  1027. }
  1028. /*
  1029. * 3.3.14 RENAME3args
  1030. *
  1031. * struct RENAME3args {
  1032. * diropargs3 from;
  1033. * diropargs3 to;
  1034. * };
  1035. */
  1036. static int nfs3_xdr_enc_rename3args(struct rpc_rqst *req, __be32 *p,
  1037. const struct nfs_renameargs *args)
  1038. {
  1039. const struct qstr *old = args->old_name;
  1040. const struct qstr *new = args->new_name;
  1041. struct xdr_stream xdr;
  1042. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  1043. encode_diropargs3(&xdr, args->old_dir, old->name, old->len);
  1044. encode_diropargs3(&xdr, args->new_dir, new->name, new->len);
  1045. return 0;
  1046. }
  1047. /*
  1048. * Encode LINK arguments
  1049. */
  1050. static int
  1051. nfs3_xdr_linkargs(struct rpc_rqst *req, __be32 *p, struct nfs3_linkargs *args)
  1052. {
  1053. p = xdr_encode_fhandle(p, args->fromfh);
  1054. p = xdr_encode_fhandle(p, args->tofh);
  1055. p = xdr_encode_array(p, args->toname, args->tolen);
  1056. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  1057. return 0;
  1058. }
  1059. /*
  1060. * 3.3.15 LINK3args
  1061. *
  1062. * struct LINK3args {
  1063. * nfs_fh3 file;
  1064. * diropargs3 link;
  1065. * };
  1066. */
  1067. static int nfs3_xdr_enc_link3args(struct rpc_rqst *req, __be32 *p,
  1068. const struct nfs3_linkargs *args)
  1069. {
  1070. struct xdr_stream xdr;
  1071. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  1072. encode_nfs_fh3(&xdr, args->fromfh);
  1073. encode_diropargs3(&xdr, args->tofh, args->toname, args->tolen);
  1074. return 0;
  1075. }
  1076. /*
  1077. * Encode arguments to readdir call
  1078. */
  1079. static int
  1080. nfs3_xdr_readdirargs(struct rpc_rqst *req, __be32 *p, struct nfs3_readdirargs *args)
  1081. {
  1082. struct rpc_auth *auth = req->rq_cred->cr_auth;
  1083. unsigned int replen;
  1084. u32 count = args->count;
  1085. p = xdr_encode_fhandle(p, args->fh);
  1086. p = xdr_encode_hyper(p, args->cookie);
  1087. *p++ = args->verf[0];
  1088. *p++ = args->verf[1];
  1089. if (args->plus) {
  1090. /* readdirplus: need dircount + buffer size.
  1091. * We just make sure we make dircount big enough */
  1092. *p++ = htonl(count >> 3);
  1093. }
  1094. *p++ = htonl(count);
  1095. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  1096. /* Inline the page array */
  1097. replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS3_readdirres_sz) << 2;
  1098. xdr_inline_pages(&req->rq_rcv_buf, replen, args->pages, 0, count);
  1099. return 0;
  1100. }
  1101. /*
  1102. * 3.3.16 READDIR3args
  1103. *
  1104. * struct READDIR3args {
  1105. * nfs_fh3 dir;
  1106. * cookie3 cookie;
  1107. * cookieverf3 cookieverf;
  1108. * count3 count;
  1109. * };
  1110. */
  1111. static void encode_readdir3args(struct xdr_stream *xdr,
  1112. const struct nfs3_readdirargs *args)
  1113. {
  1114. __be32 *p;
  1115. encode_nfs_fh3(xdr, args->fh);
  1116. p = xdr_reserve_space(xdr, 8 + NFS3_COOKIEVERFSIZE + 4);
  1117. p = xdr_encode_cookie3(p, args->cookie);
  1118. p = xdr_encode_cookieverf3(p, args->verf);
  1119. *p = cpu_to_be32(args->count);
  1120. }
  1121. static int nfs3_xdr_enc_readdir3args(struct rpc_rqst *req, __be32 *p,
  1122. const struct nfs3_readdirargs *args)
  1123. {
  1124. struct xdr_stream xdr;
  1125. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  1126. encode_readdir3args(&xdr, args);
  1127. prepare_reply_buffer(req, args->pages, 0,
  1128. args->count, NFS3_readdirres_sz);
  1129. return 0;
  1130. }
  1131. /*
  1132. * 3.3.17 READDIRPLUS3args
  1133. *
  1134. * struct READDIRPLUS3args {
  1135. * nfs_fh3 dir;
  1136. * cookie3 cookie;
  1137. * cookieverf3 cookieverf;
  1138. * count3 dircount;
  1139. * count3 maxcount;
  1140. * };
  1141. */
  1142. static void encode_readdirplus3args(struct xdr_stream *xdr,
  1143. const struct nfs3_readdirargs *args)
  1144. {
  1145. __be32 *p;
  1146. encode_nfs_fh3(xdr, args->fh);
  1147. p = xdr_reserve_space(xdr, 8 + NFS3_COOKIEVERFSIZE + 4 + 4);
  1148. p = xdr_encode_cookie3(p, args->cookie);
  1149. p = xdr_encode_cookieverf3(p, args->verf);
  1150. /*
  1151. * readdirplus: need dircount + buffer size.
  1152. * We just make sure we make dircount big enough
  1153. */
  1154. *p++ = cpu_to_be32(args->count >> 3);
  1155. *p = cpu_to_be32(args->count);
  1156. }
  1157. static int nfs3_xdr_enc_readdirplus3args(struct rpc_rqst *req, __be32 *p,
  1158. const struct nfs3_readdirargs *args)
  1159. {
  1160. struct xdr_stream xdr;
  1161. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  1162. encode_readdirplus3args(&xdr, args);
  1163. prepare_reply_buffer(req, args->pages, 0,
  1164. args->count, NFS3_readdirres_sz);
  1165. return 0;
  1166. }
  1167. /*
  1168. * Decode the result of a readdir call.
  1169. * We just check for syntactical correctness.
  1170. */
  1171. static int
  1172. nfs3_xdr_readdirres(struct rpc_rqst *req, __be32 *p, struct nfs3_readdirres *res)
  1173. {
  1174. struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
  1175. struct kvec *iov = rcvbuf->head;
  1176. struct page **page;
  1177. size_t hdrlen;
  1178. u32 recvd, pglen;
  1179. int status;
  1180. status = ntohl(*p++);
  1181. /* Decode post_op_attrs */
  1182. p = xdr_decode_post_op_attr(p, res->dir_attr);
  1183. if (status)
  1184. return nfs_stat_to_errno(status);
  1185. /* Decode verifier cookie */
  1186. if (res->verf) {
  1187. res->verf[0] = *p++;
  1188. res->verf[1] = *p++;
  1189. } else {
  1190. p += 2;
  1191. }
  1192. hdrlen = (u8 *) p - (u8 *) iov->iov_base;
  1193. if (iov->iov_len < hdrlen) {
  1194. dprintk("NFS: READDIR reply header overflowed:"
  1195. "length %Zu > %Zu\n", hdrlen, iov->iov_len);
  1196. return -errno_NFSERR_IO;
  1197. } else if (iov->iov_len != hdrlen) {
  1198. dprintk("NFS: READDIR header is short. iovec will be shifted.\n");
  1199. xdr_shift_buf(rcvbuf, iov->iov_len - hdrlen);
  1200. }
  1201. pglen = rcvbuf->page_len;
  1202. recvd = rcvbuf->len - hdrlen;
  1203. if (pglen > recvd)
  1204. pglen = recvd;
  1205. page = rcvbuf->pages;
  1206. return pglen;
  1207. }
  1208. __be32 *
  1209. nfs3_decode_dirent(struct xdr_stream *xdr, struct nfs_entry *entry, struct nfs_server *server, int plus)
  1210. {
  1211. __be32 *p;
  1212. struct nfs_entry old = *entry;
  1213. p = xdr_inline_decode(xdr, 4);
  1214. if (unlikely(!p))
  1215. goto out_overflow;
  1216. if (!ntohl(*p++)) {
  1217. p = xdr_inline_decode(xdr, 4);
  1218. if (unlikely(!p))
  1219. goto out_overflow;
  1220. if (!ntohl(*p++))
  1221. return ERR_PTR(-EAGAIN);
  1222. entry->eof = 1;
  1223. return ERR_PTR(-EBADCOOKIE);
  1224. }
  1225. p = xdr_inline_decode(xdr, 12);
  1226. if (unlikely(!p))
  1227. goto out_overflow;
  1228. p = xdr_decode_hyper(p, &entry->ino);
  1229. entry->len = ntohl(*p++);
  1230. p = xdr_inline_decode(xdr, entry->len + 8);
  1231. if (unlikely(!p))
  1232. goto out_overflow;
  1233. entry->name = (const char *) p;
  1234. p += XDR_QUADLEN(entry->len);
  1235. entry->prev_cookie = entry->cookie;
  1236. p = xdr_decode_hyper(p, &entry->cookie);
  1237. entry->d_type = DT_UNKNOWN;
  1238. if (plus) {
  1239. entry->fattr->valid = 0;
  1240. p = xdr_decode_post_op_attr_stream(xdr, entry->fattr);
  1241. if (IS_ERR(p))
  1242. goto out_overflow_exit;
  1243. entry->d_type = nfs_umode_to_dtype(entry->fattr->mode);
  1244. /* In fact, a post_op_fh3: */
  1245. p = xdr_inline_decode(xdr, 4);
  1246. if (unlikely(!p))
  1247. goto out_overflow;
  1248. if (*p++) {
  1249. p = xdr_decode_fhandle_stream(xdr, entry->fh);
  1250. if (IS_ERR(p))
  1251. goto out_overflow_exit;
  1252. /* Ugh -- server reply was truncated */
  1253. if (p == NULL) {
  1254. dprintk("NFS: FH truncated\n");
  1255. *entry = old;
  1256. return ERR_PTR(-EAGAIN);
  1257. }
  1258. } else
  1259. memset((u8*)(entry->fh), 0, sizeof(*entry->fh));
  1260. }
  1261. p = xdr_inline_peek(xdr, 8);
  1262. if (p != NULL)
  1263. entry->eof = !p[0] && p[1];
  1264. else
  1265. entry->eof = 0;
  1266. return p;
  1267. out_overflow:
  1268. print_overflow_msg(__func__, xdr);
  1269. out_overflow_exit:
  1270. return ERR_PTR(-EAGAIN);
  1271. }
  1272. /*
  1273. * Encode COMMIT arguments
  1274. */
  1275. static int
  1276. nfs3_xdr_commitargs(struct rpc_rqst *req, __be32 *p, struct nfs_writeargs *args)
  1277. {
  1278. p = xdr_encode_fhandle(p, args->fh);
  1279. p = xdr_encode_hyper(p, args->offset);
  1280. *p++ = htonl(args->count);
  1281. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  1282. return 0;
  1283. }
  1284. /*
  1285. * 3.3.21 COMMIT3args
  1286. *
  1287. * struct COMMIT3args {
  1288. * nfs_fh3 file;
  1289. * offset3 offset;
  1290. * count3 count;
  1291. * };
  1292. */
  1293. static void encode_commit3args(struct xdr_stream *xdr,
  1294. const struct nfs_writeargs *args)
  1295. {
  1296. __be32 *p;
  1297. encode_nfs_fh3(xdr, args->fh);
  1298. p = xdr_reserve_space(xdr, 8 + 4);
  1299. p = xdr_encode_hyper(p, args->offset);
  1300. *p = cpu_to_be32(args->count);
  1301. }
  1302. static int nfs3_xdr_enc_commit3args(struct rpc_rqst *req, __be32 *p,
  1303. const struct nfs_writeargs *args)
  1304. {
  1305. struct xdr_stream xdr;
  1306. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  1307. encode_commit3args(&xdr, args);
  1308. return 0;
  1309. }
  1310. #ifdef CONFIG_NFS_V3_ACL
  1311. /*
  1312. * Encode GETACL arguments
  1313. */
  1314. static int
  1315. nfs3_xdr_getaclargs(struct rpc_rqst *req, __be32 *p,
  1316. struct nfs3_getaclargs *args)
  1317. {
  1318. struct rpc_auth *auth = req->rq_cred->cr_auth;
  1319. unsigned int replen;
  1320. p = xdr_encode_fhandle(p, args->fh);
  1321. *p++ = htonl(args->mask);
  1322. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  1323. if (args->mask & (NFS_ACL | NFS_DFACL)) {
  1324. /* Inline the page array */
  1325. replen = (RPC_REPHDRSIZE + auth->au_rslack +
  1326. ACL3_getaclres_sz) << 2;
  1327. xdr_inline_pages(&req->rq_rcv_buf, replen, args->pages, 0,
  1328. NFSACL_MAXPAGES << PAGE_SHIFT);
  1329. }
  1330. return 0;
  1331. }
  1332. static int nfs3_xdr_enc_getacl3args(struct rpc_rqst *req, __be32 *p,
  1333. const struct nfs3_getaclargs *args)
  1334. {
  1335. struct xdr_stream xdr;
  1336. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  1337. encode_nfs_fh3(&xdr, args->fh);
  1338. encode_uint32(&xdr, args->mask);
  1339. if (args->mask & (NFS_ACL | NFS_DFACL))
  1340. prepare_reply_buffer(req, args->pages, 0,
  1341. NFSACL_MAXPAGES << PAGE_SHIFT,
  1342. ACL3_getaclres_sz);
  1343. return 0;
  1344. }
  1345. /*
  1346. * Encode SETACL arguments
  1347. */
  1348. static int
  1349. nfs3_xdr_setaclargs(struct rpc_rqst *req, __be32 *p,
  1350. struct nfs3_setaclargs *args)
  1351. {
  1352. struct xdr_buf *buf = &req->rq_snd_buf;
  1353. unsigned int base;
  1354. int err;
  1355. p = xdr_encode_fhandle(p, NFS_FH(args->inode));
  1356. *p++ = htonl(args->mask);
  1357. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  1358. base = req->rq_slen;
  1359. if (args->npages != 0)
  1360. xdr_encode_pages(buf, args->pages, 0, args->len);
  1361. else
  1362. req->rq_slen = xdr_adjust_iovec(req->rq_svec,
  1363. p + XDR_QUADLEN(args->len));
  1364. err = nfsacl_encode(buf, base, args->inode,
  1365. (args->mask & NFS_ACL) ?
  1366. args->acl_access : NULL, 1, 0);
  1367. if (err > 0)
  1368. err = nfsacl_encode(buf, base + err, args->inode,
  1369. (args->mask & NFS_DFACL) ?
  1370. args->acl_default : NULL, 1,
  1371. NFS_ACL_DEFAULT);
  1372. return (err > 0) ? 0 : err;
  1373. }
  1374. static int nfs3_xdr_enc_setacl3args(struct rpc_rqst *req, __be32 *p,
  1375. const struct nfs3_setaclargs *args)
  1376. {
  1377. struct xdr_stream xdr;
  1378. unsigned int base;
  1379. int error;
  1380. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  1381. encode_nfs_fh3(&xdr, NFS_FH(args->inode));
  1382. encode_uint32(&xdr, args->mask);
  1383. if (args->npages != 0)
  1384. xdr_write_pages(&xdr, args->pages, 0, args->len);
  1385. base = req->rq_slen;
  1386. error = nfsacl_encode(xdr.buf, base, args->inode,
  1387. (args->mask & NFS_ACL) ?
  1388. args->acl_access : NULL, 1, 0);
  1389. BUG_ON(error < 0);
  1390. error = nfsacl_encode(xdr.buf, base + error, args->inode,
  1391. (args->mask & NFS_DFACL) ?
  1392. args->acl_default : NULL, 1,
  1393. NFS_ACL_DEFAULT);
  1394. BUG_ON(error < 0);
  1395. return 0;
  1396. }
  1397. #endif /* CONFIG_NFS_V3_ACL */
  1398. /*
  1399. * NFS XDR decode functions
  1400. */
  1401. /*
  1402. * Decode attrstat reply.
  1403. */
  1404. static int
  1405. nfs3_xdr_attrstat(struct rpc_rqst *req, __be32 *p, struct nfs_fattr *fattr)
  1406. {
  1407. int status;
  1408. if ((status = ntohl(*p++)))
  1409. return nfs_stat_to_errno(status);
  1410. xdr_decode_fattr(p, fattr);
  1411. return 0;
  1412. }
  1413. /*
  1414. * Decode status+wcc_data reply
  1415. * SATTR, REMOVE, RMDIR
  1416. */
  1417. static int
  1418. nfs3_xdr_wccstat(struct rpc_rqst *req, __be32 *p, struct nfs_fattr *fattr)
  1419. {
  1420. int status;
  1421. if ((status = ntohl(*p++)))
  1422. status = nfs_stat_to_errno(status);
  1423. xdr_decode_wcc_data(p, fattr);
  1424. return status;
  1425. }
  1426. static int
  1427. nfs3_xdr_removeres(struct rpc_rqst *req, __be32 *p, struct nfs_removeres *res)
  1428. {
  1429. return nfs3_xdr_wccstat(req, p, res->dir_attr);
  1430. }
  1431. /*
  1432. * Decode LOOKUP reply
  1433. */
  1434. static int
  1435. nfs3_xdr_lookupres(struct rpc_rqst *req, __be32 *p, struct nfs3_diropres *res)
  1436. {
  1437. int status;
  1438. if ((status = ntohl(*p++))) {
  1439. status = nfs_stat_to_errno(status);
  1440. } else {
  1441. if (!(p = xdr_decode_fhandle(p, res->fh)))
  1442. return -errno_NFSERR_IO;
  1443. p = xdr_decode_post_op_attr(p, res->fattr);
  1444. }
  1445. xdr_decode_post_op_attr(p, res->dir_attr);
  1446. return status;
  1447. }
  1448. /*
  1449. * Decode ACCESS reply
  1450. */
  1451. static int
  1452. nfs3_xdr_accessres(struct rpc_rqst *req, __be32 *p, struct nfs3_accessres *res)
  1453. {
  1454. int status = ntohl(*p++);
  1455. p = xdr_decode_post_op_attr(p, res->fattr);
  1456. if (status)
  1457. return nfs_stat_to_errno(status);
  1458. res->access = ntohl(*p++);
  1459. return 0;
  1460. }
  1461. static int
  1462. nfs3_xdr_readlinkargs(struct rpc_rqst *req, __be32 *p, struct nfs3_readlinkargs *args)
  1463. {
  1464. struct rpc_auth *auth = req->rq_cred->cr_auth;
  1465. unsigned int replen;
  1466. p = xdr_encode_fhandle(p, args->fh);
  1467. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  1468. /* Inline the page array */
  1469. replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS3_readlinkres_sz) << 2;
  1470. xdr_inline_pages(&req->rq_rcv_buf, replen, args->pages, args->pgbase, args->pglen);
  1471. return 0;
  1472. }
  1473. /*
  1474. * Decode READLINK reply
  1475. */
  1476. static int
  1477. nfs3_xdr_readlinkres(struct rpc_rqst *req, __be32 *p, struct nfs_fattr *fattr)
  1478. {
  1479. struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
  1480. struct kvec *iov = rcvbuf->head;
  1481. size_t hdrlen;
  1482. u32 len, recvd;
  1483. int status;
  1484. status = ntohl(*p++);
  1485. p = xdr_decode_post_op_attr(p, fattr);
  1486. if (status != 0)
  1487. return nfs_stat_to_errno(status);
  1488. /* Convert length of symlink */
  1489. len = ntohl(*p++);
  1490. if (len >= rcvbuf->page_len) {
  1491. dprintk("nfs: server returned giant symlink!\n");
  1492. return -ENAMETOOLONG;
  1493. }
  1494. hdrlen = (u8 *) p - (u8 *) iov->iov_base;
  1495. if (iov->iov_len < hdrlen) {
  1496. dprintk("NFS: READLINK reply header overflowed:"
  1497. "length %Zu > %Zu\n", hdrlen, iov->iov_len);
  1498. return -errno_NFSERR_IO;
  1499. } else if (iov->iov_len != hdrlen) {
  1500. dprintk("NFS: READLINK header is short. "
  1501. "iovec will be shifted.\n");
  1502. xdr_shift_buf(rcvbuf, iov->iov_len - hdrlen);
  1503. }
  1504. recvd = req->rq_rcv_buf.len - hdrlen;
  1505. if (recvd < len) {
  1506. dprintk("NFS: server cheating in readlink reply: "
  1507. "count %u > recvd %u\n", len, recvd);
  1508. return -EIO;
  1509. }
  1510. xdr_terminate_string(rcvbuf, len);
  1511. return 0;
  1512. }
  1513. /*
  1514. * Decode READ reply
  1515. */
  1516. static int
  1517. nfs3_xdr_readres(struct rpc_rqst *req, __be32 *p, struct nfs_readres *res)
  1518. {
  1519. struct kvec *iov = req->rq_rcv_buf.head;
  1520. size_t hdrlen;
  1521. u32 count, ocount, recvd;
  1522. int status;
  1523. status = ntohl(*p++);
  1524. p = xdr_decode_post_op_attr(p, res->fattr);
  1525. if (status != 0)
  1526. return nfs_stat_to_errno(status);
  1527. /* Decode reply count and EOF flag. NFSv3 is somewhat redundant
  1528. * in that it puts the count both in the res struct and in the
  1529. * opaque data count. */
  1530. count = ntohl(*p++);
  1531. res->eof = ntohl(*p++);
  1532. ocount = ntohl(*p++);
  1533. if (ocount != count) {
  1534. dprintk("NFS: READ count doesn't match RPC opaque count.\n");
  1535. return -errno_NFSERR_IO;
  1536. }
  1537. hdrlen = (u8 *) p - (u8 *) iov->iov_base;
  1538. if (iov->iov_len < hdrlen) {
  1539. dprintk("NFS: READ reply header overflowed:"
  1540. "length %Zu > %Zu\n", hdrlen, iov->iov_len);
  1541. return -errno_NFSERR_IO;
  1542. } else if (iov->iov_len != hdrlen) {
  1543. dprintk("NFS: READ header is short. iovec will be shifted.\n");
  1544. xdr_shift_buf(&req->rq_rcv_buf, iov->iov_len - hdrlen);
  1545. }
  1546. recvd = req->rq_rcv_buf.len - hdrlen;
  1547. if (count > recvd) {
  1548. dprintk("NFS: server cheating in read reply: "
  1549. "count %u > recvd %u\n", count, recvd);
  1550. count = recvd;
  1551. res->eof = 0;
  1552. }
  1553. if (count < res->count)
  1554. res->count = count;
  1555. return count;
  1556. }
  1557. /*
  1558. * Decode WRITE response
  1559. */
  1560. static int
  1561. nfs3_xdr_writeres(struct rpc_rqst *req, __be32 *p, struct nfs_writeres *res)
  1562. {
  1563. int status;
  1564. status = ntohl(*p++);
  1565. p = xdr_decode_wcc_data(p, res->fattr);
  1566. if (status != 0)
  1567. return nfs_stat_to_errno(status);
  1568. res->count = ntohl(*p++);
  1569. res->verf->committed = (enum nfs3_stable_how)ntohl(*p++);
  1570. res->verf->verifier[0] = *p++;
  1571. res->verf->verifier[1] = *p++;
  1572. return res->count;
  1573. }
  1574. /*
  1575. * Decode a CREATE response
  1576. */
  1577. static int
  1578. nfs3_xdr_createres(struct rpc_rqst *req, __be32 *p, struct nfs3_diropres *res)
  1579. {
  1580. int status;
  1581. status = ntohl(*p++);
  1582. if (status == 0) {
  1583. if (*p++) {
  1584. if (!(p = xdr_decode_fhandle(p, res->fh)))
  1585. return -errno_NFSERR_IO;
  1586. p = xdr_decode_post_op_attr(p, res->fattr);
  1587. } else {
  1588. memset(res->fh, 0, sizeof(*res->fh));
  1589. /* Do decode post_op_attr but set it to NULL */
  1590. p = xdr_decode_post_op_attr(p, res->fattr);
  1591. res->fattr->valid = 0;
  1592. }
  1593. } else {
  1594. status = nfs_stat_to_errno(status);
  1595. }
  1596. p = xdr_decode_wcc_data(p, res->dir_attr);
  1597. return status;
  1598. }
  1599. /*
  1600. * Decode RENAME reply
  1601. */
  1602. static int
  1603. nfs3_xdr_renameres(struct rpc_rqst *req, __be32 *p, struct nfs_renameres *res)
  1604. {
  1605. int status;
  1606. if ((status = ntohl(*p++)) != 0)
  1607. status = nfs_stat_to_errno(status);
  1608. p = xdr_decode_wcc_data(p, res->old_fattr);
  1609. p = xdr_decode_wcc_data(p, res->new_fattr);
  1610. return status;
  1611. }
  1612. /*
  1613. * Decode LINK reply
  1614. */
  1615. static int
  1616. nfs3_xdr_linkres(struct rpc_rqst *req, __be32 *p, struct nfs3_linkres *res)
  1617. {
  1618. int status;
  1619. if ((status = ntohl(*p++)) != 0)
  1620. status = nfs_stat_to_errno(status);
  1621. p = xdr_decode_post_op_attr(p, res->fattr);
  1622. p = xdr_decode_wcc_data(p, res->dir_attr);
  1623. return status;
  1624. }
  1625. /*
  1626. * Decode FSSTAT reply
  1627. */
  1628. static int
  1629. nfs3_xdr_fsstatres(struct rpc_rqst *req, __be32 *p, struct nfs_fsstat *res)
  1630. {
  1631. int status;
  1632. status = ntohl(*p++);
  1633. p = xdr_decode_post_op_attr(p, res->fattr);
  1634. if (status != 0)
  1635. return nfs_stat_to_errno(status);
  1636. p = xdr_decode_hyper(p, &res->tbytes);
  1637. p = xdr_decode_hyper(p, &res->fbytes);
  1638. p = xdr_decode_hyper(p, &res->abytes);
  1639. p = xdr_decode_hyper(p, &res->tfiles);
  1640. p = xdr_decode_hyper(p, &res->ffiles);
  1641. p = xdr_decode_hyper(p, &res->afiles);
  1642. /* ignore invarsec */
  1643. return 0;
  1644. }
  1645. /*
  1646. * Decode FSINFO reply
  1647. */
  1648. static int
  1649. nfs3_xdr_fsinfores(struct rpc_rqst *req, __be32 *p, struct nfs_fsinfo *res)
  1650. {
  1651. int status;
  1652. status = ntohl(*p++);
  1653. p = xdr_decode_post_op_attr(p, res->fattr);
  1654. if (status != 0)
  1655. return nfs_stat_to_errno(status);
  1656. res->rtmax = ntohl(*p++);
  1657. res->rtpref = ntohl(*p++);
  1658. res->rtmult = ntohl(*p++);
  1659. res->wtmax = ntohl(*p++);
  1660. res->wtpref = ntohl(*p++);
  1661. res->wtmult = ntohl(*p++);
  1662. res->dtpref = ntohl(*p++);
  1663. p = xdr_decode_hyper(p, &res->maxfilesize);
  1664. p = xdr_decode_time3(p, &res->time_delta);
  1665. /* ignore properties */
  1666. res->lease_time = 0;
  1667. return 0;
  1668. }
  1669. /*
  1670. * Decode PATHCONF reply
  1671. */
  1672. static int
  1673. nfs3_xdr_pathconfres(struct rpc_rqst *req, __be32 *p, struct nfs_pathconf *res)
  1674. {
  1675. int status;
  1676. status = ntohl(*p++);
  1677. p = xdr_decode_post_op_attr(p, res->fattr);
  1678. if (status != 0)
  1679. return nfs_stat_to_errno(status);
  1680. res->max_link = ntohl(*p++);
  1681. res->max_namelen = ntohl(*p++);
  1682. /* ignore remaining fields */
  1683. return 0;
  1684. }
  1685. /*
  1686. * Decode COMMIT reply
  1687. */
  1688. static int
  1689. nfs3_xdr_commitres(struct rpc_rqst *req, __be32 *p, struct nfs_writeres *res)
  1690. {
  1691. int status;
  1692. status = ntohl(*p++);
  1693. p = xdr_decode_wcc_data(p, res->fattr);
  1694. if (status != 0)
  1695. return nfs_stat_to_errno(status);
  1696. res->verf->verifier[0] = *p++;
  1697. res->verf->verifier[1] = *p++;
  1698. return 0;
  1699. }
  1700. #ifdef CONFIG_NFS_V3_ACL
  1701. /*
  1702. * Decode GETACL reply
  1703. */
  1704. static int
  1705. nfs3_xdr_getaclres(struct rpc_rqst *req, __be32 *p,
  1706. struct nfs3_getaclres *res)
  1707. {
  1708. struct xdr_buf *buf = &req->rq_rcv_buf;
  1709. int status = ntohl(*p++);
  1710. struct posix_acl **acl;
  1711. unsigned int *aclcnt;
  1712. int err, base;
  1713. if (status != 0)
  1714. return nfs_stat_to_errno(status);
  1715. p = xdr_decode_post_op_attr(p, res->fattr);
  1716. res->mask = ntohl(*p++);
  1717. if (res->mask & ~(NFS_ACL|NFS_ACLCNT|NFS_DFACL|NFS_DFACLCNT))
  1718. return -EINVAL;
  1719. base = (char *)p - (char *)req->rq_rcv_buf.head->iov_base;
  1720. acl = (res->mask & NFS_ACL) ? &res->acl_access : NULL;
  1721. aclcnt = (res->mask & NFS_ACLCNT) ? &res->acl_access_count : NULL;
  1722. err = nfsacl_decode(buf, base, aclcnt, acl);
  1723. acl = (res->mask & NFS_DFACL) ? &res->acl_default : NULL;
  1724. aclcnt = (res->mask & NFS_DFACLCNT) ? &res->acl_default_count : NULL;
  1725. if (err > 0)
  1726. err = nfsacl_decode(buf, base + err, aclcnt, acl);
  1727. return (err > 0) ? 0 : err;
  1728. }
  1729. /*
  1730. * Decode setacl reply.
  1731. */
  1732. static int
  1733. nfs3_xdr_setaclres(struct rpc_rqst *req, __be32 *p, struct nfs_fattr *fattr)
  1734. {
  1735. int status = ntohl(*p++);
  1736. if (status)
  1737. return nfs_stat_to_errno(status);
  1738. xdr_decode_post_op_attr(p, fattr);
  1739. return 0;
  1740. }
  1741. #endif /* CONFIG_NFS_V3_ACL */
  1742. #define PROC(proc, argtype, restype, timer) \
  1743. [NFS3PROC_##proc] = { \
  1744. .p_proc = NFS3PROC_##proc, \
  1745. .p_encode = (kxdrproc_t) nfs3_xdr_##argtype, \
  1746. .p_decode = (kxdrproc_t) nfs3_xdr_##restype, \
  1747. .p_arglen = NFS3_##argtype##_sz, \
  1748. .p_replen = NFS3_##restype##_sz, \
  1749. .p_timer = timer, \
  1750. .p_statidx = NFS3PROC_##proc, \
  1751. .p_name = #proc, \
  1752. }
  1753. struct rpc_procinfo nfs3_procedures[] = {
  1754. PROC(GETATTR, fhandle, attrstat, 1),
  1755. PROC(SETATTR, sattrargs, wccstat, 0),
  1756. PROC(LOOKUP, diropargs, lookupres, 2),
  1757. PROC(ACCESS, accessargs, accessres, 1),
  1758. PROC(READLINK, readlinkargs, readlinkres, 3),
  1759. PROC(READ, readargs, readres, 3),
  1760. PROC(WRITE, writeargs, writeres, 4),
  1761. PROC(CREATE, createargs, createres, 0),
  1762. PROC(MKDIR, mkdirargs, createres, 0),
  1763. PROC(SYMLINK, symlinkargs, createres, 0),
  1764. PROC(MKNOD, mknodargs, createres, 0),
  1765. PROC(REMOVE, removeargs, removeres, 0),
  1766. PROC(RMDIR, diropargs, wccstat, 0),
  1767. PROC(RENAME, renameargs, renameres, 0),
  1768. PROC(LINK, linkargs, linkres, 0),
  1769. PROC(READDIR, readdirargs, readdirres, 3),
  1770. PROC(READDIRPLUS, readdirargs, readdirres, 3),
  1771. PROC(FSSTAT, fhandle, fsstatres, 0),
  1772. PROC(FSINFO, fhandle, fsinfores, 0),
  1773. PROC(PATHCONF, fhandle, pathconfres, 0),
  1774. PROC(COMMIT, commitargs, commitres, 5),
  1775. };
  1776. struct rpc_version nfs_version3 = {
  1777. .number = 3,
  1778. .nrprocs = ARRAY_SIZE(nfs3_procedures),
  1779. .procs = nfs3_procedures
  1780. };
  1781. #ifdef CONFIG_NFS_V3_ACL
  1782. static struct rpc_procinfo nfs3_acl_procedures[] = {
  1783. [ACLPROC3_GETACL] = {
  1784. .p_proc = ACLPROC3_GETACL,
  1785. .p_encode = (kxdrproc_t) nfs3_xdr_getaclargs,
  1786. .p_decode = (kxdrproc_t) nfs3_xdr_getaclres,
  1787. .p_arglen = ACL3_getaclargs_sz,
  1788. .p_replen = ACL3_getaclres_sz,
  1789. .p_timer = 1,
  1790. .p_name = "GETACL",
  1791. },
  1792. [ACLPROC3_SETACL] = {
  1793. .p_proc = ACLPROC3_SETACL,
  1794. .p_encode = (kxdrproc_t) nfs3_xdr_setaclargs,
  1795. .p_decode = (kxdrproc_t) nfs3_xdr_setaclres,
  1796. .p_arglen = ACL3_setaclargs_sz,
  1797. .p_replen = ACL3_setaclres_sz,
  1798. .p_timer = 0,
  1799. .p_name = "SETACL",
  1800. },
  1801. };
  1802. struct rpc_version nfsacl_version3 = {
  1803. .number = 3,
  1804. .nrprocs = sizeof(nfs3_acl_procedures)/
  1805. sizeof(nfs3_acl_procedures[0]),
  1806. .procs = nfs3_acl_procedures,
  1807. };
  1808. #endif /* CONFIG_NFS_V3_ACL */