nfs4xdr.c 102 KB

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