nfs4xdr.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687
  1. /*
  2. * fs/nfs/nfs4xdr.c
  3. *
  4. * Server-side XDR for NFSv4
  5. *
  6. * Copyright (c) 2002 The Regents of the University of Michigan.
  7. * All rights reserved.
  8. *
  9. * Kendrick Smith <kmsmith@umich.edu>
  10. * Andy Adamson <andros@umich.edu>
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. *
  16. * 1. Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. * 2. Redistributions in binary form must reproduce the above copyright
  19. * notice, this list of conditions and the following disclaimer in the
  20. * documentation and/or other materials provided with the distribution.
  21. * 3. Neither the name of the University nor the names of its
  22. * contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  26. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  27. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  32. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  33. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. * TODO: Neil Brown made the following observation: We currently
  38. * initially reserve NFSD_BUFSIZE space on the transmit queue and
  39. * never release any of that until the request is complete.
  40. * It would be good to calculate a new maximum response size while
  41. * decoding the COMPOUND, and call svc_reserve with this number
  42. * at the end of nfs4svc_decode_compoundargs.
  43. */
  44. #include <linux/param.h>
  45. #include <linux/smp.h>
  46. #include <linux/fs.h>
  47. #include <linux/namei.h>
  48. #include <linux/vfs.h>
  49. #include <linux/sunrpc/xdr.h>
  50. #include <linux/sunrpc/svc.h>
  51. #include <linux/sunrpc/clnt.h>
  52. #include <linux/nfsd/nfsd.h>
  53. #include <linux/nfsd/state.h>
  54. #include <linux/nfsd/xdr4.h>
  55. #include <linux/nfsd_idmap.h>
  56. #include <linux/nfs4.h>
  57. #include <linux/nfs4_acl.h>
  58. #define NFSDDBG_FACILITY NFSDDBG_XDR
  59. /*
  60. * As per referral draft, the fsid for a referral MUST be different from the fsid of the containing
  61. * directory in order to indicate to the client that a filesystem boundary is present
  62. * We use a fixed fsid for a referral
  63. */
  64. #define NFS4_REFERRAL_FSID_MAJOR 0x8000000ULL
  65. #define NFS4_REFERRAL_FSID_MINOR 0x8000000ULL
  66. static __be32
  67. check_filename(char *str, int len, __be32 err)
  68. {
  69. int i;
  70. if (len == 0)
  71. return nfserr_inval;
  72. if (isdotent(str, len))
  73. return err;
  74. for (i = 0; i < len; i++)
  75. if (str[i] == '/')
  76. return err;
  77. return 0;
  78. }
  79. /*
  80. * START OF "GENERIC" DECODE ROUTINES.
  81. * These may look a little ugly since they are imported from a "generic"
  82. * set of XDR encode/decode routines which are intended to be shared by
  83. * all of our NFSv4 implementations (OpenBSD, MacOS X...).
  84. *
  85. * If the pain of reading these is too great, it should be a straightforward
  86. * task to translate them into Linux-specific versions which are more
  87. * consistent with the style used in NFSv2/v3...
  88. */
  89. #define DECODE_HEAD \
  90. __be32 *p; \
  91. __be32 status
  92. #define DECODE_TAIL \
  93. status = 0; \
  94. out: \
  95. return status; \
  96. xdr_error: \
  97. printk(KERN_NOTICE "xdr error! (%s:%d)\n", __FILE__, __LINE__); \
  98. status = nfserr_bad_xdr; \
  99. goto out
  100. #define READ32(x) (x) = ntohl(*p++)
  101. #define READ64(x) do { \
  102. (x) = (u64)ntohl(*p++) << 32; \
  103. (x) |= ntohl(*p++); \
  104. } while (0)
  105. #define READTIME(x) do { \
  106. p++; \
  107. (x) = ntohl(*p++); \
  108. p++; \
  109. } while (0)
  110. #define READMEM(x,nbytes) do { \
  111. x = (char *)p; \
  112. p += XDR_QUADLEN(nbytes); \
  113. } while (0)
  114. #define SAVEMEM(x,nbytes) do { \
  115. if (!(x = (p==argp->tmp || p == argp->tmpp) ? \
  116. savemem(argp, p, nbytes) : \
  117. (char *)p)) { \
  118. printk(KERN_NOTICE "xdr error! (%s:%d)\n", __FILE__, __LINE__); \
  119. goto xdr_error; \
  120. } \
  121. p += XDR_QUADLEN(nbytes); \
  122. } while (0)
  123. #define COPYMEM(x,nbytes) do { \
  124. memcpy((x), p, nbytes); \
  125. p += XDR_QUADLEN(nbytes); \
  126. } while (0)
  127. /* READ_BUF, read_buf(): nbytes must be <= PAGE_SIZE */
  128. #define READ_BUF(nbytes) do { \
  129. if (nbytes <= (u32)((char *)argp->end - (char *)argp->p)) { \
  130. p = argp->p; \
  131. argp->p += XDR_QUADLEN(nbytes); \
  132. } else if (!(p = read_buf(argp, nbytes))) { \
  133. printk(KERN_NOTICE "xdr error! (%s:%d)\n", __FILE__, __LINE__); \
  134. goto xdr_error; \
  135. } \
  136. } while (0)
  137. static __be32 *read_buf(struct nfsd4_compoundargs *argp, int nbytes)
  138. {
  139. /* We want more bytes than seem to be available.
  140. * Maybe we need a new page, maybe we have just run out
  141. */
  142. int avail = (char*)argp->end - (char*)argp->p;
  143. __be32 *p;
  144. if (avail + argp->pagelen < nbytes)
  145. return NULL;
  146. if (avail + PAGE_SIZE < nbytes) /* need more than a page !! */
  147. return NULL;
  148. /* ok, we can do it with the current plus the next page */
  149. if (nbytes <= sizeof(argp->tmp))
  150. p = argp->tmp;
  151. else {
  152. kfree(argp->tmpp);
  153. p = argp->tmpp = kmalloc(nbytes, GFP_KERNEL);
  154. if (!p)
  155. return NULL;
  156. }
  157. memcpy(p, argp->p, avail);
  158. /* step to next page */
  159. argp->p = page_address(argp->pagelist[0]);
  160. argp->pagelist++;
  161. if (argp->pagelen < PAGE_SIZE) {
  162. argp->end = p + (argp->pagelen>>2);
  163. argp->pagelen = 0;
  164. } else {
  165. argp->end = p + (PAGE_SIZE>>2);
  166. argp->pagelen -= PAGE_SIZE;
  167. }
  168. memcpy(((char*)p)+avail, argp->p, (nbytes - avail));
  169. argp->p += XDR_QUADLEN(nbytes - avail);
  170. return p;
  171. }
  172. static int
  173. defer_free(struct nfsd4_compoundargs *argp,
  174. void (*release)(const void *), void *p)
  175. {
  176. struct tmpbuf *tb;
  177. tb = kmalloc(sizeof(*tb), GFP_KERNEL);
  178. if (!tb)
  179. return -ENOMEM;
  180. tb->buf = p;
  181. tb->release = release;
  182. tb->next = argp->to_free;
  183. argp->to_free = tb;
  184. return 0;
  185. }
  186. static char *savemem(struct nfsd4_compoundargs *argp, __be32 *p, int nbytes)
  187. {
  188. if (p == argp->tmp) {
  189. p = kmalloc(nbytes, GFP_KERNEL);
  190. if (!p)
  191. return NULL;
  192. memcpy(p, argp->tmp, nbytes);
  193. } else {
  194. BUG_ON(p != argp->tmpp);
  195. argp->tmpp = NULL;
  196. }
  197. if (defer_free(argp, kfree, p)) {
  198. kfree(p);
  199. return NULL;
  200. } else
  201. return (char *)p;
  202. }
  203. static __be32
  204. nfsd4_decode_bitmap(struct nfsd4_compoundargs *argp, u32 *bmval)
  205. {
  206. u32 bmlen;
  207. DECODE_HEAD;
  208. bmval[0] = 0;
  209. bmval[1] = 0;
  210. READ_BUF(4);
  211. READ32(bmlen);
  212. if (bmlen > 1000)
  213. goto xdr_error;
  214. READ_BUF(bmlen << 2);
  215. if (bmlen > 0)
  216. READ32(bmval[0]);
  217. if (bmlen > 1)
  218. READ32(bmval[1]);
  219. DECODE_TAIL;
  220. }
  221. static __be32
  222. nfsd4_decode_fattr(struct nfsd4_compoundargs *argp, u32 *bmval, struct iattr *iattr,
  223. struct nfs4_acl **acl)
  224. {
  225. int expected_len, len = 0;
  226. u32 dummy32;
  227. char *buf;
  228. int host_err;
  229. DECODE_HEAD;
  230. iattr->ia_valid = 0;
  231. if ((status = nfsd4_decode_bitmap(argp, bmval)))
  232. return status;
  233. /*
  234. * According to spec, unsupported attributes return ERR_ATTRNOTSUPP;
  235. * read-only attributes return ERR_INVAL.
  236. */
  237. if ((bmval[0] & ~NFSD_SUPPORTED_ATTRS_WORD0) || (bmval[1] & ~NFSD_SUPPORTED_ATTRS_WORD1))
  238. return nfserr_attrnotsupp;
  239. if ((bmval[0] & ~NFSD_WRITEABLE_ATTRS_WORD0) || (bmval[1] & ~NFSD_WRITEABLE_ATTRS_WORD1))
  240. return nfserr_inval;
  241. READ_BUF(4);
  242. READ32(expected_len);
  243. if (bmval[0] & FATTR4_WORD0_SIZE) {
  244. READ_BUF(8);
  245. len += 8;
  246. READ64(iattr->ia_size);
  247. iattr->ia_valid |= ATTR_SIZE;
  248. }
  249. if (bmval[0] & FATTR4_WORD0_ACL) {
  250. int nace;
  251. struct nfs4_ace *ace;
  252. READ_BUF(4); len += 4;
  253. READ32(nace);
  254. if (nace > NFS4_ACL_MAX)
  255. return nfserr_resource;
  256. *acl = nfs4_acl_new(nace);
  257. if (*acl == NULL) {
  258. host_err = -ENOMEM;
  259. goto out_nfserr;
  260. }
  261. defer_free(argp, kfree, *acl);
  262. (*acl)->naces = nace;
  263. for (ace = (*acl)->aces; ace < (*acl)->aces + nace; ace++) {
  264. READ_BUF(16); len += 16;
  265. READ32(ace->type);
  266. READ32(ace->flag);
  267. READ32(ace->access_mask);
  268. READ32(dummy32);
  269. READ_BUF(dummy32);
  270. len += XDR_QUADLEN(dummy32) << 2;
  271. READMEM(buf, dummy32);
  272. ace->whotype = nfs4_acl_get_whotype(buf, dummy32);
  273. host_err = 0;
  274. if (ace->whotype != NFS4_ACL_WHO_NAMED)
  275. ace->who = 0;
  276. else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
  277. host_err = nfsd_map_name_to_gid(argp->rqstp,
  278. buf, dummy32, &ace->who);
  279. else
  280. host_err = nfsd_map_name_to_uid(argp->rqstp,
  281. buf, dummy32, &ace->who);
  282. if (host_err)
  283. goto out_nfserr;
  284. }
  285. } else
  286. *acl = NULL;
  287. if (bmval[1] & FATTR4_WORD1_MODE) {
  288. READ_BUF(4);
  289. len += 4;
  290. READ32(iattr->ia_mode);
  291. iattr->ia_mode &= (S_IFMT | S_IALLUGO);
  292. iattr->ia_valid |= ATTR_MODE;
  293. }
  294. if (bmval[1] & FATTR4_WORD1_OWNER) {
  295. READ_BUF(4);
  296. len += 4;
  297. READ32(dummy32);
  298. READ_BUF(dummy32);
  299. len += (XDR_QUADLEN(dummy32) << 2);
  300. READMEM(buf, dummy32);
  301. if ((host_err = nfsd_map_name_to_uid(argp->rqstp, buf, dummy32, &iattr->ia_uid)))
  302. goto out_nfserr;
  303. iattr->ia_valid |= ATTR_UID;
  304. }
  305. if (bmval[1] & FATTR4_WORD1_OWNER_GROUP) {
  306. READ_BUF(4);
  307. len += 4;
  308. READ32(dummy32);
  309. READ_BUF(dummy32);
  310. len += (XDR_QUADLEN(dummy32) << 2);
  311. READMEM(buf, dummy32);
  312. if ((host_err = nfsd_map_name_to_gid(argp->rqstp, buf, dummy32, &iattr->ia_gid)))
  313. goto out_nfserr;
  314. iattr->ia_valid |= ATTR_GID;
  315. }
  316. if (bmval[1] & FATTR4_WORD1_TIME_ACCESS_SET) {
  317. READ_BUF(4);
  318. len += 4;
  319. READ32(dummy32);
  320. switch (dummy32) {
  321. case NFS4_SET_TO_CLIENT_TIME:
  322. /* We require the high 32 bits of 'seconds' to be 0, and we ignore
  323. all 32 bits of 'nseconds'. */
  324. READ_BUF(12);
  325. len += 12;
  326. READ32(dummy32);
  327. if (dummy32)
  328. return nfserr_inval;
  329. READ32(iattr->ia_atime.tv_sec);
  330. READ32(iattr->ia_atime.tv_nsec);
  331. if (iattr->ia_atime.tv_nsec >= (u32)1000000000)
  332. return nfserr_inval;
  333. iattr->ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
  334. break;
  335. case NFS4_SET_TO_SERVER_TIME:
  336. iattr->ia_valid |= ATTR_ATIME;
  337. break;
  338. default:
  339. goto xdr_error;
  340. }
  341. }
  342. if (bmval[1] & FATTR4_WORD1_TIME_METADATA) {
  343. /* We require the high 32 bits of 'seconds' to be 0, and we ignore
  344. all 32 bits of 'nseconds'. */
  345. READ_BUF(12);
  346. len += 12;
  347. READ32(dummy32);
  348. if (dummy32)
  349. return nfserr_inval;
  350. READ32(iattr->ia_ctime.tv_sec);
  351. READ32(iattr->ia_ctime.tv_nsec);
  352. if (iattr->ia_ctime.tv_nsec >= (u32)1000000000)
  353. return nfserr_inval;
  354. iattr->ia_valid |= ATTR_CTIME;
  355. }
  356. if (bmval[1] & FATTR4_WORD1_TIME_MODIFY_SET) {
  357. READ_BUF(4);
  358. len += 4;
  359. READ32(dummy32);
  360. switch (dummy32) {
  361. case NFS4_SET_TO_CLIENT_TIME:
  362. /* We require the high 32 bits of 'seconds' to be 0, and we ignore
  363. all 32 bits of 'nseconds'. */
  364. READ_BUF(12);
  365. len += 12;
  366. READ32(dummy32);
  367. if (dummy32)
  368. return nfserr_inval;
  369. READ32(iattr->ia_mtime.tv_sec);
  370. READ32(iattr->ia_mtime.tv_nsec);
  371. if (iattr->ia_mtime.tv_nsec >= (u32)1000000000)
  372. return nfserr_inval;
  373. iattr->ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
  374. break;
  375. case NFS4_SET_TO_SERVER_TIME:
  376. iattr->ia_valid |= ATTR_MTIME;
  377. break;
  378. default:
  379. goto xdr_error;
  380. }
  381. }
  382. if (len != expected_len)
  383. goto xdr_error;
  384. DECODE_TAIL;
  385. out_nfserr:
  386. status = nfserrno(host_err);
  387. goto out;
  388. }
  389. static __be32
  390. nfsd4_decode_access(struct nfsd4_compoundargs *argp, struct nfsd4_access *access)
  391. {
  392. DECODE_HEAD;
  393. READ_BUF(4);
  394. READ32(access->ac_req_access);
  395. DECODE_TAIL;
  396. }
  397. static __be32
  398. nfsd4_decode_close(struct nfsd4_compoundargs *argp, struct nfsd4_close *close)
  399. {
  400. DECODE_HEAD;
  401. close->cl_stateowner = NULL;
  402. READ_BUF(4 + sizeof(stateid_t));
  403. READ32(close->cl_seqid);
  404. READ32(close->cl_stateid.si_generation);
  405. COPYMEM(&close->cl_stateid.si_opaque, sizeof(stateid_opaque_t));
  406. DECODE_TAIL;
  407. }
  408. static __be32
  409. nfsd4_decode_commit(struct nfsd4_compoundargs *argp, struct nfsd4_commit *commit)
  410. {
  411. DECODE_HEAD;
  412. READ_BUF(12);
  413. READ64(commit->co_offset);
  414. READ32(commit->co_count);
  415. DECODE_TAIL;
  416. }
  417. static __be32
  418. nfsd4_decode_create(struct nfsd4_compoundargs *argp, struct nfsd4_create *create)
  419. {
  420. DECODE_HEAD;
  421. READ_BUF(4);
  422. READ32(create->cr_type);
  423. switch (create->cr_type) {
  424. case NF4LNK:
  425. READ_BUF(4);
  426. READ32(create->cr_linklen);
  427. READ_BUF(create->cr_linklen);
  428. SAVEMEM(create->cr_linkname, create->cr_linklen);
  429. break;
  430. case NF4BLK:
  431. case NF4CHR:
  432. READ_BUF(8);
  433. READ32(create->cr_specdata1);
  434. READ32(create->cr_specdata2);
  435. break;
  436. case NF4SOCK:
  437. case NF4FIFO:
  438. case NF4DIR:
  439. default:
  440. break;
  441. }
  442. READ_BUF(4);
  443. READ32(create->cr_namelen);
  444. READ_BUF(create->cr_namelen);
  445. SAVEMEM(create->cr_name, create->cr_namelen);
  446. if ((status = check_filename(create->cr_name, create->cr_namelen, nfserr_inval)))
  447. return status;
  448. if ((status = nfsd4_decode_fattr(argp, create->cr_bmval, &create->cr_iattr, &create->cr_acl)))
  449. goto out;
  450. DECODE_TAIL;
  451. }
  452. static inline __be32
  453. nfsd4_decode_delegreturn(struct nfsd4_compoundargs *argp, struct nfsd4_delegreturn *dr)
  454. {
  455. DECODE_HEAD;
  456. READ_BUF(sizeof(stateid_t));
  457. READ32(dr->dr_stateid.si_generation);
  458. COPYMEM(&dr->dr_stateid.si_opaque, sizeof(stateid_opaque_t));
  459. DECODE_TAIL;
  460. }
  461. static inline __be32
  462. nfsd4_decode_getattr(struct nfsd4_compoundargs *argp, struct nfsd4_getattr *getattr)
  463. {
  464. return nfsd4_decode_bitmap(argp, getattr->ga_bmval);
  465. }
  466. static __be32
  467. nfsd4_decode_link(struct nfsd4_compoundargs *argp, struct nfsd4_link *link)
  468. {
  469. DECODE_HEAD;
  470. READ_BUF(4);
  471. READ32(link->li_namelen);
  472. READ_BUF(link->li_namelen);
  473. SAVEMEM(link->li_name, link->li_namelen);
  474. if ((status = check_filename(link->li_name, link->li_namelen, nfserr_inval)))
  475. return status;
  476. DECODE_TAIL;
  477. }
  478. static __be32
  479. nfsd4_decode_lock(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
  480. {
  481. DECODE_HEAD;
  482. lock->lk_replay_owner = NULL;
  483. /*
  484. * type, reclaim(boolean), offset, length, new_lock_owner(boolean)
  485. */
  486. READ_BUF(28);
  487. READ32(lock->lk_type);
  488. if ((lock->lk_type < NFS4_READ_LT) || (lock->lk_type > NFS4_WRITEW_LT))
  489. goto xdr_error;
  490. READ32(lock->lk_reclaim);
  491. READ64(lock->lk_offset);
  492. READ64(lock->lk_length);
  493. READ32(lock->lk_is_new);
  494. if (lock->lk_is_new) {
  495. READ_BUF(36);
  496. READ32(lock->lk_new_open_seqid);
  497. READ32(lock->lk_new_open_stateid.si_generation);
  498. COPYMEM(&lock->lk_new_open_stateid.si_opaque, sizeof(stateid_opaque_t));
  499. READ32(lock->lk_new_lock_seqid);
  500. COPYMEM(&lock->lk_new_clientid, sizeof(clientid_t));
  501. READ32(lock->lk_new_owner.len);
  502. READ_BUF(lock->lk_new_owner.len);
  503. READMEM(lock->lk_new_owner.data, lock->lk_new_owner.len);
  504. } else {
  505. READ_BUF(20);
  506. READ32(lock->lk_old_lock_stateid.si_generation);
  507. COPYMEM(&lock->lk_old_lock_stateid.si_opaque, sizeof(stateid_opaque_t));
  508. READ32(lock->lk_old_lock_seqid);
  509. }
  510. DECODE_TAIL;
  511. }
  512. static __be32
  513. nfsd4_decode_lockt(struct nfsd4_compoundargs *argp, struct nfsd4_lockt *lockt)
  514. {
  515. DECODE_HEAD;
  516. READ_BUF(32);
  517. READ32(lockt->lt_type);
  518. if((lockt->lt_type < NFS4_READ_LT) || (lockt->lt_type > NFS4_WRITEW_LT))
  519. goto xdr_error;
  520. READ64(lockt->lt_offset);
  521. READ64(lockt->lt_length);
  522. COPYMEM(&lockt->lt_clientid, 8);
  523. READ32(lockt->lt_owner.len);
  524. READ_BUF(lockt->lt_owner.len);
  525. READMEM(lockt->lt_owner.data, lockt->lt_owner.len);
  526. DECODE_TAIL;
  527. }
  528. static __be32
  529. nfsd4_decode_locku(struct nfsd4_compoundargs *argp, struct nfsd4_locku *locku)
  530. {
  531. DECODE_HEAD;
  532. locku->lu_stateowner = NULL;
  533. READ_BUF(24 + sizeof(stateid_t));
  534. READ32(locku->lu_type);
  535. if ((locku->lu_type < NFS4_READ_LT) || (locku->lu_type > NFS4_WRITEW_LT))
  536. goto xdr_error;
  537. READ32(locku->lu_seqid);
  538. READ32(locku->lu_stateid.si_generation);
  539. COPYMEM(&locku->lu_stateid.si_opaque, sizeof(stateid_opaque_t));
  540. READ64(locku->lu_offset);
  541. READ64(locku->lu_length);
  542. DECODE_TAIL;
  543. }
  544. static __be32
  545. nfsd4_decode_lookup(struct nfsd4_compoundargs *argp, struct nfsd4_lookup *lookup)
  546. {
  547. DECODE_HEAD;
  548. READ_BUF(4);
  549. READ32(lookup->lo_len);
  550. READ_BUF(lookup->lo_len);
  551. SAVEMEM(lookup->lo_name, lookup->lo_len);
  552. if ((status = check_filename(lookup->lo_name, lookup->lo_len, nfserr_noent)))
  553. return status;
  554. DECODE_TAIL;
  555. }
  556. static __be32
  557. nfsd4_decode_open(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
  558. {
  559. DECODE_HEAD;
  560. memset(open->op_bmval, 0, sizeof(open->op_bmval));
  561. open->op_iattr.ia_valid = 0;
  562. open->op_stateowner = NULL;
  563. /* seqid, share_access, share_deny, clientid, ownerlen */
  564. READ_BUF(16 + sizeof(clientid_t));
  565. READ32(open->op_seqid);
  566. READ32(open->op_share_access);
  567. READ32(open->op_share_deny);
  568. COPYMEM(&open->op_clientid, sizeof(clientid_t));
  569. READ32(open->op_owner.len);
  570. /* owner, open_flag */
  571. READ_BUF(open->op_owner.len + 4);
  572. SAVEMEM(open->op_owner.data, open->op_owner.len);
  573. READ32(open->op_create);
  574. switch (open->op_create) {
  575. case NFS4_OPEN_NOCREATE:
  576. break;
  577. case NFS4_OPEN_CREATE:
  578. READ_BUF(4);
  579. READ32(open->op_createmode);
  580. switch (open->op_createmode) {
  581. case NFS4_CREATE_UNCHECKED:
  582. case NFS4_CREATE_GUARDED:
  583. if ((status = nfsd4_decode_fattr(argp, open->op_bmval, &open->op_iattr, &open->op_acl)))
  584. goto out;
  585. break;
  586. case NFS4_CREATE_EXCLUSIVE:
  587. READ_BUF(8);
  588. COPYMEM(open->op_verf.data, 8);
  589. break;
  590. default:
  591. goto xdr_error;
  592. }
  593. break;
  594. default:
  595. goto xdr_error;
  596. }
  597. /* open_claim */
  598. READ_BUF(4);
  599. READ32(open->op_claim_type);
  600. switch (open->op_claim_type) {
  601. case NFS4_OPEN_CLAIM_NULL:
  602. case NFS4_OPEN_CLAIM_DELEGATE_PREV:
  603. READ_BUF(4);
  604. READ32(open->op_fname.len);
  605. READ_BUF(open->op_fname.len);
  606. SAVEMEM(open->op_fname.data, open->op_fname.len);
  607. if ((status = check_filename(open->op_fname.data, open->op_fname.len, nfserr_inval)))
  608. return status;
  609. break;
  610. case NFS4_OPEN_CLAIM_PREVIOUS:
  611. READ_BUF(4);
  612. READ32(open->op_delegate_type);
  613. break;
  614. case NFS4_OPEN_CLAIM_DELEGATE_CUR:
  615. READ_BUF(sizeof(stateid_t) + 4);
  616. COPYMEM(&open->op_delegate_stateid, sizeof(stateid_t));
  617. READ32(open->op_fname.len);
  618. READ_BUF(open->op_fname.len);
  619. SAVEMEM(open->op_fname.data, open->op_fname.len);
  620. if ((status = check_filename(open->op_fname.data, open->op_fname.len, nfserr_inval)))
  621. return status;
  622. break;
  623. default:
  624. goto xdr_error;
  625. }
  626. DECODE_TAIL;
  627. }
  628. static __be32
  629. nfsd4_decode_open_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_open_confirm *open_conf)
  630. {
  631. DECODE_HEAD;
  632. open_conf->oc_stateowner = NULL;
  633. READ_BUF(4 + sizeof(stateid_t));
  634. READ32(open_conf->oc_req_stateid.si_generation);
  635. COPYMEM(&open_conf->oc_req_stateid.si_opaque, sizeof(stateid_opaque_t));
  636. READ32(open_conf->oc_seqid);
  637. DECODE_TAIL;
  638. }
  639. static __be32
  640. nfsd4_decode_open_downgrade(struct nfsd4_compoundargs *argp, struct nfsd4_open_downgrade *open_down)
  641. {
  642. DECODE_HEAD;
  643. open_down->od_stateowner = NULL;
  644. READ_BUF(12 + sizeof(stateid_t));
  645. READ32(open_down->od_stateid.si_generation);
  646. COPYMEM(&open_down->od_stateid.si_opaque, sizeof(stateid_opaque_t));
  647. READ32(open_down->od_seqid);
  648. READ32(open_down->od_share_access);
  649. READ32(open_down->od_share_deny);
  650. DECODE_TAIL;
  651. }
  652. static __be32
  653. nfsd4_decode_putfh(struct nfsd4_compoundargs *argp, struct nfsd4_putfh *putfh)
  654. {
  655. DECODE_HEAD;
  656. READ_BUF(4);
  657. READ32(putfh->pf_fhlen);
  658. if (putfh->pf_fhlen > NFS4_FHSIZE)
  659. goto xdr_error;
  660. READ_BUF(putfh->pf_fhlen);
  661. SAVEMEM(putfh->pf_fhval, putfh->pf_fhlen);
  662. DECODE_TAIL;
  663. }
  664. static __be32
  665. nfsd4_decode_read(struct nfsd4_compoundargs *argp, struct nfsd4_read *read)
  666. {
  667. DECODE_HEAD;
  668. READ_BUF(sizeof(stateid_t) + 12);
  669. READ32(read->rd_stateid.si_generation);
  670. COPYMEM(&read->rd_stateid.si_opaque, sizeof(stateid_opaque_t));
  671. READ64(read->rd_offset);
  672. READ32(read->rd_length);
  673. DECODE_TAIL;
  674. }
  675. static __be32
  676. nfsd4_decode_readdir(struct nfsd4_compoundargs *argp, struct nfsd4_readdir *readdir)
  677. {
  678. DECODE_HEAD;
  679. READ_BUF(24);
  680. READ64(readdir->rd_cookie);
  681. COPYMEM(readdir->rd_verf.data, sizeof(readdir->rd_verf.data));
  682. READ32(readdir->rd_dircount); /* just in case you needed a useless field... */
  683. READ32(readdir->rd_maxcount);
  684. if ((status = nfsd4_decode_bitmap(argp, readdir->rd_bmval)))
  685. goto out;
  686. DECODE_TAIL;
  687. }
  688. static __be32
  689. nfsd4_decode_remove(struct nfsd4_compoundargs *argp, struct nfsd4_remove *remove)
  690. {
  691. DECODE_HEAD;
  692. READ_BUF(4);
  693. READ32(remove->rm_namelen);
  694. READ_BUF(remove->rm_namelen);
  695. SAVEMEM(remove->rm_name, remove->rm_namelen);
  696. if ((status = check_filename(remove->rm_name, remove->rm_namelen, nfserr_noent)))
  697. return status;
  698. DECODE_TAIL;
  699. }
  700. static __be32
  701. nfsd4_decode_rename(struct nfsd4_compoundargs *argp, struct nfsd4_rename *rename)
  702. {
  703. DECODE_HEAD;
  704. READ_BUF(4);
  705. READ32(rename->rn_snamelen);
  706. READ_BUF(rename->rn_snamelen + 4);
  707. SAVEMEM(rename->rn_sname, rename->rn_snamelen);
  708. READ32(rename->rn_tnamelen);
  709. READ_BUF(rename->rn_tnamelen);
  710. SAVEMEM(rename->rn_tname, rename->rn_tnamelen);
  711. if ((status = check_filename(rename->rn_sname, rename->rn_snamelen, nfserr_noent)))
  712. return status;
  713. if ((status = check_filename(rename->rn_tname, rename->rn_tnamelen, nfserr_inval)))
  714. return status;
  715. DECODE_TAIL;
  716. }
  717. static __be32
  718. nfsd4_decode_renew(struct nfsd4_compoundargs *argp, clientid_t *clientid)
  719. {
  720. DECODE_HEAD;
  721. READ_BUF(sizeof(clientid_t));
  722. COPYMEM(clientid, sizeof(clientid_t));
  723. DECODE_TAIL;
  724. }
  725. static __be32
  726. nfsd4_decode_setattr(struct nfsd4_compoundargs *argp, struct nfsd4_setattr *setattr)
  727. {
  728. DECODE_HEAD;
  729. READ_BUF(sizeof(stateid_t));
  730. READ32(setattr->sa_stateid.si_generation);
  731. COPYMEM(&setattr->sa_stateid.si_opaque, sizeof(stateid_opaque_t));
  732. if ((status = nfsd4_decode_fattr(argp, setattr->sa_bmval, &setattr->sa_iattr, &setattr->sa_acl)))
  733. goto out;
  734. DECODE_TAIL;
  735. }
  736. static __be32
  737. nfsd4_decode_setclientid(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid *setclientid)
  738. {
  739. DECODE_HEAD;
  740. READ_BUF(12);
  741. COPYMEM(setclientid->se_verf.data, 8);
  742. READ32(setclientid->se_namelen);
  743. READ_BUF(setclientid->se_namelen + 8);
  744. SAVEMEM(setclientid->se_name, setclientid->se_namelen);
  745. READ32(setclientid->se_callback_prog);
  746. READ32(setclientid->se_callback_netid_len);
  747. READ_BUF(setclientid->se_callback_netid_len + 4);
  748. SAVEMEM(setclientid->se_callback_netid_val, setclientid->se_callback_netid_len);
  749. READ32(setclientid->se_callback_addr_len);
  750. READ_BUF(setclientid->se_callback_addr_len + 4);
  751. SAVEMEM(setclientid->se_callback_addr_val, setclientid->se_callback_addr_len);
  752. READ32(setclientid->se_callback_ident);
  753. DECODE_TAIL;
  754. }
  755. static __be32
  756. nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid_confirm *scd_c)
  757. {
  758. DECODE_HEAD;
  759. READ_BUF(8 + sizeof(nfs4_verifier));
  760. COPYMEM(&scd_c->sc_clientid, 8);
  761. COPYMEM(&scd_c->sc_confirm, sizeof(nfs4_verifier));
  762. DECODE_TAIL;
  763. }
  764. /* Also used for NVERIFY */
  765. static __be32
  766. nfsd4_decode_verify(struct nfsd4_compoundargs *argp, struct nfsd4_verify *verify)
  767. {
  768. #if 0
  769. struct nfsd4_compoundargs save = {
  770. .p = argp->p,
  771. .end = argp->end,
  772. .rqstp = argp->rqstp,
  773. };
  774. u32 ve_bmval[2];
  775. struct iattr ve_iattr; /* request */
  776. struct nfs4_acl *ve_acl; /* request */
  777. #endif
  778. DECODE_HEAD;
  779. if ((status = nfsd4_decode_bitmap(argp, verify->ve_bmval)))
  780. goto out;
  781. /* For convenience's sake, we compare raw xdr'd attributes in
  782. * nfsd4_proc_verify; however we still decode here just to return
  783. * correct error in case of bad xdr. */
  784. #if 0
  785. status = nfsd4_decode_fattr(ve_bmval, &ve_iattr, &ve_acl);
  786. if (status == nfserr_inval) {
  787. status = nfserrno(status);
  788. goto out;
  789. }
  790. #endif
  791. READ_BUF(4);
  792. READ32(verify->ve_attrlen);
  793. READ_BUF(verify->ve_attrlen);
  794. SAVEMEM(verify->ve_attrval, verify->ve_attrlen);
  795. DECODE_TAIL;
  796. }
  797. static __be32
  798. nfsd4_decode_write(struct nfsd4_compoundargs *argp, struct nfsd4_write *write)
  799. {
  800. int avail;
  801. int v;
  802. int len;
  803. DECODE_HEAD;
  804. READ_BUF(sizeof(stateid_opaque_t) + 20);
  805. READ32(write->wr_stateid.si_generation);
  806. COPYMEM(&write->wr_stateid.si_opaque, sizeof(stateid_opaque_t));
  807. READ64(write->wr_offset);
  808. READ32(write->wr_stable_how);
  809. if (write->wr_stable_how > 2)
  810. goto xdr_error;
  811. READ32(write->wr_buflen);
  812. /* Sorry .. no magic macros for this.. *
  813. * READ_BUF(write->wr_buflen);
  814. * SAVEMEM(write->wr_buf, write->wr_buflen);
  815. */
  816. avail = (char*)argp->end - (char*)argp->p;
  817. if (avail + argp->pagelen < write->wr_buflen) {
  818. printk(KERN_NOTICE "xdr error! (%s:%d)\n", __FILE__, __LINE__);
  819. goto xdr_error;
  820. }
  821. argp->rqstp->rq_vec[0].iov_base = p;
  822. argp->rqstp->rq_vec[0].iov_len = avail;
  823. v = 0;
  824. len = write->wr_buflen;
  825. while (len > argp->rqstp->rq_vec[v].iov_len) {
  826. len -= argp->rqstp->rq_vec[v].iov_len;
  827. v++;
  828. argp->rqstp->rq_vec[v].iov_base = page_address(argp->pagelist[0]);
  829. argp->pagelist++;
  830. if (argp->pagelen >= PAGE_SIZE) {
  831. argp->rqstp->rq_vec[v].iov_len = PAGE_SIZE;
  832. argp->pagelen -= PAGE_SIZE;
  833. } else {
  834. argp->rqstp->rq_vec[v].iov_len = argp->pagelen;
  835. argp->pagelen -= len;
  836. }
  837. }
  838. argp->end = (__be32*) (argp->rqstp->rq_vec[v].iov_base + argp->rqstp->rq_vec[v].iov_len);
  839. argp->p = (__be32*) (argp->rqstp->rq_vec[v].iov_base + (XDR_QUADLEN(len) << 2));
  840. argp->rqstp->rq_vec[v].iov_len = len;
  841. write->wr_vlen = v+1;
  842. DECODE_TAIL;
  843. }
  844. static __be32
  845. nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp, struct nfsd4_release_lockowner *rlockowner)
  846. {
  847. DECODE_HEAD;
  848. READ_BUF(12);
  849. COPYMEM(&rlockowner->rl_clientid, sizeof(clientid_t));
  850. READ32(rlockowner->rl_owner.len);
  851. READ_BUF(rlockowner->rl_owner.len);
  852. READMEM(rlockowner->rl_owner.data, rlockowner->rl_owner.len);
  853. DECODE_TAIL;
  854. }
  855. static __be32
  856. nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
  857. {
  858. DECODE_HEAD;
  859. struct nfsd4_op *op;
  860. int i;
  861. /*
  862. * XXX: According to spec, we should check the tag
  863. * for UTF-8 compliance. I'm postponing this for
  864. * now because it seems that some clients do use
  865. * binary tags.
  866. */
  867. READ_BUF(4);
  868. READ32(argp->taglen);
  869. READ_BUF(argp->taglen + 8);
  870. SAVEMEM(argp->tag, argp->taglen);
  871. READ32(argp->minorversion);
  872. READ32(argp->opcnt);
  873. if (argp->taglen > NFSD4_MAX_TAGLEN)
  874. goto xdr_error;
  875. if (argp->opcnt > 100)
  876. goto xdr_error;
  877. if (argp->opcnt > ARRAY_SIZE(argp->iops)) {
  878. argp->ops = kmalloc(argp->opcnt * sizeof(*argp->ops), GFP_KERNEL);
  879. if (!argp->ops) {
  880. argp->ops = argp->iops;
  881. printk(KERN_INFO "nfsd: couldn't allocate room for COMPOUND\n");
  882. goto xdr_error;
  883. }
  884. }
  885. for (i = 0; i < argp->opcnt; i++) {
  886. op = &argp->ops[i];
  887. op->replay = NULL;
  888. /*
  889. * We can't use READ_BUF() here because we need to handle
  890. * a missing opcode as an OP_WRITE + 1. So we need to check
  891. * to see if we're truly at the end of our buffer or if there
  892. * is another page we need to flip to.
  893. */
  894. if (argp->p == argp->end) {
  895. if (argp->pagelen < 4) {
  896. /* There isn't an opcode still on the wire */
  897. op->opnum = OP_WRITE + 1;
  898. op->status = nfserr_bad_xdr;
  899. argp->opcnt = i+1;
  900. break;
  901. }
  902. /*
  903. * False alarm. We just hit a page boundary, but there
  904. * is still data available. Move pointer across page
  905. * boundary. *snip from READ_BUF*
  906. */
  907. argp->p = page_address(argp->pagelist[0]);
  908. argp->pagelist++;
  909. if (argp->pagelen < PAGE_SIZE) {
  910. argp->end = p + (argp->pagelen>>2);
  911. argp->pagelen = 0;
  912. } else {
  913. argp->end = p + (PAGE_SIZE>>2);
  914. argp->pagelen -= PAGE_SIZE;
  915. }
  916. }
  917. op->opnum = ntohl(*argp->p++);
  918. switch (op->opnum) {
  919. case 2: /* Reserved operation */
  920. op->opnum = OP_ILLEGAL;
  921. if (argp->minorversion == 0)
  922. op->status = nfserr_op_illegal;
  923. else
  924. op->status = nfserr_minor_vers_mismatch;
  925. break;
  926. case OP_ACCESS:
  927. op->status = nfsd4_decode_access(argp, &op->u.access);
  928. break;
  929. case OP_CLOSE:
  930. op->status = nfsd4_decode_close(argp, &op->u.close);
  931. break;
  932. case OP_COMMIT:
  933. op->status = nfsd4_decode_commit(argp, &op->u.commit);
  934. break;
  935. case OP_CREATE:
  936. op->status = nfsd4_decode_create(argp, &op->u.create);
  937. break;
  938. case OP_DELEGRETURN:
  939. op->status = nfsd4_decode_delegreturn(argp, &op->u.delegreturn);
  940. break;
  941. case OP_GETATTR:
  942. op->status = nfsd4_decode_getattr(argp, &op->u.getattr);
  943. break;
  944. case OP_GETFH:
  945. op->status = nfs_ok;
  946. break;
  947. case OP_LINK:
  948. op->status = nfsd4_decode_link(argp, &op->u.link);
  949. break;
  950. case OP_LOCK:
  951. op->status = nfsd4_decode_lock(argp, &op->u.lock);
  952. break;
  953. case OP_LOCKT:
  954. op->status = nfsd4_decode_lockt(argp, &op->u.lockt);
  955. break;
  956. case OP_LOCKU:
  957. op->status = nfsd4_decode_locku(argp, &op->u.locku);
  958. break;
  959. case OP_LOOKUP:
  960. op->status = nfsd4_decode_lookup(argp, &op->u.lookup);
  961. break;
  962. case OP_LOOKUPP:
  963. op->status = nfs_ok;
  964. break;
  965. case OP_NVERIFY:
  966. op->status = nfsd4_decode_verify(argp, &op->u.nverify);
  967. break;
  968. case OP_OPEN:
  969. op->status = nfsd4_decode_open(argp, &op->u.open);
  970. break;
  971. case OP_OPEN_CONFIRM:
  972. op->status = nfsd4_decode_open_confirm(argp, &op->u.open_confirm);
  973. break;
  974. case OP_OPEN_DOWNGRADE:
  975. op->status = nfsd4_decode_open_downgrade(argp, &op->u.open_downgrade);
  976. break;
  977. case OP_PUTFH:
  978. op->status = nfsd4_decode_putfh(argp, &op->u.putfh);
  979. break;
  980. case OP_PUTROOTFH:
  981. op->status = nfs_ok;
  982. break;
  983. case OP_READ:
  984. op->status = nfsd4_decode_read(argp, &op->u.read);
  985. break;
  986. case OP_READDIR:
  987. op->status = nfsd4_decode_readdir(argp, &op->u.readdir);
  988. break;
  989. case OP_READLINK:
  990. op->status = nfs_ok;
  991. break;
  992. case OP_REMOVE:
  993. op->status = nfsd4_decode_remove(argp, &op->u.remove);
  994. break;
  995. case OP_RENAME:
  996. op->status = nfsd4_decode_rename(argp, &op->u.rename);
  997. break;
  998. case OP_RESTOREFH:
  999. op->status = nfs_ok;
  1000. break;
  1001. case OP_RENEW:
  1002. op->status = nfsd4_decode_renew(argp, &op->u.renew);
  1003. break;
  1004. case OP_SAVEFH:
  1005. op->status = nfs_ok;
  1006. break;
  1007. case OP_SETATTR:
  1008. op->status = nfsd4_decode_setattr(argp, &op->u.setattr);
  1009. break;
  1010. case OP_SETCLIENTID:
  1011. op->status = nfsd4_decode_setclientid(argp, &op->u.setclientid);
  1012. break;
  1013. case OP_SETCLIENTID_CONFIRM:
  1014. op->status = nfsd4_decode_setclientid_confirm(argp, &op->u.setclientid_confirm);
  1015. break;
  1016. case OP_VERIFY:
  1017. op->status = nfsd4_decode_verify(argp, &op->u.verify);
  1018. break;
  1019. case OP_WRITE:
  1020. op->status = nfsd4_decode_write(argp, &op->u.write);
  1021. break;
  1022. case OP_RELEASE_LOCKOWNER:
  1023. op->status = nfsd4_decode_release_lockowner(argp, &op->u.release_lockowner);
  1024. break;
  1025. default:
  1026. op->opnum = OP_ILLEGAL;
  1027. op->status = nfserr_op_illegal;
  1028. break;
  1029. }
  1030. if (op->status) {
  1031. argp->opcnt = i+1;
  1032. break;
  1033. }
  1034. }
  1035. DECODE_TAIL;
  1036. }
  1037. /*
  1038. * END OF "GENERIC" DECODE ROUTINES.
  1039. */
  1040. /*
  1041. * START OF "GENERIC" ENCODE ROUTINES.
  1042. * These may look a little ugly since they are imported from a "generic"
  1043. * set of XDR encode/decode routines which are intended to be shared by
  1044. * all of our NFSv4 implementations (OpenBSD, MacOS X...).
  1045. *
  1046. * If the pain of reading these is too great, it should be a straightforward
  1047. * task to translate them into Linux-specific versions which are more
  1048. * consistent with the style used in NFSv2/v3...
  1049. */
  1050. #define ENCODE_HEAD __be32 *p
  1051. #define WRITE32(n) *p++ = htonl(n)
  1052. #define WRITE64(n) do { \
  1053. *p++ = htonl((u32)((n) >> 32)); \
  1054. *p++ = htonl((u32)(n)); \
  1055. } while (0)
  1056. #define WRITEMEM(ptr,nbytes) do { \
  1057. *(p + XDR_QUADLEN(nbytes) -1) = 0; \
  1058. memcpy(p, ptr, nbytes); \
  1059. p += XDR_QUADLEN(nbytes); \
  1060. } while (0)
  1061. #define WRITECINFO(c) do { \
  1062. *p++ = htonl(c.atomic); \
  1063. *p++ = htonl(c.before_ctime_sec); \
  1064. *p++ = htonl(c.before_ctime_nsec); \
  1065. *p++ = htonl(c.after_ctime_sec); \
  1066. *p++ = htonl(c.after_ctime_nsec); \
  1067. } while (0)
  1068. #define RESERVE_SPACE(nbytes) do { \
  1069. p = resp->p; \
  1070. BUG_ON(p + XDR_QUADLEN(nbytes) > resp->end); \
  1071. } while (0)
  1072. #define ADJUST_ARGS() resp->p = p
  1073. /*
  1074. * Header routine to setup seqid operation replay cache
  1075. */
  1076. #define ENCODE_SEQID_OP_HEAD \
  1077. __be32 *p; \
  1078. __be32 *save; \
  1079. \
  1080. save = resp->p;
  1081. /*
  1082. * Routine for encoding the result of a "seqid-mutating" NFSv4 operation. This
  1083. * is where sequence id's are incremented, and the replay cache is filled.
  1084. * Note that we increment sequence id's here, at the last moment, so we're sure
  1085. * we know whether the error to be returned is a sequence id mutating error.
  1086. */
  1087. #define ENCODE_SEQID_OP_TAIL(stateowner) do { \
  1088. if (seqid_mutating_err(nfserr) && stateowner) { \
  1089. stateowner->so_seqid++; \
  1090. stateowner->so_replay.rp_status = nfserr; \
  1091. stateowner->so_replay.rp_buflen = \
  1092. (((char *)(resp)->p - (char *)save)); \
  1093. memcpy(stateowner->so_replay.rp_buf, save, \
  1094. stateowner->so_replay.rp_buflen); \
  1095. } } while (0);
  1096. /* Encode as an array of strings the string given with components
  1097. * seperated @sep.
  1098. */
  1099. static __be32 nfsd4_encode_components(char sep, char *components,
  1100. __be32 **pp, int *buflen)
  1101. {
  1102. __be32 *p = *pp;
  1103. __be32 *countp = p;
  1104. int strlen, count=0;
  1105. char *str, *end;
  1106. dprintk("nfsd4_encode_components(%s)\n", components);
  1107. if ((*buflen -= 4) < 0)
  1108. return nfserr_resource;
  1109. WRITE32(0); /* We will fill this in with @count later */
  1110. end = str = components;
  1111. while (*end) {
  1112. for (; *end && (*end != sep); end++)
  1113. ; /* Point to end of component */
  1114. strlen = end - str;
  1115. if (strlen) {
  1116. if ((*buflen -= ((XDR_QUADLEN(strlen) << 2) + 4)) < 0)
  1117. return nfserr_resource;
  1118. WRITE32(strlen);
  1119. WRITEMEM(str, strlen);
  1120. count++;
  1121. }
  1122. else
  1123. end++;
  1124. str = end;
  1125. }
  1126. *pp = p;
  1127. p = countp;
  1128. WRITE32(count);
  1129. return 0;
  1130. }
  1131. /*
  1132. * encode a location element of a fs_locations structure
  1133. */
  1134. static __be32 nfsd4_encode_fs_location4(struct nfsd4_fs_location *location,
  1135. __be32 **pp, int *buflen)
  1136. {
  1137. __be32 status;
  1138. __be32 *p = *pp;
  1139. status = nfsd4_encode_components(':', location->hosts, &p, buflen);
  1140. if (status)
  1141. return status;
  1142. status = nfsd4_encode_components('/', location->path, &p, buflen);
  1143. if (status)
  1144. return status;
  1145. *pp = p;
  1146. return 0;
  1147. }
  1148. /*
  1149. * Return the path to an export point in the pseudo filesystem namespace
  1150. * Returned string is safe to use as long as the caller holds a reference
  1151. * to @exp.
  1152. */
  1153. static char *nfsd4_path(struct svc_rqst *rqstp, struct svc_export *exp, __be32 *stat)
  1154. {
  1155. struct svc_fh tmp_fh;
  1156. char *path, *rootpath;
  1157. fh_init(&tmp_fh, NFS4_FHSIZE);
  1158. *stat = exp_pseudoroot(rqstp->rq_client, &tmp_fh, &rqstp->rq_chandle);
  1159. if (*stat)
  1160. return NULL;
  1161. rootpath = tmp_fh.fh_export->ex_path;
  1162. path = exp->ex_path;
  1163. if (strncmp(path, rootpath, strlen(rootpath))) {
  1164. printk("nfsd: fs_locations failed;"
  1165. "%s is not contained in %s\n", path, rootpath);
  1166. *stat = nfserr_notsupp;
  1167. return NULL;
  1168. }
  1169. return path + strlen(rootpath);
  1170. }
  1171. /*
  1172. * encode a fs_locations structure
  1173. */
  1174. static __be32 nfsd4_encode_fs_locations(struct svc_rqst *rqstp,
  1175. struct svc_export *exp,
  1176. __be32 **pp, int *buflen)
  1177. {
  1178. __be32 status;
  1179. int i;
  1180. __be32 *p = *pp;
  1181. struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
  1182. char *root = nfsd4_path(rqstp, exp, &status);
  1183. if (status)
  1184. return status;
  1185. status = nfsd4_encode_components('/', root, &p, buflen);
  1186. if (status)
  1187. return status;
  1188. if ((*buflen -= 4) < 0)
  1189. return nfserr_resource;
  1190. WRITE32(fslocs->locations_count);
  1191. for (i=0; i<fslocs->locations_count; i++) {
  1192. status = nfsd4_encode_fs_location4(&fslocs->locations[i],
  1193. &p, buflen);
  1194. if (status)
  1195. return status;
  1196. }
  1197. *pp = p;
  1198. return 0;
  1199. }
  1200. static u32 nfs4_ftypes[16] = {
  1201. NF4BAD, NF4FIFO, NF4CHR, NF4BAD,
  1202. NF4DIR, NF4BAD, NF4BLK, NF4BAD,
  1203. NF4REG, NF4BAD, NF4LNK, NF4BAD,
  1204. NF4SOCK, NF4BAD, NF4LNK, NF4BAD,
  1205. };
  1206. static __be32
  1207. nfsd4_encode_name(struct svc_rqst *rqstp, int whotype, uid_t id, int group,
  1208. __be32 **p, int *buflen)
  1209. {
  1210. int status;
  1211. if (*buflen < (XDR_QUADLEN(IDMAP_NAMESZ) << 2) + 4)
  1212. return nfserr_resource;
  1213. if (whotype != NFS4_ACL_WHO_NAMED)
  1214. status = nfs4_acl_write_who(whotype, (u8 *)(*p + 1));
  1215. else if (group)
  1216. status = nfsd_map_gid_to_name(rqstp, id, (u8 *)(*p + 1));
  1217. else
  1218. status = nfsd_map_uid_to_name(rqstp, id, (u8 *)(*p + 1));
  1219. if (status < 0)
  1220. return nfserrno(status);
  1221. *p = xdr_encode_opaque(*p, NULL, status);
  1222. *buflen -= (XDR_QUADLEN(status) << 2) + 4;
  1223. BUG_ON(*buflen < 0);
  1224. return 0;
  1225. }
  1226. static inline __be32
  1227. nfsd4_encode_user(struct svc_rqst *rqstp, uid_t uid, __be32 **p, int *buflen)
  1228. {
  1229. return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, uid, 0, p, buflen);
  1230. }
  1231. static inline __be32
  1232. nfsd4_encode_group(struct svc_rqst *rqstp, uid_t gid, __be32 **p, int *buflen)
  1233. {
  1234. return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, gid, 1, p, buflen);
  1235. }
  1236. static inline __be32
  1237. nfsd4_encode_aclname(struct svc_rqst *rqstp, int whotype, uid_t id, int group,
  1238. __be32 **p, int *buflen)
  1239. {
  1240. return nfsd4_encode_name(rqstp, whotype, id, group, p, buflen);
  1241. }
  1242. #define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
  1243. FATTR4_WORD0_RDATTR_ERROR)
  1244. #define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
  1245. static __be32 fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *rdattr_err)
  1246. {
  1247. /* As per referral draft: */
  1248. if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
  1249. *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
  1250. if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
  1251. *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
  1252. *rdattr_err = NFSERR_MOVED;
  1253. else
  1254. return nfserr_moved;
  1255. }
  1256. *bmval0 &= WORD0_ABSENT_FS_ATTRS;
  1257. *bmval1 &= WORD1_ABSENT_FS_ATTRS;
  1258. return 0;
  1259. }
  1260. /*
  1261. * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
  1262. * ourselves.
  1263. *
  1264. * @countp is the buffer size in _words_; upon successful return this becomes
  1265. * replaced with the number of words written.
  1266. */
  1267. __be32
  1268. nfsd4_encode_fattr(struct svc_fh *fhp, struct svc_export *exp,
  1269. struct dentry *dentry, __be32 *buffer, int *countp, u32 *bmval,
  1270. struct svc_rqst *rqstp)
  1271. {
  1272. u32 bmval0 = bmval[0];
  1273. u32 bmval1 = bmval[1];
  1274. struct kstat stat;
  1275. struct svc_fh tempfh;
  1276. struct kstatfs statfs;
  1277. int buflen = *countp << 2;
  1278. __be32 *attrlenp;
  1279. u32 dummy;
  1280. u64 dummy64;
  1281. u32 rdattr_err = 0;
  1282. __be32 *p = buffer;
  1283. __be32 status;
  1284. int err;
  1285. int aclsupport = 0;
  1286. struct nfs4_acl *acl = NULL;
  1287. BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
  1288. BUG_ON(bmval0 & ~NFSD_SUPPORTED_ATTRS_WORD0);
  1289. BUG_ON(bmval1 & ~NFSD_SUPPORTED_ATTRS_WORD1);
  1290. if (exp->ex_fslocs.migrated) {
  1291. status = fattr_handle_absent_fs(&bmval0, &bmval1, &rdattr_err);
  1292. if (status)
  1293. goto out;
  1294. }
  1295. err = vfs_getattr(exp->ex_mnt, dentry, &stat);
  1296. if (err)
  1297. goto out_nfserr;
  1298. if ((bmval0 & (FATTR4_WORD0_FILES_FREE | FATTR4_WORD0_FILES_TOTAL)) ||
  1299. (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
  1300. FATTR4_WORD1_SPACE_TOTAL))) {
  1301. err = vfs_statfs(dentry, &statfs);
  1302. if (err)
  1303. goto out_nfserr;
  1304. }
  1305. if ((bmval0 & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) && !fhp) {
  1306. fh_init(&tempfh, NFS4_FHSIZE);
  1307. status = fh_compose(&tempfh, exp, dentry, NULL);
  1308. if (status)
  1309. goto out;
  1310. fhp = &tempfh;
  1311. }
  1312. if (bmval0 & (FATTR4_WORD0_ACL | FATTR4_WORD0_ACLSUPPORT
  1313. | FATTR4_WORD0_SUPPORTED_ATTRS)) {
  1314. err = nfsd4_get_nfs4_acl(rqstp, dentry, &acl);
  1315. aclsupport = (err == 0);
  1316. if (bmval0 & FATTR4_WORD0_ACL) {
  1317. if (err == -EOPNOTSUPP)
  1318. bmval0 &= ~FATTR4_WORD0_ACL;
  1319. else if (err == -EINVAL) {
  1320. status = nfserr_attrnotsupp;
  1321. goto out;
  1322. } else if (err != 0)
  1323. goto out_nfserr;
  1324. }
  1325. }
  1326. if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
  1327. if (exp->ex_fslocs.locations == NULL) {
  1328. bmval0 &= ~FATTR4_WORD0_FS_LOCATIONS;
  1329. }
  1330. }
  1331. if ((buflen -= 16) < 0)
  1332. goto out_resource;
  1333. WRITE32(2);
  1334. WRITE32(bmval0);
  1335. WRITE32(bmval1);
  1336. attrlenp = p++; /* to be backfilled later */
  1337. if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
  1338. u32 word0 = NFSD_SUPPORTED_ATTRS_WORD0;
  1339. if ((buflen -= 12) < 0)
  1340. goto out_resource;
  1341. if (!aclsupport)
  1342. word0 &= ~FATTR4_WORD0_ACL;
  1343. if (!exp->ex_fslocs.locations)
  1344. word0 &= ~FATTR4_WORD0_FS_LOCATIONS;
  1345. WRITE32(2);
  1346. WRITE32(word0);
  1347. WRITE32(NFSD_SUPPORTED_ATTRS_WORD1);
  1348. }
  1349. if (bmval0 & FATTR4_WORD0_TYPE) {
  1350. if ((buflen -= 4) < 0)
  1351. goto out_resource;
  1352. dummy = nfs4_ftypes[(stat.mode & S_IFMT) >> 12];
  1353. if (dummy == NF4BAD)
  1354. goto out_serverfault;
  1355. WRITE32(dummy);
  1356. }
  1357. if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
  1358. if ((buflen -= 4) < 0)
  1359. goto out_resource;
  1360. if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
  1361. WRITE32(NFS4_FH_PERSISTENT);
  1362. else
  1363. WRITE32(NFS4_FH_PERSISTENT|NFS4_FH_VOL_RENAME);
  1364. }
  1365. if (bmval0 & FATTR4_WORD0_CHANGE) {
  1366. /*
  1367. * Note: This _must_ be consistent with the scheme for writing
  1368. * change_info, so any changes made here must be reflected there
  1369. * as well. (See xdr4.h:set_change_info() and the WRITECINFO()
  1370. * macro above.)
  1371. */
  1372. if ((buflen -= 8) < 0)
  1373. goto out_resource;
  1374. WRITE32(stat.ctime.tv_sec);
  1375. WRITE32(stat.ctime.tv_nsec);
  1376. }
  1377. if (bmval0 & FATTR4_WORD0_SIZE) {
  1378. if ((buflen -= 8) < 0)
  1379. goto out_resource;
  1380. WRITE64(stat.size);
  1381. }
  1382. if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
  1383. if ((buflen -= 4) < 0)
  1384. goto out_resource;
  1385. WRITE32(1);
  1386. }
  1387. if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
  1388. if ((buflen -= 4) < 0)
  1389. goto out_resource;
  1390. WRITE32(1);
  1391. }
  1392. if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
  1393. if ((buflen -= 4) < 0)
  1394. goto out_resource;
  1395. WRITE32(0);
  1396. }
  1397. if (bmval0 & FATTR4_WORD0_FSID) {
  1398. if ((buflen -= 16) < 0)
  1399. goto out_resource;
  1400. if (exp->ex_fslocs.migrated) {
  1401. WRITE64(NFS4_REFERRAL_FSID_MAJOR);
  1402. WRITE64(NFS4_REFERRAL_FSID_MINOR);
  1403. } else switch(fsid_source(fhp)) {
  1404. case FSIDSOURCE_FSID:
  1405. WRITE64((u64)exp->ex_fsid);
  1406. WRITE64((u64)0);
  1407. break;
  1408. case FSIDSOURCE_DEV:
  1409. WRITE32(0);
  1410. WRITE32(MAJOR(stat.dev));
  1411. WRITE32(0);
  1412. WRITE32(MINOR(stat.dev));
  1413. break;
  1414. case FSIDSOURCE_UUID:
  1415. WRITEMEM(exp->ex_uuid, 16);
  1416. break;
  1417. }
  1418. }
  1419. if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
  1420. if ((buflen -= 4) < 0)
  1421. goto out_resource;
  1422. WRITE32(0);
  1423. }
  1424. if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
  1425. if ((buflen -= 4) < 0)
  1426. goto out_resource;
  1427. WRITE32(NFSD_LEASE_TIME);
  1428. }
  1429. if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
  1430. if ((buflen -= 4) < 0)
  1431. goto out_resource;
  1432. WRITE32(rdattr_err);
  1433. }
  1434. if (bmval0 & FATTR4_WORD0_ACL) {
  1435. struct nfs4_ace *ace;
  1436. if (acl == NULL) {
  1437. if ((buflen -= 4) < 0)
  1438. goto out_resource;
  1439. WRITE32(0);
  1440. goto out_acl;
  1441. }
  1442. if ((buflen -= 4) < 0)
  1443. goto out_resource;
  1444. WRITE32(acl->naces);
  1445. for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
  1446. if ((buflen -= 4*3) < 0)
  1447. goto out_resource;
  1448. WRITE32(ace->type);
  1449. WRITE32(ace->flag);
  1450. WRITE32(ace->access_mask & NFS4_ACE_MASK_ALL);
  1451. status = nfsd4_encode_aclname(rqstp, ace->whotype,
  1452. ace->who, ace->flag & NFS4_ACE_IDENTIFIER_GROUP,
  1453. &p, &buflen);
  1454. if (status == nfserr_resource)
  1455. goto out_resource;
  1456. if (status)
  1457. goto out;
  1458. }
  1459. }
  1460. out_acl:
  1461. if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
  1462. if ((buflen -= 4) < 0)
  1463. goto out_resource;
  1464. WRITE32(aclsupport ?
  1465. ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL : 0);
  1466. }
  1467. if (bmval0 & FATTR4_WORD0_CANSETTIME) {
  1468. if ((buflen -= 4) < 0)
  1469. goto out_resource;
  1470. WRITE32(1);
  1471. }
  1472. if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
  1473. if ((buflen -= 4) < 0)
  1474. goto out_resource;
  1475. WRITE32(1);
  1476. }
  1477. if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
  1478. if ((buflen -= 4) < 0)
  1479. goto out_resource;
  1480. WRITE32(1);
  1481. }
  1482. if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
  1483. if ((buflen -= 4) < 0)
  1484. goto out_resource;
  1485. WRITE32(1);
  1486. }
  1487. if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
  1488. buflen -= (XDR_QUADLEN(fhp->fh_handle.fh_size) << 2) + 4;
  1489. if (buflen < 0)
  1490. goto out_resource;
  1491. WRITE32(fhp->fh_handle.fh_size);
  1492. WRITEMEM(&fhp->fh_handle.fh_base, fhp->fh_handle.fh_size);
  1493. }
  1494. if (bmval0 & FATTR4_WORD0_FILEID) {
  1495. if ((buflen -= 8) < 0)
  1496. goto out_resource;
  1497. WRITE64((u64) stat.ino);
  1498. }
  1499. if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
  1500. if ((buflen -= 8) < 0)
  1501. goto out_resource;
  1502. WRITE64((u64) statfs.f_ffree);
  1503. }
  1504. if (bmval0 & FATTR4_WORD0_FILES_FREE) {
  1505. if ((buflen -= 8) < 0)
  1506. goto out_resource;
  1507. WRITE64((u64) statfs.f_ffree);
  1508. }
  1509. if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
  1510. if ((buflen -= 8) < 0)
  1511. goto out_resource;
  1512. WRITE64((u64) statfs.f_files);
  1513. }
  1514. if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
  1515. status = nfsd4_encode_fs_locations(rqstp, exp, &p, &buflen);
  1516. if (status == nfserr_resource)
  1517. goto out_resource;
  1518. if (status)
  1519. goto out;
  1520. }
  1521. if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
  1522. if ((buflen -= 4) < 0)
  1523. goto out_resource;
  1524. WRITE32(1);
  1525. }
  1526. if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
  1527. if ((buflen -= 8) < 0)
  1528. goto out_resource;
  1529. WRITE64(~(u64)0);
  1530. }
  1531. if (bmval0 & FATTR4_WORD0_MAXLINK) {
  1532. if ((buflen -= 4) < 0)
  1533. goto out_resource;
  1534. WRITE32(255);
  1535. }
  1536. if (bmval0 & FATTR4_WORD0_MAXNAME) {
  1537. if ((buflen -= 4) < 0)
  1538. goto out_resource;
  1539. WRITE32(~(u32) 0);
  1540. }
  1541. if (bmval0 & FATTR4_WORD0_MAXREAD) {
  1542. if ((buflen -= 8) < 0)
  1543. goto out_resource;
  1544. WRITE64((u64) svc_max_payload(rqstp));
  1545. }
  1546. if (bmval0 & FATTR4_WORD0_MAXWRITE) {
  1547. if ((buflen -= 8) < 0)
  1548. goto out_resource;
  1549. WRITE64((u64) svc_max_payload(rqstp));
  1550. }
  1551. if (bmval1 & FATTR4_WORD1_MODE) {
  1552. if ((buflen -= 4) < 0)
  1553. goto out_resource;
  1554. WRITE32(stat.mode & S_IALLUGO);
  1555. }
  1556. if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
  1557. if ((buflen -= 4) < 0)
  1558. goto out_resource;
  1559. WRITE32(1);
  1560. }
  1561. if (bmval1 & FATTR4_WORD1_NUMLINKS) {
  1562. if ((buflen -= 4) < 0)
  1563. goto out_resource;
  1564. WRITE32(stat.nlink);
  1565. }
  1566. if (bmval1 & FATTR4_WORD1_OWNER) {
  1567. status = nfsd4_encode_user(rqstp, stat.uid, &p, &buflen);
  1568. if (status == nfserr_resource)
  1569. goto out_resource;
  1570. if (status)
  1571. goto out;
  1572. }
  1573. if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
  1574. status = nfsd4_encode_group(rqstp, stat.gid, &p, &buflen);
  1575. if (status == nfserr_resource)
  1576. goto out_resource;
  1577. if (status)
  1578. goto out;
  1579. }
  1580. if (bmval1 & FATTR4_WORD1_RAWDEV) {
  1581. if ((buflen -= 8) < 0)
  1582. goto out_resource;
  1583. WRITE32((u32) MAJOR(stat.rdev));
  1584. WRITE32((u32) MINOR(stat.rdev));
  1585. }
  1586. if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
  1587. if ((buflen -= 8) < 0)
  1588. goto out_resource;
  1589. dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
  1590. WRITE64(dummy64);
  1591. }
  1592. if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
  1593. if ((buflen -= 8) < 0)
  1594. goto out_resource;
  1595. dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
  1596. WRITE64(dummy64);
  1597. }
  1598. if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
  1599. if ((buflen -= 8) < 0)
  1600. goto out_resource;
  1601. dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
  1602. WRITE64(dummy64);
  1603. }
  1604. if (bmval1 & FATTR4_WORD1_SPACE_USED) {
  1605. if ((buflen -= 8) < 0)
  1606. goto out_resource;
  1607. dummy64 = (u64)stat.blocks << 9;
  1608. WRITE64(dummy64);
  1609. }
  1610. if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
  1611. if ((buflen -= 12) < 0)
  1612. goto out_resource;
  1613. WRITE32(0);
  1614. WRITE32(stat.atime.tv_sec);
  1615. WRITE32(stat.atime.tv_nsec);
  1616. }
  1617. if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
  1618. if ((buflen -= 12) < 0)
  1619. goto out_resource;
  1620. WRITE32(0);
  1621. WRITE32(1);
  1622. WRITE32(0);
  1623. }
  1624. if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
  1625. if ((buflen -= 12) < 0)
  1626. goto out_resource;
  1627. WRITE32(0);
  1628. WRITE32(stat.ctime.tv_sec);
  1629. WRITE32(stat.ctime.tv_nsec);
  1630. }
  1631. if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
  1632. if ((buflen -= 12) < 0)
  1633. goto out_resource;
  1634. WRITE32(0);
  1635. WRITE32(stat.mtime.tv_sec);
  1636. WRITE32(stat.mtime.tv_nsec);
  1637. }
  1638. if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
  1639. struct dentry *mnt_pnt, *mnt_root;
  1640. if ((buflen -= 8) < 0)
  1641. goto out_resource;
  1642. mnt_root = exp->ex_mnt->mnt_root;
  1643. if (mnt_root->d_inode == dentry->d_inode) {
  1644. mnt_pnt = exp->ex_mnt->mnt_mountpoint;
  1645. WRITE64((u64) mnt_pnt->d_inode->i_ino);
  1646. } else
  1647. WRITE64((u64) stat.ino);
  1648. }
  1649. *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
  1650. *countp = p - buffer;
  1651. status = nfs_ok;
  1652. out:
  1653. kfree(acl);
  1654. if (fhp == &tempfh)
  1655. fh_put(&tempfh);
  1656. return status;
  1657. out_nfserr:
  1658. status = nfserrno(err);
  1659. goto out;
  1660. out_resource:
  1661. *countp = 0;
  1662. status = nfserr_resource;
  1663. goto out;
  1664. out_serverfault:
  1665. status = nfserr_serverfault;
  1666. goto out;
  1667. }
  1668. static __be32
  1669. nfsd4_encode_dirent_fattr(struct nfsd4_readdir *cd,
  1670. const char *name, int namlen, __be32 *p, int *buflen)
  1671. {
  1672. struct svc_export *exp = cd->rd_fhp->fh_export;
  1673. struct dentry *dentry;
  1674. __be32 nfserr;
  1675. dentry = lookup_one_len(name, cd->rd_fhp->fh_dentry, namlen);
  1676. if (IS_ERR(dentry))
  1677. return nfserrno(PTR_ERR(dentry));
  1678. exp_get(exp);
  1679. if (d_mountpoint(dentry)) {
  1680. int err;
  1681. err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
  1682. if (err) {
  1683. nfserr = nfserrno(err);
  1684. goto out_put;
  1685. }
  1686. }
  1687. nfserr = nfsd4_encode_fattr(NULL, exp, dentry, p, buflen, cd->rd_bmval,
  1688. cd->rd_rqstp);
  1689. out_put:
  1690. dput(dentry);
  1691. exp_put(exp);
  1692. return nfserr;
  1693. }
  1694. static __be32 *
  1695. nfsd4_encode_rdattr_error(__be32 *p, int buflen, __be32 nfserr)
  1696. {
  1697. __be32 *attrlenp;
  1698. if (buflen < 6)
  1699. return NULL;
  1700. *p++ = htonl(2);
  1701. *p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
  1702. *p++ = htonl(0); /* bmval1 */
  1703. attrlenp = p++;
  1704. *p++ = nfserr; /* no htonl */
  1705. *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
  1706. return p;
  1707. }
  1708. static int
  1709. nfsd4_encode_dirent(void *ccdv, const char *name, int namlen,
  1710. loff_t offset, u64 ino, unsigned int d_type)
  1711. {
  1712. struct readdir_cd *ccd = ccdv;
  1713. struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
  1714. int buflen;
  1715. __be32 *p = cd->buffer;
  1716. __be32 nfserr = nfserr_toosmall;
  1717. /* In nfsv4, "." and ".." never make it onto the wire.. */
  1718. if (name && isdotent(name, namlen)) {
  1719. cd->common.err = nfs_ok;
  1720. return 0;
  1721. }
  1722. if (cd->offset)
  1723. xdr_encode_hyper(cd->offset, (u64) offset);
  1724. buflen = cd->buflen - 4 - XDR_QUADLEN(namlen);
  1725. if (buflen < 0)
  1726. goto fail;
  1727. *p++ = xdr_one; /* mark entry present */
  1728. cd->offset = p; /* remember pointer */
  1729. p = xdr_encode_hyper(p, NFS_OFFSET_MAX); /* offset of next entry */
  1730. p = xdr_encode_array(p, name, namlen); /* name length & name */
  1731. nfserr = nfsd4_encode_dirent_fattr(cd, name, namlen, p, &buflen);
  1732. switch (nfserr) {
  1733. case nfs_ok:
  1734. p += buflen;
  1735. break;
  1736. case nfserr_resource:
  1737. nfserr = nfserr_toosmall;
  1738. goto fail;
  1739. case nfserr_dropit:
  1740. goto fail;
  1741. default:
  1742. /*
  1743. * If the client requested the RDATTR_ERROR attribute,
  1744. * we stuff the error code into this attribute
  1745. * and continue. If this attribute was not requested,
  1746. * then in accordance with the spec, we fail the
  1747. * entire READDIR operation(!)
  1748. */
  1749. if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
  1750. goto fail;
  1751. p = nfsd4_encode_rdattr_error(p, buflen, nfserr);
  1752. if (p == NULL) {
  1753. nfserr = nfserr_toosmall;
  1754. goto fail;
  1755. }
  1756. }
  1757. cd->buflen -= (p - cd->buffer);
  1758. cd->buffer = p;
  1759. cd->common.err = nfs_ok;
  1760. return 0;
  1761. fail:
  1762. cd->common.err = nfserr;
  1763. return -EINVAL;
  1764. }
  1765. static void
  1766. nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_access *access)
  1767. {
  1768. ENCODE_HEAD;
  1769. if (!nfserr) {
  1770. RESERVE_SPACE(8);
  1771. WRITE32(access->ac_supported);
  1772. WRITE32(access->ac_resp_access);
  1773. ADJUST_ARGS();
  1774. }
  1775. }
  1776. static void
  1777. nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_close *close)
  1778. {
  1779. ENCODE_SEQID_OP_HEAD;
  1780. if (!nfserr) {
  1781. RESERVE_SPACE(sizeof(stateid_t));
  1782. WRITE32(close->cl_stateid.si_generation);
  1783. WRITEMEM(&close->cl_stateid.si_opaque, sizeof(stateid_opaque_t));
  1784. ADJUST_ARGS();
  1785. }
  1786. ENCODE_SEQID_OP_TAIL(close->cl_stateowner);
  1787. }
  1788. static void
  1789. nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_commit *commit)
  1790. {
  1791. ENCODE_HEAD;
  1792. if (!nfserr) {
  1793. RESERVE_SPACE(8);
  1794. WRITEMEM(commit->co_verf.data, 8);
  1795. ADJUST_ARGS();
  1796. }
  1797. }
  1798. static void
  1799. nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_create *create)
  1800. {
  1801. ENCODE_HEAD;
  1802. if (!nfserr) {
  1803. RESERVE_SPACE(32);
  1804. WRITECINFO(create->cr_cinfo);
  1805. WRITE32(2);
  1806. WRITE32(create->cr_bmval[0]);
  1807. WRITE32(create->cr_bmval[1]);
  1808. ADJUST_ARGS();
  1809. }
  1810. }
  1811. static __be32
  1812. nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_getattr *getattr)
  1813. {
  1814. struct svc_fh *fhp = getattr->ga_fhp;
  1815. int buflen;
  1816. if (nfserr)
  1817. return nfserr;
  1818. buflen = resp->end - resp->p - (COMPOUND_ERR_SLACK_SPACE >> 2);
  1819. nfserr = nfsd4_encode_fattr(fhp, fhp->fh_export, fhp->fh_dentry,
  1820. resp->p, &buflen, getattr->ga_bmval,
  1821. resp->rqstp);
  1822. if (!nfserr)
  1823. resp->p += buflen;
  1824. return nfserr;
  1825. }
  1826. static void
  1827. nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr, struct svc_fh *fhp)
  1828. {
  1829. unsigned int len;
  1830. ENCODE_HEAD;
  1831. if (!nfserr) {
  1832. len = fhp->fh_handle.fh_size;
  1833. RESERVE_SPACE(len + 4);
  1834. WRITE32(len);
  1835. WRITEMEM(&fhp->fh_handle.fh_base, len);
  1836. ADJUST_ARGS();
  1837. }
  1838. }
  1839. /*
  1840. * Including all fields other than the name, a LOCK4denied structure requires
  1841. * 8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
  1842. */
  1843. static void
  1844. nfsd4_encode_lock_denied(struct nfsd4_compoundres *resp, struct nfsd4_lock_denied *ld)
  1845. {
  1846. ENCODE_HEAD;
  1847. RESERVE_SPACE(32 + XDR_LEN(ld->ld_sop ? ld->ld_sop->so_owner.len : 0));
  1848. WRITE64(ld->ld_start);
  1849. WRITE64(ld->ld_length);
  1850. WRITE32(ld->ld_type);
  1851. if (ld->ld_sop) {
  1852. WRITEMEM(&ld->ld_clientid, 8);
  1853. WRITE32(ld->ld_sop->so_owner.len);
  1854. WRITEMEM(ld->ld_sop->so_owner.data, ld->ld_sop->so_owner.len);
  1855. kref_put(&ld->ld_sop->so_ref, nfs4_free_stateowner);
  1856. } else { /* non - nfsv4 lock in conflict, no clientid nor owner */
  1857. WRITE64((u64)0); /* clientid */
  1858. WRITE32(0); /* length of owner name */
  1859. }
  1860. ADJUST_ARGS();
  1861. }
  1862. static void
  1863. nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lock *lock)
  1864. {
  1865. ENCODE_SEQID_OP_HEAD;
  1866. if (!nfserr) {
  1867. RESERVE_SPACE(4 + sizeof(stateid_t));
  1868. WRITE32(lock->lk_resp_stateid.si_generation);
  1869. WRITEMEM(&lock->lk_resp_stateid.si_opaque, sizeof(stateid_opaque_t));
  1870. ADJUST_ARGS();
  1871. } else if (nfserr == nfserr_denied)
  1872. nfsd4_encode_lock_denied(resp, &lock->lk_denied);
  1873. ENCODE_SEQID_OP_TAIL(lock->lk_replay_owner);
  1874. }
  1875. static void
  1876. nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lockt *lockt)
  1877. {
  1878. if (nfserr == nfserr_denied)
  1879. nfsd4_encode_lock_denied(resp, &lockt->lt_denied);
  1880. }
  1881. static void
  1882. nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_locku *locku)
  1883. {
  1884. ENCODE_SEQID_OP_HEAD;
  1885. if (!nfserr) {
  1886. RESERVE_SPACE(sizeof(stateid_t));
  1887. WRITE32(locku->lu_stateid.si_generation);
  1888. WRITEMEM(&locku->lu_stateid.si_opaque, sizeof(stateid_opaque_t));
  1889. ADJUST_ARGS();
  1890. }
  1891. ENCODE_SEQID_OP_TAIL(locku->lu_stateowner);
  1892. }
  1893. static void
  1894. nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_link *link)
  1895. {
  1896. ENCODE_HEAD;
  1897. if (!nfserr) {
  1898. RESERVE_SPACE(20);
  1899. WRITECINFO(link->li_cinfo);
  1900. ADJUST_ARGS();
  1901. }
  1902. }
  1903. static void
  1904. nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open *open)
  1905. {
  1906. ENCODE_SEQID_OP_HEAD;
  1907. if (nfserr)
  1908. goto out;
  1909. RESERVE_SPACE(36 + sizeof(stateid_t));
  1910. WRITE32(open->op_stateid.si_generation);
  1911. WRITEMEM(&open->op_stateid.si_opaque, sizeof(stateid_opaque_t));
  1912. WRITECINFO(open->op_cinfo);
  1913. WRITE32(open->op_rflags);
  1914. WRITE32(2);
  1915. WRITE32(open->op_bmval[0]);
  1916. WRITE32(open->op_bmval[1]);
  1917. WRITE32(open->op_delegate_type);
  1918. ADJUST_ARGS();
  1919. switch (open->op_delegate_type) {
  1920. case NFS4_OPEN_DELEGATE_NONE:
  1921. break;
  1922. case NFS4_OPEN_DELEGATE_READ:
  1923. RESERVE_SPACE(20 + sizeof(stateid_t));
  1924. WRITEMEM(&open->op_delegate_stateid, sizeof(stateid_t));
  1925. WRITE32(open->op_recall);
  1926. /*
  1927. * TODO: ACE's in delegations
  1928. */
  1929. WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
  1930. WRITE32(0);
  1931. WRITE32(0);
  1932. WRITE32(0); /* XXX: is NULL principal ok? */
  1933. ADJUST_ARGS();
  1934. break;
  1935. case NFS4_OPEN_DELEGATE_WRITE:
  1936. RESERVE_SPACE(32 + sizeof(stateid_t));
  1937. WRITEMEM(&open->op_delegate_stateid, sizeof(stateid_t));
  1938. WRITE32(0);
  1939. /*
  1940. * TODO: space_limit's in delegations
  1941. */
  1942. WRITE32(NFS4_LIMIT_SIZE);
  1943. WRITE32(~(u32)0);
  1944. WRITE32(~(u32)0);
  1945. /*
  1946. * TODO: ACE's in delegations
  1947. */
  1948. WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
  1949. WRITE32(0);
  1950. WRITE32(0);
  1951. WRITE32(0); /* XXX: is NULL principal ok? */
  1952. ADJUST_ARGS();
  1953. break;
  1954. default:
  1955. BUG();
  1956. }
  1957. /* XXX save filehandle here */
  1958. out:
  1959. ENCODE_SEQID_OP_TAIL(open->op_stateowner);
  1960. }
  1961. static void
  1962. nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_confirm *oc)
  1963. {
  1964. ENCODE_SEQID_OP_HEAD;
  1965. if (!nfserr) {
  1966. RESERVE_SPACE(sizeof(stateid_t));
  1967. WRITE32(oc->oc_resp_stateid.si_generation);
  1968. WRITEMEM(&oc->oc_resp_stateid.si_opaque, sizeof(stateid_opaque_t));
  1969. ADJUST_ARGS();
  1970. }
  1971. ENCODE_SEQID_OP_TAIL(oc->oc_stateowner);
  1972. }
  1973. static void
  1974. nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_downgrade *od)
  1975. {
  1976. ENCODE_SEQID_OP_HEAD;
  1977. if (!nfserr) {
  1978. RESERVE_SPACE(sizeof(stateid_t));
  1979. WRITE32(od->od_stateid.si_generation);
  1980. WRITEMEM(&od->od_stateid.si_opaque, sizeof(stateid_opaque_t));
  1981. ADJUST_ARGS();
  1982. }
  1983. ENCODE_SEQID_OP_TAIL(od->od_stateowner);
  1984. }
  1985. static __be32
  1986. nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
  1987. struct nfsd4_read *read)
  1988. {
  1989. u32 eof;
  1990. int v, pn;
  1991. unsigned long maxcount;
  1992. long len;
  1993. ENCODE_HEAD;
  1994. if (nfserr)
  1995. return nfserr;
  1996. if (resp->xbuf->page_len)
  1997. return nfserr_resource;
  1998. RESERVE_SPACE(8); /* eof flag and byte count */
  1999. maxcount = svc_max_payload(resp->rqstp);
  2000. if (maxcount > read->rd_length)
  2001. maxcount = read->rd_length;
  2002. len = maxcount;
  2003. v = 0;
  2004. while (len > 0) {
  2005. pn = resp->rqstp->rq_resused++;
  2006. resp->rqstp->rq_vec[v].iov_base =
  2007. page_address(resp->rqstp->rq_respages[pn]);
  2008. resp->rqstp->rq_vec[v].iov_len =
  2009. len < PAGE_SIZE ? len : PAGE_SIZE;
  2010. v++;
  2011. len -= PAGE_SIZE;
  2012. }
  2013. read->rd_vlen = v;
  2014. nfserr = nfsd_read(read->rd_rqstp, read->rd_fhp, read->rd_filp,
  2015. read->rd_offset, resp->rqstp->rq_vec, read->rd_vlen,
  2016. &maxcount);
  2017. if (nfserr == nfserr_symlink)
  2018. nfserr = nfserr_inval;
  2019. if (nfserr)
  2020. return nfserr;
  2021. eof = (read->rd_offset + maxcount >=
  2022. read->rd_fhp->fh_dentry->d_inode->i_size);
  2023. WRITE32(eof);
  2024. WRITE32(maxcount);
  2025. ADJUST_ARGS();
  2026. resp->xbuf->head[0].iov_len = (char*)p
  2027. - (char*)resp->xbuf->head[0].iov_base;
  2028. resp->xbuf->page_len = maxcount;
  2029. /* Use rest of head for padding and remaining ops: */
  2030. resp->xbuf->tail[0].iov_base = p;
  2031. resp->xbuf->tail[0].iov_len = 0;
  2032. if (maxcount&3) {
  2033. RESERVE_SPACE(4);
  2034. WRITE32(0);
  2035. resp->xbuf->tail[0].iov_base += maxcount&3;
  2036. resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
  2037. ADJUST_ARGS();
  2038. }
  2039. return 0;
  2040. }
  2041. static __be32
  2042. nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readlink *readlink)
  2043. {
  2044. int maxcount;
  2045. char *page;
  2046. ENCODE_HEAD;
  2047. if (nfserr)
  2048. return nfserr;
  2049. if (resp->xbuf->page_len)
  2050. return nfserr_resource;
  2051. page = page_address(resp->rqstp->rq_respages[resp->rqstp->rq_resused++]);
  2052. maxcount = PAGE_SIZE;
  2053. RESERVE_SPACE(4);
  2054. /*
  2055. * XXX: By default, the ->readlink() VFS op will truncate symlinks
  2056. * if they would overflow the buffer. Is this kosher in NFSv4? If
  2057. * not, one easy fix is: if ->readlink() precisely fills the buffer,
  2058. * assume that truncation occurred, and return NFS4ERR_RESOURCE.
  2059. */
  2060. nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp, page, &maxcount);
  2061. if (nfserr == nfserr_isdir)
  2062. return nfserr_inval;
  2063. if (nfserr)
  2064. return nfserr;
  2065. WRITE32(maxcount);
  2066. ADJUST_ARGS();
  2067. resp->xbuf->head[0].iov_len = (char*)p
  2068. - (char*)resp->xbuf->head[0].iov_base;
  2069. resp->xbuf->page_len = maxcount;
  2070. /* Use rest of head for padding and remaining ops: */
  2071. resp->xbuf->tail[0].iov_base = p;
  2072. resp->xbuf->tail[0].iov_len = 0;
  2073. if (maxcount&3) {
  2074. RESERVE_SPACE(4);
  2075. WRITE32(0);
  2076. resp->xbuf->tail[0].iov_base += maxcount&3;
  2077. resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
  2078. ADJUST_ARGS();
  2079. }
  2080. return 0;
  2081. }
  2082. static __be32
  2083. nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readdir *readdir)
  2084. {
  2085. int maxcount;
  2086. loff_t offset;
  2087. __be32 *page, *savep, *tailbase;
  2088. ENCODE_HEAD;
  2089. if (nfserr)
  2090. return nfserr;
  2091. if (resp->xbuf->page_len)
  2092. return nfserr_resource;
  2093. RESERVE_SPACE(8); /* verifier */
  2094. savep = p;
  2095. /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
  2096. WRITE32(0);
  2097. WRITE32(0);
  2098. ADJUST_ARGS();
  2099. resp->xbuf->head[0].iov_len = ((char*)resp->p) - (char*)resp->xbuf->head[0].iov_base;
  2100. tailbase = p;
  2101. maxcount = PAGE_SIZE;
  2102. if (maxcount > readdir->rd_maxcount)
  2103. maxcount = readdir->rd_maxcount;
  2104. /*
  2105. * Convert from bytes to words, account for the two words already
  2106. * written, make sure to leave two words at the end for the next
  2107. * pointer and eof field.
  2108. */
  2109. maxcount = (maxcount >> 2) - 4;
  2110. if (maxcount < 0) {
  2111. nfserr = nfserr_toosmall;
  2112. goto err_no_verf;
  2113. }
  2114. page = page_address(resp->rqstp->rq_respages[resp->rqstp->rq_resused++]);
  2115. readdir->common.err = 0;
  2116. readdir->buflen = maxcount;
  2117. readdir->buffer = page;
  2118. readdir->offset = NULL;
  2119. offset = readdir->rd_cookie;
  2120. nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
  2121. &offset,
  2122. &readdir->common, nfsd4_encode_dirent);
  2123. if (nfserr == nfs_ok &&
  2124. readdir->common.err == nfserr_toosmall &&
  2125. readdir->buffer == page)
  2126. nfserr = nfserr_toosmall;
  2127. if (nfserr == nfserr_symlink)
  2128. nfserr = nfserr_notdir;
  2129. if (nfserr)
  2130. goto err_no_verf;
  2131. if (readdir->offset)
  2132. xdr_encode_hyper(readdir->offset, offset);
  2133. p = readdir->buffer;
  2134. *p++ = 0; /* no more entries */
  2135. *p++ = htonl(readdir->common.err == nfserr_eof);
  2136. resp->xbuf->page_len = ((char*)p) - (char*)page_address(
  2137. resp->rqstp->rq_respages[resp->rqstp->rq_resused-1]);
  2138. /* Use rest of head for padding and remaining ops: */
  2139. resp->xbuf->tail[0].iov_base = tailbase;
  2140. resp->xbuf->tail[0].iov_len = 0;
  2141. resp->p = resp->xbuf->tail[0].iov_base;
  2142. resp->end = resp->p + (PAGE_SIZE - resp->xbuf->head[0].iov_len)/4;
  2143. return 0;
  2144. err_no_verf:
  2145. p = savep;
  2146. ADJUST_ARGS();
  2147. return nfserr;
  2148. }
  2149. static void
  2150. nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_remove *remove)
  2151. {
  2152. ENCODE_HEAD;
  2153. if (!nfserr) {
  2154. RESERVE_SPACE(20);
  2155. WRITECINFO(remove->rm_cinfo);
  2156. ADJUST_ARGS();
  2157. }
  2158. }
  2159. static void
  2160. nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_rename *rename)
  2161. {
  2162. ENCODE_HEAD;
  2163. if (!nfserr) {
  2164. RESERVE_SPACE(40);
  2165. WRITECINFO(rename->rn_sinfo);
  2166. WRITECINFO(rename->rn_tinfo);
  2167. ADJUST_ARGS();
  2168. }
  2169. }
  2170. /*
  2171. * The SETATTR encode routine is special -- it always encodes a bitmap,
  2172. * regardless of the error status.
  2173. */
  2174. static void
  2175. nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setattr *setattr)
  2176. {
  2177. ENCODE_HEAD;
  2178. RESERVE_SPACE(12);
  2179. if (nfserr) {
  2180. WRITE32(2);
  2181. WRITE32(0);
  2182. WRITE32(0);
  2183. }
  2184. else {
  2185. WRITE32(2);
  2186. WRITE32(setattr->sa_bmval[0]);
  2187. WRITE32(setattr->sa_bmval[1]);
  2188. }
  2189. ADJUST_ARGS();
  2190. }
  2191. static void
  2192. nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setclientid *scd)
  2193. {
  2194. ENCODE_HEAD;
  2195. if (!nfserr) {
  2196. RESERVE_SPACE(8 + sizeof(nfs4_verifier));
  2197. WRITEMEM(&scd->se_clientid, 8);
  2198. WRITEMEM(&scd->se_confirm, sizeof(nfs4_verifier));
  2199. ADJUST_ARGS();
  2200. }
  2201. else if (nfserr == nfserr_clid_inuse) {
  2202. RESERVE_SPACE(8);
  2203. WRITE32(0);
  2204. WRITE32(0);
  2205. ADJUST_ARGS();
  2206. }
  2207. }
  2208. static void
  2209. nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_write *write)
  2210. {
  2211. ENCODE_HEAD;
  2212. if (!nfserr) {
  2213. RESERVE_SPACE(16);
  2214. WRITE32(write->wr_bytes_written);
  2215. WRITE32(write->wr_how_written);
  2216. WRITEMEM(write->wr_verifier.data, 8);
  2217. ADJUST_ARGS();
  2218. }
  2219. }
  2220. void
  2221. nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
  2222. {
  2223. __be32 *statp;
  2224. ENCODE_HEAD;
  2225. RESERVE_SPACE(8);
  2226. WRITE32(op->opnum);
  2227. statp = p++; /* to be backfilled at the end */
  2228. ADJUST_ARGS();
  2229. switch (op->opnum) {
  2230. case OP_ACCESS:
  2231. nfsd4_encode_access(resp, op->status, &op->u.access);
  2232. break;
  2233. case OP_CLOSE:
  2234. nfsd4_encode_close(resp, op->status, &op->u.close);
  2235. break;
  2236. case OP_COMMIT:
  2237. nfsd4_encode_commit(resp, op->status, &op->u.commit);
  2238. break;
  2239. case OP_CREATE:
  2240. nfsd4_encode_create(resp, op->status, &op->u.create);
  2241. break;
  2242. case OP_DELEGRETURN:
  2243. break;
  2244. case OP_GETATTR:
  2245. op->status = nfsd4_encode_getattr(resp, op->status, &op->u.getattr);
  2246. break;
  2247. case OP_GETFH:
  2248. nfsd4_encode_getfh(resp, op->status, op->u.getfh);
  2249. break;
  2250. case OP_LINK:
  2251. nfsd4_encode_link(resp, op->status, &op->u.link);
  2252. break;
  2253. case OP_LOCK:
  2254. nfsd4_encode_lock(resp, op->status, &op->u.lock);
  2255. break;
  2256. case OP_LOCKT:
  2257. nfsd4_encode_lockt(resp, op->status, &op->u.lockt);
  2258. break;
  2259. case OP_LOCKU:
  2260. nfsd4_encode_locku(resp, op->status, &op->u.locku);
  2261. break;
  2262. case OP_LOOKUP:
  2263. break;
  2264. case OP_LOOKUPP:
  2265. break;
  2266. case OP_NVERIFY:
  2267. break;
  2268. case OP_OPEN:
  2269. nfsd4_encode_open(resp, op->status, &op->u.open);
  2270. break;
  2271. case OP_OPEN_CONFIRM:
  2272. nfsd4_encode_open_confirm(resp, op->status, &op->u.open_confirm);
  2273. break;
  2274. case OP_OPEN_DOWNGRADE:
  2275. nfsd4_encode_open_downgrade(resp, op->status, &op->u.open_downgrade);
  2276. break;
  2277. case OP_PUTFH:
  2278. break;
  2279. case OP_PUTROOTFH:
  2280. break;
  2281. case OP_READ:
  2282. op->status = nfsd4_encode_read(resp, op->status, &op->u.read);
  2283. break;
  2284. case OP_READDIR:
  2285. op->status = nfsd4_encode_readdir(resp, op->status, &op->u.readdir);
  2286. break;
  2287. case OP_READLINK:
  2288. op->status = nfsd4_encode_readlink(resp, op->status, &op->u.readlink);
  2289. break;
  2290. case OP_REMOVE:
  2291. nfsd4_encode_remove(resp, op->status, &op->u.remove);
  2292. break;
  2293. case OP_RENAME:
  2294. nfsd4_encode_rename(resp, op->status, &op->u.rename);
  2295. break;
  2296. case OP_RENEW:
  2297. break;
  2298. case OP_RESTOREFH:
  2299. break;
  2300. case OP_SAVEFH:
  2301. break;
  2302. case OP_SETATTR:
  2303. nfsd4_encode_setattr(resp, op->status, &op->u.setattr);
  2304. break;
  2305. case OP_SETCLIENTID:
  2306. nfsd4_encode_setclientid(resp, op->status, &op->u.setclientid);
  2307. break;
  2308. case OP_SETCLIENTID_CONFIRM:
  2309. break;
  2310. case OP_VERIFY:
  2311. break;
  2312. case OP_WRITE:
  2313. nfsd4_encode_write(resp, op->status, &op->u.write);
  2314. break;
  2315. case OP_RELEASE_LOCKOWNER:
  2316. break;
  2317. default:
  2318. break;
  2319. }
  2320. /*
  2321. * Note: We write the status directly, instead of using WRITE32(),
  2322. * since it is already in network byte order.
  2323. */
  2324. *statp = op->status;
  2325. }
  2326. /*
  2327. * Encode the reply stored in the stateowner reply cache
  2328. *
  2329. * XDR note: do not encode rp->rp_buflen: the buffer contains the
  2330. * previously sent already encoded operation.
  2331. *
  2332. * called with nfs4_lock_state() held
  2333. */
  2334. void
  2335. nfsd4_encode_replay(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
  2336. {
  2337. ENCODE_HEAD;
  2338. struct nfs4_replay *rp = op->replay;
  2339. BUG_ON(!rp);
  2340. RESERVE_SPACE(8);
  2341. WRITE32(op->opnum);
  2342. *p++ = rp->rp_status; /* already xdr'ed */
  2343. ADJUST_ARGS();
  2344. RESERVE_SPACE(rp->rp_buflen);
  2345. WRITEMEM(rp->rp_buf, rp->rp_buflen);
  2346. ADJUST_ARGS();
  2347. }
  2348. /*
  2349. * END OF "GENERIC" ENCODE ROUTINES.
  2350. */
  2351. int
  2352. nfs4svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
  2353. {
  2354. return xdr_ressize_check(rqstp, p);
  2355. }
  2356. void nfsd4_release_compoundargs(struct nfsd4_compoundargs *args)
  2357. {
  2358. if (args->ops != args->iops) {
  2359. kfree(args->ops);
  2360. args->ops = args->iops;
  2361. }
  2362. kfree(args->tmpp);
  2363. args->tmpp = NULL;
  2364. while (args->to_free) {
  2365. struct tmpbuf *tb = args->to_free;
  2366. args->to_free = tb->next;
  2367. tb->release(tb->buf);
  2368. kfree(tb);
  2369. }
  2370. }
  2371. int
  2372. nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundargs *args)
  2373. {
  2374. __be32 status;
  2375. args->p = p;
  2376. args->end = rqstp->rq_arg.head[0].iov_base + rqstp->rq_arg.head[0].iov_len;
  2377. args->pagelist = rqstp->rq_arg.pages;
  2378. args->pagelen = rqstp->rq_arg.page_len;
  2379. args->tmpp = NULL;
  2380. args->to_free = NULL;
  2381. args->ops = args->iops;
  2382. args->rqstp = rqstp;
  2383. status = nfsd4_decode_compound(args);
  2384. if (status) {
  2385. nfsd4_release_compoundargs(args);
  2386. }
  2387. return !status;
  2388. }
  2389. int
  2390. nfs4svc_encode_compoundres(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundres *resp)
  2391. {
  2392. /*
  2393. * All that remains is to write the tag and operation count...
  2394. */
  2395. struct kvec *iov;
  2396. p = resp->tagp;
  2397. *p++ = htonl(resp->taglen);
  2398. memcpy(p, resp->tag, resp->taglen);
  2399. p += XDR_QUADLEN(resp->taglen);
  2400. *p++ = htonl(resp->opcnt);
  2401. if (rqstp->rq_res.page_len)
  2402. iov = &rqstp->rq_res.tail[0];
  2403. else
  2404. iov = &rqstp->rq_res.head[0];
  2405. iov->iov_len = ((char*)resp->p) - (char*)iov->iov_base;
  2406. BUG_ON(iov->iov_len > PAGE_SIZE);
  2407. return 1;
  2408. }
  2409. /*
  2410. * Local variables:
  2411. * c-basic-offset: 8
  2412. * End:
  2413. */