nfs4xdr.c 115 KB

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