nfs4xdr.c 92 KB

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