nfs4xdr.c 76 KB

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