nfs4xdr.c 112 KB

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