nfs4xdr.c 91 KB

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