nfs4xdr.c 90 KB

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