nfs4xdr.c 111 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448
  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 + 4);
  954. SAVEMEM(rename->rn_sname, rename->rn_snamelen);
  955. rename->rn_tnamelen = be32_to_cpup(p++);
  956. READ_BUF(rename->rn_tnamelen);
  957. SAVEMEM(rename->rn_tname, rename->rn_tnamelen);
  958. if ((status = check_filename(rename->rn_sname, rename->rn_snamelen)))
  959. return status;
  960. if ((status = check_filename(rename->rn_tname, rename->rn_tnamelen)))
  961. return status;
  962. DECODE_TAIL;
  963. }
  964. static __be32
  965. nfsd4_decode_renew(struct nfsd4_compoundargs *argp, clientid_t *clientid)
  966. {
  967. DECODE_HEAD;
  968. if (argp->minorversion >= 1)
  969. return nfserr_notsupp;
  970. READ_BUF(sizeof(clientid_t));
  971. COPYMEM(clientid, sizeof(clientid_t));
  972. DECODE_TAIL;
  973. }
  974. static __be32
  975. nfsd4_decode_secinfo(struct nfsd4_compoundargs *argp,
  976. struct nfsd4_secinfo *secinfo)
  977. {
  978. DECODE_HEAD;
  979. READ_BUF(4);
  980. secinfo->si_namelen = be32_to_cpup(p++);
  981. READ_BUF(secinfo->si_namelen);
  982. SAVEMEM(secinfo->si_name, secinfo->si_namelen);
  983. status = check_filename(secinfo->si_name, secinfo->si_namelen);
  984. if (status)
  985. return status;
  986. DECODE_TAIL;
  987. }
  988. static __be32
  989. nfsd4_decode_secinfo_no_name(struct nfsd4_compoundargs *argp,
  990. struct nfsd4_secinfo_no_name *sin)
  991. {
  992. DECODE_HEAD;
  993. READ_BUF(4);
  994. sin->sin_style = be32_to_cpup(p++);
  995. DECODE_TAIL;
  996. }
  997. static __be32
  998. nfsd4_decode_setattr(struct nfsd4_compoundargs *argp, struct nfsd4_setattr *setattr)
  999. {
  1000. __be32 status;
  1001. status = nfsd4_decode_stateid(argp, &setattr->sa_stateid);
  1002. if (status)
  1003. return status;
  1004. return nfsd4_decode_fattr(argp, setattr->sa_bmval, &setattr->sa_iattr,
  1005. &setattr->sa_acl, &setattr->sa_label);
  1006. }
  1007. static __be32
  1008. nfsd4_decode_setclientid(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid *setclientid)
  1009. {
  1010. DECODE_HEAD;
  1011. if (argp->minorversion >= 1)
  1012. return nfserr_notsupp;
  1013. READ_BUF(NFS4_VERIFIER_SIZE);
  1014. COPYMEM(setclientid->se_verf.data, NFS4_VERIFIER_SIZE);
  1015. status = nfsd4_decode_opaque(argp, &setclientid->se_name);
  1016. if (status)
  1017. return nfserr_bad_xdr;
  1018. READ_BUF(8);
  1019. setclientid->se_callback_prog = be32_to_cpup(p++);
  1020. setclientid->se_callback_netid_len = be32_to_cpup(p++);
  1021. READ_BUF(setclientid->se_callback_netid_len + 4);
  1022. SAVEMEM(setclientid->se_callback_netid_val, setclientid->se_callback_netid_len);
  1023. setclientid->se_callback_addr_len = be32_to_cpup(p++);
  1024. READ_BUF(setclientid->se_callback_addr_len + 4);
  1025. SAVEMEM(setclientid->se_callback_addr_val, setclientid->se_callback_addr_len);
  1026. setclientid->se_callback_ident = be32_to_cpup(p++);
  1027. DECODE_TAIL;
  1028. }
  1029. static __be32
  1030. nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid_confirm *scd_c)
  1031. {
  1032. DECODE_HEAD;
  1033. if (argp->minorversion >= 1)
  1034. return nfserr_notsupp;
  1035. READ_BUF(8 + NFS4_VERIFIER_SIZE);
  1036. COPYMEM(&scd_c->sc_clientid, 8);
  1037. COPYMEM(&scd_c->sc_confirm, NFS4_VERIFIER_SIZE);
  1038. DECODE_TAIL;
  1039. }
  1040. /* Also used for NVERIFY */
  1041. static __be32
  1042. nfsd4_decode_verify(struct nfsd4_compoundargs *argp, struct nfsd4_verify *verify)
  1043. {
  1044. DECODE_HEAD;
  1045. if ((status = nfsd4_decode_bitmap(argp, verify->ve_bmval)))
  1046. goto out;
  1047. /* For convenience's sake, we compare raw xdr'd attributes in
  1048. * nfsd4_proc_verify */
  1049. READ_BUF(4);
  1050. verify->ve_attrlen = be32_to_cpup(p++);
  1051. READ_BUF(verify->ve_attrlen);
  1052. SAVEMEM(verify->ve_attrval, verify->ve_attrlen);
  1053. DECODE_TAIL;
  1054. }
  1055. static __be32
  1056. nfsd4_decode_write(struct nfsd4_compoundargs *argp, struct nfsd4_write *write)
  1057. {
  1058. int avail;
  1059. int len;
  1060. DECODE_HEAD;
  1061. status = nfsd4_decode_stateid(argp, &write->wr_stateid);
  1062. if (status)
  1063. return status;
  1064. READ_BUF(16);
  1065. p = xdr_decode_hyper(p, &write->wr_offset);
  1066. write->wr_stable_how = be32_to_cpup(p++);
  1067. if (write->wr_stable_how > 2)
  1068. goto xdr_error;
  1069. write->wr_buflen = be32_to_cpup(p++);
  1070. /* Sorry .. no magic macros for this.. *
  1071. * READ_BUF(write->wr_buflen);
  1072. * SAVEMEM(write->wr_buf, write->wr_buflen);
  1073. */
  1074. avail = (char*)argp->end - (char*)argp->p;
  1075. if (avail + argp->pagelen < write->wr_buflen) {
  1076. dprintk("NFSD: xdr error (%s:%d)\n",
  1077. __FILE__, __LINE__);
  1078. goto xdr_error;
  1079. }
  1080. write->wr_head.iov_base = p;
  1081. write->wr_head.iov_len = avail;
  1082. write->wr_pagelist = argp->pagelist;
  1083. len = XDR_QUADLEN(write->wr_buflen) << 2;
  1084. if (len >= avail) {
  1085. int pages;
  1086. len -= avail;
  1087. pages = len >> PAGE_SHIFT;
  1088. argp->pagelist += pages;
  1089. argp->pagelen -= pages * PAGE_SIZE;
  1090. len -= pages * PAGE_SIZE;
  1091. argp->p = (__be32 *)page_address(argp->pagelist[0]);
  1092. argp->pagelist++;
  1093. argp->end = argp->p + XDR_QUADLEN(PAGE_SIZE);
  1094. }
  1095. argp->p += XDR_QUADLEN(len);
  1096. DECODE_TAIL;
  1097. }
  1098. static __be32
  1099. nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp, struct nfsd4_release_lockowner *rlockowner)
  1100. {
  1101. DECODE_HEAD;
  1102. if (argp->minorversion >= 1)
  1103. return nfserr_notsupp;
  1104. READ_BUF(12);
  1105. COPYMEM(&rlockowner->rl_clientid, sizeof(clientid_t));
  1106. rlockowner->rl_owner.len = be32_to_cpup(p++);
  1107. READ_BUF(rlockowner->rl_owner.len);
  1108. READMEM(rlockowner->rl_owner.data, rlockowner->rl_owner.len);
  1109. if (argp->minorversion && !zero_clientid(&rlockowner->rl_clientid))
  1110. return nfserr_inval;
  1111. DECODE_TAIL;
  1112. }
  1113. static __be32
  1114. nfsd4_decode_exchange_id(struct nfsd4_compoundargs *argp,
  1115. struct nfsd4_exchange_id *exid)
  1116. {
  1117. int dummy, tmp;
  1118. DECODE_HEAD;
  1119. READ_BUF(NFS4_VERIFIER_SIZE);
  1120. COPYMEM(exid->verifier.data, NFS4_VERIFIER_SIZE);
  1121. status = nfsd4_decode_opaque(argp, &exid->clname);
  1122. if (status)
  1123. return nfserr_bad_xdr;
  1124. READ_BUF(4);
  1125. exid->flags = be32_to_cpup(p++);
  1126. /* Ignore state_protect4_a */
  1127. READ_BUF(4);
  1128. exid->spa_how = be32_to_cpup(p++);
  1129. switch (exid->spa_how) {
  1130. case SP4_NONE:
  1131. break;
  1132. case SP4_MACH_CRED:
  1133. /* spo_must_enforce */
  1134. READ_BUF(4);
  1135. dummy = be32_to_cpup(p++);
  1136. READ_BUF(dummy * 4);
  1137. p += dummy;
  1138. /* spo_must_allow */
  1139. READ_BUF(4);
  1140. dummy = be32_to_cpup(p++);
  1141. READ_BUF(dummy * 4);
  1142. p += dummy;
  1143. break;
  1144. case SP4_SSV:
  1145. /* ssp_ops */
  1146. READ_BUF(4);
  1147. dummy = be32_to_cpup(p++);
  1148. READ_BUF(dummy * 4);
  1149. p += dummy;
  1150. READ_BUF(4);
  1151. dummy = be32_to_cpup(p++);
  1152. READ_BUF(dummy * 4);
  1153. p += dummy;
  1154. /* ssp_hash_algs<> */
  1155. READ_BUF(4);
  1156. tmp = be32_to_cpup(p++);
  1157. while (tmp--) {
  1158. READ_BUF(4);
  1159. dummy = be32_to_cpup(p++);
  1160. READ_BUF(dummy);
  1161. p += XDR_QUADLEN(dummy);
  1162. }
  1163. /* ssp_encr_algs<> */
  1164. READ_BUF(4);
  1165. tmp = be32_to_cpup(p++);
  1166. while (tmp--) {
  1167. READ_BUF(4);
  1168. dummy = be32_to_cpup(p++);
  1169. READ_BUF(dummy);
  1170. p += XDR_QUADLEN(dummy);
  1171. }
  1172. /* ssp_window and ssp_num_gss_handles */
  1173. READ_BUF(8);
  1174. dummy = be32_to_cpup(p++);
  1175. dummy = be32_to_cpup(p++);
  1176. break;
  1177. default:
  1178. goto xdr_error;
  1179. }
  1180. /* Ignore Implementation ID */
  1181. READ_BUF(4); /* nfs_impl_id4 array length */
  1182. dummy = be32_to_cpup(p++);
  1183. if (dummy > 1)
  1184. goto xdr_error;
  1185. if (dummy == 1) {
  1186. /* nii_domain */
  1187. READ_BUF(4);
  1188. dummy = be32_to_cpup(p++);
  1189. READ_BUF(dummy);
  1190. p += XDR_QUADLEN(dummy);
  1191. /* nii_name */
  1192. READ_BUF(4);
  1193. dummy = be32_to_cpup(p++);
  1194. READ_BUF(dummy);
  1195. p += XDR_QUADLEN(dummy);
  1196. /* nii_date */
  1197. READ_BUF(12);
  1198. p += 3;
  1199. }
  1200. DECODE_TAIL;
  1201. }
  1202. static __be32
  1203. nfsd4_decode_create_session(struct nfsd4_compoundargs *argp,
  1204. struct nfsd4_create_session *sess)
  1205. {
  1206. DECODE_HEAD;
  1207. u32 dummy;
  1208. READ_BUF(16);
  1209. COPYMEM(&sess->clientid, 8);
  1210. sess->seqid = be32_to_cpup(p++);
  1211. sess->flags = be32_to_cpup(p++);
  1212. /* Fore channel attrs */
  1213. READ_BUF(28);
  1214. dummy = be32_to_cpup(p++); /* headerpadsz is always 0 */
  1215. sess->fore_channel.maxreq_sz = be32_to_cpup(p++);
  1216. sess->fore_channel.maxresp_sz = be32_to_cpup(p++);
  1217. sess->fore_channel.maxresp_cached = be32_to_cpup(p++);
  1218. sess->fore_channel.maxops = be32_to_cpup(p++);
  1219. sess->fore_channel.maxreqs = be32_to_cpup(p++);
  1220. sess->fore_channel.nr_rdma_attrs = be32_to_cpup(p++);
  1221. if (sess->fore_channel.nr_rdma_attrs == 1) {
  1222. READ_BUF(4);
  1223. sess->fore_channel.rdma_attrs = be32_to_cpup(p++);
  1224. } else if (sess->fore_channel.nr_rdma_attrs > 1) {
  1225. dprintk("Too many fore channel attr bitmaps!\n");
  1226. goto xdr_error;
  1227. }
  1228. /* Back channel attrs */
  1229. READ_BUF(28);
  1230. dummy = be32_to_cpup(p++); /* headerpadsz is always 0 */
  1231. sess->back_channel.maxreq_sz = be32_to_cpup(p++);
  1232. sess->back_channel.maxresp_sz = be32_to_cpup(p++);
  1233. sess->back_channel.maxresp_cached = be32_to_cpup(p++);
  1234. sess->back_channel.maxops = be32_to_cpup(p++);
  1235. sess->back_channel.maxreqs = be32_to_cpup(p++);
  1236. sess->back_channel.nr_rdma_attrs = be32_to_cpup(p++);
  1237. if (sess->back_channel.nr_rdma_attrs == 1) {
  1238. READ_BUF(4);
  1239. sess->back_channel.rdma_attrs = be32_to_cpup(p++);
  1240. } else if (sess->back_channel.nr_rdma_attrs > 1) {
  1241. dprintk("Too many back channel attr bitmaps!\n");
  1242. goto xdr_error;
  1243. }
  1244. READ_BUF(4);
  1245. sess->callback_prog = be32_to_cpup(p++);
  1246. nfsd4_decode_cb_sec(argp, &sess->cb_sec);
  1247. DECODE_TAIL;
  1248. }
  1249. static __be32
  1250. nfsd4_decode_destroy_session(struct nfsd4_compoundargs *argp,
  1251. struct nfsd4_destroy_session *destroy_session)
  1252. {
  1253. DECODE_HEAD;
  1254. READ_BUF(NFS4_MAX_SESSIONID_LEN);
  1255. COPYMEM(destroy_session->sessionid.data, NFS4_MAX_SESSIONID_LEN);
  1256. DECODE_TAIL;
  1257. }
  1258. static __be32
  1259. nfsd4_decode_free_stateid(struct nfsd4_compoundargs *argp,
  1260. struct nfsd4_free_stateid *free_stateid)
  1261. {
  1262. DECODE_HEAD;
  1263. READ_BUF(sizeof(stateid_t));
  1264. free_stateid->fr_stateid.si_generation = be32_to_cpup(p++);
  1265. COPYMEM(&free_stateid->fr_stateid.si_opaque, sizeof(stateid_opaque_t));
  1266. DECODE_TAIL;
  1267. }
  1268. static __be32
  1269. nfsd4_decode_sequence(struct nfsd4_compoundargs *argp,
  1270. struct nfsd4_sequence *seq)
  1271. {
  1272. DECODE_HEAD;
  1273. READ_BUF(NFS4_MAX_SESSIONID_LEN + 16);
  1274. COPYMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
  1275. seq->seqid = be32_to_cpup(p++);
  1276. seq->slotid = be32_to_cpup(p++);
  1277. seq->maxslots = be32_to_cpup(p++);
  1278. seq->cachethis = be32_to_cpup(p++);
  1279. DECODE_TAIL;
  1280. }
  1281. static __be32
  1282. nfsd4_decode_test_stateid(struct nfsd4_compoundargs *argp, struct nfsd4_test_stateid *test_stateid)
  1283. {
  1284. int i;
  1285. __be32 *p, status;
  1286. struct nfsd4_test_stateid_id *stateid;
  1287. READ_BUF(4);
  1288. test_stateid->ts_num_ids = ntohl(*p++);
  1289. INIT_LIST_HEAD(&test_stateid->ts_stateid_list);
  1290. for (i = 0; i < test_stateid->ts_num_ids; i++) {
  1291. stateid = svcxdr_tmpalloc(argp, sizeof(*stateid));
  1292. if (!stateid) {
  1293. status = nfserrno(-ENOMEM);
  1294. goto out;
  1295. }
  1296. INIT_LIST_HEAD(&stateid->ts_id_list);
  1297. list_add_tail(&stateid->ts_id_list, &test_stateid->ts_stateid_list);
  1298. status = nfsd4_decode_stateid(argp, &stateid->ts_id_stateid);
  1299. if (status)
  1300. goto out;
  1301. }
  1302. status = 0;
  1303. out:
  1304. return status;
  1305. xdr_error:
  1306. dprintk("NFSD: xdr error (%s:%d)\n", __FILE__, __LINE__);
  1307. status = nfserr_bad_xdr;
  1308. goto out;
  1309. }
  1310. static __be32 nfsd4_decode_destroy_clientid(struct nfsd4_compoundargs *argp, struct nfsd4_destroy_clientid *dc)
  1311. {
  1312. DECODE_HEAD;
  1313. READ_BUF(8);
  1314. COPYMEM(&dc->clientid, 8);
  1315. DECODE_TAIL;
  1316. }
  1317. static __be32 nfsd4_decode_reclaim_complete(struct nfsd4_compoundargs *argp, struct nfsd4_reclaim_complete *rc)
  1318. {
  1319. DECODE_HEAD;
  1320. READ_BUF(4);
  1321. rc->rca_one_fs = be32_to_cpup(p++);
  1322. DECODE_TAIL;
  1323. }
  1324. #ifdef CONFIG_NFSD_PNFS
  1325. static __be32
  1326. nfsd4_decode_getdeviceinfo(struct nfsd4_compoundargs *argp,
  1327. struct nfsd4_getdeviceinfo *gdev)
  1328. {
  1329. DECODE_HEAD;
  1330. u32 num, i;
  1331. READ_BUF(sizeof(struct nfsd4_deviceid) + 3 * 4);
  1332. COPYMEM(&gdev->gd_devid, sizeof(struct nfsd4_deviceid));
  1333. gdev->gd_layout_type = be32_to_cpup(p++);
  1334. gdev->gd_maxcount = be32_to_cpup(p++);
  1335. num = be32_to_cpup(p++);
  1336. if (num) {
  1337. READ_BUF(4 * num);
  1338. gdev->gd_notify_types = be32_to_cpup(p++);
  1339. for (i = 1; i < num; i++) {
  1340. if (be32_to_cpup(p++)) {
  1341. status = nfserr_inval;
  1342. goto out;
  1343. }
  1344. }
  1345. }
  1346. DECODE_TAIL;
  1347. }
  1348. static __be32
  1349. nfsd4_decode_layoutget(struct nfsd4_compoundargs *argp,
  1350. struct nfsd4_layoutget *lgp)
  1351. {
  1352. DECODE_HEAD;
  1353. READ_BUF(36);
  1354. lgp->lg_signal = be32_to_cpup(p++);
  1355. lgp->lg_layout_type = be32_to_cpup(p++);
  1356. lgp->lg_seg.iomode = be32_to_cpup(p++);
  1357. p = xdr_decode_hyper(p, &lgp->lg_seg.offset);
  1358. p = xdr_decode_hyper(p, &lgp->lg_seg.length);
  1359. p = xdr_decode_hyper(p, &lgp->lg_minlength);
  1360. status = nfsd4_decode_stateid(argp, &lgp->lg_sid);
  1361. if (status)
  1362. return status;
  1363. READ_BUF(4);
  1364. lgp->lg_maxcount = be32_to_cpup(p++);
  1365. DECODE_TAIL;
  1366. }
  1367. static __be32
  1368. nfsd4_decode_layoutcommit(struct nfsd4_compoundargs *argp,
  1369. struct nfsd4_layoutcommit *lcp)
  1370. {
  1371. DECODE_HEAD;
  1372. u32 timechange;
  1373. READ_BUF(20);
  1374. p = xdr_decode_hyper(p, &lcp->lc_seg.offset);
  1375. p = xdr_decode_hyper(p, &lcp->lc_seg.length);
  1376. lcp->lc_reclaim = be32_to_cpup(p++);
  1377. status = nfsd4_decode_stateid(argp, &lcp->lc_sid);
  1378. if (status)
  1379. return status;
  1380. READ_BUF(4);
  1381. lcp->lc_newoffset = be32_to_cpup(p++);
  1382. if (lcp->lc_newoffset) {
  1383. READ_BUF(8);
  1384. p = xdr_decode_hyper(p, &lcp->lc_last_wr);
  1385. } else
  1386. lcp->lc_last_wr = 0;
  1387. READ_BUF(4);
  1388. timechange = be32_to_cpup(p++);
  1389. if (timechange) {
  1390. status = nfsd4_decode_time(argp, &lcp->lc_mtime);
  1391. if (status)
  1392. return status;
  1393. } else {
  1394. lcp->lc_mtime.tv_nsec = UTIME_NOW;
  1395. }
  1396. READ_BUF(8);
  1397. lcp->lc_layout_type = be32_to_cpup(p++);
  1398. /*
  1399. * Save the layout update in XDR format and let the layout driver deal
  1400. * with it later.
  1401. */
  1402. lcp->lc_up_len = be32_to_cpup(p++);
  1403. if (lcp->lc_up_len > 0) {
  1404. READ_BUF(lcp->lc_up_len);
  1405. READMEM(lcp->lc_up_layout, lcp->lc_up_len);
  1406. }
  1407. DECODE_TAIL;
  1408. }
  1409. static __be32
  1410. nfsd4_decode_layoutreturn(struct nfsd4_compoundargs *argp,
  1411. struct nfsd4_layoutreturn *lrp)
  1412. {
  1413. DECODE_HEAD;
  1414. READ_BUF(16);
  1415. lrp->lr_reclaim = be32_to_cpup(p++);
  1416. lrp->lr_layout_type = be32_to_cpup(p++);
  1417. lrp->lr_seg.iomode = be32_to_cpup(p++);
  1418. lrp->lr_return_type = be32_to_cpup(p++);
  1419. if (lrp->lr_return_type == RETURN_FILE) {
  1420. READ_BUF(16);
  1421. p = xdr_decode_hyper(p, &lrp->lr_seg.offset);
  1422. p = xdr_decode_hyper(p, &lrp->lr_seg.length);
  1423. status = nfsd4_decode_stateid(argp, &lrp->lr_sid);
  1424. if (status)
  1425. return status;
  1426. READ_BUF(4);
  1427. lrp->lrf_body_len = be32_to_cpup(p++);
  1428. if (lrp->lrf_body_len > 0) {
  1429. READ_BUF(lrp->lrf_body_len);
  1430. READMEM(lrp->lrf_body, lrp->lrf_body_len);
  1431. }
  1432. } else {
  1433. lrp->lr_seg.offset = 0;
  1434. lrp->lr_seg.length = NFS4_MAX_UINT64;
  1435. }
  1436. DECODE_TAIL;
  1437. }
  1438. #endif /* CONFIG_NFSD_PNFS */
  1439. static __be32
  1440. nfsd4_decode_fallocate(struct nfsd4_compoundargs *argp,
  1441. struct nfsd4_fallocate *fallocate)
  1442. {
  1443. DECODE_HEAD;
  1444. status = nfsd4_decode_stateid(argp, &fallocate->falloc_stateid);
  1445. if (status)
  1446. return status;
  1447. READ_BUF(16);
  1448. p = xdr_decode_hyper(p, &fallocate->falloc_offset);
  1449. xdr_decode_hyper(p, &fallocate->falloc_length);
  1450. DECODE_TAIL;
  1451. }
  1452. static __be32
  1453. nfsd4_decode_seek(struct nfsd4_compoundargs *argp, struct nfsd4_seek *seek)
  1454. {
  1455. DECODE_HEAD;
  1456. status = nfsd4_decode_stateid(argp, &seek->seek_stateid);
  1457. if (status)
  1458. return status;
  1459. READ_BUF(8 + 4);
  1460. p = xdr_decode_hyper(p, &seek->seek_offset);
  1461. seek->seek_whence = be32_to_cpup(p);
  1462. DECODE_TAIL;
  1463. }
  1464. static __be32
  1465. nfsd4_decode_noop(struct nfsd4_compoundargs *argp, void *p)
  1466. {
  1467. return nfs_ok;
  1468. }
  1469. static __be32
  1470. nfsd4_decode_notsupp(struct nfsd4_compoundargs *argp, void *p)
  1471. {
  1472. return nfserr_notsupp;
  1473. }
  1474. typedef __be32(*nfsd4_dec)(struct nfsd4_compoundargs *argp, void *);
  1475. static nfsd4_dec nfsd4_dec_ops[] = {
  1476. [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
  1477. [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
  1478. [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
  1479. [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
  1480. [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
  1481. [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
  1482. [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
  1483. [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
  1484. [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
  1485. [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
  1486. [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
  1487. [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
  1488. [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
  1489. [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
  1490. [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
  1491. [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
  1492. [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
  1493. [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_open_confirm,
  1494. [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
  1495. [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
  1496. [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_putpubfh,
  1497. [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
  1498. [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
  1499. [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
  1500. [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
  1501. [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
  1502. [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
  1503. [OP_RENEW] = (nfsd4_dec)nfsd4_decode_renew,
  1504. [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
  1505. [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
  1506. [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
  1507. [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
  1508. [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_setclientid,
  1509. [OP_SETCLIENTID_CONFIRM] = (nfsd4_dec)nfsd4_decode_setclientid_confirm,
  1510. [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
  1511. [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
  1512. [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_release_lockowner,
  1513. /* new operations for NFSv4.1 */
  1514. [OP_BACKCHANNEL_CTL] = (nfsd4_dec)nfsd4_decode_backchannel_ctl,
  1515. [OP_BIND_CONN_TO_SESSION]= (nfsd4_dec)nfsd4_decode_bind_conn_to_session,
  1516. [OP_EXCHANGE_ID] = (nfsd4_dec)nfsd4_decode_exchange_id,
  1517. [OP_CREATE_SESSION] = (nfsd4_dec)nfsd4_decode_create_session,
  1518. [OP_DESTROY_SESSION] = (nfsd4_dec)nfsd4_decode_destroy_session,
  1519. [OP_FREE_STATEID] = (nfsd4_dec)nfsd4_decode_free_stateid,
  1520. [OP_GET_DIR_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
  1521. #ifdef CONFIG_NFSD_PNFS
  1522. [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_getdeviceinfo,
  1523. [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
  1524. [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_layoutcommit,
  1525. [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_layoutget,
  1526. [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_layoutreturn,
  1527. #else
  1528. [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_notsupp,
  1529. [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
  1530. [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_notsupp,
  1531. [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_notsupp,
  1532. [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_notsupp,
  1533. #endif
  1534. [OP_SECINFO_NO_NAME] = (nfsd4_dec)nfsd4_decode_secinfo_no_name,
  1535. [OP_SEQUENCE] = (nfsd4_dec)nfsd4_decode_sequence,
  1536. [OP_SET_SSV] = (nfsd4_dec)nfsd4_decode_notsupp,
  1537. [OP_TEST_STATEID] = (nfsd4_dec)nfsd4_decode_test_stateid,
  1538. [OP_WANT_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
  1539. [OP_DESTROY_CLIENTID] = (nfsd4_dec)nfsd4_decode_destroy_clientid,
  1540. [OP_RECLAIM_COMPLETE] = (nfsd4_dec)nfsd4_decode_reclaim_complete,
  1541. /* new operations for NFSv4.2 */
  1542. [OP_ALLOCATE] = (nfsd4_dec)nfsd4_decode_fallocate,
  1543. [OP_COPY] = (nfsd4_dec)nfsd4_decode_notsupp,
  1544. [OP_COPY_NOTIFY] = (nfsd4_dec)nfsd4_decode_notsupp,
  1545. [OP_DEALLOCATE] = (nfsd4_dec)nfsd4_decode_fallocate,
  1546. [OP_IO_ADVISE] = (nfsd4_dec)nfsd4_decode_notsupp,
  1547. [OP_LAYOUTERROR] = (nfsd4_dec)nfsd4_decode_notsupp,
  1548. [OP_LAYOUTSTATS] = (nfsd4_dec)nfsd4_decode_notsupp,
  1549. [OP_OFFLOAD_CANCEL] = (nfsd4_dec)nfsd4_decode_notsupp,
  1550. [OP_OFFLOAD_STATUS] = (nfsd4_dec)nfsd4_decode_notsupp,
  1551. [OP_READ_PLUS] = (nfsd4_dec)nfsd4_decode_notsupp,
  1552. [OP_SEEK] = (nfsd4_dec)nfsd4_decode_seek,
  1553. [OP_WRITE_SAME] = (nfsd4_dec)nfsd4_decode_notsupp,
  1554. };
  1555. static inline bool
  1556. nfsd4_opnum_in_range(struct nfsd4_compoundargs *argp, struct nfsd4_op *op)
  1557. {
  1558. if (op->opnum < FIRST_NFS4_OP)
  1559. return false;
  1560. else if (argp->minorversion == 0 && op->opnum > LAST_NFS40_OP)
  1561. return false;
  1562. else if (argp->minorversion == 1 && op->opnum > LAST_NFS41_OP)
  1563. return false;
  1564. else if (argp->minorversion == 2 && op->opnum > LAST_NFS42_OP)
  1565. return false;
  1566. return true;
  1567. }
  1568. static __be32
  1569. nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
  1570. {
  1571. DECODE_HEAD;
  1572. struct nfsd4_op *op;
  1573. bool cachethis = false;
  1574. int auth_slack= argp->rqstp->rq_auth_slack;
  1575. int max_reply = auth_slack + 8; /* opcnt, status */
  1576. int readcount = 0;
  1577. int readbytes = 0;
  1578. int i;
  1579. READ_BUF(4);
  1580. argp->taglen = be32_to_cpup(p++);
  1581. READ_BUF(argp->taglen + 8);
  1582. SAVEMEM(argp->tag, argp->taglen);
  1583. argp->minorversion = be32_to_cpup(p++);
  1584. argp->opcnt = be32_to_cpup(p++);
  1585. max_reply += 4 + (XDR_QUADLEN(argp->taglen) << 2);
  1586. if (argp->taglen > NFSD4_MAX_TAGLEN)
  1587. goto xdr_error;
  1588. if (argp->opcnt > 100)
  1589. goto xdr_error;
  1590. if (argp->opcnt > ARRAY_SIZE(argp->iops)) {
  1591. argp->ops = kzalloc(argp->opcnt * sizeof(*argp->ops), GFP_KERNEL);
  1592. if (!argp->ops) {
  1593. argp->ops = argp->iops;
  1594. dprintk("nfsd: couldn't allocate room for COMPOUND\n");
  1595. goto xdr_error;
  1596. }
  1597. }
  1598. if (argp->minorversion > NFSD_SUPPORTED_MINOR_VERSION)
  1599. argp->opcnt = 0;
  1600. for (i = 0; i < argp->opcnt; i++) {
  1601. op = &argp->ops[i];
  1602. op->replay = NULL;
  1603. READ_BUF(4);
  1604. op->opnum = be32_to_cpup(p++);
  1605. if (nfsd4_opnum_in_range(argp, op))
  1606. op->status = nfsd4_dec_ops[op->opnum](argp, &op->u);
  1607. else {
  1608. op->opnum = OP_ILLEGAL;
  1609. op->status = nfserr_op_illegal;
  1610. }
  1611. /*
  1612. * We'll try to cache the result in the DRC if any one
  1613. * op in the compound wants to be cached:
  1614. */
  1615. cachethis |= nfsd4_cache_this_op(op);
  1616. if (op->opnum == OP_READ) {
  1617. readcount++;
  1618. readbytes += nfsd4_max_reply(argp->rqstp, op);
  1619. } else
  1620. max_reply += nfsd4_max_reply(argp->rqstp, op);
  1621. /*
  1622. * OP_LOCK may return a conflicting lock. (Special case
  1623. * because it will just skip encoding this if it runs
  1624. * out of xdr buffer space, and it is the only operation
  1625. * that behaves this way.)
  1626. */
  1627. if (op->opnum == OP_LOCK)
  1628. max_reply += NFS4_OPAQUE_LIMIT;
  1629. if (op->status) {
  1630. argp->opcnt = i+1;
  1631. break;
  1632. }
  1633. }
  1634. /* Sessions make the DRC unnecessary: */
  1635. if (argp->minorversion)
  1636. cachethis = false;
  1637. svc_reserve(argp->rqstp, max_reply + readbytes);
  1638. argp->rqstp->rq_cachetype = cachethis ? RC_REPLBUFF : RC_NOCACHE;
  1639. if (readcount > 1 || max_reply > PAGE_SIZE - auth_slack)
  1640. clear_bit(RQ_SPLICE_OK, &argp->rqstp->rq_flags);
  1641. DECODE_TAIL;
  1642. }
  1643. static __be32 *encode_change(__be32 *p, struct kstat *stat, struct inode *inode)
  1644. {
  1645. if (IS_I_VERSION(inode)) {
  1646. p = xdr_encode_hyper(p, inode->i_version);
  1647. } else {
  1648. *p++ = cpu_to_be32(stat->ctime.tv_sec);
  1649. *p++ = cpu_to_be32(stat->ctime.tv_nsec);
  1650. }
  1651. return p;
  1652. }
  1653. static __be32 *encode_cinfo(__be32 *p, struct nfsd4_change_info *c)
  1654. {
  1655. *p++ = cpu_to_be32(c->atomic);
  1656. if (c->change_supported) {
  1657. p = xdr_encode_hyper(p, c->before_change);
  1658. p = xdr_encode_hyper(p, c->after_change);
  1659. } else {
  1660. *p++ = cpu_to_be32(c->before_ctime_sec);
  1661. *p++ = cpu_to_be32(c->before_ctime_nsec);
  1662. *p++ = cpu_to_be32(c->after_ctime_sec);
  1663. *p++ = cpu_to_be32(c->after_ctime_nsec);
  1664. }
  1665. return p;
  1666. }
  1667. /* Encode as an array of strings the string given with components
  1668. * separated @sep, escaped with esc_enter and esc_exit.
  1669. */
  1670. static __be32 nfsd4_encode_components_esc(struct xdr_stream *xdr, char sep,
  1671. char *components, char esc_enter,
  1672. char esc_exit)
  1673. {
  1674. __be32 *p;
  1675. __be32 pathlen;
  1676. int pathlen_offset;
  1677. int strlen, count=0;
  1678. char *str, *end, *next;
  1679. dprintk("nfsd4_encode_components(%s)\n", components);
  1680. pathlen_offset = xdr->buf->len;
  1681. p = xdr_reserve_space(xdr, 4);
  1682. if (!p)
  1683. return nfserr_resource;
  1684. p++; /* We will fill this in with @count later */
  1685. end = str = components;
  1686. while (*end) {
  1687. bool found_esc = false;
  1688. /* try to parse as esc_start, ..., esc_end, sep */
  1689. if (*str == esc_enter) {
  1690. for (; *end && (*end != esc_exit); end++)
  1691. /* find esc_exit or end of string */;
  1692. next = end + 1;
  1693. if (*end && (!*next || *next == sep)) {
  1694. str++;
  1695. found_esc = true;
  1696. }
  1697. }
  1698. if (!found_esc)
  1699. for (; *end && (*end != sep); end++)
  1700. /* find sep or end of string */;
  1701. strlen = end - str;
  1702. if (strlen) {
  1703. p = xdr_reserve_space(xdr, strlen + 4);
  1704. if (!p)
  1705. return nfserr_resource;
  1706. p = xdr_encode_opaque(p, str, strlen);
  1707. count++;
  1708. }
  1709. else
  1710. end++;
  1711. if (found_esc)
  1712. end = next;
  1713. str = end;
  1714. }
  1715. pathlen = htonl(count);
  1716. write_bytes_to_xdr_buf(xdr->buf, pathlen_offset, &pathlen, 4);
  1717. return 0;
  1718. }
  1719. /* Encode as an array of strings the string given with components
  1720. * separated @sep.
  1721. */
  1722. static __be32 nfsd4_encode_components(struct xdr_stream *xdr, char sep,
  1723. char *components)
  1724. {
  1725. return nfsd4_encode_components_esc(xdr, sep, components, 0, 0);
  1726. }
  1727. /*
  1728. * encode a location element of a fs_locations structure
  1729. */
  1730. static __be32 nfsd4_encode_fs_location4(struct xdr_stream *xdr,
  1731. struct nfsd4_fs_location *location)
  1732. {
  1733. __be32 status;
  1734. status = nfsd4_encode_components_esc(xdr, ':', location->hosts,
  1735. '[', ']');
  1736. if (status)
  1737. return status;
  1738. status = nfsd4_encode_components(xdr, '/', location->path);
  1739. if (status)
  1740. return status;
  1741. return 0;
  1742. }
  1743. /*
  1744. * Encode a path in RFC3530 'pathname4' format
  1745. */
  1746. static __be32 nfsd4_encode_path(struct xdr_stream *xdr,
  1747. const struct path *root,
  1748. const struct path *path)
  1749. {
  1750. struct path cur = *path;
  1751. __be32 *p;
  1752. struct dentry **components = NULL;
  1753. unsigned int ncomponents = 0;
  1754. __be32 err = nfserr_jukebox;
  1755. dprintk("nfsd4_encode_components(");
  1756. path_get(&cur);
  1757. /* First walk the path up to the nfsd root, and store the
  1758. * dentries/path components in an array.
  1759. */
  1760. for (;;) {
  1761. if (path_equal(&cur, root))
  1762. break;
  1763. if (cur.dentry == cur.mnt->mnt_root) {
  1764. if (follow_up(&cur))
  1765. continue;
  1766. goto out_free;
  1767. }
  1768. if ((ncomponents & 15) == 0) {
  1769. struct dentry **new;
  1770. new = krealloc(components,
  1771. sizeof(*new) * (ncomponents + 16),
  1772. GFP_KERNEL);
  1773. if (!new)
  1774. goto out_free;
  1775. components = new;
  1776. }
  1777. components[ncomponents++] = cur.dentry;
  1778. cur.dentry = dget_parent(cur.dentry);
  1779. }
  1780. err = nfserr_resource;
  1781. p = xdr_reserve_space(xdr, 4);
  1782. if (!p)
  1783. goto out_free;
  1784. *p++ = cpu_to_be32(ncomponents);
  1785. while (ncomponents) {
  1786. struct dentry *dentry = components[ncomponents - 1];
  1787. unsigned int len;
  1788. spin_lock(&dentry->d_lock);
  1789. len = dentry->d_name.len;
  1790. p = xdr_reserve_space(xdr, len + 4);
  1791. if (!p) {
  1792. spin_unlock(&dentry->d_lock);
  1793. goto out_free;
  1794. }
  1795. p = xdr_encode_opaque(p, dentry->d_name.name, len);
  1796. dprintk("/%pd", dentry);
  1797. spin_unlock(&dentry->d_lock);
  1798. dput(dentry);
  1799. ncomponents--;
  1800. }
  1801. err = 0;
  1802. out_free:
  1803. dprintk(")\n");
  1804. while (ncomponents)
  1805. dput(components[--ncomponents]);
  1806. kfree(components);
  1807. path_put(&cur);
  1808. return err;
  1809. }
  1810. static __be32 nfsd4_encode_fsloc_fsroot(struct xdr_stream *xdr,
  1811. struct svc_rqst *rqstp, const struct path *path)
  1812. {
  1813. struct svc_export *exp_ps;
  1814. __be32 res;
  1815. exp_ps = rqst_find_fsidzero_export(rqstp);
  1816. if (IS_ERR(exp_ps))
  1817. return nfserrno(PTR_ERR(exp_ps));
  1818. res = nfsd4_encode_path(xdr, &exp_ps->ex_path, path);
  1819. exp_put(exp_ps);
  1820. return res;
  1821. }
  1822. /*
  1823. * encode a fs_locations structure
  1824. */
  1825. static __be32 nfsd4_encode_fs_locations(struct xdr_stream *xdr,
  1826. struct svc_rqst *rqstp, struct svc_export *exp)
  1827. {
  1828. __be32 status;
  1829. int i;
  1830. __be32 *p;
  1831. struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
  1832. status = nfsd4_encode_fsloc_fsroot(xdr, rqstp, &exp->ex_path);
  1833. if (status)
  1834. return status;
  1835. p = xdr_reserve_space(xdr, 4);
  1836. if (!p)
  1837. return nfserr_resource;
  1838. *p++ = cpu_to_be32(fslocs->locations_count);
  1839. for (i=0; i<fslocs->locations_count; i++) {
  1840. status = nfsd4_encode_fs_location4(xdr, &fslocs->locations[i]);
  1841. if (status)
  1842. return status;
  1843. }
  1844. return 0;
  1845. }
  1846. static u32 nfs4_file_type(umode_t mode)
  1847. {
  1848. switch (mode & S_IFMT) {
  1849. case S_IFIFO: return NF4FIFO;
  1850. case S_IFCHR: return NF4CHR;
  1851. case S_IFDIR: return NF4DIR;
  1852. case S_IFBLK: return NF4BLK;
  1853. case S_IFLNK: return NF4LNK;
  1854. case S_IFREG: return NF4REG;
  1855. case S_IFSOCK: return NF4SOCK;
  1856. default: return NF4BAD;
  1857. };
  1858. }
  1859. static inline __be32
  1860. nfsd4_encode_aclname(struct xdr_stream *xdr, struct svc_rqst *rqstp,
  1861. struct nfs4_ace *ace)
  1862. {
  1863. if (ace->whotype != NFS4_ACL_WHO_NAMED)
  1864. return nfs4_acl_write_who(xdr, ace->whotype);
  1865. else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
  1866. return nfsd4_encode_group(xdr, rqstp, ace->who_gid);
  1867. else
  1868. return nfsd4_encode_user(xdr, rqstp, ace->who_uid);
  1869. }
  1870. #define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
  1871. FATTR4_WORD0_RDATTR_ERROR)
  1872. #define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
  1873. #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
  1874. static inline __be32
  1875. nfsd4_encode_security_label(struct xdr_stream *xdr, struct svc_rqst *rqstp,
  1876. void *context, int len)
  1877. {
  1878. __be32 *p;
  1879. p = xdr_reserve_space(xdr, len + 4 + 4 + 4);
  1880. if (!p)
  1881. return nfserr_resource;
  1882. /*
  1883. * For now we use a 0 here to indicate the null translation; in
  1884. * the future we may place a call to translation code here.
  1885. */
  1886. *p++ = cpu_to_be32(0); /* lfs */
  1887. *p++ = cpu_to_be32(0); /* pi */
  1888. p = xdr_encode_opaque(p, context, len);
  1889. return 0;
  1890. }
  1891. #else
  1892. static inline __be32
  1893. nfsd4_encode_security_label(struct xdr_stream *xdr, struct svc_rqst *rqstp,
  1894. void *context, int len)
  1895. { return 0; }
  1896. #endif
  1897. static __be32 fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *rdattr_err)
  1898. {
  1899. /* As per referral draft: */
  1900. if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
  1901. *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
  1902. if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
  1903. *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
  1904. *rdattr_err = NFSERR_MOVED;
  1905. else
  1906. return nfserr_moved;
  1907. }
  1908. *bmval0 &= WORD0_ABSENT_FS_ATTRS;
  1909. *bmval1 &= WORD1_ABSENT_FS_ATTRS;
  1910. return 0;
  1911. }
  1912. static int get_parent_attributes(struct svc_export *exp, struct kstat *stat)
  1913. {
  1914. struct path path = exp->ex_path;
  1915. int err;
  1916. path_get(&path);
  1917. while (follow_up(&path)) {
  1918. if (path.dentry != path.mnt->mnt_root)
  1919. break;
  1920. }
  1921. err = vfs_getattr(&path, stat);
  1922. path_put(&path);
  1923. return err;
  1924. }
  1925. /*
  1926. * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
  1927. * ourselves.
  1928. */
  1929. static __be32
  1930. nfsd4_encode_fattr(struct xdr_stream *xdr, struct svc_fh *fhp,
  1931. struct svc_export *exp,
  1932. struct dentry *dentry, u32 *bmval,
  1933. struct svc_rqst *rqstp, int ignore_crossmnt)
  1934. {
  1935. u32 bmval0 = bmval[0];
  1936. u32 bmval1 = bmval[1];
  1937. u32 bmval2 = bmval[2];
  1938. struct kstat stat;
  1939. struct svc_fh *tempfh = NULL;
  1940. struct kstatfs statfs;
  1941. __be32 *p;
  1942. int starting_len = xdr->buf->len;
  1943. int attrlen_offset;
  1944. __be32 attrlen;
  1945. u32 dummy;
  1946. u64 dummy64;
  1947. u32 rdattr_err = 0;
  1948. __be32 status;
  1949. int err;
  1950. struct nfs4_acl *acl = NULL;
  1951. void *context = NULL;
  1952. int contextlen;
  1953. bool contextsupport = false;
  1954. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  1955. u32 minorversion = resp->cstate.minorversion;
  1956. struct path path = {
  1957. .mnt = exp->ex_path.mnt,
  1958. .dentry = dentry,
  1959. };
  1960. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1961. BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
  1962. BUG_ON(bmval0 & ~nfsd_suppattrs0(minorversion));
  1963. BUG_ON(bmval1 & ~nfsd_suppattrs1(minorversion));
  1964. BUG_ON(bmval2 & ~nfsd_suppattrs2(minorversion));
  1965. if (exp->ex_fslocs.migrated) {
  1966. BUG_ON(bmval[2]);
  1967. status = fattr_handle_absent_fs(&bmval0, &bmval1, &rdattr_err);
  1968. if (status)
  1969. goto out;
  1970. }
  1971. err = vfs_getattr(&path, &stat);
  1972. if (err)
  1973. goto out_nfserr;
  1974. if ((bmval0 & (FATTR4_WORD0_FILES_AVAIL | FATTR4_WORD0_FILES_FREE |
  1975. FATTR4_WORD0_FILES_TOTAL | FATTR4_WORD0_MAXNAME)) ||
  1976. (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
  1977. FATTR4_WORD1_SPACE_TOTAL))) {
  1978. err = vfs_statfs(&path, &statfs);
  1979. if (err)
  1980. goto out_nfserr;
  1981. }
  1982. if ((bmval0 & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) && !fhp) {
  1983. tempfh = kmalloc(sizeof(struct svc_fh), GFP_KERNEL);
  1984. status = nfserr_jukebox;
  1985. if (!tempfh)
  1986. goto out;
  1987. fh_init(tempfh, NFS4_FHSIZE);
  1988. status = fh_compose(tempfh, exp, dentry, NULL);
  1989. if (status)
  1990. goto out;
  1991. fhp = tempfh;
  1992. }
  1993. if (bmval0 & FATTR4_WORD0_ACL) {
  1994. err = nfsd4_get_nfs4_acl(rqstp, dentry, &acl);
  1995. if (err == -EOPNOTSUPP)
  1996. bmval0 &= ~FATTR4_WORD0_ACL;
  1997. else if (err == -EINVAL) {
  1998. status = nfserr_attrnotsupp;
  1999. goto out;
  2000. } else if (err != 0)
  2001. goto out_nfserr;
  2002. }
  2003. #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
  2004. if ((bmval[2] & FATTR4_WORD2_SECURITY_LABEL) ||
  2005. bmval[0] & FATTR4_WORD0_SUPPORTED_ATTRS) {
  2006. err = security_inode_getsecctx(d_inode(dentry),
  2007. &context, &contextlen);
  2008. contextsupport = (err == 0);
  2009. if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
  2010. if (err == -EOPNOTSUPP)
  2011. bmval2 &= ~FATTR4_WORD2_SECURITY_LABEL;
  2012. else if (err)
  2013. goto out_nfserr;
  2014. }
  2015. }
  2016. #endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
  2017. if (bmval2) {
  2018. p = xdr_reserve_space(xdr, 16);
  2019. if (!p)
  2020. goto out_resource;
  2021. *p++ = cpu_to_be32(3);
  2022. *p++ = cpu_to_be32(bmval0);
  2023. *p++ = cpu_to_be32(bmval1);
  2024. *p++ = cpu_to_be32(bmval2);
  2025. } else if (bmval1) {
  2026. p = xdr_reserve_space(xdr, 12);
  2027. if (!p)
  2028. goto out_resource;
  2029. *p++ = cpu_to_be32(2);
  2030. *p++ = cpu_to_be32(bmval0);
  2031. *p++ = cpu_to_be32(bmval1);
  2032. } else {
  2033. p = xdr_reserve_space(xdr, 8);
  2034. if (!p)
  2035. goto out_resource;
  2036. *p++ = cpu_to_be32(1);
  2037. *p++ = cpu_to_be32(bmval0);
  2038. }
  2039. attrlen_offset = xdr->buf->len;
  2040. p = xdr_reserve_space(xdr, 4);
  2041. if (!p)
  2042. goto out_resource;
  2043. p++; /* to be backfilled later */
  2044. if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
  2045. u32 word0 = nfsd_suppattrs0(minorversion);
  2046. u32 word1 = nfsd_suppattrs1(minorversion);
  2047. u32 word2 = nfsd_suppattrs2(minorversion);
  2048. if (!IS_POSIXACL(dentry->d_inode))
  2049. word0 &= ~FATTR4_WORD0_ACL;
  2050. if (!contextsupport)
  2051. word2 &= ~FATTR4_WORD2_SECURITY_LABEL;
  2052. if (!word2) {
  2053. p = xdr_reserve_space(xdr, 12);
  2054. if (!p)
  2055. goto out_resource;
  2056. *p++ = cpu_to_be32(2);
  2057. *p++ = cpu_to_be32(word0);
  2058. *p++ = cpu_to_be32(word1);
  2059. } else {
  2060. p = xdr_reserve_space(xdr, 16);
  2061. if (!p)
  2062. goto out_resource;
  2063. *p++ = cpu_to_be32(3);
  2064. *p++ = cpu_to_be32(word0);
  2065. *p++ = cpu_to_be32(word1);
  2066. *p++ = cpu_to_be32(word2);
  2067. }
  2068. }
  2069. if (bmval0 & FATTR4_WORD0_TYPE) {
  2070. p = xdr_reserve_space(xdr, 4);
  2071. if (!p)
  2072. goto out_resource;
  2073. dummy = nfs4_file_type(stat.mode);
  2074. if (dummy == NF4BAD) {
  2075. status = nfserr_serverfault;
  2076. goto out;
  2077. }
  2078. *p++ = cpu_to_be32(dummy);
  2079. }
  2080. if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
  2081. p = xdr_reserve_space(xdr, 4);
  2082. if (!p)
  2083. goto out_resource;
  2084. if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
  2085. *p++ = cpu_to_be32(NFS4_FH_PERSISTENT);
  2086. else
  2087. *p++ = cpu_to_be32(NFS4_FH_PERSISTENT|
  2088. NFS4_FH_VOL_RENAME);
  2089. }
  2090. if (bmval0 & FATTR4_WORD0_CHANGE) {
  2091. p = xdr_reserve_space(xdr, 8);
  2092. if (!p)
  2093. goto out_resource;
  2094. p = encode_change(p, &stat, d_inode(dentry));
  2095. }
  2096. if (bmval0 & FATTR4_WORD0_SIZE) {
  2097. p = xdr_reserve_space(xdr, 8);
  2098. if (!p)
  2099. goto out_resource;
  2100. p = xdr_encode_hyper(p, stat.size);
  2101. }
  2102. if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
  2103. p = xdr_reserve_space(xdr, 4);
  2104. if (!p)
  2105. goto out_resource;
  2106. *p++ = cpu_to_be32(1);
  2107. }
  2108. if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
  2109. p = xdr_reserve_space(xdr, 4);
  2110. if (!p)
  2111. goto out_resource;
  2112. *p++ = cpu_to_be32(1);
  2113. }
  2114. if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
  2115. p = xdr_reserve_space(xdr, 4);
  2116. if (!p)
  2117. goto out_resource;
  2118. *p++ = cpu_to_be32(0);
  2119. }
  2120. if (bmval0 & FATTR4_WORD0_FSID) {
  2121. p = xdr_reserve_space(xdr, 16);
  2122. if (!p)
  2123. goto out_resource;
  2124. if (exp->ex_fslocs.migrated) {
  2125. p = xdr_encode_hyper(p, NFS4_REFERRAL_FSID_MAJOR);
  2126. p = xdr_encode_hyper(p, NFS4_REFERRAL_FSID_MINOR);
  2127. } else switch(fsid_source(fhp)) {
  2128. case FSIDSOURCE_FSID:
  2129. p = xdr_encode_hyper(p, (u64)exp->ex_fsid);
  2130. p = xdr_encode_hyper(p, (u64)0);
  2131. break;
  2132. case FSIDSOURCE_DEV:
  2133. *p++ = cpu_to_be32(0);
  2134. *p++ = cpu_to_be32(MAJOR(stat.dev));
  2135. *p++ = cpu_to_be32(0);
  2136. *p++ = cpu_to_be32(MINOR(stat.dev));
  2137. break;
  2138. case FSIDSOURCE_UUID:
  2139. p = xdr_encode_opaque_fixed(p, exp->ex_uuid,
  2140. EX_UUID_LEN);
  2141. break;
  2142. }
  2143. }
  2144. if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
  2145. p = xdr_reserve_space(xdr, 4);
  2146. if (!p)
  2147. goto out_resource;
  2148. *p++ = cpu_to_be32(0);
  2149. }
  2150. if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
  2151. p = xdr_reserve_space(xdr, 4);
  2152. if (!p)
  2153. goto out_resource;
  2154. *p++ = cpu_to_be32(nn->nfsd4_lease);
  2155. }
  2156. if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
  2157. p = xdr_reserve_space(xdr, 4);
  2158. if (!p)
  2159. goto out_resource;
  2160. *p++ = cpu_to_be32(rdattr_err);
  2161. }
  2162. if (bmval0 & FATTR4_WORD0_ACL) {
  2163. struct nfs4_ace *ace;
  2164. if (acl == NULL) {
  2165. p = xdr_reserve_space(xdr, 4);
  2166. if (!p)
  2167. goto out_resource;
  2168. *p++ = cpu_to_be32(0);
  2169. goto out_acl;
  2170. }
  2171. p = xdr_reserve_space(xdr, 4);
  2172. if (!p)
  2173. goto out_resource;
  2174. *p++ = cpu_to_be32(acl->naces);
  2175. for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
  2176. p = xdr_reserve_space(xdr, 4*3);
  2177. if (!p)
  2178. goto out_resource;
  2179. *p++ = cpu_to_be32(ace->type);
  2180. *p++ = cpu_to_be32(ace->flag);
  2181. *p++ = cpu_to_be32(ace->access_mask &
  2182. NFS4_ACE_MASK_ALL);
  2183. status = nfsd4_encode_aclname(xdr, rqstp, ace);
  2184. if (status)
  2185. goto out;
  2186. }
  2187. }
  2188. out_acl:
  2189. if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
  2190. p = xdr_reserve_space(xdr, 4);
  2191. if (!p)
  2192. goto out_resource;
  2193. *p++ = cpu_to_be32(IS_POSIXACL(dentry->d_inode) ?
  2194. ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL : 0);
  2195. }
  2196. if (bmval0 & FATTR4_WORD0_CANSETTIME) {
  2197. p = xdr_reserve_space(xdr, 4);
  2198. if (!p)
  2199. goto out_resource;
  2200. *p++ = cpu_to_be32(1);
  2201. }
  2202. if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
  2203. p = xdr_reserve_space(xdr, 4);
  2204. if (!p)
  2205. goto out_resource;
  2206. *p++ = cpu_to_be32(0);
  2207. }
  2208. if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
  2209. p = xdr_reserve_space(xdr, 4);
  2210. if (!p)
  2211. goto out_resource;
  2212. *p++ = cpu_to_be32(1);
  2213. }
  2214. if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
  2215. p = xdr_reserve_space(xdr, 4);
  2216. if (!p)
  2217. goto out_resource;
  2218. *p++ = cpu_to_be32(1);
  2219. }
  2220. if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
  2221. p = xdr_reserve_space(xdr, fhp->fh_handle.fh_size + 4);
  2222. if (!p)
  2223. goto out_resource;
  2224. p = xdr_encode_opaque(p, &fhp->fh_handle.fh_base,
  2225. fhp->fh_handle.fh_size);
  2226. }
  2227. if (bmval0 & FATTR4_WORD0_FILEID) {
  2228. p = xdr_reserve_space(xdr, 8);
  2229. if (!p)
  2230. goto out_resource;
  2231. p = xdr_encode_hyper(p, stat.ino);
  2232. }
  2233. if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
  2234. p = xdr_reserve_space(xdr, 8);
  2235. if (!p)
  2236. goto out_resource;
  2237. p = xdr_encode_hyper(p, (u64) statfs.f_ffree);
  2238. }
  2239. if (bmval0 & FATTR4_WORD0_FILES_FREE) {
  2240. p = xdr_reserve_space(xdr, 8);
  2241. if (!p)
  2242. goto out_resource;
  2243. p = xdr_encode_hyper(p, (u64) statfs.f_ffree);
  2244. }
  2245. if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
  2246. p = xdr_reserve_space(xdr, 8);
  2247. if (!p)
  2248. goto out_resource;
  2249. p = xdr_encode_hyper(p, (u64) statfs.f_files);
  2250. }
  2251. if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
  2252. status = nfsd4_encode_fs_locations(xdr, rqstp, exp);
  2253. if (status)
  2254. goto out;
  2255. }
  2256. if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
  2257. p = xdr_reserve_space(xdr, 4);
  2258. if (!p)
  2259. goto out_resource;
  2260. *p++ = cpu_to_be32(1);
  2261. }
  2262. if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
  2263. p = xdr_reserve_space(xdr, 8);
  2264. if (!p)
  2265. goto out_resource;
  2266. p = xdr_encode_hyper(p, exp->ex_path.mnt->mnt_sb->s_maxbytes);
  2267. }
  2268. if (bmval0 & FATTR4_WORD0_MAXLINK) {
  2269. p = xdr_reserve_space(xdr, 4);
  2270. if (!p)
  2271. goto out_resource;
  2272. *p++ = cpu_to_be32(255);
  2273. }
  2274. if (bmval0 & FATTR4_WORD0_MAXNAME) {
  2275. p = xdr_reserve_space(xdr, 4);
  2276. if (!p)
  2277. goto out_resource;
  2278. *p++ = cpu_to_be32(statfs.f_namelen);
  2279. }
  2280. if (bmval0 & FATTR4_WORD0_MAXREAD) {
  2281. p = xdr_reserve_space(xdr, 8);
  2282. if (!p)
  2283. goto out_resource;
  2284. p = xdr_encode_hyper(p, (u64) svc_max_payload(rqstp));
  2285. }
  2286. if (bmval0 & FATTR4_WORD0_MAXWRITE) {
  2287. p = xdr_reserve_space(xdr, 8);
  2288. if (!p)
  2289. goto out_resource;
  2290. p = xdr_encode_hyper(p, (u64) svc_max_payload(rqstp));
  2291. }
  2292. if (bmval1 & FATTR4_WORD1_MODE) {
  2293. p = xdr_reserve_space(xdr, 4);
  2294. if (!p)
  2295. goto out_resource;
  2296. *p++ = cpu_to_be32(stat.mode & S_IALLUGO);
  2297. }
  2298. if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
  2299. p = xdr_reserve_space(xdr, 4);
  2300. if (!p)
  2301. goto out_resource;
  2302. *p++ = cpu_to_be32(1);
  2303. }
  2304. if (bmval1 & FATTR4_WORD1_NUMLINKS) {
  2305. p = xdr_reserve_space(xdr, 4);
  2306. if (!p)
  2307. goto out_resource;
  2308. *p++ = cpu_to_be32(stat.nlink);
  2309. }
  2310. if (bmval1 & FATTR4_WORD1_OWNER) {
  2311. status = nfsd4_encode_user(xdr, rqstp, stat.uid);
  2312. if (status)
  2313. goto out;
  2314. }
  2315. if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
  2316. status = nfsd4_encode_group(xdr, rqstp, stat.gid);
  2317. if (status)
  2318. goto out;
  2319. }
  2320. if (bmval1 & FATTR4_WORD1_RAWDEV) {
  2321. p = xdr_reserve_space(xdr, 8);
  2322. if (!p)
  2323. goto out_resource;
  2324. *p++ = cpu_to_be32((u32) MAJOR(stat.rdev));
  2325. *p++ = cpu_to_be32((u32) MINOR(stat.rdev));
  2326. }
  2327. if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
  2328. p = xdr_reserve_space(xdr, 8);
  2329. if (!p)
  2330. goto out_resource;
  2331. dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
  2332. p = xdr_encode_hyper(p, dummy64);
  2333. }
  2334. if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
  2335. p = xdr_reserve_space(xdr, 8);
  2336. if (!p)
  2337. goto out_resource;
  2338. dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
  2339. p = xdr_encode_hyper(p, dummy64);
  2340. }
  2341. if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
  2342. p = xdr_reserve_space(xdr, 8);
  2343. if (!p)
  2344. goto out_resource;
  2345. dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
  2346. p = xdr_encode_hyper(p, dummy64);
  2347. }
  2348. if (bmval1 & FATTR4_WORD1_SPACE_USED) {
  2349. p = xdr_reserve_space(xdr, 8);
  2350. if (!p)
  2351. goto out_resource;
  2352. dummy64 = (u64)stat.blocks << 9;
  2353. p = xdr_encode_hyper(p, dummy64);
  2354. }
  2355. if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
  2356. p = xdr_reserve_space(xdr, 12);
  2357. if (!p)
  2358. goto out_resource;
  2359. p = xdr_encode_hyper(p, (s64)stat.atime.tv_sec);
  2360. *p++ = cpu_to_be32(stat.atime.tv_nsec);
  2361. }
  2362. if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
  2363. p = xdr_reserve_space(xdr, 12);
  2364. if (!p)
  2365. goto out_resource;
  2366. *p++ = cpu_to_be32(0);
  2367. *p++ = cpu_to_be32(1);
  2368. *p++ = cpu_to_be32(0);
  2369. }
  2370. if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
  2371. p = xdr_reserve_space(xdr, 12);
  2372. if (!p)
  2373. goto out_resource;
  2374. p = xdr_encode_hyper(p, (s64)stat.ctime.tv_sec);
  2375. *p++ = cpu_to_be32(stat.ctime.tv_nsec);
  2376. }
  2377. if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
  2378. p = xdr_reserve_space(xdr, 12);
  2379. if (!p)
  2380. goto out_resource;
  2381. p = xdr_encode_hyper(p, (s64)stat.mtime.tv_sec);
  2382. *p++ = cpu_to_be32(stat.mtime.tv_nsec);
  2383. }
  2384. if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
  2385. p = xdr_reserve_space(xdr, 8);
  2386. if (!p)
  2387. goto out_resource;
  2388. /*
  2389. * Get parent's attributes if not ignoring crossmount
  2390. * and this is the root of a cross-mounted filesystem.
  2391. */
  2392. if (ignore_crossmnt == 0 &&
  2393. dentry == exp->ex_path.mnt->mnt_root)
  2394. get_parent_attributes(exp, &stat);
  2395. p = xdr_encode_hyper(p, stat.ino);
  2396. }
  2397. #ifdef CONFIG_NFSD_PNFS
  2398. if ((bmval1 & FATTR4_WORD1_FS_LAYOUT_TYPES) ||
  2399. (bmval2 & FATTR4_WORD2_LAYOUT_TYPES)) {
  2400. if (exp->ex_layout_type) {
  2401. p = xdr_reserve_space(xdr, 8);
  2402. if (!p)
  2403. goto out_resource;
  2404. *p++ = cpu_to_be32(1);
  2405. *p++ = cpu_to_be32(exp->ex_layout_type);
  2406. } else {
  2407. p = xdr_reserve_space(xdr, 4);
  2408. if (!p)
  2409. goto out_resource;
  2410. *p++ = cpu_to_be32(0);
  2411. }
  2412. }
  2413. if (bmval2 & FATTR4_WORD2_LAYOUT_BLKSIZE) {
  2414. p = xdr_reserve_space(xdr, 4);
  2415. if (!p)
  2416. goto out_resource;
  2417. *p++ = cpu_to_be32(stat.blksize);
  2418. }
  2419. #endif /* CONFIG_NFSD_PNFS */
  2420. if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
  2421. status = nfsd4_encode_security_label(xdr, rqstp, context,
  2422. contextlen);
  2423. if (status)
  2424. goto out;
  2425. }
  2426. if (bmval2 & FATTR4_WORD2_SUPPATTR_EXCLCREAT) {
  2427. p = xdr_reserve_space(xdr, 16);
  2428. if (!p)
  2429. goto out_resource;
  2430. *p++ = cpu_to_be32(3);
  2431. *p++ = cpu_to_be32(NFSD_SUPPATTR_EXCLCREAT_WORD0);
  2432. *p++ = cpu_to_be32(NFSD_SUPPATTR_EXCLCREAT_WORD1);
  2433. *p++ = cpu_to_be32(NFSD_SUPPATTR_EXCLCREAT_WORD2);
  2434. }
  2435. attrlen = htonl(xdr->buf->len - attrlen_offset - 4);
  2436. write_bytes_to_xdr_buf(xdr->buf, attrlen_offset, &attrlen, 4);
  2437. status = nfs_ok;
  2438. out:
  2439. #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
  2440. if (context)
  2441. security_release_secctx(context, contextlen);
  2442. #endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
  2443. kfree(acl);
  2444. if (tempfh) {
  2445. fh_put(tempfh);
  2446. kfree(tempfh);
  2447. }
  2448. if (status)
  2449. xdr_truncate_encode(xdr, starting_len);
  2450. return status;
  2451. out_nfserr:
  2452. status = nfserrno(err);
  2453. goto out;
  2454. out_resource:
  2455. status = nfserr_resource;
  2456. goto out;
  2457. }
  2458. static void svcxdr_init_encode_from_buffer(struct xdr_stream *xdr,
  2459. struct xdr_buf *buf, __be32 *p, int bytes)
  2460. {
  2461. xdr->scratch.iov_len = 0;
  2462. memset(buf, 0, sizeof(struct xdr_buf));
  2463. buf->head[0].iov_base = p;
  2464. buf->head[0].iov_len = 0;
  2465. buf->len = 0;
  2466. xdr->buf = buf;
  2467. xdr->iov = buf->head;
  2468. xdr->p = p;
  2469. xdr->end = (void *)p + bytes;
  2470. buf->buflen = bytes;
  2471. }
  2472. __be32 nfsd4_encode_fattr_to_buf(__be32 **p, int words,
  2473. struct svc_fh *fhp, struct svc_export *exp,
  2474. struct dentry *dentry, u32 *bmval,
  2475. struct svc_rqst *rqstp, int ignore_crossmnt)
  2476. {
  2477. struct xdr_buf dummy;
  2478. struct xdr_stream xdr;
  2479. __be32 ret;
  2480. svcxdr_init_encode_from_buffer(&xdr, &dummy, *p, words << 2);
  2481. ret = nfsd4_encode_fattr(&xdr, fhp, exp, dentry, bmval, rqstp,
  2482. ignore_crossmnt);
  2483. *p = xdr.p;
  2484. return ret;
  2485. }
  2486. static inline int attributes_need_mount(u32 *bmval)
  2487. {
  2488. if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
  2489. return 1;
  2490. if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
  2491. return 1;
  2492. return 0;
  2493. }
  2494. static __be32
  2495. nfsd4_encode_dirent_fattr(struct xdr_stream *xdr, struct nfsd4_readdir *cd,
  2496. const char *name, int namlen)
  2497. {
  2498. struct svc_export *exp = cd->rd_fhp->fh_export;
  2499. struct dentry *dentry;
  2500. __be32 nfserr;
  2501. int ignore_crossmnt = 0;
  2502. dentry = lookup_one_len(name, cd->rd_fhp->fh_dentry, namlen);
  2503. if (IS_ERR(dentry))
  2504. return nfserrno(PTR_ERR(dentry));
  2505. if (d_really_is_negative(dentry)) {
  2506. /*
  2507. * nfsd_buffered_readdir drops the i_mutex between
  2508. * readdir and calling this callback, leaving a window
  2509. * where this directory entry could have gone away.
  2510. */
  2511. dput(dentry);
  2512. return nfserr_noent;
  2513. }
  2514. exp_get(exp);
  2515. /*
  2516. * In the case of a mountpoint, the client may be asking for
  2517. * attributes that are only properties of the underlying filesystem
  2518. * as opposed to the cross-mounted file system. In such a case,
  2519. * we will not follow the cross mount and will fill the attribtutes
  2520. * directly from the mountpoint dentry.
  2521. */
  2522. if (nfsd_mountpoint(dentry, exp)) {
  2523. int err;
  2524. if (!(exp->ex_flags & NFSEXP_V4ROOT)
  2525. && !attributes_need_mount(cd->rd_bmval)) {
  2526. ignore_crossmnt = 1;
  2527. goto out_encode;
  2528. }
  2529. /*
  2530. * Why the heck aren't we just using nfsd_lookup??
  2531. * Different "."/".." handling? Something else?
  2532. * At least, add a comment here to explain....
  2533. */
  2534. err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
  2535. if (err) {
  2536. nfserr = nfserrno(err);
  2537. goto out_put;
  2538. }
  2539. nfserr = check_nfsd_access(exp, cd->rd_rqstp);
  2540. if (nfserr)
  2541. goto out_put;
  2542. }
  2543. out_encode:
  2544. nfserr = nfsd4_encode_fattr(xdr, NULL, exp, dentry, cd->rd_bmval,
  2545. cd->rd_rqstp, ignore_crossmnt);
  2546. out_put:
  2547. dput(dentry);
  2548. exp_put(exp);
  2549. return nfserr;
  2550. }
  2551. static __be32 *
  2552. nfsd4_encode_rdattr_error(struct xdr_stream *xdr, __be32 nfserr)
  2553. {
  2554. __be32 *p;
  2555. p = xdr_reserve_space(xdr, 20);
  2556. if (!p)
  2557. return NULL;
  2558. *p++ = htonl(2);
  2559. *p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
  2560. *p++ = htonl(0); /* bmval1 */
  2561. *p++ = htonl(4); /* attribute length */
  2562. *p++ = nfserr; /* no htonl */
  2563. return p;
  2564. }
  2565. static int
  2566. nfsd4_encode_dirent(void *ccdv, const char *name, int namlen,
  2567. loff_t offset, u64 ino, unsigned int d_type)
  2568. {
  2569. struct readdir_cd *ccd = ccdv;
  2570. struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
  2571. struct xdr_stream *xdr = cd->xdr;
  2572. int start_offset = xdr->buf->len;
  2573. int cookie_offset;
  2574. u32 name_and_cookie;
  2575. int entry_bytes;
  2576. __be32 nfserr = nfserr_toosmall;
  2577. __be64 wire_offset;
  2578. __be32 *p;
  2579. /* In nfsv4, "." and ".." never make it onto the wire.. */
  2580. if (name && isdotent(name, namlen)) {
  2581. cd->common.err = nfs_ok;
  2582. return 0;
  2583. }
  2584. if (cd->cookie_offset) {
  2585. wire_offset = cpu_to_be64(offset);
  2586. write_bytes_to_xdr_buf(xdr->buf, cd->cookie_offset,
  2587. &wire_offset, 8);
  2588. }
  2589. p = xdr_reserve_space(xdr, 4);
  2590. if (!p)
  2591. goto fail;
  2592. *p++ = xdr_one; /* mark entry present */
  2593. cookie_offset = xdr->buf->len;
  2594. p = xdr_reserve_space(xdr, 3*4 + namlen);
  2595. if (!p)
  2596. goto fail;
  2597. p = xdr_encode_hyper(p, NFS_OFFSET_MAX); /* offset of next entry */
  2598. p = xdr_encode_array(p, name, namlen); /* name length & name */
  2599. nfserr = nfsd4_encode_dirent_fattr(xdr, cd, name, namlen);
  2600. switch (nfserr) {
  2601. case nfs_ok:
  2602. break;
  2603. case nfserr_resource:
  2604. nfserr = nfserr_toosmall;
  2605. goto fail;
  2606. case nfserr_noent:
  2607. xdr_truncate_encode(xdr, start_offset);
  2608. goto skip_entry;
  2609. default:
  2610. /*
  2611. * If the client requested the RDATTR_ERROR attribute,
  2612. * we stuff the error code into this attribute
  2613. * and continue. If this attribute was not requested,
  2614. * then in accordance with the spec, we fail the
  2615. * entire READDIR operation(!)
  2616. */
  2617. if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
  2618. goto fail;
  2619. p = nfsd4_encode_rdattr_error(xdr, nfserr);
  2620. if (p == NULL) {
  2621. nfserr = nfserr_toosmall;
  2622. goto fail;
  2623. }
  2624. }
  2625. nfserr = nfserr_toosmall;
  2626. entry_bytes = xdr->buf->len - start_offset;
  2627. if (entry_bytes > cd->rd_maxcount)
  2628. goto fail;
  2629. cd->rd_maxcount -= entry_bytes;
  2630. /*
  2631. * RFC 3530 14.2.24 describes rd_dircount as only a "hint", so
  2632. * let's always let through the first entry, at least:
  2633. */
  2634. if (!cd->rd_dircount)
  2635. goto fail;
  2636. name_and_cookie = 4 + 4 * XDR_QUADLEN(namlen) + 8;
  2637. if (name_and_cookie > cd->rd_dircount && cd->cookie_offset)
  2638. goto fail;
  2639. cd->rd_dircount -= min(cd->rd_dircount, name_and_cookie);
  2640. cd->cookie_offset = cookie_offset;
  2641. skip_entry:
  2642. cd->common.err = nfs_ok;
  2643. return 0;
  2644. fail:
  2645. xdr_truncate_encode(xdr, start_offset);
  2646. cd->common.err = nfserr;
  2647. return -EINVAL;
  2648. }
  2649. static __be32
  2650. nfsd4_encode_stateid(struct xdr_stream *xdr, stateid_t *sid)
  2651. {
  2652. __be32 *p;
  2653. p = xdr_reserve_space(xdr, sizeof(stateid_t));
  2654. if (!p)
  2655. return nfserr_resource;
  2656. *p++ = cpu_to_be32(sid->si_generation);
  2657. p = xdr_encode_opaque_fixed(p, &sid->si_opaque,
  2658. sizeof(stateid_opaque_t));
  2659. return 0;
  2660. }
  2661. static __be32
  2662. nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_access *access)
  2663. {
  2664. struct xdr_stream *xdr = &resp->xdr;
  2665. __be32 *p;
  2666. if (!nfserr) {
  2667. p = xdr_reserve_space(xdr, 8);
  2668. if (!p)
  2669. return nfserr_resource;
  2670. *p++ = cpu_to_be32(access->ac_supported);
  2671. *p++ = cpu_to_be32(access->ac_resp_access);
  2672. }
  2673. return nfserr;
  2674. }
  2675. static __be32 nfsd4_encode_bind_conn_to_session(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_bind_conn_to_session *bcts)
  2676. {
  2677. struct xdr_stream *xdr = &resp->xdr;
  2678. __be32 *p;
  2679. if (!nfserr) {
  2680. p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN + 8);
  2681. if (!p)
  2682. return nfserr_resource;
  2683. p = xdr_encode_opaque_fixed(p, bcts->sessionid.data,
  2684. NFS4_MAX_SESSIONID_LEN);
  2685. *p++ = cpu_to_be32(bcts->dir);
  2686. /* Sorry, we do not yet support RDMA over 4.1: */
  2687. *p++ = cpu_to_be32(0);
  2688. }
  2689. return nfserr;
  2690. }
  2691. static __be32
  2692. nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_close *close)
  2693. {
  2694. struct xdr_stream *xdr = &resp->xdr;
  2695. if (!nfserr)
  2696. nfserr = nfsd4_encode_stateid(xdr, &close->cl_stateid);
  2697. return nfserr;
  2698. }
  2699. static __be32
  2700. nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_commit *commit)
  2701. {
  2702. struct xdr_stream *xdr = &resp->xdr;
  2703. __be32 *p;
  2704. if (!nfserr) {
  2705. p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
  2706. if (!p)
  2707. return nfserr_resource;
  2708. p = xdr_encode_opaque_fixed(p, commit->co_verf.data,
  2709. NFS4_VERIFIER_SIZE);
  2710. }
  2711. return nfserr;
  2712. }
  2713. static __be32
  2714. nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_create *create)
  2715. {
  2716. struct xdr_stream *xdr = &resp->xdr;
  2717. __be32 *p;
  2718. if (!nfserr) {
  2719. p = xdr_reserve_space(xdr, 32);
  2720. if (!p)
  2721. return nfserr_resource;
  2722. p = encode_cinfo(p, &create->cr_cinfo);
  2723. *p++ = cpu_to_be32(2);
  2724. *p++ = cpu_to_be32(create->cr_bmval[0]);
  2725. *p++ = cpu_to_be32(create->cr_bmval[1]);
  2726. }
  2727. return nfserr;
  2728. }
  2729. static __be32
  2730. nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_getattr *getattr)
  2731. {
  2732. struct svc_fh *fhp = getattr->ga_fhp;
  2733. struct xdr_stream *xdr = &resp->xdr;
  2734. if (nfserr)
  2735. return nfserr;
  2736. nfserr = nfsd4_encode_fattr(xdr, fhp, fhp->fh_export, fhp->fh_dentry,
  2737. getattr->ga_bmval,
  2738. resp->rqstp, 0);
  2739. return nfserr;
  2740. }
  2741. static __be32
  2742. nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr, struct svc_fh **fhpp)
  2743. {
  2744. struct xdr_stream *xdr = &resp->xdr;
  2745. struct svc_fh *fhp = *fhpp;
  2746. unsigned int len;
  2747. __be32 *p;
  2748. if (!nfserr) {
  2749. len = fhp->fh_handle.fh_size;
  2750. p = xdr_reserve_space(xdr, len + 4);
  2751. if (!p)
  2752. return nfserr_resource;
  2753. p = xdr_encode_opaque(p, &fhp->fh_handle.fh_base, len);
  2754. }
  2755. return nfserr;
  2756. }
  2757. /*
  2758. * Including all fields other than the name, a LOCK4denied structure requires
  2759. * 8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
  2760. */
  2761. static __be32
  2762. nfsd4_encode_lock_denied(struct xdr_stream *xdr, struct nfsd4_lock_denied *ld)
  2763. {
  2764. struct xdr_netobj *conf = &ld->ld_owner;
  2765. __be32 *p;
  2766. again:
  2767. p = xdr_reserve_space(xdr, 32 + XDR_LEN(conf->len));
  2768. if (!p) {
  2769. /*
  2770. * Don't fail to return the result just because we can't
  2771. * return the conflicting open:
  2772. */
  2773. if (conf->len) {
  2774. kfree(conf->data);
  2775. conf->len = 0;
  2776. conf->data = NULL;
  2777. goto again;
  2778. }
  2779. return nfserr_resource;
  2780. }
  2781. p = xdr_encode_hyper(p, ld->ld_start);
  2782. p = xdr_encode_hyper(p, ld->ld_length);
  2783. *p++ = cpu_to_be32(ld->ld_type);
  2784. if (conf->len) {
  2785. p = xdr_encode_opaque_fixed(p, &ld->ld_clientid, 8);
  2786. p = xdr_encode_opaque(p, conf->data, conf->len);
  2787. kfree(conf->data);
  2788. } else { /* non - nfsv4 lock in conflict, no clientid nor owner */
  2789. p = xdr_encode_hyper(p, (u64)0); /* clientid */
  2790. *p++ = cpu_to_be32(0); /* length of owner name */
  2791. }
  2792. return nfserr_denied;
  2793. }
  2794. static __be32
  2795. nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lock *lock)
  2796. {
  2797. struct xdr_stream *xdr = &resp->xdr;
  2798. if (!nfserr)
  2799. nfserr = nfsd4_encode_stateid(xdr, &lock->lk_resp_stateid);
  2800. else if (nfserr == nfserr_denied)
  2801. nfserr = nfsd4_encode_lock_denied(xdr, &lock->lk_denied);
  2802. return nfserr;
  2803. }
  2804. static __be32
  2805. nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lockt *lockt)
  2806. {
  2807. struct xdr_stream *xdr = &resp->xdr;
  2808. if (nfserr == nfserr_denied)
  2809. nfsd4_encode_lock_denied(xdr, &lockt->lt_denied);
  2810. return nfserr;
  2811. }
  2812. static __be32
  2813. nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_locku *locku)
  2814. {
  2815. struct xdr_stream *xdr = &resp->xdr;
  2816. if (!nfserr)
  2817. nfserr = nfsd4_encode_stateid(xdr, &locku->lu_stateid);
  2818. return nfserr;
  2819. }
  2820. static __be32
  2821. nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_link *link)
  2822. {
  2823. struct xdr_stream *xdr = &resp->xdr;
  2824. __be32 *p;
  2825. if (!nfserr) {
  2826. p = xdr_reserve_space(xdr, 20);
  2827. if (!p)
  2828. return nfserr_resource;
  2829. p = encode_cinfo(p, &link->li_cinfo);
  2830. }
  2831. return nfserr;
  2832. }
  2833. static __be32
  2834. nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open *open)
  2835. {
  2836. struct xdr_stream *xdr = &resp->xdr;
  2837. __be32 *p;
  2838. if (nfserr)
  2839. goto out;
  2840. nfserr = nfsd4_encode_stateid(xdr, &open->op_stateid);
  2841. if (nfserr)
  2842. goto out;
  2843. p = xdr_reserve_space(xdr, 40);
  2844. if (!p)
  2845. return nfserr_resource;
  2846. p = encode_cinfo(p, &open->op_cinfo);
  2847. *p++ = cpu_to_be32(open->op_rflags);
  2848. *p++ = cpu_to_be32(2);
  2849. *p++ = cpu_to_be32(open->op_bmval[0]);
  2850. *p++ = cpu_to_be32(open->op_bmval[1]);
  2851. *p++ = cpu_to_be32(open->op_delegate_type);
  2852. switch (open->op_delegate_type) {
  2853. case NFS4_OPEN_DELEGATE_NONE:
  2854. break;
  2855. case NFS4_OPEN_DELEGATE_READ:
  2856. nfserr = nfsd4_encode_stateid(xdr, &open->op_delegate_stateid);
  2857. if (nfserr)
  2858. return nfserr;
  2859. p = xdr_reserve_space(xdr, 20);
  2860. if (!p)
  2861. return nfserr_resource;
  2862. *p++ = cpu_to_be32(open->op_recall);
  2863. /*
  2864. * TODO: ACE's in delegations
  2865. */
  2866. *p++ = cpu_to_be32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
  2867. *p++ = cpu_to_be32(0);
  2868. *p++ = cpu_to_be32(0);
  2869. *p++ = cpu_to_be32(0); /* XXX: is NULL principal ok? */
  2870. break;
  2871. case NFS4_OPEN_DELEGATE_WRITE:
  2872. nfserr = nfsd4_encode_stateid(xdr, &open->op_delegate_stateid);
  2873. if (nfserr)
  2874. return nfserr;
  2875. p = xdr_reserve_space(xdr, 32);
  2876. if (!p)
  2877. return nfserr_resource;
  2878. *p++ = cpu_to_be32(0);
  2879. /*
  2880. * TODO: space_limit's in delegations
  2881. */
  2882. *p++ = cpu_to_be32(NFS4_LIMIT_SIZE);
  2883. *p++ = cpu_to_be32(~(u32)0);
  2884. *p++ = cpu_to_be32(~(u32)0);
  2885. /*
  2886. * TODO: ACE's in delegations
  2887. */
  2888. *p++ = cpu_to_be32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
  2889. *p++ = cpu_to_be32(0);
  2890. *p++ = cpu_to_be32(0);
  2891. *p++ = cpu_to_be32(0); /* XXX: is NULL principal ok? */
  2892. break;
  2893. case NFS4_OPEN_DELEGATE_NONE_EXT: /* 4.1 */
  2894. switch (open->op_why_no_deleg) {
  2895. case WND4_CONTENTION:
  2896. case WND4_RESOURCE:
  2897. p = xdr_reserve_space(xdr, 8);
  2898. if (!p)
  2899. return nfserr_resource;
  2900. *p++ = cpu_to_be32(open->op_why_no_deleg);
  2901. /* deleg signaling not supported yet: */
  2902. *p++ = cpu_to_be32(0);
  2903. break;
  2904. default:
  2905. p = xdr_reserve_space(xdr, 4);
  2906. if (!p)
  2907. return nfserr_resource;
  2908. *p++ = cpu_to_be32(open->op_why_no_deleg);
  2909. }
  2910. break;
  2911. default:
  2912. BUG();
  2913. }
  2914. /* XXX save filehandle here */
  2915. out:
  2916. return nfserr;
  2917. }
  2918. static __be32
  2919. nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_confirm *oc)
  2920. {
  2921. struct xdr_stream *xdr = &resp->xdr;
  2922. if (!nfserr)
  2923. nfserr = nfsd4_encode_stateid(xdr, &oc->oc_resp_stateid);
  2924. return nfserr;
  2925. }
  2926. static __be32
  2927. nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_downgrade *od)
  2928. {
  2929. struct xdr_stream *xdr = &resp->xdr;
  2930. if (!nfserr)
  2931. nfserr = nfsd4_encode_stateid(xdr, &od->od_stateid);
  2932. return nfserr;
  2933. }
  2934. static __be32 nfsd4_encode_splice_read(
  2935. struct nfsd4_compoundres *resp,
  2936. struct nfsd4_read *read,
  2937. struct file *file, unsigned long maxcount)
  2938. {
  2939. struct xdr_stream *xdr = &resp->xdr;
  2940. struct xdr_buf *buf = xdr->buf;
  2941. u32 eof;
  2942. int space_left;
  2943. __be32 nfserr;
  2944. __be32 *p = xdr->p - 2;
  2945. /* Make sure there will be room for padding if needed */
  2946. if (xdr->end - xdr->p < 1)
  2947. return nfserr_resource;
  2948. nfserr = nfsd_splice_read(read->rd_rqstp, file,
  2949. read->rd_offset, &maxcount);
  2950. if (nfserr) {
  2951. /*
  2952. * nfsd_splice_actor may have already messed with the
  2953. * page length; reset it so as not to confuse
  2954. * xdr_truncate_encode:
  2955. */
  2956. buf->page_len = 0;
  2957. return nfserr;
  2958. }
  2959. eof = (read->rd_offset + maxcount >=
  2960. d_inode(read->rd_fhp->fh_dentry)->i_size);
  2961. *(p++) = htonl(eof);
  2962. *(p++) = htonl(maxcount);
  2963. buf->page_len = maxcount;
  2964. buf->len += maxcount;
  2965. xdr->page_ptr += (buf->page_base + maxcount + PAGE_SIZE - 1)
  2966. / PAGE_SIZE;
  2967. /* Use rest of head for padding and remaining ops: */
  2968. buf->tail[0].iov_base = xdr->p;
  2969. buf->tail[0].iov_len = 0;
  2970. xdr->iov = buf->tail;
  2971. if (maxcount&3) {
  2972. int pad = 4 - (maxcount&3);
  2973. *(xdr->p++) = 0;
  2974. buf->tail[0].iov_base += maxcount&3;
  2975. buf->tail[0].iov_len = pad;
  2976. buf->len += pad;
  2977. }
  2978. space_left = min_t(int, (void *)xdr->end - (void *)xdr->p,
  2979. buf->buflen - buf->len);
  2980. buf->buflen = buf->len + space_left;
  2981. xdr->end = (__be32 *)((void *)xdr->end + space_left);
  2982. return 0;
  2983. }
  2984. static __be32 nfsd4_encode_readv(struct nfsd4_compoundres *resp,
  2985. struct nfsd4_read *read,
  2986. struct file *file, unsigned long maxcount)
  2987. {
  2988. struct xdr_stream *xdr = &resp->xdr;
  2989. u32 eof;
  2990. int v;
  2991. int starting_len = xdr->buf->len - 8;
  2992. long len;
  2993. int thislen;
  2994. __be32 nfserr;
  2995. __be32 tmp;
  2996. __be32 *p;
  2997. u32 zzz = 0;
  2998. int pad;
  2999. len = maxcount;
  3000. v = 0;
  3001. thislen = min_t(long, len, ((void *)xdr->end - (void *)xdr->p));
  3002. p = xdr_reserve_space(xdr, (thislen+3)&~3);
  3003. WARN_ON_ONCE(!p);
  3004. resp->rqstp->rq_vec[v].iov_base = p;
  3005. resp->rqstp->rq_vec[v].iov_len = thislen;
  3006. v++;
  3007. len -= thislen;
  3008. while (len) {
  3009. thislen = min_t(long, len, PAGE_SIZE);
  3010. p = xdr_reserve_space(xdr, (thislen+3)&~3);
  3011. WARN_ON_ONCE(!p);
  3012. resp->rqstp->rq_vec[v].iov_base = p;
  3013. resp->rqstp->rq_vec[v].iov_len = thislen;
  3014. v++;
  3015. len -= thislen;
  3016. }
  3017. read->rd_vlen = v;
  3018. nfserr = nfsd_readv(file, read->rd_offset, resp->rqstp->rq_vec,
  3019. read->rd_vlen, &maxcount);
  3020. if (nfserr)
  3021. return nfserr;
  3022. xdr_truncate_encode(xdr, starting_len + 8 + ((maxcount+3)&~3));
  3023. eof = (read->rd_offset + maxcount >=
  3024. d_inode(read->rd_fhp->fh_dentry)->i_size);
  3025. tmp = htonl(eof);
  3026. write_bytes_to_xdr_buf(xdr->buf, starting_len , &tmp, 4);
  3027. tmp = htonl(maxcount);
  3028. write_bytes_to_xdr_buf(xdr->buf, starting_len + 4, &tmp, 4);
  3029. pad = (maxcount&3) ? 4 - (maxcount&3) : 0;
  3030. write_bytes_to_xdr_buf(xdr->buf, starting_len + 8 + maxcount,
  3031. &zzz, pad);
  3032. return 0;
  3033. }
  3034. static __be32
  3035. nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
  3036. struct nfsd4_read *read)
  3037. {
  3038. unsigned long maxcount;
  3039. struct xdr_stream *xdr = &resp->xdr;
  3040. struct file *file = read->rd_filp;
  3041. int starting_len = xdr->buf->len;
  3042. struct raparms *ra = NULL;
  3043. __be32 *p;
  3044. if (nfserr)
  3045. goto out;
  3046. p = xdr_reserve_space(xdr, 8); /* eof flag and byte count */
  3047. if (!p) {
  3048. WARN_ON_ONCE(test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags));
  3049. nfserr = nfserr_resource;
  3050. goto out;
  3051. }
  3052. if (resp->xdr.buf->page_len &&
  3053. test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags)) {
  3054. WARN_ON_ONCE(1);
  3055. nfserr = nfserr_resource;
  3056. goto out;
  3057. }
  3058. xdr_commit_encode(xdr);
  3059. maxcount = svc_max_payload(resp->rqstp);
  3060. maxcount = min_t(unsigned long, maxcount,
  3061. (xdr->buf->buflen - xdr->buf->len));
  3062. maxcount = min_t(unsigned long, maxcount, read->rd_length);
  3063. if (read->rd_tmp_file)
  3064. ra = nfsd_init_raparms(file);
  3065. if (file->f_op->splice_read &&
  3066. test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags))
  3067. nfserr = nfsd4_encode_splice_read(resp, read, file, maxcount);
  3068. else
  3069. nfserr = nfsd4_encode_readv(resp, read, file, maxcount);
  3070. if (ra)
  3071. nfsd_put_raparams(file, ra);
  3072. if (nfserr)
  3073. xdr_truncate_encode(xdr, starting_len);
  3074. out:
  3075. if (file)
  3076. fput(file);
  3077. return nfserr;
  3078. }
  3079. static __be32
  3080. nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readlink *readlink)
  3081. {
  3082. int maxcount;
  3083. __be32 wire_count;
  3084. int zero = 0;
  3085. struct xdr_stream *xdr = &resp->xdr;
  3086. int length_offset = xdr->buf->len;
  3087. __be32 *p;
  3088. if (nfserr)
  3089. return nfserr;
  3090. p = xdr_reserve_space(xdr, 4);
  3091. if (!p)
  3092. return nfserr_resource;
  3093. maxcount = PAGE_SIZE;
  3094. p = xdr_reserve_space(xdr, maxcount);
  3095. if (!p)
  3096. return nfserr_resource;
  3097. /*
  3098. * XXX: By default, the ->readlink() VFS op will truncate symlinks
  3099. * if they would overflow the buffer. Is this kosher in NFSv4? If
  3100. * not, one easy fix is: if ->readlink() precisely fills the buffer,
  3101. * assume that truncation occurred, and return NFS4ERR_RESOURCE.
  3102. */
  3103. nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp,
  3104. (char *)p, &maxcount);
  3105. if (nfserr == nfserr_isdir)
  3106. nfserr = nfserr_inval;
  3107. if (nfserr) {
  3108. xdr_truncate_encode(xdr, length_offset);
  3109. return nfserr;
  3110. }
  3111. wire_count = htonl(maxcount);
  3112. write_bytes_to_xdr_buf(xdr->buf, length_offset, &wire_count, 4);
  3113. xdr_truncate_encode(xdr, length_offset + 4 + ALIGN(maxcount, 4));
  3114. if (maxcount & 3)
  3115. write_bytes_to_xdr_buf(xdr->buf, length_offset + 4 + maxcount,
  3116. &zero, 4 - (maxcount&3));
  3117. return 0;
  3118. }
  3119. static __be32
  3120. nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readdir *readdir)
  3121. {
  3122. int maxcount;
  3123. int bytes_left;
  3124. loff_t offset;
  3125. __be64 wire_offset;
  3126. struct xdr_stream *xdr = &resp->xdr;
  3127. int starting_len = xdr->buf->len;
  3128. __be32 *p;
  3129. if (nfserr)
  3130. return nfserr;
  3131. p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
  3132. if (!p)
  3133. return nfserr_resource;
  3134. /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
  3135. *p++ = cpu_to_be32(0);
  3136. *p++ = cpu_to_be32(0);
  3137. resp->xdr.buf->head[0].iov_len = ((char *)resp->xdr.p)
  3138. - (char *)resp->xdr.buf->head[0].iov_base;
  3139. /*
  3140. * Number of bytes left for directory entries allowing for the
  3141. * final 8 bytes of the readdir and a following failed op:
  3142. */
  3143. bytes_left = xdr->buf->buflen - xdr->buf->len
  3144. - COMPOUND_ERR_SLACK_SPACE - 8;
  3145. if (bytes_left < 0) {
  3146. nfserr = nfserr_resource;
  3147. goto err_no_verf;
  3148. }
  3149. maxcount = min_t(u32, readdir->rd_maxcount, INT_MAX);
  3150. /*
  3151. * Note the rfc defines rd_maxcount as the size of the
  3152. * READDIR4resok structure, which includes the verifier above
  3153. * and the 8 bytes encoded at the end of this function:
  3154. */
  3155. if (maxcount < 16) {
  3156. nfserr = nfserr_toosmall;
  3157. goto err_no_verf;
  3158. }
  3159. maxcount = min_t(int, maxcount-16, bytes_left);
  3160. /* RFC 3530 14.2.24 allows us to ignore dircount when it's 0: */
  3161. if (!readdir->rd_dircount)
  3162. readdir->rd_dircount = INT_MAX;
  3163. readdir->xdr = xdr;
  3164. readdir->rd_maxcount = maxcount;
  3165. readdir->common.err = 0;
  3166. readdir->cookie_offset = 0;
  3167. offset = readdir->rd_cookie;
  3168. nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
  3169. &offset,
  3170. &readdir->common, nfsd4_encode_dirent);
  3171. if (nfserr == nfs_ok &&
  3172. readdir->common.err == nfserr_toosmall &&
  3173. xdr->buf->len == starting_len + 8) {
  3174. /* nothing encoded; which limit did we hit?: */
  3175. if (maxcount - 16 < bytes_left)
  3176. /* It was the fault of rd_maxcount: */
  3177. nfserr = nfserr_toosmall;
  3178. else
  3179. /* We ran out of buffer space: */
  3180. nfserr = nfserr_resource;
  3181. }
  3182. if (nfserr)
  3183. goto err_no_verf;
  3184. if (readdir->cookie_offset) {
  3185. wire_offset = cpu_to_be64(offset);
  3186. write_bytes_to_xdr_buf(xdr->buf, readdir->cookie_offset,
  3187. &wire_offset, 8);
  3188. }
  3189. p = xdr_reserve_space(xdr, 8);
  3190. if (!p) {
  3191. WARN_ON_ONCE(1);
  3192. goto err_no_verf;
  3193. }
  3194. *p++ = 0; /* no more entries */
  3195. *p++ = htonl(readdir->common.err == nfserr_eof);
  3196. return 0;
  3197. err_no_verf:
  3198. xdr_truncate_encode(xdr, starting_len);
  3199. return nfserr;
  3200. }
  3201. static __be32
  3202. nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_remove *remove)
  3203. {
  3204. struct xdr_stream *xdr = &resp->xdr;
  3205. __be32 *p;
  3206. if (!nfserr) {
  3207. p = xdr_reserve_space(xdr, 20);
  3208. if (!p)
  3209. return nfserr_resource;
  3210. p = encode_cinfo(p, &remove->rm_cinfo);
  3211. }
  3212. return nfserr;
  3213. }
  3214. static __be32
  3215. nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_rename *rename)
  3216. {
  3217. struct xdr_stream *xdr = &resp->xdr;
  3218. __be32 *p;
  3219. if (!nfserr) {
  3220. p = xdr_reserve_space(xdr, 40);
  3221. if (!p)
  3222. return nfserr_resource;
  3223. p = encode_cinfo(p, &rename->rn_sinfo);
  3224. p = encode_cinfo(p, &rename->rn_tinfo);
  3225. }
  3226. return nfserr;
  3227. }
  3228. static __be32
  3229. nfsd4_do_encode_secinfo(struct xdr_stream *xdr,
  3230. __be32 nfserr, struct svc_export *exp)
  3231. {
  3232. u32 i, nflavs, supported;
  3233. struct exp_flavor_info *flavs;
  3234. struct exp_flavor_info def_flavs[2];
  3235. __be32 *p, *flavorsp;
  3236. static bool report = true;
  3237. if (nfserr)
  3238. goto out;
  3239. nfserr = nfserr_resource;
  3240. if (exp->ex_nflavors) {
  3241. flavs = exp->ex_flavors;
  3242. nflavs = exp->ex_nflavors;
  3243. } else { /* Handling of some defaults in absence of real secinfo: */
  3244. flavs = def_flavs;
  3245. if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
  3246. nflavs = 2;
  3247. flavs[0].pseudoflavor = RPC_AUTH_UNIX;
  3248. flavs[1].pseudoflavor = RPC_AUTH_NULL;
  3249. } else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
  3250. nflavs = 1;
  3251. flavs[0].pseudoflavor
  3252. = svcauth_gss_flavor(exp->ex_client);
  3253. } else {
  3254. nflavs = 1;
  3255. flavs[0].pseudoflavor
  3256. = exp->ex_client->flavour->flavour;
  3257. }
  3258. }
  3259. supported = 0;
  3260. p = xdr_reserve_space(xdr, 4);
  3261. if (!p)
  3262. goto out;
  3263. flavorsp = p++; /* to be backfilled later */
  3264. for (i = 0; i < nflavs; i++) {
  3265. rpc_authflavor_t pf = flavs[i].pseudoflavor;
  3266. struct rpcsec_gss_info info;
  3267. if (rpcauth_get_gssinfo(pf, &info) == 0) {
  3268. supported++;
  3269. p = xdr_reserve_space(xdr, 4 + 4 +
  3270. XDR_LEN(info.oid.len) + 4 + 4);
  3271. if (!p)
  3272. goto out;
  3273. *p++ = cpu_to_be32(RPC_AUTH_GSS);
  3274. p = xdr_encode_opaque(p, info.oid.data, info.oid.len);
  3275. *p++ = cpu_to_be32(info.qop);
  3276. *p++ = cpu_to_be32(info.service);
  3277. } else if (pf < RPC_AUTH_MAXFLAVOR) {
  3278. supported++;
  3279. p = xdr_reserve_space(xdr, 4);
  3280. if (!p)
  3281. goto out;
  3282. *p++ = cpu_to_be32(pf);
  3283. } else {
  3284. if (report)
  3285. pr_warn("NFS: SECINFO: security flavor %u "
  3286. "is not supported\n", pf);
  3287. }
  3288. }
  3289. if (nflavs != supported)
  3290. report = false;
  3291. *flavorsp = htonl(supported);
  3292. nfserr = 0;
  3293. out:
  3294. if (exp)
  3295. exp_put(exp);
  3296. return nfserr;
  3297. }
  3298. static __be32
  3299. nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
  3300. struct nfsd4_secinfo *secinfo)
  3301. {
  3302. struct xdr_stream *xdr = &resp->xdr;
  3303. return nfsd4_do_encode_secinfo(xdr, nfserr, secinfo->si_exp);
  3304. }
  3305. static __be32
  3306. nfsd4_encode_secinfo_no_name(struct nfsd4_compoundres *resp, __be32 nfserr,
  3307. struct nfsd4_secinfo_no_name *secinfo)
  3308. {
  3309. struct xdr_stream *xdr = &resp->xdr;
  3310. return nfsd4_do_encode_secinfo(xdr, nfserr, secinfo->sin_exp);
  3311. }
  3312. /*
  3313. * The SETATTR encode routine is special -- it always encodes a bitmap,
  3314. * regardless of the error status.
  3315. */
  3316. static __be32
  3317. nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setattr *setattr)
  3318. {
  3319. struct xdr_stream *xdr = &resp->xdr;
  3320. __be32 *p;
  3321. p = xdr_reserve_space(xdr, 16);
  3322. if (!p)
  3323. return nfserr_resource;
  3324. if (nfserr) {
  3325. *p++ = cpu_to_be32(3);
  3326. *p++ = cpu_to_be32(0);
  3327. *p++ = cpu_to_be32(0);
  3328. *p++ = cpu_to_be32(0);
  3329. }
  3330. else {
  3331. *p++ = cpu_to_be32(3);
  3332. *p++ = cpu_to_be32(setattr->sa_bmval[0]);
  3333. *p++ = cpu_to_be32(setattr->sa_bmval[1]);
  3334. *p++ = cpu_to_be32(setattr->sa_bmval[2]);
  3335. }
  3336. return nfserr;
  3337. }
  3338. static __be32
  3339. nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setclientid *scd)
  3340. {
  3341. struct xdr_stream *xdr = &resp->xdr;
  3342. __be32 *p;
  3343. if (!nfserr) {
  3344. p = xdr_reserve_space(xdr, 8 + NFS4_VERIFIER_SIZE);
  3345. if (!p)
  3346. return nfserr_resource;
  3347. p = xdr_encode_opaque_fixed(p, &scd->se_clientid, 8);
  3348. p = xdr_encode_opaque_fixed(p, &scd->se_confirm,
  3349. NFS4_VERIFIER_SIZE);
  3350. }
  3351. else if (nfserr == nfserr_clid_inuse) {
  3352. p = xdr_reserve_space(xdr, 8);
  3353. if (!p)
  3354. return nfserr_resource;
  3355. *p++ = cpu_to_be32(0);
  3356. *p++ = cpu_to_be32(0);
  3357. }
  3358. return nfserr;
  3359. }
  3360. static __be32
  3361. nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_write *write)
  3362. {
  3363. struct xdr_stream *xdr = &resp->xdr;
  3364. __be32 *p;
  3365. if (!nfserr) {
  3366. p = xdr_reserve_space(xdr, 16);
  3367. if (!p)
  3368. return nfserr_resource;
  3369. *p++ = cpu_to_be32(write->wr_bytes_written);
  3370. *p++ = cpu_to_be32(write->wr_how_written);
  3371. p = xdr_encode_opaque_fixed(p, write->wr_verifier.data,
  3372. NFS4_VERIFIER_SIZE);
  3373. }
  3374. return nfserr;
  3375. }
  3376. static const u32 nfs4_minimal_spo_must_enforce[2] = {
  3377. [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
  3378. 1 << (OP_EXCHANGE_ID - 32) |
  3379. 1 << (OP_CREATE_SESSION - 32) |
  3380. 1 << (OP_DESTROY_SESSION - 32) |
  3381. 1 << (OP_DESTROY_CLIENTID - 32)
  3382. };
  3383. static __be32
  3384. nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, __be32 nfserr,
  3385. struct nfsd4_exchange_id *exid)
  3386. {
  3387. struct xdr_stream *xdr = &resp->xdr;
  3388. __be32 *p;
  3389. char *major_id;
  3390. char *server_scope;
  3391. int major_id_sz;
  3392. int server_scope_sz;
  3393. uint64_t minor_id = 0;
  3394. if (nfserr)
  3395. return nfserr;
  3396. major_id = utsname()->nodename;
  3397. major_id_sz = strlen(major_id);
  3398. server_scope = utsname()->nodename;
  3399. server_scope_sz = strlen(server_scope);
  3400. p = xdr_reserve_space(xdr,
  3401. 8 /* eir_clientid */ +
  3402. 4 /* eir_sequenceid */ +
  3403. 4 /* eir_flags */ +
  3404. 4 /* spr_how */);
  3405. if (!p)
  3406. return nfserr_resource;
  3407. p = xdr_encode_opaque_fixed(p, &exid->clientid, 8);
  3408. *p++ = cpu_to_be32(exid->seqid);
  3409. *p++ = cpu_to_be32(exid->flags);
  3410. *p++ = cpu_to_be32(exid->spa_how);
  3411. switch (exid->spa_how) {
  3412. case SP4_NONE:
  3413. break;
  3414. case SP4_MACH_CRED:
  3415. /* spo_must_enforce, spo_must_allow */
  3416. p = xdr_reserve_space(xdr, 16);
  3417. if (!p)
  3418. return nfserr_resource;
  3419. /* spo_must_enforce bitmap: */
  3420. *p++ = cpu_to_be32(2);
  3421. *p++ = cpu_to_be32(nfs4_minimal_spo_must_enforce[0]);
  3422. *p++ = cpu_to_be32(nfs4_minimal_spo_must_enforce[1]);
  3423. /* empty spo_must_allow bitmap: */
  3424. *p++ = cpu_to_be32(0);
  3425. break;
  3426. default:
  3427. WARN_ON_ONCE(1);
  3428. }
  3429. p = xdr_reserve_space(xdr,
  3430. 8 /* so_minor_id */ +
  3431. 4 /* so_major_id.len */ +
  3432. (XDR_QUADLEN(major_id_sz) * 4) +
  3433. 4 /* eir_server_scope.len */ +
  3434. (XDR_QUADLEN(server_scope_sz) * 4) +
  3435. 4 /* eir_server_impl_id.count (0) */);
  3436. if (!p)
  3437. return nfserr_resource;
  3438. /* The server_owner struct */
  3439. p = xdr_encode_hyper(p, minor_id); /* Minor id */
  3440. /* major id */
  3441. p = xdr_encode_opaque(p, major_id, major_id_sz);
  3442. /* Server scope */
  3443. p = xdr_encode_opaque(p, server_scope, server_scope_sz);
  3444. /* Implementation id */
  3445. *p++ = cpu_to_be32(0); /* zero length nfs_impl_id4 array */
  3446. return 0;
  3447. }
  3448. static __be32
  3449. nfsd4_encode_create_session(struct nfsd4_compoundres *resp, __be32 nfserr,
  3450. struct nfsd4_create_session *sess)
  3451. {
  3452. struct xdr_stream *xdr = &resp->xdr;
  3453. __be32 *p;
  3454. if (nfserr)
  3455. return nfserr;
  3456. p = xdr_reserve_space(xdr, 24);
  3457. if (!p)
  3458. return nfserr_resource;
  3459. p = xdr_encode_opaque_fixed(p, sess->sessionid.data,
  3460. NFS4_MAX_SESSIONID_LEN);
  3461. *p++ = cpu_to_be32(sess->seqid);
  3462. *p++ = cpu_to_be32(sess->flags);
  3463. p = xdr_reserve_space(xdr, 28);
  3464. if (!p)
  3465. return nfserr_resource;
  3466. *p++ = cpu_to_be32(0); /* headerpadsz */
  3467. *p++ = cpu_to_be32(sess->fore_channel.maxreq_sz);
  3468. *p++ = cpu_to_be32(sess->fore_channel.maxresp_sz);
  3469. *p++ = cpu_to_be32(sess->fore_channel.maxresp_cached);
  3470. *p++ = cpu_to_be32(sess->fore_channel.maxops);
  3471. *p++ = cpu_to_be32(sess->fore_channel.maxreqs);
  3472. *p++ = cpu_to_be32(sess->fore_channel.nr_rdma_attrs);
  3473. if (sess->fore_channel.nr_rdma_attrs) {
  3474. p = xdr_reserve_space(xdr, 4);
  3475. if (!p)
  3476. return nfserr_resource;
  3477. *p++ = cpu_to_be32(sess->fore_channel.rdma_attrs);
  3478. }
  3479. p = xdr_reserve_space(xdr, 28);
  3480. if (!p)
  3481. return nfserr_resource;
  3482. *p++ = cpu_to_be32(0); /* headerpadsz */
  3483. *p++ = cpu_to_be32(sess->back_channel.maxreq_sz);
  3484. *p++ = cpu_to_be32(sess->back_channel.maxresp_sz);
  3485. *p++ = cpu_to_be32(sess->back_channel.maxresp_cached);
  3486. *p++ = cpu_to_be32(sess->back_channel.maxops);
  3487. *p++ = cpu_to_be32(sess->back_channel.maxreqs);
  3488. *p++ = cpu_to_be32(sess->back_channel.nr_rdma_attrs);
  3489. if (sess->back_channel.nr_rdma_attrs) {
  3490. p = xdr_reserve_space(xdr, 4);
  3491. if (!p)
  3492. return nfserr_resource;
  3493. *p++ = cpu_to_be32(sess->back_channel.rdma_attrs);
  3494. }
  3495. return 0;
  3496. }
  3497. static __be32
  3498. nfsd4_encode_sequence(struct nfsd4_compoundres *resp, __be32 nfserr,
  3499. struct nfsd4_sequence *seq)
  3500. {
  3501. struct xdr_stream *xdr = &resp->xdr;
  3502. __be32 *p;
  3503. if (nfserr)
  3504. return nfserr;
  3505. p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN + 20);
  3506. if (!p)
  3507. return nfserr_resource;
  3508. p = xdr_encode_opaque_fixed(p, seq->sessionid.data,
  3509. NFS4_MAX_SESSIONID_LEN);
  3510. *p++ = cpu_to_be32(seq->seqid);
  3511. *p++ = cpu_to_be32(seq->slotid);
  3512. /* Note slotid's are numbered from zero: */
  3513. *p++ = cpu_to_be32(seq->maxslots - 1); /* sr_highest_slotid */
  3514. *p++ = cpu_to_be32(seq->maxslots - 1); /* sr_target_highest_slotid */
  3515. *p++ = cpu_to_be32(seq->status_flags);
  3516. resp->cstate.data_offset = xdr->buf->len; /* DRC cache data pointer */
  3517. return 0;
  3518. }
  3519. static __be32
  3520. nfsd4_encode_test_stateid(struct nfsd4_compoundres *resp, __be32 nfserr,
  3521. struct nfsd4_test_stateid *test_stateid)
  3522. {
  3523. struct xdr_stream *xdr = &resp->xdr;
  3524. struct nfsd4_test_stateid_id *stateid, *next;
  3525. __be32 *p;
  3526. if (nfserr)
  3527. return nfserr;
  3528. p = xdr_reserve_space(xdr, 4 + (4 * test_stateid->ts_num_ids));
  3529. if (!p)
  3530. return nfserr_resource;
  3531. *p++ = htonl(test_stateid->ts_num_ids);
  3532. list_for_each_entry_safe(stateid, next, &test_stateid->ts_stateid_list, ts_id_list) {
  3533. *p++ = stateid->ts_id_status;
  3534. }
  3535. return nfserr;
  3536. }
  3537. #ifdef CONFIG_NFSD_PNFS
  3538. static __be32
  3539. nfsd4_encode_getdeviceinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
  3540. struct nfsd4_getdeviceinfo *gdev)
  3541. {
  3542. struct xdr_stream *xdr = &resp->xdr;
  3543. const struct nfsd4_layout_ops *ops =
  3544. nfsd4_layout_ops[gdev->gd_layout_type];
  3545. u32 starting_len = xdr->buf->len, needed_len;
  3546. __be32 *p;
  3547. dprintk("%s: err %d\n", __func__, nfserr);
  3548. if (nfserr)
  3549. goto out;
  3550. nfserr = nfserr_resource;
  3551. p = xdr_reserve_space(xdr, 4);
  3552. if (!p)
  3553. goto out;
  3554. *p++ = cpu_to_be32(gdev->gd_layout_type);
  3555. /* If maxcount is 0 then just update notifications */
  3556. if (gdev->gd_maxcount != 0) {
  3557. nfserr = ops->encode_getdeviceinfo(xdr, gdev);
  3558. if (nfserr) {
  3559. /*
  3560. * We don't bother to burden the layout drivers with
  3561. * enforcing gd_maxcount, just tell the client to
  3562. * come back with a bigger buffer if it's not enough.
  3563. */
  3564. if (xdr->buf->len + 4 > gdev->gd_maxcount)
  3565. goto toosmall;
  3566. goto out;
  3567. }
  3568. }
  3569. nfserr = nfserr_resource;
  3570. if (gdev->gd_notify_types) {
  3571. p = xdr_reserve_space(xdr, 4 + 4);
  3572. if (!p)
  3573. goto out;
  3574. *p++ = cpu_to_be32(1); /* bitmap length */
  3575. *p++ = cpu_to_be32(gdev->gd_notify_types);
  3576. } else {
  3577. p = xdr_reserve_space(xdr, 4);
  3578. if (!p)
  3579. goto out;
  3580. *p++ = 0;
  3581. }
  3582. nfserr = 0;
  3583. out:
  3584. kfree(gdev->gd_device);
  3585. dprintk("%s: done: %d\n", __func__, be32_to_cpu(nfserr));
  3586. return nfserr;
  3587. toosmall:
  3588. dprintk("%s: maxcount too small\n", __func__);
  3589. needed_len = xdr->buf->len + 4 /* notifications */;
  3590. xdr_truncate_encode(xdr, starting_len);
  3591. p = xdr_reserve_space(xdr, 4);
  3592. if (!p) {
  3593. nfserr = nfserr_resource;
  3594. } else {
  3595. *p++ = cpu_to_be32(needed_len);
  3596. nfserr = nfserr_toosmall;
  3597. }
  3598. goto out;
  3599. }
  3600. static __be32
  3601. nfsd4_encode_layoutget(struct nfsd4_compoundres *resp, __be32 nfserr,
  3602. struct nfsd4_layoutget *lgp)
  3603. {
  3604. struct xdr_stream *xdr = &resp->xdr;
  3605. const struct nfsd4_layout_ops *ops =
  3606. nfsd4_layout_ops[lgp->lg_layout_type];
  3607. __be32 *p;
  3608. dprintk("%s: err %d\n", __func__, nfserr);
  3609. if (nfserr)
  3610. goto out;
  3611. nfserr = nfserr_resource;
  3612. p = xdr_reserve_space(xdr, 36 + sizeof(stateid_opaque_t));
  3613. if (!p)
  3614. goto out;
  3615. *p++ = cpu_to_be32(1); /* we always set return-on-close */
  3616. *p++ = cpu_to_be32(lgp->lg_sid.si_generation);
  3617. p = xdr_encode_opaque_fixed(p, &lgp->lg_sid.si_opaque,
  3618. sizeof(stateid_opaque_t));
  3619. *p++ = cpu_to_be32(1); /* we always return a single layout */
  3620. p = xdr_encode_hyper(p, lgp->lg_seg.offset);
  3621. p = xdr_encode_hyper(p, lgp->lg_seg.length);
  3622. *p++ = cpu_to_be32(lgp->lg_seg.iomode);
  3623. *p++ = cpu_to_be32(lgp->lg_layout_type);
  3624. nfserr = ops->encode_layoutget(xdr, lgp);
  3625. out:
  3626. kfree(lgp->lg_content);
  3627. return nfserr;
  3628. }
  3629. static __be32
  3630. nfsd4_encode_layoutcommit(struct nfsd4_compoundres *resp, __be32 nfserr,
  3631. struct nfsd4_layoutcommit *lcp)
  3632. {
  3633. struct xdr_stream *xdr = &resp->xdr;
  3634. __be32 *p;
  3635. if (nfserr)
  3636. return nfserr;
  3637. p = xdr_reserve_space(xdr, 4);
  3638. if (!p)
  3639. return nfserr_resource;
  3640. *p++ = cpu_to_be32(lcp->lc_size_chg);
  3641. if (lcp->lc_size_chg) {
  3642. p = xdr_reserve_space(xdr, 8);
  3643. if (!p)
  3644. return nfserr_resource;
  3645. p = xdr_encode_hyper(p, lcp->lc_newsize);
  3646. }
  3647. return nfs_ok;
  3648. }
  3649. static __be32
  3650. nfsd4_encode_layoutreturn(struct nfsd4_compoundres *resp, __be32 nfserr,
  3651. struct nfsd4_layoutreturn *lrp)
  3652. {
  3653. struct xdr_stream *xdr = &resp->xdr;
  3654. __be32 *p;
  3655. if (nfserr)
  3656. return nfserr;
  3657. p = xdr_reserve_space(xdr, 4);
  3658. if (!p)
  3659. return nfserr_resource;
  3660. *p++ = cpu_to_be32(lrp->lrs_present);
  3661. if (lrp->lrs_present)
  3662. return nfsd4_encode_stateid(xdr, &lrp->lr_sid);
  3663. return nfs_ok;
  3664. }
  3665. #endif /* CONFIG_NFSD_PNFS */
  3666. static __be32
  3667. nfsd4_encode_seek(struct nfsd4_compoundres *resp, __be32 nfserr,
  3668. struct nfsd4_seek *seek)
  3669. {
  3670. __be32 *p;
  3671. if (nfserr)
  3672. return nfserr;
  3673. p = xdr_reserve_space(&resp->xdr, 4 + 8);
  3674. *p++ = cpu_to_be32(seek->seek_eof);
  3675. p = xdr_encode_hyper(p, seek->seek_pos);
  3676. return nfserr;
  3677. }
  3678. static __be32
  3679. nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr, void *p)
  3680. {
  3681. return nfserr;
  3682. }
  3683. typedef __be32(* nfsd4_enc)(struct nfsd4_compoundres *, __be32, void *);
  3684. /*
  3685. * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
  3686. * since we don't need to filter out obsolete ops as this is
  3687. * done in the decoding phase.
  3688. */
  3689. static nfsd4_enc nfsd4_enc_ops[] = {
  3690. [OP_ACCESS] = (nfsd4_enc)nfsd4_encode_access,
  3691. [OP_CLOSE] = (nfsd4_enc)nfsd4_encode_close,
  3692. [OP_COMMIT] = (nfsd4_enc)nfsd4_encode_commit,
  3693. [OP_CREATE] = (nfsd4_enc)nfsd4_encode_create,
  3694. [OP_DELEGPURGE] = (nfsd4_enc)nfsd4_encode_noop,
  3695. [OP_DELEGRETURN] = (nfsd4_enc)nfsd4_encode_noop,
  3696. [OP_GETATTR] = (nfsd4_enc)nfsd4_encode_getattr,
  3697. [OP_GETFH] = (nfsd4_enc)nfsd4_encode_getfh,
  3698. [OP_LINK] = (nfsd4_enc)nfsd4_encode_link,
  3699. [OP_LOCK] = (nfsd4_enc)nfsd4_encode_lock,
  3700. [OP_LOCKT] = (nfsd4_enc)nfsd4_encode_lockt,
  3701. [OP_LOCKU] = (nfsd4_enc)nfsd4_encode_locku,
  3702. [OP_LOOKUP] = (nfsd4_enc)nfsd4_encode_noop,
  3703. [OP_LOOKUPP] = (nfsd4_enc)nfsd4_encode_noop,
  3704. [OP_NVERIFY] = (nfsd4_enc)nfsd4_encode_noop,
  3705. [OP_OPEN] = (nfsd4_enc)nfsd4_encode_open,
  3706. [OP_OPENATTR] = (nfsd4_enc)nfsd4_encode_noop,
  3707. [OP_OPEN_CONFIRM] = (nfsd4_enc)nfsd4_encode_open_confirm,
  3708. [OP_OPEN_DOWNGRADE] = (nfsd4_enc)nfsd4_encode_open_downgrade,
  3709. [OP_PUTFH] = (nfsd4_enc)nfsd4_encode_noop,
  3710. [OP_PUTPUBFH] = (nfsd4_enc)nfsd4_encode_noop,
  3711. [OP_PUTROOTFH] = (nfsd4_enc)nfsd4_encode_noop,
  3712. [OP_READ] = (nfsd4_enc)nfsd4_encode_read,
  3713. [OP_READDIR] = (nfsd4_enc)nfsd4_encode_readdir,
  3714. [OP_READLINK] = (nfsd4_enc)nfsd4_encode_readlink,
  3715. [OP_REMOVE] = (nfsd4_enc)nfsd4_encode_remove,
  3716. [OP_RENAME] = (nfsd4_enc)nfsd4_encode_rename,
  3717. [OP_RENEW] = (nfsd4_enc)nfsd4_encode_noop,
  3718. [OP_RESTOREFH] = (nfsd4_enc)nfsd4_encode_noop,
  3719. [OP_SAVEFH] = (nfsd4_enc)nfsd4_encode_noop,
  3720. [OP_SECINFO] = (nfsd4_enc)nfsd4_encode_secinfo,
  3721. [OP_SETATTR] = (nfsd4_enc)nfsd4_encode_setattr,
  3722. [OP_SETCLIENTID] = (nfsd4_enc)nfsd4_encode_setclientid,
  3723. [OP_SETCLIENTID_CONFIRM] = (nfsd4_enc)nfsd4_encode_noop,
  3724. [OP_VERIFY] = (nfsd4_enc)nfsd4_encode_noop,
  3725. [OP_WRITE] = (nfsd4_enc)nfsd4_encode_write,
  3726. [OP_RELEASE_LOCKOWNER] = (nfsd4_enc)nfsd4_encode_noop,
  3727. /* NFSv4.1 operations */
  3728. [OP_BACKCHANNEL_CTL] = (nfsd4_enc)nfsd4_encode_noop,
  3729. [OP_BIND_CONN_TO_SESSION] = (nfsd4_enc)nfsd4_encode_bind_conn_to_session,
  3730. [OP_EXCHANGE_ID] = (nfsd4_enc)nfsd4_encode_exchange_id,
  3731. [OP_CREATE_SESSION] = (nfsd4_enc)nfsd4_encode_create_session,
  3732. [OP_DESTROY_SESSION] = (nfsd4_enc)nfsd4_encode_noop,
  3733. [OP_FREE_STATEID] = (nfsd4_enc)nfsd4_encode_noop,
  3734. [OP_GET_DIR_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
  3735. #ifdef CONFIG_NFSD_PNFS
  3736. [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_getdeviceinfo,
  3737. [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
  3738. [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_layoutcommit,
  3739. [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_layoutget,
  3740. [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_layoutreturn,
  3741. #else
  3742. [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_noop,
  3743. [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
  3744. [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_noop,
  3745. [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_noop,
  3746. [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_noop,
  3747. #endif
  3748. [OP_SECINFO_NO_NAME] = (nfsd4_enc)nfsd4_encode_secinfo_no_name,
  3749. [OP_SEQUENCE] = (nfsd4_enc)nfsd4_encode_sequence,
  3750. [OP_SET_SSV] = (nfsd4_enc)nfsd4_encode_noop,
  3751. [OP_TEST_STATEID] = (nfsd4_enc)nfsd4_encode_test_stateid,
  3752. [OP_WANT_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
  3753. [OP_DESTROY_CLIENTID] = (nfsd4_enc)nfsd4_encode_noop,
  3754. [OP_RECLAIM_COMPLETE] = (nfsd4_enc)nfsd4_encode_noop,
  3755. /* NFSv4.2 operations */
  3756. [OP_ALLOCATE] = (nfsd4_enc)nfsd4_encode_noop,
  3757. [OP_COPY] = (nfsd4_enc)nfsd4_encode_noop,
  3758. [OP_COPY_NOTIFY] = (nfsd4_enc)nfsd4_encode_noop,
  3759. [OP_DEALLOCATE] = (nfsd4_enc)nfsd4_encode_noop,
  3760. [OP_IO_ADVISE] = (nfsd4_enc)nfsd4_encode_noop,
  3761. [OP_LAYOUTERROR] = (nfsd4_enc)nfsd4_encode_noop,
  3762. [OP_LAYOUTSTATS] = (nfsd4_enc)nfsd4_encode_noop,
  3763. [OP_OFFLOAD_CANCEL] = (nfsd4_enc)nfsd4_encode_noop,
  3764. [OP_OFFLOAD_STATUS] = (nfsd4_enc)nfsd4_encode_noop,
  3765. [OP_READ_PLUS] = (nfsd4_enc)nfsd4_encode_noop,
  3766. [OP_SEEK] = (nfsd4_enc)nfsd4_encode_seek,
  3767. [OP_WRITE_SAME] = (nfsd4_enc)nfsd4_encode_noop,
  3768. };
  3769. /*
  3770. * Calculate whether we still have space to encode repsize bytes.
  3771. * There are two considerations:
  3772. * - For NFS versions >=4.1, the size of the reply must stay within
  3773. * session limits
  3774. * - For all NFS versions, we must stay within limited preallocated
  3775. * buffer space.
  3776. *
  3777. * This is called before the operation is processed, so can only provide
  3778. * an upper estimate. For some nonidempotent operations (such as
  3779. * getattr), it's not necessarily a problem if that estimate is wrong,
  3780. * as we can fail it after processing without significant side effects.
  3781. */
  3782. __be32 nfsd4_check_resp_size(struct nfsd4_compoundres *resp, u32 respsize)
  3783. {
  3784. struct xdr_buf *buf = &resp->rqstp->rq_res;
  3785. struct nfsd4_slot *slot = resp->cstate.slot;
  3786. if (buf->len + respsize <= buf->buflen)
  3787. return nfs_ok;
  3788. if (!nfsd4_has_session(&resp->cstate))
  3789. return nfserr_resource;
  3790. if (slot->sl_flags & NFSD4_SLOT_CACHETHIS) {
  3791. WARN_ON_ONCE(1);
  3792. return nfserr_rep_too_big_to_cache;
  3793. }
  3794. return nfserr_rep_too_big;
  3795. }
  3796. void
  3797. nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
  3798. {
  3799. struct xdr_stream *xdr = &resp->xdr;
  3800. struct nfs4_stateowner *so = resp->cstate.replay_owner;
  3801. struct svc_rqst *rqstp = resp->rqstp;
  3802. int post_err_offset;
  3803. nfsd4_enc encoder;
  3804. __be32 *p;
  3805. p = xdr_reserve_space(xdr, 8);
  3806. if (!p) {
  3807. WARN_ON_ONCE(1);
  3808. return;
  3809. }
  3810. *p++ = cpu_to_be32(op->opnum);
  3811. post_err_offset = xdr->buf->len;
  3812. if (op->opnum == OP_ILLEGAL)
  3813. goto status;
  3814. BUG_ON(op->opnum < 0 || op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
  3815. !nfsd4_enc_ops[op->opnum]);
  3816. encoder = nfsd4_enc_ops[op->opnum];
  3817. op->status = encoder(resp, op->status, &op->u);
  3818. xdr_commit_encode(xdr);
  3819. /* nfsd4_check_resp_size guarantees enough room for error status */
  3820. if (!op->status) {
  3821. int space_needed = 0;
  3822. if (!nfsd4_last_compound_op(rqstp))
  3823. space_needed = COMPOUND_ERR_SLACK_SPACE;
  3824. op->status = nfsd4_check_resp_size(resp, space_needed);
  3825. }
  3826. if (op->status == nfserr_resource && nfsd4_has_session(&resp->cstate)) {
  3827. struct nfsd4_slot *slot = resp->cstate.slot;
  3828. if (slot->sl_flags & NFSD4_SLOT_CACHETHIS)
  3829. op->status = nfserr_rep_too_big_to_cache;
  3830. else
  3831. op->status = nfserr_rep_too_big;
  3832. }
  3833. if (op->status == nfserr_resource ||
  3834. op->status == nfserr_rep_too_big ||
  3835. op->status == nfserr_rep_too_big_to_cache) {
  3836. /*
  3837. * The operation may have already been encoded or
  3838. * partially encoded. No op returns anything additional
  3839. * in the case of one of these three errors, so we can
  3840. * just truncate back to after the status. But it's a
  3841. * bug if we had to do this on a non-idempotent op:
  3842. */
  3843. warn_on_nonidempotent_op(op);
  3844. xdr_truncate_encode(xdr, post_err_offset);
  3845. }
  3846. if (so) {
  3847. int len = xdr->buf->len - post_err_offset;
  3848. so->so_replay.rp_status = op->status;
  3849. so->so_replay.rp_buflen = len;
  3850. read_bytes_from_xdr_buf(xdr->buf, post_err_offset,
  3851. so->so_replay.rp_buf, len);
  3852. }
  3853. status:
  3854. /* Note that op->status is already in network byte order: */
  3855. write_bytes_to_xdr_buf(xdr->buf, post_err_offset - 4, &op->status, 4);
  3856. }
  3857. /*
  3858. * Encode the reply stored in the stateowner reply cache
  3859. *
  3860. * XDR note: do not encode rp->rp_buflen: the buffer contains the
  3861. * previously sent already encoded operation.
  3862. */
  3863. void
  3864. nfsd4_encode_replay(struct xdr_stream *xdr, struct nfsd4_op *op)
  3865. {
  3866. __be32 *p;
  3867. struct nfs4_replay *rp = op->replay;
  3868. BUG_ON(!rp);
  3869. p = xdr_reserve_space(xdr, 8 + rp->rp_buflen);
  3870. if (!p) {
  3871. WARN_ON_ONCE(1);
  3872. return;
  3873. }
  3874. *p++ = cpu_to_be32(op->opnum);
  3875. *p++ = rp->rp_status; /* already xdr'ed */
  3876. p = xdr_encode_opaque_fixed(p, rp->rp_buf, rp->rp_buflen);
  3877. }
  3878. int
  3879. nfs4svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
  3880. {
  3881. return xdr_ressize_check(rqstp, p);
  3882. }
  3883. int nfsd4_release_compoundargs(void *rq, __be32 *p, void *resp)
  3884. {
  3885. struct svc_rqst *rqstp = rq;
  3886. struct nfsd4_compoundargs *args = rqstp->rq_argp;
  3887. if (args->ops != args->iops) {
  3888. kfree(args->ops);
  3889. args->ops = args->iops;
  3890. }
  3891. kfree(args->tmpp);
  3892. args->tmpp = NULL;
  3893. while (args->to_free) {
  3894. struct svcxdr_tmpbuf *tb = args->to_free;
  3895. args->to_free = tb->next;
  3896. kfree(tb);
  3897. }
  3898. return 1;
  3899. }
  3900. int
  3901. nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundargs *args)
  3902. {
  3903. if (rqstp->rq_arg.head[0].iov_len % 4) {
  3904. /* client is nuts */
  3905. dprintk("%s: compound not properly padded! (peeraddr=%pISc xid=0x%x)",
  3906. __func__, svc_addr(rqstp), be32_to_cpu(rqstp->rq_xid));
  3907. return 0;
  3908. }
  3909. args->p = p;
  3910. args->end = rqstp->rq_arg.head[0].iov_base + rqstp->rq_arg.head[0].iov_len;
  3911. args->pagelist = rqstp->rq_arg.pages;
  3912. args->pagelen = rqstp->rq_arg.page_len;
  3913. args->tmpp = NULL;
  3914. args->to_free = NULL;
  3915. args->ops = args->iops;
  3916. args->rqstp = rqstp;
  3917. return !nfsd4_decode_compound(args);
  3918. }
  3919. int
  3920. nfs4svc_encode_compoundres(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundres *resp)
  3921. {
  3922. /*
  3923. * All that remains is to write the tag and operation count...
  3924. */
  3925. struct xdr_buf *buf = resp->xdr.buf;
  3926. WARN_ON_ONCE(buf->len != buf->head[0].iov_len + buf->page_len +
  3927. buf->tail[0].iov_len);
  3928. rqstp->rq_next_page = resp->xdr.page_ptr + 1;
  3929. p = resp->tagp;
  3930. *p++ = htonl(resp->taglen);
  3931. memcpy(p, resp->tag, resp->taglen);
  3932. p += XDR_QUADLEN(resp->taglen);
  3933. *p++ = htonl(resp->opcnt);
  3934. nfsd4_sequence_done(resp);
  3935. return 1;
  3936. }
  3937. /*
  3938. * Local variables:
  3939. * c-basic-offset: 8
  3940. * End:
  3941. */