nfs4xdr.c 69 KB

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