nfs4xdr.c 114 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535
  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. #include <linux/fs_struct.h>
  36. #include <linux/file.h>
  37. #include <linux/slab.h>
  38. #include <linux/namei.h>
  39. #include <linux/statfs.h>
  40. #include <linux/utsname.h>
  41. #include <linux/pagemap.h>
  42. #include <linux/sunrpc/svcauth_gss.h>
  43. #include "idmap.h"
  44. #include "acl.h"
  45. #include "xdr4.h"
  46. #include "vfs.h"
  47. #include "state.h"
  48. #include "cache.h"
  49. #include "netns.h"
  50. #include "pnfs.h"
  51. #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
  52. #include <linux/security.h>
  53. #endif
  54. #define NFSDDBG_FACILITY NFSDDBG_XDR
  55. const u32 nfsd_suppattrs[3][3] = {
  56. {NFSD4_SUPPORTED_ATTRS_WORD0,
  57. NFSD4_SUPPORTED_ATTRS_WORD1,
  58. NFSD4_SUPPORTED_ATTRS_WORD2},
  59. {NFSD4_1_SUPPORTED_ATTRS_WORD0,
  60. NFSD4_1_SUPPORTED_ATTRS_WORD1,
  61. NFSD4_1_SUPPORTED_ATTRS_WORD2},
  62. {NFSD4_1_SUPPORTED_ATTRS_WORD0,
  63. NFSD4_1_SUPPORTED_ATTRS_WORD1,
  64. NFSD4_2_SUPPORTED_ATTRS_WORD2},
  65. };
  66. /*
  67. * As per referral draft, the fsid for a referral MUST be different from the fsid of the containing
  68. * directory in order to indicate to the client that a filesystem boundary is present
  69. * We use a fixed fsid for a referral
  70. */
  71. #define NFS4_REFERRAL_FSID_MAJOR 0x8000000ULL
  72. #define NFS4_REFERRAL_FSID_MINOR 0x8000000ULL
  73. static __be32
  74. check_filename(char *str, int len)
  75. {
  76. int i;
  77. if (len == 0)
  78. return nfserr_inval;
  79. if (isdotent(str, len))
  80. return nfserr_badname;
  81. for (i = 0; i < len; i++)
  82. if (str[i] == '/')
  83. return nfserr_badname;
  84. return 0;
  85. }
  86. #define DECODE_HEAD \
  87. __be32 *p; \
  88. __be32 status
  89. #define DECODE_TAIL \
  90. status = 0; \
  91. out: \
  92. return status; \
  93. xdr_error: \
  94. dprintk("NFSD: xdr error (%s:%d)\n", \
  95. __FILE__, __LINE__); \
  96. status = nfserr_bad_xdr; \
  97. goto out
  98. #define READMEM(x,nbytes) do { \
  99. x = (char *)p; \
  100. p += XDR_QUADLEN(nbytes); \
  101. } while (0)
  102. #define SAVEMEM(x,nbytes) do { \
  103. if (!(x = (p==argp->tmp || p == argp->tmpp) ? \
  104. savemem(argp, p, nbytes) : \
  105. (char *)p)) { \
  106. dprintk("NFSD: xdr error (%s:%d)\n", \
  107. __FILE__, __LINE__); \
  108. goto xdr_error; \
  109. } \
  110. p += XDR_QUADLEN(nbytes); \
  111. } while (0)
  112. #define COPYMEM(x,nbytes) do { \
  113. memcpy((x), p, nbytes); \
  114. p += XDR_QUADLEN(nbytes); \
  115. } while (0)
  116. /* READ_BUF, read_buf(): nbytes must be <= PAGE_SIZE */
  117. #define READ_BUF(nbytes) do { \
  118. if (nbytes <= (u32)((char *)argp->end - (char *)argp->p)) { \
  119. p = argp->p; \
  120. argp->p += XDR_QUADLEN(nbytes); \
  121. } else if (!(p = read_buf(argp, nbytes))) { \
  122. dprintk("NFSD: xdr error (%s:%d)\n", \
  123. __FILE__, __LINE__); \
  124. goto xdr_error; \
  125. } \
  126. } while (0)
  127. static void next_decode_page(struct nfsd4_compoundargs *argp)
  128. {
  129. argp->p = page_address(argp->pagelist[0]);
  130. argp->pagelist++;
  131. if (argp->pagelen < PAGE_SIZE) {
  132. argp->end = argp->p + XDR_QUADLEN(argp->pagelen);
  133. argp->pagelen = 0;
  134. } else {
  135. argp->end = argp->p + (PAGE_SIZE>>2);
  136. argp->pagelen -= PAGE_SIZE;
  137. }
  138. }
  139. static __be32 *read_buf(struct nfsd4_compoundargs *argp, u32 nbytes)
  140. {
  141. /* We want more bytes than seem to be available.
  142. * Maybe we need a new page, maybe we have just run out
  143. */
  144. unsigned int avail = (char *)argp->end - (char *)argp->p;
  145. __be32 *p;
  146. if (argp->pagelen == 0) {
  147. struct kvec *vec = &argp->rqstp->rq_arg.tail[0];
  148. if (!argp->tail) {
  149. argp->tail = true;
  150. avail = vec->iov_len;
  151. argp->p = vec->iov_base;
  152. argp->end = vec->iov_base + avail;
  153. }
  154. if (avail < nbytes)
  155. return NULL;
  156. p = argp->p;
  157. argp->p += XDR_QUADLEN(nbytes);
  158. return p;
  159. }
  160. if (avail + argp->pagelen < nbytes)
  161. return NULL;
  162. if (avail + PAGE_SIZE < nbytes) /* need more than a page !! */
  163. return NULL;
  164. /* ok, we can do it with the current plus the next page */
  165. if (nbytes <= sizeof(argp->tmp))
  166. p = argp->tmp;
  167. else {
  168. kfree(argp->tmpp);
  169. p = argp->tmpp = kmalloc(nbytes, GFP_KERNEL);
  170. if (!p)
  171. return NULL;
  172. }
  173. /*
  174. * The following memcpy is safe because read_buf is always
  175. * called with nbytes > avail, and the two cases above both
  176. * guarantee p points to at least nbytes bytes.
  177. */
  178. memcpy(p, argp->p, avail);
  179. next_decode_page(argp);
  180. memcpy(((char*)p)+avail, argp->p, (nbytes - avail));
  181. argp->p += XDR_QUADLEN(nbytes - avail);
  182. return p;
  183. }
  184. static int zero_clientid(clientid_t *clid)
  185. {
  186. return (clid->cl_boot == 0) && (clid->cl_id == 0);
  187. }
  188. /**
  189. * svcxdr_tmpalloc - allocate memory to be freed after compound processing
  190. * @argp: NFSv4 compound argument structure
  191. * @p: pointer to be freed (with kfree())
  192. *
  193. * Marks @p to be freed when processing the compound operation
  194. * described in @argp finishes.
  195. */
  196. static void *
  197. svcxdr_tmpalloc(struct nfsd4_compoundargs *argp, u32 len)
  198. {
  199. struct svcxdr_tmpbuf *tb;
  200. tb = kmalloc(sizeof(*tb) + len, GFP_KERNEL);
  201. if (!tb)
  202. return NULL;
  203. tb->next = argp->to_free;
  204. argp->to_free = tb;
  205. return tb->buf;
  206. }
  207. /*
  208. * For xdr strings that need to be passed to other kernel api's
  209. * as null-terminated strings.
  210. *
  211. * Note null-terminating in place usually isn't safe since the
  212. * buffer might end on a page boundary.
  213. */
  214. static char *
  215. svcxdr_dupstr(struct nfsd4_compoundargs *argp, void *buf, u32 len)
  216. {
  217. char *p = svcxdr_tmpalloc(argp, len + 1);
  218. if (!p)
  219. return NULL;
  220. memcpy(p, buf, len);
  221. p[len] = '\0';
  222. return p;
  223. }
  224. /**
  225. * savemem - duplicate a chunk of memory for later processing
  226. * @argp: NFSv4 compound argument structure to be freed with
  227. * @p: pointer to be duplicated
  228. * @nbytes: length to be duplicated
  229. *
  230. * Returns a pointer to a copy of @nbytes bytes of memory at @p
  231. * that are preserved until processing of the NFSv4 compound
  232. * operation described by @argp finishes.
  233. */
  234. static char *savemem(struct nfsd4_compoundargs *argp, __be32 *p, int nbytes)
  235. {
  236. void *ret;
  237. ret = svcxdr_tmpalloc(argp, nbytes);
  238. if (!ret)
  239. return NULL;
  240. memcpy(ret, p, nbytes);
  241. return ret;
  242. }
  243. /*
  244. * We require the high 32 bits of 'seconds' to be 0, and
  245. * we ignore all 32 bits of 'nseconds'.
  246. */
  247. static __be32
  248. nfsd4_decode_time(struct nfsd4_compoundargs *argp, struct timespec *tv)
  249. {
  250. DECODE_HEAD;
  251. u64 sec;
  252. READ_BUF(12);
  253. p = xdr_decode_hyper(p, &sec);
  254. tv->tv_sec = sec;
  255. tv->tv_nsec = be32_to_cpup(p++);
  256. if (tv->tv_nsec >= (u32)1000000000)
  257. return nfserr_inval;
  258. DECODE_TAIL;
  259. }
  260. static __be32
  261. nfsd4_decode_bitmap(struct nfsd4_compoundargs *argp, u32 *bmval)
  262. {
  263. u32 bmlen;
  264. DECODE_HEAD;
  265. bmval[0] = 0;
  266. bmval[1] = 0;
  267. bmval[2] = 0;
  268. READ_BUF(4);
  269. bmlen = be32_to_cpup(p++);
  270. if (bmlen > 1000)
  271. goto xdr_error;
  272. READ_BUF(bmlen << 2);
  273. if (bmlen > 0)
  274. bmval[0] = be32_to_cpup(p++);
  275. if (bmlen > 1)
  276. bmval[1] = be32_to_cpup(p++);
  277. if (bmlen > 2)
  278. bmval[2] = be32_to_cpup(p++);
  279. DECODE_TAIL;
  280. }
  281. static __be32
  282. nfsd4_decode_fattr(struct nfsd4_compoundargs *argp, u32 *bmval,
  283. struct iattr *iattr, struct nfs4_acl **acl,
  284. struct xdr_netobj *label, int *umask)
  285. {
  286. int expected_len, len = 0;
  287. u32 dummy32;
  288. char *buf;
  289. DECODE_HEAD;
  290. iattr->ia_valid = 0;
  291. if ((status = nfsd4_decode_bitmap(argp, bmval)))
  292. return status;
  293. if (bmval[0] & ~NFSD_WRITEABLE_ATTRS_WORD0
  294. || bmval[1] & ~NFSD_WRITEABLE_ATTRS_WORD1
  295. || bmval[2] & ~NFSD_WRITEABLE_ATTRS_WORD2) {
  296. if (nfsd_attrs_supported(argp->minorversion, bmval))
  297. return nfserr_inval;
  298. return nfserr_attrnotsupp;
  299. }
  300. READ_BUF(4);
  301. expected_len = be32_to_cpup(p++);
  302. if (bmval[0] & FATTR4_WORD0_SIZE) {
  303. READ_BUF(8);
  304. len += 8;
  305. p = xdr_decode_hyper(p, &iattr->ia_size);
  306. iattr->ia_valid |= ATTR_SIZE;
  307. }
  308. if (bmval[0] & FATTR4_WORD0_ACL) {
  309. u32 nace;
  310. struct nfs4_ace *ace;
  311. READ_BUF(4); len += 4;
  312. nace = be32_to_cpup(p++);
  313. if (nace > NFS4_ACL_MAX)
  314. return nfserr_fbig;
  315. *acl = svcxdr_tmpalloc(argp, nfs4_acl_bytes(nace));
  316. if (*acl == NULL)
  317. return nfserr_jukebox;
  318. (*acl)->naces = nace;
  319. for (ace = (*acl)->aces; ace < (*acl)->aces + nace; ace++) {
  320. READ_BUF(16); len += 16;
  321. ace->type = be32_to_cpup(p++);
  322. ace->flag = be32_to_cpup(p++);
  323. ace->access_mask = be32_to_cpup(p++);
  324. dummy32 = be32_to_cpup(p++);
  325. READ_BUF(dummy32);
  326. len += XDR_QUADLEN(dummy32) << 2;
  327. READMEM(buf, dummy32);
  328. ace->whotype = nfs4_acl_get_whotype(buf, dummy32);
  329. status = nfs_ok;
  330. if (ace->whotype != NFS4_ACL_WHO_NAMED)
  331. ;
  332. else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
  333. status = nfsd_map_name_to_gid(argp->rqstp,
  334. buf, dummy32, &ace->who_gid);
  335. else
  336. status = nfsd_map_name_to_uid(argp->rqstp,
  337. buf, dummy32, &ace->who_uid);
  338. if (status)
  339. return status;
  340. }
  341. } else
  342. *acl = NULL;
  343. if (bmval[1] & FATTR4_WORD1_MODE) {
  344. READ_BUF(4);
  345. len += 4;
  346. iattr->ia_mode = be32_to_cpup(p++);
  347. iattr->ia_mode &= (S_IFMT | S_IALLUGO);
  348. iattr->ia_valid |= ATTR_MODE;
  349. }
  350. if (bmval[1] & FATTR4_WORD1_OWNER) {
  351. READ_BUF(4);
  352. len += 4;
  353. dummy32 = be32_to_cpup(p++);
  354. READ_BUF(dummy32);
  355. len += (XDR_QUADLEN(dummy32) << 2);
  356. READMEM(buf, dummy32);
  357. if ((status = nfsd_map_name_to_uid(argp->rqstp, buf, dummy32, &iattr->ia_uid)))
  358. return status;
  359. iattr->ia_valid |= ATTR_UID;
  360. }
  361. if (bmval[1] & FATTR4_WORD1_OWNER_GROUP) {
  362. READ_BUF(4);
  363. len += 4;
  364. dummy32 = be32_to_cpup(p++);
  365. READ_BUF(dummy32);
  366. len += (XDR_QUADLEN(dummy32) << 2);
  367. READMEM(buf, dummy32);
  368. if ((status = nfsd_map_name_to_gid(argp->rqstp, buf, dummy32, &iattr->ia_gid)))
  369. return status;
  370. iattr->ia_valid |= ATTR_GID;
  371. }
  372. if (bmval[1] & FATTR4_WORD1_TIME_ACCESS_SET) {
  373. READ_BUF(4);
  374. len += 4;
  375. dummy32 = be32_to_cpup(p++);
  376. switch (dummy32) {
  377. case NFS4_SET_TO_CLIENT_TIME:
  378. len += 12;
  379. status = nfsd4_decode_time(argp, &iattr->ia_atime);
  380. if (status)
  381. return status;
  382. iattr->ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
  383. break;
  384. case NFS4_SET_TO_SERVER_TIME:
  385. iattr->ia_valid |= ATTR_ATIME;
  386. break;
  387. default:
  388. goto xdr_error;
  389. }
  390. }
  391. if (bmval[1] & FATTR4_WORD1_TIME_MODIFY_SET) {
  392. READ_BUF(4);
  393. len += 4;
  394. dummy32 = be32_to_cpup(p++);
  395. switch (dummy32) {
  396. case NFS4_SET_TO_CLIENT_TIME:
  397. len += 12;
  398. status = nfsd4_decode_time(argp, &iattr->ia_mtime);
  399. if (status)
  400. return status;
  401. iattr->ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
  402. break;
  403. case NFS4_SET_TO_SERVER_TIME:
  404. iattr->ia_valid |= ATTR_MTIME;
  405. break;
  406. default:
  407. goto xdr_error;
  408. }
  409. }
  410. label->len = 0;
  411. #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
  412. if (bmval[2] & FATTR4_WORD2_SECURITY_LABEL) {
  413. READ_BUF(4);
  414. len += 4;
  415. dummy32 = be32_to_cpup(p++); /* lfs: we don't use it */
  416. READ_BUF(4);
  417. len += 4;
  418. dummy32 = be32_to_cpup(p++); /* pi: we don't use it either */
  419. READ_BUF(4);
  420. len += 4;
  421. dummy32 = be32_to_cpup(p++);
  422. READ_BUF(dummy32);
  423. if (dummy32 > NFS4_MAXLABELLEN)
  424. return nfserr_badlabel;
  425. len += (XDR_QUADLEN(dummy32) << 2);
  426. READMEM(buf, dummy32);
  427. label->len = dummy32;
  428. label->data = svcxdr_dupstr(argp, buf, dummy32);
  429. if (!label->data)
  430. return nfserr_jukebox;
  431. }
  432. #endif
  433. if (bmval[2] & FATTR4_WORD2_MODE_UMASK) {
  434. if (!umask)
  435. goto xdr_error;
  436. READ_BUF(8);
  437. len += 8;
  438. dummy32 = be32_to_cpup(p++);
  439. iattr->ia_mode = dummy32 & (S_IFMT | S_IALLUGO);
  440. dummy32 = be32_to_cpup(p++);
  441. *umask = dummy32 & S_IRWXUGO;
  442. iattr->ia_valid |= ATTR_MODE;
  443. }
  444. if (len != expected_len)
  445. goto xdr_error;
  446. DECODE_TAIL;
  447. }
  448. static __be32
  449. nfsd4_decode_stateid(struct nfsd4_compoundargs *argp, stateid_t *sid)
  450. {
  451. DECODE_HEAD;
  452. READ_BUF(sizeof(stateid_t));
  453. sid->si_generation = be32_to_cpup(p++);
  454. COPYMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
  455. DECODE_TAIL;
  456. }
  457. static __be32
  458. nfsd4_decode_access(struct nfsd4_compoundargs *argp, struct nfsd4_access *access)
  459. {
  460. DECODE_HEAD;
  461. READ_BUF(4);
  462. access->ac_req_access = be32_to_cpup(p++);
  463. DECODE_TAIL;
  464. }
  465. static __be32 nfsd4_decode_cb_sec(struct nfsd4_compoundargs *argp, struct nfsd4_cb_sec *cbs)
  466. {
  467. DECODE_HEAD;
  468. u32 dummy, uid, gid;
  469. char *machine_name;
  470. int i;
  471. int nr_secflavs;
  472. /* callback_sec_params4 */
  473. READ_BUF(4);
  474. nr_secflavs = be32_to_cpup(p++);
  475. if (nr_secflavs)
  476. cbs->flavor = (u32)(-1);
  477. else
  478. /* Is this legal? Be generous, take it to mean AUTH_NONE: */
  479. cbs->flavor = 0;
  480. for (i = 0; i < nr_secflavs; ++i) {
  481. READ_BUF(4);
  482. dummy = be32_to_cpup(p++);
  483. switch (dummy) {
  484. case RPC_AUTH_NULL:
  485. /* Nothing to read */
  486. if (cbs->flavor == (u32)(-1))
  487. cbs->flavor = RPC_AUTH_NULL;
  488. break;
  489. case RPC_AUTH_UNIX:
  490. READ_BUF(8);
  491. /* stamp */
  492. dummy = be32_to_cpup(p++);
  493. /* machine name */
  494. dummy = be32_to_cpup(p++);
  495. READ_BUF(dummy);
  496. SAVEMEM(machine_name, dummy);
  497. /* uid, gid */
  498. READ_BUF(8);
  499. uid = be32_to_cpup(p++);
  500. gid = be32_to_cpup(p++);
  501. /* more gids */
  502. READ_BUF(4);
  503. dummy = be32_to_cpup(p++);
  504. READ_BUF(dummy * 4);
  505. if (cbs->flavor == (u32)(-1)) {
  506. kuid_t kuid = make_kuid(&init_user_ns, uid);
  507. kgid_t kgid = make_kgid(&init_user_ns, gid);
  508. if (uid_valid(kuid) && gid_valid(kgid)) {
  509. cbs->uid = kuid;
  510. cbs->gid = kgid;
  511. cbs->flavor = RPC_AUTH_UNIX;
  512. } else {
  513. dprintk("RPC_AUTH_UNIX with invalid"
  514. "uid or gid ignoring!\n");
  515. }
  516. }
  517. break;
  518. case RPC_AUTH_GSS:
  519. dprintk("RPC_AUTH_GSS callback secflavor "
  520. "not supported!\n");
  521. READ_BUF(8);
  522. /* gcbp_service */
  523. dummy = be32_to_cpup(p++);
  524. /* gcbp_handle_from_server */
  525. dummy = be32_to_cpup(p++);
  526. READ_BUF(dummy);
  527. p += XDR_QUADLEN(dummy);
  528. /* gcbp_handle_from_client */
  529. READ_BUF(4);
  530. dummy = be32_to_cpup(p++);
  531. READ_BUF(dummy);
  532. break;
  533. default:
  534. dprintk("Illegal callback secflavor\n");
  535. return nfserr_inval;
  536. }
  537. }
  538. DECODE_TAIL;
  539. }
  540. static __be32 nfsd4_decode_backchannel_ctl(struct nfsd4_compoundargs *argp, struct nfsd4_backchannel_ctl *bc)
  541. {
  542. DECODE_HEAD;
  543. READ_BUF(4);
  544. bc->bc_cb_program = be32_to_cpup(p++);
  545. nfsd4_decode_cb_sec(argp, &bc->bc_cb_sec);
  546. DECODE_TAIL;
  547. }
  548. static __be32 nfsd4_decode_bind_conn_to_session(struct nfsd4_compoundargs *argp, struct nfsd4_bind_conn_to_session *bcts)
  549. {
  550. DECODE_HEAD;
  551. READ_BUF(NFS4_MAX_SESSIONID_LEN + 8);
  552. COPYMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
  553. bcts->dir = be32_to_cpup(p++);
  554. /* XXX: skipping ctsa_use_conn_in_rdma_mode. Perhaps Tom Tucker
  555. * could help us figure out we should be using it. */
  556. DECODE_TAIL;
  557. }
  558. static __be32
  559. nfsd4_decode_close(struct nfsd4_compoundargs *argp, struct nfsd4_close *close)
  560. {
  561. DECODE_HEAD;
  562. READ_BUF(4);
  563. close->cl_seqid = be32_to_cpup(p++);
  564. return nfsd4_decode_stateid(argp, &close->cl_stateid);
  565. DECODE_TAIL;
  566. }
  567. static __be32
  568. nfsd4_decode_commit(struct nfsd4_compoundargs *argp, struct nfsd4_commit *commit)
  569. {
  570. DECODE_HEAD;
  571. READ_BUF(12);
  572. p = xdr_decode_hyper(p, &commit->co_offset);
  573. commit->co_count = be32_to_cpup(p++);
  574. DECODE_TAIL;
  575. }
  576. static __be32
  577. nfsd4_decode_create(struct nfsd4_compoundargs *argp, struct nfsd4_create *create)
  578. {
  579. DECODE_HEAD;
  580. READ_BUF(4);
  581. create->cr_type = be32_to_cpup(p++);
  582. switch (create->cr_type) {
  583. case NF4LNK:
  584. READ_BUF(4);
  585. create->cr_datalen = be32_to_cpup(p++);
  586. READ_BUF(create->cr_datalen);
  587. create->cr_data = svcxdr_dupstr(argp, p, create->cr_datalen);
  588. if (!create->cr_data)
  589. return nfserr_jukebox;
  590. break;
  591. case NF4BLK:
  592. case NF4CHR:
  593. READ_BUF(8);
  594. create->cr_specdata1 = be32_to_cpup(p++);
  595. create->cr_specdata2 = be32_to_cpup(p++);
  596. break;
  597. case NF4SOCK:
  598. case NF4FIFO:
  599. case NF4DIR:
  600. default:
  601. break;
  602. }
  603. READ_BUF(4);
  604. create->cr_namelen = be32_to_cpup(p++);
  605. READ_BUF(create->cr_namelen);
  606. SAVEMEM(create->cr_name, create->cr_namelen);
  607. if ((status = check_filename(create->cr_name, create->cr_namelen)))
  608. return status;
  609. status = nfsd4_decode_fattr(argp, create->cr_bmval, &create->cr_iattr,
  610. &create->cr_acl, &create->cr_label,
  611. &current->fs->umask);
  612. if (status)
  613. goto out;
  614. DECODE_TAIL;
  615. }
  616. static inline __be32
  617. nfsd4_decode_delegreturn(struct nfsd4_compoundargs *argp, struct nfsd4_delegreturn *dr)
  618. {
  619. return nfsd4_decode_stateid(argp, &dr->dr_stateid);
  620. }
  621. static inline __be32
  622. nfsd4_decode_getattr(struct nfsd4_compoundargs *argp, struct nfsd4_getattr *getattr)
  623. {
  624. return nfsd4_decode_bitmap(argp, getattr->ga_bmval);
  625. }
  626. static __be32
  627. nfsd4_decode_link(struct nfsd4_compoundargs *argp, struct nfsd4_link *link)
  628. {
  629. DECODE_HEAD;
  630. READ_BUF(4);
  631. link->li_namelen = be32_to_cpup(p++);
  632. READ_BUF(link->li_namelen);
  633. SAVEMEM(link->li_name, link->li_namelen);
  634. if ((status = check_filename(link->li_name, link->li_namelen)))
  635. return status;
  636. DECODE_TAIL;
  637. }
  638. static __be32
  639. nfsd4_decode_lock(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
  640. {
  641. DECODE_HEAD;
  642. /*
  643. * type, reclaim(boolean), offset, length, new_lock_owner(boolean)
  644. */
  645. READ_BUF(28);
  646. lock->lk_type = be32_to_cpup(p++);
  647. if ((lock->lk_type < NFS4_READ_LT) || (lock->lk_type > NFS4_WRITEW_LT))
  648. goto xdr_error;
  649. lock->lk_reclaim = be32_to_cpup(p++);
  650. p = xdr_decode_hyper(p, &lock->lk_offset);
  651. p = xdr_decode_hyper(p, &lock->lk_length);
  652. lock->lk_is_new = be32_to_cpup(p++);
  653. if (lock->lk_is_new) {
  654. READ_BUF(4);
  655. lock->lk_new_open_seqid = be32_to_cpup(p++);
  656. status = nfsd4_decode_stateid(argp, &lock->lk_new_open_stateid);
  657. if (status)
  658. return status;
  659. READ_BUF(8 + sizeof(clientid_t));
  660. lock->lk_new_lock_seqid = be32_to_cpup(p++);
  661. COPYMEM(&lock->lk_new_clientid, sizeof(clientid_t));
  662. lock->lk_new_owner.len = be32_to_cpup(p++);
  663. READ_BUF(lock->lk_new_owner.len);
  664. READMEM(lock->lk_new_owner.data, lock->lk_new_owner.len);
  665. } else {
  666. status = nfsd4_decode_stateid(argp, &lock->lk_old_lock_stateid);
  667. if (status)
  668. return status;
  669. READ_BUF(4);
  670. lock->lk_old_lock_seqid = be32_to_cpup(p++);
  671. }
  672. DECODE_TAIL;
  673. }
  674. static __be32
  675. nfsd4_decode_lockt(struct nfsd4_compoundargs *argp, struct nfsd4_lockt *lockt)
  676. {
  677. DECODE_HEAD;
  678. READ_BUF(32);
  679. lockt->lt_type = be32_to_cpup(p++);
  680. if((lockt->lt_type < NFS4_READ_LT) || (lockt->lt_type > NFS4_WRITEW_LT))
  681. goto xdr_error;
  682. p = xdr_decode_hyper(p, &lockt->lt_offset);
  683. p = xdr_decode_hyper(p, &lockt->lt_length);
  684. COPYMEM(&lockt->lt_clientid, 8);
  685. lockt->lt_owner.len = be32_to_cpup(p++);
  686. READ_BUF(lockt->lt_owner.len);
  687. READMEM(lockt->lt_owner.data, lockt->lt_owner.len);
  688. DECODE_TAIL;
  689. }
  690. static __be32
  691. nfsd4_decode_locku(struct nfsd4_compoundargs *argp, struct nfsd4_locku *locku)
  692. {
  693. DECODE_HEAD;
  694. READ_BUF(8);
  695. locku->lu_type = be32_to_cpup(p++);
  696. if ((locku->lu_type < NFS4_READ_LT) || (locku->lu_type > NFS4_WRITEW_LT))
  697. goto xdr_error;
  698. locku->lu_seqid = be32_to_cpup(p++);
  699. status = nfsd4_decode_stateid(argp, &locku->lu_stateid);
  700. if (status)
  701. return status;
  702. READ_BUF(16);
  703. p = xdr_decode_hyper(p, &locku->lu_offset);
  704. p = xdr_decode_hyper(p, &locku->lu_length);
  705. DECODE_TAIL;
  706. }
  707. static __be32
  708. nfsd4_decode_lookup(struct nfsd4_compoundargs *argp, struct nfsd4_lookup *lookup)
  709. {
  710. DECODE_HEAD;
  711. READ_BUF(4);
  712. lookup->lo_len = be32_to_cpup(p++);
  713. READ_BUF(lookup->lo_len);
  714. SAVEMEM(lookup->lo_name, lookup->lo_len);
  715. if ((status = check_filename(lookup->lo_name, lookup->lo_len)))
  716. return status;
  717. DECODE_TAIL;
  718. }
  719. static __be32 nfsd4_decode_share_access(struct nfsd4_compoundargs *argp, u32 *share_access, u32 *deleg_want, u32 *deleg_when)
  720. {
  721. __be32 *p;
  722. u32 w;
  723. READ_BUF(4);
  724. w = be32_to_cpup(p++);
  725. *share_access = w & NFS4_SHARE_ACCESS_MASK;
  726. *deleg_want = w & NFS4_SHARE_WANT_MASK;
  727. if (deleg_when)
  728. *deleg_when = w & NFS4_SHARE_WHEN_MASK;
  729. switch (w & NFS4_SHARE_ACCESS_MASK) {
  730. case NFS4_SHARE_ACCESS_READ:
  731. case NFS4_SHARE_ACCESS_WRITE:
  732. case NFS4_SHARE_ACCESS_BOTH:
  733. break;
  734. default:
  735. return nfserr_bad_xdr;
  736. }
  737. w &= ~NFS4_SHARE_ACCESS_MASK;
  738. if (!w)
  739. return nfs_ok;
  740. if (!argp->minorversion)
  741. return nfserr_bad_xdr;
  742. switch (w & NFS4_SHARE_WANT_MASK) {
  743. case NFS4_SHARE_WANT_NO_PREFERENCE:
  744. case NFS4_SHARE_WANT_READ_DELEG:
  745. case NFS4_SHARE_WANT_WRITE_DELEG:
  746. case NFS4_SHARE_WANT_ANY_DELEG:
  747. case NFS4_SHARE_WANT_NO_DELEG:
  748. case NFS4_SHARE_WANT_CANCEL:
  749. break;
  750. default:
  751. return nfserr_bad_xdr;
  752. }
  753. w &= ~NFS4_SHARE_WANT_MASK;
  754. if (!w)
  755. return nfs_ok;
  756. if (!deleg_when) /* open_downgrade */
  757. return nfserr_inval;
  758. switch (w) {
  759. case NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL:
  760. case NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED:
  761. case (NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL |
  762. NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED):
  763. return nfs_ok;
  764. }
  765. xdr_error:
  766. return nfserr_bad_xdr;
  767. }
  768. static __be32 nfsd4_decode_share_deny(struct nfsd4_compoundargs *argp, u32 *x)
  769. {
  770. __be32 *p;
  771. READ_BUF(4);
  772. *x = be32_to_cpup(p++);
  773. /* Note: unlinke access bits, deny bits may be zero. */
  774. if (*x & ~NFS4_SHARE_DENY_BOTH)
  775. return nfserr_bad_xdr;
  776. return nfs_ok;
  777. xdr_error:
  778. return nfserr_bad_xdr;
  779. }
  780. static __be32 nfsd4_decode_opaque(struct nfsd4_compoundargs *argp, struct xdr_netobj *o)
  781. {
  782. __be32 *p;
  783. READ_BUF(4);
  784. o->len = be32_to_cpup(p++);
  785. if (o->len == 0 || o->len > NFS4_OPAQUE_LIMIT)
  786. return nfserr_bad_xdr;
  787. READ_BUF(o->len);
  788. SAVEMEM(o->data, o->len);
  789. return nfs_ok;
  790. xdr_error:
  791. return nfserr_bad_xdr;
  792. }
  793. static __be32
  794. nfsd4_decode_open(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
  795. {
  796. DECODE_HEAD;
  797. u32 dummy;
  798. memset(open->op_bmval, 0, sizeof(open->op_bmval));
  799. open->op_iattr.ia_valid = 0;
  800. open->op_openowner = NULL;
  801. open->op_xdr_error = 0;
  802. /* seqid, share_access, share_deny, clientid, ownerlen */
  803. READ_BUF(4);
  804. open->op_seqid = be32_to_cpup(p++);
  805. /* decode, yet ignore deleg_when until supported */
  806. status = nfsd4_decode_share_access(argp, &open->op_share_access,
  807. &open->op_deleg_want, &dummy);
  808. if (status)
  809. goto xdr_error;
  810. status = nfsd4_decode_share_deny(argp, &open->op_share_deny);
  811. if (status)
  812. goto xdr_error;
  813. READ_BUF(sizeof(clientid_t));
  814. COPYMEM(&open->op_clientid, sizeof(clientid_t));
  815. status = nfsd4_decode_opaque(argp, &open->op_owner);
  816. if (status)
  817. goto xdr_error;
  818. READ_BUF(4);
  819. open->op_create = be32_to_cpup(p++);
  820. switch (open->op_create) {
  821. case NFS4_OPEN_NOCREATE:
  822. break;
  823. case NFS4_OPEN_CREATE:
  824. current->fs->umask = 0;
  825. READ_BUF(4);
  826. open->op_createmode = be32_to_cpup(p++);
  827. switch (open->op_createmode) {
  828. case NFS4_CREATE_UNCHECKED:
  829. case NFS4_CREATE_GUARDED:
  830. status = nfsd4_decode_fattr(argp, open->op_bmval,
  831. &open->op_iattr, &open->op_acl, &open->op_label,
  832. &current->fs->umask);
  833. if (status)
  834. goto out;
  835. break;
  836. case NFS4_CREATE_EXCLUSIVE:
  837. READ_BUF(NFS4_VERIFIER_SIZE);
  838. COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
  839. break;
  840. case NFS4_CREATE_EXCLUSIVE4_1:
  841. if (argp->minorversion < 1)
  842. goto xdr_error;
  843. READ_BUF(NFS4_VERIFIER_SIZE);
  844. COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
  845. status = nfsd4_decode_fattr(argp, open->op_bmval,
  846. &open->op_iattr, &open->op_acl, &open->op_label,
  847. &current->fs->umask);
  848. if (status)
  849. goto out;
  850. break;
  851. default:
  852. goto xdr_error;
  853. }
  854. break;
  855. default:
  856. goto xdr_error;
  857. }
  858. /* open_claim */
  859. READ_BUF(4);
  860. open->op_claim_type = be32_to_cpup(p++);
  861. switch (open->op_claim_type) {
  862. case NFS4_OPEN_CLAIM_NULL:
  863. case NFS4_OPEN_CLAIM_DELEGATE_PREV:
  864. READ_BUF(4);
  865. open->op_fname.len = be32_to_cpup(p++);
  866. READ_BUF(open->op_fname.len);
  867. SAVEMEM(open->op_fname.data, open->op_fname.len);
  868. if ((status = check_filename(open->op_fname.data, open->op_fname.len)))
  869. return status;
  870. break;
  871. case NFS4_OPEN_CLAIM_PREVIOUS:
  872. READ_BUF(4);
  873. open->op_delegate_type = be32_to_cpup(p++);
  874. break;
  875. case NFS4_OPEN_CLAIM_DELEGATE_CUR:
  876. status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
  877. if (status)
  878. return status;
  879. READ_BUF(4);
  880. open->op_fname.len = be32_to_cpup(p++);
  881. READ_BUF(open->op_fname.len);
  882. SAVEMEM(open->op_fname.data, open->op_fname.len);
  883. if ((status = check_filename(open->op_fname.data, open->op_fname.len)))
  884. return status;
  885. break;
  886. case NFS4_OPEN_CLAIM_FH:
  887. case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
  888. if (argp->minorversion < 1)
  889. goto xdr_error;
  890. /* void */
  891. break;
  892. case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
  893. if (argp->minorversion < 1)
  894. goto xdr_error;
  895. status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
  896. if (status)
  897. return status;
  898. break;
  899. default:
  900. goto xdr_error;
  901. }
  902. DECODE_TAIL;
  903. }
  904. static __be32
  905. nfsd4_decode_open_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_open_confirm *open_conf)
  906. {
  907. DECODE_HEAD;
  908. if (argp->minorversion >= 1)
  909. return nfserr_notsupp;
  910. status = nfsd4_decode_stateid(argp, &open_conf->oc_req_stateid);
  911. if (status)
  912. return status;
  913. READ_BUF(4);
  914. open_conf->oc_seqid = be32_to_cpup(p++);
  915. DECODE_TAIL;
  916. }
  917. static __be32
  918. nfsd4_decode_open_downgrade(struct nfsd4_compoundargs *argp, struct nfsd4_open_downgrade *open_down)
  919. {
  920. DECODE_HEAD;
  921. status = nfsd4_decode_stateid(argp, &open_down->od_stateid);
  922. if (status)
  923. return status;
  924. READ_BUF(4);
  925. open_down->od_seqid = be32_to_cpup(p++);
  926. status = nfsd4_decode_share_access(argp, &open_down->od_share_access,
  927. &open_down->od_deleg_want, NULL);
  928. if (status)
  929. return status;
  930. status = nfsd4_decode_share_deny(argp, &open_down->od_share_deny);
  931. if (status)
  932. return status;
  933. DECODE_TAIL;
  934. }
  935. static __be32
  936. nfsd4_decode_putfh(struct nfsd4_compoundargs *argp, struct nfsd4_putfh *putfh)
  937. {
  938. DECODE_HEAD;
  939. READ_BUF(4);
  940. putfh->pf_fhlen = be32_to_cpup(p++);
  941. if (putfh->pf_fhlen > NFS4_FHSIZE)
  942. goto xdr_error;
  943. READ_BUF(putfh->pf_fhlen);
  944. SAVEMEM(putfh->pf_fhval, putfh->pf_fhlen);
  945. DECODE_TAIL;
  946. }
  947. static __be32
  948. nfsd4_decode_putpubfh(struct nfsd4_compoundargs *argp, void *p)
  949. {
  950. if (argp->minorversion == 0)
  951. return nfs_ok;
  952. return nfserr_notsupp;
  953. }
  954. static __be32
  955. nfsd4_decode_read(struct nfsd4_compoundargs *argp, struct nfsd4_read *read)
  956. {
  957. DECODE_HEAD;
  958. status = nfsd4_decode_stateid(argp, &read->rd_stateid);
  959. if (status)
  960. return status;
  961. READ_BUF(12);
  962. p = xdr_decode_hyper(p, &read->rd_offset);
  963. read->rd_length = be32_to_cpup(p++);
  964. DECODE_TAIL;
  965. }
  966. static __be32
  967. nfsd4_decode_readdir(struct nfsd4_compoundargs *argp, struct nfsd4_readdir *readdir)
  968. {
  969. DECODE_HEAD;
  970. READ_BUF(24);
  971. p = xdr_decode_hyper(p, &readdir->rd_cookie);
  972. COPYMEM(readdir->rd_verf.data, sizeof(readdir->rd_verf.data));
  973. readdir->rd_dircount = be32_to_cpup(p++);
  974. readdir->rd_maxcount = be32_to_cpup(p++);
  975. if ((status = nfsd4_decode_bitmap(argp, readdir->rd_bmval)))
  976. goto out;
  977. DECODE_TAIL;
  978. }
  979. static __be32
  980. nfsd4_decode_remove(struct nfsd4_compoundargs *argp, struct nfsd4_remove *remove)
  981. {
  982. DECODE_HEAD;
  983. READ_BUF(4);
  984. remove->rm_namelen = be32_to_cpup(p++);
  985. READ_BUF(remove->rm_namelen);
  986. SAVEMEM(remove->rm_name, remove->rm_namelen);
  987. if ((status = check_filename(remove->rm_name, remove->rm_namelen)))
  988. return status;
  989. DECODE_TAIL;
  990. }
  991. static __be32
  992. nfsd4_decode_rename(struct nfsd4_compoundargs *argp, struct nfsd4_rename *rename)
  993. {
  994. DECODE_HEAD;
  995. READ_BUF(4);
  996. rename->rn_snamelen = be32_to_cpup(p++);
  997. READ_BUF(rename->rn_snamelen);
  998. SAVEMEM(rename->rn_sname, rename->rn_snamelen);
  999. READ_BUF(4);
  1000. rename->rn_tnamelen = be32_to_cpup(p++);
  1001. READ_BUF(rename->rn_tnamelen);
  1002. SAVEMEM(rename->rn_tname, rename->rn_tnamelen);
  1003. if ((status = check_filename(rename->rn_sname, rename->rn_snamelen)))
  1004. return status;
  1005. if ((status = check_filename(rename->rn_tname, rename->rn_tnamelen)))
  1006. return status;
  1007. DECODE_TAIL;
  1008. }
  1009. static __be32
  1010. nfsd4_decode_renew(struct nfsd4_compoundargs *argp, clientid_t *clientid)
  1011. {
  1012. DECODE_HEAD;
  1013. if (argp->minorversion >= 1)
  1014. return nfserr_notsupp;
  1015. READ_BUF(sizeof(clientid_t));
  1016. COPYMEM(clientid, sizeof(clientid_t));
  1017. DECODE_TAIL;
  1018. }
  1019. static __be32
  1020. nfsd4_decode_secinfo(struct nfsd4_compoundargs *argp,
  1021. struct nfsd4_secinfo *secinfo)
  1022. {
  1023. DECODE_HEAD;
  1024. READ_BUF(4);
  1025. secinfo->si_namelen = be32_to_cpup(p++);
  1026. READ_BUF(secinfo->si_namelen);
  1027. SAVEMEM(secinfo->si_name, secinfo->si_namelen);
  1028. status = check_filename(secinfo->si_name, secinfo->si_namelen);
  1029. if (status)
  1030. return status;
  1031. DECODE_TAIL;
  1032. }
  1033. static __be32
  1034. nfsd4_decode_secinfo_no_name(struct nfsd4_compoundargs *argp,
  1035. struct nfsd4_secinfo_no_name *sin)
  1036. {
  1037. DECODE_HEAD;
  1038. READ_BUF(4);
  1039. sin->sin_style = be32_to_cpup(p++);
  1040. DECODE_TAIL;
  1041. }
  1042. static __be32
  1043. nfsd4_decode_setattr(struct nfsd4_compoundargs *argp, struct nfsd4_setattr *setattr)
  1044. {
  1045. __be32 status;
  1046. status = nfsd4_decode_stateid(argp, &setattr->sa_stateid);
  1047. if (status)
  1048. return status;
  1049. return nfsd4_decode_fattr(argp, setattr->sa_bmval, &setattr->sa_iattr,
  1050. &setattr->sa_acl, &setattr->sa_label, NULL);
  1051. }
  1052. static __be32
  1053. nfsd4_decode_setclientid(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid *setclientid)
  1054. {
  1055. DECODE_HEAD;
  1056. if (argp->minorversion >= 1)
  1057. return nfserr_notsupp;
  1058. READ_BUF(NFS4_VERIFIER_SIZE);
  1059. COPYMEM(setclientid->se_verf.data, NFS4_VERIFIER_SIZE);
  1060. status = nfsd4_decode_opaque(argp, &setclientid->se_name);
  1061. if (status)
  1062. return nfserr_bad_xdr;
  1063. READ_BUF(8);
  1064. setclientid->se_callback_prog = be32_to_cpup(p++);
  1065. setclientid->se_callback_netid_len = be32_to_cpup(p++);
  1066. READ_BUF(setclientid->se_callback_netid_len);
  1067. SAVEMEM(setclientid->se_callback_netid_val, setclientid->se_callback_netid_len);
  1068. READ_BUF(4);
  1069. setclientid->se_callback_addr_len = be32_to_cpup(p++);
  1070. READ_BUF(setclientid->se_callback_addr_len);
  1071. SAVEMEM(setclientid->se_callback_addr_val, setclientid->se_callback_addr_len);
  1072. READ_BUF(4);
  1073. setclientid->se_callback_ident = be32_to_cpup(p++);
  1074. DECODE_TAIL;
  1075. }
  1076. static __be32
  1077. nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid_confirm *scd_c)
  1078. {
  1079. DECODE_HEAD;
  1080. if (argp->minorversion >= 1)
  1081. return nfserr_notsupp;
  1082. READ_BUF(8 + NFS4_VERIFIER_SIZE);
  1083. COPYMEM(&scd_c->sc_clientid, 8);
  1084. COPYMEM(&scd_c->sc_confirm, NFS4_VERIFIER_SIZE);
  1085. DECODE_TAIL;
  1086. }
  1087. /* Also used for NVERIFY */
  1088. static __be32
  1089. nfsd4_decode_verify(struct nfsd4_compoundargs *argp, struct nfsd4_verify *verify)
  1090. {
  1091. DECODE_HEAD;
  1092. if ((status = nfsd4_decode_bitmap(argp, verify->ve_bmval)))
  1093. goto out;
  1094. /* For convenience's sake, we compare raw xdr'd attributes in
  1095. * nfsd4_proc_verify */
  1096. READ_BUF(4);
  1097. verify->ve_attrlen = be32_to_cpup(p++);
  1098. READ_BUF(verify->ve_attrlen);
  1099. SAVEMEM(verify->ve_attrval, verify->ve_attrlen);
  1100. DECODE_TAIL;
  1101. }
  1102. static __be32
  1103. nfsd4_decode_write(struct nfsd4_compoundargs *argp, struct nfsd4_write *write)
  1104. {
  1105. int avail;
  1106. int len;
  1107. DECODE_HEAD;
  1108. status = nfsd4_decode_stateid(argp, &write->wr_stateid);
  1109. if (status)
  1110. return status;
  1111. READ_BUF(16);
  1112. p = xdr_decode_hyper(p, &write->wr_offset);
  1113. write->wr_stable_how = be32_to_cpup(p++);
  1114. if (write->wr_stable_how > NFS_FILE_SYNC)
  1115. goto xdr_error;
  1116. write->wr_buflen = be32_to_cpup(p++);
  1117. /* Sorry .. no magic macros for this.. *
  1118. * READ_BUF(write->wr_buflen);
  1119. * SAVEMEM(write->wr_buf, write->wr_buflen);
  1120. */
  1121. avail = (char*)argp->end - (char*)argp->p;
  1122. if (avail + argp->pagelen < write->wr_buflen) {
  1123. dprintk("NFSD: xdr error (%s:%d)\n",
  1124. __FILE__, __LINE__);
  1125. goto xdr_error;
  1126. }
  1127. write->wr_head.iov_base = p;
  1128. write->wr_head.iov_len = avail;
  1129. write->wr_pagelist = argp->pagelist;
  1130. len = XDR_QUADLEN(write->wr_buflen) << 2;
  1131. if (len >= avail) {
  1132. int pages;
  1133. len -= avail;
  1134. pages = len >> PAGE_SHIFT;
  1135. argp->pagelist += pages;
  1136. argp->pagelen -= pages * PAGE_SIZE;
  1137. len -= pages * PAGE_SIZE;
  1138. next_decode_page(argp);
  1139. }
  1140. argp->p += XDR_QUADLEN(len);
  1141. DECODE_TAIL;
  1142. }
  1143. static __be32
  1144. nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp, struct nfsd4_release_lockowner *rlockowner)
  1145. {
  1146. DECODE_HEAD;
  1147. if (argp->minorversion >= 1)
  1148. return nfserr_notsupp;
  1149. READ_BUF(12);
  1150. COPYMEM(&rlockowner->rl_clientid, sizeof(clientid_t));
  1151. rlockowner->rl_owner.len = be32_to_cpup(p++);
  1152. READ_BUF(rlockowner->rl_owner.len);
  1153. READMEM(rlockowner->rl_owner.data, rlockowner->rl_owner.len);
  1154. if (argp->minorversion && !zero_clientid(&rlockowner->rl_clientid))
  1155. return nfserr_inval;
  1156. DECODE_TAIL;
  1157. }
  1158. static __be32
  1159. nfsd4_decode_exchange_id(struct nfsd4_compoundargs *argp,
  1160. struct nfsd4_exchange_id *exid)
  1161. {
  1162. int dummy, tmp;
  1163. DECODE_HEAD;
  1164. READ_BUF(NFS4_VERIFIER_SIZE);
  1165. COPYMEM(exid->verifier.data, NFS4_VERIFIER_SIZE);
  1166. status = nfsd4_decode_opaque(argp, &exid->clname);
  1167. if (status)
  1168. return nfserr_bad_xdr;
  1169. READ_BUF(4);
  1170. exid->flags = be32_to_cpup(p++);
  1171. /* Ignore state_protect4_a */
  1172. READ_BUF(4);
  1173. exid->spa_how = be32_to_cpup(p++);
  1174. switch (exid->spa_how) {
  1175. case SP4_NONE:
  1176. break;
  1177. case SP4_MACH_CRED:
  1178. /* spo_must_enforce */
  1179. status = nfsd4_decode_bitmap(argp,
  1180. exid->spo_must_enforce);
  1181. if (status)
  1182. goto out;
  1183. /* spo_must_allow */
  1184. status = nfsd4_decode_bitmap(argp, exid->spo_must_allow);
  1185. if (status)
  1186. goto out;
  1187. break;
  1188. case SP4_SSV:
  1189. /* ssp_ops */
  1190. READ_BUF(4);
  1191. dummy = be32_to_cpup(p++);
  1192. READ_BUF(dummy * 4);
  1193. p += dummy;
  1194. READ_BUF(4);
  1195. dummy = be32_to_cpup(p++);
  1196. READ_BUF(dummy * 4);
  1197. p += dummy;
  1198. /* ssp_hash_algs<> */
  1199. READ_BUF(4);
  1200. tmp = be32_to_cpup(p++);
  1201. while (tmp--) {
  1202. READ_BUF(4);
  1203. dummy = be32_to_cpup(p++);
  1204. READ_BUF(dummy);
  1205. p += XDR_QUADLEN(dummy);
  1206. }
  1207. /* ssp_encr_algs<> */
  1208. READ_BUF(4);
  1209. tmp = be32_to_cpup(p++);
  1210. while (tmp--) {
  1211. READ_BUF(4);
  1212. dummy = be32_to_cpup(p++);
  1213. READ_BUF(dummy);
  1214. p += XDR_QUADLEN(dummy);
  1215. }
  1216. /* ssp_window and ssp_num_gss_handles */
  1217. READ_BUF(8);
  1218. dummy = be32_to_cpup(p++);
  1219. dummy = be32_to_cpup(p++);
  1220. break;
  1221. default:
  1222. goto xdr_error;
  1223. }
  1224. /* Ignore Implementation ID */
  1225. READ_BUF(4); /* nfs_impl_id4 array length */
  1226. dummy = be32_to_cpup(p++);
  1227. if (dummy > 1)
  1228. goto xdr_error;
  1229. if (dummy == 1) {
  1230. /* nii_domain */
  1231. READ_BUF(4);
  1232. dummy = be32_to_cpup(p++);
  1233. READ_BUF(dummy);
  1234. p += XDR_QUADLEN(dummy);
  1235. /* nii_name */
  1236. READ_BUF(4);
  1237. dummy = be32_to_cpup(p++);
  1238. READ_BUF(dummy);
  1239. p += XDR_QUADLEN(dummy);
  1240. /* nii_date */
  1241. READ_BUF(12);
  1242. p += 3;
  1243. }
  1244. DECODE_TAIL;
  1245. }
  1246. static __be32
  1247. nfsd4_decode_create_session(struct nfsd4_compoundargs *argp,
  1248. struct nfsd4_create_session *sess)
  1249. {
  1250. DECODE_HEAD;
  1251. u32 dummy;
  1252. READ_BUF(16);
  1253. COPYMEM(&sess->clientid, 8);
  1254. sess->seqid = be32_to_cpup(p++);
  1255. sess->flags = be32_to_cpup(p++);
  1256. /* Fore channel attrs */
  1257. READ_BUF(28);
  1258. dummy = be32_to_cpup(p++); /* headerpadsz is always 0 */
  1259. sess->fore_channel.maxreq_sz = be32_to_cpup(p++);
  1260. sess->fore_channel.maxresp_sz = be32_to_cpup(p++);
  1261. sess->fore_channel.maxresp_cached = be32_to_cpup(p++);
  1262. sess->fore_channel.maxops = be32_to_cpup(p++);
  1263. sess->fore_channel.maxreqs = be32_to_cpup(p++);
  1264. sess->fore_channel.nr_rdma_attrs = be32_to_cpup(p++);
  1265. if (sess->fore_channel.nr_rdma_attrs == 1) {
  1266. READ_BUF(4);
  1267. sess->fore_channel.rdma_attrs = be32_to_cpup(p++);
  1268. } else if (sess->fore_channel.nr_rdma_attrs > 1) {
  1269. dprintk("Too many fore channel attr bitmaps!\n");
  1270. goto xdr_error;
  1271. }
  1272. /* Back channel attrs */
  1273. READ_BUF(28);
  1274. dummy = be32_to_cpup(p++); /* headerpadsz is always 0 */
  1275. sess->back_channel.maxreq_sz = be32_to_cpup(p++);
  1276. sess->back_channel.maxresp_sz = be32_to_cpup(p++);
  1277. sess->back_channel.maxresp_cached = be32_to_cpup(p++);
  1278. sess->back_channel.maxops = be32_to_cpup(p++);
  1279. sess->back_channel.maxreqs = be32_to_cpup(p++);
  1280. sess->back_channel.nr_rdma_attrs = be32_to_cpup(p++);
  1281. if (sess->back_channel.nr_rdma_attrs == 1) {
  1282. READ_BUF(4);
  1283. sess->back_channel.rdma_attrs = be32_to_cpup(p++);
  1284. } else if (sess->back_channel.nr_rdma_attrs > 1) {
  1285. dprintk("Too many back channel attr bitmaps!\n");
  1286. goto xdr_error;
  1287. }
  1288. READ_BUF(4);
  1289. sess->callback_prog = be32_to_cpup(p++);
  1290. nfsd4_decode_cb_sec(argp, &sess->cb_sec);
  1291. DECODE_TAIL;
  1292. }
  1293. static __be32
  1294. nfsd4_decode_destroy_session(struct nfsd4_compoundargs *argp,
  1295. struct nfsd4_destroy_session *destroy_session)
  1296. {
  1297. DECODE_HEAD;
  1298. READ_BUF(NFS4_MAX_SESSIONID_LEN);
  1299. COPYMEM(destroy_session->sessionid.data, NFS4_MAX_SESSIONID_LEN);
  1300. DECODE_TAIL;
  1301. }
  1302. static __be32
  1303. nfsd4_decode_free_stateid(struct nfsd4_compoundargs *argp,
  1304. struct nfsd4_free_stateid *free_stateid)
  1305. {
  1306. DECODE_HEAD;
  1307. READ_BUF(sizeof(stateid_t));
  1308. free_stateid->fr_stateid.si_generation = be32_to_cpup(p++);
  1309. COPYMEM(&free_stateid->fr_stateid.si_opaque, sizeof(stateid_opaque_t));
  1310. DECODE_TAIL;
  1311. }
  1312. static __be32
  1313. nfsd4_decode_sequence(struct nfsd4_compoundargs *argp,
  1314. struct nfsd4_sequence *seq)
  1315. {
  1316. DECODE_HEAD;
  1317. READ_BUF(NFS4_MAX_SESSIONID_LEN + 16);
  1318. COPYMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
  1319. seq->seqid = be32_to_cpup(p++);
  1320. seq->slotid = be32_to_cpup(p++);
  1321. seq->maxslots = be32_to_cpup(p++);
  1322. seq->cachethis = be32_to_cpup(p++);
  1323. DECODE_TAIL;
  1324. }
  1325. static __be32
  1326. nfsd4_decode_test_stateid(struct nfsd4_compoundargs *argp, struct nfsd4_test_stateid *test_stateid)
  1327. {
  1328. int i;
  1329. __be32 *p, status;
  1330. struct nfsd4_test_stateid_id *stateid;
  1331. READ_BUF(4);
  1332. test_stateid->ts_num_ids = ntohl(*p++);
  1333. INIT_LIST_HEAD(&test_stateid->ts_stateid_list);
  1334. for (i = 0; i < test_stateid->ts_num_ids; i++) {
  1335. stateid = svcxdr_tmpalloc(argp, sizeof(*stateid));
  1336. if (!stateid) {
  1337. status = nfserrno(-ENOMEM);
  1338. goto out;
  1339. }
  1340. INIT_LIST_HEAD(&stateid->ts_id_list);
  1341. list_add_tail(&stateid->ts_id_list, &test_stateid->ts_stateid_list);
  1342. status = nfsd4_decode_stateid(argp, &stateid->ts_id_stateid);
  1343. if (status)
  1344. goto out;
  1345. }
  1346. status = 0;
  1347. out:
  1348. return status;
  1349. xdr_error:
  1350. dprintk("NFSD: xdr error (%s:%d)\n", __FILE__, __LINE__);
  1351. status = nfserr_bad_xdr;
  1352. goto out;
  1353. }
  1354. static __be32 nfsd4_decode_destroy_clientid(struct nfsd4_compoundargs *argp, struct nfsd4_destroy_clientid *dc)
  1355. {
  1356. DECODE_HEAD;
  1357. READ_BUF(8);
  1358. COPYMEM(&dc->clientid, 8);
  1359. DECODE_TAIL;
  1360. }
  1361. static __be32 nfsd4_decode_reclaim_complete(struct nfsd4_compoundargs *argp, struct nfsd4_reclaim_complete *rc)
  1362. {
  1363. DECODE_HEAD;
  1364. READ_BUF(4);
  1365. rc->rca_one_fs = be32_to_cpup(p++);
  1366. DECODE_TAIL;
  1367. }
  1368. #ifdef CONFIG_NFSD_PNFS
  1369. static __be32
  1370. nfsd4_decode_getdeviceinfo(struct nfsd4_compoundargs *argp,
  1371. struct nfsd4_getdeviceinfo *gdev)
  1372. {
  1373. DECODE_HEAD;
  1374. u32 num, i;
  1375. READ_BUF(sizeof(struct nfsd4_deviceid) + 3 * 4);
  1376. COPYMEM(&gdev->gd_devid, sizeof(struct nfsd4_deviceid));
  1377. gdev->gd_layout_type = be32_to_cpup(p++);
  1378. gdev->gd_maxcount = be32_to_cpup(p++);
  1379. num = be32_to_cpup(p++);
  1380. if (num) {
  1381. READ_BUF(4 * num);
  1382. gdev->gd_notify_types = be32_to_cpup(p++);
  1383. for (i = 1; i < num; i++) {
  1384. if (be32_to_cpup(p++)) {
  1385. status = nfserr_inval;
  1386. goto out;
  1387. }
  1388. }
  1389. }
  1390. DECODE_TAIL;
  1391. }
  1392. static __be32
  1393. nfsd4_decode_layoutget(struct nfsd4_compoundargs *argp,
  1394. struct nfsd4_layoutget *lgp)
  1395. {
  1396. DECODE_HEAD;
  1397. READ_BUF(36);
  1398. lgp->lg_signal = be32_to_cpup(p++);
  1399. lgp->lg_layout_type = be32_to_cpup(p++);
  1400. lgp->lg_seg.iomode = be32_to_cpup(p++);
  1401. p = xdr_decode_hyper(p, &lgp->lg_seg.offset);
  1402. p = xdr_decode_hyper(p, &lgp->lg_seg.length);
  1403. p = xdr_decode_hyper(p, &lgp->lg_minlength);
  1404. status = nfsd4_decode_stateid(argp, &lgp->lg_sid);
  1405. if (status)
  1406. return status;
  1407. READ_BUF(4);
  1408. lgp->lg_maxcount = be32_to_cpup(p++);
  1409. DECODE_TAIL;
  1410. }
  1411. static __be32
  1412. nfsd4_decode_layoutcommit(struct nfsd4_compoundargs *argp,
  1413. struct nfsd4_layoutcommit *lcp)
  1414. {
  1415. DECODE_HEAD;
  1416. u32 timechange;
  1417. READ_BUF(20);
  1418. p = xdr_decode_hyper(p, &lcp->lc_seg.offset);
  1419. p = xdr_decode_hyper(p, &lcp->lc_seg.length);
  1420. lcp->lc_reclaim = be32_to_cpup(p++);
  1421. status = nfsd4_decode_stateid(argp, &lcp->lc_sid);
  1422. if (status)
  1423. return status;
  1424. READ_BUF(4);
  1425. lcp->lc_newoffset = be32_to_cpup(p++);
  1426. if (lcp->lc_newoffset) {
  1427. READ_BUF(8);
  1428. p = xdr_decode_hyper(p, &lcp->lc_last_wr);
  1429. } else
  1430. lcp->lc_last_wr = 0;
  1431. READ_BUF(4);
  1432. timechange = be32_to_cpup(p++);
  1433. if (timechange) {
  1434. status = nfsd4_decode_time(argp, &lcp->lc_mtime);
  1435. if (status)
  1436. return status;
  1437. } else {
  1438. lcp->lc_mtime.tv_nsec = UTIME_NOW;
  1439. }
  1440. READ_BUF(8);
  1441. lcp->lc_layout_type = be32_to_cpup(p++);
  1442. /*
  1443. * Save the layout update in XDR format and let the layout driver deal
  1444. * with it later.
  1445. */
  1446. lcp->lc_up_len = be32_to_cpup(p++);
  1447. if (lcp->lc_up_len > 0) {
  1448. READ_BUF(lcp->lc_up_len);
  1449. READMEM(lcp->lc_up_layout, lcp->lc_up_len);
  1450. }
  1451. DECODE_TAIL;
  1452. }
  1453. static __be32
  1454. nfsd4_decode_layoutreturn(struct nfsd4_compoundargs *argp,
  1455. struct nfsd4_layoutreturn *lrp)
  1456. {
  1457. DECODE_HEAD;
  1458. READ_BUF(16);
  1459. lrp->lr_reclaim = be32_to_cpup(p++);
  1460. lrp->lr_layout_type = be32_to_cpup(p++);
  1461. lrp->lr_seg.iomode = be32_to_cpup(p++);
  1462. lrp->lr_return_type = be32_to_cpup(p++);
  1463. if (lrp->lr_return_type == RETURN_FILE) {
  1464. READ_BUF(16);
  1465. p = xdr_decode_hyper(p, &lrp->lr_seg.offset);
  1466. p = xdr_decode_hyper(p, &lrp->lr_seg.length);
  1467. status = nfsd4_decode_stateid(argp, &lrp->lr_sid);
  1468. if (status)
  1469. return status;
  1470. READ_BUF(4);
  1471. lrp->lrf_body_len = be32_to_cpup(p++);
  1472. if (lrp->lrf_body_len > 0) {
  1473. READ_BUF(lrp->lrf_body_len);
  1474. READMEM(lrp->lrf_body, lrp->lrf_body_len);
  1475. }
  1476. } else {
  1477. lrp->lr_seg.offset = 0;
  1478. lrp->lr_seg.length = NFS4_MAX_UINT64;
  1479. }
  1480. DECODE_TAIL;
  1481. }
  1482. #endif /* CONFIG_NFSD_PNFS */
  1483. static __be32
  1484. nfsd4_decode_fallocate(struct nfsd4_compoundargs *argp,
  1485. struct nfsd4_fallocate *fallocate)
  1486. {
  1487. DECODE_HEAD;
  1488. status = nfsd4_decode_stateid(argp, &fallocate->falloc_stateid);
  1489. if (status)
  1490. return status;
  1491. READ_BUF(16);
  1492. p = xdr_decode_hyper(p, &fallocate->falloc_offset);
  1493. xdr_decode_hyper(p, &fallocate->falloc_length);
  1494. DECODE_TAIL;
  1495. }
  1496. static __be32
  1497. nfsd4_decode_clone(struct nfsd4_compoundargs *argp, struct nfsd4_clone *clone)
  1498. {
  1499. DECODE_HEAD;
  1500. status = nfsd4_decode_stateid(argp, &clone->cl_src_stateid);
  1501. if (status)
  1502. return status;
  1503. status = nfsd4_decode_stateid(argp, &clone->cl_dst_stateid);
  1504. if (status)
  1505. return status;
  1506. READ_BUF(8 + 8 + 8);
  1507. p = xdr_decode_hyper(p, &clone->cl_src_pos);
  1508. p = xdr_decode_hyper(p, &clone->cl_dst_pos);
  1509. p = xdr_decode_hyper(p, &clone->cl_count);
  1510. DECODE_TAIL;
  1511. }
  1512. static __be32
  1513. nfsd4_decode_copy(struct nfsd4_compoundargs *argp, struct nfsd4_copy *copy)
  1514. {
  1515. DECODE_HEAD;
  1516. unsigned int tmp;
  1517. status = nfsd4_decode_stateid(argp, &copy->cp_src_stateid);
  1518. if (status)
  1519. return status;
  1520. status = nfsd4_decode_stateid(argp, &copy->cp_dst_stateid);
  1521. if (status)
  1522. return status;
  1523. READ_BUF(8 + 8 + 8 + 4 + 4 + 4);
  1524. p = xdr_decode_hyper(p, &copy->cp_src_pos);
  1525. p = xdr_decode_hyper(p, &copy->cp_dst_pos);
  1526. p = xdr_decode_hyper(p, &copy->cp_count);
  1527. copy->cp_consecutive = be32_to_cpup(p++);
  1528. copy->cp_synchronous = be32_to_cpup(p++);
  1529. tmp = be32_to_cpup(p); /* Source server list not supported */
  1530. DECODE_TAIL;
  1531. }
  1532. static __be32
  1533. nfsd4_decode_seek(struct nfsd4_compoundargs *argp, struct nfsd4_seek *seek)
  1534. {
  1535. DECODE_HEAD;
  1536. status = nfsd4_decode_stateid(argp, &seek->seek_stateid);
  1537. if (status)
  1538. return status;
  1539. READ_BUF(8 + 4);
  1540. p = xdr_decode_hyper(p, &seek->seek_offset);
  1541. seek->seek_whence = be32_to_cpup(p);
  1542. DECODE_TAIL;
  1543. }
  1544. static __be32
  1545. nfsd4_decode_noop(struct nfsd4_compoundargs *argp, void *p)
  1546. {
  1547. return nfs_ok;
  1548. }
  1549. static __be32
  1550. nfsd4_decode_notsupp(struct nfsd4_compoundargs *argp, void *p)
  1551. {
  1552. return nfserr_notsupp;
  1553. }
  1554. typedef __be32(*nfsd4_dec)(struct nfsd4_compoundargs *argp, void *);
  1555. static const nfsd4_dec nfsd4_dec_ops[] = {
  1556. [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
  1557. [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
  1558. [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
  1559. [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
  1560. [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
  1561. [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
  1562. [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
  1563. [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
  1564. [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
  1565. [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
  1566. [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
  1567. [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
  1568. [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
  1569. [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
  1570. [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
  1571. [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
  1572. [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
  1573. [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_open_confirm,
  1574. [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
  1575. [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
  1576. [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_putpubfh,
  1577. [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
  1578. [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
  1579. [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
  1580. [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
  1581. [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
  1582. [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
  1583. [OP_RENEW] = (nfsd4_dec)nfsd4_decode_renew,
  1584. [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
  1585. [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
  1586. [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
  1587. [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
  1588. [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_setclientid,
  1589. [OP_SETCLIENTID_CONFIRM] = (nfsd4_dec)nfsd4_decode_setclientid_confirm,
  1590. [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
  1591. [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
  1592. [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_release_lockowner,
  1593. /* new operations for NFSv4.1 */
  1594. [OP_BACKCHANNEL_CTL] = (nfsd4_dec)nfsd4_decode_backchannel_ctl,
  1595. [OP_BIND_CONN_TO_SESSION]= (nfsd4_dec)nfsd4_decode_bind_conn_to_session,
  1596. [OP_EXCHANGE_ID] = (nfsd4_dec)nfsd4_decode_exchange_id,
  1597. [OP_CREATE_SESSION] = (nfsd4_dec)nfsd4_decode_create_session,
  1598. [OP_DESTROY_SESSION] = (nfsd4_dec)nfsd4_decode_destroy_session,
  1599. [OP_FREE_STATEID] = (nfsd4_dec)nfsd4_decode_free_stateid,
  1600. [OP_GET_DIR_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
  1601. #ifdef CONFIG_NFSD_PNFS
  1602. [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_getdeviceinfo,
  1603. [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
  1604. [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_layoutcommit,
  1605. [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_layoutget,
  1606. [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_layoutreturn,
  1607. #else
  1608. [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_notsupp,
  1609. [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
  1610. [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_notsupp,
  1611. [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_notsupp,
  1612. [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_notsupp,
  1613. #endif
  1614. [OP_SECINFO_NO_NAME] = (nfsd4_dec)nfsd4_decode_secinfo_no_name,
  1615. [OP_SEQUENCE] = (nfsd4_dec)nfsd4_decode_sequence,
  1616. [OP_SET_SSV] = (nfsd4_dec)nfsd4_decode_notsupp,
  1617. [OP_TEST_STATEID] = (nfsd4_dec)nfsd4_decode_test_stateid,
  1618. [OP_WANT_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
  1619. [OP_DESTROY_CLIENTID] = (nfsd4_dec)nfsd4_decode_destroy_clientid,
  1620. [OP_RECLAIM_COMPLETE] = (nfsd4_dec)nfsd4_decode_reclaim_complete,
  1621. /* new operations for NFSv4.2 */
  1622. [OP_ALLOCATE] = (nfsd4_dec)nfsd4_decode_fallocate,
  1623. [OP_COPY] = (nfsd4_dec)nfsd4_decode_copy,
  1624. [OP_COPY_NOTIFY] = (nfsd4_dec)nfsd4_decode_notsupp,
  1625. [OP_DEALLOCATE] = (nfsd4_dec)nfsd4_decode_fallocate,
  1626. [OP_IO_ADVISE] = (nfsd4_dec)nfsd4_decode_notsupp,
  1627. [OP_LAYOUTERROR] = (nfsd4_dec)nfsd4_decode_notsupp,
  1628. [OP_LAYOUTSTATS] = (nfsd4_dec)nfsd4_decode_notsupp,
  1629. [OP_OFFLOAD_CANCEL] = (nfsd4_dec)nfsd4_decode_notsupp,
  1630. [OP_OFFLOAD_STATUS] = (nfsd4_dec)nfsd4_decode_notsupp,
  1631. [OP_READ_PLUS] = (nfsd4_dec)nfsd4_decode_notsupp,
  1632. [OP_SEEK] = (nfsd4_dec)nfsd4_decode_seek,
  1633. [OP_WRITE_SAME] = (nfsd4_dec)nfsd4_decode_notsupp,
  1634. [OP_CLONE] = (nfsd4_dec)nfsd4_decode_clone,
  1635. };
  1636. static inline bool
  1637. nfsd4_opnum_in_range(struct nfsd4_compoundargs *argp, struct nfsd4_op *op)
  1638. {
  1639. if (op->opnum < FIRST_NFS4_OP)
  1640. return false;
  1641. else if (argp->minorversion == 0 && op->opnum > LAST_NFS40_OP)
  1642. return false;
  1643. else if (argp->minorversion == 1 && op->opnum > LAST_NFS41_OP)
  1644. return false;
  1645. else if (argp->minorversion == 2 && op->opnum > LAST_NFS42_OP)
  1646. return false;
  1647. return true;
  1648. }
  1649. static __be32
  1650. nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
  1651. {
  1652. DECODE_HEAD;
  1653. struct nfsd4_op *op;
  1654. bool cachethis = false;
  1655. int auth_slack= argp->rqstp->rq_auth_slack;
  1656. int max_reply = auth_slack + 8; /* opcnt, status */
  1657. int readcount = 0;
  1658. int readbytes = 0;
  1659. int i;
  1660. READ_BUF(4);
  1661. argp->taglen = be32_to_cpup(p++);
  1662. READ_BUF(argp->taglen);
  1663. SAVEMEM(argp->tag, argp->taglen);
  1664. READ_BUF(8);
  1665. argp->minorversion = be32_to_cpup(p++);
  1666. argp->opcnt = be32_to_cpup(p++);
  1667. max_reply += 4 + (XDR_QUADLEN(argp->taglen) << 2);
  1668. if (argp->taglen > NFSD4_MAX_TAGLEN)
  1669. goto xdr_error;
  1670. /*
  1671. * NFS4ERR_RESOURCE is a more helpful error than GARBAGE_ARGS
  1672. * here, so we return success at the xdr level so that
  1673. * nfsd4_proc can handle this is an NFS-level error.
  1674. */
  1675. if (argp->opcnt > NFSD_MAX_OPS_PER_COMPOUND)
  1676. return 0;
  1677. if (argp->opcnt > ARRAY_SIZE(argp->iops)) {
  1678. argp->ops = kzalloc(argp->opcnt * sizeof(*argp->ops), GFP_KERNEL);
  1679. if (!argp->ops) {
  1680. argp->ops = argp->iops;
  1681. dprintk("nfsd: couldn't allocate room for COMPOUND\n");
  1682. goto xdr_error;
  1683. }
  1684. }
  1685. if (argp->minorversion > NFSD_SUPPORTED_MINOR_VERSION)
  1686. argp->opcnt = 0;
  1687. for (i = 0; i < argp->opcnt; i++) {
  1688. op = &argp->ops[i];
  1689. op->replay = NULL;
  1690. READ_BUF(4);
  1691. op->opnum = be32_to_cpup(p++);
  1692. if (nfsd4_opnum_in_range(argp, op))
  1693. op->status = nfsd4_dec_ops[op->opnum](argp, &op->u);
  1694. else {
  1695. op->opnum = OP_ILLEGAL;
  1696. op->status = nfserr_op_illegal;
  1697. }
  1698. op->opdesc = OPDESC(op);
  1699. /*
  1700. * We'll try to cache the result in the DRC if any one
  1701. * op in the compound wants to be cached:
  1702. */
  1703. cachethis |= nfsd4_cache_this_op(op);
  1704. if (op->opnum == OP_READ) {
  1705. readcount++;
  1706. readbytes += nfsd4_max_reply(argp->rqstp, op);
  1707. } else
  1708. max_reply += nfsd4_max_reply(argp->rqstp, op);
  1709. /*
  1710. * OP_LOCK and OP_LOCKT may return a conflicting lock.
  1711. * (Special case because it will just skip encoding this
  1712. * if it runs out of xdr buffer space, and it is the only
  1713. * operation that behaves this way.)
  1714. */
  1715. if (op->opnum == OP_LOCK || op->opnum == OP_LOCKT)
  1716. max_reply += NFS4_OPAQUE_LIMIT;
  1717. if (op->status) {
  1718. argp->opcnt = i+1;
  1719. break;
  1720. }
  1721. }
  1722. /* Sessions make the DRC unnecessary: */
  1723. if (argp->minorversion)
  1724. cachethis = false;
  1725. svc_reserve(argp->rqstp, max_reply + readbytes);
  1726. argp->rqstp->rq_cachetype = cachethis ? RC_REPLBUFF : RC_NOCACHE;
  1727. if (readcount > 1 || max_reply > PAGE_SIZE - auth_slack)
  1728. clear_bit(RQ_SPLICE_OK, &argp->rqstp->rq_flags);
  1729. DECODE_TAIL;
  1730. }
  1731. static __be32 *encode_change(__be32 *p, struct kstat *stat, struct inode *inode,
  1732. struct svc_export *exp)
  1733. {
  1734. if (exp->ex_flags & NFSEXP_V4ROOT) {
  1735. *p++ = cpu_to_be32(convert_to_wallclock(exp->cd->flush_time));
  1736. *p++ = 0;
  1737. } else if (IS_I_VERSION(inode)) {
  1738. p = xdr_encode_hyper(p, nfsd4_change_attribute(stat, inode));
  1739. } else {
  1740. *p++ = cpu_to_be32(stat->ctime.tv_sec);
  1741. *p++ = cpu_to_be32(stat->ctime.tv_nsec);
  1742. }
  1743. return p;
  1744. }
  1745. static __be32 *encode_cinfo(__be32 *p, struct nfsd4_change_info *c)
  1746. {
  1747. *p++ = cpu_to_be32(c->atomic);
  1748. if (c->change_supported) {
  1749. p = xdr_encode_hyper(p, c->before_change);
  1750. p = xdr_encode_hyper(p, c->after_change);
  1751. } else {
  1752. *p++ = cpu_to_be32(c->before_ctime_sec);
  1753. *p++ = cpu_to_be32(c->before_ctime_nsec);
  1754. *p++ = cpu_to_be32(c->after_ctime_sec);
  1755. *p++ = cpu_to_be32(c->after_ctime_nsec);
  1756. }
  1757. return p;
  1758. }
  1759. /* Encode as an array of strings the string given with components
  1760. * separated @sep, escaped with esc_enter and esc_exit.
  1761. */
  1762. static __be32 nfsd4_encode_components_esc(struct xdr_stream *xdr, char sep,
  1763. char *components, char esc_enter,
  1764. char esc_exit)
  1765. {
  1766. __be32 *p;
  1767. __be32 pathlen;
  1768. int pathlen_offset;
  1769. int strlen, count=0;
  1770. char *str, *end, *next;
  1771. dprintk("nfsd4_encode_components(%s)\n", components);
  1772. pathlen_offset = xdr->buf->len;
  1773. p = xdr_reserve_space(xdr, 4);
  1774. if (!p)
  1775. return nfserr_resource;
  1776. p++; /* We will fill this in with @count later */
  1777. end = str = components;
  1778. while (*end) {
  1779. bool found_esc = false;
  1780. /* try to parse as esc_start, ..., esc_end, sep */
  1781. if (*str == esc_enter) {
  1782. for (; *end && (*end != esc_exit); end++)
  1783. /* find esc_exit or end of string */;
  1784. next = end + 1;
  1785. if (*end && (!*next || *next == sep)) {
  1786. str++;
  1787. found_esc = true;
  1788. }
  1789. }
  1790. if (!found_esc)
  1791. for (; *end && (*end != sep); end++)
  1792. /* find sep or end of string */;
  1793. strlen = end - str;
  1794. if (strlen) {
  1795. p = xdr_reserve_space(xdr, strlen + 4);
  1796. if (!p)
  1797. return nfserr_resource;
  1798. p = xdr_encode_opaque(p, str, strlen);
  1799. count++;
  1800. }
  1801. else
  1802. end++;
  1803. if (found_esc)
  1804. end = next;
  1805. str = end;
  1806. }
  1807. pathlen = htonl(count);
  1808. write_bytes_to_xdr_buf(xdr->buf, pathlen_offset, &pathlen, 4);
  1809. return 0;
  1810. }
  1811. /* Encode as an array of strings the string given with components
  1812. * separated @sep.
  1813. */
  1814. static __be32 nfsd4_encode_components(struct xdr_stream *xdr, char sep,
  1815. char *components)
  1816. {
  1817. return nfsd4_encode_components_esc(xdr, sep, components, 0, 0);
  1818. }
  1819. /*
  1820. * encode a location element of a fs_locations structure
  1821. */
  1822. static __be32 nfsd4_encode_fs_location4(struct xdr_stream *xdr,
  1823. struct nfsd4_fs_location *location)
  1824. {
  1825. __be32 status;
  1826. status = nfsd4_encode_components_esc(xdr, ':', location->hosts,
  1827. '[', ']');
  1828. if (status)
  1829. return status;
  1830. status = nfsd4_encode_components(xdr, '/', location->path);
  1831. if (status)
  1832. return status;
  1833. return 0;
  1834. }
  1835. /*
  1836. * Encode a path in RFC3530 'pathname4' format
  1837. */
  1838. static __be32 nfsd4_encode_path(struct xdr_stream *xdr,
  1839. const struct path *root,
  1840. const struct path *path)
  1841. {
  1842. struct path cur = *path;
  1843. __be32 *p;
  1844. struct dentry **components = NULL;
  1845. unsigned int ncomponents = 0;
  1846. __be32 err = nfserr_jukebox;
  1847. dprintk("nfsd4_encode_components(");
  1848. path_get(&cur);
  1849. /* First walk the path up to the nfsd root, and store the
  1850. * dentries/path components in an array.
  1851. */
  1852. for (;;) {
  1853. if (path_equal(&cur, root))
  1854. break;
  1855. if (cur.dentry == cur.mnt->mnt_root) {
  1856. if (follow_up(&cur))
  1857. continue;
  1858. goto out_free;
  1859. }
  1860. if ((ncomponents & 15) == 0) {
  1861. struct dentry **new;
  1862. new = krealloc(components,
  1863. sizeof(*new) * (ncomponents + 16),
  1864. GFP_KERNEL);
  1865. if (!new)
  1866. goto out_free;
  1867. components = new;
  1868. }
  1869. components[ncomponents++] = cur.dentry;
  1870. cur.dentry = dget_parent(cur.dentry);
  1871. }
  1872. err = nfserr_resource;
  1873. p = xdr_reserve_space(xdr, 4);
  1874. if (!p)
  1875. goto out_free;
  1876. *p++ = cpu_to_be32(ncomponents);
  1877. while (ncomponents) {
  1878. struct dentry *dentry = components[ncomponents - 1];
  1879. unsigned int len;
  1880. spin_lock(&dentry->d_lock);
  1881. len = dentry->d_name.len;
  1882. p = xdr_reserve_space(xdr, len + 4);
  1883. if (!p) {
  1884. spin_unlock(&dentry->d_lock);
  1885. goto out_free;
  1886. }
  1887. p = xdr_encode_opaque(p, dentry->d_name.name, len);
  1888. dprintk("/%pd", dentry);
  1889. spin_unlock(&dentry->d_lock);
  1890. dput(dentry);
  1891. ncomponents--;
  1892. }
  1893. err = 0;
  1894. out_free:
  1895. dprintk(")\n");
  1896. while (ncomponents)
  1897. dput(components[--ncomponents]);
  1898. kfree(components);
  1899. path_put(&cur);
  1900. return err;
  1901. }
  1902. static __be32 nfsd4_encode_fsloc_fsroot(struct xdr_stream *xdr,
  1903. struct svc_rqst *rqstp, const struct path *path)
  1904. {
  1905. struct svc_export *exp_ps;
  1906. __be32 res;
  1907. exp_ps = rqst_find_fsidzero_export(rqstp);
  1908. if (IS_ERR(exp_ps))
  1909. return nfserrno(PTR_ERR(exp_ps));
  1910. res = nfsd4_encode_path(xdr, &exp_ps->ex_path, path);
  1911. exp_put(exp_ps);
  1912. return res;
  1913. }
  1914. /*
  1915. * encode a fs_locations structure
  1916. */
  1917. static __be32 nfsd4_encode_fs_locations(struct xdr_stream *xdr,
  1918. struct svc_rqst *rqstp, struct svc_export *exp)
  1919. {
  1920. __be32 status;
  1921. int i;
  1922. __be32 *p;
  1923. struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
  1924. status = nfsd4_encode_fsloc_fsroot(xdr, rqstp, &exp->ex_path);
  1925. if (status)
  1926. return status;
  1927. p = xdr_reserve_space(xdr, 4);
  1928. if (!p)
  1929. return nfserr_resource;
  1930. *p++ = cpu_to_be32(fslocs->locations_count);
  1931. for (i=0; i<fslocs->locations_count; i++) {
  1932. status = nfsd4_encode_fs_location4(xdr, &fslocs->locations[i]);
  1933. if (status)
  1934. return status;
  1935. }
  1936. return 0;
  1937. }
  1938. static u32 nfs4_file_type(umode_t mode)
  1939. {
  1940. switch (mode & S_IFMT) {
  1941. case S_IFIFO: return NF4FIFO;
  1942. case S_IFCHR: return NF4CHR;
  1943. case S_IFDIR: return NF4DIR;
  1944. case S_IFBLK: return NF4BLK;
  1945. case S_IFLNK: return NF4LNK;
  1946. case S_IFREG: return NF4REG;
  1947. case S_IFSOCK: return NF4SOCK;
  1948. default: return NF4BAD;
  1949. };
  1950. }
  1951. static inline __be32
  1952. nfsd4_encode_aclname(struct xdr_stream *xdr, struct svc_rqst *rqstp,
  1953. struct nfs4_ace *ace)
  1954. {
  1955. if (ace->whotype != NFS4_ACL_WHO_NAMED)
  1956. return nfs4_acl_write_who(xdr, ace->whotype);
  1957. else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
  1958. return nfsd4_encode_group(xdr, rqstp, ace->who_gid);
  1959. else
  1960. return nfsd4_encode_user(xdr, rqstp, ace->who_uid);
  1961. }
  1962. static inline __be32
  1963. nfsd4_encode_layout_types(struct xdr_stream *xdr, u32 layout_types)
  1964. {
  1965. __be32 *p;
  1966. unsigned long i = hweight_long(layout_types);
  1967. p = xdr_reserve_space(xdr, 4 + 4 * i);
  1968. if (!p)
  1969. return nfserr_resource;
  1970. *p++ = cpu_to_be32(i);
  1971. for (i = LAYOUT_NFSV4_1_FILES; i < LAYOUT_TYPE_MAX; ++i)
  1972. if (layout_types & (1 << i))
  1973. *p++ = cpu_to_be32(i);
  1974. return 0;
  1975. }
  1976. #define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
  1977. FATTR4_WORD0_RDATTR_ERROR)
  1978. #define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
  1979. #define WORD2_ABSENT_FS_ATTRS 0
  1980. #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
  1981. static inline __be32
  1982. nfsd4_encode_security_label(struct xdr_stream *xdr, struct svc_rqst *rqstp,
  1983. void *context, int len)
  1984. {
  1985. __be32 *p;
  1986. p = xdr_reserve_space(xdr, len + 4 + 4 + 4);
  1987. if (!p)
  1988. return nfserr_resource;
  1989. /*
  1990. * For now we use a 0 here to indicate the null translation; in
  1991. * the future we may place a call to translation code here.
  1992. */
  1993. *p++ = cpu_to_be32(0); /* lfs */
  1994. *p++ = cpu_to_be32(0); /* pi */
  1995. p = xdr_encode_opaque(p, context, len);
  1996. return 0;
  1997. }
  1998. #else
  1999. static inline __be32
  2000. nfsd4_encode_security_label(struct xdr_stream *xdr, struct svc_rqst *rqstp,
  2001. void *context, int len)
  2002. { return 0; }
  2003. #endif
  2004. static __be32 fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *bmval2, u32 *rdattr_err)
  2005. {
  2006. /* As per referral draft: */
  2007. if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
  2008. *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
  2009. if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
  2010. *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
  2011. *rdattr_err = NFSERR_MOVED;
  2012. else
  2013. return nfserr_moved;
  2014. }
  2015. *bmval0 &= WORD0_ABSENT_FS_ATTRS;
  2016. *bmval1 &= WORD1_ABSENT_FS_ATTRS;
  2017. *bmval2 &= WORD2_ABSENT_FS_ATTRS;
  2018. return 0;
  2019. }
  2020. static int get_parent_attributes(struct svc_export *exp, struct kstat *stat)
  2021. {
  2022. struct path path = exp->ex_path;
  2023. int err;
  2024. path_get(&path);
  2025. while (follow_up(&path)) {
  2026. if (path.dentry != path.mnt->mnt_root)
  2027. break;
  2028. }
  2029. err = vfs_getattr(&path, stat, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
  2030. path_put(&path);
  2031. return err;
  2032. }
  2033. static __be32
  2034. nfsd4_encode_bitmap(struct xdr_stream *xdr, u32 bmval0, u32 bmval1, u32 bmval2)
  2035. {
  2036. __be32 *p;
  2037. if (bmval2) {
  2038. p = xdr_reserve_space(xdr, 16);
  2039. if (!p)
  2040. goto out_resource;
  2041. *p++ = cpu_to_be32(3);
  2042. *p++ = cpu_to_be32(bmval0);
  2043. *p++ = cpu_to_be32(bmval1);
  2044. *p++ = cpu_to_be32(bmval2);
  2045. } else if (bmval1) {
  2046. p = xdr_reserve_space(xdr, 12);
  2047. if (!p)
  2048. goto out_resource;
  2049. *p++ = cpu_to_be32(2);
  2050. *p++ = cpu_to_be32(bmval0);
  2051. *p++ = cpu_to_be32(bmval1);
  2052. } else {
  2053. p = xdr_reserve_space(xdr, 8);
  2054. if (!p)
  2055. goto out_resource;
  2056. *p++ = cpu_to_be32(1);
  2057. *p++ = cpu_to_be32(bmval0);
  2058. }
  2059. return 0;
  2060. out_resource:
  2061. return nfserr_resource;
  2062. }
  2063. /*
  2064. * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
  2065. * ourselves.
  2066. */
  2067. static __be32
  2068. nfsd4_encode_fattr(struct xdr_stream *xdr, struct svc_fh *fhp,
  2069. struct svc_export *exp,
  2070. struct dentry *dentry, u32 *bmval,
  2071. struct svc_rqst *rqstp, int ignore_crossmnt)
  2072. {
  2073. u32 bmval0 = bmval[0];
  2074. u32 bmval1 = bmval[1];
  2075. u32 bmval2 = bmval[2];
  2076. struct kstat stat;
  2077. struct svc_fh *tempfh = NULL;
  2078. struct kstatfs statfs;
  2079. __be32 *p;
  2080. int starting_len = xdr->buf->len;
  2081. int attrlen_offset;
  2082. __be32 attrlen;
  2083. u32 dummy;
  2084. u64 dummy64;
  2085. u32 rdattr_err = 0;
  2086. __be32 status;
  2087. int err;
  2088. struct nfs4_acl *acl = NULL;
  2089. void *context = NULL;
  2090. int contextlen;
  2091. bool contextsupport = false;
  2092. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  2093. u32 minorversion = resp->cstate.minorversion;
  2094. struct path path = {
  2095. .mnt = exp->ex_path.mnt,
  2096. .dentry = dentry,
  2097. };
  2098. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  2099. BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
  2100. BUG_ON(!nfsd_attrs_supported(minorversion, bmval));
  2101. if (exp->ex_fslocs.migrated) {
  2102. status = fattr_handle_absent_fs(&bmval0, &bmval1, &bmval2, &rdattr_err);
  2103. if (status)
  2104. goto out;
  2105. }
  2106. err = vfs_getattr(&path, &stat, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
  2107. if (err)
  2108. goto out_nfserr;
  2109. if ((bmval0 & (FATTR4_WORD0_FILES_AVAIL | FATTR4_WORD0_FILES_FREE |
  2110. FATTR4_WORD0_FILES_TOTAL | FATTR4_WORD0_MAXNAME)) ||
  2111. (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
  2112. FATTR4_WORD1_SPACE_TOTAL))) {
  2113. err = vfs_statfs(&path, &statfs);
  2114. if (err)
  2115. goto out_nfserr;
  2116. }
  2117. if ((bmval0 & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) && !fhp) {
  2118. tempfh = kmalloc(sizeof(struct svc_fh), GFP_KERNEL);
  2119. status = nfserr_jukebox;
  2120. if (!tempfh)
  2121. goto out;
  2122. fh_init(tempfh, NFS4_FHSIZE);
  2123. status = fh_compose(tempfh, exp, dentry, NULL);
  2124. if (status)
  2125. goto out;
  2126. fhp = tempfh;
  2127. }
  2128. if (bmval0 & FATTR4_WORD0_ACL) {
  2129. err = nfsd4_get_nfs4_acl(rqstp, dentry, &acl);
  2130. if (err == -EOPNOTSUPP)
  2131. bmval0 &= ~FATTR4_WORD0_ACL;
  2132. else if (err == -EINVAL) {
  2133. status = nfserr_attrnotsupp;
  2134. goto out;
  2135. } else if (err != 0)
  2136. goto out_nfserr;
  2137. }
  2138. #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
  2139. if ((bmval2 & FATTR4_WORD2_SECURITY_LABEL) ||
  2140. bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
  2141. if (exp->ex_flags & NFSEXP_SECURITY_LABEL)
  2142. err = security_inode_getsecctx(d_inode(dentry),
  2143. &context, &contextlen);
  2144. else
  2145. err = -EOPNOTSUPP;
  2146. contextsupport = (err == 0);
  2147. if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
  2148. if (err == -EOPNOTSUPP)
  2149. bmval2 &= ~FATTR4_WORD2_SECURITY_LABEL;
  2150. else if (err)
  2151. goto out_nfserr;
  2152. }
  2153. }
  2154. #endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
  2155. status = nfsd4_encode_bitmap(xdr, bmval0, bmval1, bmval2);
  2156. if (status)
  2157. goto out;
  2158. attrlen_offset = xdr->buf->len;
  2159. p = xdr_reserve_space(xdr, 4);
  2160. if (!p)
  2161. goto out_resource;
  2162. p++; /* to be backfilled later */
  2163. if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
  2164. u32 supp[3];
  2165. memcpy(supp, nfsd_suppattrs[minorversion], sizeof(supp));
  2166. if (!IS_POSIXACL(dentry->d_inode))
  2167. supp[0] &= ~FATTR4_WORD0_ACL;
  2168. if (!contextsupport)
  2169. supp[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
  2170. if (!supp[2]) {
  2171. p = xdr_reserve_space(xdr, 12);
  2172. if (!p)
  2173. goto out_resource;
  2174. *p++ = cpu_to_be32(2);
  2175. *p++ = cpu_to_be32(supp[0]);
  2176. *p++ = cpu_to_be32(supp[1]);
  2177. } else {
  2178. p = xdr_reserve_space(xdr, 16);
  2179. if (!p)
  2180. goto out_resource;
  2181. *p++ = cpu_to_be32(3);
  2182. *p++ = cpu_to_be32(supp[0]);
  2183. *p++ = cpu_to_be32(supp[1]);
  2184. *p++ = cpu_to_be32(supp[2]);
  2185. }
  2186. }
  2187. if (bmval0 & FATTR4_WORD0_TYPE) {
  2188. p = xdr_reserve_space(xdr, 4);
  2189. if (!p)
  2190. goto out_resource;
  2191. dummy = nfs4_file_type(stat.mode);
  2192. if (dummy == NF4BAD) {
  2193. status = nfserr_serverfault;
  2194. goto out;
  2195. }
  2196. *p++ = cpu_to_be32(dummy);
  2197. }
  2198. if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
  2199. p = xdr_reserve_space(xdr, 4);
  2200. if (!p)
  2201. goto out_resource;
  2202. if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
  2203. *p++ = cpu_to_be32(NFS4_FH_PERSISTENT);
  2204. else
  2205. *p++ = cpu_to_be32(NFS4_FH_PERSISTENT|
  2206. NFS4_FH_VOL_RENAME);
  2207. }
  2208. if (bmval0 & FATTR4_WORD0_CHANGE) {
  2209. p = xdr_reserve_space(xdr, 8);
  2210. if (!p)
  2211. goto out_resource;
  2212. p = encode_change(p, &stat, d_inode(dentry), exp);
  2213. }
  2214. if (bmval0 & FATTR4_WORD0_SIZE) {
  2215. p = xdr_reserve_space(xdr, 8);
  2216. if (!p)
  2217. goto out_resource;
  2218. p = xdr_encode_hyper(p, stat.size);
  2219. }
  2220. if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
  2221. p = xdr_reserve_space(xdr, 4);
  2222. if (!p)
  2223. goto out_resource;
  2224. *p++ = cpu_to_be32(1);
  2225. }
  2226. if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
  2227. p = xdr_reserve_space(xdr, 4);
  2228. if (!p)
  2229. goto out_resource;
  2230. *p++ = cpu_to_be32(1);
  2231. }
  2232. if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
  2233. p = xdr_reserve_space(xdr, 4);
  2234. if (!p)
  2235. goto out_resource;
  2236. *p++ = cpu_to_be32(0);
  2237. }
  2238. if (bmval0 & FATTR4_WORD0_FSID) {
  2239. p = xdr_reserve_space(xdr, 16);
  2240. if (!p)
  2241. goto out_resource;
  2242. if (exp->ex_fslocs.migrated) {
  2243. p = xdr_encode_hyper(p, NFS4_REFERRAL_FSID_MAJOR);
  2244. p = xdr_encode_hyper(p, NFS4_REFERRAL_FSID_MINOR);
  2245. } else switch(fsid_source(fhp)) {
  2246. case FSIDSOURCE_FSID:
  2247. p = xdr_encode_hyper(p, (u64)exp->ex_fsid);
  2248. p = xdr_encode_hyper(p, (u64)0);
  2249. break;
  2250. case FSIDSOURCE_DEV:
  2251. *p++ = cpu_to_be32(0);
  2252. *p++ = cpu_to_be32(MAJOR(stat.dev));
  2253. *p++ = cpu_to_be32(0);
  2254. *p++ = cpu_to_be32(MINOR(stat.dev));
  2255. break;
  2256. case FSIDSOURCE_UUID:
  2257. p = xdr_encode_opaque_fixed(p, exp->ex_uuid,
  2258. EX_UUID_LEN);
  2259. break;
  2260. }
  2261. }
  2262. if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
  2263. p = xdr_reserve_space(xdr, 4);
  2264. if (!p)
  2265. goto out_resource;
  2266. *p++ = cpu_to_be32(0);
  2267. }
  2268. if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
  2269. p = xdr_reserve_space(xdr, 4);
  2270. if (!p)
  2271. goto out_resource;
  2272. *p++ = cpu_to_be32(nn->nfsd4_lease);
  2273. }
  2274. if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
  2275. p = xdr_reserve_space(xdr, 4);
  2276. if (!p)
  2277. goto out_resource;
  2278. *p++ = cpu_to_be32(rdattr_err);
  2279. }
  2280. if (bmval0 & FATTR4_WORD0_ACL) {
  2281. struct nfs4_ace *ace;
  2282. if (acl == NULL) {
  2283. p = xdr_reserve_space(xdr, 4);
  2284. if (!p)
  2285. goto out_resource;
  2286. *p++ = cpu_to_be32(0);
  2287. goto out_acl;
  2288. }
  2289. p = xdr_reserve_space(xdr, 4);
  2290. if (!p)
  2291. goto out_resource;
  2292. *p++ = cpu_to_be32(acl->naces);
  2293. for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
  2294. p = xdr_reserve_space(xdr, 4*3);
  2295. if (!p)
  2296. goto out_resource;
  2297. *p++ = cpu_to_be32(ace->type);
  2298. *p++ = cpu_to_be32(ace->flag);
  2299. *p++ = cpu_to_be32(ace->access_mask &
  2300. NFS4_ACE_MASK_ALL);
  2301. status = nfsd4_encode_aclname(xdr, rqstp, ace);
  2302. if (status)
  2303. goto out;
  2304. }
  2305. }
  2306. out_acl:
  2307. if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
  2308. p = xdr_reserve_space(xdr, 4);
  2309. if (!p)
  2310. goto out_resource;
  2311. *p++ = cpu_to_be32(IS_POSIXACL(dentry->d_inode) ?
  2312. ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL : 0);
  2313. }
  2314. if (bmval0 & FATTR4_WORD0_CANSETTIME) {
  2315. p = xdr_reserve_space(xdr, 4);
  2316. if (!p)
  2317. goto out_resource;
  2318. *p++ = cpu_to_be32(1);
  2319. }
  2320. if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
  2321. p = xdr_reserve_space(xdr, 4);
  2322. if (!p)
  2323. goto out_resource;
  2324. *p++ = cpu_to_be32(0);
  2325. }
  2326. if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
  2327. p = xdr_reserve_space(xdr, 4);
  2328. if (!p)
  2329. goto out_resource;
  2330. *p++ = cpu_to_be32(1);
  2331. }
  2332. if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
  2333. p = xdr_reserve_space(xdr, 4);
  2334. if (!p)
  2335. goto out_resource;
  2336. *p++ = cpu_to_be32(1);
  2337. }
  2338. if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
  2339. p = xdr_reserve_space(xdr, fhp->fh_handle.fh_size + 4);
  2340. if (!p)
  2341. goto out_resource;
  2342. p = xdr_encode_opaque(p, &fhp->fh_handle.fh_base,
  2343. fhp->fh_handle.fh_size);
  2344. }
  2345. if (bmval0 & FATTR4_WORD0_FILEID) {
  2346. p = xdr_reserve_space(xdr, 8);
  2347. if (!p)
  2348. goto out_resource;
  2349. p = xdr_encode_hyper(p, stat.ino);
  2350. }
  2351. if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
  2352. p = xdr_reserve_space(xdr, 8);
  2353. if (!p)
  2354. goto out_resource;
  2355. p = xdr_encode_hyper(p, (u64) statfs.f_ffree);
  2356. }
  2357. if (bmval0 & FATTR4_WORD0_FILES_FREE) {
  2358. p = xdr_reserve_space(xdr, 8);
  2359. if (!p)
  2360. goto out_resource;
  2361. p = xdr_encode_hyper(p, (u64) statfs.f_ffree);
  2362. }
  2363. if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
  2364. p = xdr_reserve_space(xdr, 8);
  2365. if (!p)
  2366. goto out_resource;
  2367. p = xdr_encode_hyper(p, (u64) statfs.f_files);
  2368. }
  2369. if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
  2370. status = nfsd4_encode_fs_locations(xdr, rqstp, exp);
  2371. if (status)
  2372. goto out;
  2373. }
  2374. if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
  2375. p = xdr_reserve_space(xdr, 4);
  2376. if (!p)
  2377. goto out_resource;
  2378. *p++ = cpu_to_be32(1);
  2379. }
  2380. if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
  2381. p = xdr_reserve_space(xdr, 8);
  2382. if (!p)
  2383. goto out_resource;
  2384. p = xdr_encode_hyper(p, exp->ex_path.mnt->mnt_sb->s_maxbytes);
  2385. }
  2386. if (bmval0 & FATTR4_WORD0_MAXLINK) {
  2387. p = xdr_reserve_space(xdr, 4);
  2388. if (!p)
  2389. goto out_resource;
  2390. *p++ = cpu_to_be32(255);
  2391. }
  2392. if (bmval0 & FATTR4_WORD0_MAXNAME) {
  2393. p = xdr_reserve_space(xdr, 4);
  2394. if (!p)
  2395. goto out_resource;
  2396. *p++ = cpu_to_be32(statfs.f_namelen);
  2397. }
  2398. if (bmval0 & FATTR4_WORD0_MAXREAD) {
  2399. p = xdr_reserve_space(xdr, 8);
  2400. if (!p)
  2401. goto out_resource;
  2402. p = xdr_encode_hyper(p, (u64) svc_max_payload(rqstp));
  2403. }
  2404. if (bmval0 & FATTR4_WORD0_MAXWRITE) {
  2405. p = xdr_reserve_space(xdr, 8);
  2406. if (!p)
  2407. goto out_resource;
  2408. p = xdr_encode_hyper(p, (u64) svc_max_payload(rqstp));
  2409. }
  2410. if (bmval1 & FATTR4_WORD1_MODE) {
  2411. p = xdr_reserve_space(xdr, 4);
  2412. if (!p)
  2413. goto out_resource;
  2414. *p++ = cpu_to_be32(stat.mode & S_IALLUGO);
  2415. }
  2416. if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
  2417. p = xdr_reserve_space(xdr, 4);
  2418. if (!p)
  2419. goto out_resource;
  2420. *p++ = cpu_to_be32(1);
  2421. }
  2422. if (bmval1 & FATTR4_WORD1_NUMLINKS) {
  2423. p = xdr_reserve_space(xdr, 4);
  2424. if (!p)
  2425. goto out_resource;
  2426. *p++ = cpu_to_be32(stat.nlink);
  2427. }
  2428. if (bmval1 & FATTR4_WORD1_OWNER) {
  2429. status = nfsd4_encode_user(xdr, rqstp, stat.uid);
  2430. if (status)
  2431. goto out;
  2432. }
  2433. if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
  2434. status = nfsd4_encode_group(xdr, rqstp, stat.gid);
  2435. if (status)
  2436. goto out;
  2437. }
  2438. if (bmval1 & FATTR4_WORD1_RAWDEV) {
  2439. p = xdr_reserve_space(xdr, 8);
  2440. if (!p)
  2441. goto out_resource;
  2442. *p++ = cpu_to_be32((u32) MAJOR(stat.rdev));
  2443. *p++ = cpu_to_be32((u32) MINOR(stat.rdev));
  2444. }
  2445. if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
  2446. p = xdr_reserve_space(xdr, 8);
  2447. if (!p)
  2448. goto out_resource;
  2449. dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
  2450. p = xdr_encode_hyper(p, dummy64);
  2451. }
  2452. if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
  2453. p = xdr_reserve_space(xdr, 8);
  2454. if (!p)
  2455. goto out_resource;
  2456. dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
  2457. p = xdr_encode_hyper(p, dummy64);
  2458. }
  2459. if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
  2460. p = xdr_reserve_space(xdr, 8);
  2461. if (!p)
  2462. goto out_resource;
  2463. dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
  2464. p = xdr_encode_hyper(p, dummy64);
  2465. }
  2466. if (bmval1 & FATTR4_WORD1_SPACE_USED) {
  2467. p = xdr_reserve_space(xdr, 8);
  2468. if (!p)
  2469. goto out_resource;
  2470. dummy64 = (u64)stat.blocks << 9;
  2471. p = xdr_encode_hyper(p, dummy64);
  2472. }
  2473. if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
  2474. p = xdr_reserve_space(xdr, 12);
  2475. if (!p)
  2476. goto out_resource;
  2477. p = xdr_encode_hyper(p, (s64)stat.atime.tv_sec);
  2478. *p++ = cpu_to_be32(stat.atime.tv_nsec);
  2479. }
  2480. if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
  2481. p = xdr_reserve_space(xdr, 12);
  2482. if (!p)
  2483. goto out_resource;
  2484. *p++ = cpu_to_be32(0);
  2485. *p++ = cpu_to_be32(1);
  2486. *p++ = cpu_to_be32(0);
  2487. }
  2488. if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
  2489. p = xdr_reserve_space(xdr, 12);
  2490. if (!p)
  2491. goto out_resource;
  2492. p = xdr_encode_hyper(p, (s64)stat.ctime.tv_sec);
  2493. *p++ = cpu_to_be32(stat.ctime.tv_nsec);
  2494. }
  2495. if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
  2496. p = xdr_reserve_space(xdr, 12);
  2497. if (!p)
  2498. goto out_resource;
  2499. p = xdr_encode_hyper(p, (s64)stat.mtime.tv_sec);
  2500. *p++ = cpu_to_be32(stat.mtime.tv_nsec);
  2501. }
  2502. if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
  2503. struct kstat parent_stat;
  2504. u64 ino = stat.ino;
  2505. p = xdr_reserve_space(xdr, 8);
  2506. if (!p)
  2507. goto out_resource;
  2508. /*
  2509. * Get parent's attributes if not ignoring crossmount
  2510. * and this is the root of a cross-mounted filesystem.
  2511. */
  2512. if (ignore_crossmnt == 0 &&
  2513. dentry == exp->ex_path.mnt->mnt_root) {
  2514. err = get_parent_attributes(exp, &parent_stat);
  2515. if (err)
  2516. goto out_nfserr;
  2517. ino = parent_stat.ino;
  2518. }
  2519. p = xdr_encode_hyper(p, ino);
  2520. }
  2521. #ifdef CONFIG_NFSD_PNFS
  2522. if (bmval1 & FATTR4_WORD1_FS_LAYOUT_TYPES) {
  2523. status = nfsd4_encode_layout_types(xdr, exp->ex_layout_types);
  2524. if (status)
  2525. goto out;
  2526. }
  2527. if (bmval2 & FATTR4_WORD2_LAYOUT_TYPES) {
  2528. status = nfsd4_encode_layout_types(xdr, exp->ex_layout_types);
  2529. if (status)
  2530. goto out;
  2531. }
  2532. if (bmval2 & FATTR4_WORD2_LAYOUT_BLKSIZE) {
  2533. p = xdr_reserve_space(xdr, 4);
  2534. if (!p)
  2535. goto out_resource;
  2536. *p++ = cpu_to_be32(stat.blksize);
  2537. }
  2538. #endif /* CONFIG_NFSD_PNFS */
  2539. if (bmval2 & FATTR4_WORD2_SUPPATTR_EXCLCREAT) {
  2540. u32 supp[3];
  2541. memcpy(supp, nfsd_suppattrs[minorversion], sizeof(supp));
  2542. supp[0] &= NFSD_SUPPATTR_EXCLCREAT_WORD0;
  2543. supp[1] &= NFSD_SUPPATTR_EXCLCREAT_WORD1;
  2544. supp[2] &= NFSD_SUPPATTR_EXCLCREAT_WORD2;
  2545. status = nfsd4_encode_bitmap(xdr, supp[0], supp[1], supp[2]);
  2546. if (status)
  2547. goto out;
  2548. }
  2549. if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
  2550. status = nfsd4_encode_security_label(xdr, rqstp, context,
  2551. contextlen);
  2552. if (status)
  2553. goto out;
  2554. }
  2555. attrlen = htonl(xdr->buf->len - attrlen_offset - 4);
  2556. write_bytes_to_xdr_buf(xdr->buf, attrlen_offset, &attrlen, 4);
  2557. status = nfs_ok;
  2558. out:
  2559. #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
  2560. if (context)
  2561. security_release_secctx(context, contextlen);
  2562. #endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
  2563. kfree(acl);
  2564. if (tempfh) {
  2565. fh_put(tempfh);
  2566. kfree(tempfh);
  2567. }
  2568. if (status)
  2569. xdr_truncate_encode(xdr, starting_len);
  2570. return status;
  2571. out_nfserr:
  2572. status = nfserrno(err);
  2573. goto out;
  2574. out_resource:
  2575. status = nfserr_resource;
  2576. goto out;
  2577. }
  2578. static void svcxdr_init_encode_from_buffer(struct xdr_stream *xdr,
  2579. struct xdr_buf *buf, __be32 *p, int bytes)
  2580. {
  2581. xdr->scratch.iov_len = 0;
  2582. memset(buf, 0, sizeof(struct xdr_buf));
  2583. buf->head[0].iov_base = p;
  2584. buf->head[0].iov_len = 0;
  2585. buf->len = 0;
  2586. xdr->buf = buf;
  2587. xdr->iov = buf->head;
  2588. xdr->p = p;
  2589. xdr->end = (void *)p + bytes;
  2590. buf->buflen = bytes;
  2591. }
  2592. __be32 nfsd4_encode_fattr_to_buf(__be32 **p, int words,
  2593. struct svc_fh *fhp, struct svc_export *exp,
  2594. struct dentry *dentry, u32 *bmval,
  2595. struct svc_rqst *rqstp, int ignore_crossmnt)
  2596. {
  2597. struct xdr_buf dummy;
  2598. struct xdr_stream xdr;
  2599. __be32 ret;
  2600. svcxdr_init_encode_from_buffer(&xdr, &dummy, *p, words << 2);
  2601. ret = nfsd4_encode_fattr(&xdr, fhp, exp, dentry, bmval, rqstp,
  2602. ignore_crossmnt);
  2603. *p = xdr.p;
  2604. return ret;
  2605. }
  2606. static inline int attributes_need_mount(u32 *bmval)
  2607. {
  2608. if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
  2609. return 1;
  2610. if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
  2611. return 1;
  2612. return 0;
  2613. }
  2614. static __be32
  2615. nfsd4_encode_dirent_fattr(struct xdr_stream *xdr, struct nfsd4_readdir *cd,
  2616. const char *name, int namlen)
  2617. {
  2618. struct svc_export *exp = cd->rd_fhp->fh_export;
  2619. struct dentry *dentry;
  2620. __be32 nfserr;
  2621. int ignore_crossmnt = 0;
  2622. dentry = lookup_one_len_unlocked(name, cd->rd_fhp->fh_dentry, namlen);
  2623. if (IS_ERR(dentry))
  2624. return nfserrno(PTR_ERR(dentry));
  2625. if (d_really_is_negative(dentry)) {
  2626. /*
  2627. * we're not holding the i_mutex here, so there's
  2628. * a window where this directory entry could have gone
  2629. * away.
  2630. */
  2631. dput(dentry);
  2632. return nfserr_noent;
  2633. }
  2634. exp_get(exp);
  2635. /*
  2636. * In the case of a mountpoint, the client may be asking for
  2637. * attributes that are only properties of the underlying filesystem
  2638. * as opposed to the cross-mounted file system. In such a case,
  2639. * we will not follow the cross mount and will fill the attribtutes
  2640. * directly from the mountpoint dentry.
  2641. */
  2642. if (nfsd_mountpoint(dentry, exp)) {
  2643. int err;
  2644. if (!(exp->ex_flags & NFSEXP_V4ROOT)
  2645. && !attributes_need_mount(cd->rd_bmval)) {
  2646. ignore_crossmnt = 1;
  2647. goto out_encode;
  2648. }
  2649. /*
  2650. * Why the heck aren't we just using nfsd_lookup??
  2651. * Different "."/".." handling? Something else?
  2652. * At least, add a comment here to explain....
  2653. */
  2654. err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
  2655. if (err) {
  2656. nfserr = nfserrno(err);
  2657. goto out_put;
  2658. }
  2659. nfserr = check_nfsd_access(exp, cd->rd_rqstp);
  2660. if (nfserr)
  2661. goto out_put;
  2662. }
  2663. out_encode:
  2664. nfserr = nfsd4_encode_fattr(xdr, NULL, exp, dentry, cd->rd_bmval,
  2665. cd->rd_rqstp, ignore_crossmnt);
  2666. out_put:
  2667. dput(dentry);
  2668. exp_put(exp);
  2669. return nfserr;
  2670. }
  2671. static __be32 *
  2672. nfsd4_encode_rdattr_error(struct xdr_stream *xdr, __be32 nfserr)
  2673. {
  2674. __be32 *p;
  2675. p = xdr_reserve_space(xdr, 20);
  2676. if (!p)
  2677. return NULL;
  2678. *p++ = htonl(2);
  2679. *p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
  2680. *p++ = htonl(0); /* bmval1 */
  2681. *p++ = htonl(4); /* attribute length */
  2682. *p++ = nfserr; /* no htonl */
  2683. return p;
  2684. }
  2685. static int
  2686. nfsd4_encode_dirent(void *ccdv, const char *name, int namlen,
  2687. loff_t offset, u64 ino, unsigned int d_type)
  2688. {
  2689. struct readdir_cd *ccd = ccdv;
  2690. struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
  2691. struct xdr_stream *xdr = cd->xdr;
  2692. int start_offset = xdr->buf->len;
  2693. int cookie_offset;
  2694. u32 name_and_cookie;
  2695. int entry_bytes;
  2696. __be32 nfserr = nfserr_toosmall;
  2697. __be64 wire_offset;
  2698. __be32 *p;
  2699. /* In nfsv4, "." and ".." never make it onto the wire.. */
  2700. if (name && isdotent(name, namlen)) {
  2701. cd->common.err = nfs_ok;
  2702. return 0;
  2703. }
  2704. if (cd->cookie_offset) {
  2705. wire_offset = cpu_to_be64(offset);
  2706. write_bytes_to_xdr_buf(xdr->buf, cd->cookie_offset,
  2707. &wire_offset, 8);
  2708. }
  2709. p = xdr_reserve_space(xdr, 4);
  2710. if (!p)
  2711. goto fail;
  2712. *p++ = xdr_one; /* mark entry present */
  2713. cookie_offset = xdr->buf->len;
  2714. p = xdr_reserve_space(xdr, 3*4 + namlen);
  2715. if (!p)
  2716. goto fail;
  2717. p = xdr_encode_hyper(p, NFS_OFFSET_MAX); /* offset of next entry */
  2718. p = xdr_encode_array(p, name, namlen); /* name length & name */
  2719. nfserr = nfsd4_encode_dirent_fattr(xdr, cd, name, namlen);
  2720. switch (nfserr) {
  2721. case nfs_ok:
  2722. break;
  2723. case nfserr_resource:
  2724. nfserr = nfserr_toosmall;
  2725. goto fail;
  2726. case nfserr_noent:
  2727. xdr_truncate_encode(xdr, start_offset);
  2728. goto skip_entry;
  2729. default:
  2730. /*
  2731. * If the client requested the RDATTR_ERROR attribute,
  2732. * we stuff the error code into this attribute
  2733. * and continue. If this attribute was not requested,
  2734. * then in accordance with the spec, we fail the
  2735. * entire READDIR operation(!)
  2736. */
  2737. if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
  2738. goto fail;
  2739. p = nfsd4_encode_rdattr_error(xdr, nfserr);
  2740. if (p == NULL) {
  2741. nfserr = nfserr_toosmall;
  2742. goto fail;
  2743. }
  2744. }
  2745. nfserr = nfserr_toosmall;
  2746. entry_bytes = xdr->buf->len - start_offset;
  2747. if (entry_bytes > cd->rd_maxcount)
  2748. goto fail;
  2749. cd->rd_maxcount -= entry_bytes;
  2750. /*
  2751. * RFC 3530 14.2.24 describes rd_dircount as only a "hint", so
  2752. * let's always let through the first entry, at least:
  2753. */
  2754. if (!cd->rd_dircount)
  2755. goto fail;
  2756. name_and_cookie = 4 + 4 * XDR_QUADLEN(namlen) + 8;
  2757. if (name_and_cookie > cd->rd_dircount && cd->cookie_offset)
  2758. goto fail;
  2759. cd->rd_dircount -= min(cd->rd_dircount, name_and_cookie);
  2760. cd->cookie_offset = cookie_offset;
  2761. skip_entry:
  2762. cd->common.err = nfs_ok;
  2763. return 0;
  2764. fail:
  2765. xdr_truncate_encode(xdr, start_offset);
  2766. cd->common.err = nfserr;
  2767. return -EINVAL;
  2768. }
  2769. static __be32
  2770. nfsd4_encode_stateid(struct xdr_stream *xdr, stateid_t *sid)
  2771. {
  2772. __be32 *p;
  2773. p = xdr_reserve_space(xdr, sizeof(stateid_t));
  2774. if (!p)
  2775. return nfserr_resource;
  2776. *p++ = cpu_to_be32(sid->si_generation);
  2777. p = xdr_encode_opaque_fixed(p, &sid->si_opaque,
  2778. sizeof(stateid_opaque_t));
  2779. return 0;
  2780. }
  2781. static __be32
  2782. nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_access *access)
  2783. {
  2784. struct xdr_stream *xdr = &resp->xdr;
  2785. __be32 *p;
  2786. p = xdr_reserve_space(xdr, 8);
  2787. if (!p)
  2788. return nfserr_resource;
  2789. *p++ = cpu_to_be32(access->ac_supported);
  2790. *p++ = cpu_to_be32(access->ac_resp_access);
  2791. return 0;
  2792. }
  2793. static __be32 nfsd4_encode_bind_conn_to_session(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_bind_conn_to_session *bcts)
  2794. {
  2795. struct xdr_stream *xdr = &resp->xdr;
  2796. __be32 *p;
  2797. p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN + 8);
  2798. if (!p)
  2799. return nfserr_resource;
  2800. p = xdr_encode_opaque_fixed(p, bcts->sessionid.data,
  2801. NFS4_MAX_SESSIONID_LEN);
  2802. *p++ = cpu_to_be32(bcts->dir);
  2803. /* Upshifting from TCP to RDMA is not supported */
  2804. *p++ = cpu_to_be32(0);
  2805. return 0;
  2806. }
  2807. static __be32
  2808. nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_close *close)
  2809. {
  2810. struct xdr_stream *xdr = &resp->xdr;
  2811. return nfsd4_encode_stateid(xdr, &close->cl_stateid);
  2812. }
  2813. static __be32
  2814. nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_commit *commit)
  2815. {
  2816. struct xdr_stream *xdr = &resp->xdr;
  2817. __be32 *p;
  2818. p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
  2819. if (!p)
  2820. return nfserr_resource;
  2821. p = xdr_encode_opaque_fixed(p, commit->co_verf.data,
  2822. NFS4_VERIFIER_SIZE);
  2823. return 0;
  2824. }
  2825. static __be32
  2826. nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_create *create)
  2827. {
  2828. struct xdr_stream *xdr = &resp->xdr;
  2829. __be32 *p;
  2830. p = xdr_reserve_space(xdr, 20);
  2831. if (!p)
  2832. return nfserr_resource;
  2833. encode_cinfo(p, &create->cr_cinfo);
  2834. nfserr = nfsd4_encode_bitmap(xdr, create->cr_bmval[0],
  2835. create->cr_bmval[1], create->cr_bmval[2]);
  2836. return 0;
  2837. }
  2838. static __be32
  2839. nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_getattr *getattr)
  2840. {
  2841. struct svc_fh *fhp = getattr->ga_fhp;
  2842. struct xdr_stream *xdr = &resp->xdr;
  2843. return nfsd4_encode_fattr(xdr, fhp, fhp->fh_export, fhp->fh_dentry,
  2844. getattr->ga_bmval, resp->rqstp, 0);
  2845. }
  2846. static __be32
  2847. nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr, struct svc_fh **fhpp)
  2848. {
  2849. struct xdr_stream *xdr = &resp->xdr;
  2850. struct svc_fh *fhp = *fhpp;
  2851. unsigned int len;
  2852. __be32 *p;
  2853. len = fhp->fh_handle.fh_size;
  2854. p = xdr_reserve_space(xdr, len + 4);
  2855. if (!p)
  2856. return nfserr_resource;
  2857. p = xdr_encode_opaque(p, &fhp->fh_handle.fh_base, len);
  2858. return 0;
  2859. }
  2860. /*
  2861. * Including all fields other than the name, a LOCK4denied structure requires
  2862. * 8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
  2863. */
  2864. static __be32
  2865. nfsd4_encode_lock_denied(struct xdr_stream *xdr, struct nfsd4_lock_denied *ld)
  2866. {
  2867. struct xdr_netobj *conf = &ld->ld_owner;
  2868. __be32 *p;
  2869. again:
  2870. p = xdr_reserve_space(xdr, 32 + XDR_LEN(conf->len));
  2871. if (!p) {
  2872. /*
  2873. * Don't fail to return the result just because we can't
  2874. * return the conflicting open:
  2875. */
  2876. if (conf->len) {
  2877. kfree(conf->data);
  2878. conf->len = 0;
  2879. conf->data = NULL;
  2880. goto again;
  2881. }
  2882. return nfserr_resource;
  2883. }
  2884. p = xdr_encode_hyper(p, ld->ld_start);
  2885. p = xdr_encode_hyper(p, ld->ld_length);
  2886. *p++ = cpu_to_be32(ld->ld_type);
  2887. if (conf->len) {
  2888. p = xdr_encode_opaque_fixed(p, &ld->ld_clientid, 8);
  2889. p = xdr_encode_opaque(p, conf->data, conf->len);
  2890. kfree(conf->data);
  2891. } else { /* non - nfsv4 lock in conflict, no clientid nor owner */
  2892. p = xdr_encode_hyper(p, (u64)0); /* clientid */
  2893. *p++ = cpu_to_be32(0); /* length of owner name */
  2894. }
  2895. return nfserr_denied;
  2896. }
  2897. static __be32
  2898. nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lock *lock)
  2899. {
  2900. struct xdr_stream *xdr = &resp->xdr;
  2901. if (!nfserr)
  2902. nfserr = nfsd4_encode_stateid(xdr, &lock->lk_resp_stateid);
  2903. else if (nfserr == nfserr_denied)
  2904. nfserr = nfsd4_encode_lock_denied(xdr, &lock->lk_denied);
  2905. return nfserr;
  2906. }
  2907. static __be32
  2908. nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lockt *lockt)
  2909. {
  2910. struct xdr_stream *xdr = &resp->xdr;
  2911. if (nfserr == nfserr_denied)
  2912. nfsd4_encode_lock_denied(xdr, &lockt->lt_denied);
  2913. return nfserr;
  2914. }
  2915. static __be32
  2916. nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_locku *locku)
  2917. {
  2918. struct xdr_stream *xdr = &resp->xdr;
  2919. return nfsd4_encode_stateid(xdr, &locku->lu_stateid);
  2920. }
  2921. static __be32
  2922. nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_link *link)
  2923. {
  2924. struct xdr_stream *xdr = &resp->xdr;
  2925. __be32 *p;
  2926. p = xdr_reserve_space(xdr, 20);
  2927. if (!p)
  2928. return nfserr_resource;
  2929. p = encode_cinfo(p, &link->li_cinfo);
  2930. return 0;
  2931. }
  2932. static __be32
  2933. nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open *open)
  2934. {
  2935. struct xdr_stream *xdr = &resp->xdr;
  2936. __be32 *p;
  2937. nfserr = nfsd4_encode_stateid(xdr, &open->op_stateid);
  2938. if (nfserr)
  2939. return nfserr;
  2940. p = xdr_reserve_space(xdr, 24);
  2941. if (!p)
  2942. return nfserr_resource;
  2943. p = encode_cinfo(p, &open->op_cinfo);
  2944. *p++ = cpu_to_be32(open->op_rflags);
  2945. nfserr = nfsd4_encode_bitmap(xdr, open->op_bmval[0], open->op_bmval[1],
  2946. open->op_bmval[2]);
  2947. if (nfserr)
  2948. return nfserr;
  2949. p = xdr_reserve_space(xdr, 4);
  2950. if (!p)
  2951. return nfserr_resource;
  2952. *p++ = cpu_to_be32(open->op_delegate_type);
  2953. switch (open->op_delegate_type) {
  2954. case NFS4_OPEN_DELEGATE_NONE:
  2955. break;
  2956. case NFS4_OPEN_DELEGATE_READ:
  2957. nfserr = nfsd4_encode_stateid(xdr, &open->op_delegate_stateid);
  2958. if (nfserr)
  2959. return nfserr;
  2960. p = xdr_reserve_space(xdr, 20);
  2961. if (!p)
  2962. return nfserr_resource;
  2963. *p++ = cpu_to_be32(open->op_recall);
  2964. /*
  2965. * TODO: ACE's in delegations
  2966. */
  2967. *p++ = cpu_to_be32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
  2968. *p++ = cpu_to_be32(0);
  2969. *p++ = cpu_to_be32(0);
  2970. *p++ = cpu_to_be32(0); /* XXX: is NULL principal ok? */
  2971. break;
  2972. case NFS4_OPEN_DELEGATE_WRITE:
  2973. nfserr = nfsd4_encode_stateid(xdr, &open->op_delegate_stateid);
  2974. if (nfserr)
  2975. return nfserr;
  2976. p = xdr_reserve_space(xdr, 32);
  2977. if (!p)
  2978. return nfserr_resource;
  2979. *p++ = cpu_to_be32(0);
  2980. /*
  2981. * TODO: space_limit's in delegations
  2982. */
  2983. *p++ = cpu_to_be32(NFS4_LIMIT_SIZE);
  2984. *p++ = cpu_to_be32(~(u32)0);
  2985. *p++ = cpu_to_be32(~(u32)0);
  2986. /*
  2987. * TODO: ACE's in delegations
  2988. */
  2989. *p++ = cpu_to_be32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
  2990. *p++ = cpu_to_be32(0);
  2991. *p++ = cpu_to_be32(0);
  2992. *p++ = cpu_to_be32(0); /* XXX: is NULL principal ok? */
  2993. break;
  2994. case NFS4_OPEN_DELEGATE_NONE_EXT: /* 4.1 */
  2995. switch (open->op_why_no_deleg) {
  2996. case WND4_CONTENTION:
  2997. case WND4_RESOURCE:
  2998. p = xdr_reserve_space(xdr, 8);
  2999. if (!p)
  3000. return nfserr_resource;
  3001. *p++ = cpu_to_be32(open->op_why_no_deleg);
  3002. /* deleg signaling not supported yet: */
  3003. *p++ = cpu_to_be32(0);
  3004. break;
  3005. default:
  3006. p = xdr_reserve_space(xdr, 4);
  3007. if (!p)
  3008. return nfserr_resource;
  3009. *p++ = cpu_to_be32(open->op_why_no_deleg);
  3010. }
  3011. break;
  3012. default:
  3013. BUG();
  3014. }
  3015. /* XXX save filehandle here */
  3016. return 0;
  3017. }
  3018. static __be32
  3019. nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_confirm *oc)
  3020. {
  3021. struct xdr_stream *xdr = &resp->xdr;
  3022. return nfsd4_encode_stateid(xdr, &oc->oc_resp_stateid);
  3023. }
  3024. static __be32
  3025. nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_downgrade *od)
  3026. {
  3027. struct xdr_stream *xdr = &resp->xdr;
  3028. return nfsd4_encode_stateid(xdr, &od->od_stateid);
  3029. }
  3030. static __be32 nfsd4_encode_splice_read(
  3031. struct nfsd4_compoundres *resp,
  3032. struct nfsd4_read *read,
  3033. struct file *file, unsigned long maxcount)
  3034. {
  3035. struct xdr_stream *xdr = &resp->xdr;
  3036. struct xdr_buf *buf = xdr->buf;
  3037. u32 eof;
  3038. long len;
  3039. int space_left;
  3040. __be32 nfserr;
  3041. __be32 *p = xdr->p - 2;
  3042. /* Make sure there will be room for padding if needed */
  3043. if (xdr->end - xdr->p < 1)
  3044. return nfserr_resource;
  3045. len = maxcount;
  3046. nfserr = nfsd_splice_read(read->rd_rqstp, file,
  3047. read->rd_offset, &maxcount);
  3048. if (nfserr) {
  3049. /*
  3050. * nfsd_splice_actor may have already messed with the
  3051. * page length; reset it so as not to confuse
  3052. * xdr_truncate_encode:
  3053. */
  3054. buf->page_len = 0;
  3055. return nfserr;
  3056. }
  3057. eof = nfsd_eof_on_read(len, maxcount, read->rd_offset,
  3058. d_inode(read->rd_fhp->fh_dentry)->i_size);
  3059. *(p++) = htonl(eof);
  3060. *(p++) = htonl(maxcount);
  3061. buf->page_len = maxcount;
  3062. buf->len += maxcount;
  3063. xdr->page_ptr += (buf->page_base + maxcount + PAGE_SIZE - 1)
  3064. / PAGE_SIZE;
  3065. /* Use rest of head for padding and remaining ops: */
  3066. buf->tail[0].iov_base = xdr->p;
  3067. buf->tail[0].iov_len = 0;
  3068. xdr->iov = buf->tail;
  3069. if (maxcount&3) {
  3070. int pad = 4 - (maxcount&3);
  3071. *(xdr->p++) = 0;
  3072. buf->tail[0].iov_base += maxcount&3;
  3073. buf->tail[0].iov_len = pad;
  3074. buf->len += pad;
  3075. }
  3076. space_left = min_t(int, (void *)xdr->end - (void *)xdr->p,
  3077. buf->buflen - buf->len);
  3078. buf->buflen = buf->len + space_left;
  3079. xdr->end = (__be32 *)((void *)xdr->end + space_left);
  3080. return 0;
  3081. }
  3082. static __be32 nfsd4_encode_readv(struct nfsd4_compoundres *resp,
  3083. struct nfsd4_read *read,
  3084. struct file *file, unsigned long maxcount)
  3085. {
  3086. struct xdr_stream *xdr = &resp->xdr;
  3087. u32 eof;
  3088. int v;
  3089. int starting_len = xdr->buf->len - 8;
  3090. long len;
  3091. int thislen;
  3092. __be32 nfserr;
  3093. __be32 tmp;
  3094. __be32 *p;
  3095. u32 zzz = 0;
  3096. int pad;
  3097. len = maxcount;
  3098. v = 0;
  3099. thislen = min_t(long, len, ((void *)xdr->end - (void *)xdr->p));
  3100. p = xdr_reserve_space(xdr, (thislen+3)&~3);
  3101. WARN_ON_ONCE(!p);
  3102. resp->rqstp->rq_vec[v].iov_base = p;
  3103. resp->rqstp->rq_vec[v].iov_len = thislen;
  3104. v++;
  3105. len -= thislen;
  3106. while (len) {
  3107. thislen = min_t(long, len, PAGE_SIZE);
  3108. p = xdr_reserve_space(xdr, (thislen+3)&~3);
  3109. WARN_ON_ONCE(!p);
  3110. resp->rqstp->rq_vec[v].iov_base = p;
  3111. resp->rqstp->rq_vec[v].iov_len = thislen;
  3112. v++;
  3113. len -= thislen;
  3114. }
  3115. read->rd_vlen = v;
  3116. len = maxcount;
  3117. nfserr = nfsd_readv(file, read->rd_offset, resp->rqstp->rq_vec,
  3118. read->rd_vlen, &maxcount);
  3119. if (nfserr)
  3120. return nfserr;
  3121. xdr_truncate_encode(xdr, starting_len + 8 + ((maxcount+3)&~3));
  3122. eof = nfsd_eof_on_read(len, maxcount, read->rd_offset,
  3123. d_inode(read->rd_fhp->fh_dentry)->i_size);
  3124. tmp = htonl(eof);
  3125. write_bytes_to_xdr_buf(xdr->buf, starting_len , &tmp, 4);
  3126. tmp = htonl(maxcount);
  3127. write_bytes_to_xdr_buf(xdr->buf, starting_len + 4, &tmp, 4);
  3128. pad = (maxcount&3) ? 4 - (maxcount&3) : 0;
  3129. write_bytes_to_xdr_buf(xdr->buf, starting_len + 8 + maxcount,
  3130. &zzz, pad);
  3131. return 0;
  3132. }
  3133. static __be32
  3134. nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
  3135. struct nfsd4_read *read)
  3136. {
  3137. unsigned long maxcount;
  3138. struct xdr_stream *xdr = &resp->xdr;
  3139. struct file *file = read->rd_filp;
  3140. int starting_len = xdr->buf->len;
  3141. struct raparms *ra = NULL;
  3142. __be32 *p;
  3143. p = xdr_reserve_space(xdr, 8); /* eof flag and byte count */
  3144. if (!p) {
  3145. WARN_ON_ONCE(test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags));
  3146. return nfserr_resource;
  3147. }
  3148. if (resp->xdr.buf->page_len &&
  3149. test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags)) {
  3150. WARN_ON_ONCE(1);
  3151. return nfserr_resource;
  3152. }
  3153. xdr_commit_encode(xdr);
  3154. maxcount = svc_max_payload(resp->rqstp);
  3155. maxcount = min_t(unsigned long, maxcount,
  3156. (xdr->buf->buflen - xdr->buf->len));
  3157. maxcount = min_t(unsigned long, maxcount, read->rd_length);
  3158. if (read->rd_tmp_file)
  3159. ra = nfsd_init_raparms(file);
  3160. if (file->f_op->splice_read &&
  3161. test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags))
  3162. nfserr = nfsd4_encode_splice_read(resp, read, file, maxcount);
  3163. else
  3164. nfserr = nfsd4_encode_readv(resp, read, file, maxcount);
  3165. if (ra)
  3166. nfsd_put_raparams(file, ra);
  3167. if (nfserr)
  3168. xdr_truncate_encode(xdr, starting_len);
  3169. return nfserr;
  3170. }
  3171. static __be32
  3172. nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readlink *readlink)
  3173. {
  3174. int maxcount;
  3175. __be32 wire_count;
  3176. int zero = 0;
  3177. struct xdr_stream *xdr = &resp->xdr;
  3178. int length_offset = xdr->buf->len;
  3179. __be32 *p;
  3180. p = xdr_reserve_space(xdr, 4);
  3181. if (!p)
  3182. return nfserr_resource;
  3183. maxcount = PAGE_SIZE;
  3184. p = xdr_reserve_space(xdr, maxcount);
  3185. if (!p)
  3186. return nfserr_resource;
  3187. /*
  3188. * XXX: By default, vfs_readlink() will truncate symlinks if they
  3189. * would overflow the buffer. Is this kosher in NFSv4? If not, one
  3190. * easy fix is: if vfs_readlink() precisely fills the buffer, assume
  3191. * that truncation occurred, and return NFS4ERR_RESOURCE.
  3192. */
  3193. nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp,
  3194. (char *)p, &maxcount);
  3195. if (nfserr == nfserr_isdir)
  3196. nfserr = nfserr_inval;
  3197. if (nfserr) {
  3198. xdr_truncate_encode(xdr, length_offset);
  3199. return nfserr;
  3200. }
  3201. wire_count = htonl(maxcount);
  3202. write_bytes_to_xdr_buf(xdr->buf, length_offset, &wire_count, 4);
  3203. xdr_truncate_encode(xdr, length_offset + 4 + ALIGN(maxcount, 4));
  3204. if (maxcount & 3)
  3205. write_bytes_to_xdr_buf(xdr->buf, length_offset + 4 + maxcount,
  3206. &zero, 4 - (maxcount&3));
  3207. return 0;
  3208. }
  3209. static __be32
  3210. nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readdir *readdir)
  3211. {
  3212. int maxcount;
  3213. int bytes_left;
  3214. loff_t offset;
  3215. __be64 wire_offset;
  3216. struct xdr_stream *xdr = &resp->xdr;
  3217. int starting_len = xdr->buf->len;
  3218. __be32 *p;
  3219. p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
  3220. if (!p)
  3221. return nfserr_resource;
  3222. /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
  3223. *p++ = cpu_to_be32(0);
  3224. *p++ = cpu_to_be32(0);
  3225. resp->xdr.buf->head[0].iov_len = ((char *)resp->xdr.p)
  3226. - (char *)resp->xdr.buf->head[0].iov_base;
  3227. /*
  3228. * Number of bytes left for directory entries allowing for the
  3229. * final 8 bytes of the readdir and a following failed op:
  3230. */
  3231. bytes_left = xdr->buf->buflen - xdr->buf->len
  3232. - COMPOUND_ERR_SLACK_SPACE - 8;
  3233. if (bytes_left < 0) {
  3234. nfserr = nfserr_resource;
  3235. goto err_no_verf;
  3236. }
  3237. maxcount = min_t(u32, readdir->rd_maxcount, INT_MAX);
  3238. /*
  3239. * Note the rfc defines rd_maxcount as the size of the
  3240. * READDIR4resok structure, which includes the verifier above
  3241. * and the 8 bytes encoded at the end of this function:
  3242. */
  3243. if (maxcount < 16) {
  3244. nfserr = nfserr_toosmall;
  3245. goto err_no_verf;
  3246. }
  3247. maxcount = min_t(int, maxcount-16, bytes_left);
  3248. /* RFC 3530 14.2.24 allows us to ignore dircount when it's 0: */
  3249. if (!readdir->rd_dircount)
  3250. readdir->rd_dircount = INT_MAX;
  3251. readdir->xdr = xdr;
  3252. readdir->rd_maxcount = maxcount;
  3253. readdir->common.err = 0;
  3254. readdir->cookie_offset = 0;
  3255. offset = readdir->rd_cookie;
  3256. nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
  3257. &offset,
  3258. &readdir->common, nfsd4_encode_dirent);
  3259. if (nfserr == nfs_ok &&
  3260. readdir->common.err == nfserr_toosmall &&
  3261. xdr->buf->len == starting_len + 8) {
  3262. /* nothing encoded; which limit did we hit?: */
  3263. if (maxcount - 16 < bytes_left)
  3264. /* It was the fault of rd_maxcount: */
  3265. nfserr = nfserr_toosmall;
  3266. else
  3267. /* We ran out of buffer space: */
  3268. nfserr = nfserr_resource;
  3269. }
  3270. if (nfserr)
  3271. goto err_no_verf;
  3272. if (readdir->cookie_offset) {
  3273. wire_offset = cpu_to_be64(offset);
  3274. write_bytes_to_xdr_buf(xdr->buf, readdir->cookie_offset,
  3275. &wire_offset, 8);
  3276. }
  3277. p = xdr_reserve_space(xdr, 8);
  3278. if (!p) {
  3279. WARN_ON_ONCE(1);
  3280. goto err_no_verf;
  3281. }
  3282. *p++ = 0; /* no more entries */
  3283. *p++ = htonl(readdir->common.err == nfserr_eof);
  3284. return 0;
  3285. err_no_verf:
  3286. xdr_truncate_encode(xdr, starting_len);
  3287. return nfserr;
  3288. }
  3289. static __be32
  3290. nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_remove *remove)
  3291. {
  3292. struct xdr_stream *xdr = &resp->xdr;
  3293. __be32 *p;
  3294. p = xdr_reserve_space(xdr, 20);
  3295. if (!p)
  3296. return nfserr_resource;
  3297. p = encode_cinfo(p, &remove->rm_cinfo);
  3298. return 0;
  3299. }
  3300. static __be32
  3301. nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_rename *rename)
  3302. {
  3303. struct xdr_stream *xdr = &resp->xdr;
  3304. __be32 *p;
  3305. p = xdr_reserve_space(xdr, 40);
  3306. if (!p)
  3307. return nfserr_resource;
  3308. p = encode_cinfo(p, &rename->rn_sinfo);
  3309. p = encode_cinfo(p, &rename->rn_tinfo);
  3310. return 0;
  3311. }
  3312. static __be32
  3313. nfsd4_do_encode_secinfo(struct xdr_stream *xdr, struct svc_export *exp)
  3314. {
  3315. u32 i, nflavs, supported;
  3316. struct exp_flavor_info *flavs;
  3317. struct exp_flavor_info def_flavs[2];
  3318. __be32 *p, *flavorsp;
  3319. static bool report = true;
  3320. if (exp->ex_nflavors) {
  3321. flavs = exp->ex_flavors;
  3322. nflavs = exp->ex_nflavors;
  3323. } else { /* Handling of some defaults in absence of real secinfo: */
  3324. flavs = def_flavs;
  3325. if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
  3326. nflavs = 2;
  3327. flavs[0].pseudoflavor = RPC_AUTH_UNIX;
  3328. flavs[1].pseudoflavor = RPC_AUTH_NULL;
  3329. } else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
  3330. nflavs = 1;
  3331. flavs[0].pseudoflavor
  3332. = svcauth_gss_flavor(exp->ex_client);
  3333. } else {
  3334. nflavs = 1;
  3335. flavs[0].pseudoflavor
  3336. = exp->ex_client->flavour->flavour;
  3337. }
  3338. }
  3339. supported = 0;
  3340. p = xdr_reserve_space(xdr, 4);
  3341. if (!p)
  3342. return nfserr_resource;
  3343. flavorsp = p++; /* to be backfilled later */
  3344. for (i = 0; i < nflavs; i++) {
  3345. rpc_authflavor_t pf = flavs[i].pseudoflavor;
  3346. struct rpcsec_gss_info info;
  3347. if (rpcauth_get_gssinfo(pf, &info) == 0) {
  3348. supported++;
  3349. p = xdr_reserve_space(xdr, 4 + 4 +
  3350. XDR_LEN(info.oid.len) + 4 + 4);
  3351. if (!p)
  3352. return nfserr_resource;
  3353. *p++ = cpu_to_be32(RPC_AUTH_GSS);
  3354. p = xdr_encode_opaque(p, info.oid.data, info.oid.len);
  3355. *p++ = cpu_to_be32(info.qop);
  3356. *p++ = cpu_to_be32(info.service);
  3357. } else if (pf < RPC_AUTH_MAXFLAVOR) {
  3358. supported++;
  3359. p = xdr_reserve_space(xdr, 4);
  3360. if (!p)
  3361. return nfserr_resource;
  3362. *p++ = cpu_to_be32(pf);
  3363. } else {
  3364. if (report)
  3365. pr_warn("NFS: SECINFO: security flavor %u "
  3366. "is not supported\n", pf);
  3367. }
  3368. }
  3369. if (nflavs != supported)
  3370. report = false;
  3371. *flavorsp = htonl(supported);
  3372. return 0;
  3373. }
  3374. static __be32
  3375. nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
  3376. struct nfsd4_secinfo *secinfo)
  3377. {
  3378. struct xdr_stream *xdr = &resp->xdr;
  3379. return nfsd4_do_encode_secinfo(xdr, secinfo->si_exp);
  3380. }
  3381. static __be32
  3382. nfsd4_encode_secinfo_no_name(struct nfsd4_compoundres *resp, __be32 nfserr,
  3383. struct nfsd4_secinfo_no_name *secinfo)
  3384. {
  3385. struct xdr_stream *xdr = &resp->xdr;
  3386. return nfsd4_do_encode_secinfo(xdr, secinfo->sin_exp);
  3387. }
  3388. /*
  3389. * The SETATTR encode routine is special -- it always encodes a bitmap,
  3390. * regardless of the error status.
  3391. */
  3392. static __be32
  3393. nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setattr *setattr)
  3394. {
  3395. struct xdr_stream *xdr = &resp->xdr;
  3396. __be32 *p;
  3397. p = xdr_reserve_space(xdr, 16);
  3398. if (!p)
  3399. return nfserr_resource;
  3400. if (nfserr) {
  3401. *p++ = cpu_to_be32(3);
  3402. *p++ = cpu_to_be32(0);
  3403. *p++ = cpu_to_be32(0);
  3404. *p++ = cpu_to_be32(0);
  3405. }
  3406. else {
  3407. *p++ = cpu_to_be32(3);
  3408. *p++ = cpu_to_be32(setattr->sa_bmval[0]);
  3409. *p++ = cpu_to_be32(setattr->sa_bmval[1]);
  3410. *p++ = cpu_to_be32(setattr->sa_bmval[2]);
  3411. }
  3412. return nfserr;
  3413. }
  3414. static __be32
  3415. nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setclientid *scd)
  3416. {
  3417. struct xdr_stream *xdr = &resp->xdr;
  3418. __be32 *p;
  3419. if (!nfserr) {
  3420. p = xdr_reserve_space(xdr, 8 + NFS4_VERIFIER_SIZE);
  3421. if (!p)
  3422. return nfserr_resource;
  3423. p = xdr_encode_opaque_fixed(p, &scd->se_clientid, 8);
  3424. p = xdr_encode_opaque_fixed(p, &scd->se_confirm,
  3425. NFS4_VERIFIER_SIZE);
  3426. }
  3427. else if (nfserr == nfserr_clid_inuse) {
  3428. p = xdr_reserve_space(xdr, 8);
  3429. if (!p)
  3430. return nfserr_resource;
  3431. *p++ = cpu_to_be32(0);
  3432. *p++ = cpu_to_be32(0);
  3433. }
  3434. return nfserr;
  3435. }
  3436. static __be32
  3437. nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_write *write)
  3438. {
  3439. struct xdr_stream *xdr = &resp->xdr;
  3440. __be32 *p;
  3441. p = xdr_reserve_space(xdr, 16);
  3442. if (!p)
  3443. return nfserr_resource;
  3444. *p++ = cpu_to_be32(write->wr_bytes_written);
  3445. *p++ = cpu_to_be32(write->wr_how_written);
  3446. p = xdr_encode_opaque_fixed(p, write->wr_verifier.data,
  3447. NFS4_VERIFIER_SIZE);
  3448. return 0;
  3449. }
  3450. static __be32
  3451. nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, __be32 nfserr,
  3452. struct nfsd4_exchange_id *exid)
  3453. {
  3454. struct xdr_stream *xdr = &resp->xdr;
  3455. __be32 *p;
  3456. char *major_id;
  3457. char *server_scope;
  3458. int major_id_sz;
  3459. int server_scope_sz;
  3460. uint64_t minor_id = 0;
  3461. major_id = utsname()->nodename;
  3462. major_id_sz = strlen(major_id);
  3463. server_scope = utsname()->nodename;
  3464. server_scope_sz = strlen(server_scope);
  3465. p = xdr_reserve_space(xdr,
  3466. 8 /* eir_clientid */ +
  3467. 4 /* eir_sequenceid */ +
  3468. 4 /* eir_flags */ +
  3469. 4 /* spr_how */);
  3470. if (!p)
  3471. return nfserr_resource;
  3472. p = xdr_encode_opaque_fixed(p, &exid->clientid, 8);
  3473. *p++ = cpu_to_be32(exid->seqid);
  3474. *p++ = cpu_to_be32(exid->flags);
  3475. *p++ = cpu_to_be32(exid->spa_how);
  3476. switch (exid->spa_how) {
  3477. case SP4_NONE:
  3478. break;
  3479. case SP4_MACH_CRED:
  3480. /* spo_must_enforce bitmap: */
  3481. nfserr = nfsd4_encode_bitmap(xdr,
  3482. exid->spo_must_enforce[0],
  3483. exid->spo_must_enforce[1],
  3484. exid->spo_must_enforce[2]);
  3485. if (nfserr)
  3486. return nfserr;
  3487. /* spo_must_allow bitmap: */
  3488. nfserr = nfsd4_encode_bitmap(xdr,
  3489. exid->spo_must_allow[0],
  3490. exid->spo_must_allow[1],
  3491. exid->spo_must_allow[2]);
  3492. if (nfserr)
  3493. return nfserr;
  3494. break;
  3495. default:
  3496. WARN_ON_ONCE(1);
  3497. }
  3498. p = xdr_reserve_space(xdr,
  3499. 8 /* so_minor_id */ +
  3500. 4 /* so_major_id.len */ +
  3501. (XDR_QUADLEN(major_id_sz) * 4) +
  3502. 4 /* eir_server_scope.len */ +
  3503. (XDR_QUADLEN(server_scope_sz) * 4) +
  3504. 4 /* eir_server_impl_id.count (0) */);
  3505. if (!p)
  3506. return nfserr_resource;
  3507. /* The server_owner struct */
  3508. p = xdr_encode_hyper(p, minor_id); /* Minor id */
  3509. /* major id */
  3510. p = xdr_encode_opaque(p, major_id, major_id_sz);
  3511. /* Server scope */
  3512. p = xdr_encode_opaque(p, server_scope, server_scope_sz);
  3513. /* Implementation id */
  3514. *p++ = cpu_to_be32(0); /* zero length nfs_impl_id4 array */
  3515. return 0;
  3516. }
  3517. static __be32
  3518. nfsd4_encode_create_session(struct nfsd4_compoundres *resp, __be32 nfserr,
  3519. struct nfsd4_create_session *sess)
  3520. {
  3521. struct xdr_stream *xdr = &resp->xdr;
  3522. __be32 *p;
  3523. p = xdr_reserve_space(xdr, 24);
  3524. if (!p)
  3525. return nfserr_resource;
  3526. p = xdr_encode_opaque_fixed(p, sess->sessionid.data,
  3527. NFS4_MAX_SESSIONID_LEN);
  3528. *p++ = cpu_to_be32(sess->seqid);
  3529. *p++ = cpu_to_be32(sess->flags);
  3530. p = xdr_reserve_space(xdr, 28);
  3531. if (!p)
  3532. return nfserr_resource;
  3533. *p++ = cpu_to_be32(0); /* headerpadsz */
  3534. *p++ = cpu_to_be32(sess->fore_channel.maxreq_sz);
  3535. *p++ = cpu_to_be32(sess->fore_channel.maxresp_sz);
  3536. *p++ = cpu_to_be32(sess->fore_channel.maxresp_cached);
  3537. *p++ = cpu_to_be32(sess->fore_channel.maxops);
  3538. *p++ = cpu_to_be32(sess->fore_channel.maxreqs);
  3539. *p++ = cpu_to_be32(sess->fore_channel.nr_rdma_attrs);
  3540. if (sess->fore_channel.nr_rdma_attrs) {
  3541. p = xdr_reserve_space(xdr, 4);
  3542. if (!p)
  3543. return nfserr_resource;
  3544. *p++ = cpu_to_be32(sess->fore_channel.rdma_attrs);
  3545. }
  3546. p = xdr_reserve_space(xdr, 28);
  3547. if (!p)
  3548. return nfserr_resource;
  3549. *p++ = cpu_to_be32(0); /* headerpadsz */
  3550. *p++ = cpu_to_be32(sess->back_channel.maxreq_sz);
  3551. *p++ = cpu_to_be32(sess->back_channel.maxresp_sz);
  3552. *p++ = cpu_to_be32(sess->back_channel.maxresp_cached);
  3553. *p++ = cpu_to_be32(sess->back_channel.maxops);
  3554. *p++ = cpu_to_be32(sess->back_channel.maxreqs);
  3555. *p++ = cpu_to_be32(sess->back_channel.nr_rdma_attrs);
  3556. if (sess->back_channel.nr_rdma_attrs) {
  3557. p = xdr_reserve_space(xdr, 4);
  3558. if (!p)
  3559. return nfserr_resource;
  3560. *p++ = cpu_to_be32(sess->back_channel.rdma_attrs);
  3561. }
  3562. return 0;
  3563. }
  3564. static __be32
  3565. nfsd4_encode_sequence(struct nfsd4_compoundres *resp, __be32 nfserr,
  3566. struct nfsd4_sequence *seq)
  3567. {
  3568. struct xdr_stream *xdr = &resp->xdr;
  3569. __be32 *p;
  3570. p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN + 20);
  3571. if (!p)
  3572. return nfserr_resource;
  3573. p = xdr_encode_opaque_fixed(p, seq->sessionid.data,
  3574. NFS4_MAX_SESSIONID_LEN);
  3575. *p++ = cpu_to_be32(seq->seqid);
  3576. *p++ = cpu_to_be32(seq->slotid);
  3577. /* Note slotid's are numbered from zero: */
  3578. *p++ = cpu_to_be32(seq->maxslots - 1); /* sr_highest_slotid */
  3579. *p++ = cpu_to_be32(seq->maxslots - 1); /* sr_target_highest_slotid */
  3580. *p++ = cpu_to_be32(seq->status_flags);
  3581. resp->cstate.data_offset = xdr->buf->len; /* DRC cache data pointer */
  3582. return 0;
  3583. }
  3584. static __be32
  3585. nfsd4_encode_test_stateid(struct nfsd4_compoundres *resp, __be32 nfserr,
  3586. struct nfsd4_test_stateid *test_stateid)
  3587. {
  3588. struct xdr_stream *xdr = &resp->xdr;
  3589. struct nfsd4_test_stateid_id *stateid, *next;
  3590. __be32 *p;
  3591. p = xdr_reserve_space(xdr, 4 + (4 * test_stateid->ts_num_ids));
  3592. if (!p)
  3593. return nfserr_resource;
  3594. *p++ = htonl(test_stateid->ts_num_ids);
  3595. list_for_each_entry_safe(stateid, next, &test_stateid->ts_stateid_list, ts_id_list) {
  3596. *p++ = stateid->ts_id_status;
  3597. }
  3598. return 0;
  3599. }
  3600. #ifdef CONFIG_NFSD_PNFS
  3601. static __be32
  3602. nfsd4_encode_getdeviceinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
  3603. struct nfsd4_getdeviceinfo *gdev)
  3604. {
  3605. struct xdr_stream *xdr = &resp->xdr;
  3606. const struct nfsd4_layout_ops *ops;
  3607. u32 starting_len = xdr->buf->len, needed_len;
  3608. __be32 *p;
  3609. p = xdr_reserve_space(xdr, 4);
  3610. if (!p)
  3611. return nfserr_resource;
  3612. *p++ = cpu_to_be32(gdev->gd_layout_type);
  3613. /* If maxcount is 0 then just update notifications */
  3614. if (gdev->gd_maxcount != 0) {
  3615. ops = nfsd4_layout_ops[gdev->gd_layout_type];
  3616. nfserr = ops->encode_getdeviceinfo(xdr, gdev);
  3617. if (nfserr) {
  3618. /*
  3619. * We don't bother to burden the layout drivers with
  3620. * enforcing gd_maxcount, just tell the client to
  3621. * come back with a bigger buffer if it's not enough.
  3622. */
  3623. if (xdr->buf->len + 4 > gdev->gd_maxcount)
  3624. goto toosmall;
  3625. return nfserr;
  3626. }
  3627. }
  3628. if (gdev->gd_notify_types) {
  3629. p = xdr_reserve_space(xdr, 4 + 4);
  3630. if (!p)
  3631. return nfserr_resource;
  3632. *p++ = cpu_to_be32(1); /* bitmap length */
  3633. *p++ = cpu_to_be32(gdev->gd_notify_types);
  3634. } else {
  3635. p = xdr_reserve_space(xdr, 4);
  3636. if (!p)
  3637. return nfserr_resource;
  3638. *p++ = 0;
  3639. }
  3640. return 0;
  3641. toosmall:
  3642. dprintk("%s: maxcount too small\n", __func__);
  3643. needed_len = xdr->buf->len + 4 /* notifications */;
  3644. xdr_truncate_encode(xdr, starting_len);
  3645. p = xdr_reserve_space(xdr, 4);
  3646. if (!p)
  3647. return nfserr_resource;
  3648. *p++ = cpu_to_be32(needed_len);
  3649. return nfserr_toosmall;
  3650. }
  3651. static __be32
  3652. nfsd4_encode_layoutget(struct nfsd4_compoundres *resp, __be32 nfserr,
  3653. struct nfsd4_layoutget *lgp)
  3654. {
  3655. struct xdr_stream *xdr = &resp->xdr;
  3656. const struct nfsd4_layout_ops *ops;
  3657. __be32 *p;
  3658. p = xdr_reserve_space(xdr, 36 + sizeof(stateid_opaque_t));
  3659. if (!p)
  3660. return nfserr_resource;
  3661. *p++ = cpu_to_be32(1); /* we always set return-on-close */
  3662. *p++ = cpu_to_be32(lgp->lg_sid.si_generation);
  3663. p = xdr_encode_opaque_fixed(p, &lgp->lg_sid.si_opaque,
  3664. sizeof(stateid_opaque_t));
  3665. *p++ = cpu_to_be32(1); /* we always return a single layout */
  3666. p = xdr_encode_hyper(p, lgp->lg_seg.offset);
  3667. p = xdr_encode_hyper(p, lgp->lg_seg.length);
  3668. *p++ = cpu_to_be32(lgp->lg_seg.iomode);
  3669. *p++ = cpu_to_be32(lgp->lg_layout_type);
  3670. ops = nfsd4_layout_ops[lgp->lg_layout_type];
  3671. return ops->encode_layoutget(xdr, lgp);
  3672. }
  3673. static __be32
  3674. nfsd4_encode_layoutcommit(struct nfsd4_compoundres *resp, __be32 nfserr,
  3675. struct nfsd4_layoutcommit *lcp)
  3676. {
  3677. struct xdr_stream *xdr = &resp->xdr;
  3678. __be32 *p;
  3679. p = xdr_reserve_space(xdr, 4);
  3680. if (!p)
  3681. return nfserr_resource;
  3682. *p++ = cpu_to_be32(lcp->lc_size_chg);
  3683. if (lcp->lc_size_chg) {
  3684. p = xdr_reserve_space(xdr, 8);
  3685. if (!p)
  3686. return nfserr_resource;
  3687. p = xdr_encode_hyper(p, lcp->lc_newsize);
  3688. }
  3689. return 0;
  3690. }
  3691. static __be32
  3692. nfsd4_encode_layoutreturn(struct nfsd4_compoundres *resp, __be32 nfserr,
  3693. struct nfsd4_layoutreturn *lrp)
  3694. {
  3695. struct xdr_stream *xdr = &resp->xdr;
  3696. __be32 *p;
  3697. p = xdr_reserve_space(xdr, 4);
  3698. if (!p)
  3699. return nfserr_resource;
  3700. *p++ = cpu_to_be32(lrp->lrs_present);
  3701. if (lrp->lrs_present)
  3702. return nfsd4_encode_stateid(xdr, &lrp->lr_sid);
  3703. return 0;
  3704. }
  3705. #endif /* CONFIG_NFSD_PNFS */
  3706. static __be32
  3707. nfsd42_encode_write_res(struct nfsd4_compoundres *resp, struct nfsd42_write_res *write)
  3708. {
  3709. __be32 *p;
  3710. p = xdr_reserve_space(&resp->xdr, 4 + 8 + 4 + NFS4_VERIFIER_SIZE);
  3711. if (!p)
  3712. return nfserr_resource;
  3713. *p++ = cpu_to_be32(0);
  3714. p = xdr_encode_hyper(p, write->wr_bytes_written);
  3715. *p++ = cpu_to_be32(write->wr_stable_how);
  3716. p = xdr_encode_opaque_fixed(p, write->wr_verifier.data,
  3717. NFS4_VERIFIER_SIZE);
  3718. return nfs_ok;
  3719. }
  3720. static __be32
  3721. nfsd4_encode_copy(struct nfsd4_compoundres *resp, __be32 nfserr,
  3722. struct nfsd4_copy *copy)
  3723. {
  3724. __be32 *p;
  3725. nfserr = nfsd42_encode_write_res(resp, &copy->cp_res);
  3726. if (nfserr)
  3727. return nfserr;
  3728. p = xdr_reserve_space(&resp->xdr, 4 + 4);
  3729. *p++ = cpu_to_be32(copy->cp_consecutive);
  3730. *p++ = cpu_to_be32(copy->cp_synchronous);
  3731. return 0;
  3732. }
  3733. static __be32
  3734. nfsd4_encode_seek(struct nfsd4_compoundres *resp, __be32 nfserr,
  3735. struct nfsd4_seek *seek)
  3736. {
  3737. __be32 *p;
  3738. p = xdr_reserve_space(&resp->xdr, 4 + 8);
  3739. *p++ = cpu_to_be32(seek->seek_eof);
  3740. p = xdr_encode_hyper(p, seek->seek_pos);
  3741. return 0;
  3742. }
  3743. static __be32
  3744. nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr, void *p)
  3745. {
  3746. return nfserr;
  3747. }
  3748. typedef __be32(* nfsd4_enc)(struct nfsd4_compoundres *, __be32, void *);
  3749. /*
  3750. * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
  3751. * since we don't need to filter out obsolete ops as this is
  3752. * done in the decoding phase.
  3753. */
  3754. static const nfsd4_enc nfsd4_enc_ops[] = {
  3755. [OP_ACCESS] = (nfsd4_enc)nfsd4_encode_access,
  3756. [OP_CLOSE] = (nfsd4_enc)nfsd4_encode_close,
  3757. [OP_COMMIT] = (nfsd4_enc)nfsd4_encode_commit,
  3758. [OP_CREATE] = (nfsd4_enc)nfsd4_encode_create,
  3759. [OP_DELEGPURGE] = (nfsd4_enc)nfsd4_encode_noop,
  3760. [OP_DELEGRETURN] = (nfsd4_enc)nfsd4_encode_noop,
  3761. [OP_GETATTR] = (nfsd4_enc)nfsd4_encode_getattr,
  3762. [OP_GETFH] = (nfsd4_enc)nfsd4_encode_getfh,
  3763. [OP_LINK] = (nfsd4_enc)nfsd4_encode_link,
  3764. [OP_LOCK] = (nfsd4_enc)nfsd4_encode_lock,
  3765. [OP_LOCKT] = (nfsd4_enc)nfsd4_encode_lockt,
  3766. [OP_LOCKU] = (nfsd4_enc)nfsd4_encode_locku,
  3767. [OP_LOOKUP] = (nfsd4_enc)nfsd4_encode_noop,
  3768. [OP_LOOKUPP] = (nfsd4_enc)nfsd4_encode_noop,
  3769. [OP_NVERIFY] = (nfsd4_enc)nfsd4_encode_noop,
  3770. [OP_OPEN] = (nfsd4_enc)nfsd4_encode_open,
  3771. [OP_OPENATTR] = (nfsd4_enc)nfsd4_encode_noop,
  3772. [OP_OPEN_CONFIRM] = (nfsd4_enc)nfsd4_encode_open_confirm,
  3773. [OP_OPEN_DOWNGRADE] = (nfsd4_enc)nfsd4_encode_open_downgrade,
  3774. [OP_PUTFH] = (nfsd4_enc)nfsd4_encode_noop,
  3775. [OP_PUTPUBFH] = (nfsd4_enc)nfsd4_encode_noop,
  3776. [OP_PUTROOTFH] = (nfsd4_enc)nfsd4_encode_noop,
  3777. [OP_READ] = (nfsd4_enc)nfsd4_encode_read,
  3778. [OP_READDIR] = (nfsd4_enc)nfsd4_encode_readdir,
  3779. [OP_READLINK] = (nfsd4_enc)nfsd4_encode_readlink,
  3780. [OP_REMOVE] = (nfsd4_enc)nfsd4_encode_remove,
  3781. [OP_RENAME] = (nfsd4_enc)nfsd4_encode_rename,
  3782. [OP_RENEW] = (nfsd4_enc)nfsd4_encode_noop,
  3783. [OP_RESTOREFH] = (nfsd4_enc)nfsd4_encode_noop,
  3784. [OP_SAVEFH] = (nfsd4_enc)nfsd4_encode_noop,
  3785. [OP_SECINFO] = (nfsd4_enc)nfsd4_encode_secinfo,
  3786. [OP_SETATTR] = (nfsd4_enc)nfsd4_encode_setattr,
  3787. [OP_SETCLIENTID] = (nfsd4_enc)nfsd4_encode_setclientid,
  3788. [OP_SETCLIENTID_CONFIRM] = (nfsd4_enc)nfsd4_encode_noop,
  3789. [OP_VERIFY] = (nfsd4_enc)nfsd4_encode_noop,
  3790. [OP_WRITE] = (nfsd4_enc)nfsd4_encode_write,
  3791. [OP_RELEASE_LOCKOWNER] = (nfsd4_enc)nfsd4_encode_noop,
  3792. /* NFSv4.1 operations */
  3793. [OP_BACKCHANNEL_CTL] = (nfsd4_enc)nfsd4_encode_noop,
  3794. [OP_BIND_CONN_TO_SESSION] = (nfsd4_enc)nfsd4_encode_bind_conn_to_session,
  3795. [OP_EXCHANGE_ID] = (nfsd4_enc)nfsd4_encode_exchange_id,
  3796. [OP_CREATE_SESSION] = (nfsd4_enc)nfsd4_encode_create_session,
  3797. [OP_DESTROY_SESSION] = (nfsd4_enc)nfsd4_encode_noop,
  3798. [OP_FREE_STATEID] = (nfsd4_enc)nfsd4_encode_noop,
  3799. [OP_GET_DIR_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
  3800. #ifdef CONFIG_NFSD_PNFS
  3801. [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_getdeviceinfo,
  3802. [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
  3803. [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_layoutcommit,
  3804. [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_layoutget,
  3805. [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_layoutreturn,
  3806. #else
  3807. [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_noop,
  3808. [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
  3809. [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_noop,
  3810. [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_noop,
  3811. [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_noop,
  3812. #endif
  3813. [OP_SECINFO_NO_NAME] = (nfsd4_enc)nfsd4_encode_secinfo_no_name,
  3814. [OP_SEQUENCE] = (nfsd4_enc)nfsd4_encode_sequence,
  3815. [OP_SET_SSV] = (nfsd4_enc)nfsd4_encode_noop,
  3816. [OP_TEST_STATEID] = (nfsd4_enc)nfsd4_encode_test_stateid,
  3817. [OP_WANT_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
  3818. [OP_DESTROY_CLIENTID] = (nfsd4_enc)nfsd4_encode_noop,
  3819. [OP_RECLAIM_COMPLETE] = (nfsd4_enc)nfsd4_encode_noop,
  3820. /* NFSv4.2 operations */
  3821. [OP_ALLOCATE] = (nfsd4_enc)nfsd4_encode_noop,
  3822. [OP_COPY] = (nfsd4_enc)nfsd4_encode_copy,
  3823. [OP_COPY_NOTIFY] = (nfsd4_enc)nfsd4_encode_noop,
  3824. [OP_DEALLOCATE] = (nfsd4_enc)nfsd4_encode_noop,
  3825. [OP_IO_ADVISE] = (nfsd4_enc)nfsd4_encode_noop,
  3826. [OP_LAYOUTERROR] = (nfsd4_enc)nfsd4_encode_noop,
  3827. [OP_LAYOUTSTATS] = (nfsd4_enc)nfsd4_encode_noop,
  3828. [OP_OFFLOAD_CANCEL] = (nfsd4_enc)nfsd4_encode_noop,
  3829. [OP_OFFLOAD_STATUS] = (nfsd4_enc)nfsd4_encode_noop,
  3830. [OP_READ_PLUS] = (nfsd4_enc)nfsd4_encode_noop,
  3831. [OP_SEEK] = (nfsd4_enc)nfsd4_encode_seek,
  3832. [OP_WRITE_SAME] = (nfsd4_enc)nfsd4_encode_noop,
  3833. [OP_CLONE] = (nfsd4_enc)nfsd4_encode_noop,
  3834. };
  3835. /*
  3836. * Calculate whether we still have space to encode repsize bytes.
  3837. * There are two considerations:
  3838. * - For NFS versions >=4.1, the size of the reply must stay within
  3839. * session limits
  3840. * - For all NFS versions, we must stay within limited preallocated
  3841. * buffer space.
  3842. *
  3843. * This is called before the operation is processed, so can only provide
  3844. * an upper estimate. For some nonidempotent operations (such as
  3845. * getattr), it's not necessarily a problem if that estimate is wrong,
  3846. * as we can fail it after processing without significant side effects.
  3847. */
  3848. __be32 nfsd4_check_resp_size(struct nfsd4_compoundres *resp, u32 respsize)
  3849. {
  3850. struct xdr_buf *buf = &resp->rqstp->rq_res;
  3851. struct nfsd4_slot *slot = resp->cstate.slot;
  3852. if (buf->len + respsize <= buf->buflen)
  3853. return nfs_ok;
  3854. if (!nfsd4_has_session(&resp->cstate))
  3855. return nfserr_resource;
  3856. if (slot->sl_flags & NFSD4_SLOT_CACHETHIS) {
  3857. WARN_ON_ONCE(1);
  3858. return nfserr_rep_too_big_to_cache;
  3859. }
  3860. return nfserr_rep_too_big;
  3861. }
  3862. void
  3863. nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
  3864. {
  3865. struct xdr_stream *xdr = &resp->xdr;
  3866. struct nfs4_stateowner *so = resp->cstate.replay_owner;
  3867. struct svc_rqst *rqstp = resp->rqstp;
  3868. const struct nfsd4_operation *opdesc = op->opdesc;
  3869. int post_err_offset;
  3870. nfsd4_enc encoder;
  3871. __be32 *p;
  3872. p = xdr_reserve_space(xdr, 8);
  3873. if (!p) {
  3874. WARN_ON_ONCE(1);
  3875. return;
  3876. }
  3877. *p++ = cpu_to_be32(op->opnum);
  3878. post_err_offset = xdr->buf->len;
  3879. if (op->opnum == OP_ILLEGAL)
  3880. goto status;
  3881. if (op->status && opdesc &&
  3882. !(opdesc->op_flags & OP_NONTRIVIAL_ERROR_ENCODE))
  3883. goto status;
  3884. BUG_ON(op->opnum < 0 || op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
  3885. !nfsd4_enc_ops[op->opnum]);
  3886. encoder = nfsd4_enc_ops[op->opnum];
  3887. op->status = encoder(resp, op->status, &op->u);
  3888. if (opdesc && opdesc->op_release)
  3889. opdesc->op_release(&op->u);
  3890. xdr_commit_encode(xdr);
  3891. /* nfsd4_check_resp_size guarantees enough room for error status */
  3892. if (!op->status) {
  3893. int space_needed = 0;
  3894. if (!nfsd4_last_compound_op(rqstp))
  3895. space_needed = COMPOUND_ERR_SLACK_SPACE;
  3896. op->status = nfsd4_check_resp_size(resp, space_needed);
  3897. }
  3898. if (op->status == nfserr_resource && nfsd4_has_session(&resp->cstate)) {
  3899. struct nfsd4_slot *slot = resp->cstate.slot;
  3900. if (slot->sl_flags & NFSD4_SLOT_CACHETHIS)
  3901. op->status = nfserr_rep_too_big_to_cache;
  3902. else
  3903. op->status = nfserr_rep_too_big;
  3904. }
  3905. if (op->status == nfserr_resource ||
  3906. op->status == nfserr_rep_too_big ||
  3907. op->status == nfserr_rep_too_big_to_cache) {
  3908. /*
  3909. * The operation may have already been encoded or
  3910. * partially encoded. No op returns anything additional
  3911. * in the case of one of these three errors, so we can
  3912. * just truncate back to after the status. But it's a
  3913. * bug if we had to do this on a non-idempotent op:
  3914. */
  3915. warn_on_nonidempotent_op(op);
  3916. xdr_truncate_encode(xdr, post_err_offset);
  3917. }
  3918. if (so) {
  3919. int len = xdr->buf->len - post_err_offset;
  3920. so->so_replay.rp_status = op->status;
  3921. so->so_replay.rp_buflen = len;
  3922. read_bytes_from_xdr_buf(xdr->buf, post_err_offset,
  3923. so->so_replay.rp_buf, len);
  3924. }
  3925. status:
  3926. /* Note that op->status is already in network byte order: */
  3927. write_bytes_to_xdr_buf(xdr->buf, post_err_offset - 4, &op->status, 4);
  3928. }
  3929. /*
  3930. * Encode the reply stored in the stateowner reply cache
  3931. *
  3932. * XDR note: do not encode rp->rp_buflen: the buffer contains the
  3933. * previously sent already encoded operation.
  3934. */
  3935. void
  3936. nfsd4_encode_replay(struct xdr_stream *xdr, struct nfsd4_op *op)
  3937. {
  3938. __be32 *p;
  3939. struct nfs4_replay *rp = op->replay;
  3940. BUG_ON(!rp);
  3941. p = xdr_reserve_space(xdr, 8 + rp->rp_buflen);
  3942. if (!p) {
  3943. WARN_ON_ONCE(1);
  3944. return;
  3945. }
  3946. *p++ = cpu_to_be32(op->opnum);
  3947. *p++ = rp->rp_status; /* already xdr'ed */
  3948. p = xdr_encode_opaque_fixed(p, rp->rp_buf, rp->rp_buflen);
  3949. }
  3950. int
  3951. nfs4svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p)
  3952. {
  3953. return xdr_ressize_check(rqstp, p);
  3954. }
  3955. void nfsd4_release_compoundargs(struct svc_rqst *rqstp)
  3956. {
  3957. struct nfsd4_compoundargs *args = rqstp->rq_argp;
  3958. if (args->ops != args->iops) {
  3959. kfree(args->ops);
  3960. args->ops = args->iops;
  3961. }
  3962. kfree(args->tmpp);
  3963. args->tmpp = NULL;
  3964. while (args->to_free) {
  3965. struct svcxdr_tmpbuf *tb = args->to_free;
  3966. args->to_free = tb->next;
  3967. kfree(tb);
  3968. }
  3969. }
  3970. int
  3971. nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, __be32 *p)
  3972. {
  3973. struct nfsd4_compoundargs *args = rqstp->rq_argp;
  3974. if (rqstp->rq_arg.head[0].iov_len % 4) {
  3975. /* client is nuts */
  3976. dprintk("%s: compound not properly padded! (peeraddr=%pISc xid=0x%x)",
  3977. __func__, svc_addr(rqstp), be32_to_cpu(rqstp->rq_xid));
  3978. return 0;
  3979. }
  3980. args->p = p;
  3981. args->end = rqstp->rq_arg.head[0].iov_base + rqstp->rq_arg.head[0].iov_len;
  3982. args->pagelist = rqstp->rq_arg.pages;
  3983. args->pagelen = rqstp->rq_arg.page_len;
  3984. args->tail = false;
  3985. args->tmpp = NULL;
  3986. args->to_free = NULL;
  3987. args->ops = args->iops;
  3988. args->rqstp = rqstp;
  3989. return !nfsd4_decode_compound(args);
  3990. }
  3991. int
  3992. nfs4svc_encode_compoundres(struct svc_rqst *rqstp, __be32 *p)
  3993. {
  3994. /*
  3995. * All that remains is to write the tag and operation count...
  3996. */
  3997. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  3998. struct xdr_buf *buf = resp->xdr.buf;
  3999. WARN_ON_ONCE(buf->len != buf->head[0].iov_len + buf->page_len +
  4000. buf->tail[0].iov_len);
  4001. rqstp->rq_next_page = resp->xdr.page_ptr + 1;
  4002. p = resp->tagp;
  4003. *p++ = htonl(resp->taglen);
  4004. memcpy(p, resp->tag, resp->taglen);
  4005. p += XDR_QUADLEN(resp->taglen);
  4006. *p++ = htonl(resp->opcnt);
  4007. nfsd4_sequence_done(resp);
  4008. return 1;
  4009. }
  4010. /*
  4011. * Local variables:
  4012. * c-basic-offset: 8
  4013. * End:
  4014. */