nfs4xdr.c 91 KB

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