nfs4xdr.c 116 KB

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