nfs4xdr.c 84 KB

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