nfs4xdr.c 66 KB

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