nfs4xdr.c 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653
  1. /*
  2. * Server-side XDR for NFSv4
  3. *
  4. * Copyright (c) 2002 The Regents of the University of Michigan.
  5. * All rights reserved.
  6. *
  7. * Kendrick Smith <kmsmith@umich.edu>
  8. * Andy Adamson <andros@umich.edu>
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * 3. Neither the name of the University nor the names of its
  20. * contributors may be used to endorse or promote products derived
  21. * from this software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  24. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  25. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  27. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  30. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  31. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  32. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  33. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. * TODO: Neil Brown made the following observation: We currently
  36. * initially reserve NFSD_BUFSIZE space on the transmit queue and
  37. * never release any of that until the request is complete.
  38. * It would be good to calculate a new maximum response size while
  39. * decoding the COMPOUND, and call svc_reserve with this number
  40. * at the end of nfs4svc_decode_compoundargs.
  41. */
  42. #include <linux/slab.h>
  43. #include <linux/namei.h>
  44. #include <linux/statfs.h>
  45. #include <linux/utsname.h>
  46. #include <linux/pagemap.h>
  47. #include <linux/sunrpc/svcauth_gss.h>
  48. #include "idmap.h"
  49. #include "acl.h"
  50. #include "xdr4.h"
  51. #include "vfs.h"
  52. #include "state.h"
  53. #include "cache.h"
  54. #include "netns.h"
  55. #define NFSDDBG_FACILITY NFSDDBG_XDR
  56. /*
  57. * As per referral draft, the fsid for a referral MUST be different from the fsid of the containing
  58. * directory in order to indicate to the client that a filesystem boundary is present
  59. * We use a fixed fsid for a referral
  60. */
  61. #define NFS4_REFERRAL_FSID_MAJOR 0x8000000ULL
  62. #define NFS4_REFERRAL_FSID_MINOR 0x8000000ULL
  63. static __be32
  64. check_filename(char *str, int len)
  65. {
  66. int i;
  67. if (len == 0)
  68. return nfserr_inval;
  69. if (isdotent(str, len))
  70. return nfserr_badname;
  71. for (i = 0; i < len; i++)
  72. if (str[i] == '/')
  73. return nfserr_badname;
  74. return 0;
  75. }
  76. #define DECODE_HEAD \
  77. __be32 *p; \
  78. __be32 status
  79. #define DECODE_TAIL \
  80. status = 0; \
  81. out: \
  82. return status; \
  83. xdr_error: \
  84. dprintk("NFSD: xdr error (%s:%d)\n", \
  85. __FILE__, __LINE__); \
  86. status = nfserr_bad_xdr; \
  87. goto out
  88. #define READ32(x) (x) = ntohl(*p++)
  89. #define READ64(x) do { \
  90. (x) = (u64)ntohl(*p++) << 32; \
  91. (x) |= ntohl(*p++); \
  92. } while (0)
  93. #define READTIME(x) do { \
  94. p++; \
  95. (x) = ntohl(*p++); \
  96. p++; \
  97. } while (0)
  98. #define READMEM(x,nbytes) do { \
  99. x = (char *)p; \
  100. p += XDR_QUADLEN(nbytes); \
  101. } while (0)
  102. #define SAVEMEM(x,nbytes) do { \
  103. if (!(x = (p==argp->tmp || p == argp->tmpp) ? \
  104. savemem(argp, p, nbytes) : \
  105. (char *)p)) { \
  106. dprintk("NFSD: xdr error (%s:%d)\n", \
  107. __FILE__, __LINE__); \
  108. goto xdr_error; \
  109. } \
  110. p += XDR_QUADLEN(nbytes); \
  111. } while (0)
  112. #define COPYMEM(x,nbytes) do { \
  113. memcpy((x), p, nbytes); \
  114. p += XDR_QUADLEN(nbytes); \
  115. } while (0)
  116. /* READ_BUF, read_buf(): nbytes must be <= PAGE_SIZE */
  117. #define READ_BUF(nbytes) do { \
  118. if (nbytes <= (u32)((char *)argp->end - (char *)argp->p)) { \
  119. p = argp->p; \
  120. argp->p += XDR_QUADLEN(nbytes); \
  121. } else if (!(p = read_buf(argp, nbytes))) { \
  122. dprintk("NFSD: xdr error (%s:%d)\n", \
  123. __FILE__, __LINE__); \
  124. goto xdr_error; \
  125. } \
  126. } while (0)
  127. static __be32 *read_buf(struct nfsd4_compoundargs *argp, u32 nbytes)
  128. {
  129. /* We want more bytes than seem to be available.
  130. * Maybe we need a new page, maybe we have just run out
  131. */
  132. unsigned int avail = (char *)argp->end - (char *)argp->p;
  133. __be32 *p;
  134. if (avail + argp->pagelen < nbytes)
  135. return NULL;
  136. if (avail + PAGE_SIZE < nbytes) /* need more than a page !! */
  137. return NULL;
  138. /* ok, we can do it with the current plus the next page */
  139. if (nbytes <= sizeof(argp->tmp))
  140. p = argp->tmp;
  141. else {
  142. kfree(argp->tmpp);
  143. p = argp->tmpp = kmalloc(nbytes, GFP_KERNEL);
  144. if (!p)
  145. return NULL;
  146. }
  147. /*
  148. * The following memcpy is safe because read_buf is always
  149. * called with nbytes > avail, and the two cases above both
  150. * guarantee p points to at least nbytes bytes.
  151. */
  152. memcpy(p, argp->p, avail);
  153. /* step to next page */
  154. argp->p = page_address(argp->pagelist[0]);
  155. argp->pagelist++;
  156. if (argp->pagelen < PAGE_SIZE) {
  157. argp->end = argp->p + (argp->pagelen>>2);
  158. argp->pagelen = 0;
  159. } else {
  160. argp->end = argp->p + (PAGE_SIZE>>2);
  161. argp->pagelen -= PAGE_SIZE;
  162. }
  163. memcpy(((char*)p)+avail, argp->p, (nbytes - avail));
  164. argp->p += XDR_QUADLEN(nbytes - avail);
  165. return p;
  166. }
  167. static int zero_clientid(clientid_t *clid)
  168. {
  169. return (clid->cl_boot == 0) && (clid->cl_id == 0);
  170. }
  171. static int
  172. defer_free(struct nfsd4_compoundargs *argp,
  173. void (*release)(const void *), void *p)
  174. {
  175. struct tmpbuf *tb;
  176. tb = kmalloc(sizeof(*tb), GFP_KERNEL);
  177. if (!tb)
  178. return -ENOMEM;
  179. tb->buf = p;
  180. tb->release = release;
  181. tb->next = argp->to_free;
  182. argp->to_free = tb;
  183. return 0;
  184. }
  185. static char *savemem(struct nfsd4_compoundargs *argp, __be32 *p, int nbytes)
  186. {
  187. if (p == argp->tmp) {
  188. p = kmemdup(argp->tmp, nbytes, GFP_KERNEL);
  189. if (!p)
  190. return NULL;
  191. } else {
  192. BUG_ON(p != argp->tmpp);
  193. argp->tmpp = NULL;
  194. }
  195. if (defer_free(argp, kfree, p)) {
  196. kfree(p);
  197. return NULL;
  198. } else
  199. return (char *)p;
  200. }
  201. static __be32
  202. nfsd4_decode_bitmap(struct nfsd4_compoundargs *argp, u32 *bmval)
  203. {
  204. u32 bmlen;
  205. DECODE_HEAD;
  206. bmval[0] = 0;
  207. bmval[1] = 0;
  208. bmval[2] = 0;
  209. READ_BUF(4);
  210. READ32(bmlen);
  211. if (bmlen > 1000)
  212. goto xdr_error;
  213. READ_BUF(bmlen << 2);
  214. if (bmlen > 0)
  215. READ32(bmval[0]);
  216. if (bmlen > 1)
  217. READ32(bmval[1]);
  218. if (bmlen > 2)
  219. READ32(bmval[2]);
  220. DECODE_TAIL;
  221. }
  222. static __be32
  223. nfsd4_decode_fattr(struct nfsd4_compoundargs *argp, u32 *bmval,
  224. struct iattr *iattr, struct nfs4_acl **acl)
  225. {
  226. int expected_len, len = 0;
  227. u32 dummy32;
  228. char *buf;
  229. int host_err;
  230. DECODE_HEAD;
  231. iattr->ia_valid = 0;
  232. if ((status = nfsd4_decode_bitmap(argp, bmval)))
  233. return status;
  234. READ_BUF(4);
  235. READ32(expected_len);
  236. if (bmval[0] & FATTR4_WORD0_SIZE) {
  237. READ_BUF(8);
  238. len += 8;
  239. READ64(iattr->ia_size);
  240. iattr->ia_valid |= ATTR_SIZE;
  241. }
  242. if (bmval[0] & FATTR4_WORD0_ACL) {
  243. u32 nace;
  244. struct nfs4_ace *ace;
  245. READ_BUF(4); len += 4;
  246. READ32(nace);
  247. if (nace > NFS4_ACL_MAX)
  248. return nfserr_resource;
  249. *acl = nfs4_acl_new(nace);
  250. if (*acl == NULL) {
  251. host_err = -ENOMEM;
  252. goto out_nfserr;
  253. }
  254. defer_free(argp, kfree, *acl);
  255. (*acl)->naces = nace;
  256. for (ace = (*acl)->aces; ace < (*acl)->aces + nace; ace++) {
  257. READ_BUF(16); len += 16;
  258. READ32(ace->type);
  259. READ32(ace->flag);
  260. READ32(ace->access_mask);
  261. READ32(dummy32);
  262. READ_BUF(dummy32);
  263. len += XDR_QUADLEN(dummy32) << 2;
  264. READMEM(buf, dummy32);
  265. ace->whotype = nfs4_acl_get_whotype(buf, dummy32);
  266. status = nfs_ok;
  267. if (ace->whotype != NFS4_ACL_WHO_NAMED)
  268. ;
  269. else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
  270. status = nfsd_map_name_to_gid(argp->rqstp,
  271. buf, dummy32, &ace->who_gid);
  272. else
  273. status = nfsd_map_name_to_uid(argp->rqstp,
  274. buf, dummy32, &ace->who_uid);
  275. if (status)
  276. return status;
  277. }
  278. } else
  279. *acl = NULL;
  280. if (bmval[1] & FATTR4_WORD1_MODE) {
  281. READ_BUF(4);
  282. len += 4;
  283. READ32(iattr->ia_mode);
  284. iattr->ia_mode &= (S_IFMT | S_IALLUGO);
  285. iattr->ia_valid |= ATTR_MODE;
  286. }
  287. if (bmval[1] & FATTR4_WORD1_OWNER) {
  288. READ_BUF(4);
  289. len += 4;
  290. READ32(dummy32);
  291. READ_BUF(dummy32);
  292. len += (XDR_QUADLEN(dummy32) << 2);
  293. READMEM(buf, dummy32);
  294. if ((status = nfsd_map_name_to_uid(argp->rqstp, buf, dummy32, &iattr->ia_uid)))
  295. return status;
  296. iattr->ia_valid |= ATTR_UID;
  297. }
  298. if (bmval[1] & FATTR4_WORD1_OWNER_GROUP) {
  299. READ_BUF(4);
  300. len += 4;
  301. READ32(dummy32);
  302. READ_BUF(dummy32);
  303. len += (XDR_QUADLEN(dummy32) << 2);
  304. READMEM(buf, dummy32);
  305. if ((status = nfsd_map_name_to_gid(argp->rqstp, buf, dummy32, &iattr->ia_gid)))
  306. return status;
  307. iattr->ia_valid |= ATTR_GID;
  308. }
  309. if (bmval[1] & FATTR4_WORD1_TIME_ACCESS_SET) {
  310. READ_BUF(4);
  311. len += 4;
  312. READ32(dummy32);
  313. switch (dummy32) {
  314. case NFS4_SET_TO_CLIENT_TIME:
  315. /* We require the high 32 bits of 'seconds' to be 0, and we ignore
  316. all 32 bits of 'nseconds'. */
  317. READ_BUF(12);
  318. len += 12;
  319. READ64(iattr->ia_atime.tv_sec);
  320. READ32(iattr->ia_atime.tv_nsec);
  321. if (iattr->ia_atime.tv_nsec >= (u32)1000000000)
  322. return nfserr_inval;
  323. iattr->ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
  324. break;
  325. case NFS4_SET_TO_SERVER_TIME:
  326. iattr->ia_valid |= ATTR_ATIME;
  327. break;
  328. default:
  329. goto xdr_error;
  330. }
  331. }
  332. if (bmval[1] & FATTR4_WORD1_TIME_MODIFY_SET) {
  333. READ_BUF(4);
  334. len += 4;
  335. READ32(dummy32);
  336. switch (dummy32) {
  337. case NFS4_SET_TO_CLIENT_TIME:
  338. /* We require the high 32 bits of 'seconds' to be 0, and we ignore
  339. all 32 bits of 'nseconds'. */
  340. READ_BUF(12);
  341. len += 12;
  342. READ64(iattr->ia_mtime.tv_sec);
  343. READ32(iattr->ia_mtime.tv_nsec);
  344. if (iattr->ia_mtime.tv_nsec >= (u32)1000000000)
  345. return nfserr_inval;
  346. iattr->ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
  347. break;
  348. case NFS4_SET_TO_SERVER_TIME:
  349. iattr->ia_valid |= ATTR_MTIME;
  350. break;
  351. default:
  352. goto xdr_error;
  353. }
  354. }
  355. if (bmval[0] & ~NFSD_WRITEABLE_ATTRS_WORD0
  356. || bmval[1] & ~NFSD_WRITEABLE_ATTRS_WORD1
  357. || bmval[2] & ~NFSD_WRITEABLE_ATTRS_WORD2)
  358. READ_BUF(expected_len - len);
  359. else if (len != expected_len)
  360. goto xdr_error;
  361. DECODE_TAIL;
  362. out_nfserr:
  363. status = nfserrno(host_err);
  364. goto out;
  365. }
  366. static __be32
  367. nfsd4_decode_stateid(struct nfsd4_compoundargs *argp, stateid_t *sid)
  368. {
  369. DECODE_HEAD;
  370. READ_BUF(sizeof(stateid_t));
  371. READ32(sid->si_generation);
  372. COPYMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
  373. DECODE_TAIL;
  374. }
  375. static __be32
  376. nfsd4_decode_access(struct nfsd4_compoundargs *argp, struct nfsd4_access *access)
  377. {
  378. DECODE_HEAD;
  379. READ_BUF(4);
  380. READ32(access->ac_req_access);
  381. DECODE_TAIL;
  382. }
  383. static __be32 nfsd4_decode_cb_sec(struct nfsd4_compoundargs *argp, struct nfsd4_cb_sec *cbs)
  384. {
  385. DECODE_HEAD;
  386. u32 dummy, uid, gid;
  387. char *machine_name;
  388. int i;
  389. int nr_secflavs;
  390. /* callback_sec_params4 */
  391. READ_BUF(4);
  392. READ32(nr_secflavs);
  393. cbs->flavor = (u32)(-1);
  394. for (i = 0; i < nr_secflavs; ++i) {
  395. READ_BUF(4);
  396. READ32(dummy);
  397. switch (dummy) {
  398. case RPC_AUTH_NULL:
  399. /* Nothing to read */
  400. if (cbs->flavor == (u32)(-1))
  401. cbs->flavor = RPC_AUTH_NULL;
  402. break;
  403. case RPC_AUTH_UNIX:
  404. READ_BUF(8);
  405. /* stamp */
  406. READ32(dummy);
  407. /* machine name */
  408. READ32(dummy);
  409. READ_BUF(dummy);
  410. SAVEMEM(machine_name, dummy);
  411. /* uid, gid */
  412. READ_BUF(8);
  413. READ32(uid);
  414. READ32(gid);
  415. /* more gids */
  416. READ_BUF(4);
  417. READ32(dummy);
  418. READ_BUF(dummy * 4);
  419. if (cbs->flavor == (u32)(-1)) {
  420. kuid_t kuid = make_kuid(&init_user_ns, uid);
  421. kgid_t kgid = make_kgid(&init_user_ns, gid);
  422. if (uid_valid(kuid) && gid_valid(kgid)) {
  423. cbs->uid = kuid;
  424. cbs->gid = kgid;
  425. cbs->flavor = RPC_AUTH_UNIX;
  426. } else {
  427. dprintk("RPC_AUTH_UNIX with invalid"
  428. "uid or gid ignoring!\n");
  429. }
  430. }
  431. break;
  432. case RPC_AUTH_GSS:
  433. dprintk("RPC_AUTH_GSS callback secflavor "
  434. "not supported!\n");
  435. READ_BUF(8);
  436. /* gcbp_service */
  437. READ32(dummy);
  438. /* gcbp_handle_from_server */
  439. READ32(dummy);
  440. READ_BUF(dummy);
  441. p += XDR_QUADLEN(dummy);
  442. /* gcbp_handle_from_client */
  443. READ_BUF(4);
  444. READ32(dummy);
  445. READ_BUF(dummy);
  446. break;
  447. default:
  448. dprintk("Illegal callback secflavor\n");
  449. return nfserr_inval;
  450. }
  451. }
  452. DECODE_TAIL;
  453. }
  454. static __be32 nfsd4_decode_backchannel_ctl(struct nfsd4_compoundargs *argp, struct nfsd4_backchannel_ctl *bc)
  455. {
  456. DECODE_HEAD;
  457. READ_BUF(4);
  458. READ32(bc->bc_cb_program);
  459. nfsd4_decode_cb_sec(argp, &bc->bc_cb_sec);
  460. DECODE_TAIL;
  461. }
  462. static __be32 nfsd4_decode_bind_conn_to_session(struct nfsd4_compoundargs *argp, struct nfsd4_bind_conn_to_session *bcts)
  463. {
  464. DECODE_HEAD;
  465. READ_BUF(NFS4_MAX_SESSIONID_LEN + 8);
  466. COPYMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
  467. READ32(bcts->dir);
  468. /* XXX: skipping ctsa_use_conn_in_rdma_mode. Perhaps Tom Tucker
  469. * could help us figure out we should be using it. */
  470. DECODE_TAIL;
  471. }
  472. static __be32
  473. nfsd4_decode_close(struct nfsd4_compoundargs *argp, struct nfsd4_close *close)
  474. {
  475. DECODE_HEAD;
  476. READ_BUF(4);
  477. READ32(close->cl_seqid);
  478. return nfsd4_decode_stateid(argp, &close->cl_stateid);
  479. DECODE_TAIL;
  480. }
  481. static __be32
  482. nfsd4_decode_commit(struct nfsd4_compoundargs *argp, struct nfsd4_commit *commit)
  483. {
  484. DECODE_HEAD;
  485. READ_BUF(12);
  486. READ64(commit->co_offset);
  487. READ32(commit->co_count);
  488. DECODE_TAIL;
  489. }
  490. static __be32
  491. nfsd4_decode_create(struct nfsd4_compoundargs *argp, struct nfsd4_create *create)
  492. {
  493. DECODE_HEAD;
  494. READ_BUF(4);
  495. READ32(create->cr_type);
  496. switch (create->cr_type) {
  497. case NF4LNK:
  498. READ_BUF(4);
  499. READ32(create->cr_linklen);
  500. READ_BUF(create->cr_linklen);
  501. SAVEMEM(create->cr_linkname, create->cr_linklen);
  502. break;
  503. case NF4BLK:
  504. case NF4CHR:
  505. READ_BUF(8);
  506. READ32(create->cr_specdata1);
  507. READ32(create->cr_specdata2);
  508. break;
  509. case NF4SOCK:
  510. case NF4FIFO:
  511. case NF4DIR:
  512. default:
  513. break;
  514. }
  515. READ_BUF(4);
  516. READ32(create->cr_namelen);
  517. READ_BUF(create->cr_namelen);
  518. SAVEMEM(create->cr_name, create->cr_namelen);
  519. if ((status = check_filename(create->cr_name, create->cr_namelen)))
  520. return status;
  521. status = nfsd4_decode_fattr(argp, create->cr_bmval, &create->cr_iattr,
  522. &create->cr_acl);
  523. if (status)
  524. goto out;
  525. DECODE_TAIL;
  526. }
  527. static inline __be32
  528. nfsd4_decode_delegreturn(struct nfsd4_compoundargs *argp, struct nfsd4_delegreturn *dr)
  529. {
  530. return nfsd4_decode_stateid(argp, &dr->dr_stateid);
  531. }
  532. static inline __be32
  533. nfsd4_decode_getattr(struct nfsd4_compoundargs *argp, struct nfsd4_getattr *getattr)
  534. {
  535. return nfsd4_decode_bitmap(argp, getattr->ga_bmval);
  536. }
  537. static __be32
  538. nfsd4_decode_link(struct nfsd4_compoundargs *argp, struct nfsd4_link *link)
  539. {
  540. DECODE_HEAD;
  541. READ_BUF(4);
  542. READ32(link->li_namelen);
  543. READ_BUF(link->li_namelen);
  544. SAVEMEM(link->li_name, link->li_namelen);
  545. if ((status = check_filename(link->li_name, link->li_namelen)))
  546. return status;
  547. DECODE_TAIL;
  548. }
  549. static __be32
  550. nfsd4_decode_lock(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
  551. {
  552. DECODE_HEAD;
  553. /*
  554. * type, reclaim(boolean), offset, length, new_lock_owner(boolean)
  555. */
  556. READ_BUF(28);
  557. READ32(lock->lk_type);
  558. if ((lock->lk_type < NFS4_READ_LT) || (lock->lk_type > NFS4_WRITEW_LT))
  559. goto xdr_error;
  560. READ32(lock->lk_reclaim);
  561. READ64(lock->lk_offset);
  562. READ64(lock->lk_length);
  563. READ32(lock->lk_is_new);
  564. if (lock->lk_is_new) {
  565. READ_BUF(4);
  566. READ32(lock->lk_new_open_seqid);
  567. status = nfsd4_decode_stateid(argp, &lock->lk_new_open_stateid);
  568. if (status)
  569. return status;
  570. READ_BUF(8 + sizeof(clientid_t));
  571. READ32(lock->lk_new_lock_seqid);
  572. COPYMEM(&lock->lk_new_clientid, sizeof(clientid_t));
  573. READ32(lock->lk_new_owner.len);
  574. READ_BUF(lock->lk_new_owner.len);
  575. READMEM(lock->lk_new_owner.data, lock->lk_new_owner.len);
  576. } else {
  577. status = nfsd4_decode_stateid(argp, &lock->lk_old_lock_stateid);
  578. if (status)
  579. return status;
  580. READ_BUF(4);
  581. READ32(lock->lk_old_lock_seqid);
  582. }
  583. DECODE_TAIL;
  584. }
  585. static __be32
  586. nfsd4_decode_lockt(struct nfsd4_compoundargs *argp, struct nfsd4_lockt *lockt)
  587. {
  588. DECODE_HEAD;
  589. READ_BUF(32);
  590. READ32(lockt->lt_type);
  591. if((lockt->lt_type < NFS4_READ_LT) || (lockt->lt_type > NFS4_WRITEW_LT))
  592. goto xdr_error;
  593. READ64(lockt->lt_offset);
  594. READ64(lockt->lt_length);
  595. COPYMEM(&lockt->lt_clientid, 8);
  596. READ32(lockt->lt_owner.len);
  597. READ_BUF(lockt->lt_owner.len);
  598. READMEM(lockt->lt_owner.data, lockt->lt_owner.len);
  599. DECODE_TAIL;
  600. }
  601. static __be32
  602. nfsd4_decode_locku(struct nfsd4_compoundargs *argp, struct nfsd4_locku *locku)
  603. {
  604. DECODE_HEAD;
  605. READ_BUF(8);
  606. READ32(locku->lu_type);
  607. if ((locku->lu_type < NFS4_READ_LT) || (locku->lu_type > NFS4_WRITEW_LT))
  608. goto xdr_error;
  609. READ32(locku->lu_seqid);
  610. status = nfsd4_decode_stateid(argp, &locku->lu_stateid);
  611. if (status)
  612. return status;
  613. READ_BUF(16);
  614. READ64(locku->lu_offset);
  615. READ64(locku->lu_length);
  616. DECODE_TAIL;
  617. }
  618. static __be32
  619. nfsd4_decode_lookup(struct nfsd4_compoundargs *argp, struct nfsd4_lookup *lookup)
  620. {
  621. DECODE_HEAD;
  622. READ_BUF(4);
  623. READ32(lookup->lo_len);
  624. READ_BUF(lookup->lo_len);
  625. SAVEMEM(lookup->lo_name, lookup->lo_len);
  626. if ((status = check_filename(lookup->lo_name, lookup->lo_len)))
  627. return status;
  628. DECODE_TAIL;
  629. }
  630. static __be32 nfsd4_decode_share_access(struct nfsd4_compoundargs *argp, u32 *share_access, u32 *deleg_want, u32 *deleg_when)
  631. {
  632. __be32 *p;
  633. u32 w;
  634. READ_BUF(4);
  635. READ32(w);
  636. *share_access = w & NFS4_SHARE_ACCESS_MASK;
  637. *deleg_want = w & NFS4_SHARE_WANT_MASK;
  638. if (deleg_when)
  639. *deleg_when = w & NFS4_SHARE_WHEN_MASK;
  640. switch (w & NFS4_SHARE_ACCESS_MASK) {
  641. case NFS4_SHARE_ACCESS_READ:
  642. case NFS4_SHARE_ACCESS_WRITE:
  643. case NFS4_SHARE_ACCESS_BOTH:
  644. break;
  645. default:
  646. return nfserr_bad_xdr;
  647. }
  648. w &= ~NFS4_SHARE_ACCESS_MASK;
  649. if (!w)
  650. return nfs_ok;
  651. if (!argp->minorversion)
  652. return nfserr_bad_xdr;
  653. switch (w & NFS4_SHARE_WANT_MASK) {
  654. case NFS4_SHARE_WANT_NO_PREFERENCE:
  655. case NFS4_SHARE_WANT_READ_DELEG:
  656. case NFS4_SHARE_WANT_WRITE_DELEG:
  657. case NFS4_SHARE_WANT_ANY_DELEG:
  658. case NFS4_SHARE_WANT_NO_DELEG:
  659. case NFS4_SHARE_WANT_CANCEL:
  660. break;
  661. default:
  662. return nfserr_bad_xdr;
  663. }
  664. w &= ~NFS4_SHARE_WANT_MASK;
  665. if (!w)
  666. return nfs_ok;
  667. if (!deleg_when) /* open_downgrade */
  668. return nfserr_inval;
  669. switch (w) {
  670. case NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL:
  671. case NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED:
  672. case (NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL |
  673. NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED):
  674. return nfs_ok;
  675. }
  676. xdr_error:
  677. return nfserr_bad_xdr;
  678. }
  679. static __be32 nfsd4_decode_share_deny(struct nfsd4_compoundargs *argp, u32 *x)
  680. {
  681. __be32 *p;
  682. READ_BUF(4);
  683. READ32(*x);
  684. /* Note: unlinke access bits, deny bits may be zero. */
  685. if (*x & ~NFS4_SHARE_DENY_BOTH)
  686. return nfserr_bad_xdr;
  687. return nfs_ok;
  688. xdr_error:
  689. return nfserr_bad_xdr;
  690. }
  691. static __be32 nfsd4_decode_opaque(struct nfsd4_compoundargs *argp, struct xdr_netobj *o)
  692. {
  693. __be32 *p;
  694. READ_BUF(4);
  695. READ32(o->len);
  696. if (o->len == 0 || o->len > NFS4_OPAQUE_LIMIT)
  697. return nfserr_bad_xdr;
  698. READ_BUF(o->len);
  699. SAVEMEM(o->data, o->len);
  700. return nfs_ok;
  701. xdr_error:
  702. return nfserr_bad_xdr;
  703. }
  704. static __be32
  705. nfsd4_decode_open(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
  706. {
  707. DECODE_HEAD;
  708. u32 dummy;
  709. memset(open->op_bmval, 0, sizeof(open->op_bmval));
  710. open->op_iattr.ia_valid = 0;
  711. open->op_openowner = NULL;
  712. open->op_xdr_error = 0;
  713. /* seqid, share_access, share_deny, clientid, ownerlen */
  714. READ_BUF(4);
  715. READ32(open->op_seqid);
  716. /* decode, yet ignore deleg_when until supported */
  717. status = nfsd4_decode_share_access(argp, &open->op_share_access,
  718. &open->op_deleg_want, &dummy);
  719. if (status)
  720. goto xdr_error;
  721. status = nfsd4_decode_share_deny(argp, &open->op_share_deny);
  722. if (status)
  723. goto xdr_error;
  724. READ_BUF(sizeof(clientid_t));
  725. COPYMEM(&open->op_clientid, sizeof(clientid_t));
  726. status = nfsd4_decode_opaque(argp, &open->op_owner);
  727. if (status)
  728. goto xdr_error;
  729. READ_BUF(4);
  730. READ32(open->op_create);
  731. switch (open->op_create) {
  732. case NFS4_OPEN_NOCREATE:
  733. break;
  734. case NFS4_OPEN_CREATE:
  735. READ_BUF(4);
  736. READ32(open->op_createmode);
  737. switch (open->op_createmode) {
  738. case NFS4_CREATE_UNCHECKED:
  739. case NFS4_CREATE_GUARDED:
  740. status = nfsd4_decode_fattr(argp, open->op_bmval,
  741. &open->op_iattr, &open->op_acl);
  742. if (status)
  743. goto out;
  744. break;
  745. case NFS4_CREATE_EXCLUSIVE:
  746. READ_BUF(NFS4_VERIFIER_SIZE);
  747. COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
  748. break;
  749. case NFS4_CREATE_EXCLUSIVE4_1:
  750. if (argp->minorversion < 1)
  751. goto xdr_error;
  752. READ_BUF(NFS4_VERIFIER_SIZE);
  753. COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
  754. status = nfsd4_decode_fattr(argp, open->op_bmval,
  755. &open->op_iattr, &open->op_acl);
  756. if (status)
  757. goto out;
  758. break;
  759. default:
  760. goto xdr_error;
  761. }
  762. break;
  763. default:
  764. goto xdr_error;
  765. }
  766. /* open_claim */
  767. READ_BUF(4);
  768. READ32(open->op_claim_type);
  769. switch (open->op_claim_type) {
  770. case NFS4_OPEN_CLAIM_NULL:
  771. case NFS4_OPEN_CLAIM_DELEGATE_PREV:
  772. READ_BUF(4);
  773. READ32(open->op_fname.len);
  774. READ_BUF(open->op_fname.len);
  775. SAVEMEM(open->op_fname.data, open->op_fname.len);
  776. if ((status = check_filename(open->op_fname.data, open->op_fname.len)))
  777. return status;
  778. break;
  779. case NFS4_OPEN_CLAIM_PREVIOUS:
  780. READ_BUF(4);
  781. READ32(open->op_delegate_type);
  782. break;
  783. case NFS4_OPEN_CLAIM_DELEGATE_CUR:
  784. status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
  785. if (status)
  786. return status;
  787. READ_BUF(4);
  788. READ32(open->op_fname.len);
  789. READ_BUF(open->op_fname.len);
  790. SAVEMEM(open->op_fname.data, open->op_fname.len);
  791. if ((status = check_filename(open->op_fname.data, open->op_fname.len)))
  792. return status;
  793. break;
  794. case NFS4_OPEN_CLAIM_FH:
  795. case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
  796. if (argp->minorversion < 1)
  797. goto xdr_error;
  798. /* void */
  799. break;
  800. case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
  801. if (argp->minorversion < 1)
  802. goto xdr_error;
  803. status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
  804. if (status)
  805. return status;
  806. break;
  807. default:
  808. goto xdr_error;
  809. }
  810. DECODE_TAIL;
  811. }
  812. static __be32
  813. nfsd4_decode_open_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_open_confirm *open_conf)
  814. {
  815. DECODE_HEAD;
  816. status = nfsd4_decode_stateid(argp, &open_conf->oc_req_stateid);
  817. if (status)
  818. return status;
  819. READ_BUF(4);
  820. READ32(open_conf->oc_seqid);
  821. DECODE_TAIL;
  822. }
  823. static __be32
  824. nfsd4_decode_open_downgrade(struct nfsd4_compoundargs *argp, struct nfsd4_open_downgrade *open_down)
  825. {
  826. DECODE_HEAD;
  827. status = nfsd4_decode_stateid(argp, &open_down->od_stateid);
  828. if (status)
  829. return status;
  830. READ_BUF(4);
  831. READ32(open_down->od_seqid);
  832. status = nfsd4_decode_share_access(argp, &open_down->od_share_access,
  833. &open_down->od_deleg_want, NULL);
  834. if (status)
  835. return status;
  836. status = nfsd4_decode_share_deny(argp, &open_down->od_share_deny);
  837. if (status)
  838. return status;
  839. DECODE_TAIL;
  840. }
  841. static __be32
  842. nfsd4_decode_putfh(struct nfsd4_compoundargs *argp, struct nfsd4_putfh *putfh)
  843. {
  844. DECODE_HEAD;
  845. READ_BUF(4);
  846. READ32(putfh->pf_fhlen);
  847. if (putfh->pf_fhlen > NFS4_FHSIZE)
  848. goto xdr_error;
  849. READ_BUF(putfh->pf_fhlen);
  850. SAVEMEM(putfh->pf_fhval, putfh->pf_fhlen);
  851. DECODE_TAIL;
  852. }
  853. static __be32
  854. nfsd4_decode_read(struct nfsd4_compoundargs *argp, struct nfsd4_read *read)
  855. {
  856. DECODE_HEAD;
  857. status = nfsd4_decode_stateid(argp, &read->rd_stateid);
  858. if (status)
  859. return status;
  860. READ_BUF(12);
  861. READ64(read->rd_offset);
  862. READ32(read->rd_length);
  863. DECODE_TAIL;
  864. }
  865. static __be32
  866. nfsd4_decode_readdir(struct nfsd4_compoundargs *argp, struct nfsd4_readdir *readdir)
  867. {
  868. DECODE_HEAD;
  869. READ_BUF(24);
  870. READ64(readdir->rd_cookie);
  871. COPYMEM(readdir->rd_verf.data, sizeof(readdir->rd_verf.data));
  872. READ32(readdir->rd_dircount); /* just in case you needed a useless field... */
  873. READ32(readdir->rd_maxcount);
  874. if ((status = nfsd4_decode_bitmap(argp, readdir->rd_bmval)))
  875. goto out;
  876. DECODE_TAIL;
  877. }
  878. static __be32
  879. nfsd4_decode_remove(struct nfsd4_compoundargs *argp, struct nfsd4_remove *remove)
  880. {
  881. DECODE_HEAD;
  882. READ_BUF(4);
  883. READ32(remove->rm_namelen);
  884. READ_BUF(remove->rm_namelen);
  885. SAVEMEM(remove->rm_name, remove->rm_namelen);
  886. if ((status = check_filename(remove->rm_name, remove->rm_namelen)))
  887. return status;
  888. DECODE_TAIL;
  889. }
  890. static __be32
  891. nfsd4_decode_rename(struct nfsd4_compoundargs *argp, struct nfsd4_rename *rename)
  892. {
  893. DECODE_HEAD;
  894. READ_BUF(4);
  895. READ32(rename->rn_snamelen);
  896. READ_BUF(rename->rn_snamelen + 4);
  897. SAVEMEM(rename->rn_sname, rename->rn_snamelen);
  898. READ32(rename->rn_tnamelen);
  899. READ_BUF(rename->rn_tnamelen);
  900. SAVEMEM(rename->rn_tname, rename->rn_tnamelen);
  901. if ((status = check_filename(rename->rn_sname, rename->rn_snamelen)))
  902. return status;
  903. if ((status = check_filename(rename->rn_tname, rename->rn_tnamelen)))
  904. return status;
  905. DECODE_TAIL;
  906. }
  907. static __be32
  908. nfsd4_decode_renew(struct nfsd4_compoundargs *argp, clientid_t *clientid)
  909. {
  910. DECODE_HEAD;
  911. READ_BUF(sizeof(clientid_t));
  912. COPYMEM(clientid, sizeof(clientid_t));
  913. DECODE_TAIL;
  914. }
  915. static __be32
  916. nfsd4_decode_secinfo(struct nfsd4_compoundargs *argp,
  917. struct nfsd4_secinfo *secinfo)
  918. {
  919. DECODE_HEAD;
  920. READ_BUF(4);
  921. READ32(secinfo->si_namelen);
  922. READ_BUF(secinfo->si_namelen);
  923. SAVEMEM(secinfo->si_name, secinfo->si_namelen);
  924. status = check_filename(secinfo->si_name, secinfo->si_namelen);
  925. if (status)
  926. return status;
  927. DECODE_TAIL;
  928. }
  929. static __be32
  930. nfsd4_decode_secinfo_no_name(struct nfsd4_compoundargs *argp,
  931. struct nfsd4_secinfo_no_name *sin)
  932. {
  933. DECODE_HEAD;
  934. READ_BUF(4);
  935. READ32(sin->sin_style);
  936. DECODE_TAIL;
  937. }
  938. static __be32
  939. nfsd4_decode_setattr(struct nfsd4_compoundargs *argp, struct nfsd4_setattr *setattr)
  940. {
  941. __be32 status;
  942. status = nfsd4_decode_stateid(argp, &setattr->sa_stateid);
  943. if (status)
  944. return status;
  945. return nfsd4_decode_fattr(argp, setattr->sa_bmval, &setattr->sa_iattr,
  946. &setattr->sa_acl);
  947. }
  948. static __be32
  949. nfsd4_decode_setclientid(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid *setclientid)
  950. {
  951. DECODE_HEAD;
  952. READ_BUF(NFS4_VERIFIER_SIZE);
  953. COPYMEM(setclientid->se_verf.data, NFS4_VERIFIER_SIZE);
  954. status = nfsd4_decode_opaque(argp, &setclientid->se_name);
  955. if (status)
  956. return nfserr_bad_xdr;
  957. READ_BUF(8);
  958. READ32(setclientid->se_callback_prog);
  959. READ32(setclientid->se_callback_netid_len);
  960. READ_BUF(setclientid->se_callback_netid_len + 4);
  961. SAVEMEM(setclientid->se_callback_netid_val, setclientid->se_callback_netid_len);
  962. READ32(setclientid->se_callback_addr_len);
  963. READ_BUF(setclientid->se_callback_addr_len + 4);
  964. SAVEMEM(setclientid->se_callback_addr_val, setclientid->se_callback_addr_len);
  965. READ32(setclientid->se_callback_ident);
  966. DECODE_TAIL;
  967. }
  968. static __be32
  969. nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid_confirm *scd_c)
  970. {
  971. DECODE_HEAD;
  972. READ_BUF(8 + NFS4_VERIFIER_SIZE);
  973. COPYMEM(&scd_c->sc_clientid, 8);
  974. COPYMEM(&scd_c->sc_confirm, NFS4_VERIFIER_SIZE);
  975. DECODE_TAIL;
  976. }
  977. /* Also used for NVERIFY */
  978. static __be32
  979. nfsd4_decode_verify(struct nfsd4_compoundargs *argp, struct nfsd4_verify *verify)
  980. {
  981. DECODE_HEAD;
  982. if ((status = nfsd4_decode_bitmap(argp, verify->ve_bmval)))
  983. goto out;
  984. /* For convenience's sake, we compare raw xdr'd attributes in
  985. * nfsd4_proc_verify */
  986. READ_BUF(4);
  987. READ32(verify->ve_attrlen);
  988. READ_BUF(verify->ve_attrlen);
  989. SAVEMEM(verify->ve_attrval, verify->ve_attrlen);
  990. DECODE_TAIL;
  991. }
  992. static __be32
  993. nfsd4_decode_write(struct nfsd4_compoundargs *argp, struct nfsd4_write *write)
  994. {
  995. int avail;
  996. int len;
  997. DECODE_HEAD;
  998. status = nfsd4_decode_stateid(argp, &write->wr_stateid);
  999. if (status)
  1000. return status;
  1001. READ_BUF(16);
  1002. READ64(write->wr_offset);
  1003. READ32(write->wr_stable_how);
  1004. if (write->wr_stable_how > 2)
  1005. goto xdr_error;
  1006. READ32(write->wr_buflen);
  1007. /* Sorry .. no magic macros for this.. *
  1008. * READ_BUF(write->wr_buflen);
  1009. * SAVEMEM(write->wr_buf, write->wr_buflen);
  1010. */
  1011. avail = (char*)argp->end - (char*)argp->p;
  1012. if (avail + argp->pagelen < write->wr_buflen) {
  1013. dprintk("NFSD: xdr error (%s:%d)\n",
  1014. __FILE__, __LINE__);
  1015. goto xdr_error;
  1016. }
  1017. write->wr_head.iov_base = p;
  1018. write->wr_head.iov_len = avail;
  1019. WARN_ON(avail != (XDR_QUADLEN(avail) << 2));
  1020. write->wr_pagelist = argp->pagelist;
  1021. len = XDR_QUADLEN(write->wr_buflen) << 2;
  1022. if (len >= avail) {
  1023. int pages;
  1024. len -= avail;
  1025. pages = len >> PAGE_SHIFT;
  1026. argp->pagelist += pages;
  1027. argp->pagelen -= pages * PAGE_SIZE;
  1028. len -= pages * PAGE_SIZE;
  1029. argp->p = (__be32 *)page_address(argp->pagelist[0]);
  1030. argp->end = argp->p + XDR_QUADLEN(PAGE_SIZE);
  1031. }
  1032. argp->p += XDR_QUADLEN(len);
  1033. DECODE_TAIL;
  1034. }
  1035. static __be32
  1036. nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp, struct nfsd4_release_lockowner *rlockowner)
  1037. {
  1038. DECODE_HEAD;
  1039. READ_BUF(12);
  1040. COPYMEM(&rlockowner->rl_clientid, sizeof(clientid_t));
  1041. READ32(rlockowner->rl_owner.len);
  1042. READ_BUF(rlockowner->rl_owner.len);
  1043. READMEM(rlockowner->rl_owner.data, rlockowner->rl_owner.len);
  1044. if (argp->minorversion && !zero_clientid(&rlockowner->rl_clientid))
  1045. return nfserr_inval;
  1046. DECODE_TAIL;
  1047. }
  1048. static __be32
  1049. nfsd4_decode_exchange_id(struct nfsd4_compoundargs *argp,
  1050. struct nfsd4_exchange_id *exid)
  1051. {
  1052. int dummy, tmp;
  1053. DECODE_HEAD;
  1054. READ_BUF(NFS4_VERIFIER_SIZE);
  1055. COPYMEM(exid->verifier.data, NFS4_VERIFIER_SIZE);
  1056. status = nfsd4_decode_opaque(argp, &exid->clname);
  1057. if (status)
  1058. return nfserr_bad_xdr;
  1059. READ_BUF(4);
  1060. READ32(exid->flags);
  1061. /* Ignore state_protect4_a */
  1062. READ_BUF(4);
  1063. READ32(exid->spa_how);
  1064. switch (exid->spa_how) {
  1065. case SP4_NONE:
  1066. break;
  1067. case SP4_MACH_CRED:
  1068. /* spo_must_enforce */
  1069. READ_BUF(4);
  1070. READ32(dummy);
  1071. READ_BUF(dummy * 4);
  1072. p += dummy;
  1073. /* spo_must_allow */
  1074. READ_BUF(4);
  1075. READ32(dummy);
  1076. READ_BUF(dummy * 4);
  1077. p += dummy;
  1078. break;
  1079. case SP4_SSV:
  1080. /* ssp_ops */
  1081. READ_BUF(4);
  1082. READ32(dummy);
  1083. READ_BUF(dummy * 4);
  1084. p += dummy;
  1085. READ_BUF(4);
  1086. READ32(dummy);
  1087. READ_BUF(dummy * 4);
  1088. p += dummy;
  1089. /* ssp_hash_algs<> */
  1090. READ_BUF(4);
  1091. READ32(tmp);
  1092. while (tmp--) {
  1093. READ_BUF(4);
  1094. READ32(dummy);
  1095. READ_BUF(dummy);
  1096. p += XDR_QUADLEN(dummy);
  1097. }
  1098. /* ssp_encr_algs<> */
  1099. READ_BUF(4);
  1100. READ32(tmp);
  1101. while (tmp--) {
  1102. READ_BUF(4);
  1103. READ32(dummy);
  1104. READ_BUF(dummy);
  1105. p += XDR_QUADLEN(dummy);
  1106. }
  1107. /* ssp_window and ssp_num_gss_handles */
  1108. READ_BUF(8);
  1109. READ32(dummy);
  1110. READ32(dummy);
  1111. break;
  1112. default:
  1113. goto xdr_error;
  1114. }
  1115. /* Ignore Implementation ID */
  1116. READ_BUF(4); /* nfs_impl_id4 array length */
  1117. READ32(dummy);
  1118. if (dummy > 1)
  1119. goto xdr_error;
  1120. if (dummy == 1) {
  1121. /* nii_domain */
  1122. READ_BUF(4);
  1123. READ32(dummy);
  1124. READ_BUF(dummy);
  1125. p += XDR_QUADLEN(dummy);
  1126. /* nii_name */
  1127. READ_BUF(4);
  1128. READ32(dummy);
  1129. READ_BUF(dummy);
  1130. p += XDR_QUADLEN(dummy);
  1131. /* nii_date */
  1132. READ_BUF(12);
  1133. p += 3;
  1134. }
  1135. DECODE_TAIL;
  1136. }
  1137. static __be32
  1138. nfsd4_decode_create_session(struct nfsd4_compoundargs *argp,
  1139. struct nfsd4_create_session *sess)
  1140. {
  1141. DECODE_HEAD;
  1142. u32 dummy;
  1143. READ_BUF(16);
  1144. COPYMEM(&sess->clientid, 8);
  1145. READ32(sess->seqid);
  1146. READ32(sess->flags);
  1147. /* Fore channel attrs */
  1148. READ_BUF(28);
  1149. READ32(dummy); /* headerpadsz is always 0 */
  1150. READ32(sess->fore_channel.maxreq_sz);
  1151. READ32(sess->fore_channel.maxresp_sz);
  1152. READ32(sess->fore_channel.maxresp_cached);
  1153. READ32(sess->fore_channel.maxops);
  1154. READ32(sess->fore_channel.maxreqs);
  1155. READ32(sess->fore_channel.nr_rdma_attrs);
  1156. if (sess->fore_channel.nr_rdma_attrs == 1) {
  1157. READ_BUF(4);
  1158. READ32(sess->fore_channel.rdma_attrs);
  1159. } else if (sess->fore_channel.nr_rdma_attrs > 1) {
  1160. dprintk("Too many fore channel attr bitmaps!\n");
  1161. goto xdr_error;
  1162. }
  1163. /* Back channel attrs */
  1164. READ_BUF(28);
  1165. READ32(dummy); /* headerpadsz is always 0 */
  1166. READ32(sess->back_channel.maxreq_sz);
  1167. READ32(sess->back_channel.maxresp_sz);
  1168. READ32(sess->back_channel.maxresp_cached);
  1169. READ32(sess->back_channel.maxops);
  1170. READ32(sess->back_channel.maxreqs);
  1171. READ32(sess->back_channel.nr_rdma_attrs);
  1172. if (sess->back_channel.nr_rdma_attrs == 1) {
  1173. READ_BUF(4);
  1174. READ32(sess->back_channel.rdma_attrs);
  1175. } else if (sess->back_channel.nr_rdma_attrs > 1) {
  1176. dprintk("Too many back channel attr bitmaps!\n");
  1177. goto xdr_error;
  1178. }
  1179. READ_BUF(4);
  1180. READ32(sess->callback_prog);
  1181. nfsd4_decode_cb_sec(argp, &sess->cb_sec);
  1182. DECODE_TAIL;
  1183. }
  1184. static __be32
  1185. nfsd4_decode_destroy_session(struct nfsd4_compoundargs *argp,
  1186. struct nfsd4_destroy_session *destroy_session)
  1187. {
  1188. DECODE_HEAD;
  1189. READ_BUF(NFS4_MAX_SESSIONID_LEN);
  1190. COPYMEM(destroy_session->sessionid.data, NFS4_MAX_SESSIONID_LEN);
  1191. DECODE_TAIL;
  1192. }
  1193. static __be32
  1194. nfsd4_decode_free_stateid(struct nfsd4_compoundargs *argp,
  1195. struct nfsd4_free_stateid *free_stateid)
  1196. {
  1197. DECODE_HEAD;
  1198. READ_BUF(sizeof(stateid_t));
  1199. READ32(free_stateid->fr_stateid.si_generation);
  1200. COPYMEM(&free_stateid->fr_stateid.si_opaque, sizeof(stateid_opaque_t));
  1201. DECODE_TAIL;
  1202. }
  1203. static __be32
  1204. nfsd4_decode_sequence(struct nfsd4_compoundargs *argp,
  1205. struct nfsd4_sequence *seq)
  1206. {
  1207. DECODE_HEAD;
  1208. READ_BUF(NFS4_MAX_SESSIONID_LEN + 16);
  1209. COPYMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
  1210. READ32(seq->seqid);
  1211. READ32(seq->slotid);
  1212. READ32(seq->maxslots);
  1213. READ32(seq->cachethis);
  1214. DECODE_TAIL;
  1215. }
  1216. static __be32
  1217. nfsd4_decode_test_stateid(struct nfsd4_compoundargs *argp, struct nfsd4_test_stateid *test_stateid)
  1218. {
  1219. int i;
  1220. __be32 *p, status;
  1221. struct nfsd4_test_stateid_id *stateid;
  1222. READ_BUF(4);
  1223. test_stateid->ts_num_ids = ntohl(*p++);
  1224. INIT_LIST_HEAD(&test_stateid->ts_stateid_list);
  1225. for (i = 0; i < test_stateid->ts_num_ids; i++) {
  1226. stateid = kmalloc(sizeof(struct nfsd4_test_stateid_id), GFP_KERNEL);
  1227. if (!stateid) {
  1228. status = nfserrno(-ENOMEM);
  1229. goto out;
  1230. }
  1231. defer_free(argp, kfree, stateid);
  1232. INIT_LIST_HEAD(&stateid->ts_id_list);
  1233. list_add_tail(&stateid->ts_id_list, &test_stateid->ts_stateid_list);
  1234. status = nfsd4_decode_stateid(argp, &stateid->ts_id_stateid);
  1235. if (status)
  1236. goto out;
  1237. }
  1238. status = 0;
  1239. out:
  1240. return status;
  1241. xdr_error:
  1242. dprintk("NFSD: xdr error (%s:%d)\n", __FILE__, __LINE__);
  1243. status = nfserr_bad_xdr;
  1244. goto out;
  1245. }
  1246. static __be32 nfsd4_decode_destroy_clientid(struct nfsd4_compoundargs *argp, struct nfsd4_destroy_clientid *dc)
  1247. {
  1248. DECODE_HEAD;
  1249. READ_BUF(8);
  1250. COPYMEM(&dc->clientid, 8);
  1251. DECODE_TAIL;
  1252. }
  1253. static __be32 nfsd4_decode_reclaim_complete(struct nfsd4_compoundargs *argp, struct nfsd4_reclaim_complete *rc)
  1254. {
  1255. DECODE_HEAD;
  1256. READ_BUF(4);
  1257. READ32(rc->rca_one_fs);
  1258. DECODE_TAIL;
  1259. }
  1260. static __be32
  1261. nfsd4_decode_noop(struct nfsd4_compoundargs *argp, void *p)
  1262. {
  1263. return nfs_ok;
  1264. }
  1265. static __be32
  1266. nfsd4_decode_notsupp(struct nfsd4_compoundargs *argp, void *p)
  1267. {
  1268. return nfserr_notsupp;
  1269. }
  1270. typedef __be32(*nfsd4_dec)(struct nfsd4_compoundargs *argp, void *);
  1271. static nfsd4_dec nfsd4_dec_ops[] = {
  1272. [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
  1273. [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
  1274. [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
  1275. [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
  1276. [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
  1277. [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
  1278. [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
  1279. [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
  1280. [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
  1281. [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
  1282. [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
  1283. [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
  1284. [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
  1285. [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
  1286. [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
  1287. [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
  1288. [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
  1289. [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_open_confirm,
  1290. [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
  1291. [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
  1292. [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_noop,
  1293. [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
  1294. [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
  1295. [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
  1296. [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
  1297. [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
  1298. [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
  1299. [OP_RENEW] = (nfsd4_dec)nfsd4_decode_renew,
  1300. [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
  1301. [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
  1302. [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
  1303. [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
  1304. [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_setclientid,
  1305. [OP_SETCLIENTID_CONFIRM] = (nfsd4_dec)nfsd4_decode_setclientid_confirm,
  1306. [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
  1307. [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
  1308. [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_release_lockowner,
  1309. };
  1310. static nfsd4_dec nfsd41_dec_ops[] = {
  1311. [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
  1312. [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
  1313. [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
  1314. [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
  1315. [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
  1316. [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
  1317. [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
  1318. [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
  1319. [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
  1320. [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
  1321. [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
  1322. [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
  1323. [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
  1324. [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
  1325. [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
  1326. [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
  1327. [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
  1328. [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_notsupp,
  1329. [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
  1330. [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
  1331. [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_notsupp,
  1332. [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
  1333. [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
  1334. [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
  1335. [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
  1336. [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
  1337. [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
  1338. [OP_RENEW] = (nfsd4_dec)nfsd4_decode_notsupp,
  1339. [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
  1340. [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
  1341. [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
  1342. [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
  1343. [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_notsupp,
  1344. [OP_SETCLIENTID_CONFIRM]= (nfsd4_dec)nfsd4_decode_notsupp,
  1345. [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
  1346. [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
  1347. [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_notsupp,
  1348. /* new operations for NFSv4.1 */
  1349. [OP_BACKCHANNEL_CTL] = (nfsd4_dec)nfsd4_decode_backchannel_ctl,
  1350. [OP_BIND_CONN_TO_SESSION]= (nfsd4_dec)nfsd4_decode_bind_conn_to_session,
  1351. [OP_EXCHANGE_ID] = (nfsd4_dec)nfsd4_decode_exchange_id,
  1352. [OP_CREATE_SESSION] = (nfsd4_dec)nfsd4_decode_create_session,
  1353. [OP_DESTROY_SESSION] = (nfsd4_dec)nfsd4_decode_destroy_session,
  1354. [OP_FREE_STATEID] = (nfsd4_dec)nfsd4_decode_free_stateid,
  1355. [OP_GET_DIR_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
  1356. [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_notsupp,
  1357. [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
  1358. [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_notsupp,
  1359. [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_notsupp,
  1360. [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_notsupp,
  1361. [OP_SECINFO_NO_NAME] = (nfsd4_dec)nfsd4_decode_secinfo_no_name,
  1362. [OP_SEQUENCE] = (nfsd4_dec)nfsd4_decode_sequence,
  1363. [OP_SET_SSV] = (nfsd4_dec)nfsd4_decode_notsupp,
  1364. [OP_TEST_STATEID] = (nfsd4_dec)nfsd4_decode_test_stateid,
  1365. [OP_WANT_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
  1366. [OP_DESTROY_CLIENTID] = (nfsd4_dec)nfsd4_decode_destroy_clientid,
  1367. [OP_RECLAIM_COMPLETE] = (nfsd4_dec)nfsd4_decode_reclaim_complete,
  1368. };
  1369. struct nfsd4_minorversion_ops {
  1370. nfsd4_dec *decoders;
  1371. int nops;
  1372. };
  1373. static struct nfsd4_minorversion_ops nfsd4_minorversion[] = {
  1374. [0] = { nfsd4_dec_ops, ARRAY_SIZE(nfsd4_dec_ops) },
  1375. [1] = { nfsd41_dec_ops, ARRAY_SIZE(nfsd41_dec_ops) },
  1376. };
  1377. static __be32
  1378. nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
  1379. {
  1380. DECODE_HEAD;
  1381. struct nfsd4_op *op;
  1382. struct nfsd4_minorversion_ops *ops;
  1383. bool cachethis = false;
  1384. int i;
  1385. READ_BUF(4);
  1386. READ32(argp->taglen);
  1387. READ_BUF(argp->taglen + 8);
  1388. SAVEMEM(argp->tag, argp->taglen);
  1389. READ32(argp->minorversion);
  1390. READ32(argp->opcnt);
  1391. if (argp->taglen > NFSD4_MAX_TAGLEN)
  1392. goto xdr_error;
  1393. if (argp->opcnt > 100)
  1394. goto xdr_error;
  1395. if (argp->opcnt > ARRAY_SIZE(argp->iops)) {
  1396. argp->ops = kmalloc(argp->opcnt * sizeof(*argp->ops), GFP_KERNEL);
  1397. if (!argp->ops) {
  1398. argp->ops = argp->iops;
  1399. dprintk("nfsd: couldn't allocate room for COMPOUND\n");
  1400. goto xdr_error;
  1401. }
  1402. }
  1403. if (argp->minorversion >= ARRAY_SIZE(nfsd4_minorversion))
  1404. argp->opcnt = 0;
  1405. ops = &nfsd4_minorversion[argp->minorversion];
  1406. for (i = 0; i < argp->opcnt; i++) {
  1407. op = &argp->ops[i];
  1408. op->replay = NULL;
  1409. READ_BUF(4);
  1410. READ32(op->opnum);
  1411. if (op->opnum >= FIRST_NFS4_OP && op->opnum <= LAST_NFS4_OP)
  1412. op->status = ops->decoders[op->opnum](argp, &op->u);
  1413. else {
  1414. op->opnum = OP_ILLEGAL;
  1415. op->status = nfserr_op_illegal;
  1416. }
  1417. if (op->status) {
  1418. argp->opcnt = i+1;
  1419. break;
  1420. }
  1421. /*
  1422. * We'll try to cache the result in the DRC if any one
  1423. * op in the compound wants to be cached:
  1424. */
  1425. cachethis |= nfsd4_cache_this_op(op);
  1426. }
  1427. /* Sessions make the DRC unnecessary: */
  1428. if (argp->minorversion)
  1429. cachethis = false;
  1430. argp->rqstp->rq_cachetype = cachethis ? RC_REPLBUFF : RC_NOCACHE;
  1431. DECODE_TAIL;
  1432. }
  1433. #define WRITE32(n) *p++ = htonl(n)
  1434. #define WRITE64(n) do { \
  1435. *p++ = htonl((u32)((n) >> 32)); \
  1436. *p++ = htonl((u32)(n)); \
  1437. } while (0)
  1438. #define WRITEMEM(ptr,nbytes) do { if (nbytes > 0) { \
  1439. *(p + XDR_QUADLEN(nbytes) -1) = 0; \
  1440. memcpy(p, ptr, nbytes); \
  1441. p += XDR_QUADLEN(nbytes); \
  1442. }} while (0)
  1443. static void write32(__be32 **p, u32 n)
  1444. {
  1445. *(*p)++ = htonl(n);
  1446. }
  1447. static void write64(__be32 **p, u64 n)
  1448. {
  1449. write32(p, (n >> 32));
  1450. write32(p, (u32)n);
  1451. }
  1452. static void write_change(__be32 **p, struct kstat *stat, struct inode *inode)
  1453. {
  1454. if (IS_I_VERSION(inode)) {
  1455. write64(p, inode->i_version);
  1456. } else {
  1457. write32(p, stat->ctime.tv_sec);
  1458. write32(p, stat->ctime.tv_nsec);
  1459. }
  1460. }
  1461. static void write_cinfo(__be32 **p, struct nfsd4_change_info *c)
  1462. {
  1463. write32(p, c->atomic);
  1464. if (c->change_supported) {
  1465. write64(p, c->before_change);
  1466. write64(p, c->after_change);
  1467. } else {
  1468. write32(p, c->before_ctime_sec);
  1469. write32(p, c->before_ctime_nsec);
  1470. write32(p, c->after_ctime_sec);
  1471. write32(p, c->after_ctime_nsec);
  1472. }
  1473. }
  1474. #define RESERVE_SPACE(nbytes) do { \
  1475. p = resp->p; \
  1476. BUG_ON(p + XDR_QUADLEN(nbytes) > resp->end); \
  1477. } while (0)
  1478. #define ADJUST_ARGS() resp->p = p
  1479. /* Encode as an array of strings the string given with components
  1480. * separated @sep, escaped with esc_enter and esc_exit.
  1481. */
  1482. static __be32 nfsd4_encode_components_esc(char sep, char *components,
  1483. __be32 **pp, int *buflen,
  1484. char esc_enter, char esc_exit)
  1485. {
  1486. __be32 *p = *pp;
  1487. __be32 *countp = p;
  1488. int strlen, count=0;
  1489. char *str, *end, *next;
  1490. dprintk("nfsd4_encode_components(%s)\n", components);
  1491. if ((*buflen -= 4) < 0)
  1492. return nfserr_resource;
  1493. WRITE32(0); /* We will fill this in with @count later */
  1494. end = str = components;
  1495. while (*end) {
  1496. bool found_esc = false;
  1497. /* try to parse as esc_start, ..., esc_end, sep */
  1498. if (*str == esc_enter) {
  1499. for (; *end && (*end != esc_exit); end++)
  1500. /* find esc_exit or end of string */;
  1501. next = end + 1;
  1502. if (*end && (!*next || *next == sep)) {
  1503. str++;
  1504. found_esc = true;
  1505. }
  1506. }
  1507. if (!found_esc)
  1508. for (; *end && (*end != sep); end++)
  1509. /* find sep or end of string */;
  1510. strlen = end - str;
  1511. if (strlen) {
  1512. if ((*buflen -= ((XDR_QUADLEN(strlen) << 2) + 4)) < 0)
  1513. return nfserr_resource;
  1514. WRITE32(strlen);
  1515. WRITEMEM(str, strlen);
  1516. count++;
  1517. }
  1518. else
  1519. end++;
  1520. str = end;
  1521. }
  1522. *pp = p;
  1523. p = countp;
  1524. WRITE32(count);
  1525. return 0;
  1526. }
  1527. /* Encode as an array of strings the string given with components
  1528. * separated @sep.
  1529. */
  1530. static __be32 nfsd4_encode_components(char sep, char *components,
  1531. __be32 **pp, int *buflen)
  1532. {
  1533. return nfsd4_encode_components_esc(sep, components, pp, buflen, 0, 0);
  1534. }
  1535. /*
  1536. * encode a location element of a fs_locations structure
  1537. */
  1538. static __be32 nfsd4_encode_fs_location4(struct nfsd4_fs_location *location,
  1539. __be32 **pp, int *buflen)
  1540. {
  1541. __be32 status;
  1542. __be32 *p = *pp;
  1543. status = nfsd4_encode_components_esc(':', location->hosts, &p, buflen,
  1544. '[', ']');
  1545. if (status)
  1546. return status;
  1547. status = nfsd4_encode_components('/', location->path, &p, buflen);
  1548. if (status)
  1549. return status;
  1550. *pp = p;
  1551. return 0;
  1552. }
  1553. /*
  1554. * Encode a path in RFC3530 'pathname4' format
  1555. */
  1556. static __be32 nfsd4_encode_path(const struct path *root,
  1557. const struct path *path, __be32 **pp, int *buflen)
  1558. {
  1559. struct path cur = {
  1560. .mnt = path->mnt,
  1561. .dentry = path->dentry,
  1562. };
  1563. __be32 *p = *pp;
  1564. struct dentry **components = NULL;
  1565. unsigned int ncomponents = 0;
  1566. __be32 err = nfserr_jukebox;
  1567. dprintk("nfsd4_encode_components(");
  1568. path_get(&cur);
  1569. /* First walk the path up to the nfsd root, and store the
  1570. * dentries/path components in an array.
  1571. */
  1572. for (;;) {
  1573. if (cur.dentry == root->dentry && cur.mnt == root->mnt)
  1574. break;
  1575. if (cur.dentry == cur.mnt->mnt_root) {
  1576. if (follow_up(&cur))
  1577. continue;
  1578. goto out_free;
  1579. }
  1580. if ((ncomponents & 15) == 0) {
  1581. struct dentry **new;
  1582. new = krealloc(components,
  1583. sizeof(*new) * (ncomponents + 16),
  1584. GFP_KERNEL);
  1585. if (!new)
  1586. goto out_free;
  1587. components = new;
  1588. }
  1589. components[ncomponents++] = cur.dentry;
  1590. cur.dentry = dget_parent(cur.dentry);
  1591. }
  1592. *buflen -= 4;
  1593. if (*buflen < 0)
  1594. goto out_free;
  1595. WRITE32(ncomponents);
  1596. while (ncomponents) {
  1597. struct dentry *dentry = components[ncomponents - 1];
  1598. unsigned int len = dentry->d_name.len;
  1599. *buflen -= 4 + (XDR_QUADLEN(len) << 2);
  1600. if (*buflen < 0)
  1601. goto out_free;
  1602. WRITE32(len);
  1603. WRITEMEM(dentry->d_name.name, len);
  1604. dprintk("/%s", dentry->d_name.name);
  1605. dput(dentry);
  1606. ncomponents--;
  1607. }
  1608. *pp = p;
  1609. err = 0;
  1610. out_free:
  1611. dprintk(")\n");
  1612. while (ncomponents)
  1613. dput(components[--ncomponents]);
  1614. kfree(components);
  1615. path_put(&cur);
  1616. return err;
  1617. }
  1618. static __be32 nfsd4_encode_fsloc_fsroot(struct svc_rqst *rqstp,
  1619. const struct path *path, __be32 **pp, int *buflen)
  1620. {
  1621. struct svc_export *exp_ps;
  1622. __be32 res;
  1623. exp_ps = rqst_find_fsidzero_export(rqstp);
  1624. if (IS_ERR(exp_ps))
  1625. return nfserrno(PTR_ERR(exp_ps));
  1626. res = nfsd4_encode_path(&exp_ps->ex_path, path, pp, buflen);
  1627. exp_put(exp_ps);
  1628. return res;
  1629. }
  1630. /*
  1631. * encode a fs_locations structure
  1632. */
  1633. static __be32 nfsd4_encode_fs_locations(struct svc_rqst *rqstp,
  1634. struct svc_export *exp,
  1635. __be32 **pp, int *buflen)
  1636. {
  1637. __be32 status;
  1638. int i;
  1639. __be32 *p = *pp;
  1640. struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
  1641. status = nfsd4_encode_fsloc_fsroot(rqstp, &exp->ex_path, &p, buflen);
  1642. if (status)
  1643. return status;
  1644. if ((*buflen -= 4) < 0)
  1645. return nfserr_resource;
  1646. WRITE32(fslocs->locations_count);
  1647. for (i=0; i<fslocs->locations_count; i++) {
  1648. status = nfsd4_encode_fs_location4(&fslocs->locations[i],
  1649. &p, buflen);
  1650. if (status)
  1651. return status;
  1652. }
  1653. *pp = p;
  1654. return 0;
  1655. }
  1656. static u32 nfs4_file_type(umode_t mode)
  1657. {
  1658. switch (mode & S_IFMT) {
  1659. case S_IFIFO: return NF4FIFO;
  1660. case S_IFCHR: return NF4CHR;
  1661. case S_IFDIR: return NF4DIR;
  1662. case S_IFBLK: return NF4BLK;
  1663. case S_IFLNK: return NF4LNK;
  1664. case S_IFREG: return NF4REG;
  1665. case S_IFSOCK: return NF4SOCK;
  1666. default: return NF4BAD;
  1667. };
  1668. }
  1669. static __be32
  1670. nfsd4_encode_name(struct svc_rqst *rqstp, int whotype, kuid_t uid, kgid_t gid,
  1671. __be32 **p, int *buflen)
  1672. {
  1673. int status;
  1674. if (*buflen < (XDR_QUADLEN(IDMAP_NAMESZ) << 2) + 4)
  1675. return nfserr_resource;
  1676. if (whotype != NFS4_ACL_WHO_NAMED)
  1677. status = nfs4_acl_write_who(whotype, (u8 *)(*p + 1));
  1678. else if (gid_valid(gid))
  1679. status = nfsd_map_gid_to_name(rqstp, gid, (u8 *)(*p + 1));
  1680. else
  1681. status = nfsd_map_uid_to_name(rqstp, uid, (u8 *)(*p + 1));
  1682. if (status < 0)
  1683. return nfserrno(status);
  1684. *p = xdr_encode_opaque(*p, NULL, status);
  1685. *buflen -= (XDR_QUADLEN(status) << 2) + 4;
  1686. BUG_ON(*buflen < 0);
  1687. return 0;
  1688. }
  1689. static inline __be32
  1690. nfsd4_encode_user(struct svc_rqst *rqstp, kuid_t user, __be32 **p, int *buflen)
  1691. {
  1692. return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, user, INVALID_GID,
  1693. p, buflen);
  1694. }
  1695. static inline __be32
  1696. nfsd4_encode_group(struct svc_rqst *rqstp, kgid_t group, __be32 **p, int *buflen)
  1697. {
  1698. return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, INVALID_UID, group,
  1699. p, buflen);
  1700. }
  1701. static inline __be32
  1702. nfsd4_encode_aclname(struct svc_rqst *rqstp, struct nfs4_ace *ace,
  1703. __be32 **p, int *buflen)
  1704. {
  1705. kuid_t uid = INVALID_UID;
  1706. kgid_t gid = INVALID_GID;
  1707. if (ace->whotype == NFS4_ACL_WHO_NAMED) {
  1708. if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
  1709. gid = ace->who_gid;
  1710. else
  1711. uid = ace->who_uid;
  1712. }
  1713. return nfsd4_encode_name(rqstp, ace->whotype, uid, gid, p, buflen);
  1714. }
  1715. #define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
  1716. FATTR4_WORD0_RDATTR_ERROR)
  1717. #define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
  1718. static __be32 fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *rdattr_err)
  1719. {
  1720. /* As per referral draft: */
  1721. if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
  1722. *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
  1723. if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
  1724. *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
  1725. *rdattr_err = NFSERR_MOVED;
  1726. else
  1727. return nfserr_moved;
  1728. }
  1729. *bmval0 &= WORD0_ABSENT_FS_ATTRS;
  1730. *bmval1 &= WORD1_ABSENT_FS_ATTRS;
  1731. return 0;
  1732. }
  1733. static int get_parent_attributes(struct svc_export *exp, struct kstat *stat)
  1734. {
  1735. struct path path = exp->ex_path;
  1736. int err;
  1737. path_get(&path);
  1738. while (follow_up(&path)) {
  1739. if (path.dentry != path.mnt->mnt_root)
  1740. break;
  1741. }
  1742. err = vfs_getattr(&path, stat);
  1743. path_put(&path);
  1744. return err;
  1745. }
  1746. /*
  1747. * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
  1748. * ourselves.
  1749. *
  1750. * countp is the buffer size in _words_
  1751. */
  1752. __be32
  1753. nfsd4_encode_fattr(struct svc_fh *fhp, struct svc_export *exp,
  1754. struct dentry *dentry, __be32 **buffer, int count, u32 *bmval,
  1755. struct svc_rqst *rqstp, int ignore_crossmnt)
  1756. {
  1757. u32 bmval0 = bmval[0];
  1758. u32 bmval1 = bmval[1];
  1759. u32 bmval2 = bmval[2];
  1760. struct kstat stat;
  1761. struct svc_fh tempfh;
  1762. struct kstatfs statfs;
  1763. int buflen = count << 2;
  1764. __be32 *attrlenp;
  1765. u32 dummy;
  1766. u64 dummy64;
  1767. u32 rdattr_err = 0;
  1768. __be32 *p = *buffer;
  1769. __be32 status;
  1770. int err;
  1771. int aclsupport = 0;
  1772. struct nfs4_acl *acl = NULL;
  1773. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  1774. u32 minorversion = resp->cstate.minorversion;
  1775. struct path path = {
  1776. .mnt = exp->ex_path.mnt,
  1777. .dentry = dentry,
  1778. };
  1779. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1780. BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
  1781. BUG_ON(bmval0 & ~nfsd_suppattrs0(minorversion));
  1782. BUG_ON(bmval1 & ~nfsd_suppattrs1(minorversion));
  1783. BUG_ON(bmval2 & ~nfsd_suppattrs2(minorversion));
  1784. if (exp->ex_fslocs.migrated) {
  1785. BUG_ON(bmval[2]);
  1786. status = fattr_handle_absent_fs(&bmval0, &bmval1, &rdattr_err);
  1787. if (status)
  1788. goto out;
  1789. }
  1790. err = vfs_getattr(&path, &stat);
  1791. if (err)
  1792. goto out_nfserr;
  1793. if ((bmval0 & (FATTR4_WORD0_FILES_FREE | FATTR4_WORD0_FILES_TOTAL |
  1794. FATTR4_WORD0_MAXNAME)) ||
  1795. (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
  1796. FATTR4_WORD1_SPACE_TOTAL))) {
  1797. err = vfs_statfs(&path, &statfs);
  1798. if (err)
  1799. goto out_nfserr;
  1800. }
  1801. if ((bmval0 & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) && !fhp) {
  1802. fh_init(&tempfh, NFS4_FHSIZE);
  1803. status = fh_compose(&tempfh, exp, dentry, NULL);
  1804. if (status)
  1805. goto out;
  1806. fhp = &tempfh;
  1807. }
  1808. if (bmval0 & (FATTR4_WORD0_ACL | FATTR4_WORD0_ACLSUPPORT
  1809. | FATTR4_WORD0_SUPPORTED_ATTRS)) {
  1810. err = nfsd4_get_nfs4_acl(rqstp, dentry, &acl);
  1811. aclsupport = (err == 0);
  1812. if (bmval0 & FATTR4_WORD0_ACL) {
  1813. if (err == -EOPNOTSUPP)
  1814. bmval0 &= ~FATTR4_WORD0_ACL;
  1815. else if (err == -EINVAL) {
  1816. status = nfserr_attrnotsupp;
  1817. goto out;
  1818. } else if (err != 0)
  1819. goto out_nfserr;
  1820. }
  1821. }
  1822. if (bmval2) {
  1823. if ((buflen -= 16) < 0)
  1824. goto out_resource;
  1825. WRITE32(3);
  1826. WRITE32(bmval0);
  1827. WRITE32(bmval1);
  1828. WRITE32(bmval2);
  1829. } else if (bmval1) {
  1830. if ((buflen -= 12) < 0)
  1831. goto out_resource;
  1832. WRITE32(2);
  1833. WRITE32(bmval0);
  1834. WRITE32(bmval1);
  1835. } else {
  1836. if ((buflen -= 8) < 0)
  1837. goto out_resource;
  1838. WRITE32(1);
  1839. WRITE32(bmval0);
  1840. }
  1841. attrlenp = p++; /* to be backfilled later */
  1842. if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
  1843. u32 word0 = nfsd_suppattrs0(minorversion);
  1844. u32 word1 = nfsd_suppattrs1(minorversion);
  1845. u32 word2 = nfsd_suppattrs2(minorversion);
  1846. if (!aclsupport)
  1847. word0 &= ~FATTR4_WORD0_ACL;
  1848. if (!word2) {
  1849. if ((buflen -= 12) < 0)
  1850. goto out_resource;
  1851. WRITE32(2);
  1852. WRITE32(word0);
  1853. WRITE32(word1);
  1854. } else {
  1855. if ((buflen -= 16) < 0)
  1856. goto out_resource;
  1857. WRITE32(3);
  1858. WRITE32(word0);
  1859. WRITE32(word1);
  1860. WRITE32(word2);
  1861. }
  1862. }
  1863. if (bmval0 & FATTR4_WORD0_TYPE) {
  1864. if ((buflen -= 4) < 0)
  1865. goto out_resource;
  1866. dummy = nfs4_file_type(stat.mode);
  1867. if (dummy == NF4BAD)
  1868. goto out_serverfault;
  1869. WRITE32(dummy);
  1870. }
  1871. if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
  1872. if ((buflen -= 4) < 0)
  1873. goto out_resource;
  1874. if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
  1875. WRITE32(NFS4_FH_PERSISTENT);
  1876. else
  1877. WRITE32(NFS4_FH_PERSISTENT|NFS4_FH_VOL_RENAME);
  1878. }
  1879. if (bmval0 & FATTR4_WORD0_CHANGE) {
  1880. if ((buflen -= 8) < 0)
  1881. goto out_resource;
  1882. write_change(&p, &stat, dentry->d_inode);
  1883. }
  1884. if (bmval0 & FATTR4_WORD0_SIZE) {
  1885. if ((buflen -= 8) < 0)
  1886. goto out_resource;
  1887. WRITE64(stat.size);
  1888. }
  1889. if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
  1890. if ((buflen -= 4) < 0)
  1891. goto out_resource;
  1892. WRITE32(1);
  1893. }
  1894. if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
  1895. if ((buflen -= 4) < 0)
  1896. goto out_resource;
  1897. WRITE32(1);
  1898. }
  1899. if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
  1900. if ((buflen -= 4) < 0)
  1901. goto out_resource;
  1902. WRITE32(0);
  1903. }
  1904. if (bmval0 & FATTR4_WORD0_FSID) {
  1905. if ((buflen -= 16) < 0)
  1906. goto out_resource;
  1907. if (exp->ex_fslocs.migrated) {
  1908. WRITE64(NFS4_REFERRAL_FSID_MAJOR);
  1909. WRITE64(NFS4_REFERRAL_FSID_MINOR);
  1910. } else switch(fsid_source(fhp)) {
  1911. case FSIDSOURCE_FSID:
  1912. WRITE64((u64)exp->ex_fsid);
  1913. WRITE64((u64)0);
  1914. break;
  1915. case FSIDSOURCE_DEV:
  1916. WRITE32(0);
  1917. WRITE32(MAJOR(stat.dev));
  1918. WRITE32(0);
  1919. WRITE32(MINOR(stat.dev));
  1920. break;
  1921. case FSIDSOURCE_UUID:
  1922. WRITEMEM(exp->ex_uuid, 16);
  1923. break;
  1924. }
  1925. }
  1926. if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
  1927. if ((buflen -= 4) < 0)
  1928. goto out_resource;
  1929. WRITE32(0);
  1930. }
  1931. if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
  1932. if ((buflen -= 4) < 0)
  1933. goto out_resource;
  1934. WRITE32(nn->nfsd4_lease);
  1935. }
  1936. if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
  1937. if ((buflen -= 4) < 0)
  1938. goto out_resource;
  1939. WRITE32(rdattr_err);
  1940. }
  1941. if (bmval0 & FATTR4_WORD0_ACL) {
  1942. struct nfs4_ace *ace;
  1943. if (acl == NULL) {
  1944. if ((buflen -= 4) < 0)
  1945. goto out_resource;
  1946. WRITE32(0);
  1947. goto out_acl;
  1948. }
  1949. if ((buflen -= 4) < 0)
  1950. goto out_resource;
  1951. WRITE32(acl->naces);
  1952. for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
  1953. if ((buflen -= 4*3) < 0)
  1954. goto out_resource;
  1955. WRITE32(ace->type);
  1956. WRITE32(ace->flag);
  1957. WRITE32(ace->access_mask & NFS4_ACE_MASK_ALL);
  1958. status = nfsd4_encode_aclname(rqstp, ace, &p, &buflen);
  1959. if (status == nfserr_resource)
  1960. goto out_resource;
  1961. if (status)
  1962. goto out;
  1963. }
  1964. }
  1965. out_acl:
  1966. if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
  1967. if ((buflen -= 4) < 0)
  1968. goto out_resource;
  1969. WRITE32(aclsupport ?
  1970. ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL : 0);
  1971. }
  1972. if (bmval0 & FATTR4_WORD0_CANSETTIME) {
  1973. if ((buflen -= 4) < 0)
  1974. goto out_resource;
  1975. WRITE32(1);
  1976. }
  1977. if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
  1978. if ((buflen -= 4) < 0)
  1979. goto out_resource;
  1980. WRITE32(0);
  1981. }
  1982. if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
  1983. if ((buflen -= 4) < 0)
  1984. goto out_resource;
  1985. WRITE32(1);
  1986. }
  1987. if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
  1988. if ((buflen -= 4) < 0)
  1989. goto out_resource;
  1990. WRITE32(1);
  1991. }
  1992. if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
  1993. buflen -= (XDR_QUADLEN(fhp->fh_handle.fh_size) << 2) + 4;
  1994. if (buflen < 0)
  1995. goto out_resource;
  1996. WRITE32(fhp->fh_handle.fh_size);
  1997. WRITEMEM(&fhp->fh_handle.fh_base, fhp->fh_handle.fh_size);
  1998. }
  1999. if (bmval0 & FATTR4_WORD0_FILEID) {
  2000. if ((buflen -= 8) < 0)
  2001. goto out_resource;
  2002. WRITE64(stat.ino);
  2003. }
  2004. if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
  2005. if ((buflen -= 8) < 0)
  2006. goto out_resource;
  2007. WRITE64((u64) statfs.f_ffree);
  2008. }
  2009. if (bmval0 & FATTR4_WORD0_FILES_FREE) {
  2010. if ((buflen -= 8) < 0)
  2011. goto out_resource;
  2012. WRITE64((u64) statfs.f_ffree);
  2013. }
  2014. if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
  2015. if ((buflen -= 8) < 0)
  2016. goto out_resource;
  2017. WRITE64((u64) statfs.f_files);
  2018. }
  2019. if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
  2020. status = nfsd4_encode_fs_locations(rqstp, exp, &p, &buflen);
  2021. if (status == nfserr_resource)
  2022. goto out_resource;
  2023. if (status)
  2024. goto out;
  2025. }
  2026. if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
  2027. if ((buflen -= 4) < 0)
  2028. goto out_resource;
  2029. WRITE32(1);
  2030. }
  2031. if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
  2032. if ((buflen -= 8) < 0)
  2033. goto out_resource;
  2034. WRITE64(~(u64)0);
  2035. }
  2036. if (bmval0 & FATTR4_WORD0_MAXLINK) {
  2037. if ((buflen -= 4) < 0)
  2038. goto out_resource;
  2039. WRITE32(255);
  2040. }
  2041. if (bmval0 & FATTR4_WORD0_MAXNAME) {
  2042. if ((buflen -= 4) < 0)
  2043. goto out_resource;
  2044. WRITE32(statfs.f_namelen);
  2045. }
  2046. if (bmval0 & FATTR4_WORD0_MAXREAD) {
  2047. if ((buflen -= 8) < 0)
  2048. goto out_resource;
  2049. WRITE64((u64) svc_max_payload(rqstp));
  2050. }
  2051. if (bmval0 & FATTR4_WORD0_MAXWRITE) {
  2052. if ((buflen -= 8) < 0)
  2053. goto out_resource;
  2054. WRITE64((u64) svc_max_payload(rqstp));
  2055. }
  2056. if (bmval1 & FATTR4_WORD1_MODE) {
  2057. if ((buflen -= 4) < 0)
  2058. goto out_resource;
  2059. WRITE32(stat.mode & S_IALLUGO);
  2060. }
  2061. if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
  2062. if ((buflen -= 4) < 0)
  2063. goto out_resource;
  2064. WRITE32(1);
  2065. }
  2066. if (bmval1 & FATTR4_WORD1_NUMLINKS) {
  2067. if ((buflen -= 4) < 0)
  2068. goto out_resource;
  2069. WRITE32(stat.nlink);
  2070. }
  2071. if (bmval1 & FATTR4_WORD1_OWNER) {
  2072. status = nfsd4_encode_user(rqstp, stat.uid, &p, &buflen);
  2073. if (status == nfserr_resource)
  2074. goto out_resource;
  2075. if (status)
  2076. goto out;
  2077. }
  2078. if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
  2079. status = nfsd4_encode_group(rqstp, stat.gid, &p, &buflen);
  2080. if (status == nfserr_resource)
  2081. goto out_resource;
  2082. if (status)
  2083. goto out;
  2084. }
  2085. if (bmval1 & FATTR4_WORD1_RAWDEV) {
  2086. if ((buflen -= 8) < 0)
  2087. goto out_resource;
  2088. WRITE32((u32) MAJOR(stat.rdev));
  2089. WRITE32((u32) MINOR(stat.rdev));
  2090. }
  2091. if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
  2092. if ((buflen -= 8) < 0)
  2093. goto out_resource;
  2094. dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
  2095. WRITE64(dummy64);
  2096. }
  2097. if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
  2098. if ((buflen -= 8) < 0)
  2099. goto out_resource;
  2100. dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
  2101. WRITE64(dummy64);
  2102. }
  2103. if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
  2104. if ((buflen -= 8) < 0)
  2105. goto out_resource;
  2106. dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
  2107. WRITE64(dummy64);
  2108. }
  2109. if (bmval1 & FATTR4_WORD1_SPACE_USED) {
  2110. if ((buflen -= 8) < 0)
  2111. goto out_resource;
  2112. dummy64 = (u64)stat.blocks << 9;
  2113. WRITE64(dummy64);
  2114. }
  2115. if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
  2116. if ((buflen -= 12) < 0)
  2117. goto out_resource;
  2118. WRITE64((s64)stat.atime.tv_sec);
  2119. WRITE32(stat.atime.tv_nsec);
  2120. }
  2121. if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
  2122. if ((buflen -= 12) < 0)
  2123. goto out_resource;
  2124. WRITE32(0);
  2125. WRITE32(1);
  2126. WRITE32(0);
  2127. }
  2128. if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
  2129. if ((buflen -= 12) < 0)
  2130. goto out_resource;
  2131. WRITE64((s64)stat.ctime.tv_sec);
  2132. WRITE32(stat.ctime.tv_nsec);
  2133. }
  2134. if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
  2135. if ((buflen -= 12) < 0)
  2136. goto out_resource;
  2137. WRITE64((s64)stat.mtime.tv_sec);
  2138. WRITE32(stat.mtime.tv_nsec);
  2139. }
  2140. if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
  2141. if ((buflen -= 8) < 0)
  2142. goto out_resource;
  2143. /*
  2144. * Get parent's attributes if not ignoring crossmount
  2145. * and this is the root of a cross-mounted filesystem.
  2146. */
  2147. if (ignore_crossmnt == 0 &&
  2148. dentry == exp->ex_path.mnt->mnt_root)
  2149. get_parent_attributes(exp, &stat);
  2150. WRITE64(stat.ino);
  2151. }
  2152. if (bmval2 & FATTR4_WORD2_SUPPATTR_EXCLCREAT) {
  2153. WRITE32(3);
  2154. WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD0);
  2155. WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD1);
  2156. WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD2);
  2157. }
  2158. *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
  2159. *buffer = p;
  2160. status = nfs_ok;
  2161. out:
  2162. kfree(acl);
  2163. if (fhp == &tempfh)
  2164. fh_put(&tempfh);
  2165. return status;
  2166. out_nfserr:
  2167. status = nfserrno(err);
  2168. goto out;
  2169. out_resource:
  2170. status = nfserr_resource;
  2171. goto out;
  2172. out_serverfault:
  2173. status = nfserr_serverfault;
  2174. goto out;
  2175. }
  2176. static inline int attributes_need_mount(u32 *bmval)
  2177. {
  2178. if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
  2179. return 1;
  2180. if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
  2181. return 1;
  2182. return 0;
  2183. }
  2184. static __be32
  2185. nfsd4_encode_dirent_fattr(struct nfsd4_readdir *cd,
  2186. const char *name, int namlen, __be32 **p, int buflen)
  2187. {
  2188. struct svc_export *exp = cd->rd_fhp->fh_export;
  2189. struct dentry *dentry;
  2190. __be32 nfserr;
  2191. int ignore_crossmnt = 0;
  2192. dentry = lookup_one_len(name, cd->rd_fhp->fh_dentry, namlen);
  2193. if (IS_ERR(dentry))
  2194. return nfserrno(PTR_ERR(dentry));
  2195. if (!dentry->d_inode) {
  2196. /*
  2197. * nfsd_buffered_readdir drops the i_mutex between
  2198. * readdir and calling this callback, leaving a window
  2199. * where this directory entry could have gone away.
  2200. */
  2201. dput(dentry);
  2202. return nfserr_noent;
  2203. }
  2204. exp_get(exp);
  2205. /*
  2206. * In the case of a mountpoint, the client may be asking for
  2207. * attributes that are only properties of the underlying filesystem
  2208. * as opposed to the cross-mounted file system. In such a case,
  2209. * we will not follow the cross mount and will fill the attribtutes
  2210. * directly from the mountpoint dentry.
  2211. */
  2212. if (nfsd_mountpoint(dentry, exp)) {
  2213. int err;
  2214. if (!(exp->ex_flags & NFSEXP_V4ROOT)
  2215. && !attributes_need_mount(cd->rd_bmval)) {
  2216. ignore_crossmnt = 1;
  2217. goto out_encode;
  2218. }
  2219. /*
  2220. * Why the heck aren't we just using nfsd_lookup??
  2221. * Different "."/".." handling? Something else?
  2222. * At least, add a comment here to explain....
  2223. */
  2224. err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
  2225. if (err) {
  2226. nfserr = nfserrno(err);
  2227. goto out_put;
  2228. }
  2229. nfserr = check_nfsd_access(exp, cd->rd_rqstp);
  2230. if (nfserr)
  2231. goto out_put;
  2232. }
  2233. out_encode:
  2234. nfserr = nfsd4_encode_fattr(NULL, exp, dentry, p, buflen, cd->rd_bmval,
  2235. cd->rd_rqstp, ignore_crossmnt);
  2236. out_put:
  2237. dput(dentry);
  2238. exp_put(exp);
  2239. return nfserr;
  2240. }
  2241. static __be32 *
  2242. nfsd4_encode_rdattr_error(__be32 *p, int buflen, __be32 nfserr)
  2243. {
  2244. __be32 *attrlenp;
  2245. if (buflen < 6)
  2246. return NULL;
  2247. *p++ = htonl(2);
  2248. *p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
  2249. *p++ = htonl(0); /* bmval1 */
  2250. attrlenp = p++;
  2251. *p++ = nfserr; /* no htonl */
  2252. *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
  2253. return p;
  2254. }
  2255. static int
  2256. nfsd4_encode_dirent(void *ccdv, const char *name, int namlen,
  2257. loff_t offset, u64 ino, unsigned int d_type)
  2258. {
  2259. struct readdir_cd *ccd = ccdv;
  2260. struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
  2261. int buflen;
  2262. __be32 *p = cd->buffer;
  2263. __be32 *cookiep;
  2264. __be32 nfserr = nfserr_toosmall;
  2265. /* In nfsv4, "." and ".." never make it onto the wire.. */
  2266. if (name && isdotent(name, namlen)) {
  2267. cd->common.err = nfs_ok;
  2268. return 0;
  2269. }
  2270. if (cd->offset)
  2271. xdr_encode_hyper(cd->offset, (u64) offset);
  2272. buflen = cd->buflen - 4 - XDR_QUADLEN(namlen);
  2273. if (buflen < 0)
  2274. goto fail;
  2275. *p++ = xdr_one; /* mark entry present */
  2276. cookiep = p;
  2277. p = xdr_encode_hyper(p, NFS_OFFSET_MAX); /* offset of next entry */
  2278. p = xdr_encode_array(p, name, namlen); /* name length & name */
  2279. nfserr = nfsd4_encode_dirent_fattr(cd, name, namlen, &p, buflen);
  2280. switch (nfserr) {
  2281. case nfs_ok:
  2282. break;
  2283. case nfserr_resource:
  2284. nfserr = nfserr_toosmall;
  2285. goto fail;
  2286. case nfserr_noent:
  2287. goto skip_entry;
  2288. default:
  2289. /*
  2290. * If the client requested the RDATTR_ERROR attribute,
  2291. * we stuff the error code into this attribute
  2292. * and continue. If this attribute was not requested,
  2293. * then in accordance with the spec, we fail the
  2294. * entire READDIR operation(!)
  2295. */
  2296. if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
  2297. goto fail;
  2298. p = nfsd4_encode_rdattr_error(p, buflen, nfserr);
  2299. if (p == NULL) {
  2300. nfserr = nfserr_toosmall;
  2301. goto fail;
  2302. }
  2303. }
  2304. cd->buflen -= (p - cd->buffer);
  2305. cd->buffer = p;
  2306. cd->offset = cookiep;
  2307. skip_entry:
  2308. cd->common.err = nfs_ok;
  2309. return 0;
  2310. fail:
  2311. cd->common.err = nfserr;
  2312. return -EINVAL;
  2313. }
  2314. static void
  2315. nfsd4_encode_stateid(struct nfsd4_compoundres *resp, stateid_t *sid)
  2316. {
  2317. __be32 *p;
  2318. RESERVE_SPACE(sizeof(stateid_t));
  2319. WRITE32(sid->si_generation);
  2320. WRITEMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
  2321. ADJUST_ARGS();
  2322. }
  2323. static __be32
  2324. nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_access *access)
  2325. {
  2326. __be32 *p;
  2327. if (!nfserr) {
  2328. RESERVE_SPACE(8);
  2329. WRITE32(access->ac_supported);
  2330. WRITE32(access->ac_resp_access);
  2331. ADJUST_ARGS();
  2332. }
  2333. return nfserr;
  2334. }
  2335. static __be32 nfsd4_encode_bind_conn_to_session(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_bind_conn_to_session *bcts)
  2336. {
  2337. __be32 *p;
  2338. if (!nfserr) {
  2339. RESERVE_SPACE(NFS4_MAX_SESSIONID_LEN + 8);
  2340. WRITEMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
  2341. WRITE32(bcts->dir);
  2342. /* Sorry, we do not yet support RDMA over 4.1: */
  2343. WRITE32(0);
  2344. ADJUST_ARGS();
  2345. }
  2346. return nfserr;
  2347. }
  2348. static __be32
  2349. nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_close *close)
  2350. {
  2351. if (!nfserr)
  2352. nfsd4_encode_stateid(resp, &close->cl_stateid);
  2353. return nfserr;
  2354. }
  2355. static __be32
  2356. nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_commit *commit)
  2357. {
  2358. __be32 *p;
  2359. if (!nfserr) {
  2360. RESERVE_SPACE(NFS4_VERIFIER_SIZE);
  2361. WRITEMEM(commit->co_verf.data, NFS4_VERIFIER_SIZE);
  2362. ADJUST_ARGS();
  2363. }
  2364. return nfserr;
  2365. }
  2366. static __be32
  2367. nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_create *create)
  2368. {
  2369. __be32 *p;
  2370. if (!nfserr) {
  2371. RESERVE_SPACE(32);
  2372. write_cinfo(&p, &create->cr_cinfo);
  2373. WRITE32(2);
  2374. WRITE32(create->cr_bmval[0]);
  2375. WRITE32(create->cr_bmval[1]);
  2376. ADJUST_ARGS();
  2377. }
  2378. return nfserr;
  2379. }
  2380. static __be32
  2381. nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_getattr *getattr)
  2382. {
  2383. struct svc_fh *fhp = getattr->ga_fhp;
  2384. int buflen;
  2385. if (nfserr)
  2386. return nfserr;
  2387. buflen = resp->end - resp->p - (COMPOUND_ERR_SLACK_SPACE >> 2);
  2388. nfserr = nfsd4_encode_fattr(fhp, fhp->fh_export, fhp->fh_dentry,
  2389. &resp->p, buflen, getattr->ga_bmval,
  2390. resp->rqstp, 0);
  2391. return nfserr;
  2392. }
  2393. static __be32
  2394. nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr, struct svc_fh **fhpp)
  2395. {
  2396. struct svc_fh *fhp = *fhpp;
  2397. unsigned int len;
  2398. __be32 *p;
  2399. if (!nfserr) {
  2400. len = fhp->fh_handle.fh_size;
  2401. RESERVE_SPACE(len + 4);
  2402. WRITE32(len);
  2403. WRITEMEM(&fhp->fh_handle.fh_base, len);
  2404. ADJUST_ARGS();
  2405. }
  2406. return nfserr;
  2407. }
  2408. /*
  2409. * Including all fields other than the name, a LOCK4denied structure requires
  2410. * 8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
  2411. */
  2412. static void
  2413. nfsd4_encode_lock_denied(struct nfsd4_compoundres *resp, struct nfsd4_lock_denied *ld)
  2414. {
  2415. struct xdr_netobj *conf = &ld->ld_owner;
  2416. __be32 *p;
  2417. RESERVE_SPACE(32 + XDR_LEN(conf->len));
  2418. WRITE64(ld->ld_start);
  2419. WRITE64(ld->ld_length);
  2420. WRITE32(ld->ld_type);
  2421. if (conf->len) {
  2422. WRITEMEM(&ld->ld_clientid, 8);
  2423. WRITE32(conf->len);
  2424. WRITEMEM(conf->data, conf->len);
  2425. kfree(conf->data);
  2426. } else { /* non - nfsv4 lock in conflict, no clientid nor owner */
  2427. WRITE64((u64)0); /* clientid */
  2428. WRITE32(0); /* length of owner name */
  2429. }
  2430. ADJUST_ARGS();
  2431. }
  2432. static __be32
  2433. nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lock *lock)
  2434. {
  2435. if (!nfserr)
  2436. nfsd4_encode_stateid(resp, &lock->lk_resp_stateid);
  2437. else if (nfserr == nfserr_denied)
  2438. nfsd4_encode_lock_denied(resp, &lock->lk_denied);
  2439. return nfserr;
  2440. }
  2441. static __be32
  2442. nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lockt *lockt)
  2443. {
  2444. if (nfserr == nfserr_denied)
  2445. nfsd4_encode_lock_denied(resp, &lockt->lt_denied);
  2446. return nfserr;
  2447. }
  2448. static __be32
  2449. nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_locku *locku)
  2450. {
  2451. if (!nfserr)
  2452. nfsd4_encode_stateid(resp, &locku->lu_stateid);
  2453. return nfserr;
  2454. }
  2455. static __be32
  2456. nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_link *link)
  2457. {
  2458. __be32 *p;
  2459. if (!nfserr) {
  2460. RESERVE_SPACE(20);
  2461. write_cinfo(&p, &link->li_cinfo);
  2462. ADJUST_ARGS();
  2463. }
  2464. return nfserr;
  2465. }
  2466. static __be32
  2467. nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open *open)
  2468. {
  2469. __be32 *p;
  2470. if (nfserr)
  2471. goto out;
  2472. nfsd4_encode_stateid(resp, &open->op_stateid);
  2473. RESERVE_SPACE(40);
  2474. write_cinfo(&p, &open->op_cinfo);
  2475. WRITE32(open->op_rflags);
  2476. WRITE32(2);
  2477. WRITE32(open->op_bmval[0]);
  2478. WRITE32(open->op_bmval[1]);
  2479. WRITE32(open->op_delegate_type);
  2480. ADJUST_ARGS();
  2481. switch (open->op_delegate_type) {
  2482. case NFS4_OPEN_DELEGATE_NONE:
  2483. break;
  2484. case NFS4_OPEN_DELEGATE_READ:
  2485. nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
  2486. RESERVE_SPACE(20);
  2487. WRITE32(open->op_recall);
  2488. /*
  2489. * TODO: ACE's in delegations
  2490. */
  2491. WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
  2492. WRITE32(0);
  2493. WRITE32(0);
  2494. WRITE32(0); /* XXX: is NULL principal ok? */
  2495. ADJUST_ARGS();
  2496. break;
  2497. case NFS4_OPEN_DELEGATE_WRITE:
  2498. nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
  2499. RESERVE_SPACE(32);
  2500. WRITE32(0);
  2501. /*
  2502. * TODO: space_limit's in delegations
  2503. */
  2504. WRITE32(NFS4_LIMIT_SIZE);
  2505. WRITE32(~(u32)0);
  2506. WRITE32(~(u32)0);
  2507. /*
  2508. * TODO: ACE's in delegations
  2509. */
  2510. WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
  2511. WRITE32(0);
  2512. WRITE32(0);
  2513. WRITE32(0); /* XXX: is NULL principal ok? */
  2514. ADJUST_ARGS();
  2515. break;
  2516. case NFS4_OPEN_DELEGATE_NONE_EXT: /* 4.1 */
  2517. switch (open->op_why_no_deleg) {
  2518. case WND4_CONTENTION:
  2519. case WND4_RESOURCE:
  2520. RESERVE_SPACE(8);
  2521. WRITE32(open->op_why_no_deleg);
  2522. WRITE32(0); /* deleg signaling not supported yet */
  2523. break;
  2524. default:
  2525. RESERVE_SPACE(4);
  2526. WRITE32(open->op_why_no_deleg);
  2527. }
  2528. ADJUST_ARGS();
  2529. break;
  2530. default:
  2531. BUG();
  2532. }
  2533. /* XXX save filehandle here */
  2534. out:
  2535. return nfserr;
  2536. }
  2537. static __be32
  2538. nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_confirm *oc)
  2539. {
  2540. if (!nfserr)
  2541. nfsd4_encode_stateid(resp, &oc->oc_resp_stateid);
  2542. return nfserr;
  2543. }
  2544. static __be32
  2545. nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_downgrade *od)
  2546. {
  2547. if (!nfserr)
  2548. nfsd4_encode_stateid(resp, &od->od_stateid);
  2549. return nfserr;
  2550. }
  2551. static __be32
  2552. nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
  2553. struct nfsd4_read *read)
  2554. {
  2555. u32 eof;
  2556. int v;
  2557. struct page *page;
  2558. unsigned long maxcount;
  2559. long len;
  2560. __be32 *p;
  2561. if (nfserr)
  2562. return nfserr;
  2563. if (resp->xbuf->page_len)
  2564. return nfserr_resource;
  2565. RESERVE_SPACE(8); /* eof flag and byte count */
  2566. maxcount = svc_max_payload(resp->rqstp);
  2567. if (maxcount > read->rd_length)
  2568. maxcount = read->rd_length;
  2569. len = maxcount;
  2570. v = 0;
  2571. while (len > 0) {
  2572. page = *(resp->rqstp->rq_next_page);
  2573. if (!page) { /* ran out of pages */
  2574. maxcount -= len;
  2575. break;
  2576. }
  2577. resp->rqstp->rq_vec[v].iov_base = page_address(page);
  2578. resp->rqstp->rq_vec[v].iov_len =
  2579. len < PAGE_SIZE ? len : PAGE_SIZE;
  2580. resp->rqstp->rq_next_page++;
  2581. v++;
  2582. len -= PAGE_SIZE;
  2583. }
  2584. read->rd_vlen = v;
  2585. nfserr = nfsd_read_file(read->rd_rqstp, read->rd_fhp, read->rd_filp,
  2586. read->rd_offset, resp->rqstp->rq_vec, read->rd_vlen,
  2587. &maxcount);
  2588. if (nfserr)
  2589. return nfserr;
  2590. eof = (read->rd_offset + maxcount >=
  2591. read->rd_fhp->fh_dentry->d_inode->i_size);
  2592. WRITE32(eof);
  2593. WRITE32(maxcount);
  2594. ADJUST_ARGS();
  2595. resp->xbuf->head[0].iov_len = (char*)p
  2596. - (char*)resp->xbuf->head[0].iov_base;
  2597. resp->xbuf->page_len = maxcount;
  2598. /* Use rest of head for padding and remaining ops: */
  2599. resp->xbuf->tail[0].iov_base = p;
  2600. resp->xbuf->tail[0].iov_len = 0;
  2601. if (maxcount&3) {
  2602. RESERVE_SPACE(4);
  2603. WRITE32(0);
  2604. resp->xbuf->tail[0].iov_base += maxcount&3;
  2605. resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
  2606. ADJUST_ARGS();
  2607. }
  2608. return 0;
  2609. }
  2610. static __be32
  2611. nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readlink *readlink)
  2612. {
  2613. int maxcount;
  2614. char *page;
  2615. __be32 *p;
  2616. if (nfserr)
  2617. return nfserr;
  2618. if (resp->xbuf->page_len)
  2619. return nfserr_resource;
  2620. if (!*resp->rqstp->rq_next_page)
  2621. return nfserr_resource;
  2622. page = page_address(*(resp->rqstp->rq_next_page++));
  2623. maxcount = PAGE_SIZE;
  2624. RESERVE_SPACE(4);
  2625. /*
  2626. * XXX: By default, the ->readlink() VFS op will truncate symlinks
  2627. * if they would overflow the buffer. Is this kosher in NFSv4? If
  2628. * not, one easy fix is: if ->readlink() precisely fills the buffer,
  2629. * assume that truncation occurred, and return NFS4ERR_RESOURCE.
  2630. */
  2631. nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp, page, &maxcount);
  2632. if (nfserr == nfserr_isdir)
  2633. return nfserr_inval;
  2634. if (nfserr)
  2635. return nfserr;
  2636. WRITE32(maxcount);
  2637. ADJUST_ARGS();
  2638. resp->xbuf->head[0].iov_len = (char*)p
  2639. - (char*)resp->xbuf->head[0].iov_base;
  2640. resp->xbuf->page_len = maxcount;
  2641. /* Use rest of head for padding and remaining ops: */
  2642. resp->xbuf->tail[0].iov_base = p;
  2643. resp->xbuf->tail[0].iov_len = 0;
  2644. if (maxcount&3) {
  2645. RESERVE_SPACE(4);
  2646. WRITE32(0);
  2647. resp->xbuf->tail[0].iov_base += maxcount&3;
  2648. resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
  2649. ADJUST_ARGS();
  2650. }
  2651. return 0;
  2652. }
  2653. static __be32
  2654. nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readdir *readdir)
  2655. {
  2656. int maxcount;
  2657. loff_t offset;
  2658. __be32 *page, *savep, *tailbase;
  2659. __be32 *p;
  2660. if (nfserr)
  2661. return nfserr;
  2662. if (resp->xbuf->page_len)
  2663. return nfserr_resource;
  2664. if (!*resp->rqstp->rq_next_page)
  2665. return nfserr_resource;
  2666. RESERVE_SPACE(NFS4_VERIFIER_SIZE);
  2667. savep = p;
  2668. /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
  2669. WRITE32(0);
  2670. WRITE32(0);
  2671. ADJUST_ARGS();
  2672. resp->xbuf->head[0].iov_len = ((char*)resp->p) - (char*)resp->xbuf->head[0].iov_base;
  2673. tailbase = p;
  2674. maxcount = PAGE_SIZE;
  2675. if (maxcount > readdir->rd_maxcount)
  2676. maxcount = readdir->rd_maxcount;
  2677. /*
  2678. * Convert from bytes to words, account for the two words already
  2679. * written, make sure to leave two words at the end for the next
  2680. * pointer and eof field.
  2681. */
  2682. maxcount = (maxcount >> 2) - 4;
  2683. if (maxcount < 0) {
  2684. nfserr = nfserr_toosmall;
  2685. goto err_no_verf;
  2686. }
  2687. page = page_address(*(resp->rqstp->rq_next_page++));
  2688. readdir->common.err = 0;
  2689. readdir->buflen = maxcount;
  2690. readdir->buffer = page;
  2691. readdir->offset = NULL;
  2692. offset = readdir->rd_cookie;
  2693. nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
  2694. &offset,
  2695. &readdir->common, nfsd4_encode_dirent);
  2696. if (nfserr == nfs_ok &&
  2697. readdir->common.err == nfserr_toosmall &&
  2698. readdir->buffer == page)
  2699. nfserr = nfserr_toosmall;
  2700. if (nfserr)
  2701. goto err_no_verf;
  2702. if (readdir->offset)
  2703. xdr_encode_hyper(readdir->offset, offset);
  2704. p = readdir->buffer;
  2705. *p++ = 0; /* no more entries */
  2706. *p++ = htonl(readdir->common.err == nfserr_eof);
  2707. resp->xbuf->page_len = ((char*)p) -
  2708. (char*)page_address(*(resp->rqstp->rq_next_page-1));
  2709. /* Use rest of head for padding and remaining ops: */
  2710. resp->xbuf->tail[0].iov_base = tailbase;
  2711. resp->xbuf->tail[0].iov_len = 0;
  2712. resp->p = resp->xbuf->tail[0].iov_base;
  2713. resp->end = resp->p + (PAGE_SIZE - resp->xbuf->head[0].iov_len)/4;
  2714. return 0;
  2715. err_no_verf:
  2716. p = savep;
  2717. ADJUST_ARGS();
  2718. return nfserr;
  2719. }
  2720. static __be32
  2721. nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_remove *remove)
  2722. {
  2723. __be32 *p;
  2724. if (!nfserr) {
  2725. RESERVE_SPACE(20);
  2726. write_cinfo(&p, &remove->rm_cinfo);
  2727. ADJUST_ARGS();
  2728. }
  2729. return nfserr;
  2730. }
  2731. static __be32
  2732. nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_rename *rename)
  2733. {
  2734. __be32 *p;
  2735. if (!nfserr) {
  2736. RESERVE_SPACE(40);
  2737. write_cinfo(&p, &rename->rn_sinfo);
  2738. write_cinfo(&p, &rename->rn_tinfo);
  2739. ADJUST_ARGS();
  2740. }
  2741. return nfserr;
  2742. }
  2743. static __be32
  2744. nfsd4_do_encode_secinfo(struct nfsd4_compoundres *resp,
  2745. __be32 nfserr, struct svc_export *exp)
  2746. {
  2747. u32 i, nflavs, supported;
  2748. struct exp_flavor_info *flavs;
  2749. struct exp_flavor_info def_flavs[2];
  2750. __be32 *p, *flavorsp;
  2751. static bool report = true;
  2752. if (nfserr)
  2753. goto out;
  2754. if (exp->ex_nflavors) {
  2755. flavs = exp->ex_flavors;
  2756. nflavs = exp->ex_nflavors;
  2757. } else { /* Handling of some defaults in absence of real secinfo: */
  2758. flavs = def_flavs;
  2759. if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
  2760. nflavs = 2;
  2761. flavs[0].pseudoflavor = RPC_AUTH_UNIX;
  2762. flavs[1].pseudoflavor = RPC_AUTH_NULL;
  2763. } else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
  2764. nflavs = 1;
  2765. flavs[0].pseudoflavor
  2766. = svcauth_gss_flavor(exp->ex_client);
  2767. } else {
  2768. nflavs = 1;
  2769. flavs[0].pseudoflavor
  2770. = exp->ex_client->flavour->flavour;
  2771. }
  2772. }
  2773. supported = 0;
  2774. RESERVE_SPACE(4);
  2775. flavorsp = p++; /* to be backfilled later */
  2776. ADJUST_ARGS();
  2777. for (i = 0; i < nflavs; i++) {
  2778. rpc_authflavor_t pf = flavs[i].pseudoflavor;
  2779. struct rpcsec_gss_info info;
  2780. if (rpcauth_get_gssinfo(pf, &info) == 0) {
  2781. supported++;
  2782. RESERVE_SPACE(4 + 4 + info.oid.len + 4 + 4);
  2783. WRITE32(RPC_AUTH_GSS);
  2784. WRITE32(info.oid.len);
  2785. WRITEMEM(info.oid.data, info.oid.len);
  2786. WRITE32(info.qop);
  2787. WRITE32(info.service);
  2788. ADJUST_ARGS();
  2789. } else if (pf < RPC_AUTH_MAXFLAVOR) {
  2790. supported++;
  2791. RESERVE_SPACE(4);
  2792. WRITE32(pf);
  2793. ADJUST_ARGS();
  2794. } else {
  2795. if (report)
  2796. pr_warn("NFS: SECINFO: security flavor %u "
  2797. "is not supported\n", pf);
  2798. }
  2799. }
  2800. if (nflavs != supported)
  2801. report = false;
  2802. *flavorsp = htonl(supported);
  2803. out:
  2804. if (exp)
  2805. exp_put(exp);
  2806. return nfserr;
  2807. }
  2808. static __be32
  2809. nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
  2810. struct nfsd4_secinfo *secinfo)
  2811. {
  2812. return nfsd4_do_encode_secinfo(resp, nfserr, secinfo->si_exp);
  2813. }
  2814. static __be32
  2815. nfsd4_encode_secinfo_no_name(struct nfsd4_compoundres *resp, __be32 nfserr,
  2816. struct nfsd4_secinfo_no_name *secinfo)
  2817. {
  2818. return nfsd4_do_encode_secinfo(resp, nfserr, secinfo->sin_exp);
  2819. }
  2820. /*
  2821. * The SETATTR encode routine is special -- it always encodes a bitmap,
  2822. * regardless of the error status.
  2823. */
  2824. static __be32
  2825. nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setattr *setattr)
  2826. {
  2827. __be32 *p;
  2828. RESERVE_SPACE(12);
  2829. if (nfserr) {
  2830. WRITE32(2);
  2831. WRITE32(0);
  2832. WRITE32(0);
  2833. }
  2834. else {
  2835. WRITE32(2);
  2836. WRITE32(setattr->sa_bmval[0]);
  2837. WRITE32(setattr->sa_bmval[1]);
  2838. }
  2839. ADJUST_ARGS();
  2840. return nfserr;
  2841. }
  2842. static __be32
  2843. nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setclientid *scd)
  2844. {
  2845. __be32 *p;
  2846. if (!nfserr) {
  2847. RESERVE_SPACE(8 + NFS4_VERIFIER_SIZE);
  2848. WRITEMEM(&scd->se_clientid, 8);
  2849. WRITEMEM(&scd->se_confirm, NFS4_VERIFIER_SIZE);
  2850. ADJUST_ARGS();
  2851. }
  2852. else if (nfserr == nfserr_clid_inuse) {
  2853. RESERVE_SPACE(8);
  2854. WRITE32(0);
  2855. WRITE32(0);
  2856. ADJUST_ARGS();
  2857. }
  2858. return nfserr;
  2859. }
  2860. static __be32
  2861. nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_write *write)
  2862. {
  2863. __be32 *p;
  2864. if (!nfserr) {
  2865. RESERVE_SPACE(16);
  2866. WRITE32(write->wr_bytes_written);
  2867. WRITE32(write->wr_how_written);
  2868. WRITEMEM(write->wr_verifier.data, NFS4_VERIFIER_SIZE);
  2869. ADJUST_ARGS();
  2870. }
  2871. return nfserr;
  2872. }
  2873. static __be32
  2874. nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, __be32 nfserr,
  2875. struct nfsd4_exchange_id *exid)
  2876. {
  2877. __be32 *p;
  2878. char *major_id;
  2879. char *server_scope;
  2880. int major_id_sz;
  2881. int server_scope_sz;
  2882. uint64_t minor_id = 0;
  2883. if (nfserr)
  2884. return nfserr;
  2885. major_id = utsname()->nodename;
  2886. major_id_sz = strlen(major_id);
  2887. server_scope = utsname()->nodename;
  2888. server_scope_sz = strlen(server_scope);
  2889. RESERVE_SPACE(
  2890. 8 /* eir_clientid */ +
  2891. 4 /* eir_sequenceid */ +
  2892. 4 /* eir_flags */ +
  2893. 4 /* spr_how (SP4_NONE) */ +
  2894. 8 /* so_minor_id */ +
  2895. 4 /* so_major_id.len */ +
  2896. (XDR_QUADLEN(major_id_sz) * 4) +
  2897. 4 /* eir_server_scope.len */ +
  2898. (XDR_QUADLEN(server_scope_sz) * 4) +
  2899. 4 /* eir_server_impl_id.count (0) */);
  2900. WRITEMEM(&exid->clientid, 8);
  2901. WRITE32(exid->seqid);
  2902. WRITE32(exid->flags);
  2903. /* state_protect4_r. Currently only support SP4_NONE */
  2904. BUG_ON(exid->spa_how != SP4_NONE);
  2905. WRITE32(exid->spa_how);
  2906. /* The server_owner struct */
  2907. WRITE64(minor_id); /* Minor id */
  2908. /* major id */
  2909. WRITE32(major_id_sz);
  2910. WRITEMEM(major_id, major_id_sz);
  2911. /* Server scope */
  2912. WRITE32(server_scope_sz);
  2913. WRITEMEM(server_scope, server_scope_sz);
  2914. /* Implementation id */
  2915. WRITE32(0); /* zero length nfs_impl_id4 array */
  2916. ADJUST_ARGS();
  2917. return 0;
  2918. }
  2919. static __be32
  2920. nfsd4_encode_create_session(struct nfsd4_compoundres *resp, __be32 nfserr,
  2921. struct nfsd4_create_session *sess)
  2922. {
  2923. __be32 *p;
  2924. if (nfserr)
  2925. return nfserr;
  2926. RESERVE_SPACE(24);
  2927. WRITEMEM(sess->sessionid.data, NFS4_MAX_SESSIONID_LEN);
  2928. WRITE32(sess->seqid);
  2929. WRITE32(sess->flags);
  2930. ADJUST_ARGS();
  2931. RESERVE_SPACE(28);
  2932. WRITE32(0); /* headerpadsz */
  2933. WRITE32(sess->fore_channel.maxreq_sz);
  2934. WRITE32(sess->fore_channel.maxresp_sz);
  2935. WRITE32(sess->fore_channel.maxresp_cached);
  2936. WRITE32(sess->fore_channel.maxops);
  2937. WRITE32(sess->fore_channel.maxreqs);
  2938. WRITE32(sess->fore_channel.nr_rdma_attrs);
  2939. ADJUST_ARGS();
  2940. if (sess->fore_channel.nr_rdma_attrs) {
  2941. RESERVE_SPACE(4);
  2942. WRITE32(sess->fore_channel.rdma_attrs);
  2943. ADJUST_ARGS();
  2944. }
  2945. RESERVE_SPACE(28);
  2946. WRITE32(0); /* headerpadsz */
  2947. WRITE32(sess->back_channel.maxreq_sz);
  2948. WRITE32(sess->back_channel.maxresp_sz);
  2949. WRITE32(sess->back_channel.maxresp_cached);
  2950. WRITE32(sess->back_channel.maxops);
  2951. WRITE32(sess->back_channel.maxreqs);
  2952. WRITE32(sess->back_channel.nr_rdma_attrs);
  2953. ADJUST_ARGS();
  2954. if (sess->back_channel.nr_rdma_attrs) {
  2955. RESERVE_SPACE(4);
  2956. WRITE32(sess->back_channel.rdma_attrs);
  2957. ADJUST_ARGS();
  2958. }
  2959. return 0;
  2960. }
  2961. static __be32
  2962. nfsd4_encode_destroy_session(struct nfsd4_compoundres *resp, __be32 nfserr,
  2963. struct nfsd4_destroy_session *destroy_session)
  2964. {
  2965. return nfserr;
  2966. }
  2967. static __be32
  2968. nfsd4_encode_free_stateid(struct nfsd4_compoundres *resp, __be32 nfserr,
  2969. struct nfsd4_free_stateid *free_stateid)
  2970. {
  2971. __be32 *p;
  2972. if (nfserr)
  2973. return nfserr;
  2974. RESERVE_SPACE(4);
  2975. *p++ = nfserr;
  2976. ADJUST_ARGS();
  2977. return nfserr;
  2978. }
  2979. static __be32
  2980. nfsd4_encode_sequence(struct nfsd4_compoundres *resp, __be32 nfserr,
  2981. struct nfsd4_sequence *seq)
  2982. {
  2983. __be32 *p;
  2984. if (nfserr)
  2985. return nfserr;
  2986. RESERVE_SPACE(NFS4_MAX_SESSIONID_LEN + 20);
  2987. WRITEMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
  2988. WRITE32(seq->seqid);
  2989. WRITE32(seq->slotid);
  2990. /* Note slotid's are numbered from zero: */
  2991. WRITE32(seq->maxslots - 1); /* sr_highest_slotid */
  2992. WRITE32(seq->maxslots - 1); /* sr_target_highest_slotid */
  2993. WRITE32(seq->status_flags);
  2994. ADJUST_ARGS();
  2995. resp->cstate.datap = p; /* DRC cache data pointer */
  2996. return 0;
  2997. }
  2998. static __be32
  2999. nfsd4_encode_test_stateid(struct nfsd4_compoundres *resp, __be32 nfserr,
  3000. struct nfsd4_test_stateid *test_stateid)
  3001. {
  3002. struct nfsd4_test_stateid_id *stateid, *next;
  3003. __be32 *p;
  3004. RESERVE_SPACE(4 + (4 * test_stateid->ts_num_ids));
  3005. *p++ = htonl(test_stateid->ts_num_ids);
  3006. list_for_each_entry_safe(stateid, next, &test_stateid->ts_stateid_list, ts_id_list) {
  3007. *p++ = stateid->ts_id_status;
  3008. }
  3009. ADJUST_ARGS();
  3010. return nfserr;
  3011. }
  3012. static __be32
  3013. nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr, void *p)
  3014. {
  3015. return nfserr;
  3016. }
  3017. typedef __be32(* nfsd4_enc)(struct nfsd4_compoundres *, __be32, void *);
  3018. /*
  3019. * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
  3020. * since we don't need to filter out obsolete ops as this is
  3021. * done in the decoding phase.
  3022. */
  3023. static nfsd4_enc nfsd4_enc_ops[] = {
  3024. [OP_ACCESS] = (nfsd4_enc)nfsd4_encode_access,
  3025. [OP_CLOSE] = (nfsd4_enc)nfsd4_encode_close,
  3026. [OP_COMMIT] = (nfsd4_enc)nfsd4_encode_commit,
  3027. [OP_CREATE] = (nfsd4_enc)nfsd4_encode_create,
  3028. [OP_DELEGPURGE] = (nfsd4_enc)nfsd4_encode_noop,
  3029. [OP_DELEGRETURN] = (nfsd4_enc)nfsd4_encode_noop,
  3030. [OP_GETATTR] = (nfsd4_enc)nfsd4_encode_getattr,
  3031. [OP_GETFH] = (nfsd4_enc)nfsd4_encode_getfh,
  3032. [OP_LINK] = (nfsd4_enc)nfsd4_encode_link,
  3033. [OP_LOCK] = (nfsd4_enc)nfsd4_encode_lock,
  3034. [OP_LOCKT] = (nfsd4_enc)nfsd4_encode_lockt,
  3035. [OP_LOCKU] = (nfsd4_enc)nfsd4_encode_locku,
  3036. [OP_LOOKUP] = (nfsd4_enc)nfsd4_encode_noop,
  3037. [OP_LOOKUPP] = (nfsd4_enc)nfsd4_encode_noop,
  3038. [OP_NVERIFY] = (nfsd4_enc)nfsd4_encode_noop,
  3039. [OP_OPEN] = (nfsd4_enc)nfsd4_encode_open,
  3040. [OP_OPENATTR] = (nfsd4_enc)nfsd4_encode_noop,
  3041. [OP_OPEN_CONFIRM] = (nfsd4_enc)nfsd4_encode_open_confirm,
  3042. [OP_OPEN_DOWNGRADE] = (nfsd4_enc)nfsd4_encode_open_downgrade,
  3043. [OP_PUTFH] = (nfsd4_enc)nfsd4_encode_noop,
  3044. [OP_PUTPUBFH] = (nfsd4_enc)nfsd4_encode_noop,
  3045. [OP_PUTROOTFH] = (nfsd4_enc)nfsd4_encode_noop,
  3046. [OP_READ] = (nfsd4_enc)nfsd4_encode_read,
  3047. [OP_READDIR] = (nfsd4_enc)nfsd4_encode_readdir,
  3048. [OP_READLINK] = (nfsd4_enc)nfsd4_encode_readlink,
  3049. [OP_REMOVE] = (nfsd4_enc)nfsd4_encode_remove,
  3050. [OP_RENAME] = (nfsd4_enc)nfsd4_encode_rename,
  3051. [OP_RENEW] = (nfsd4_enc)nfsd4_encode_noop,
  3052. [OP_RESTOREFH] = (nfsd4_enc)nfsd4_encode_noop,
  3053. [OP_SAVEFH] = (nfsd4_enc)nfsd4_encode_noop,
  3054. [OP_SECINFO] = (nfsd4_enc)nfsd4_encode_secinfo,
  3055. [OP_SETATTR] = (nfsd4_enc)nfsd4_encode_setattr,
  3056. [OP_SETCLIENTID] = (nfsd4_enc)nfsd4_encode_setclientid,
  3057. [OP_SETCLIENTID_CONFIRM] = (nfsd4_enc)nfsd4_encode_noop,
  3058. [OP_VERIFY] = (nfsd4_enc)nfsd4_encode_noop,
  3059. [OP_WRITE] = (nfsd4_enc)nfsd4_encode_write,
  3060. [OP_RELEASE_LOCKOWNER] = (nfsd4_enc)nfsd4_encode_noop,
  3061. /* NFSv4.1 operations */
  3062. [OP_BACKCHANNEL_CTL] = (nfsd4_enc)nfsd4_encode_noop,
  3063. [OP_BIND_CONN_TO_SESSION] = (nfsd4_enc)nfsd4_encode_bind_conn_to_session,
  3064. [OP_EXCHANGE_ID] = (nfsd4_enc)nfsd4_encode_exchange_id,
  3065. [OP_CREATE_SESSION] = (nfsd4_enc)nfsd4_encode_create_session,
  3066. [OP_DESTROY_SESSION] = (nfsd4_enc)nfsd4_encode_destroy_session,
  3067. [OP_FREE_STATEID] = (nfsd4_enc)nfsd4_encode_free_stateid,
  3068. [OP_GET_DIR_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
  3069. [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_noop,
  3070. [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
  3071. [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_noop,
  3072. [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_noop,
  3073. [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_noop,
  3074. [OP_SECINFO_NO_NAME] = (nfsd4_enc)nfsd4_encode_secinfo_no_name,
  3075. [OP_SEQUENCE] = (nfsd4_enc)nfsd4_encode_sequence,
  3076. [OP_SET_SSV] = (nfsd4_enc)nfsd4_encode_noop,
  3077. [OP_TEST_STATEID] = (nfsd4_enc)nfsd4_encode_test_stateid,
  3078. [OP_WANT_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
  3079. [OP_DESTROY_CLIENTID] = (nfsd4_enc)nfsd4_encode_noop,
  3080. [OP_RECLAIM_COMPLETE] = (nfsd4_enc)nfsd4_encode_noop,
  3081. };
  3082. /*
  3083. * Calculate the total amount of memory that the compound response has taken
  3084. * after encoding the current operation with pad.
  3085. *
  3086. * pad: if operation is non-idempotent, pad was calculate by op_rsize_bop()
  3087. * which was specified at nfsd4_operation, else pad is zero.
  3088. *
  3089. * Compare this length to the session se_fmaxresp_sz and se_fmaxresp_cached.
  3090. *
  3091. * Our se_fmaxresp_cached will always be a multiple of PAGE_SIZE, and so
  3092. * will be at least a page and will therefore hold the xdr_buf head.
  3093. */
  3094. __be32 nfsd4_check_resp_size(struct nfsd4_compoundres *resp, u32 pad)
  3095. {
  3096. struct xdr_buf *xb = &resp->rqstp->rq_res;
  3097. struct nfsd4_session *session = NULL;
  3098. struct nfsd4_slot *slot = resp->cstate.slot;
  3099. u32 length, tlen = 0;
  3100. if (!nfsd4_has_session(&resp->cstate))
  3101. return 0;
  3102. session = resp->cstate.session;
  3103. if (session == NULL)
  3104. return 0;
  3105. if (xb->page_len == 0) {
  3106. length = (char *)resp->p - (char *)xb->head[0].iov_base + pad;
  3107. } else {
  3108. if (xb->tail[0].iov_base && xb->tail[0].iov_len > 0)
  3109. tlen = (char *)resp->p - (char *)xb->tail[0].iov_base;
  3110. length = xb->head[0].iov_len + xb->page_len + tlen + pad;
  3111. }
  3112. dprintk("%s length %u, xb->page_len %u tlen %u pad %u\n", __func__,
  3113. length, xb->page_len, tlen, pad);
  3114. if (length > session->se_fchannel.maxresp_sz)
  3115. return nfserr_rep_too_big;
  3116. if ((slot->sl_flags & NFSD4_SLOT_CACHETHIS) &&
  3117. length > session->se_fchannel.maxresp_cached)
  3118. return nfserr_rep_too_big_to_cache;
  3119. return 0;
  3120. }
  3121. void
  3122. nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
  3123. {
  3124. struct nfs4_stateowner *so = resp->cstate.replay_owner;
  3125. __be32 *statp;
  3126. __be32 *p;
  3127. RESERVE_SPACE(8);
  3128. WRITE32(op->opnum);
  3129. statp = p++; /* to be backfilled at the end */
  3130. ADJUST_ARGS();
  3131. if (op->opnum == OP_ILLEGAL)
  3132. goto status;
  3133. BUG_ON(op->opnum < 0 || op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
  3134. !nfsd4_enc_ops[op->opnum]);
  3135. op->status = nfsd4_enc_ops[op->opnum](resp, op->status, &op->u);
  3136. /* nfsd4_check_drc_limit guarantees enough room for error status */
  3137. if (!op->status)
  3138. op->status = nfsd4_check_resp_size(resp, 0);
  3139. if (so) {
  3140. so->so_replay.rp_status = op->status;
  3141. so->so_replay.rp_buflen = (char *)resp->p - (char *)(statp+1);
  3142. memcpy(so->so_replay.rp_buf, statp+1, so->so_replay.rp_buflen);
  3143. }
  3144. status:
  3145. /*
  3146. * Note: We write the status directly, instead of using WRITE32(),
  3147. * since it is already in network byte order.
  3148. */
  3149. *statp = op->status;
  3150. }
  3151. /*
  3152. * Encode the reply stored in the stateowner reply cache
  3153. *
  3154. * XDR note: do not encode rp->rp_buflen: the buffer contains the
  3155. * previously sent already encoded operation.
  3156. *
  3157. * called with nfs4_lock_state() held
  3158. */
  3159. void
  3160. nfsd4_encode_replay(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
  3161. {
  3162. __be32 *p;
  3163. struct nfs4_replay *rp = op->replay;
  3164. BUG_ON(!rp);
  3165. RESERVE_SPACE(8);
  3166. WRITE32(op->opnum);
  3167. *p++ = rp->rp_status; /* already xdr'ed */
  3168. ADJUST_ARGS();
  3169. RESERVE_SPACE(rp->rp_buflen);
  3170. WRITEMEM(rp->rp_buf, rp->rp_buflen);
  3171. ADJUST_ARGS();
  3172. }
  3173. int
  3174. nfs4svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
  3175. {
  3176. return xdr_ressize_check(rqstp, p);
  3177. }
  3178. int nfsd4_release_compoundargs(void *rq, __be32 *p, void *resp)
  3179. {
  3180. struct svc_rqst *rqstp = rq;
  3181. struct nfsd4_compoundargs *args = rqstp->rq_argp;
  3182. if (args->ops != args->iops) {
  3183. kfree(args->ops);
  3184. args->ops = args->iops;
  3185. }
  3186. kfree(args->tmpp);
  3187. args->tmpp = NULL;
  3188. while (args->to_free) {
  3189. struct tmpbuf *tb = args->to_free;
  3190. args->to_free = tb->next;
  3191. tb->release(tb->buf);
  3192. kfree(tb);
  3193. }
  3194. return 1;
  3195. }
  3196. int
  3197. nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundargs *args)
  3198. {
  3199. args->p = p;
  3200. args->end = rqstp->rq_arg.head[0].iov_base + rqstp->rq_arg.head[0].iov_len;
  3201. args->pagelist = rqstp->rq_arg.pages;
  3202. args->pagelen = rqstp->rq_arg.page_len;
  3203. args->tmpp = NULL;
  3204. args->to_free = NULL;
  3205. args->ops = args->iops;
  3206. args->rqstp = rqstp;
  3207. return !nfsd4_decode_compound(args);
  3208. }
  3209. int
  3210. nfs4svc_encode_compoundres(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundres *resp)
  3211. {
  3212. /*
  3213. * All that remains is to write the tag and operation count...
  3214. */
  3215. struct nfsd4_compound_state *cs = &resp->cstate;
  3216. struct kvec *iov;
  3217. p = resp->tagp;
  3218. *p++ = htonl(resp->taglen);
  3219. memcpy(p, resp->tag, resp->taglen);
  3220. p += XDR_QUADLEN(resp->taglen);
  3221. *p++ = htonl(resp->opcnt);
  3222. if (rqstp->rq_res.page_len)
  3223. iov = &rqstp->rq_res.tail[0];
  3224. else
  3225. iov = &rqstp->rq_res.head[0];
  3226. iov->iov_len = ((char*)resp->p) - (char*)iov->iov_base;
  3227. BUG_ON(iov->iov_len > PAGE_SIZE);
  3228. if (nfsd4_has_session(cs)) {
  3229. if (cs->status != nfserr_replay_cache) {
  3230. nfsd4_store_cache_entry(resp);
  3231. cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
  3232. }
  3233. /* Renew the clientid on success and on replay */
  3234. put_client_renew(cs->session->se_client);
  3235. nfsd4_put_session(cs->session);
  3236. }
  3237. return 1;
  3238. }
  3239. /*
  3240. * Local variables:
  3241. * c-basic-offset: 8
  3242. * End:
  3243. */