nfs4xdr.c 102 KB

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