nfs4xdr.c 91 KB

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