nfs4xdr.c 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746
  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_resource;
  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. WARN_ON(avail != (XDR_QUADLEN(avail) << 2));
  1079. write->wr_pagelist = argp->pagelist;
  1080. len = XDR_QUADLEN(write->wr_buflen) << 2;
  1081. if (len >= avail) {
  1082. int pages;
  1083. len -= avail;
  1084. pages = len >> PAGE_SHIFT;
  1085. argp->pagelist += pages;
  1086. argp->pagelen -= pages * PAGE_SIZE;
  1087. len -= pages * PAGE_SIZE;
  1088. argp->p = (__be32 *)page_address(argp->pagelist[0]);
  1089. argp->pagelist++;
  1090. argp->end = argp->p + XDR_QUADLEN(PAGE_SIZE);
  1091. }
  1092. argp->p += XDR_QUADLEN(len);
  1093. DECODE_TAIL;
  1094. }
  1095. static __be32
  1096. nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp, struct nfsd4_release_lockowner *rlockowner)
  1097. {
  1098. DECODE_HEAD;
  1099. if (argp->minorversion >= 1)
  1100. return nfserr_notsupp;
  1101. READ_BUF(12);
  1102. COPYMEM(&rlockowner->rl_clientid, sizeof(clientid_t));
  1103. READ32(rlockowner->rl_owner.len);
  1104. READ_BUF(rlockowner->rl_owner.len);
  1105. READMEM(rlockowner->rl_owner.data, rlockowner->rl_owner.len);
  1106. if (argp->minorversion && !zero_clientid(&rlockowner->rl_clientid))
  1107. return nfserr_inval;
  1108. DECODE_TAIL;
  1109. }
  1110. static __be32
  1111. nfsd4_decode_exchange_id(struct nfsd4_compoundargs *argp,
  1112. struct nfsd4_exchange_id *exid)
  1113. {
  1114. int dummy, tmp;
  1115. DECODE_HEAD;
  1116. READ_BUF(NFS4_VERIFIER_SIZE);
  1117. COPYMEM(exid->verifier.data, NFS4_VERIFIER_SIZE);
  1118. status = nfsd4_decode_opaque(argp, &exid->clname);
  1119. if (status)
  1120. return nfserr_bad_xdr;
  1121. READ_BUF(4);
  1122. READ32(exid->flags);
  1123. /* Ignore state_protect4_a */
  1124. READ_BUF(4);
  1125. READ32(exid->spa_how);
  1126. switch (exid->spa_how) {
  1127. case SP4_NONE:
  1128. break;
  1129. case SP4_MACH_CRED:
  1130. /* spo_must_enforce */
  1131. READ_BUF(4);
  1132. READ32(dummy);
  1133. READ_BUF(dummy * 4);
  1134. p += dummy;
  1135. /* spo_must_allow */
  1136. READ_BUF(4);
  1137. READ32(dummy);
  1138. READ_BUF(dummy * 4);
  1139. p += dummy;
  1140. break;
  1141. case SP4_SSV:
  1142. /* ssp_ops */
  1143. READ_BUF(4);
  1144. READ32(dummy);
  1145. READ_BUF(dummy * 4);
  1146. p += dummy;
  1147. READ_BUF(4);
  1148. READ32(dummy);
  1149. READ_BUF(dummy * 4);
  1150. p += dummy;
  1151. /* ssp_hash_algs<> */
  1152. READ_BUF(4);
  1153. READ32(tmp);
  1154. while (tmp--) {
  1155. READ_BUF(4);
  1156. READ32(dummy);
  1157. READ_BUF(dummy);
  1158. p += XDR_QUADLEN(dummy);
  1159. }
  1160. /* ssp_encr_algs<> */
  1161. READ_BUF(4);
  1162. READ32(tmp);
  1163. while (tmp--) {
  1164. READ_BUF(4);
  1165. READ32(dummy);
  1166. READ_BUF(dummy);
  1167. p += XDR_QUADLEN(dummy);
  1168. }
  1169. /* ssp_window and ssp_num_gss_handles */
  1170. READ_BUF(8);
  1171. READ32(dummy);
  1172. READ32(dummy);
  1173. break;
  1174. default:
  1175. goto xdr_error;
  1176. }
  1177. /* Ignore Implementation ID */
  1178. READ_BUF(4); /* nfs_impl_id4 array length */
  1179. READ32(dummy);
  1180. if (dummy > 1)
  1181. goto xdr_error;
  1182. if (dummy == 1) {
  1183. /* nii_domain */
  1184. READ_BUF(4);
  1185. READ32(dummy);
  1186. READ_BUF(dummy);
  1187. p += XDR_QUADLEN(dummy);
  1188. /* nii_name */
  1189. READ_BUF(4);
  1190. READ32(dummy);
  1191. READ_BUF(dummy);
  1192. p += XDR_QUADLEN(dummy);
  1193. /* nii_date */
  1194. READ_BUF(12);
  1195. p += 3;
  1196. }
  1197. DECODE_TAIL;
  1198. }
  1199. static __be32
  1200. nfsd4_decode_create_session(struct nfsd4_compoundargs *argp,
  1201. struct nfsd4_create_session *sess)
  1202. {
  1203. DECODE_HEAD;
  1204. u32 dummy;
  1205. READ_BUF(16);
  1206. COPYMEM(&sess->clientid, 8);
  1207. READ32(sess->seqid);
  1208. READ32(sess->flags);
  1209. /* Fore channel attrs */
  1210. READ_BUF(28);
  1211. READ32(dummy); /* headerpadsz is always 0 */
  1212. READ32(sess->fore_channel.maxreq_sz);
  1213. READ32(sess->fore_channel.maxresp_sz);
  1214. READ32(sess->fore_channel.maxresp_cached);
  1215. READ32(sess->fore_channel.maxops);
  1216. READ32(sess->fore_channel.maxreqs);
  1217. READ32(sess->fore_channel.nr_rdma_attrs);
  1218. if (sess->fore_channel.nr_rdma_attrs == 1) {
  1219. READ_BUF(4);
  1220. READ32(sess->fore_channel.rdma_attrs);
  1221. } else if (sess->fore_channel.nr_rdma_attrs > 1) {
  1222. dprintk("Too many fore channel attr bitmaps!\n");
  1223. goto xdr_error;
  1224. }
  1225. /* Back channel attrs */
  1226. READ_BUF(28);
  1227. READ32(dummy); /* headerpadsz is always 0 */
  1228. READ32(sess->back_channel.maxreq_sz);
  1229. READ32(sess->back_channel.maxresp_sz);
  1230. READ32(sess->back_channel.maxresp_cached);
  1231. READ32(sess->back_channel.maxops);
  1232. READ32(sess->back_channel.maxreqs);
  1233. READ32(sess->back_channel.nr_rdma_attrs);
  1234. if (sess->back_channel.nr_rdma_attrs == 1) {
  1235. READ_BUF(4);
  1236. READ32(sess->back_channel.rdma_attrs);
  1237. } else if (sess->back_channel.nr_rdma_attrs > 1) {
  1238. dprintk("Too many back channel attr bitmaps!\n");
  1239. goto xdr_error;
  1240. }
  1241. READ_BUF(4);
  1242. READ32(sess->callback_prog);
  1243. nfsd4_decode_cb_sec(argp, &sess->cb_sec);
  1244. DECODE_TAIL;
  1245. }
  1246. static __be32
  1247. nfsd4_decode_destroy_session(struct nfsd4_compoundargs *argp,
  1248. struct nfsd4_destroy_session *destroy_session)
  1249. {
  1250. DECODE_HEAD;
  1251. READ_BUF(NFS4_MAX_SESSIONID_LEN);
  1252. COPYMEM(destroy_session->sessionid.data, NFS4_MAX_SESSIONID_LEN);
  1253. DECODE_TAIL;
  1254. }
  1255. static __be32
  1256. nfsd4_decode_free_stateid(struct nfsd4_compoundargs *argp,
  1257. struct nfsd4_free_stateid *free_stateid)
  1258. {
  1259. DECODE_HEAD;
  1260. READ_BUF(sizeof(stateid_t));
  1261. READ32(free_stateid->fr_stateid.si_generation);
  1262. COPYMEM(&free_stateid->fr_stateid.si_opaque, sizeof(stateid_opaque_t));
  1263. DECODE_TAIL;
  1264. }
  1265. static __be32
  1266. nfsd4_decode_sequence(struct nfsd4_compoundargs *argp,
  1267. struct nfsd4_sequence *seq)
  1268. {
  1269. DECODE_HEAD;
  1270. READ_BUF(NFS4_MAX_SESSIONID_LEN + 16);
  1271. COPYMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
  1272. READ32(seq->seqid);
  1273. READ32(seq->slotid);
  1274. READ32(seq->maxslots);
  1275. READ32(seq->cachethis);
  1276. DECODE_TAIL;
  1277. }
  1278. static __be32
  1279. nfsd4_decode_test_stateid(struct nfsd4_compoundargs *argp, struct nfsd4_test_stateid *test_stateid)
  1280. {
  1281. int i;
  1282. __be32 *p, status;
  1283. struct nfsd4_test_stateid_id *stateid;
  1284. READ_BUF(4);
  1285. test_stateid->ts_num_ids = ntohl(*p++);
  1286. INIT_LIST_HEAD(&test_stateid->ts_stateid_list);
  1287. for (i = 0; i < test_stateid->ts_num_ids; i++) {
  1288. stateid = kmalloc(sizeof(struct nfsd4_test_stateid_id), GFP_KERNEL);
  1289. if (!stateid) {
  1290. status = nfserrno(-ENOMEM);
  1291. goto out;
  1292. }
  1293. defer_free(argp, kfree, stateid);
  1294. INIT_LIST_HEAD(&stateid->ts_id_list);
  1295. list_add_tail(&stateid->ts_id_list, &test_stateid->ts_stateid_list);
  1296. status = nfsd4_decode_stateid(argp, &stateid->ts_id_stateid);
  1297. if (status)
  1298. goto out;
  1299. }
  1300. status = 0;
  1301. out:
  1302. return status;
  1303. xdr_error:
  1304. dprintk("NFSD: xdr error (%s:%d)\n", __FILE__, __LINE__);
  1305. status = nfserr_bad_xdr;
  1306. goto out;
  1307. }
  1308. static __be32 nfsd4_decode_destroy_clientid(struct nfsd4_compoundargs *argp, struct nfsd4_destroy_clientid *dc)
  1309. {
  1310. DECODE_HEAD;
  1311. READ_BUF(8);
  1312. COPYMEM(&dc->clientid, 8);
  1313. DECODE_TAIL;
  1314. }
  1315. static __be32 nfsd4_decode_reclaim_complete(struct nfsd4_compoundargs *argp, struct nfsd4_reclaim_complete *rc)
  1316. {
  1317. DECODE_HEAD;
  1318. READ_BUF(4);
  1319. READ32(rc->rca_one_fs);
  1320. DECODE_TAIL;
  1321. }
  1322. static __be32
  1323. nfsd4_decode_noop(struct nfsd4_compoundargs *argp, void *p)
  1324. {
  1325. return nfs_ok;
  1326. }
  1327. static __be32
  1328. nfsd4_decode_notsupp(struct nfsd4_compoundargs *argp, void *p)
  1329. {
  1330. return nfserr_notsupp;
  1331. }
  1332. typedef __be32(*nfsd4_dec)(struct nfsd4_compoundargs *argp, void *);
  1333. static nfsd4_dec nfsd4_dec_ops[] = {
  1334. [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
  1335. [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
  1336. [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
  1337. [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
  1338. [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
  1339. [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
  1340. [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
  1341. [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
  1342. [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
  1343. [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
  1344. [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
  1345. [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
  1346. [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
  1347. [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
  1348. [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
  1349. [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
  1350. [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
  1351. [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_open_confirm,
  1352. [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
  1353. [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
  1354. [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_putpubfh,
  1355. [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
  1356. [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
  1357. [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
  1358. [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
  1359. [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
  1360. [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
  1361. [OP_RENEW] = (nfsd4_dec)nfsd4_decode_renew,
  1362. [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
  1363. [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
  1364. [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
  1365. [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
  1366. [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_setclientid,
  1367. [OP_SETCLIENTID_CONFIRM] = (nfsd4_dec)nfsd4_decode_setclientid_confirm,
  1368. [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
  1369. [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
  1370. [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_release_lockowner,
  1371. /* new operations for NFSv4.1 */
  1372. [OP_BACKCHANNEL_CTL] = (nfsd4_dec)nfsd4_decode_backchannel_ctl,
  1373. [OP_BIND_CONN_TO_SESSION]= (nfsd4_dec)nfsd4_decode_bind_conn_to_session,
  1374. [OP_EXCHANGE_ID] = (nfsd4_dec)nfsd4_decode_exchange_id,
  1375. [OP_CREATE_SESSION] = (nfsd4_dec)nfsd4_decode_create_session,
  1376. [OP_DESTROY_SESSION] = (nfsd4_dec)nfsd4_decode_destroy_session,
  1377. [OP_FREE_STATEID] = (nfsd4_dec)nfsd4_decode_free_stateid,
  1378. [OP_GET_DIR_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
  1379. [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_notsupp,
  1380. [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
  1381. [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_notsupp,
  1382. [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_notsupp,
  1383. [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_notsupp,
  1384. [OP_SECINFO_NO_NAME] = (nfsd4_dec)nfsd4_decode_secinfo_no_name,
  1385. [OP_SEQUENCE] = (nfsd4_dec)nfsd4_decode_sequence,
  1386. [OP_SET_SSV] = (nfsd4_dec)nfsd4_decode_notsupp,
  1387. [OP_TEST_STATEID] = (nfsd4_dec)nfsd4_decode_test_stateid,
  1388. [OP_WANT_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
  1389. [OP_DESTROY_CLIENTID] = (nfsd4_dec)nfsd4_decode_destroy_clientid,
  1390. [OP_RECLAIM_COMPLETE] = (nfsd4_dec)nfsd4_decode_reclaim_complete,
  1391. };
  1392. static inline bool
  1393. nfsd4_opnum_in_range(struct nfsd4_compoundargs *argp, struct nfsd4_op *op)
  1394. {
  1395. if (op->opnum < FIRST_NFS4_OP)
  1396. return false;
  1397. else if (argp->minorversion == 0 && op->opnum > LAST_NFS40_OP)
  1398. return false;
  1399. else if (argp->minorversion == 1 && op->opnum > LAST_NFS41_OP)
  1400. return false;
  1401. else if (argp->minorversion == 2 && op->opnum > LAST_NFS42_OP)
  1402. return false;
  1403. return true;
  1404. }
  1405. /*
  1406. * Return a rough estimate of the maximum possible reply size. Note the
  1407. * estimate includes rpc headers so is meant to be passed to
  1408. * svc_reserve, not svc_reserve_auth.
  1409. *
  1410. * Also note the current compound encoding permits only one operation to
  1411. * use pages beyond the first one, so the maximum possible length is the
  1412. * maximum over these values, not the sum.
  1413. */
  1414. static int nfsd4_max_reply(u32 opnum)
  1415. {
  1416. switch (opnum) {
  1417. case OP_READLINK:
  1418. case OP_READDIR:
  1419. /*
  1420. * Both of these ops take a single page for data and put
  1421. * the head and tail in another page:
  1422. */
  1423. return 2 * PAGE_SIZE;
  1424. case OP_READ:
  1425. return INT_MAX;
  1426. default:
  1427. return PAGE_SIZE;
  1428. }
  1429. }
  1430. static __be32
  1431. nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
  1432. {
  1433. DECODE_HEAD;
  1434. struct nfsd4_op *op;
  1435. bool cachethis = false;
  1436. int max_reply = PAGE_SIZE;
  1437. int i;
  1438. READ_BUF(4);
  1439. READ32(argp->taglen);
  1440. READ_BUF(argp->taglen + 8);
  1441. SAVEMEM(argp->tag, argp->taglen);
  1442. READ32(argp->minorversion);
  1443. READ32(argp->opcnt);
  1444. if (argp->taglen > NFSD4_MAX_TAGLEN)
  1445. goto xdr_error;
  1446. if (argp->opcnt > 100)
  1447. goto xdr_error;
  1448. if (argp->opcnt > ARRAY_SIZE(argp->iops)) {
  1449. argp->ops = kmalloc(argp->opcnt * sizeof(*argp->ops), GFP_KERNEL);
  1450. if (!argp->ops) {
  1451. argp->ops = argp->iops;
  1452. dprintk("nfsd: couldn't allocate room for COMPOUND\n");
  1453. goto xdr_error;
  1454. }
  1455. }
  1456. if (argp->minorversion > NFSD_SUPPORTED_MINOR_VERSION)
  1457. argp->opcnt = 0;
  1458. for (i = 0; i < argp->opcnt; i++) {
  1459. op = &argp->ops[i];
  1460. op->replay = NULL;
  1461. READ_BUF(4);
  1462. READ32(op->opnum);
  1463. if (nfsd4_opnum_in_range(argp, op))
  1464. op->status = nfsd4_dec_ops[op->opnum](argp, &op->u);
  1465. else {
  1466. op->opnum = OP_ILLEGAL;
  1467. op->status = nfserr_op_illegal;
  1468. }
  1469. if (op->status) {
  1470. argp->opcnt = i+1;
  1471. break;
  1472. }
  1473. /*
  1474. * We'll try to cache the result in the DRC if any one
  1475. * op in the compound wants to be cached:
  1476. */
  1477. cachethis |= nfsd4_cache_this_op(op);
  1478. max_reply = max(max_reply, nfsd4_max_reply(op->opnum));
  1479. }
  1480. /* Sessions make the DRC unnecessary: */
  1481. if (argp->minorversion)
  1482. cachethis = false;
  1483. if (max_reply != INT_MAX)
  1484. svc_reserve(argp->rqstp, max_reply);
  1485. argp->rqstp->rq_cachetype = cachethis ? RC_REPLBUFF : RC_NOCACHE;
  1486. DECODE_TAIL;
  1487. }
  1488. #define WRITE32(n) *p++ = htonl(n)
  1489. #define WRITE64(n) do { \
  1490. *p++ = htonl((u32)((n) >> 32)); \
  1491. *p++ = htonl((u32)(n)); \
  1492. } while (0)
  1493. #define WRITEMEM(ptr,nbytes) do { if (nbytes > 0) { \
  1494. *(p + XDR_QUADLEN(nbytes) -1) = 0; \
  1495. memcpy(p, ptr, nbytes); \
  1496. p += XDR_QUADLEN(nbytes); \
  1497. }} while (0)
  1498. static void write32(__be32 **p, u32 n)
  1499. {
  1500. *(*p)++ = htonl(n);
  1501. }
  1502. static void write64(__be32 **p, u64 n)
  1503. {
  1504. write32(p, (n >> 32));
  1505. write32(p, (u32)n);
  1506. }
  1507. static void write_change(__be32 **p, struct kstat *stat, struct inode *inode)
  1508. {
  1509. if (IS_I_VERSION(inode)) {
  1510. write64(p, inode->i_version);
  1511. } else {
  1512. write32(p, stat->ctime.tv_sec);
  1513. write32(p, stat->ctime.tv_nsec);
  1514. }
  1515. }
  1516. static void write_cinfo(__be32 **p, struct nfsd4_change_info *c)
  1517. {
  1518. write32(p, c->atomic);
  1519. if (c->change_supported) {
  1520. write64(p, c->before_change);
  1521. write64(p, c->after_change);
  1522. } else {
  1523. write32(p, c->before_ctime_sec);
  1524. write32(p, c->before_ctime_nsec);
  1525. write32(p, c->after_ctime_sec);
  1526. write32(p, c->after_ctime_nsec);
  1527. }
  1528. }
  1529. #define RESERVE_SPACE(nbytes) do { \
  1530. p = resp->p; \
  1531. BUG_ON(p + XDR_QUADLEN(nbytes) > resp->end); \
  1532. } while (0)
  1533. #define ADJUST_ARGS() resp->p = p
  1534. /* Encode as an array of strings the string given with components
  1535. * separated @sep, escaped with esc_enter and esc_exit.
  1536. */
  1537. static __be32 nfsd4_encode_components_esc(char sep, char *components,
  1538. __be32 **pp, int *buflen,
  1539. char esc_enter, char esc_exit)
  1540. {
  1541. __be32 *p = *pp;
  1542. __be32 *countp = p;
  1543. int strlen, count=0;
  1544. char *str, *end, *next;
  1545. dprintk("nfsd4_encode_components(%s)\n", components);
  1546. if ((*buflen -= 4) < 0)
  1547. return nfserr_resource;
  1548. WRITE32(0); /* We will fill this in with @count later */
  1549. end = str = components;
  1550. while (*end) {
  1551. bool found_esc = false;
  1552. /* try to parse as esc_start, ..., esc_end, sep */
  1553. if (*str == esc_enter) {
  1554. for (; *end && (*end != esc_exit); end++)
  1555. /* find esc_exit or end of string */;
  1556. next = end + 1;
  1557. if (*end && (!*next || *next == sep)) {
  1558. str++;
  1559. found_esc = true;
  1560. }
  1561. }
  1562. if (!found_esc)
  1563. for (; *end && (*end != sep); end++)
  1564. /* find sep or end of string */;
  1565. strlen = end - str;
  1566. if (strlen) {
  1567. if ((*buflen -= ((XDR_QUADLEN(strlen) << 2) + 4)) < 0)
  1568. return nfserr_resource;
  1569. WRITE32(strlen);
  1570. WRITEMEM(str, strlen);
  1571. count++;
  1572. }
  1573. else
  1574. end++;
  1575. str = end;
  1576. }
  1577. *pp = p;
  1578. p = countp;
  1579. WRITE32(count);
  1580. return 0;
  1581. }
  1582. /* Encode as an array of strings the string given with components
  1583. * separated @sep.
  1584. */
  1585. static __be32 nfsd4_encode_components(char sep, char *components,
  1586. __be32 **pp, int *buflen)
  1587. {
  1588. return nfsd4_encode_components_esc(sep, components, pp, buflen, 0, 0);
  1589. }
  1590. /*
  1591. * encode a location element of a fs_locations structure
  1592. */
  1593. static __be32 nfsd4_encode_fs_location4(struct nfsd4_fs_location *location,
  1594. __be32 **pp, int *buflen)
  1595. {
  1596. __be32 status;
  1597. __be32 *p = *pp;
  1598. status = nfsd4_encode_components_esc(':', location->hosts, &p, buflen,
  1599. '[', ']');
  1600. if (status)
  1601. return status;
  1602. status = nfsd4_encode_components('/', location->path, &p, buflen);
  1603. if (status)
  1604. return status;
  1605. *pp = p;
  1606. return 0;
  1607. }
  1608. /*
  1609. * Encode a path in RFC3530 'pathname4' format
  1610. */
  1611. static __be32 nfsd4_encode_path(const struct path *root,
  1612. const struct path *path, __be32 **pp, int *buflen)
  1613. {
  1614. struct path cur = *path;
  1615. __be32 *p = *pp;
  1616. struct dentry **components = NULL;
  1617. unsigned int ncomponents = 0;
  1618. __be32 err = nfserr_jukebox;
  1619. dprintk("nfsd4_encode_components(");
  1620. path_get(&cur);
  1621. /* First walk the path up to the nfsd root, and store the
  1622. * dentries/path components in an array.
  1623. */
  1624. for (;;) {
  1625. if (cur.dentry == root->dentry && cur.mnt == root->mnt)
  1626. break;
  1627. if (cur.dentry == cur.mnt->mnt_root) {
  1628. if (follow_up(&cur))
  1629. continue;
  1630. goto out_free;
  1631. }
  1632. if ((ncomponents & 15) == 0) {
  1633. struct dentry **new;
  1634. new = krealloc(components,
  1635. sizeof(*new) * (ncomponents + 16),
  1636. GFP_KERNEL);
  1637. if (!new)
  1638. goto out_free;
  1639. components = new;
  1640. }
  1641. components[ncomponents++] = cur.dentry;
  1642. cur.dentry = dget_parent(cur.dentry);
  1643. }
  1644. *buflen -= 4;
  1645. if (*buflen < 0)
  1646. goto out_free;
  1647. WRITE32(ncomponents);
  1648. while (ncomponents) {
  1649. struct dentry *dentry = components[ncomponents - 1];
  1650. unsigned int len;
  1651. spin_lock(&dentry->d_lock);
  1652. len = dentry->d_name.len;
  1653. *buflen -= 4 + (XDR_QUADLEN(len) << 2);
  1654. if (*buflen < 0) {
  1655. spin_unlock(&dentry->d_lock);
  1656. goto out_free;
  1657. }
  1658. WRITE32(len);
  1659. WRITEMEM(dentry->d_name.name, len);
  1660. dprintk("/%s", dentry->d_name.name);
  1661. spin_unlock(&dentry->d_lock);
  1662. dput(dentry);
  1663. ncomponents--;
  1664. }
  1665. *pp = p;
  1666. err = 0;
  1667. out_free:
  1668. dprintk(")\n");
  1669. while (ncomponents)
  1670. dput(components[--ncomponents]);
  1671. kfree(components);
  1672. path_put(&cur);
  1673. return err;
  1674. }
  1675. static __be32 nfsd4_encode_fsloc_fsroot(struct svc_rqst *rqstp,
  1676. const struct path *path, __be32 **pp, int *buflen)
  1677. {
  1678. struct svc_export *exp_ps;
  1679. __be32 res;
  1680. exp_ps = rqst_find_fsidzero_export(rqstp);
  1681. if (IS_ERR(exp_ps))
  1682. return nfserrno(PTR_ERR(exp_ps));
  1683. res = nfsd4_encode_path(&exp_ps->ex_path, path, pp, buflen);
  1684. exp_put(exp_ps);
  1685. return res;
  1686. }
  1687. /*
  1688. * encode a fs_locations structure
  1689. */
  1690. static __be32 nfsd4_encode_fs_locations(struct svc_rqst *rqstp,
  1691. struct svc_export *exp,
  1692. __be32 **pp, int *buflen)
  1693. {
  1694. __be32 status;
  1695. int i;
  1696. __be32 *p = *pp;
  1697. struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
  1698. status = nfsd4_encode_fsloc_fsroot(rqstp, &exp->ex_path, &p, buflen);
  1699. if (status)
  1700. return status;
  1701. if ((*buflen -= 4) < 0)
  1702. return nfserr_resource;
  1703. WRITE32(fslocs->locations_count);
  1704. for (i=0; i<fslocs->locations_count; i++) {
  1705. status = nfsd4_encode_fs_location4(&fslocs->locations[i],
  1706. &p, buflen);
  1707. if (status)
  1708. return status;
  1709. }
  1710. *pp = p;
  1711. return 0;
  1712. }
  1713. static u32 nfs4_file_type(umode_t mode)
  1714. {
  1715. switch (mode & S_IFMT) {
  1716. case S_IFIFO: return NF4FIFO;
  1717. case S_IFCHR: return NF4CHR;
  1718. case S_IFDIR: return NF4DIR;
  1719. case S_IFBLK: return NF4BLK;
  1720. case S_IFLNK: return NF4LNK;
  1721. case S_IFREG: return NF4REG;
  1722. case S_IFSOCK: return NF4SOCK;
  1723. default: return NF4BAD;
  1724. };
  1725. }
  1726. static inline __be32
  1727. nfsd4_encode_aclname(struct svc_rqst *rqstp, struct nfs4_ace *ace,
  1728. __be32 **p, int *buflen)
  1729. {
  1730. if (ace->whotype != NFS4_ACL_WHO_NAMED)
  1731. return nfs4_acl_write_who(ace->whotype, p, buflen);
  1732. else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
  1733. return nfsd4_encode_group(rqstp, ace->who_gid, p, buflen);
  1734. else
  1735. return nfsd4_encode_user(rqstp, ace->who_uid, p, buflen);
  1736. }
  1737. #define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
  1738. FATTR4_WORD0_RDATTR_ERROR)
  1739. #define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
  1740. #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
  1741. static inline __be32
  1742. nfsd4_encode_security_label(struct svc_rqst *rqstp, void *context, int len, __be32 **pp, int *buflen)
  1743. {
  1744. __be32 *p = *pp;
  1745. if (*buflen < ((XDR_QUADLEN(len) << 2) + 4 + 4 + 4))
  1746. return nfserr_resource;
  1747. /*
  1748. * For now we use a 0 here to indicate the null translation; in
  1749. * the future we may place a call to translation code here.
  1750. */
  1751. if ((*buflen -= 8) < 0)
  1752. return nfserr_resource;
  1753. WRITE32(0); /* lfs */
  1754. WRITE32(0); /* pi */
  1755. p = xdr_encode_opaque(p, context, len);
  1756. *buflen -= (XDR_QUADLEN(len) << 2) + 4;
  1757. *pp = p;
  1758. return 0;
  1759. }
  1760. #else
  1761. static inline __be32
  1762. nfsd4_encode_security_label(struct svc_rqst *rqstp, void *context, int len, __be32 **pp, int *buflen)
  1763. { return 0; }
  1764. #endif
  1765. static __be32 fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *rdattr_err)
  1766. {
  1767. /* As per referral draft: */
  1768. if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
  1769. *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
  1770. if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
  1771. *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
  1772. *rdattr_err = NFSERR_MOVED;
  1773. else
  1774. return nfserr_moved;
  1775. }
  1776. *bmval0 &= WORD0_ABSENT_FS_ATTRS;
  1777. *bmval1 &= WORD1_ABSENT_FS_ATTRS;
  1778. return 0;
  1779. }
  1780. static int get_parent_attributes(struct svc_export *exp, struct kstat *stat)
  1781. {
  1782. struct path path = exp->ex_path;
  1783. int err;
  1784. path_get(&path);
  1785. while (follow_up(&path)) {
  1786. if (path.dentry != path.mnt->mnt_root)
  1787. break;
  1788. }
  1789. err = vfs_getattr(&path, stat);
  1790. path_put(&path);
  1791. return err;
  1792. }
  1793. /*
  1794. * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
  1795. * ourselves.
  1796. *
  1797. * countp is the buffer size in _words_
  1798. */
  1799. __be32
  1800. nfsd4_encode_fattr(struct svc_fh *fhp, struct svc_export *exp,
  1801. struct dentry *dentry, __be32 **buffer, int count, u32 *bmval,
  1802. struct svc_rqst *rqstp, int ignore_crossmnt)
  1803. {
  1804. u32 bmval0 = bmval[0];
  1805. u32 bmval1 = bmval[1];
  1806. u32 bmval2 = bmval[2];
  1807. struct kstat stat;
  1808. struct svc_fh *tempfh = NULL;
  1809. struct kstatfs statfs;
  1810. int buflen = count << 2;
  1811. __be32 *attrlenp;
  1812. u32 dummy;
  1813. u64 dummy64;
  1814. u32 rdattr_err = 0;
  1815. __be32 *p = *buffer;
  1816. __be32 status;
  1817. int err;
  1818. int aclsupport = 0;
  1819. struct nfs4_acl *acl = NULL;
  1820. void *context = NULL;
  1821. int contextlen;
  1822. bool contextsupport = false;
  1823. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  1824. u32 minorversion = resp->cstate.minorversion;
  1825. struct path path = {
  1826. .mnt = exp->ex_path.mnt,
  1827. .dentry = dentry,
  1828. };
  1829. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1830. BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
  1831. BUG_ON(bmval0 & ~nfsd_suppattrs0(minorversion));
  1832. BUG_ON(bmval1 & ~nfsd_suppattrs1(minorversion));
  1833. BUG_ON(bmval2 & ~nfsd_suppattrs2(minorversion));
  1834. if (exp->ex_fslocs.migrated) {
  1835. BUG_ON(bmval[2]);
  1836. status = fattr_handle_absent_fs(&bmval0, &bmval1, &rdattr_err);
  1837. if (status)
  1838. goto out;
  1839. }
  1840. err = vfs_getattr(&path, &stat);
  1841. if (err)
  1842. goto out_nfserr;
  1843. if ((bmval0 & (FATTR4_WORD0_FILES_FREE | FATTR4_WORD0_FILES_TOTAL |
  1844. FATTR4_WORD0_MAXNAME)) ||
  1845. (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
  1846. FATTR4_WORD1_SPACE_TOTAL))) {
  1847. err = vfs_statfs(&path, &statfs);
  1848. if (err)
  1849. goto out_nfserr;
  1850. }
  1851. if ((bmval0 & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) && !fhp) {
  1852. tempfh = kmalloc(sizeof(struct svc_fh), GFP_KERNEL);
  1853. status = nfserr_jukebox;
  1854. if (!tempfh)
  1855. goto out;
  1856. fh_init(tempfh, NFS4_FHSIZE);
  1857. status = fh_compose(tempfh, exp, dentry, NULL);
  1858. if (status)
  1859. goto out;
  1860. fhp = tempfh;
  1861. }
  1862. if (bmval0 & (FATTR4_WORD0_ACL | FATTR4_WORD0_ACLSUPPORT
  1863. | FATTR4_WORD0_SUPPORTED_ATTRS)) {
  1864. err = nfsd4_get_nfs4_acl(rqstp, dentry, &acl);
  1865. aclsupport = (err == 0);
  1866. if (bmval0 & FATTR4_WORD0_ACL) {
  1867. if (err == -EOPNOTSUPP)
  1868. bmval0 &= ~FATTR4_WORD0_ACL;
  1869. else if (err == -EINVAL) {
  1870. status = nfserr_attrnotsupp;
  1871. goto out;
  1872. } else if (err != 0)
  1873. goto out_nfserr;
  1874. }
  1875. }
  1876. #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
  1877. if ((bmval[2] & FATTR4_WORD2_SECURITY_LABEL) ||
  1878. bmval[0] & FATTR4_WORD0_SUPPORTED_ATTRS) {
  1879. err = security_inode_getsecctx(dentry->d_inode,
  1880. &context, &contextlen);
  1881. contextsupport = (err == 0);
  1882. if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
  1883. if (err == -EOPNOTSUPP)
  1884. bmval2 &= ~FATTR4_WORD2_SECURITY_LABEL;
  1885. else if (err)
  1886. goto out_nfserr;
  1887. }
  1888. }
  1889. #endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
  1890. if (bmval2) {
  1891. if ((buflen -= 16) < 0)
  1892. goto out_resource;
  1893. WRITE32(3);
  1894. WRITE32(bmval0);
  1895. WRITE32(bmval1);
  1896. WRITE32(bmval2);
  1897. } else if (bmval1) {
  1898. if ((buflen -= 12) < 0)
  1899. goto out_resource;
  1900. WRITE32(2);
  1901. WRITE32(bmval0);
  1902. WRITE32(bmval1);
  1903. } else {
  1904. if ((buflen -= 8) < 0)
  1905. goto out_resource;
  1906. WRITE32(1);
  1907. WRITE32(bmval0);
  1908. }
  1909. attrlenp = p++; /* to be backfilled later */
  1910. if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
  1911. u32 word0 = nfsd_suppattrs0(minorversion);
  1912. u32 word1 = nfsd_suppattrs1(minorversion);
  1913. u32 word2 = nfsd_suppattrs2(minorversion);
  1914. if (!aclsupport)
  1915. word0 &= ~FATTR4_WORD0_ACL;
  1916. if (!contextsupport)
  1917. word2 &= ~FATTR4_WORD2_SECURITY_LABEL;
  1918. if (!word2) {
  1919. if ((buflen -= 12) < 0)
  1920. goto out_resource;
  1921. WRITE32(2);
  1922. WRITE32(word0);
  1923. WRITE32(word1);
  1924. } else {
  1925. if ((buflen -= 16) < 0)
  1926. goto out_resource;
  1927. WRITE32(3);
  1928. WRITE32(word0);
  1929. WRITE32(word1);
  1930. WRITE32(word2);
  1931. }
  1932. }
  1933. if (bmval0 & FATTR4_WORD0_TYPE) {
  1934. if ((buflen -= 4) < 0)
  1935. goto out_resource;
  1936. dummy = nfs4_file_type(stat.mode);
  1937. if (dummy == NF4BAD) {
  1938. status = nfserr_serverfault;
  1939. goto out;
  1940. }
  1941. WRITE32(dummy);
  1942. }
  1943. if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
  1944. if ((buflen -= 4) < 0)
  1945. goto out_resource;
  1946. if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
  1947. WRITE32(NFS4_FH_PERSISTENT);
  1948. else
  1949. WRITE32(NFS4_FH_PERSISTENT|NFS4_FH_VOL_RENAME);
  1950. }
  1951. if (bmval0 & FATTR4_WORD0_CHANGE) {
  1952. if ((buflen -= 8) < 0)
  1953. goto out_resource;
  1954. write_change(&p, &stat, dentry->d_inode);
  1955. }
  1956. if (bmval0 & FATTR4_WORD0_SIZE) {
  1957. if ((buflen -= 8) < 0)
  1958. goto out_resource;
  1959. WRITE64(stat.size);
  1960. }
  1961. if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
  1962. if ((buflen -= 4) < 0)
  1963. goto out_resource;
  1964. WRITE32(1);
  1965. }
  1966. if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
  1967. if ((buflen -= 4) < 0)
  1968. goto out_resource;
  1969. WRITE32(1);
  1970. }
  1971. if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
  1972. if ((buflen -= 4) < 0)
  1973. goto out_resource;
  1974. WRITE32(0);
  1975. }
  1976. if (bmval0 & FATTR4_WORD0_FSID) {
  1977. if ((buflen -= 16) < 0)
  1978. goto out_resource;
  1979. if (exp->ex_fslocs.migrated) {
  1980. WRITE64(NFS4_REFERRAL_FSID_MAJOR);
  1981. WRITE64(NFS4_REFERRAL_FSID_MINOR);
  1982. } else switch(fsid_source(fhp)) {
  1983. case FSIDSOURCE_FSID:
  1984. WRITE64((u64)exp->ex_fsid);
  1985. WRITE64((u64)0);
  1986. break;
  1987. case FSIDSOURCE_DEV:
  1988. WRITE32(0);
  1989. WRITE32(MAJOR(stat.dev));
  1990. WRITE32(0);
  1991. WRITE32(MINOR(stat.dev));
  1992. break;
  1993. case FSIDSOURCE_UUID:
  1994. WRITEMEM(exp->ex_uuid, 16);
  1995. break;
  1996. }
  1997. }
  1998. if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
  1999. if ((buflen -= 4) < 0)
  2000. goto out_resource;
  2001. WRITE32(0);
  2002. }
  2003. if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
  2004. if ((buflen -= 4) < 0)
  2005. goto out_resource;
  2006. WRITE32(nn->nfsd4_lease);
  2007. }
  2008. if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
  2009. if ((buflen -= 4) < 0)
  2010. goto out_resource;
  2011. WRITE32(rdattr_err);
  2012. }
  2013. if (bmval0 & FATTR4_WORD0_ACL) {
  2014. struct nfs4_ace *ace;
  2015. if (acl == NULL) {
  2016. if ((buflen -= 4) < 0)
  2017. goto out_resource;
  2018. WRITE32(0);
  2019. goto out_acl;
  2020. }
  2021. if ((buflen -= 4) < 0)
  2022. goto out_resource;
  2023. WRITE32(acl->naces);
  2024. for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
  2025. if ((buflen -= 4*3) < 0)
  2026. goto out_resource;
  2027. WRITE32(ace->type);
  2028. WRITE32(ace->flag);
  2029. WRITE32(ace->access_mask & NFS4_ACE_MASK_ALL);
  2030. status = nfsd4_encode_aclname(rqstp, ace, &p, &buflen);
  2031. if (status)
  2032. goto out;
  2033. }
  2034. }
  2035. out_acl:
  2036. if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
  2037. if ((buflen -= 4) < 0)
  2038. goto out_resource;
  2039. WRITE32(aclsupport ?
  2040. ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL : 0);
  2041. }
  2042. if (bmval0 & FATTR4_WORD0_CANSETTIME) {
  2043. if ((buflen -= 4) < 0)
  2044. goto out_resource;
  2045. WRITE32(1);
  2046. }
  2047. if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
  2048. if ((buflen -= 4) < 0)
  2049. goto out_resource;
  2050. WRITE32(0);
  2051. }
  2052. if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
  2053. if ((buflen -= 4) < 0)
  2054. goto out_resource;
  2055. WRITE32(1);
  2056. }
  2057. if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
  2058. if ((buflen -= 4) < 0)
  2059. goto out_resource;
  2060. WRITE32(1);
  2061. }
  2062. if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
  2063. buflen -= (XDR_QUADLEN(fhp->fh_handle.fh_size) << 2) + 4;
  2064. if (buflen < 0)
  2065. goto out_resource;
  2066. WRITE32(fhp->fh_handle.fh_size);
  2067. WRITEMEM(&fhp->fh_handle.fh_base, fhp->fh_handle.fh_size);
  2068. }
  2069. if (bmval0 & FATTR4_WORD0_FILEID) {
  2070. if ((buflen -= 8) < 0)
  2071. goto out_resource;
  2072. WRITE64(stat.ino);
  2073. }
  2074. if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
  2075. if ((buflen -= 8) < 0)
  2076. goto out_resource;
  2077. WRITE64((u64) statfs.f_ffree);
  2078. }
  2079. if (bmval0 & FATTR4_WORD0_FILES_FREE) {
  2080. if ((buflen -= 8) < 0)
  2081. goto out_resource;
  2082. WRITE64((u64) statfs.f_ffree);
  2083. }
  2084. if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
  2085. if ((buflen -= 8) < 0)
  2086. goto out_resource;
  2087. WRITE64((u64) statfs.f_files);
  2088. }
  2089. if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
  2090. status = nfsd4_encode_fs_locations(rqstp, exp, &p, &buflen);
  2091. if (status)
  2092. goto out;
  2093. }
  2094. if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
  2095. if ((buflen -= 4) < 0)
  2096. goto out_resource;
  2097. WRITE32(1);
  2098. }
  2099. if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
  2100. if ((buflen -= 8) < 0)
  2101. goto out_resource;
  2102. WRITE64(exp->ex_path.mnt->mnt_sb->s_maxbytes);
  2103. }
  2104. if (bmval0 & FATTR4_WORD0_MAXLINK) {
  2105. if ((buflen -= 4) < 0)
  2106. goto out_resource;
  2107. WRITE32(255);
  2108. }
  2109. if (bmval0 & FATTR4_WORD0_MAXNAME) {
  2110. if ((buflen -= 4) < 0)
  2111. goto out_resource;
  2112. WRITE32(statfs.f_namelen);
  2113. }
  2114. if (bmval0 & FATTR4_WORD0_MAXREAD) {
  2115. if ((buflen -= 8) < 0)
  2116. goto out_resource;
  2117. WRITE64((u64) svc_max_payload(rqstp));
  2118. }
  2119. if (bmval0 & FATTR4_WORD0_MAXWRITE) {
  2120. if ((buflen -= 8) < 0)
  2121. goto out_resource;
  2122. WRITE64((u64) svc_max_payload(rqstp));
  2123. }
  2124. if (bmval1 & FATTR4_WORD1_MODE) {
  2125. if ((buflen -= 4) < 0)
  2126. goto out_resource;
  2127. WRITE32(stat.mode & S_IALLUGO);
  2128. }
  2129. if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
  2130. if ((buflen -= 4) < 0)
  2131. goto out_resource;
  2132. WRITE32(1);
  2133. }
  2134. if (bmval1 & FATTR4_WORD1_NUMLINKS) {
  2135. if ((buflen -= 4) < 0)
  2136. goto out_resource;
  2137. WRITE32(stat.nlink);
  2138. }
  2139. if (bmval1 & FATTR4_WORD1_OWNER) {
  2140. status = nfsd4_encode_user(rqstp, stat.uid, &p, &buflen);
  2141. if (status)
  2142. goto out;
  2143. }
  2144. if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
  2145. status = nfsd4_encode_group(rqstp, stat.gid, &p, &buflen);
  2146. if (status)
  2147. goto out;
  2148. }
  2149. if (bmval1 & FATTR4_WORD1_RAWDEV) {
  2150. if ((buflen -= 8) < 0)
  2151. goto out_resource;
  2152. WRITE32((u32) MAJOR(stat.rdev));
  2153. WRITE32((u32) MINOR(stat.rdev));
  2154. }
  2155. if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
  2156. if ((buflen -= 8) < 0)
  2157. goto out_resource;
  2158. dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
  2159. WRITE64(dummy64);
  2160. }
  2161. if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
  2162. if ((buflen -= 8) < 0)
  2163. goto out_resource;
  2164. dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
  2165. WRITE64(dummy64);
  2166. }
  2167. if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
  2168. if ((buflen -= 8) < 0)
  2169. goto out_resource;
  2170. dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
  2171. WRITE64(dummy64);
  2172. }
  2173. if (bmval1 & FATTR4_WORD1_SPACE_USED) {
  2174. if ((buflen -= 8) < 0)
  2175. goto out_resource;
  2176. dummy64 = (u64)stat.blocks << 9;
  2177. WRITE64(dummy64);
  2178. }
  2179. if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
  2180. if ((buflen -= 12) < 0)
  2181. goto out_resource;
  2182. WRITE64((s64)stat.atime.tv_sec);
  2183. WRITE32(stat.atime.tv_nsec);
  2184. }
  2185. if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
  2186. if ((buflen -= 12) < 0)
  2187. goto out_resource;
  2188. WRITE32(0);
  2189. WRITE32(1);
  2190. WRITE32(0);
  2191. }
  2192. if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
  2193. if ((buflen -= 12) < 0)
  2194. goto out_resource;
  2195. WRITE64((s64)stat.ctime.tv_sec);
  2196. WRITE32(stat.ctime.tv_nsec);
  2197. }
  2198. if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
  2199. if ((buflen -= 12) < 0)
  2200. goto out_resource;
  2201. WRITE64((s64)stat.mtime.tv_sec);
  2202. WRITE32(stat.mtime.tv_nsec);
  2203. }
  2204. if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
  2205. if ((buflen -= 8) < 0)
  2206. goto out_resource;
  2207. /*
  2208. * Get parent's attributes if not ignoring crossmount
  2209. * and this is the root of a cross-mounted filesystem.
  2210. */
  2211. if (ignore_crossmnt == 0 &&
  2212. dentry == exp->ex_path.mnt->mnt_root)
  2213. get_parent_attributes(exp, &stat);
  2214. WRITE64(stat.ino);
  2215. }
  2216. if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
  2217. status = nfsd4_encode_security_label(rqstp, context,
  2218. contextlen, &p, &buflen);
  2219. if (status)
  2220. goto out;
  2221. }
  2222. if (bmval2 & FATTR4_WORD2_SUPPATTR_EXCLCREAT) {
  2223. WRITE32(3);
  2224. WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD0);
  2225. WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD1);
  2226. WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD2);
  2227. }
  2228. *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
  2229. *buffer = p;
  2230. status = nfs_ok;
  2231. out:
  2232. #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
  2233. if (context)
  2234. security_release_secctx(context, contextlen);
  2235. #endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
  2236. kfree(acl);
  2237. if (tempfh)
  2238. fh_put(tempfh);
  2239. return status;
  2240. out_nfserr:
  2241. status = nfserrno(err);
  2242. goto out;
  2243. out_resource:
  2244. status = nfserr_resource;
  2245. goto out;
  2246. }
  2247. static inline int attributes_need_mount(u32 *bmval)
  2248. {
  2249. if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
  2250. return 1;
  2251. if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
  2252. return 1;
  2253. return 0;
  2254. }
  2255. static __be32
  2256. nfsd4_encode_dirent_fattr(struct nfsd4_readdir *cd,
  2257. const char *name, int namlen, __be32 **p, int buflen)
  2258. {
  2259. struct svc_export *exp = cd->rd_fhp->fh_export;
  2260. struct dentry *dentry;
  2261. __be32 nfserr;
  2262. int ignore_crossmnt = 0;
  2263. dentry = lookup_one_len(name, cd->rd_fhp->fh_dentry, namlen);
  2264. if (IS_ERR(dentry))
  2265. return nfserrno(PTR_ERR(dentry));
  2266. if (!dentry->d_inode) {
  2267. /*
  2268. * nfsd_buffered_readdir drops the i_mutex between
  2269. * readdir and calling this callback, leaving a window
  2270. * where this directory entry could have gone away.
  2271. */
  2272. dput(dentry);
  2273. return nfserr_noent;
  2274. }
  2275. exp_get(exp);
  2276. /*
  2277. * In the case of a mountpoint, the client may be asking for
  2278. * attributes that are only properties of the underlying filesystem
  2279. * as opposed to the cross-mounted file system. In such a case,
  2280. * we will not follow the cross mount and will fill the attribtutes
  2281. * directly from the mountpoint dentry.
  2282. */
  2283. if (nfsd_mountpoint(dentry, exp)) {
  2284. int err;
  2285. if (!(exp->ex_flags & NFSEXP_V4ROOT)
  2286. && !attributes_need_mount(cd->rd_bmval)) {
  2287. ignore_crossmnt = 1;
  2288. goto out_encode;
  2289. }
  2290. /*
  2291. * Why the heck aren't we just using nfsd_lookup??
  2292. * Different "."/".." handling? Something else?
  2293. * At least, add a comment here to explain....
  2294. */
  2295. err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
  2296. if (err) {
  2297. nfserr = nfserrno(err);
  2298. goto out_put;
  2299. }
  2300. nfserr = check_nfsd_access(exp, cd->rd_rqstp);
  2301. if (nfserr)
  2302. goto out_put;
  2303. }
  2304. out_encode:
  2305. nfserr = nfsd4_encode_fattr(NULL, exp, dentry, p, buflen, cd->rd_bmval,
  2306. cd->rd_rqstp, ignore_crossmnt);
  2307. out_put:
  2308. dput(dentry);
  2309. exp_put(exp);
  2310. return nfserr;
  2311. }
  2312. static __be32 *
  2313. nfsd4_encode_rdattr_error(__be32 *p, int buflen, __be32 nfserr)
  2314. {
  2315. if (buflen < 6)
  2316. return NULL;
  2317. *p++ = htonl(2);
  2318. *p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
  2319. *p++ = htonl(0); /* bmval1 */
  2320. *p++ = htonl(4); /* attribute length */
  2321. *p++ = nfserr; /* no htonl */
  2322. return p;
  2323. }
  2324. static int
  2325. nfsd4_encode_dirent(void *ccdv, const char *name, int namlen,
  2326. loff_t offset, u64 ino, unsigned int d_type)
  2327. {
  2328. struct readdir_cd *ccd = ccdv;
  2329. struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
  2330. int buflen;
  2331. __be32 *p = cd->buffer;
  2332. __be32 *cookiep;
  2333. __be32 nfserr = nfserr_toosmall;
  2334. /* In nfsv4, "." and ".." never make it onto the wire.. */
  2335. if (name && isdotent(name, namlen)) {
  2336. cd->common.err = nfs_ok;
  2337. return 0;
  2338. }
  2339. if (cd->offset)
  2340. xdr_encode_hyper(cd->offset, (u64) offset);
  2341. buflen = cd->buflen - 4 - XDR_QUADLEN(namlen);
  2342. if (buflen < 0)
  2343. goto fail;
  2344. *p++ = xdr_one; /* mark entry present */
  2345. cookiep = p;
  2346. p = xdr_encode_hyper(p, NFS_OFFSET_MAX); /* offset of next entry */
  2347. p = xdr_encode_array(p, name, namlen); /* name length & name */
  2348. nfserr = nfsd4_encode_dirent_fattr(cd, name, namlen, &p, buflen);
  2349. switch (nfserr) {
  2350. case nfs_ok:
  2351. break;
  2352. case nfserr_resource:
  2353. nfserr = nfserr_toosmall;
  2354. goto fail;
  2355. case nfserr_noent:
  2356. goto skip_entry;
  2357. default:
  2358. /*
  2359. * If the client requested the RDATTR_ERROR attribute,
  2360. * we stuff the error code into this attribute
  2361. * and continue. If this attribute was not requested,
  2362. * then in accordance with the spec, we fail the
  2363. * entire READDIR operation(!)
  2364. */
  2365. if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
  2366. goto fail;
  2367. p = nfsd4_encode_rdattr_error(p, buflen, nfserr);
  2368. if (p == NULL) {
  2369. nfserr = nfserr_toosmall;
  2370. goto fail;
  2371. }
  2372. }
  2373. cd->buflen -= (p - cd->buffer);
  2374. cd->buffer = p;
  2375. cd->offset = cookiep;
  2376. skip_entry:
  2377. cd->common.err = nfs_ok;
  2378. return 0;
  2379. fail:
  2380. cd->common.err = nfserr;
  2381. return -EINVAL;
  2382. }
  2383. static void
  2384. nfsd4_encode_stateid(struct nfsd4_compoundres *resp, stateid_t *sid)
  2385. {
  2386. __be32 *p;
  2387. RESERVE_SPACE(sizeof(stateid_t));
  2388. WRITE32(sid->si_generation);
  2389. WRITEMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
  2390. ADJUST_ARGS();
  2391. }
  2392. static __be32
  2393. nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_access *access)
  2394. {
  2395. __be32 *p;
  2396. if (!nfserr) {
  2397. RESERVE_SPACE(8);
  2398. WRITE32(access->ac_supported);
  2399. WRITE32(access->ac_resp_access);
  2400. ADJUST_ARGS();
  2401. }
  2402. return nfserr;
  2403. }
  2404. static __be32 nfsd4_encode_bind_conn_to_session(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_bind_conn_to_session *bcts)
  2405. {
  2406. __be32 *p;
  2407. if (!nfserr) {
  2408. RESERVE_SPACE(NFS4_MAX_SESSIONID_LEN + 8);
  2409. WRITEMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
  2410. WRITE32(bcts->dir);
  2411. /* Sorry, we do not yet support RDMA over 4.1: */
  2412. WRITE32(0);
  2413. ADJUST_ARGS();
  2414. }
  2415. return nfserr;
  2416. }
  2417. static __be32
  2418. nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_close *close)
  2419. {
  2420. if (!nfserr)
  2421. nfsd4_encode_stateid(resp, &close->cl_stateid);
  2422. return nfserr;
  2423. }
  2424. static __be32
  2425. nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_commit *commit)
  2426. {
  2427. __be32 *p;
  2428. if (!nfserr) {
  2429. RESERVE_SPACE(NFS4_VERIFIER_SIZE);
  2430. WRITEMEM(commit->co_verf.data, NFS4_VERIFIER_SIZE);
  2431. ADJUST_ARGS();
  2432. }
  2433. return nfserr;
  2434. }
  2435. static __be32
  2436. nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_create *create)
  2437. {
  2438. __be32 *p;
  2439. if (!nfserr) {
  2440. RESERVE_SPACE(32);
  2441. write_cinfo(&p, &create->cr_cinfo);
  2442. WRITE32(2);
  2443. WRITE32(create->cr_bmval[0]);
  2444. WRITE32(create->cr_bmval[1]);
  2445. ADJUST_ARGS();
  2446. }
  2447. return nfserr;
  2448. }
  2449. static __be32
  2450. nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_getattr *getattr)
  2451. {
  2452. struct svc_fh *fhp = getattr->ga_fhp;
  2453. int buflen;
  2454. if (nfserr)
  2455. return nfserr;
  2456. buflen = resp->end - resp->p - (COMPOUND_ERR_SLACK_SPACE >> 2);
  2457. nfserr = nfsd4_encode_fattr(fhp, fhp->fh_export, fhp->fh_dentry,
  2458. &resp->p, buflen, getattr->ga_bmval,
  2459. resp->rqstp, 0);
  2460. return nfserr;
  2461. }
  2462. static __be32
  2463. nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr, struct svc_fh **fhpp)
  2464. {
  2465. struct svc_fh *fhp = *fhpp;
  2466. unsigned int len;
  2467. __be32 *p;
  2468. if (!nfserr) {
  2469. len = fhp->fh_handle.fh_size;
  2470. RESERVE_SPACE(len + 4);
  2471. WRITE32(len);
  2472. WRITEMEM(&fhp->fh_handle.fh_base, len);
  2473. ADJUST_ARGS();
  2474. }
  2475. return nfserr;
  2476. }
  2477. /*
  2478. * Including all fields other than the name, a LOCK4denied structure requires
  2479. * 8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
  2480. */
  2481. static void
  2482. nfsd4_encode_lock_denied(struct nfsd4_compoundres *resp, struct nfsd4_lock_denied *ld)
  2483. {
  2484. struct xdr_netobj *conf = &ld->ld_owner;
  2485. __be32 *p;
  2486. RESERVE_SPACE(32 + XDR_LEN(conf->len));
  2487. WRITE64(ld->ld_start);
  2488. WRITE64(ld->ld_length);
  2489. WRITE32(ld->ld_type);
  2490. if (conf->len) {
  2491. WRITEMEM(&ld->ld_clientid, 8);
  2492. WRITE32(conf->len);
  2493. WRITEMEM(conf->data, conf->len);
  2494. kfree(conf->data);
  2495. } else { /* non - nfsv4 lock in conflict, no clientid nor owner */
  2496. WRITE64((u64)0); /* clientid */
  2497. WRITE32(0); /* length of owner name */
  2498. }
  2499. ADJUST_ARGS();
  2500. }
  2501. static __be32
  2502. nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lock *lock)
  2503. {
  2504. if (!nfserr)
  2505. nfsd4_encode_stateid(resp, &lock->lk_resp_stateid);
  2506. else if (nfserr == nfserr_denied)
  2507. nfsd4_encode_lock_denied(resp, &lock->lk_denied);
  2508. return nfserr;
  2509. }
  2510. static __be32
  2511. nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lockt *lockt)
  2512. {
  2513. if (nfserr == nfserr_denied)
  2514. nfsd4_encode_lock_denied(resp, &lockt->lt_denied);
  2515. return nfserr;
  2516. }
  2517. static __be32
  2518. nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_locku *locku)
  2519. {
  2520. if (!nfserr)
  2521. nfsd4_encode_stateid(resp, &locku->lu_stateid);
  2522. return nfserr;
  2523. }
  2524. static __be32
  2525. nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_link *link)
  2526. {
  2527. __be32 *p;
  2528. if (!nfserr) {
  2529. RESERVE_SPACE(20);
  2530. write_cinfo(&p, &link->li_cinfo);
  2531. ADJUST_ARGS();
  2532. }
  2533. return nfserr;
  2534. }
  2535. static __be32
  2536. nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open *open)
  2537. {
  2538. __be32 *p;
  2539. if (nfserr)
  2540. goto out;
  2541. nfsd4_encode_stateid(resp, &open->op_stateid);
  2542. RESERVE_SPACE(40);
  2543. write_cinfo(&p, &open->op_cinfo);
  2544. WRITE32(open->op_rflags);
  2545. WRITE32(2);
  2546. WRITE32(open->op_bmval[0]);
  2547. WRITE32(open->op_bmval[1]);
  2548. WRITE32(open->op_delegate_type);
  2549. ADJUST_ARGS();
  2550. switch (open->op_delegate_type) {
  2551. case NFS4_OPEN_DELEGATE_NONE:
  2552. break;
  2553. case NFS4_OPEN_DELEGATE_READ:
  2554. nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
  2555. RESERVE_SPACE(20);
  2556. WRITE32(open->op_recall);
  2557. /*
  2558. * TODO: ACE's in delegations
  2559. */
  2560. WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
  2561. WRITE32(0);
  2562. WRITE32(0);
  2563. WRITE32(0); /* XXX: is NULL principal ok? */
  2564. ADJUST_ARGS();
  2565. break;
  2566. case NFS4_OPEN_DELEGATE_WRITE:
  2567. nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
  2568. RESERVE_SPACE(32);
  2569. WRITE32(0);
  2570. /*
  2571. * TODO: space_limit's in delegations
  2572. */
  2573. WRITE32(NFS4_LIMIT_SIZE);
  2574. WRITE32(~(u32)0);
  2575. WRITE32(~(u32)0);
  2576. /*
  2577. * TODO: ACE's in delegations
  2578. */
  2579. WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
  2580. WRITE32(0);
  2581. WRITE32(0);
  2582. WRITE32(0); /* XXX: is NULL principal ok? */
  2583. ADJUST_ARGS();
  2584. break;
  2585. case NFS4_OPEN_DELEGATE_NONE_EXT: /* 4.1 */
  2586. switch (open->op_why_no_deleg) {
  2587. case WND4_CONTENTION:
  2588. case WND4_RESOURCE:
  2589. RESERVE_SPACE(8);
  2590. WRITE32(open->op_why_no_deleg);
  2591. WRITE32(0); /* deleg signaling not supported yet */
  2592. break;
  2593. default:
  2594. RESERVE_SPACE(4);
  2595. WRITE32(open->op_why_no_deleg);
  2596. }
  2597. ADJUST_ARGS();
  2598. break;
  2599. default:
  2600. BUG();
  2601. }
  2602. /* XXX save filehandle here */
  2603. out:
  2604. return nfserr;
  2605. }
  2606. static __be32
  2607. nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_confirm *oc)
  2608. {
  2609. if (!nfserr)
  2610. nfsd4_encode_stateid(resp, &oc->oc_resp_stateid);
  2611. return nfserr;
  2612. }
  2613. static __be32
  2614. nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_downgrade *od)
  2615. {
  2616. if (!nfserr)
  2617. nfsd4_encode_stateid(resp, &od->od_stateid);
  2618. return nfserr;
  2619. }
  2620. static __be32
  2621. nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
  2622. struct nfsd4_read *read)
  2623. {
  2624. u32 eof;
  2625. int v;
  2626. struct page *page;
  2627. unsigned long maxcount;
  2628. long len;
  2629. __be32 *p;
  2630. if (nfserr)
  2631. return nfserr;
  2632. if (resp->xbuf->page_len)
  2633. return nfserr_resource;
  2634. RESERVE_SPACE(8); /* eof flag and byte count */
  2635. maxcount = svc_max_payload(resp->rqstp);
  2636. if (maxcount > read->rd_length)
  2637. maxcount = read->rd_length;
  2638. len = maxcount;
  2639. v = 0;
  2640. while (len > 0) {
  2641. page = *(resp->rqstp->rq_next_page);
  2642. if (!page) { /* ran out of pages */
  2643. maxcount -= len;
  2644. break;
  2645. }
  2646. resp->rqstp->rq_vec[v].iov_base = page_address(page);
  2647. resp->rqstp->rq_vec[v].iov_len =
  2648. len < PAGE_SIZE ? len : PAGE_SIZE;
  2649. resp->rqstp->rq_next_page++;
  2650. v++;
  2651. len -= PAGE_SIZE;
  2652. }
  2653. read->rd_vlen = v;
  2654. nfserr = nfsd_read_file(read->rd_rqstp, read->rd_fhp, read->rd_filp,
  2655. read->rd_offset, resp->rqstp->rq_vec, read->rd_vlen,
  2656. &maxcount);
  2657. if (nfserr)
  2658. return nfserr;
  2659. eof = (read->rd_offset + maxcount >=
  2660. read->rd_fhp->fh_dentry->d_inode->i_size);
  2661. WRITE32(eof);
  2662. WRITE32(maxcount);
  2663. ADJUST_ARGS();
  2664. resp->xbuf->head[0].iov_len = (char*)p
  2665. - (char*)resp->xbuf->head[0].iov_base;
  2666. resp->xbuf->page_len = maxcount;
  2667. /* Use rest of head for padding and remaining ops: */
  2668. resp->xbuf->tail[0].iov_base = p;
  2669. resp->xbuf->tail[0].iov_len = 0;
  2670. if (maxcount&3) {
  2671. RESERVE_SPACE(4);
  2672. WRITE32(0);
  2673. resp->xbuf->tail[0].iov_base += maxcount&3;
  2674. resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
  2675. ADJUST_ARGS();
  2676. }
  2677. return 0;
  2678. }
  2679. static __be32
  2680. nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readlink *readlink)
  2681. {
  2682. int maxcount;
  2683. char *page;
  2684. __be32 *p;
  2685. if (nfserr)
  2686. return nfserr;
  2687. if (resp->xbuf->page_len)
  2688. return nfserr_resource;
  2689. if (!*resp->rqstp->rq_next_page)
  2690. return nfserr_resource;
  2691. page = page_address(*(resp->rqstp->rq_next_page++));
  2692. maxcount = PAGE_SIZE;
  2693. RESERVE_SPACE(4);
  2694. /*
  2695. * XXX: By default, the ->readlink() VFS op will truncate symlinks
  2696. * if they would overflow the buffer. Is this kosher in NFSv4? If
  2697. * not, one easy fix is: if ->readlink() precisely fills the buffer,
  2698. * assume that truncation occurred, and return NFS4ERR_RESOURCE.
  2699. */
  2700. nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp, page, &maxcount);
  2701. if (nfserr == nfserr_isdir)
  2702. return nfserr_inval;
  2703. if (nfserr)
  2704. return nfserr;
  2705. WRITE32(maxcount);
  2706. ADJUST_ARGS();
  2707. resp->xbuf->head[0].iov_len = (char*)p
  2708. - (char*)resp->xbuf->head[0].iov_base;
  2709. resp->xbuf->page_len = maxcount;
  2710. /* Use rest of head for padding and remaining ops: */
  2711. resp->xbuf->tail[0].iov_base = p;
  2712. resp->xbuf->tail[0].iov_len = 0;
  2713. if (maxcount&3) {
  2714. RESERVE_SPACE(4);
  2715. WRITE32(0);
  2716. resp->xbuf->tail[0].iov_base += maxcount&3;
  2717. resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
  2718. ADJUST_ARGS();
  2719. }
  2720. return 0;
  2721. }
  2722. static __be32
  2723. nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readdir *readdir)
  2724. {
  2725. int maxcount;
  2726. loff_t offset;
  2727. __be32 *page, *savep, *tailbase;
  2728. __be32 *p;
  2729. if (nfserr)
  2730. return nfserr;
  2731. if (resp->xbuf->page_len)
  2732. return nfserr_resource;
  2733. if (!*resp->rqstp->rq_next_page)
  2734. return nfserr_resource;
  2735. RESERVE_SPACE(NFS4_VERIFIER_SIZE);
  2736. savep = p;
  2737. /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
  2738. WRITE32(0);
  2739. WRITE32(0);
  2740. ADJUST_ARGS();
  2741. resp->xbuf->head[0].iov_len = ((char*)resp->p) - (char*)resp->xbuf->head[0].iov_base;
  2742. tailbase = p;
  2743. maxcount = PAGE_SIZE;
  2744. if (maxcount > readdir->rd_maxcount)
  2745. maxcount = readdir->rd_maxcount;
  2746. /*
  2747. * Convert from bytes to words, account for the two words already
  2748. * written, make sure to leave two words at the end for the next
  2749. * pointer and eof field.
  2750. */
  2751. maxcount = (maxcount >> 2) - 4;
  2752. if (maxcount < 0) {
  2753. nfserr = nfserr_toosmall;
  2754. goto err_no_verf;
  2755. }
  2756. page = page_address(*(resp->rqstp->rq_next_page++));
  2757. readdir->common.err = 0;
  2758. readdir->buflen = maxcount;
  2759. readdir->buffer = page;
  2760. readdir->offset = NULL;
  2761. offset = readdir->rd_cookie;
  2762. nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
  2763. &offset,
  2764. &readdir->common, nfsd4_encode_dirent);
  2765. if (nfserr == nfs_ok &&
  2766. readdir->common.err == nfserr_toosmall &&
  2767. readdir->buffer == page)
  2768. nfserr = nfserr_toosmall;
  2769. if (nfserr)
  2770. goto err_no_verf;
  2771. if (readdir->offset)
  2772. xdr_encode_hyper(readdir->offset, offset);
  2773. p = readdir->buffer;
  2774. *p++ = 0; /* no more entries */
  2775. *p++ = htonl(readdir->common.err == nfserr_eof);
  2776. resp->xbuf->page_len = ((char*)p) -
  2777. (char*)page_address(*(resp->rqstp->rq_next_page-1));
  2778. /* Use rest of head for padding and remaining ops: */
  2779. resp->xbuf->tail[0].iov_base = tailbase;
  2780. resp->xbuf->tail[0].iov_len = 0;
  2781. resp->p = resp->xbuf->tail[0].iov_base;
  2782. resp->end = resp->p + (PAGE_SIZE - resp->xbuf->head[0].iov_len)/4;
  2783. return 0;
  2784. err_no_verf:
  2785. p = savep;
  2786. ADJUST_ARGS();
  2787. return nfserr;
  2788. }
  2789. static __be32
  2790. nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_remove *remove)
  2791. {
  2792. __be32 *p;
  2793. if (!nfserr) {
  2794. RESERVE_SPACE(20);
  2795. write_cinfo(&p, &remove->rm_cinfo);
  2796. ADJUST_ARGS();
  2797. }
  2798. return nfserr;
  2799. }
  2800. static __be32
  2801. nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_rename *rename)
  2802. {
  2803. __be32 *p;
  2804. if (!nfserr) {
  2805. RESERVE_SPACE(40);
  2806. write_cinfo(&p, &rename->rn_sinfo);
  2807. write_cinfo(&p, &rename->rn_tinfo);
  2808. ADJUST_ARGS();
  2809. }
  2810. return nfserr;
  2811. }
  2812. static __be32
  2813. nfsd4_do_encode_secinfo(struct nfsd4_compoundres *resp,
  2814. __be32 nfserr, struct svc_export *exp)
  2815. {
  2816. u32 i, nflavs, supported;
  2817. struct exp_flavor_info *flavs;
  2818. struct exp_flavor_info def_flavs[2];
  2819. __be32 *p, *flavorsp;
  2820. static bool report = true;
  2821. if (nfserr)
  2822. goto out;
  2823. if (exp->ex_nflavors) {
  2824. flavs = exp->ex_flavors;
  2825. nflavs = exp->ex_nflavors;
  2826. } else { /* Handling of some defaults in absence of real secinfo: */
  2827. flavs = def_flavs;
  2828. if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
  2829. nflavs = 2;
  2830. flavs[0].pseudoflavor = RPC_AUTH_UNIX;
  2831. flavs[1].pseudoflavor = RPC_AUTH_NULL;
  2832. } else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
  2833. nflavs = 1;
  2834. flavs[0].pseudoflavor
  2835. = svcauth_gss_flavor(exp->ex_client);
  2836. } else {
  2837. nflavs = 1;
  2838. flavs[0].pseudoflavor
  2839. = exp->ex_client->flavour->flavour;
  2840. }
  2841. }
  2842. supported = 0;
  2843. RESERVE_SPACE(4);
  2844. flavorsp = p++; /* to be backfilled later */
  2845. ADJUST_ARGS();
  2846. for (i = 0; i < nflavs; i++) {
  2847. rpc_authflavor_t pf = flavs[i].pseudoflavor;
  2848. struct rpcsec_gss_info info;
  2849. if (rpcauth_get_gssinfo(pf, &info) == 0) {
  2850. supported++;
  2851. RESERVE_SPACE(4 + 4 + XDR_LEN(info.oid.len) + 4 + 4);
  2852. WRITE32(RPC_AUTH_GSS);
  2853. WRITE32(info.oid.len);
  2854. WRITEMEM(info.oid.data, info.oid.len);
  2855. WRITE32(info.qop);
  2856. WRITE32(info.service);
  2857. ADJUST_ARGS();
  2858. } else if (pf < RPC_AUTH_MAXFLAVOR) {
  2859. supported++;
  2860. RESERVE_SPACE(4);
  2861. WRITE32(pf);
  2862. ADJUST_ARGS();
  2863. } else {
  2864. if (report)
  2865. pr_warn("NFS: SECINFO: security flavor %u "
  2866. "is not supported\n", pf);
  2867. }
  2868. }
  2869. if (nflavs != supported)
  2870. report = false;
  2871. *flavorsp = htonl(supported);
  2872. out:
  2873. if (exp)
  2874. exp_put(exp);
  2875. return nfserr;
  2876. }
  2877. static __be32
  2878. nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
  2879. struct nfsd4_secinfo *secinfo)
  2880. {
  2881. return nfsd4_do_encode_secinfo(resp, nfserr, secinfo->si_exp);
  2882. }
  2883. static __be32
  2884. nfsd4_encode_secinfo_no_name(struct nfsd4_compoundres *resp, __be32 nfserr,
  2885. struct nfsd4_secinfo_no_name *secinfo)
  2886. {
  2887. return nfsd4_do_encode_secinfo(resp, nfserr, secinfo->sin_exp);
  2888. }
  2889. /*
  2890. * The SETATTR encode routine is special -- it always encodes a bitmap,
  2891. * regardless of the error status.
  2892. */
  2893. static __be32
  2894. nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setattr *setattr)
  2895. {
  2896. __be32 *p;
  2897. RESERVE_SPACE(16);
  2898. if (nfserr) {
  2899. WRITE32(3);
  2900. WRITE32(0);
  2901. WRITE32(0);
  2902. WRITE32(0);
  2903. }
  2904. else {
  2905. WRITE32(3);
  2906. WRITE32(setattr->sa_bmval[0]);
  2907. WRITE32(setattr->sa_bmval[1]);
  2908. WRITE32(setattr->sa_bmval[2]);
  2909. }
  2910. ADJUST_ARGS();
  2911. return nfserr;
  2912. }
  2913. static __be32
  2914. nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setclientid *scd)
  2915. {
  2916. __be32 *p;
  2917. if (!nfserr) {
  2918. RESERVE_SPACE(8 + NFS4_VERIFIER_SIZE);
  2919. WRITEMEM(&scd->se_clientid, 8);
  2920. WRITEMEM(&scd->se_confirm, NFS4_VERIFIER_SIZE);
  2921. ADJUST_ARGS();
  2922. }
  2923. else if (nfserr == nfserr_clid_inuse) {
  2924. RESERVE_SPACE(8);
  2925. WRITE32(0);
  2926. WRITE32(0);
  2927. ADJUST_ARGS();
  2928. }
  2929. return nfserr;
  2930. }
  2931. static __be32
  2932. nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_write *write)
  2933. {
  2934. __be32 *p;
  2935. if (!nfserr) {
  2936. RESERVE_SPACE(16);
  2937. WRITE32(write->wr_bytes_written);
  2938. WRITE32(write->wr_how_written);
  2939. WRITEMEM(write->wr_verifier.data, NFS4_VERIFIER_SIZE);
  2940. ADJUST_ARGS();
  2941. }
  2942. return nfserr;
  2943. }
  2944. static const u32 nfs4_minimal_spo_must_enforce[2] = {
  2945. [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
  2946. 1 << (OP_EXCHANGE_ID - 32) |
  2947. 1 << (OP_CREATE_SESSION - 32) |
  2948. 1 << (OP_DESTROY_SESSION - 32) |
  2949. 1 << (OP_DESTROY_CLIENTID - 32)
  2950. };
  2951. static __be32
  2952. nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, __be32 nfserr,
  2953. struct nfsd4_exchange_id *exid)
  2954. {
  2955. __be32 *p;
  2956. char *major_id;
  2957. char *server_scope;
  2958. int major_id_sz;
  2959. int server_scope_sz;
  2960. uint64_t minor_id = 0;
  2961. if (nfserr)
  2962. return nfserr;
  2963. major_id = utsname()->nodename;
  2964. major_id_sz = strlen(major_id);
  2965. server_scope = utsname()->nodename;
  2966. server_scope_sz = strlen(server_scope);
  2967. RESERVE_SPACE(
  2968. 8 /* eir_clientid */ +
  2969. 4 /* eir_sequenceid */ +
  2970. 4 /* eir_flags */ +
  2971. 4 /* spr_how */);
  2972. WRITEMEM(&exid->clientid, 8);
  2973. WRITE32(exid->seqid);
  2974. WRITE32(exid->flags);
  2975. WRITE32(exid->spa_how);
  2976. ADJUST_ARGS();
  2977. switch (exid->spa_how) {
  2978. case SP4_NONE:
  2979. break;
  2980. case SP4_MACH_CRED:
  2981. /* spo_must_enforce, spo_must_allow */
  2982. RESERVE_SPACE(16);
  2983. /* spo_must_enforce bitmap: */
  2984. WRITE32(2);
  2985. WRITE32(nfs4_minimal_spo_must_enforce[0]);
  2986. WRITE32(nfs4_minimal_spo_must_enforce[1]);
  2987. /* empty spo_must_allow bitmap: */
  2988. WRITE32(0);
  2989. ADJUST_ARGS();
  2990. break;
  2991. default:
  2992. WARN_ON_ONCE(1);
  2993. }
  2994. RESERVE_SPACE(
  2995. 8 /* so_minor_id */ +
  2996. 4 /* so_major_id.len */ +
  2997. (XDR_QUADLEN(major_id_sz) * 4) +
  2998. 4 /* eir_server_scope.len */ +
  2999. (XDR_QUADLEN(server_scope_sz) * 4) +
  3000. 4 /* eir_server_impl_id.count (0) */);
  3001. /* The server_owner struct */
  3002. WRITE64(minor_id); /* Minor id */
  3003. /* major id */
  3004. WRITE32(major_id_sz);
  3005. WRITEMEM(major_id, major_id_sz);
  3006. /* Server scope */
  3007. WRITE32(server_scope_sz);
  3008. WRITEMEM(server_scope, server_scope_sz);
  3009. /* Implementation id */
  3010. WRITE32(0); /* zero length nfs_impl_id4 array */
  3011. ADJUST_ARGS();
  3012. return 0;
  3013. }
  3014. static __be32
  3015. nfsd4_encode_create_session(struct nfsd4_compoundres *resp, __be32 nfserr,
  3016. struct nfsd4_create_session *sess)
  3017. {
  3018. __be32 *p;
  3019. if (nfserr)
  3020. return nfserr;
  3021. RESERVE_SPACE(24);
  3022. WRITEMEM(sess->sessionid.data, NFS4_MAX_SESSIONID_LEN);
  3023. WRITE32(sess->seqid);
  3024. WRITE32(sess->flags);
  3025. ADJUST_ARGS();
  3026. RESERVE_SPACE(28);
  3027. WRITE32(0); /* headerpadsz */
  3028. WRITE32(sess->fore_channel.maxreq_sz);
  3029. WRITE32(sess->fore_channel.maxresp_sz);
  3030. WRITE32(sess->fore_channel.maxresp_cached);
  3031. WRITE32(sess->fore_channel.maxops);
  3032. WRITE32(sess->fore_channel.maxreqs);
  3033. WRITE32(sess->fore_channel.nr_rdma_attrs);
  3034. ADJUST_ARGS();
  3035. if (sess->fore_channel.nr_rdma_attrs) {
  3036. RESERVE_SPACE(4);
  3037. WRITE32(sess->fore_channel.rdma_attrs);
  3038. ADJUST_ARGS();
  3039. }
  3040. RESERVE_SPACE(28);
  3041. WRITE32(0); /* headerpadsz */
  3042. WRITE32(sess->back_channel.maxreq_sz);
  3043. WRITE32(sess->back_channel.maxresp_sz);
  3044. WRITE32(sess->back_channel.maxresp_cached);
  3045. WRITE32(sess->back_channel.maxops);
  3046. WRITE32(sess->back_channel.maxreqs);
  3047. WRITE32(sess->back_channel.nr_rdma_attrs);
  3048. ADJUST_ARGS();
  3049. if (sess->back_channel.nr_rdma_attrs) {
  3050. RESERVE_SPACE(4);
  3051. WRITE32(sess->back_channel.rdma_attrs);
  3052. ADJUST_ARGS();
  3053. }
  3054. return 0;
  3055. }
  3056. static __be32
  3057. nfsd4_encode_sequence(struct nfsd4_compoundres *resp, __be32 nfserr,
  3058. struct nfsd4_sequence *seq)
  3059. {
  3060. __be32 *p;
  3061. if (nfserr)
  3062. return nfserr;
  3063. RESERVE_SPACE(NFS4_MAX_SESSIONID_LEN + 20);
  3064. WRITEMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
  3065. WRITE32(seq->seqid);
  3066. WRITE32(seq->slotid);
  3067. /* Note slotid's are numbered from zero: */
  3068. WRITE32(seq->maxslots - 1); /* sr_highest_slotid */
  3069. WRITE32(seq->maxslots - 1); /* sr_target_highest_slotid */
  3070. WRITE32(seq->status_flags);
  3071. ADJUST_ARGS();
  3072. resp->cstate.datap = p; /* DRC cache data pointer */
  3073. return 0;
  3074. }
  3075. static __be32
  3076. nfsd4_encode_test_stateid(struct nfsd4_compoundres *resp, __be32 nfserr,
  3077. struct nfsd4_test_stateid *test_stateid)
  3078. {
  3079. struct nfsd4_test_stateid_id *stateid, *next;
  3080. __be32 *p;
  3081. RESERVE_SPACE(4 + (4 * test_stateid->ts_num_ids));
  3082. *p++ = htonl(test_stateid->ts_num_ids);
  3083. list_for_each_entry_safe(stateid, next, &test_stateid->ts_stateid_list, ts_id_list) {
  3084. *p++ = stateid->ts_id_status;
  3085. }
  3086. ADJUST_ARGS();
  3087. return nfserr;
  3088. }
  3089. static __be32
  3090. nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr, void *p)
  3091. {
  3092. return nfserr;
  3093. }
  3094. typedef __be32(* nfsd4_enc)(struct nfsd4_compoundres *, __be32, void *);
  3095. /*
  3096. * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
  3097. * since we don't need to filter out obsolete ops as this is
  3098. * done in the decoding phase.
  3099. */
  3100. static nfsd4_enc nfsd4_enc_ops[] = {
  3101. [OP_ACCESS] = (nfsd4_enc)nfsd4_encode_access,
  3102. [OP_CLOSE] = (nfsd4_enc)nfsd4_encode_close,
  3103. [OP_COMMIT] = (nfsd4_enc)nfsd4_encode_commit,
  3104. [OP_CREATE] = (nfsd4_enc)nfsd4_encode_create,
  3105. [OP_DELEGPURGE] = (nfsd4_enc)nfsd4_encode_noop,
  3106. [OP_DELEGRETURN] = (nfsd4_enc)nfsd4_encode_noop,
  3107. [OP_GETATTR] = (nfsd4_enc)nfsd4_encode_getattr,
  3108. [OP_GETFH] = (nfsd4_enc)nfsd4_encode_getfh,
  3109. [OP_LINK] = (nfsd4_enc)nfsd4_encode_link,
  3110. [OP_LOCK] = (nfsd4_enc)nfsd4_encode_lock,
  3111. [OP_LOCKT] = (nfsd4_enc)nfsd4_encode_lockt,
  3112. [OP_LOCKU] = (nfsd4_enc)nfsd4_encode_locku,
  3113. [OP_LOOKUP] = (nfsd4_enc)nfsd4_encode_noop,
  3114. [OP_LOOKUPP] = (nfsd4_enc)nfsd4_encode_noop,
  3115. [OP_NVERIFY] = (nfsd4_enc)nfsd4_encode_noop,
  3116. [OP_OPEN] = (nfsd4_enc)nfsd4_encode_open,
  3117. [OP_OPENATTR] = (nfsd4_enc)nfsd4_encode_noop,
  3118. [OP_OPEN_CONFIRM] = (nfsd4_enc)nfsd4_encode_open_confirm,
  3119. [OP_OPEN_DOWNGRADE] = (nfsd4_enc)nfsd4_encode_open_downgrade,
  3120. [OP_PUTFH] = (nfsd4_enc)nfsd4_encode_noop,
  3121. [OP_PUTPUBFH] = (nfsd4_enc)nfsd4_encode_noop,
  3122. [OP_PUTROOTFH] = (nfsd4_enc)nfsd4_encode_noop,
  3123. [OP_READ] = (nfsd4_enc)nfsd4_encode_read,
  3124. [OP_READDIR] = (nfsd4_enc)nfsd4_encode_readdir,
  3125. [OP_READLINK] = (nfsd4_enc)nfsd4_encode_readlink,
  3126. [OP_REMOVE] = (nfsd4_enc)nfsd4_encode_remove,
  3127. [OP_RENAME] = (nfsd4_enc)nfsd4_encode_rename,
  3128. [OP_RENEW] = (nfsd4_enc)nfsd4_encode_noop,
  3129. [OP_RESTOREFH] = (nfsd4_enc)nfsd4_encode_noop,
  3130. [OP_SAVEFH] = (nfsd4_enc)nfsd4_encode_noop,
  3131. [OP_SECINFO] = (nfsd4_enc)nfsd4_encode_secinfo,
  3132. [OP_SETATTR] = (nfsd4_enc)nfsd4_encode_setattr,
  3133. [OP_SETCLIENTID] = (nfsd4_enc)nfsd4_encode_setclientid,
  3134. [OP_SETCLIENTID_CONFIRM] = (nfsd4_enc)nfsd4_encode_noop,
  3135. [OP_VERIFY] = (nfsd4_enc)nfsd4_encode_noop,
  3136. [OP_WRITE] = (nfsd4_enc)nfsd4_encode_write,
  3137. [OP_RELEASE_LOCKOWNER] = (nfsd4_enc)nfsd4_encode_noop,
  3138. /* NFSv4.1 operations */
  3139. [OP_BACKCHANNEL_CTL] = (nfsd4_enc)nfsd4_encode_noop,
  3140. [OP_BIND_CONN_TO_SESSION] = (nfsd4_enc)nfsd4_encode_bind_conn_to_session,
  3141. [OP_EXCHANGE_ID] = (nfsd4_enc)nfsd4_encode_exchange_id,
  3142. [OP_CREATE_SESSION] = (nfsd4_enc)nfsd4_encode_create_session,
  3143. [OP_DESTROY_SESSION] = (nfsd4_enc)nfsd4_encode_noop,
  3144. [OP_FREE_STATEID] = (nfsd4_enc)nfsd4_encode_noop,
  3145. [OP_GET_DIR_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
  3146. [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_noop,
  3147. [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
  3148. [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_noop,
  3149. [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_noop,
  3150. [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_noop,
  3151. [OP_SECINFO_NO_NAME] = (nfsd4_enc)nfsd4_encode_secinfo_no_name,
  3152. [OP_SEQUENCE] = (nfsd4_enc)nfsd4_encode_sequence,
  3153. [OP_SET_SSV] = (nfsd4_enc)nfsd4_encode_noop,
  3154. [OP_TEST_STATEID] = (nfsd4_enc)nfsd4_encode_test_stateid,
  3155. [OP_WANT_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
  3156. [OP_DESTROY_CLIENTID] = (nfsd4_enc)nfsd4_encode_noop,
  3157. [OP_RECLAIM_COMPLETE] = (nfsd4_enc)nfsd4_encode_noop,
  3158. };
  3159. /*
  3160. * Calculate the total amount of memory that the compound response has taken
  3161. * after encoding the current operation with pad.
  3162. *
  3163. * pad: if operation is non-idempotent, pad was calculate by op_rsize_bop()
  3164. * which was specified at nfsd4_operation, else pad is zero.
  3165. *
  3166. * Compare this length to the session se_fmaxresp_sz and se_fmaxresp_cached.
  3167. *
  3168. * Our se_fmaxresp_cached will always be a multiple of PAGE_SIZE, and so
  3169. * will be at least a page and will therefore hold the xdr_buf head.
  3170. */
  3171. __be32 nfsd4_check_resp_size(struct nfsd4_compoundres *resp, u32 pad)
  3172. {
  3173. struct xdr_buf *xb = &resp->rqstp->rq_res;
  3174. struct nfsd4_session *session = NULL;
  3175. struct nfsd4_slot *slot = resp->cstate.slot;
  3176. u32 length, tlen = 0;
  3177. if (!nfsd4_has_session(&resp->cstate))
  3178. return 0;
  3179. session = resp->cstate.session;
  3180. if (session == NULL)
  3181. return 0;
  3182. if (xb->page_len == 0) {
  3183. length = (char *)resp->p - (char *)xb->head[0].iov_base + pad;
  3184. } else {
  3185. if (xb->tail[0].iov_base && xb->tail[0].iov_len > 0)
  3186. tlen = (char *)resp->p - (char *)xb->tail[0].iov_base;
  3187. length = xb->head[0].iov_len + xb->page_len + tlen + pad;
  3188. }
  3189. dprintk("%s length %u, xb->page_len %u tlen %u pad %u\n", __func__,
  3190. length, xb->page_len, tlen, pad);
  3191. if (length > session->se_fchannel.maxresp_sz)
  3192. return nfserr_rep_too_big;
  3193. if ((slot->sl_flags & NFSD4_SLOT_CACHETHIS) &&
  3194. length > session->se_fchannel.maxresp_cached)
  3195. return nfserr_rep_too_big_to_cache;
  3196. return 0;
  3197. }
  3198. void
  3199. nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
  3200. {
  3201. struct nfs4_stateowner *so = resp->cstate.replay_owner;
  3202. __be32 *statp;
  3203. __be32 *p;
  3204. RESERVE_SPACE(8);
  3205. WRITE32(op->opnum);
  3206. statp = p++; /* to be backfilled at the end */
  3207. ADJUST_ARGS();
  3208. if (op->opnum == OP_ILLEGAL)
  3209. goto status;
  3210. BUG_ON(op->opnum < 0 || op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
  3211. !nfsd4_enc_ops[op->opnum]);
  3212. op->status = nfsd4_enc_ops[op->opnum](resp, op->status, &op->u);
  3213. /* nfsd4_check_drc_limit guarantees enough room for error status */
  3214. if (!op->status)
  3215. op->status = nfsd4_check_resp_size(resp, 0);
  3216. if (so) {
  3217. so->so_replay.rp_status = op->status;
  3218. so->so_replay.rp_buflen = (char *)resp->p - (char *)(statp+1);
  3219. memcpy(so->so_replay.rp_buf, statp+1, so->so_replay.rp_buflen);
  3220. }
  3221. status:
  3222. /*
  3223. * Note: We write the status directly, instead of using WRITE32(),
  3224. * since it is already in network byte order.
  3225. */
  3226. *statp = op->status;
  3227. }
  3228. /*
  3229. * Encode the reply stored in the stateowner reply cache
  3230. *
  3231. * XDR note: do not encode rp->rp_buflen: the buffer contains the
  3232. * previously sent already encoded operation.
  3233. *
  3234. * called with nfs4_lock_state() held
  3235. */
  3236. void
  3237. nfsd4_encode_replay(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
  3238. {
  3239. __be32 *p;
  3240. struct nfs4_replay *rp = op->replay;
  3241. BUG_ON(!rp);
  3242. RESERVE_SPACE(8);
  3243. WRITE32(op->opnum);
  3244. *p++ = rp->rp_status; /* already xdr'ed */
  3245. ADJUST_ARGS();
  3246. RESERVE_SPACE(rp->rp_buflen);
  3247. WRITEMEM(rp->rp_buf, rp->rp_buflen);
  3248. ADJUST_ARGS();
  3249. }
  3250. int
  3251. nfs4svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
  3252. {
  3253. return xdr_ressize_check(rqstp, p);
  3254. }
  3255. int nfsd4_release_compoundargs(void *rq, __be32 *p, void *resp)
  3256. {
  3257. struct svc_rqst *rqstp = rq;
  3258. struct nfsd4_compoundargs *args = rqstp->rq_argp;
  3259. if (args->ops != args->iops) {
  3260. kfree(args->ops);
  3261. args->ops = args->iops;
  3262. }
  3263. kfree(args->tmpp);
  3264. args->tmpp = NULL;
  3265. while (args->to_free) {
  3266. struct tmpbuf *tb = args->to_free;
  3267. args->to_free = tb->next;
  3268. tb->release(tb->buf);
  3269. kfree(tb);
  3270. }
  3271. return 1;
  3272. }
  3273. int
  3274. nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundargs *args)
  3275. {
  3276. args->p = p;
  3277. args->end = rqstp->rq_arg.head[0].iov_base + rqstp->rq_arg.head[0].iov_len;
  3278. args->pagelist = rqstp->rq_arg.pages;
  3279. args->pagelen = rqstp->rq_arg.page_len;
  3280. args->tmpp = NULL;
  3281. args->to_free = NULL;
  3282. args->ops = args->iops;
  3283. args->rqstp = rqstp;
  3284. return !nfsd4_decode_compound(args);
  3285. }
  3286. int
  3287. nfs4svc_encode_compoundres(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundres *resp)
  3288. {
  3289. /*
  3290. * All that remains is to write the tag and operation count...
  3291. */
  3292. struct nfsd4_compound_state *cs = &resp->cstate;
  3293. struct kvec *iov;
  3294. p = resp->tagp;
  3295. *p++ = htonl(resp->taglen);
  3296. memcpy(p, resp->tag, resp->taglen);
  3297. p += XDR_QUADLEN(resp->taglen);
  3298. *p++ = htonl(resp->opcnt);
  3299. if (rqstp->rq_res.page_len)
  3300. iov = &rqstp->rq_res.tail[0];
  3301. else
  3302. iov = &rqstp->rq_res.head[0];
  3303. iov->iov_len = ((char*)resp->p) - (char*)iov->iov_base;
  3304. BUG_ON(iov->iov_len > PAGE_SIZE);
  3305. if (nfsd4_has_session(cs)) {
  3306. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  3307. struct nfs4_client *clp = cs->session->se_client;
  3308. if (cs->status != nfserr_replay_cache) {
  3309. nfsd4_store_cache_entry(resp);
  3310. cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
  3311. }
  3312. /* Renew the clientid on success and on replay */
  3313. spin_lock(&nn->client_lock);
  3314. nfsd4_put_session(cs->session);
  3315. spin_unlock(&nn->client_lock);
  3316. put_client_renew(clp);
  3317. }
  3318. return 1;
  3319. }
  3320. /*
  3321. * Local variables:
  3322. * c-basic-offset: 8
  3323. * End:
  3324. */