nfs4xdr.c 92 KB

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