nfs4xdr.c 87 KB

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