nfs4xdr.c 102 KB

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