nfs4xdr.c 68 KB

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