nfs4xdr.c 90 KB

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