nfs4xdr.c 84 KB

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