nfs4xdr.c 101 KB

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