nfs4xdr.c 104 KB

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