nfs4xdr.c 104 KB

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