nfs4xdr.c 114 KB

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