nfs4xdr.c 68 KB

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