nfs4xdr.c 115 KB

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