nfs4xdr.c 98 KB

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