nfs4xdr.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687
  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 int
  68. check_filename(char *str, int len, int 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. u32 *p; \
  92. int 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 u32 *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. u32 *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, u32 *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 int
  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 int
  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. DECODE_HEAD;
  231. iattr->ia_valid = 0;
  232. if ((status = nfsd4_decode_bitmap(argp, bmval)))
  233. return status;
  234. /*
  235. * According to spec, unsupported attributes return ERR_NOTSUPP;
  236. * read-only attributes return ERR_INVAL.
  237. */
  238. if ((bmval[0] & ~NFSD_SUPPORTED_ATTRS_WORD0) || (bmval[1] & ~NFSD_SUPPORTED_ATTRS_WORD1))
  239. return nfserr_attrnotsupp;
  240. if ((bmval[0] & ~NFSD_WRITEABLE_ATTRS_WORD0) || (bmval[1] & ~NFSD_WRITEABLE_ATTRS_WORD1))
  241. return nfserr_inval;
  242. READ_BUF(4);
  243. READ32(expected_len);
  244. if (bmval[0] & FATTR4_WORD0_SIZE) {
  245. READ_BUF(8);
  246. len += 8;
  247. READ64(iattr->ia_size);
  248. iattr->ia_valid |= ATTR_SIZE;
  249. }
  250. if (bmval[0] & FATTR4_WORD0_ACL) {
  251. int nace, i;
  252. struct nfs4_ace ace;
  253. READ_BUF(4); len += 4;
  254. READ32(nace);
  255. *acl = nfs4_acl_new();
  256. if (*acl == NULL) {
  257. status = -ENOMEM;
  258. goto out_nfserr;
  259. }
  260. defer_free(argp, (void (*)(const void *))nfs4_acl_free, *acl);
  261. for (i = 0; i < nace; i++) {
  262. READ_BUF(16); len += 16;
  263. READ32(ace.type);
  264. READ32(ace.flag);
  265. READ32(ace.access_mask);
  266. READ32(dummy32);
  267. READ_BUF(dummy32);
  268. len += XDR_QUADLEN(dummy32) << 2;
  269. READMEM(buf, dummy32);
  270. ace.whotype = nfs4_acl_get_whotype(buf, dummy32);
  271. status = 0;
  272. if (ace.whotype != NFS4_ACL_WHO_NAMED)
  273. ace.who = 0;
  274. else if (ace.flag & NFS4_ACE_IDENTIFIER_GROUP)
  275. status = nfsd_map_name_to_gid(argp->rqstp,
  276. buf, dummy32, &ace.who);
  277. else
  278. status = nfsd_map_name_to_uid(argp->rqstp,
  279. buf, dummy32, &ace.who);
  280. if (status)
  281. goto out_nfserr;
  282. status = nfs4_acl_add_ace(*acl, ace.type, ace.flag,
  283. ace.access_mask, ace.whotype, ace.who);
  284. if (status)
  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 ((status = 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 ((status = 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(status);
  389. goto out;
  390. }
  391. static int
  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 int
  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 int
  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 int
  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 int
  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 int
  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 int
  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 int
  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 int
  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 int
  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 int
  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 int
  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 int
  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 int
  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 int
  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 int
  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 int
  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 int
  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 int
  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 int
  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 int
  728. nfsd4_decode_setattr(struct nfsd4_compoundargs *argp, struct nfsd4_setattr *setattr)
  729. {
  730. DECODE_HEAD;
  731. READ_BUF(sizeof(stateid_t));
  732. READ32(setattr->sa_stateid.si_generation);
  733. COPYMEM(&setattr->sa_stateid.si_opaque, sizeof(stateid_opaque_t));
  734. if ((status = nfsd4_decode_fattr(argp, setattr->sa_bmval, &setattr->sa_iattr, &setattr->sa_acl)))
  735. goto out;
  736. DECODE_TAIL;
  737. }
  738. static int
  739. nfsd4_decode_setclientid(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid *setclientid)
  740. {
  741. DECODE_HEAD;
  742. READ_BUF(12);
  743. COPYMEM(setclientid->se_verf.data, 8);
  744. READ32(setclientid->se_namelen);
  745. READ_BUF(setclientid->se_namelen + 8);
  746. SAVEMEM(setclientid->se_name, setclientid->se_namelen);
  747. READ32(setclientid->se_callback_prog);
  748. READ32(setclientid->se_callback_netid_len);
  749. READ_BUF(setclientid->se_callback_netid_len + 4);
  750. SAVEMEM(setclientid->se_callback_netid_val, setclientid->se_callback_netid_len);
  751. READ32(setclientid->se_callback_addr_len);
  752. READ_BUF(setclientid->se_callback_addr_len + 4);
  753. SAVEMEM(setclientid->se_callback_addr_val, setclientid->se_callback_addr_len);
  754. READ32(setclientid->se_callback_ident);
  755. DECODE_TAIL;
  756. }
  757. static int
  758. nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid_confirm *scd_c)
  759. {
  760. DECODE_HEAD;
  761. READ_BUF(8 + sizeof(nfs4_verifier));
  762. COPYMEM(&scd_c->sc_clientid, 8);
  763. COPYMEM(&scd_c->sc_confirm, sizeof(nfs4_verifier));
  764. DECODE_TAIL;
  765. }
  766. /* Also used for NVERIFY */
  767. static int
  768. nfsd4_decode_verify(struct nfsd4_compoundargs *argp, struct nfsd4_verify *verify)
  769. {
  770. #if 0
  771. struct nfsd4_compoundargs save = {
  772. .p = argp->p,
  773. .end = argp->end,
  774. .rqstp = argp->rqstp,
  775. };
  776. u32 ve_bmval[2];
  777. struct iattr ve_iattr; /* request */
  778. struct nfs4_acl *ve_acl; /* request */
  779. #endif
  780. DECODE_HEAD;
  781. if ((status = nfsd4_decode_bitmap(argp, verify->ve_bmval)))
  782. goto out;
  783. /* For convenience's sake, we compare raw xdr'd attributes in
  784. * nfsd4_proc_verify; however we still decode here just to return
  785. * correct error in case of bad xdr. */
  786. #if 0
  787. status = nfsd4_decode_fattr(ve_bmval, &ve_iattr, &ve_acl);
  788. if (status == nfserr_inval) {
  789. status = nfserrno(status);
  790. goto out;
  791. }
  792. #endif
  793. READ_BUF(4);
  794. READ32(verify->ve_attrlen);
  795. READ_BUF(verify->ve_attrlen);
  796. SAVEMEM(verify->ve_attrval, verify->ve_attrlen);
  797. DECODE_TAIL;
  798. }
  799. static int
  800. nfsd4_decode_write(struct nfsd4_compoundargs *argp, struct nfsd4_write *write)
  801. {
  802. int avail;
  803. int v;
  804. int len;
  805. DECODE_HEAD;
  806. READ_BUF(sizeof(stateid_opaque_t) + 20);
  807. READ32(write->wr_stateid.si_generation);
  808. COPYMEM(&write->wr_stateid.si_opaque, sizeof(stateid_opaque_t));
  809. READ64(write->wr_offset);
  810. READ32(write->wr_stable_how);
  811. if (write->wr_stable_how > 2)
  812. goto xdr_error;
  813. READ32(write->wr_buflen);
  814. /* Sorry .. no magic macros for this.. *
  815. * READ_BUF(write->wr_buflen);
  816. * SAVEMEM(write->wr_buf, write->wr_buflen);
  817. */
  818. avail = (char*)argp->end - (char*)argp->p;
  819. if (avail + argp->pagelen < write->wr_buflen) {
  820. printk(KERN_NOTICE "xdr error! (%s:%d)\n", __FILE__, __LINE__);
  821. goto xdr_error;
  822. }
  823. argp->rqstp->rq_vec[0].iov_base = p;
  824. argp->rqstp->rq_vec[0].iov_len = avail;
  825. v = 0;
  826. len = write->wr_buflen;
  827. while (len > argp->rqstp->rq_vec[v].iov_len) {
  828. len -= argp->rqstp->rq_vec[v].iov_len;
  829. v++;
  830. argp->rqstp->rq_vec[v].iov_base = page_address(argp->pagelist[0]);
  831. argp->pagelist++;
  832. if (argp->pagelen >= PAGE_SIZE) {
  833. argp->rqstp->rq_vec[v].iov_len = PAGE_SIZE;
  834. argp->pagelen -= PAGE_SIZE;
  835. } else {
  836. argp->rqstp->rq_vec[v].iov_len = argp->pagelen;
  837. argp->pagelen -= len;
  838. }
  839. }
  840. argp->end = (u32*) (argp->rqstp->rq_vec[v].iov_base + argp->rqstp->rq_vec[v].iov_len);
  841. argp->p = (u32*) (argp->rqstp->rq_vec[v].iov_base + (XDR_QUADLEN(len) << 2));
  842. argp->rqstp->rq_vec[v].iov_len = len;
  843. write->wr_vlen = v+1;
  844. DECODE_TAIL;
  845. }
  846. static int
  847. nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp, struct nfsd4_release_lockowner *rlockowner)
  848. {
  849. DECODE_HEAD;
  850. READ_BUF(12);
  851. COPYMEM(&rlockowner->rl_clientid, sizeof(clientid_t));
  852. READ32(rlockowner->rl_owner.len);
  853. READ_BUF(rlockowner->rl_owner.len);
  854. READMEM(rlockowner->rl_owner.data, rlockowner->rl_owner.len);
  855. DECODE_TAIL;
  856. }
  857. static int
  858. nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
  859. {
  860. DECODE_HEAD;
  861. struct nfsd4_op *op;
  862. int i;
  863. /*
  864. * XXX: According to spec, we should check the tag
  865. * for UTF-8 compliance. I'm postponing this for
  866. * now because it seems that some clients do use
  867. * binary tags.
  868. */
  869. READ_BUF(4);
  870. READ32(argp->taglen);
  871. READ_BUF(argp->taglen + 8);
  872. SAVEMEM(argp->tag, argp->taglen);
  873. READ32(argp->minorversion);
  874. READ32(argp->opcnt);
  875. if (argp->taglen > NFSD4_MAX_TAGLEN)
  876. goto xdr_error;
  877. if (argp->opcnt > 100)
  878. goto xdr_error;
  879. if (argp->opcnt > ARRAY_SIZE(argp->iops)) {
  880. argp->ops = kmalloc(argp->opcnt * sizeof(*argp->ops), GFP_KERNEL);
  881. if (!argp->ops) {
  882. argp->ops = argp->iops;
  883. printk(KERN_INFO "nfsd: couldn't allocate room for COMPOUND\n");
  884. goto xdr_error;
  885. }
  886. }
  887. for (i = 0; i < argp->opcnt; i++) {
  888. op = &argp->ops[i];
  889. op->replay = NULL;
  890. /*
  891. * We can't use READ_BUF() here because we need to handle
  892. * a missing opcode as an OP_WRITE + 1. So we need to check
  893. * to see if we're truly at the end of our buffer or if there
  894. * is another page we need to flip to.
  895. */
  896. if (argp->p == argp->end) {
  897. if (argp->pagelen < 4) {
  898. /* There isn't an opcode still on the wire */
  899. op->opnum = OP_WRITE + 1;
  900. op->status = nfserr_bad_xdr;
  901. argp->opcnt = i+1;
  902. break;
  903. }
  904. /*
  905. * False alarm. We just hit a page boundary, but there
  906. * is still data available. Move pointer across page
  907. * boundary. *snip from READ_BUF*
  908. */
  909. argp->p = page_address(argp->pagelist[0]);
  910. argp->pagelist++;
  911. if (argp->pagelen < PAGE_SIZE) {
  912. argp->end = p + (argp->pagelen>>2);
  913. argp->pagelen = 0;
  914. } else {
  915. argp->end = p + (PAGE_SIZE>>2);
  916. argp->pagelen -= PAGE_SIZE;
  917. }
  918. }
  919. op->opnum = ntohl(*argp->p++);
  920. switch (op->opnum) {
  921. case 2: /* Reserved operation */
  922. op->opnum = OP_ILLEGAL;
  923. if (argp->minorversion == 0)
  924. op->status = nfserr_op_illegal;
  925. else
  926. op->status = nfserr_minor_vers_mismatch;
  927. break;
  928. case OP_ACCESS:
  929. op->status = nfsd4_decode_access(argp, &op->u.access);
  930. break;
  931. case OP_CLOSE:
  932. op->status = nfsd4_decode_close(argp, &op->u.close);
  933. break;
  934. case OP_COMMIT:
  935. op->status = nfsd4_decode_commit(argp, &op->u.commit);
  936. break;
  937. case OP_CREATE:
  938. op->status = nfsd4_decode_create(argp, &op->u.create);
  939. break;
  940. case OP_DELEGRETURN:
  941. op->status = nfsd4_decode_delegreturn(argp, &op->u.delegreturn);
  942. break;
  943. case OP_GETATTR:
  944. op->status = nfsd4_decode_getattr(argp, &op->u.getattr);
  945. break;
  946. case OP_GETFH:
  947. op->status = nfs_ok;
  948. break;
  949. case OP_LINK:
  950. op->status = nfsd4_decode_link(argp, &op->u.link);
  951. break;
  952. case OP_LOCK:
  953. op->status = nfsd4_decode_lock(argp, &op->u.lock);
  954. break;
  955. case OP_LOCKT:
  956. op->status = nfsd4_decode_lockt(argp, &op->u.lockt);
  957. break;
  958. case OP_LOCKU:
  959. op->status = nfsd4_decode_locku(argp, &op->u.locku);
  960. break;
  961. case OP_LOOKUP:
  962. op->status = nfsd4_decode_lookup(argp, &op->u.lookup);
  963. break;
  964. case OP_LOOKUPP:
  965. op->status = nfs_ok;
  966. break;
  967. case OP_NVERIFY:
  968. op->status = nfsd4_decode_verify(argp, &op->u.nverify);
  969. break;
  970. case OP_OPEN:
  971. op->status = nfsd4_decode_open(argp, &op->u.open);
  972. break;
  973. case OP_OPEN_CONFIRM:
  974. op->status = nfsd4_decode_open_confirm(argp, &op->u.open_confirm);
  975. break;
  976. case OP_OPEN_DOWNGRADE:
  977. op->status = nfsd4_decode_open_downgrade(argp, &op->u.open_downgrade);
  978. break;
  979. case OP_PUTFH:
  980. op->status = nfsd4_decode_putfh(argp, &op->u.putfh);
  981. break;
  982. case OP_PUTROOTFH:
  983. op->status = nfs_ok;
  984. break;
  985. case OP_READ:
  986. op->status = nfsd4_decode_read(argp, &op->u.read);
  987. break;
  988. case OP_READDIR:
  989. op->status = nfsd4_decode_readdir(argp, &op->u.readdir);
  990. break;
  991. case OP_READLINK:
  992. op->status = nfs_ok;
  993. break;
  994. case OP_REMOVE:
  995. op->status = nfsd4_decode_remove(argp, &op->u.remove);
  996. break;
  997. case OP_RENAME:
  998. op->status = nfsd4_decode_rename(argp, &op->u.rename);
  999. break;
  1000. case OP_RESTOREFH:
  1001. op->status = nfs_ok;
  1002. break;
  1003. case OP_RENEW:
  1004. op->status = nfsd4_decode_renew(argp, &op->u.renew);
  1005. break;
  1006. case OP_SAVEFH:
  1007. op->status = nfs_ok;
  1008. break;
  1009. case OP_SETATTR:
  1010. op->status = nfsd4_decode_setattr(argp, &op->u.setattr);
  1011. break;
  1012. case OP_SETCLIENTID:
  1013. op->status = nfsd4_decode_setclientid(argp, &op->u.setclientid);
  1014. break;
  1015. case OP_SETCLIENTID_CONFIRM:
  1016. op->status = nfsd4_decode_setclientid_confirm(argp, &op->u.setclientid_confirm);
  1017. break;
  1018. case OP_VERIFY:
  1019. op->status = nfsd4_decode_verify(argp, &op->u.verify);
  1020. break;
  1021. case OP_WRITE:
  1022. op->status = nfsd4_decode_write(argp, &op->u.write);
  1023. break;
  1024. case OP_RELEASE_LOCKOWNER:
  1025. op->status = nfsd4_decode_release_lockowner(argp, &op->u.release_lockowner);
  1026. break;
  1027. default:
  1028. op->opnum = OP_ILLEGAL;
  1029. op->status = nfserr_op_illegal;
  1030. break;
  1031. }
  1032. if (op->status) {
  1033. argp->opcnt = i+1;
  1034. break;
  1035. }
  1036. }
  1037. DECODE_TAIL;
  1038. }
  1039. /*
  1040. * END OF "GENERIC" DECODE ROUTINES.
  1041. */
  1042. /*
  1043. * START OF "GENERIC" ENCODE ROUTINES.
  1044. * These may look a little ugly since they are imported from a "generic"
  1045. * set of XDR encode/decode routines which are intended to be shared by
  1046. * all of our NFSv4 implementations (OpenBSD, MacOS X...).
  1047. *
  1048. * If the pain of reading these is too great, it should be a straightforward
  1049. * task to translate them into Linux-specific versions which are more
  1050. * consistent with the style used in NFSv2/v3...
  1051. */
  1052. #define ENCODE_HEAD u32 *p
  1053. #define WRITE32(n) *p++ = htonl(n)
  1054. #define WRITE64(n) do { \
  1055. *p++ = htonl((u32)((n) >> 32)); \
  1056. *p++ = htonl((u32)(n)); \
  1057. } while (0)
  1058. #define WRITEMEM(ptr,nbytes) do { \
  1059. *(p + XDR_QUADLEN(nbytes) -1) = 0; \
  1060. memcpy(p, ptr, nbytes); \
  1061. p += XDR_QUADLEN(nbytes); \
  1062. } while (0)
  1063. #define WRITECINFO(c) do { \
  1064. *p++ = htonl(c.atomic); \
  1065. *p++ = htonl(c.before_ctime_sec); \
  1066. *p++ = htonl(c.before_ctime_nsec); \
  1067. *p++ = htonl(c.after_ctime_sec); \
  1068. *p++ = htonl(c.after_ctime_nsec); \
  1069. } while (0)
  1070. #define RESERVE_SPACE(nbytes) do { \
  1071. p = resp->p; \
  1072. BUG_ON(p + XDR_QUADLEN(nbytes) > resp->end); \
  1073. } while (0)
  1074. #define ADJUST_ARGS() resp->p = p
  1075. /*
  1076. * Header routine to setup seqid operation replay cache
  1077. */
  1078. #define ENCODE_SEQID_OP_HEAD \
  1079. u32 *p; \
  1080. u32 *save; \
  1081. \
  1082. save = resp->p;
  1083. /*
  1084. * Routine for encoding the result of a "seqid-mutating" NFSv4 operation. This
  1085. * is where sequence id's are incremented, and the replay cache is filled.
  1086. * Note that we increment sequence id's here, at the last moment, so we're sure
  1087. * we know whether the error to be returned is a sequence id mutating error.
  1088. */
  1089. #define ENCODE_SEQID_OP_TAIL(stateowner) do { \
  1090. if (seqid_mutating_err(nfserr) && stateowner) { \
  1091. stateowner->so_seqid++; \
  1092. stateowner->so_replay.rp_status = nfserr; \
  1093. stateowner->so_replay.rp_buflen = \
  1094. (((char *)(resp)->p - (char *)save)); \
  1095. memcpy(stateowner->so_replay.rp_buf, save, \
  1096. stateowner->so_replay.rp_buflen); \
  1097. } } while (0);
  1098. /* Encode as an array of strings the string given with components
  1099. * seperated @sep.
  1100. */
  1101. static int nfsd4_encode_components(char sep, char *components,
  1102. u32 **pp, int *buflen)
  1103. {
  1104. u32 *p = *pp;
  1105. u32 *countp = p;
  1106. int strlen, count=0;
  1107. char *str, *end;
  1108. dprintk("nfsd4_encode_components(%s)\n", components);
  1109. if ((*buflen -= 4) < 0)
  1110. return nfserr_resource;
  1111. WRITE32(0); /* We will fill this in with @count later */
  1112. end = str = components;
  1113. while (*end) {
  1114. for (; *end && (*end != sep); end++)
  1115. ; /* Point to end of component */
  1116. strlen = end - str;
  1117. if (strlen) {
  1118. if ((*buflen -= ((XDR_QUADLEN(strlen) << 2) + 4)) < 0)
  1119. return nfserr_resource;
  1120. WRITE32(strlen);
  1121. WRITEMEM(str, strlen);
  1122. count++;
  1123. }
  1124. else
  1125. end++;
  1126. str = end;
  1127. }
  1128. *pp = p;
  1129. p = countp;
  1130. WRITE32(count);
  1131. return 0;
  1132. }
  1133. /*
  1134. * encode a location element of a fs_locations structure
  1135. */
  1136. static int nfsd4_encode_fs_location4(struct nfsd4_fs_location *location,
  1137. u32 **pp, int *buflen)
  1138. {
  1139. int status;
  1140. u32 *p = *pp;
  1141. status = nfsd4_encode_components(':', location->hosts, &p, buflen);
  1142. if (status)
  1143. return status;
  1144. status = nfsd4_encode_components('/', location->path, &p, buflen);
  1145. if (status)
  1146. return status;
  1147. *pp = p;
  1148. return 0;
  1149. }
  1150. /*
  1151. * Return the path to an export point in the pseudo filesystem namespace
  1152. * Returned string is safe to use as long as the caller holds a reference
  1153. * to @exp.
  1154. */
  1155. static char *nfsd4_path(struct svc_rqst *rqstp, struct svc_export *exp)
  1156. {
  1157. struct svc_fh tmp_fh;
  1158. char *path, *rootpath;
  1159. int stat;
  1160. fh_init(&tmp_fh, NFS4_FHSIZE);
  1161. stat = exp_pseudoroot(rqstp->rq_client, &tmp_fh, &rqstp->rq_chandle);
  1162. if (stat)
  1163. return ERR_PTR(stat);
  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. return ERR_PTR(-EOPNOTSUPP);
  1170. }
  1171. return path + strlen(rootpath);
  1172. }
  1173. /*
  1174. * encode a fs_locations structure
  1175. */
  1176. static int nfsd4_encode_fs_locations(struct svc_rqst *rqstp,
  1177. struct svc_export *exp,
  1178. u32 **pp, int *buflen)
  1179. {
  1180. int status, i;
  1181. u32 *p = *pp;
  1182. struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
  1183. char *root = nfsd4_path(rqstp, exp);
  1184. if (IS_ERR(root))
  1185. return PTR_ERR(root);
  1186. status = nfsd4_encode_components('/', root, &p, buflen);
  1187. if (status)
  1188. return status;
  1189. if ((*buflen -= 4) < 0)
  1190. return nfserr_resource;
  1191. WRITE32(fslocs->locations_count);
  1192. for (i=0; i<fslocs->locations_count; i++) {
  1193. status = nfsd4_encode_fs_location4(&fslocs->locations[i],
  1194. &p, buflen);
  1195. if (status)
  1196. return status;
  1197. }
  1198. *pp = p;
  1199. return 0;
  1200. }
  1201. static u32 nfs4_ftypes[16] = {
  1202. NF4BAD, NF4FIFO, NF4CHR, NF4BAD,
  1203. NF4DIR, NF4BAD, NF4BLK, NF4BAD,
  1204. NF4REG, NF4BAD, NF4LNK, NF4BAD,
  1205. NF4SOCK, NF4BAD, NF4LNK, NF4BAD,
  1206. };
  1207. static int
  1208. nfsd4_encode_name(struct svc_rqst *rqstp, int whotype, uid_t id, int group,
  1209. u32 **p, int *buflen)
  1210. {
  1211. int status;
  1212. if (*buflen < (XDR_QUADLEN(IDMAP_NAMESZ) << 2) + 4)
  1213. return nfserr_resource;
  1214. if (whotype != NFS4_ACL_WHO_NAMED)
  1215. status = nfs4_acl_write_who(whotype, (u8 *)(*p + 1));
  1216. else if (group)
  1217. status = nfsd_map_gid_to_name(rqstp, id, (u8 *)(*p + 1));
  1218. else
  1219. status = nfsd_map_uid_to_name(rqstp, id, (u8 *)(*p + 1));
  1220. if (status < 0)
  1221. return nfserrno(status);
  1222. *p = xdr_encode_opaque(*p, NULL, status);
  1223. *buflen -= (XDR_QUADLEN(status) << 2) + 4;
  1224. BUG_ON(*buflen < 0);
  1225. return 0;
  1226. }
  1227. static inline int
  1228. nfsd4_encode_user(struct svc_rqst *rqstp, uid_t uid, u32 **p, int *buflen)
  1229. {
  1230. return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, uid, 0, p, buflen);
  1231. }
  1232. static inline int
  1233. nfsd4_encode_group(struct svc_rqst *rqstp, uid_t gid, u32 **p, int *buflen)
  1234. {
  1235. return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, gid, 1, p, buflen);
  1236. }
  1237. static inline int
  1238. nfsd4_encode_aclname(struct svc_rqst *rqstp, int whotype, uid_t id, int group,
  1239. u32 **p, int *buflen)
  1240. {
  1241. return nfsd4_encode_name(rqstp, whotype, id, group, p, buflen);
  1242. }
  1243. #define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
  1244. FATTR4_WORD0_RDATTR_ERROR)
  1245. #define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
  1246. static int fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *rdattr_err)
  1247. {
  1248. /* As per referral draft: */
  1249. if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
  1250. *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
  1251. if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
  1252. *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
  1253. *rdattr_err = NFSERR_MOVED;
  1254. else
  1255. return nfserr_moved;
  1256. }
  1257. *bmval0 &= WORD0_ABSENT_FS_ATTRS;
  1258. *bmval1 &= WORD1_ABSENT_FS_ATTRS;
  1259. return 0;
  1260. }
  1261. /*
  1262. * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
  1263. * ourselves.
  1264. *
  1265. * @countp is the buffer size in _words_; upon successful return this becomes
  1266. * replaced with the number of words written.
  1267. */
  1268. int
  1269. nfsd4_encode_fattr(struct svc_fh *fhp, struct svc_export *exp,
  1270. struct dentry *dentry, u32 *buffer, int *countp, u32 *bmval,
  1271. struct svc_rqst *rqstp)
  1272. {
  1273. u32 bmval0 = bmval[0];
  1274. u32 bmval1 = bmval[1];
  1275. struct kstat stat;
  1276. struct svc_fh tempfh;
  1277. struct kstatfs statfs;
  1278. int buflen = *countp << 2;
  1279. u32 *attrlenp;
  1280. u32 dummy;
  1281. u64 dummy64;
  1282. u32 rdattr_err = 0;
  1283. u32 *p = buffer;
  1284. int status;
  1285. int aclsupport = 0;
  1286. struct nfs4_acl *acl = NULL;
  1287. BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
  1288. BUG_ON(bmval0 & ~NFSD_SUPPORTED_ATTRS_WORD0);
  1289. BUG_ON(bmval1 & ~NFSD_SUPPORTED_ATTRS_WORD1);
  1290. if (exp->ex_fslocs.migrated) {
  1291. status = fattr_handle_absent_fs(&bmval0, &bmval1, &rdattr_err);
  1292. if (status)
  1293. goto out;
  1294. }
  1295. status = vfs_getattr(exp->ex_mnt, dentry, &stat);
  1296. if (status)
  1297. goto out_nfserr;
  1298. if ((bmval0 & (FATTR4_WORD0_FILES_FREE | FATTR4_WORD0_FILES_TOTAL)) ||
  1299. (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
  1300. FATTR4_WORD1_SPACE_TOTAL))) {
  1301. status = vfs_statfs(dentry, &statfs);
  1302. if (status)
  1303. goto out_nfserr;
  1304. }
  1305. if ((bmval0 & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) && !fhp) {
  1306. fh_init(&tempfh, NFS4_FHSIZE);
  1307. status = fh_compose(&tempfh, exp, dentry, NULL);
  1308. if (status)
  1309. goto out;
  1310. fhp = &tempfh;
  1311. }
  1312. if (bmval0 & (FATTR4_WORD0_ACL | FATTR4_WORD0_ACLSUPPORT
  1313. | FATTR4_WORD0_SUPPORTED_ATTRS)) {
  1314. status = nfsd4_get_nfs4_acl(rqstp, dentry, &acl);
  1315. aclsupport = (status == 0);
  1316. if (bmval0 & FATTR4_WORD0_ACL) {
  1317. if (status == -EOPNOTSUPP)
  1318. bmval0 &= ~FATTR4_WORD0_ACL;
  1319. else if (status == -EINVAL) {
  1320. status = nfserr_attrnotsupp;
  1321. goto out;
  1322. } else if (status != 0)
  1323. goto out_nfserr;
  1324. }
  1325. }
  1326. if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
  1327. if (exp->ex_fslocs.locations == NULL) {
  1328. bmval0 &= ~FATTR4_WORD0_FS_LOCATIONS;
  1329. }
  1330. }
  1331. if ((buflen -= 16) < 0)
  1332. goto out_resource;
  1333. WRITE32(2);
  1334. WRITE32(bmval0);
  1335. WRITE32(bmval1);
  1336. attrlenp = p++; /* to be backfilled later */
  1337. if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
  1338. u32 word0 = NFSD_SUPPORTED_ATTRS_WORD0;
  1339. if ((buflen -= 12) < 0)
  1340. goto out_resource;
  1341. if (!aclsupport)
  1342. word0 &= ~FATTR4_WORD0_ACL;
  1343. if (!exp->ex_fslocs.locations)
  1344. word0 &= ~FATTR4_WORD0_FS_LOCATIONS;
  1345. WRITE32(2);
  1346. WRITE32(word0);
  1347. WRITE32(NFSD_SUPPORTED_ATTRS_WORD1);
  1348. }
  1349. if (bmval0 & FATTR4_WORD0_TYPE) {
  1350. if ((buflen -= 4) < 0)
  1351. goto out_resource;
  1352. dummy = nfs4_ftypes[(stat.mode & S_IFMT) >> 12];
  1353. if (dummy == NF4BAD)
  1354. goto out_serverfault;
  1355. WRITE32(dummy);
  1356. }
  1357. if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
  1358. if ((buflen -= 4) < 0)
  1359. goto out_resource;
  1360. if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
  1361. WRITE32(NFS4_FH_PERSISTENT);
  1362. else
  1363. WRITE32(NFS4_FH_PERSISTENT|NFS4_FH_VOL_RENAME);
  1364. }
  1365. if (bmval0 & FATTR4_WORD0_CHANGE) {
  1366. /*
  1367. * Note: This _must_ be consistent with the scheme for writing
  1368. * change_info, so any changes made here must be reflected there
  1369. * as well. (See xdr4.h:set_change_info() and the WRITECINFO()
  1370. * macro above.)
  1371. */
  1372. if ((buflen -= 8) < 0)
  1373. goto out_resource;
  1374. WRITE32(stat.ctime.tv_sec);
  1375. WRITE32(stat.ctime.tv_nsec);
  1376. }
  1377. if (bmval0 & FATTR4_WORD0_SIZE) {
  1378. if ((buflen -= 8) < 0)
  1379. goto out_resource;
  1380. WRITE64(stat.size);
  1381. }
  1382. if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
  1383. if ((buflen -= 4) < 0)
  1384. goto out_resource;
  1385. WRITE32(1);
  1386. }
  1387. if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
  1388. if ((buflen -= 4) < 0)
  1389. goto out_resource;
  1390. WRITE32(1);
  1391. }
  1392. if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
  1393. if ((buflen -= 4) < 0)
  1394. goto out_resource;
  1395. WRITE32(0);
  1396. }
  1397. if (bmval0 & FATTR4_WORD0_FSID) {
  1398. if ((buflen -= 16) < 0)
  1399. goto out_resource;
  1400. if (exp->ex_fslocs.migrated) {
  1401. WRITE64(NFS4_REFERRAL_FSID_MAJOR);
  1402. WRITE64(NFS4_REFERRAL_FSID_MINOR);
  1403. } else if (is_fsid(fhp, rqstp->rq_reffh)) {
  1404. WRITE64((u64)exp->ex_fsid);
  1405. WRITE64((u64)0);
  1406. } else {
  1407. WRITE32(0);
  1408. WRITE32(MAJOR(stat.dev));
  1409. WRITE32(0);
  1410. WRITE32(MINOR(stat.dev));
  1411. }
  1412. }
  1413. if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
  1414. if ((buflen -= 4) < 0)
  1415. goto out_resource;
  1416. WRITE32(0);
  1417. }
  1418. if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
  1419. if ((buflen -= 4) < 0)
  1420. goto out_resource;
  1421. WRITE32(NFSD_LEASE_TIME);
  1422. }
  1423. if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
  1424. if ((buflen -= 4) < 0)
  1425. goto out_resource;
  1426. WRITE32(rdattr_err);
  1427. }
  1428. if (bmval0 & FATTR4_WORD0_ACL) {
  1429. struct nfs4_ace *ace;
  1430. struct list_head *h;
  1431. if (acl == NULL) {
  1432. if ((buflen -= 4) < 0)
  1433. goto out_resource;
  1434. WRITE32(0);
  1435. goto out_acl;
  1436. }
  1437. if ((buflen -= 4) < 0)
  1438. goto out_resource;
  1439. WRITE32(acl->naces);
  1440. list_for_each(h, &acl->ace_head) {
  1441. ace = list_entry(h, struct nfs4_ace, l_ace);
  1442. if ((buflen -= 4*3) < 0)
  1443. goto out_resource;
  1444. WRITE32(ace->type);
  1445. WRITE32(ace->flag);
  1446. WRITE32(ace->access_mask & NFS4_ACE_MASK_ALL);
  1447. status = nfsd4_encode_aclname(rqstp, ace->whotype,
  1448. ace->who, ace->flag & NFS4_ACE_IDENTIFIER_GROUP,
  1449. &p, &buflen);
  1450. if (status == nfserr_resource)
  1451. goto out_resource;
  1452. if (status)
  1453. goto out;
  1454. }
  1455. }
  1456. out_acl:
  1457. if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
  1458. if ((buflen -= 4) < 0)
  1459. goto out_resource;
  1460. WRITE32(aclsupport ?
  1461. ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL : 0);
  1462. }
  1463. if (bmval0 & FATTR4_WORD0_CANSETTIME) {
  1464. if ((buflen -= 4) < 0)
  1465. goto out_resource;
  1466. WRITE32(1);
  1467. }
  1468. if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
  1469. if ((buflen -= 4) < 0)
  1470. goto out_resource;
  1471. WRITE32(1);
  1472. }
  1473. if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
  1474. if ((buflen -= 4) < 0)
  1475. goto out_resource;
  1476. WRITE32(1);
  1477. }
  1478. if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
  1479. if ((buflen -= 4) < 0)
  1480. goto out_resource;
  1481. WRITE32(1);
  1482. }
  1483. if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
  1484. buflen -= (XDR_QUADLEN(fhp->fh_handle.fh_size) << 2) + 4;
  1485. if (buflen < 0)
  1486. goto out_resource;
  1487. WRITE32(fhp->fh_handle.fh_size);
  1488. WRITEMEM(&fhp->fh_handle.fh_base, fhp->fh_handle.fh_size);
  1489. }
  1490. if (bmval0 & FATTR4_WORD0_FILEID) {
  1491. if ((buflen -= 8) < 0)
  1492. goto out_resource;
  1493. WRITE64((u64) stat.ino);
  1494. }
  1495. if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
  1496. if ((buflen -= 8) < 0)
  1497. goto out_resource;
  1498. WRITE64((u64) statfs.f_ffree);
  1499. }
  1500. if (bmval0 & FATTR4_WORD0_FILES_FREE) {
  1501. if ((buflen -= 8) < 0)
  1502. goto out_resource;
  1503. WRITE64((u64) statfs.f_ffree);
  1504. }
  1505. if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
  1506. if ((buflen -= 8) < 0)
  1507. goto out_resource;
  1508. WRITE64((u64) statfs.f_files);
  1509. }
  1510. if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
  1511. status = nfsd4_encode_fs_locations(rqstp, exp, &p, &buflen);
  1512. if (status == nfserr_resource)
  1513. goto out_resource;
  1514. if (status)
  1515. goto out;
  1516. }
  1517. if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
  1518. if ((buflen -= 4) < 0)
  1519. goto out_resource;
  1520. WRITE32(1);
  1521. }
  1522. if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
  1523. if ((buflen -= 8) < 0)
  1524. goto out_resource;
  1525. WRITE64(~(u64)0);
  1526. }
  1527. if (bmval0 & FATTR4_WORD0_MAXLINK) {
  1528. if ((buflen -= 4) < 0)
  1529. goto out_resource;
  1530. WRITE32(255);
  1531. }
  1532. if (bmval0 & FATTR4_WORD0_MAXNAME) {
  1533. if ((buflen -= 4) < 0)
  1534. goto out_resource;
  1535. WRITE32(~(u32) 0);
  1536. }
  1537. if (bmval0 & FATTR4_WORD0_MAXREAD) {
  1538. if ((buflen -= 8) < 0)
  1539. goto out_resource;
  1540. WRITE64((u64) svc_max_payload(rqstp));
  1541. }
  1542. if (bmval0 & FATTR4_WORD0_MAXWRITE) {
  1543. if ((buflen -= 8) < 0)
  1544. goto out_resource;
  1545. WRITE64((u64) svc_max_payload(rqstp));
  1546. }
  1547. if (bmval1 & FATTR4_WORD1_MODE) {
  1548. if ((buflen -= 4) < 0)
  1549. goto out_resource;
  1550. WRITE32(stat.mode & S_IALLUGO);
  1551. }
  1552. if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
  1553. if ((buflen -= 4) < 0)
  1554. goto out_resource;
  1555. WRITE32(1);
  1556. }
  1557. if (bmval1 & FATTR4_WORD1_NUMLINKS) {
  1558. if ((buflen -= 4) < 0)
  1559. goto out_resource;
  1560. WRITE32(stat.nlink);
  1561. }
  1562. if (bmval1 & FATTR4_WORD1_OWNER) {
  1563. status = nfsd4_encode_user(rqstp, stat.uid, &p, &buflen);
  1564. if (status == nfserr_resource)
  1565. goto out_resource;
  1566. if (status)
  1567. goto out;
  1568. }
  1569. if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
  1570. status = nfsd4_encode_group(rqstp, stat.gid, &p, &buflen);
  1571. if (status == nfserr_resource)
  1572. goto out_resource;
  1573. if (status)
  1574. goto out;
  1575. }
  1576. if (bmval1 & FATTR4_WORD1_RAWDEV) {
  1577. if ((buflen -= 8) < 0)
  1578. goto out_resource;
  1579. WRITE32((u32) MAJOR(stat.rdev));
  1580. WRITE32((u32) MINOR(stat.rdev));
  1581. }
  1582. if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
  1583. if ((buflen -= 8) < 0)
  1584. goto out_resource;
  1585. dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
  1586. WRITE64(dummy64);
  1587. }
  1588. if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
  1589. if ((buflen -= 8) < 0)
  1590. goto out_resource;
  1591. dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
  1592. WRITE64(dummy64);
  1593. }
  1594. if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
  1595. if ((buflen -= 8) < 0)
  1596. goto out_resource;
  1597. dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
  1598. WRITE64(dummy64);
  1599. }
  1600. if (bmval1 & FATTR4_WORD1_SPACE_USED) {
  1601. if ((buflen -= 8) < 0)
  1602. goto out_resource;
  1603. dummy64 = (u64)stat.blocks << 9;
  1604. WRITE64(dummy64);
  1605. }
  1606. if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
  1607. if ((buflen -= 12) < 0)
  1608. goto out_resource;
  1609. WRITE32(0);
  1610. WRITE32(stat.atime.tv_sec);
  1611. WRITE32(stat.atime.tv_nsec);
  1612. }
  1613. if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
  1614. if ((buflen -= 12) < 0)
  1615. goto out_resource;
  1616. WRITE32(0);
  1617. WRITE32(1);
  1618. WRITE32(0);
  1619. }
  1620. if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
  1621. if ((buflen -= 12) < 0)
  1622. goto out_resource;
  1623. WRITE32(0);
  1624. WRITE32(stat.ctime.tv_sec);
  1625. WRITE32(stat.ctime.tv_nsec);
  1626. }
  1627. if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
  1628. if ((buflen -= 12) < 0)
  1629. goto out_resource;
  1630. WRITE32(0);
  1631. WRITE32(stat.mtime.tv_sec);
  1632. WRITE32(stat.mtime.tv_nsec);
  1633. }
  1634. if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
  1635. struct dentry *mnt_pnt, *mnt_root;
  1636. if ((buflen -= 8) < 0)
  1637. goto out_resource;
  1638. mnt_root = exp->ex_mnt->mnt_root;
  1639. if (mnt_root->d_inode == dentry->d_inode) {
  1640. mnt_pnt = exp->ex_mnt->mnt_mountpoint;
  1641. WRITE64((u64) mnt_pnt->d_inode->i_ino);
  1642. } else
  1643. WRITE64((u64) stat.ino);
  1644. }
  1645. *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
  1646. *countp = p - buffer;
  1647. status = nfs_ok;
  1648. out:
  1649. nfs4_acl_free(acl);
  1650. if (fhp == &tempfh)
  1651. fh_put(&tempfh);
  1652. return status;
  1653. out_nfserr:
  1654. status = nfserrno(status);
  1655. goto out;
  1656. out_resource:
  1657. *countp = 0;
  1658. status = nfserr_resource;
  1659. goto out;
  1660. out_serverfault:
  1661. status = nfserr_serverfault;
  1662. goto out;
  1663. }
  1664. static int
  1665. nfsd4_encode_dirent_fattr(struct nfsd4_readdir *cd,
  1666. const char *name, int namlen, u32 *p, int *buflen)
  1667. {
  1668. struct svc_export *exp = cd->rd_fhp->fh_export;
  1669. struct dentry *dentry;
  1670. int nfserr;
  1671. dentry = lookup_one_len(name, cd->rd_fhp->fh_dentry, namlen);
  1672. if (IS_ERR(dentry))
  1673. return nfserrno(PTR_ERR(dentry));
  1674. exp_get(exp);
  1675. if (d_mountpoint(dentry)) {
  1676. if (nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp)) {
  1677. /*
  1678. * -EAGAIN is the only error returned from
  1679. * nfsd_cross_mnt() and it indicates that an
  1680. * up-call has been initiated to fill in the export
  1681. * options on exp. When the answer comes back,
  1682. * this call will be retried.
  1683. */
  1684. nfserr = nfserr_dropit;
  1685. goto out_put;
  1686. }
  1687. }
  1688. nfserr = nfsd4_encode_fattr(NULL, exp, dentry, p, buflen, cd->rd_bmval,
  1689. cd->rd_rqstp);
  1690. out_put:
  1691. dput(dentry);
  1692. exp_put(exp);
  1693. return nfserr;
  1694. }
  1695. static u32 *
  1696. nfsd4_encode_rdattr_error(u32 *p, int buflen, int nfserr)
  1697. {
  1698. u32 *attrlenp;
  1699. if (buflen < 6)
  1700. return NULL;
  1701. *p++ = htonl(2);
  1702. *p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
  1703. *p++ = htonl(0); /* bmval1 */
  1704. attrlenp = p++;
  1705. *p++ = nfserr; /* no htonl */
  1706. *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
  1707. return p;
  1708. }
  1709. static int
  1710. nfsd4_encode_dirent(struct readdir_cd *ccd, const char *name, int namlen,
  1711. loff_t offset, ino_t ino, unsigned int d_type)
  1712. {
  1713. struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
  1714. int buflen;
  1715. u32 *p = cd->buffer;
  1716. int nfserr = nfserr_toosmall;
  1717. /* In nfsv4, "." and ".." never make it onto the wire.. */
  1718. if (name && isdotent(name, namlen)) {
  1719. cd->common.err = nfs_ok;
  1720. return 0;
  1721. }
  1722. if (cd->offset)
  1723. xdr_encode_hyper(cd->offset, (u64) offset);
  1724. buflen = cd->buflen - 4 - XDR_QUADLEN(namlen);
  1725. if (buflen < 0)
  1726. goto fail;
  1727. *p++ = xdr_one; /* mark entry present */
  1728. cd->offset = p; /* remember pointer */
  1729. p = xdr_encode_hyper(p, NFS_OFFSET_MAX); /* offset of next entry */
  1730. p = xdr_encode_array(p, name, namlen); /* name length & name */
  1731. nfserr = nfsd4_encode_dirent_fattr(cd, name, namlen, p, &buflen);
  1732. switch (nfserr) {
  1733. case nfs_ok:
  1734. p += buflen;
  1735. break;
  1736. case nfserr_resource:
  1737. nfserr = nfserr_toosmall;
  1738. goto fail;
  1739. case nfserr_dropit:
  1740. goto fail;
  1741. default:
  1742. /*
  1743. * If the client requested the RDATTR_ERROR attribute,
  1744. * we stuff the error code into this attribute
  1745. * and continue. If this attribute was not requested,
  1746. * then in accordance with the spec, we fail the
  1747. * entire READDIR operation(!)
  1748. */
  1749. if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
  1750. goto fail;
  1751. p = nfsd4_encode_rdattr_error(p, buflen, nfserr);
  1752. if (p == NULL) {
  1753. nfserr = nfserr_toosmall;
  1754. goto fail;
  1755. }
  1756. }
  1757. cd->buflen -= (p - cd->buffer);
  1758. cd->buffer = p;
  1759. cd->common.err = nfs_ok;
  1760. return 0;
  1761. fail:
  1762. cd->common.err = nfserr;
  1763. return -EINVAL;
  1764. }
  1765. static void
  1766. nfsd4_encode_access(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_access *access)
  1767. {
  1768. ENCODE_HEAD;
  1769. if (!nfserr) {
  1770. RESERVE_SPACE(8);
  1771. WRITE32(access->ac_supported);
  1772. WRITE32(access->ac_resp_access);
  1773. ADJUST_ARGS();
  1774. }
  1775. }
  1776. static void
  1777. nfsd4_encode_close(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_close *close)
  1778. {
  1779. ENCODE_SEQID_OP_HEAD;
  1780. if (!nfserr) {
  1781. RESERVE_SPACE(sizeof(stateid_t));
  1782. WRITE32(close->cl_stateid.si_generation);
  1783. WRITEMEM(&close->cl_stateid.si_opaque, sizeof(stateid_opaque_t));
  1784. ADJUST_ARGS();
  1785. }
  1786. ENCODE_SEQID_OP_TAIL(close->cl_stateowner);
  1787. }
  1788. static void
  1789. nfsd4_encode_commit(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_commit *commit)
  1790. {
  1791. ENCODE_HEAD;
  1792. if (!nfserr) {
  1793. RESERVE_SPACE(8);
  1794. WRITEMEM(commit->co_verf.data, 8);
  1795. ADJUST_ARGS();
  1796. }
  1797. }
  1798. static void
  1799. nfsd4_encode_create(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_create *create)
  1800. {
  1801. ENCODE_HEAD;
  1802. if (!nfserr) {
  1803. RESERVE_SPACE(32);
  1804. WRITECINFO(create->cr_cinfo);
  1805. WRITE32(2);
  1806. WRITE32(create->cr_bmval[0]);
  1807. WRITE32(create->cr_bmval[1]);
  1808. ADJUST_ARGS();
  1809. }
  1810. }
  1811. static int
  1812. nfsd4_encode_getattr(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_getattr *getattr)
  1813. {
  1814. struct svc_fh *fhp = getattr->ga_fhp;
  1815. int buflen;
  1816. if (nfserr)
  1817. return nfserr;
  1818. buflen = resp->end - resp->p - (COMPOUND_ERR_SLACK_SPACE >> 2);
  1819. nfserr = nfsd4_encode_fattr(fhp, fhp->fh_export, fhp->fh_dentry,
  1820. resp->p, &buflen, getattr->ga_bmval,
  1821. resp->rqstp);
  1822. if (!nfserr)
  1823. resp->p += buflen;
  1824. return nfserr;
  1825. }
  1826. static void
  1827. nfsd4_encode_getfh(struct nfsd4_compoundres *resp, int nfserr, struct svc_fh *fhp)
  1828. {
  1829. unsigned int len;
  1830. ENCODE_HEAD;
  1831. if (!nfserr) {
  1832. len = fhp->fh_handle.fh_size;
  1833. RESERVE_SPACE(len + 4);
  1834. WRITE32(len);
  1835. WRITEMEM(&fhp->fh_handle.fh_base, len);
  1836. ADJUST_ARGS();
  1837. }
  1838. }
  1839. /*
  1840. * Including all fields other than the name, a LOCK4denied structure requires
  1841. * 8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
  1842. */
  1843. static void
  1844. nfsd4_encode_lock_denied(struct nfsd4_compoundres *resp, struct nfsd4_lock_denied *ld)
  1845. {
  1846. ENCODE_HEAD;
  1847. RESERVE_SPACE(32 + XDR_LEN(ld->ld_sop ? ld->ld_sop->so_owner.len : 0));
  1848. WRITE64(ld->ld_start);
  1849. WRITE64(ld->ld_length);
  1850. WRITE32(ld->ld_type);
  1851. if (ld->ld_sop) {
  1852. WRITEMEM(&ld->ld_clientid, 8);
  1853. WRITE32(ld->ld_sop->so_owner.len);
  1854. WRITEMEM(ld->ld_sop->so_owner.data, ld->ld_sop->so_owner.len);
  1855. kref_put(&ld->ld_sop->so_ref, nfs4_free_stateowner);
  1856. } else { /* non - nfsv4 lock in conflict, no clientid nor owner */
  1857. WRITE64((u64)0); /* clientid */
  1858. WRITE32(0); /* length of owner name */
  1859. }
  1860. ADJUST_ARGS();
  1861. }
  1862. static void
  1863. nfsd4_encode_lock(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_lock *lock)
  1864. {
  1865. ENCODE_SEQID_OP_HEAD;
  1866. if (!nfserr) {
  1867. RESERVE_SPACE(4 + sizeof(stateid_t));
  1868. WRITE32(lock->lk_resp_stateid.si_generation);
  1869. WRITEMEM(&lock->lk_resp_stateid.si_opaque, sizeof(stateid_opaque_t));
  1870. ADJUST_ARGS();
  1871. } else if (nfserr == nfserr_denied)
  1872. nfsd4_encode_lock_denied(resp, &lock->lk_denied);
  1873. ENCODE_SEQID_OP_TAIL(lock->lk_replay_owner);
  1874. }
  1875. static void
  1876. nfsd4_encode_lockt(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_lockt *lockt)
  1877. {
  1878. if (nfserr == nfserr_denied)
  1879. nfsd4_encode_lock_denied(resp, &lockt->lt_denied);
  1880. }
  1881. static void
  1882. nfsd4_encode_locku(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_locku *locku)
  1883. {
  1884. ENCODE_SEQID_OP_HEAD;
  1885. if (!nfserr) {
  1886. RESERVE_SPACE(sizeof(stateid_t));
  1887. WRITE32(locku->lu_stateid.si_generation);
  1888. WRITEMEM(&locku->lu_stateid.si_opaque, sizeof(stateid_opaque_t));
  1889. ADJUST_ARGS();
  1890. }
  1891. ENCODE_SEQID_OP_TAIL(locku->lu_stateowner);
  1892. }
  1893. static void
  1894. nfsd4_encode_link(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_link *link)
  1895. {
  1896. ENCODE_HEAD;
  1897. if (!nfserr) {
  1898. RESERVE_SPACE(20);
  1899. WRITECINFO(link->li_cinfo);
  1900. ADJUST_ARGS();
  1901. }
  1902. }
  1903. static void
  1904. nfsd4_encode_open(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_open *open)
  1905. {
  1906. ENCODE_SEQID_OP_HEAD;
  1907. if (nfserr)
  1908. goto out;
  1909. RESERVE_SPACE(36 + sizeof(stateid_t));
  1910. WRITE32(open->op_stateid.si_generation);
  1911. WRITEMEM(&open->op_stateid.si_opaque, sizeof(stateid_opaque_t));
  1912. WRITECINFO(open->op_cinfo);
  1913. WRITE32(open->op_rflags);
  1914. WRITE32(2);
  1915. WRITE32(open->op_bmval[0]);
  1916. WRITE32(open->op_bmval[1]);
  1917. WRITE32(open->op_delegate_type);
  1918. ADJUST_ARGS();
  1919. switch (open->op_delegate_type) {
  1920. case NFS4_OPEN_DELEGATE_NONE:
  1921. break;
  1922. case NFS4_OPEN_DELEGATE_READ:
  1923. RESERVE_SPACE(20 + sizeof(stateid_t));
  1924. WRITEMEM(&open->op_delegate_stateid, sizeof(stateid_t));
  1925. WRITE32(open->op_recall);
  1926. /*
  1927. * TODO: ACE's in delegations
  1928. */
  1929. WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
  1930. WRITE32(0);
  1931. WRITE32(0);
  1932. WRITE32(0); /* XXX: is NULL principal ok? */
  1933. ADJUST_ARGS();
  1934. break;
  1935. case NFS4_OPEN_DELEGATE_WRITE:
  1936. RESERVE_SPACE(32 + sizeof(stateid_t));
  1937. WRITEMEM(&open->op_delegate_stateid, sizeof(stateid_t));
  1938. WRITE32(0);
  1939. /*
  1940. * TODO: space_limit's in delegations
  1941. */
  1942. WRITE32(NFS4_LIMIT_SIZE);
  1943. WRITE32(~(u32)0);
  1944. WRITE32(~(u32)0);
  1945. /*
  1946. * TODO: ACE's in delegations
  1947. */
  1948. WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
  1949. WRITE32(0);
  1950. WRITE32(0);
  1951. WRITE32(0); /* XXX: is NULL principal ok? */
  1952. ADJUST_ARGS();
  1953. break;
  1954. default:
  1955. BUG();
  1956. }
  1957. /* XXX save filehandle here */
  1958. out:
  1959. ENCODE_SEQID_OP_TAIL(open->op_stateowner);
  1960. }
  1961. static void
  1962. nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_open_confirm *oc)
  1963. {
  1964. ENCODE_SEQID_OP_HEAD;
  1965. if (!nfserr) {
  1966. RESERVE_SPACE(sizeof(stateid_t));
  1967. WRITE32(oc->oc_resp_stateid.si_generation);
  1968. WRITEMEM(&oc->oc_resp_stateid.si_opaque, sizeof(stateid_opaque_t));
  1969. ADJUST_ARGS();
  1970. }
  1971. ENCODE_SEQID_OP_TAIL(oc->oc_stateowner);
  1972. }
  1973. static void
  1974. nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_open_downgrade *od)
  1975. {
  1976. ENCODE_SEQID_OP_HEAD;
  1977. if (!nfserr) {
  1978. RESERVE_SPACE(sizeof(stateid_t));
  1979. WRITE32(od->od_stateid.si_generation);
  1980. WRITEMEM(&od->od_stateid.si_opaque, sizeof(stateid_opaque_t));
  1981. ADJUST_ARGS();
  1982. }
  1983. ENCODE_SEQID_OP_TAIL(od->od_stateowner);
  1984. }
  1985. static int
  1986. nfsd4_encode_read(struct nfsd4_compoundres *resp, int nfserr,
  1987. struct nfsd4_read *read)
  1988. {
  1989. u32 eof;
  1990. int v, pn;
  1991. unsigned long maxcount;
  1992. long len;
  1993. ENCODE_HEAD;
  1994. if (nfserr)
  1995. return nfserr;
  1996. if (resp->xbuf->page_len)
  1997. return nfserr_resource;
  1998. RESERVE_SPACE(8); /* eof flag and byte count */
  1999. maxcount = svc_max_payload(resp->rqstp);
  2000. if (maxcount > read->rd_length)
  2001. maxcount = read->rd_length;
  2002. len = maxcount;
  2003. v = 0;
  2004. while (len > 0) {
  2005. pn = resp->rqstp->rq_resused++;
  2006. resp->rqstp->rq_vec[v].iov_base =
  2007. page_address(resp->rqstp->rq_respages[pn]);
  2008. resp->rqstp->rq_vec[v].iov_len =
  2009. len < PAGE_SIZE ? len : PAGE_SIZE;
  2010. v++;
  2011. len -= PAGE_SIZE;
  2012. }
  2013. read->rd_vlen = v;
  2014. nfserr = nfsd_read(read->rd_rqstp, read->rd_fhp, read->rd_filp,
  2015. read->rd_offset, resp->rqstp->rq_vec, read->rd_vlen,
  2016. &maxcount);
  2017. if (nfserr == nfserr_symlink)
  2018. nfserr = nfserr_inval;
  2019. if (nfserr)
  2020. return nfserr;
  2021. eof = (read->rd_offset + maxcount >=
  2022. read->rd_fhp->fh_dentry->d_inode->i_size);
  2023. WRITE32(eof);
  2024. WRITE32(maxcount);
  2025. ADJUST_ARGS();
  2026. resp->xbuf->head[0].iov_len = (char*)p
  2027. - (char*)resp->xbuf->head[0].iov_base;
  2028. resp->xbuf->page_len = maxcount;
  2029. /* Use rest of head for padding and remaining ops: */
  2030. resp->xbuf->tail[0].iov_base = p;
  2031. resp->xbuf->tail[0].iov_len = 0;
  2032. if (maxcount&3) {
  2033. RESERVE_SPACE(4);
  2034. WRITE32(0);
  2035. resp->xbuf->tail[0].iov_base += maxcount&3;
  2036. resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
  2037. ADJUST_ARGS();
  2038. }
  2039. return 0;
  2040. }
  2041. static int
  2042. nfsd4_encode_readlink(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_readlink *readlink)
  2043. {
  2044. int maxcount;
  2045. char *page;
  2046. ENCODE_HEAD;
  2047. if (nfserr)
  2048. return nfserr;
  2049. if (resp->xbuf->page_len)
  2050. return nfserr_resource;
  2051. page = page_address(resp->rqstp->rq_respages[resp->rqstp->rq_resused++]);
  2052. maxcount = PAGE_SIZE;
  2053. RESERVE_SPACE(4);
  2054. /*
  2055. * XXX: By default, the ->readlink() VFS op will truncate symlinks
  2056. * if they would overflow the buffer. Is this kosher in NFSv4? If
  2057. * not, one easy fix is: if ->readlink() precisely fills the buffer,
  2058. * assume that truncation occurred, and return NFS4ERR_RESOURCE.
  2059. */
  2060. nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp, page, &maxcount);
  2061. if (nfserr == nfserr_isdir)
  2062. return nfserr_inval;
  2063. if (nfserr)
  2064. return nfserr;
  2065. WRITE32(maxcount);
  2066. ADJUST_ARGS();
  2067. resp->xbuf->head[0].iov_len = (char*)p
  2068. - (char*)resp->xbuf->head[0].iov_base;
  2069. resp->xbuf->page_len = maxcount;
  2070. /* Use rest of head for padding and remaining ops: */
  2071. resp->xbuf->tail[0].iov_base = p;
  2072. resp->xbuf->tail[0].iov_len = 0;
  2073. if (maxcount&3) {
  2074. RESERVE_SPACE(4);
  2075. WRITE32(0);
  2076. resp->xbuf->tail[0].iov_base += maxcount&3;
  2077. resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
  2078. ADJUST_ARGS();
  2079. }
  2080. return 0;
  2081. }
  2082. static int
  2083. nfsd4_encode_readdir(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_readdir *readdir)
  2084. {
  2085. int maxcount;
  2086. loff_t offset;
  2087. u32 *page, *savep, *tailbase;
  2088. ENCODE_HEAD;
  2089. if (nfserr)
  2090. return nfserr;
  2091. if (resp->xbuf->page_len)
  2092. return nfserr_resource;
  2093. RESERVE_SPACE(8); /* verifier */
  2094. savep = p;
  2095. /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
  2096. WRITE32(0);
  2097. WRITE32(0);
  2098. ADJUST_ARGS();
  2099. resp->xbuf->head[0].iov_len = ((char*)resp->p) - (char*)resp->xbuf->head[0].iov_base;
  2100. tailbase = p;
  2101. maxcount = PAGE_SIZE;
  2102. if (maxcount > readdir->rd_maxcount)
  2103. maxcount = readdir->rd_maxcount;
  2104. /*
  2105. * Convert from bytes to words, account for the two words already
  2106. * written, make sure to leave two words at the end for the next
  2107. * pointer and eof field.
  2108. */
  2109. maxcount = (maxcount >> 2) - 4;
  2110. if (maxcount < 0) {
  2111. nfserr = nfserr_toosmall;
  2112. goto err_no_verf;
  2113. }
  2114. page = page_address(resp->rqstp->rq_respages[resp->rqstp->rq_resused++]);
  2115. readdir->common.err = 0;
  2116. readdir->buflen = maxcount;
  2117. readdir->buffer = page;
  2118. readdir->offset = NULL;
  2119. offset = readdir->rd_cookie;
  2120. nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
  2121. &offset,
  2122. &readdir->common, nfsd4_encode_dirent);
  2123. if (nfserr == nfs_ok &&
  2124. readdir->common.err == nfserr_toosmall &&
  2125. readdir->buffer == page)
  2126. nfserr = nfserr_toosmall;
  2127. if (nfserr == nfserr_symlink)
  2128. nfserr = nfserr_notdir;
  2129. if (nfserr)
  2130. goto err_no_verf;
  2131. if (readdir->offset)
  2132. xdr_encode_hyper(readdir->offset, offset);
  2133. p = readdir->buffer;
  2134. *p++ = 0; /* no more entries */
  2135. *p++ = htonl(readdir->common.err == nfserr_eof);
  2136. resp->xbuf->page_len = ((char*)p) - (char*)page_address(
  2137. resp->rqstp->rq_respages[resp->rqstp->rq_resused-1]);
  2138. /* Use rest of head for padding and remaining ops: */
  2139. resp->xbuf->tail[0].iov_base = tailbase;
  2140. resp->xbuf->tail[0].iov_len = 0;
  2141. resp->p = resp->xbuf->tail[0].iov_base;
  2142. resp->end = resp->p + (PAGE_SIZE - resp->xbuf->head[0].iov_len)/4;
  2143. return 0;
  2144. err_no_verf:
  2145. p = savep;
  2146. ADJUST_ARGS();
  2147. return nfserr;
  2148. }
  2149. static void
  2150. nfsd4_encode_remove(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_remove *remove)
  2151. {
  2152. ENCODE_HEAD;
  2153. if (!nfserr) {
  2154. RESERVE_SPACE(20);
  2155. WRITECINFO(remove->rm_cinfo);
  2156. ADJUST_ARGS();
  2157. }
  2158. }
  2159. static void
  2160. nfsd4_encode_rename(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_rename *rename)
  2161. {
  2162. ENCODE_HEAD;
  2163. if (!nfserr) {
  2164. RESERVE_SPACE(40);
  2165. WRITECINFO(rename->rn_sinfo);
  2166. WRITECINFO(rename->rn_tinfo);
  2167. ADJUST_ARGS();
  2168. }
  2169. }
  2170. /*
  2171. * The SETATTR encode routine is special -- it always encodes a bitmap,
  2172. * regardless of the error status.
  2173. */
  2174. static void
  2175. nfsd4_encode_setattr(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_setattr *setattr)
  2176. {
  2177. ENCODE_HEAD;
  2178. RESERVE_SPACE(12);
  2179. if (nfserr) {
  2180. WRITE32(2);
  2181. WRITE32(0);
  2182. WRITE32(0);
  2183. }
  2184. else {
  2185. WRITE32(2);
  2186. WRITE32(setattr->sa_bmval[0]);
  2187. WRITE32(setattr->sa_bmval[1]);
  2188. }
  2189. ADJUST_ARGS();
  2190. }
  2191. static void
  2192. nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_setclientid *scd)
  2193. {
  2194. ENCODE_HEAD;
  2195. if (!nfserr) {
  2196. RESERVE_SPACE(8 + sizeof(nfs4_verifier));
  2197. WRITEMEM(&scd->se_clientid, 8);
  2198. WRITEMEM(&scd->se_confirm, sizeof(nfs4_verifier));
  2199. ADJUST_ARGS();
  2200. }
  2201. else if (nfserr == nfserr_clid_inuse) {
  2202. RESERVE_SPACE(8);
  2203. WRITE32(0);
  2204. WRITE32(0);
  2205. ADJUST_ARGS();
  2206. }
  2207. }
  2208. static void
  2209. nfsd4_encode_write(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_write *write)
  2210. {
  2211. ENCODE_HEAD;
  2212. if (!nfserr) {
  2213. RESERVE_SPACE(16);
  2214. WRITE32(write->wr_bytes_written);
  2215. WRITE32(write->wr_how_written);
  2216. WRITEMEM(write->wr_verifier.data, 8);
  2217. ADJUST_ARGS();
  2218. }
  2219. }
  2220. void
  2221. nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
  2222. {
  2223. u32 *statp;
  2224. ENCODE_HEAD;
  2225. RESERVE_SPACE(8);
  2226. WRITE32(op->opnum);
  2227. statp = p++; /* to be backfilled at the end */
  2228. ADJUST_ARGS();
  2229. switch (op->opnum) {
  2230. case OP_ACCESS:
  2231. nfsd4_encode_access(resp, op->status, &op->u.access);
  2232. break;
  2233. case OP_CLOSE:
  2234. nfsd4_encode_close(resp, op->status, &op->u.close);
  2235. break;
  2236. case OP_COMMIT:
  2237. nfsd4_encode_commit(resp, op->status, &op->u.commit);
  2238. break;
  2239. case OP_CREATE:
  2240. nfsd4_encode_create(resp, op->status, &op->u.create);
  2241. break;
  2242. case OP_DELEGRETURN:
  2243. break;
  2244. case OP_GETATTR:
  2245. op->status = nfsd4_encode_getattr(resp, op->status, &op->u.getattr);
  2246. break;
  2247. case OP_GETFH:
  2248. nfsd4_encode_getfh(resp, op->status, op->u.getfh);
  2249. break;
  2250. case OP_LINK:
  2251. nfsd4_encode_link(resp, op->status, &op->u.link);
  2252. break;
  2253. case OP_LOCK:
  2254. nfsd4_encode_lock(resp, op->status, &op->u.lock);
  2255. break;
  2256. case OP_LOCKT:
  2257. nfsd4_encode_lockt(resp, op->status, &op->u.lockt);
  2258. break;
  2259. case OP_LOCKU:
  2260. nfsd4_encode_locku(resp, op->status, &op->u.locku);
  2261. break;
  2262. case OP_LOOKUP:
  2263. break;
  2264. case OP_LOOKUPP:
  2265. break;
  2266. case OP_NVERIFY:
  2267. break;
  2268. case OP_OPEN:
  2269. nfsd4_encode_open(resp, op->status, &op->u.open);
  2270. break;
  2271. case OP_OPEN_CONFIRM:
  2272. nfsd4_encode_open_confirm(resp, op->status, &op->u.open_confirm);
  2273. break;
  2274. case OP_OPEN_DOWNGRADE:
  2275. nfsd4_encode_open_downgrade(resp, op->status, &op->u.open_downgrade);
  2276. break;
  2277. case OP_PUTFH:
  2278. break;
  2279. case OP_PUTROOTFH:
  2280. break;
  2281. case OP_READ:
  2282. op->status = nfsd4_encode_read(resp, op->status, &op->u.read);
  2283. break;
  2284. case OP_READDIR:
  2285. op->status = nfsd4_encode_readdir(resp, op->status, &op->u.readdir);
  2286. break;
  2287. case OP_READLINK:
  2288. op->status = nfsd4_encode_readlink(resp, op->status, &op->u.readlink);
  2289. break;
  2290. case OP_REMOVE:
  2291. nfsd4_encode_remove(resp, op->status, &op->u.remove);
  2292. break;
  2293. case OP_RENAME:
  2294. nfsd4_encode_rename(resp, op->status, &op->u.rename);
  2295. break;
  2296. case OP_RENEW:
  2297. break;
  2298. case OP_RESTOREFH:
  2299. break;
  2300. case OP_SAVEFH:
  2301. break;
  2302. case OP_SETATTR:
  2303. nfsd4_encode_setattr(resp, op->status, &op->u.setattr);
  2304. break;
  2305. case OP_SETCLIENTID:
  2306. nfsd4_encode_setclientid(resp, op->status, &op->u.setclientid);
  2307. break;
  2308. case OP_SETCLIENTID_CONFIRM:
  2309. break;
  2310. case OP_VERIFY:
  2311. break;
  2312. case OP_WRITE:
  2313. nfsd4_encode_write(resp, op->status, &op->u.write);
  2314. break;
  2315. case OP_RELEASE_LOCKOWNER:
  2316. break;
  2317. default:
  2318. break;
  2319. }
  2320. /*
  2321. * Note: We write the status directly, instead of using WRITE32(),
  2322. * since it is already in network byte order.
  2323. */
  2324. *statp = op->status;
  2325. }
  2326. /*
  2327. * Encode the reply stored in the stateowner reply cache
  2328. *
  2329. * XDR note: do not encode rp->rp_buflen: the buffer contains the
  2330. * previously sent already encoded operation.
  2331. *
  2332. * called with nfs4_lock_state() held
  2333. */
  2334. void
  2335. nfsd4_encode_replay(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
  2336. {
  2337. ENCODE_HEAD;
  2338. struct nfs4_replay *rp = op->replay;
  2339. BUG_ON(!rp);
  2340. RESERVE_SPACE(8);
  2341. WRITE32(op->opnum);
  2342. *p++ = rp->rp_status; /* already xdr'ed */
  2343. ADJUST_ARGS();
  2344. RESERVE_SPACE(rp->rp_buflen);
  2345. WRITEMEM(rp->rp_buf, rp->rp_buflen);
  2346. ADJUST_ARGS();
  2347. }
  2348. /*
  2349. * END OF "GENERIC" ENCODE ROUTINES.
  2350. */
  2351. int
  2352. nfs4svc_encode_voidres(struct svc_rqst *rqstp, u32 *p, void *dummy)
  2353. {
  2354. return xdr_ressize_check(rqstp, p);
  2355. }
  2356. void nfsd4_release_compoundargs(struct nfsd4_compoundargs *args)
  2357. {
  2358. if (args->ops != args->iops) {
  2359. kfree(args->ops);
  2360. args->ops = args->iops;
  2361. }
  2362. kfree(args->tmpp);
  2363. args->tmpp = NULL;
  2364. while (args->to_free) {
  2365. struct tmpbuf *tb = args->to_free;
  2366. args->to_free = tb->next;
  2367. tb->release(tb->buf);
  2368. kfree(tb);
  2369. }
  2370. }
  2371. int
  2372. nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, u32 *p, struct nfsd4_compoundargs *args)
  2373. {
  2374. int status;
  2375. args->p = p;
  2376. args->end = rqstp->rq_arg.head[0].iov_base + rqstp->rq_arg.head[0].iov_len;
  2377. args->pagelist = rqstp->rq_arg.pages;
  2378. args->pagelen = rqstp->rq_arg.page_len;
  2379. args->tmpp = NULL;
  2380. args->to_free = NULL;
  2381. args->ops = args->iops;
  2382. args->rqstp = rqstp;
  2383. status = nfsd4_decode_compound(args);
  2384. if (status) {
  2385. nfsd4_release_compoundargs(args);
  2386. }
  2387. return !status;
  2388. }
  2389. int
  2390. nfs4svc_encode_compoundres(struct svc_rqst *rqstp, u32 *p, struct nfsd4_compoundres *resp)
  2391. {
  2392. /*
  2393. * All that remains is to write the tag and operation count...
  2394. */
  2395. struct kvec *iov;
  2396. p = resp->tagp;
  2397. *p++ = htonl(resp->taglen);
  2398. memcpy(p, resp->tag, resp->taglen);
  2399. p += XDR_QUADLEN(resp->taglen);
  2400. *p++ = htonl(resp->opcnt);
  2401. if (rqstp->rq_res.page_len)
  2402. iov = &rqstp->rq_res.tail[0];
  2403. else
  2404. iov = &rqstp->rq_res.head[0];
  2405. iov->iov_len = ((char*)resp->p) - (char*)iov->iov_base;
  2406. BUG_ON(iov->iov_len > PAGE_SIZE);
  2407. return 1;
  2408. }
  2409. /*
  2410. * Local variables:
  2411. * c-basic-offset: 8
  2412. * End:
  2413. */