nfs4xdr.c 62 KB

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