nfs4xdr.c 89 KB

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