nfs4xdr.c 109 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374
  1. /*
  2. * fs/nfs/nfs4xdr.c
  3. *
  4. * Client-side XDR for NFSv4.
  5. *
  6. * Copyright (c) 2002 The Regents of the University of Michigan.
  7. * All rights reserved.
  8. *
  9. * Kendrick Smith <kmsmith@umich.edu>
  10. * Andy Adamson <andros@umich.edu>
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. *
  16. * 1. Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. * 2. Redistributions in binary form must reproduce the above copyright
  19. * notice, this list of conditions and the following disclaimer in the
  20. * documentation and/or other materials provided with the distribution.
  21. * 3. Neither the name of the University nor the names of its
  22. * contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  26. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  27. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  32. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  33. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #include <linux/param.h>
  38. #include <linux/time.h>
  39. #include <linux/mm.h>
  40. #include <linux/slab.h>
  41. #include <linux/utsname.h>
  42. #include <linux/errno.h>
  43. #include <linux/string.h>
  44. #include <linux/in.h>
  45. #include <linux/pagemap.h>
  46. #include <linux/proc_fs.h>
  47. #include <linux/kdev_t.h>
  48. #include <linux/sunrpc/clnt.h>
  49. #include <linux/nfs.h>
  50. #include <linux/nfs4.h>
  51. #include <linux/nfs_fs.h>
  52. #include <linux/nfs_idmap.h>
  53. #include "nfs4_fs.h"
  54. #define NFSDBG_FACILITY NFSDBG_XDR
  55. /* Mapping from NFS error code to "errno" error code. */
  56. #define errno_NFSERR_IO EIO
  57. static int nfs_stat_to_errno(int);
  58. /* NFSv4 COMPOUND tags are only wanted for debugging purposes */
  59. #ifdef DEBUG
  60. #define NFS4_MAXTAGLEN 20
  61. #else
  62. #define NFS4_MAXTAGLEN 0
  63. #endif
  64. /* lock,open owner id:
  65. * we currently use size 1 (u32) out of (NFS4_OPAQUE_LIMIT >> 2)
  66. */
  67. #define owner_id_maxsz (1 + 1)
  68. #define compound_encode_hdr_maxsz (3 + (NFS4_MAXTAGLEN >> 2))
  69. #define compound_decode_hdr_maxsz (3 + (NFS4_MAXTAGLEN >> 2))
  70. #define op_encode_hdr_maxsz (1)
  71. #define op_decode_hdr_maxsz (2)
  72. #define encode_putfh_maxsz (op_encode_hdr_maxsz + 1 + \
  73. (NFS4_FHSIZE >> 2))
  74. #define decode_putfh_maxsz (op_decode_hdr_maxsz)
  75. #define encode_putrootfh_maxsz (op_encode_hdr_maxsz)
  76. #define decode_putrootfh_maxsz (op_decode_hdr_maxsz)
  77. #define encode_getfh_maxsz (op_encode_hdr_maxsz)
  78. #define decode_getfh_maxsz (op_decode_hdr_maxsz + 1 + \
  79. ((3+NFS4_FHSIZE) >> 2))
  80. #define nfs4_fattr_bitmap_maxsz 3
  81. #define encode_getattr_maxsz (op_encode_hdr_maxsz + nfs4_fattr_bitmap_maxsz)
  82. #define nfs4_name_maxsz (1 + ((3 + NFS4_MAXNAMLEN) >> 2))
  83. #define nfs4_path_maxsz (1 + ((3 + NFS4_MAXPATHLEN) >> 2))
  84. /* This is based on getfattr, which uses the most attributes: */
  85. #define nfs4_fattr_value_maxsz (1 + (1 + 2 + 2 + 4 + 2 + 1 + 1 + 2 + 2 + \
  86. 3 + 3 + 3 + 2 * nfs4_name_maxsz))
  87. #define nfs4_fattr_maxsz (nfs4_fattr_bitmap_maxsz + \
  88. nfs4_fattr_value_maxsz)
  89. #define decode_getattr_maxsz (op_decode_hdr_maxsz + nfs4_fattr_maxsz)
  90. #define encode_savefh_maxsz (op_encode_hdr_maxsz)
  91. #define decode_savefh_maxsz (op_decode_hdr_maxsz)
  92. #define encode_restorefh_maxsz (op_encode_hdr_maxsz)
  93. #define decode_restorefh_maxsz (op_decode_hdr_maxsz)
  94. #define encode_fsinfo_maxsz (op_encode_hdr_maxsz + 2)
  95. #define decode_fsinfo_maxsz (op_decode_hdr_maxsz + 11)
  96. #define encode_renew_maxsz (op_encode_hdr_maxsz + 3)
  97. #define decode_renew_maxsz (op_decode_hdr_maxsz)
  98. #define encode_setclientid_maxsz \
  99. (op_encode_hdr_maxsz + \
  100. 4 /*server->ip_addr*/ + \
  101. 1 /*Netid*/ + \
  102. 6 /*uaddr*/ + \
  103. 6 + (NFS4_VERIFIER_SIZE >> 2))
  104. #define decode_setclientid_maxsz \
  105. (op_decode_hdr_maxsz + \
  106. 2 + \
  107. 1024) /* large value for CLID_INUSE */
  108. #define encode_setclientid_confirm_maxsz \
  109. (op_encode_hdr_maxsz + \
  110. 3 + (NFS4_VERIFIER_SIZE >> 2))
  111. #define decode_setclientid_confirm_maxsz \
  112. (op_decode_hdr_maxsz)
  113. #define encode_lookup_maxsz (op_encode_hdr_maxsz + \
  114. 1 + ((3 + NFS4_FHSIZE) >> 2))
  115. #define encode_remove_maxsz (op_encode_hdr_maxsz + \
  116. nfs4_name_maxsz)
  117. #define encode_rename_maxsz (op_encode_hdr_maxsz + \
  118. 2 * nfs4_name_maxsz)
  119. #define decode_rename_maxsz (op_decode_hdr_maxsz + 5 + 5)
  120. #define encode_link_maxsz (op_encode_hdr_maxsz + \
  121. nfs4_name_maxsz)
  122. #define decode_link_maxsz (op_decode_hdr_maxsz + 5)
  123. #define encode_symlink_maxsz (op_encode_hdr_maxsz + \
  124. 1 + nfs4_name_maxsz + \
  125. nfs4_path_maxsz + \
  126. nfs4_fattr_maxsz)
  127. #define decode_symlink_maxsz (op_decode_hdr_maxsz + 8)
  128. #define encode_create_maxsz (op_encode_hdr_maxsz + \
  129. 2 + nfs4_name_maxsz + \
  130. nfs4_fattr_maxsz)
  131. #define decode_create_maxsz (op_decode_hdr_maxsz + 8)
  132. #define encode_delegreturn_maxsz (op_encode_hdr_maxsz + 4)
  133. #define decode_delegreturn_maxsz (op_decode_hdr_maxsz)
  134. #define NFS4_enc_compound_sz (1024) /* XXX: large enough? */
  135. #define NFS4_dec_compound_sz (1024) /* XXX: large enough? */
  136. #define NFS4_enc_read_sz (compound_encode_hdr_maxsz + \
  137. encode_putfh_maxsz + \
  138. op_encode_hdr_maxsz + 7)
  139. #define NFS4_dec_read_sz (compound_decode_hdr_maxsz + \
  140. decode_putfh_maxsz + \
  141. op_decode_hdr_maxsz + 2)
  142. #define NFS4_enc_readlink_sz (compound_encode_hdr_maxsz + \
  143. encode_putfh_maxsz + \
  144. op_encode_hdr_maxsz)
  145. #define NFS4_dec_readlink_sz (compound_decode_hdr_maxsz + \
  146. decode_putfh_maxsz + \
  147. op_decode_hdr_maxsz)
  148. #define NFS4_enc_readdir_sz (compound_encode_hdr_maxsz + \
  149. encode_putfh_maxsz + \
  150. op_encode_hdr_maxsz + 9)
  151. #define NFS4_dec_readdir_sz (compound_decode_hdr_maxsz + \
  152. decode_putfh_maxsz + \
  153. op_decode_hdr_maxsz + 2)
  154. #define NFS4_enc_write_sz (compound_encode_hdr_maxsz + \
  155. encode_putfh_maxsz + \
  156. op_encode_hdr_maxsz + 8 + \
  157. encode_getattr_maxsz)
  158. #define NFS4_dec_write_sz (compound_decode_hdr_maxsz + \
  159. decode_putfh_maxsz + \
  160. op_decode_hdr_maxsz + 4 + \
  161. decode_getattr_maxsz)
  162. #define NFS4_enc_commit_sz (compound_encode_hdr_maxsz + \
  163. encode_putfh_maxsz + \
  164. op_encode_hdr_maxsz + 3 + \
  165. encode_getattr_maxsz)
  166. #define NFS4_dec_commit_sz (compound_decode_hdr_maxsz + \
  167. decode_putfh_maxsz + \
  168. op_decode_hdr_maxsz + 2 + \
  169. decode_getattr_maxsz)
  170. #define NFS4_enc_open_sz (compound_encode_hdr_maxsz + \
  171. encode_putfh_maxsz + \
  172. op_encode_hdr_maxsz + \
  173. 13 + 3 + 2 + 64 + \
  174. encode_getattr_maxsz + \
  175. encode_getfh_maxsz)
  176. #define NFS4_dec_open_sz (compound_decode_hdr_maxsz + \
  177. decode_putfh_maxsz + \
  178. op_decode_hdr_maxsz + 4 + 5 + 2 + 3 + \
  179. decode_getattr_maxsz + \
  180. decode_getfh_maxsz)
  181. #define NFS4_enc_open_confirm_sz \
  182. (compound_encode_hdr_maxsz + \
  183. encode_putfh_maxsz + \
  184. op_encode_hdr_maxsz + 5)
  185. #define NFS4_dec_open_confirm_sz (compound_decode_hdr_maxsz + \
  186. decode_putfh_maxsz + \
  187. op_decode_hdr_maxsz + 4)
  188. #define NFS4_enc_open_noattr_sz (compound_encode_hdr_maxsz + \
  189. encode_putfh_maxsz + \
  190. op_encode_hdr_maxsz + \
  191. 11)
  192. #define NFS4_dec_open_noattr_sz (compound_decode_hdr_maxsz + \
  193. decode_putfh_maxsz + \
  194. op_decode_hdr_maxsz + \
  195. 4 + 5 + 2 + 3)
  196. #define NFS4_enc_open_downgrade_sz \
  197. (compound_encode_hdr_maxsz + \
  198. encode_putfh_maxsz + \
  199. op_encode_hdr_maxsz + 7 + \
  200. encode_getattr_maxsz)
  201. #define NFS4_dec_open_downgrade_sz \
  202. (compound_decode_hdr_maxsz + \
  203. decode_putfh_maxsz + \
  204. op_decode_hdr_maxsz + 4 + \
  205. decode_getattr_maxsz)
  206. #define NFS4_enc_close_sz (compound_encode_hdr_maxsz + \
  207. encode_putfh_maxsz + \
  208. op_encode_hdr_maxsz + 5 + \
  209. encode_getattr_maxsz)
  210. #define NFS4_dec_close_sz (compound_decode_hdr_maxsz + \
  211. decode_putfh_maxsz + \
  212. op_decode_hdr_maxsz + 4 + \
  213. decode_getattr_maxsz)
  214. #define NFS4_enc_setattr_sz (compound_encode_hdr_maxsz + \
  215. encode_putfh_maxsz + \
  216. op_encode_hdr_maxsz + 4 + \
  217. nfs4_fattr_maxsz + \
  218. encode_getattr_maxsz)
  219. #define NFS4_dec_setattr_sz (compound_decode_hdr_maxsz + \
  220. decode_putfh_maxsz + \
  221. op_decode_hdr_maxsz + 3)
  222. #define NFS4_enc_fsinfo_sz (compound_encode_hdr_maxsz + \
  223. encode_putfh_maxsz + \
  224. encode_fsinfo_maxsz)
  225. #define NFS4_dec_fsinfo_sz (compound_decode_hdr_maxsz + \
  226. decode_putfh_maxsz + \
  227. decode_fsinfo_maxsz)
  228. #define NFS4_enc_renew_sz (compound_encode_hdr_maxsz + \
  229. encode_renew_maxsz)
  230. #define NFS4_dec_renew_sz (compound_decode_hdr_maxsz + \
  231. decode_renew_maxsz)
  232. #define NFS4_enc_setclientid_sz (compound_encode_hdr_maxsz + \
  233. encode_setclientid_maxsz)
  234. #define NFS4_dec_setclientid_sz (compound_decode_hdr_maxsz + \
  235. decode_setclientid_maxsz)
  236. #define NFS4_enc_setclientid_confirm_sz \
  237. (compound_encode_hdr_maxsz + \
  238. encode_setclientid_confirm_maxsz + \
  239. encode_putrootfh_maxsz + \
  240. encode_fsinfo_maxsz)
  241. #define NFS4_dec_setclientid_confirm_sz \
  242. (compound_decode_hdr_maxsz + \
  243. decode_setclientid_confirm_maxsz + \
  244. decode_putrootfh_maxsz + \
  245. decode_fsinfo_maxsz)
  246. #define NFS4_enc_lock_sz (compound_encode_hdr_maxsz + \
  247. encode_putfh_maxsz + \
  248. encode_getattr_maxsz + \
  249. op_encode_hdr_maxsz + \
  250. 1 + 1 + 2 + 2 + \
  251. 1 + 4 + 1 + 2 + \
  252. owner_id_maxsz)
  253. #define NFS4_dec_lock_sz (compound_decode_hdr_maxsz + \
  254. decode_putfh_maxsz + \
  255. decode_getattr_maxsz + \
  256. op_decode_hdr_maxsz + \
  257. 2 + 2 + 1 + 2 + \
  258. owner_id_maxsz)
  259. #define NFS4_enc_lockt_sz (compound_encode_hdr_maxsz + \
  260. encode_putfh_maxsz + \
  261. encode_getattr_maxsz + \
  262. op_encode_hdr_maxsz + \
  263. 1 + 2 + 2 + 2 + \
  264. owner_id_maxsz)
  265. #define NFS4_dec_lockt_sz (NFS4_dec_lock_sz)
  266. #define NFS4_enc_locku_sz (compound_encode_hdr_maxsz + \
  267. encode_putfh_maxsz + \
  268. encode_getattr_maxsz + \
  269. op_encode_hdr_maxsz + \
  270. 1 + 1 + 4 + 2 + 2)
  271. #define NFS4_dec_locku_sz (compound_decode_hdr_maxsz + \
  272. decode_putfh_maxsz + \
  273. decode_getattr_maxsz + \
  274. op_decode_hdr_maxsz + 4)
  275. #define NFS4_enc_access_sz (compound_encode_hdr_maxsz + \
  276. encode_putfh_maxsz + \
  277. op_encode_hdr_maxsz + 1)
  278. #define NFS4_dec_access_sz (compound_decode_hdr_maxsz + \
  279. decode_putfh_maxsz + \
  280. op_decode_hdr_maxsz + 2)
  281. #define NFS4_enc_getattr_sz (compound_encode_hdr_maxsz + \
  282. encode_putfh_maxsz + \
  283. encode_getattr_maxsz)
  284. #define NFS4_dec_getattr_sz (compound_decode_hdr_maxsz + \
  285. decode_putfh_maxsz + \
  286. decode_getattr_maxsz)
  287. #define NFS4_enc_lookup_sz (compound_encode_hdr_maxsz + \
  288. encode_putfh_maxsz + \
  289. encode_lookup_maxsz + \
  290. encode_getattr_maxsz + \
  291. encode_getfh_maxsz)
  292. #define NFS4_dec_lookup_sz (compound_decode_hdr_maxsz + \
  293. decode_putfh_maxsz + \
  294. op_decode_hdr_maxsz + \
  295. decode_getattr_maxsz + \
  296. decode_getfh_maxsz)
  297. #define NFS4_enc_lookup_root_sz (compound_encode_hdr_maxsz + \
  298. encode_putrootfh_maxsz + \
  299. encode_getattr_maxsz + \
  300. encode_getfh_maxsz)
  301. #define NFS4_dec_lookup_root_sz (compound_decode_hdr_maxsz + \
  302. decode_putrootfh_maxsz + \
  303. decode_getattr_maxsz + \
  304. decode_getfh_maxsz)
  305. #define NFS4_enc_remove_sz (compound_encode_hdr_maxsz + \
  306. encode_putfh_maxsz + \
  307. encode_remove_maxsz + \
  308. encode_getattr_maxsz)
  309. #define NFS4_dec_remove_sz (compound_decode_hdr_maxsz + \
  310. decode_putfh_maxsz + \
  311. op_decode_hdr_maxsz + 5 + \
  312. decode_getattr_maxsz)
  313. #define NFS4_enc_rename_sz (compound_encode_hdr_maxsz + \
  314. encode_putfh_maxsz + \
  315. encode_savefh_maxsz + \
  316. encode_putfh_maxsz + \
  317. encode_rename_maxsz + \
  318. encode_getattr_maxsz + \
  319. encode_restorefh_maxsz + \
  320. encode_getattr_maxsz)
  321. #define NFS4_dec_rename_sz (compound_decode_hdr_maxsz + \
  322. decode_putfh_maxsz + \
  323. decode_savefh_maxsz + \
  324. decode_putfh_maxsz + \
  325. decode_rename_maxsz + \
  326. decode_getattr_maxsz + \
  327. decode_restorefh_maxsz + \
  328. decode_getattr_maxsz)
  329. #define NFS4_enc_link_sz (compound_encode_hdr_maxsz + \
  330. encode_putfh_maxsz + \
  331. encode_savefh_maxsz + \
  332. encode_putfh_maxsz + \
  333. encode_link_maxsz + \
  334. decode_getattr_maxsz + \
  335. encode_restorefh_maxsz + \
  336. decode_getattr_maxsz)
  337. #define NFS4_dec_link_sz (compound_decode_hdr_maxsz + \
  338. decode_putfh_maxsz + \
  339. decode_savefh_maxsz + \
  340. decode_putfh_maxsz + \
  341. decode_link_maxsz + \
  342. decode_getattr_maxsz + \
  343. decode_restorefh_maxsz + \
  344. decode_getattr_maxsz)
  345. #define NFS4_enc_symlink_sz (compound_encode_hdr_maxsz + \
  346. encode_putfh_maxsz + \
  347. encode_symlink_maxsz + \
  348. encode_getattr_maxsz + \
  349. encode_getfh_maxsz)
  350. #define NFS4_dec_symlink_sz (compound_decode_hdr_maxsz + \
  351. decode_putfh_maxsz + \
  352. decode_symlink_maxsz + \
  353. decode_getattr_maxsz + \
  354. decode_getfh_maxsz)
  355. #define NFS4_enc_create_sz (compound_encode_hdr_maxsz + \
  356. encode_putfh_maxsz + \
  357. encode_savefh_maxsz + \
  358. encode_create_maxsz + \
  359. encode_getfh_maxsz + \
  360. encode_getattr_maxsz + \
  361. encode_restorefh_maxsz + \
  362. encode_getattr_maxsz)
  363. #define NFS4_dec_create_sz (compound_decode_hdr_maxsz + \
  364. decode_putfh_maxsz + \
  365. decode_savefh_maxsz + \
  366. decode_create_maxsz + \
  367. decode_getfh_maxsz + \
  368. decode_getattr_maxsz + \
  369. decode_restorefh_maxsz + \
  370. decode_getattr_maxsz)
  371. #define NFS4_enc_pathconf_sz (compound_encode_hdr_maxsz + \
  372. encode_putfh_maxsz + \
  373. encode_getattr_maxsz)
  374. #define NFS4_dec_pathconf_sz (compound_decode_hdr_maxsz + \
  375. decode_putfh_maxsz + \
  376. decode_getattr_maxsz)
  377. #define NFS4_enc_statfs_sz (compound_encode_hdr_maxsz + \
  378. encode_putfh_maxsz + \
  379. encode_getattr_maxsz)
  380. #define NFS4_dec_statfs_sz (compound_decode_hdr_maxsz + \
  381. decode_putfh_maxsz + \
  382. op_decode_hdr_maxsz + 12)
  383. #define NFS4_enc_server_caps_sz (compound_encode_hdr_maxsz + \
  384. encode_getattr_maxsz)
  385. #define NFS4_dec_server_caps_sz (compound_decode_hdr_maxsz + \
  386. decode_getattr_maxsz)
  387. #define NFS4_enc_delegreturn_sz (compound_encode_hdr_maxsz + \
  388. encode_putfh_maxsz + \
  389. encode_delegreturn_maxsz)
  390. #define NFS4_dec_delegreturn_sz (compound_decode_hdr_maxsz + \
  391. decode_delegreturn_maxsz)
  392. #define NFS4_enc_getacl_sz (compound_encode_hdr_maxsz + \
  393. encode_putfh_maxsz + \
  394. encode_getattr_maxsz)
  395. #define NFS4_dec_getacl_sz (compound_decode_hdr_maxsz + \
  396. decode_putfh_maxsz + \
  397. op_decode_hdr_maxsz + \
  398. nfs4_fattr_bitmap_maxsz + 1)
  399. #define NFS4_enc_setacl_sz (compound_encode_hdr_maxsz + \
  400. encode_putfh_maxsz + \
  401. op_encode_hdr_maxsz + 4 + \
  402. nfs4_fattr_bitmap_maxsz + 1)
  403. #define NFS4_dec_setacl_sz (compound_decode_hdr_maxsz + \
  404. decode_putfh_maxsz + \
  405. op_decode_hdr_maxsz + nfs4_fattr_bitmap_maxsz)
  406. static struct {
  407. unsigned int mode;
  408. unsigned int nfs2type;
  409. } nfs_type2fmt[] = {
  410. { 0, NFNON },
  411. { S_IFREG, NFREG },
  412. { S_IFDIR, NFDIR },
  413. { S_IFBLK, NFBLK },
  414. { S_IFCHR, NFCHR },
  415. { S_IFLNK, NFLNK },
  416. { S_IFSOCK, NFSOCK },
  417. { S_IFIFO, NFFIFO },
  418. { 0, NFNON },
  419. { 0, NFNON },
  420. };
  421. struct compound_hdr {
  422. int32_t status;
  423. uint32_t nops;
  424. uint32_t taglen;
  425. char * tag;
  426. };
  427. /*
  428. * START OF "GENERIC" ENCODE ROUTINES.
  429. * These may look a little ugly since they are imported from a "generic"
  430. * set of XDR encode/decode routines which are intended to be shared by
  431. * all of our NFSv4 implementations (OpenBSD, MacOS X...).
  432. *
  433. * If the pain of reading these is too great, it should be a straightforward
  434. * task to translate them into Linux-specific versions which are more
  435. * consistent with the style used in NFSv2/v3...
  436. */
  437. #define WRITE32(n) *p++ = htonl(n)
  438. #define WRITE64(n) do { \
  439. *p++ = htonl((uint32_t)((n) >> 32)); \
  440. *p++ = htonl((uint32_t)(n)); \
  441. } while (0)
  442. #define WRITEMEM(ptr,nbytes) do { \
  443. p = xdr_encode_opaque_fixed(p, ptr, nbytes); \
  444. } while (0)
  445. #define RESERVE_SPACE(nbytes) do { \
  446. p = xdr_reserve_space(xdr, nbytes); \
  447. if (!p) printk("RESERVE_SPACE(%d) failed in function %s\n", (int) (nbytes), __FUNCTION__); \
  448. BUG_ON(!p); \
  449. } while (0)
  450. static void encode_string(struct xdr_stream *xdr, unsigned int len, const char *str)
  451. {
  452. uint32_t *p;
  453. p = xdr_reserve_space(xdr, 4 + len);
  454. BUG_ON(p == NULL);
  455. xdr_encode_opaque(p, str, len);
  456. }
  457. static int encode_compound_hdr(struct xdr_stream *xdr, struct compound_hdr *hdr)
  458. {
  459. uint32_t *p;
  460. dprintk("encode_compound: tag=%.*s\n", (int)hdr->taglen, hdr->tag);
  461. BUG_ON(hdr->taglen > NFS4_MAXTAGLEN);
  462. RESERVE_SPACE(12+(XDR_QUADLEN(hdr->taglen)<<2));
  463. WRITE32(hdr->taglen);
  464. WRITEMEM(hdr->tag, hdr->taglen);
  465. WRITE32(NFS4_MINOR_VERSION);
  466. WRITE32(hdr->nops);
  467. return 0;
  468. }
  469. static void encode_nfs4_verifier(struct xdr_stream *xdr, const nfs4_verifier *verf)
  470. {
  471. uint32_t *p;
  472. p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
  473. BUG_ON(p == NULL);
  474. xdr_encode_opaque_fixed(p, verf->data, NFS4_VERIFIER_SIZE);
  475. }
  476. static int encode_attrs(struct xdr_stream *xdr, const struct iattr *iap, const struct nfs_server *server)
  477. {
  478. char owner_name[IDMAP_NAMESZ];
  479. char owner_group[IDMAP_NAMESZ];
  480. int owner_namelen = 0;
  481. int owner_grouplen = 0;
  482. uint32_t *p;
  483. uint32_t *q;
  484. int len;
  485. uint32_t bmval0 = 0;
  486. uint32_t bmval1 = 0;
  487. int status;
  488. /*
  489. * We reserve enough space to write the entire attribute buffer at once.
  490. * In the worst-case, this would be
  491. * 12(bitmap) + 4(attrlen) + 8(size) + 4(mode) + 4(atime) + 4(mtime)
  492. * = 36 bytes, plus any contribution from variable-length fields
  493. * such as owner/group.
  494. */
  495. len = 16;
  496. /* Sigh */
  497. if (iap->ia_valid & ATTR_SIZE)
  498. len += 8;
  499. if (iap->ia_valid & ATTR_MODE)
  500. len += 4;
  501. if (iap->ia_valid & ATTR_UID) {
  502. owner_namelen = nfs_map_uid_to_name(server->nfs4_state, iap->ia_uid, owner_name);
  503. if (owner_namelen < 0) {
  504. printk(KERN_WARNING "nfs: couldn't resolve uid %d to string\n",
  505. iap->ia_uid);
  506. /* XXX */
  507. strcpy(owner_name, "nobody");
  508. owner_namelen = sizeof("nobody") - 1;
  509. /* goto out; */
  510. }
  511. len += 4 + (XDR_QUADLEN(owner_namelen) << 2);
  512. }
  513. if (iap->ia_valid & ATTR_GID) {
  514. owner_grouplen = nfs_map_gid_to_group(server->nfs4_state, iap->ia_gid, owner_group);
  515. if (owner_grouplen < 0) {
  516. printk(KERN_WARNING "nfs4: couldn't resolve gid %d to string\n",
  517. iap->ia_gid);
  518. strcpy(owner_group, "nobody");
  519. owner_grouplen = sizeof("nobody") - 1;
  520. /* goto out; */
  521. }
  522. len += 4 + (XDR_QUADLEN(owner_grouplen) << 2);
  523. }
  524. if (iap->ia_valid & ATTR_ATIME_SET)
  525. len += 16;
  526. else if (iap->ia_valid & ATTR_ATIME)
  527. len += 4;
  528. if (iap->ia_valid & ATTR_MTIME_SET)
  529. len += 16;
  530. else if (iap->ia_valid & ATTR_MTIME)
  531. len += 4;
  532. RESERVE_SPACE(len);
  533. /*
  534. * We write the bitmap length now, but leave the bitmap and the attribute
  535. * buffer length to be backfilled at the end of this routine.
  536. */
  537. WRITE32(2);
  538. q = p;
  539. p += 3;
  540. if (iap->ia_valid & ATTR_SIZE) {
  541. bmval0 |= FATTR4_WORD0_SIZE;
  542. WRITE64(iap->ia_size);
  543. }
  544. if (iap->ia_valid & ATTR_MODE) {
  545. bmval1 |= FATTR4_WORD1_MODE;
  546. WRITE32(iap->ia_mode);
  547. }
  548. if (iap->ia_valid & ATTR_UID) {
  549. bmval1 |= FATTR4_WORD1_OWNER;
  550. WRITE32(owner_namelen);
  551. WRITEMEM(owner_name, owner_namelen);
  552. }
  553. if (iap->ia_valid & ATTR_GID) {
  554. bmval1 |= FATTR4_WORD1_OWNER_GROUP;
  555. WRITE32(owner_grouplen);
  556. WRITEMEM(owner_group, owner_grouplen);
  557. }
  558. if (iap->ia_valid & ATTR_ATIME_SET) {
  559. bmval1 |= FATTR4_WORD1_TIME_ACCESS_SET;
  560. WRITE32(NFS4_SET_TO_CLIENT_TIME);
  561. WRITE32(0);
  562. WRITE32(iap->ia_mtime.tv_sec);
  563. WRITE32(iap->ia_mtime.tv_nsec);
  564. }
  565. else if (iap->ia_valid & ATTR_ATIME) {
  566. bmval1 |= FATTR4_WORD1_TIME_ACCESS_SET;
  567. WRITE32(NFS4_SET_TO_SERVER_TIME);
  568. }
  569. if (iap->ia_valid & ATTR_MTIME_SET) {
  570. bmval1 |= FATTR4_WORD1_TIME_MODIFY_SET;
  571. WRITE32(NFS4_SET_TO_CLIENT_TIME);
  572. WRITE32(0);
  573. WRITE32(iap->ia_mtime.tv_sec);
  574. WRITE32(iap->ia_mtime.tv_nsec);
  575. }
  576. else if (iap->ia_valid & ATTR_MTIME) {
  577. bmval1 |= FATTR4_WORD1_TIME_MODIFY_SET;
  578. WRITE32(NFS4_SET_TO_SERVER_TIME);
  579. }
  580. /*
  581. * Now we backfill the bitmap and the attribute buffer length.
  582. */
  583. if (len != ((char *)p - (char *)q) + 4) {
  584. printk ("encode_attr: Attr length calculation error! %u != %Zu\n",
  585. len, ((char *)p - (char *)q) + 4);
  586. BUG();
  587. }
  588. len = (char *)p - (char *)q - 12;
  589. *q++ = htonl(bmval0);
  590. *q++ = htonl(bmval1);
  591. *q++ = htonl(len);
  592. status = 0;
  593. /* out: */
  594. return status;
  595. }
  596. static int encode_access(struct xdr_stream *xdr, u32 access)
  597. {
  598. uint32_t *p;
  599. RESERVE_SPACE(8);
  600. WRITE32(OP_ACCESS);
  601. WRITE32(access);
  602. return 0;
  603. }
  604. static int encode_close(struct xdr_stream *xdr, const struct nfs_closeargs *arg)
  605. {
  606. uint32_t *p;
  607. RESERVE_SPACE(8+sizeof(arg->stateid->data));
  608. WRITE32(OP_CLOSE);
  609. WRITE32(arg->seqid->sequence->counter);
  610. WRITEMEM(arg->stateid->data, sizeof(arg->stateid->data));
  611. return 0;
  612. }
  613. static int encode_commit(struct xdr_stream *xdr, const struct nfs_writeargs *args)
  614. {
  615. uint32_t *p;
  616. RESERVE_SPACE(16);
  617. WRITE32(OP_COMMIT);
  618. WRITE64(args->offset);
  619. WRITE32(args->count);
  620. return 0;
  621. }
  622. static int encode_create(struct xdr_stream *xdr, const struct nfs4_create_arg *create)
  623. {
  624. uint32_t *p;
  625. RESERVE_SPACE(8);
  626. WRITE32(OP_CREATE);
  627. WRITE32(create->ftype);
  628. switch (create->ftype) {
  629. case NF4LNK:
  630. RESERVE_SPACE(4 + create->u.symlink->len);
  631. WRITE32(create->u.symlink->len);
  632. WRITEMEM(create->u.symlink->name, create->u.symlink->len);
  633. break;
  634. case NF4BLK: case NF4CHR:
  635. RESERVE_SPACE(8);
  636. WRITE32(create->u.device.specdata1);
  637. WRITE32(create->u.device.specdata2);
  638. break;
  639. default:
  640. break;
  641. }
  642. RESERVE_SPACE(4 + create->name->len);
  643. WRITE32(create->name->len);
  644. WRITEMEM(create->name->name, create->name->len);
  645. return encode_attrs(xdr, create->attrs, create->server);
  646. }
  647. static int encode_getattr_one(struct xdr_stream *xdr, uint32_t bitmap)
  648. {
  649. uint32_t *p;
  650. RESERVE_SPACE(12);
  651. WRITE32(OP_GETATTR);
  652. WRITE32(1);
  653. WRITE32(bitmap);
  654. return 0;
  655. }
  656. static int encode_getattr_two(struct xdr_stream *xdr, uint32_t bm0, uint32_t bm1)
  657. {
  658. uint32_t *p;
  659. RESERVE_SPACE(16);
  660. WRITE32(OP_GETATTR);
  661. WRITE32(2);
  662. WRITE32(bm0);
  663. WRITE32(bm1);
  664. return 0;
  665. }
  666. static int encode_getfattr(struct xdr_stream *xdr, const u32* bitmask)
  667. {
  668. return encode_getattr_two(xdr,
  669. bitmask[0] & nfs4_fattr_bitmap[0],
  670. bitmask[1] & nfs4_fattr_bitmap[1]);
  671. }
  672. static int encode_fsinfo(struct xdr_stream *xdr, const u32* bitmask)
  673. {
  674. return encode_getattr_two(xdr, bitmask[0] & nfs4_fsinfo_bitmap[0],
  675. bitmask[1] & nfs4_fsinfo_bitmap[1]);
  676. }
  677. static int encode_getfh(struct xdr_stream *xdr)
  678. {
  679. uint32_t *p;
  680. RESERVE_SPACE(4);
  681. WRITE32(OP_GETFH);
  682. return 0;
  683. }
  684. static int encode_link(struct xdr_stream *xdr, const struct qstr *name)
  685. {
  686. uint32_t *p;
  687. RESERVE_SPACE(8 + name->len);
  688. WRITE32(OP_LINK);
  689. WRITE32(name->len);
  690. WRITEMEM(name->name, name->len);
  691. return 0;
  692. }
  693. /*
  694. * opcode,type,reclaim,offset,length,new_lock_owner = 32
  695. * open_seqid,open_stateid,lock_seqid,lock_owner.clientid, lock_owner.id = 40
  696. */
  697. static int encode_lock(struct xdr_stream *xdr, const struct nfs_lockargs *arg)
  698. {
  699. uint32_t *p;
  700. struct nfs_lock_opargs *opargs = arg->u.lock;
  701. RESERVE_SPACE(32);
  702. WRITE32(OP_LOCK);
  703. WRITE32(arg->type);
  704. WRITE32(opargs->reclaim);
  705. WRITE64(arg->offset);
  706. WRITE64(arg->length);
  707. WRITE32(opargs->new_lock_owner);
  708. if (opargs->new_lock_owner){
  709. RESERVE_SPACE(40);
  710. WRITE32(opargs->open_seqid->sequence->counter);
  711. WRITEMEM(opargs->open_stateid->data, sizeof(opargs->open_stateid->data));
  712. WRITE32(opargs->lock_seqid->sequence->counter);
  713. WRITE64(opargs->lock_owner.clientid);
  714. WRITE32(4);
  715. WRITE32(opargs->lock_owner.id);
  716. }
  717. else {
  718. RESERVE_SPACE(20);
  719. WRITEMEM(opargs->lock_stateid->data, sizeof(opargs->lock_stateid->data));
  720. WRITE32(opargs->lock_seqid->sequence->counter);
  721. }
  722. return 0;
  723. }
  724. static int encode_lockt(struct xdr_stream *xdr, const struct nfs_lockargs *arg)
  725. {
  726. uint32_t *p;
  727. struct nfs_lowner *opargs = arg->u.lockt;
  728. RESERVE_SPACE(40);
  729. WRITE32(OP_LOCKT);
  730. WRITE32(arg->type);
  731. WRITE64(arg->offset);
  732. WRITE64(arg->length);
  733. WRITE64(opargs->clientid);
  734. WRITE32(4);
  735. WRITE32(opargs->id);
  736. return 0;
  737. }
  738. static int encode_locku(struct xdr_stream *xdr, const struct nfs_lockargs *arg)
  739. {
  740. uint32_t *p;
  741. struct nfs_locku_opargs *opargs = arg->u.locku;
  742. RESERVE_SPACE(44);
  743. WRITE32(OP_LOCKU);
  744. WRITE32(arg->type);
  745. WRITE32(opargs->seqid->sequence->counter);
  746. WRITEMEM(opargs->stateid->data, sizeof(opargs->stateid->data));
  747. WRITE64(arg->offset);
  748. WRITE64(arg->length);
  749. return 0;
  750. }
  751. static int encode_lookup(struct xdr_stream *xdr, const struct qstr *name)
  752. {
  753. int len = name->len;
  754. uint32_t *p;
  755. RESERVE_SPACE(8 + len);
  756. WRITE32(OP_LOOKUP);
  757. WRITE32(len);
  758. WRITEMEM(name->name, len);
  759. return 0;
  760. }
  761. static void encode_share_access(struct xdr_stream *xdr, int open_flags)
  762. {
  763. uint32_t *p;
  764. RESERVE_SPACE(8);
  765. switch (open_flags & (FMODE_READ|FMODE_WRITE)) {
  766. case FMODE_READ:
  767. WRITE32(NFS4_SHARE_ACCESS_READ);
  768. break;
  769. case FMODE_WRITE:
  770. WRITE32(NFS4_SHARE_ACCESS_WRITE);
  771. break;
  772. case FMODE_READ|FMODE_WRITE:
  773. WRITE32(NFS4_SHARE_ACCESS_BOTH);
  774. break;
  775. default:
  776. BUG();
  777. }
  778. WRITE32(0); /* for linux, share_deny = 0 always */
  779. }
  780. static inline void encode_openhdr(struct xdr_stream *xdr, const struct nfs_openargs *arg)
  781. {
  782. uint32_t *p;
  783. /*
  784. * opcode 4, seqid 4, share_access 4, share_deny 4, clientid 8, ownerlen 4,
  785. * owner 4 = 32
  786. */
  787. RESERVE_SPACE(8);
  788. WRITE32(OP_OPEN);
  789. WRITE32(arg->seqid->sequence->counter);
  790. encode_share_access(xdr, arg->open_flags);
  791. RESERVE_SPACE(16);
  792. WRITE64(arg->clientid);
  793. WRITE32(4);
  794. WRITE32(arg->id);
  795. }
  796. static inline void encode_createmode(struct xdr_stream *xdr, const struct nfs_openargs *arg)
  797. {
  798. uint32_t *p;
  799. RESERVE_SPACE(4);
  800. switch(arg->open_flags & O_EXCL) {
  801. case 0:
  802. WRITE32(NFS4_CREATE_UNCHECKED);
  803. encode_attrs(xdr, arg->u.attrs, arg->server);
  804. break;
  805. default:
  806. WRITE32(NFS4_CREATE_EXCLUSIVE);
  807. encode_nfs4_verifier(xdr, &arg->u.verifier);
  808. }
  809. }
  810. static void encode_opentype(struct xdr_stream *xdr, const struct nfs_openargs *arg)
  811. {
  812. uint32_t *p;
  813. RESERVE_SPACE(4);
  814. switch (arg->open_flags & O_CREAT) {
  815. case 0:
  816. WRITE32(NFS4_OPEN_NOCREATE);
  817. break;
  818. default:
  819. BUG_ON(arg->claim != NFS4_OPEN_CLAIM_NULL);
  820. WRITE32(NFS4_OPEN_CREATE);
  821. encode_createmode(xdr, arg);
  822. }
  823. }
  824. static inline void encode_delegation_type(struct xdr_stream *xdr, int delegation_type)
  825. {
  826. uint32_t *p;
  827. RESERVE_SPACE(4);
  828. switch (delegation_type) {
  829. case 0:
  830. WRITE32(NFS4_OPEN_DELEGATE_NONE);
  831. break;
  832. case FMODE_READ:
  833. WRITE32(NFS4_OPEN_DELEGATE_READ);
  834. break;
  835. case FMODE_WRITE|FMODE_READ:
  836. WRITE32(NFS4_OPEN_DELEGATE_WRITE);
  837. break;
  838. default:
  839. BUG();
  840. }
  841. }
  842. static inline void encode_claim_null(struct xdr_stream *xdr, const struct qstr *name)
  843. {
  844. uint32_t *p;
  845. RESERVE_SPACE(4);
  846. WRITE32(NFS4_OPEN_CLAIM_NULL);
  847. encode_string(xdr, name->len, name->name);
  848. }
  849. static inline void encode_claim_previous(struct xdr_stream *xdr, int type)
  850. {
  851. uint32_t *p;
  852. RESERVE_SPACE(4);
  853. WRITE32(NFS4_OPEN_CLAIM_PREVIOUS);
  854. encode_delegation_type(xdr, type);
  855. }
  856. static inline void encode_claim_delegate_cur(struct xdr_stream *xdr, const struct qstr *name, const nfs4_stateid *stateid)
  857. {
  858. uint32_t *p;
  859. RESERVE_SPACE(4+sizeof(stateid->data));
  860. WRITE32(NFS4_OPEN_CLAIM_DELEGATE_CUR);
  861. WRITEMEM(stateid->data, sizeof(stateid->data));
  862. encode_string(xdr, name->len, name->name);
  863. }
  864. static int encode_open(struct xdr_stream *xdr, const struct nfs_openargs *arg)
  865. {
  866. encode_openhdr(xdr, arg);
  867. encode_opentype(xdr, arg);
  868. switch (arg->claim) {
  869. case NFS4_OPEN_CLAIM_NULL:
  870. encode_claim_null(xdr, arg->name);
  871. break;
  872. case NFS4_OPEN_CLAIM_PREVIOUS:
  873. encode_claim_previous(xdr, arg->u.delegation_type);
  874. break;
  875. case NFS4_OPEN_CLAIM_DELEGATE_CUR:
  876. encode_claim_delegate_cur(xdr, arg->name, &arg->u.delegation);
  877. break;
  878. default:
  879. BUG();
  880. }
  881. return 0;
  882. }
  883. static int encode_open_confirm(struct xdr_stream *xdr, const struct nfs_open_confirmargs *arg)
  884. {
  885. uint32_t *p;
  886. RESERVE_SPACE(8+sizeof(arg->stateid.data));
  887. WRITE32(OP_OPEN_CONFIRM);
  888. WRITEMEM(arg->stateid.data, sizeof(arg->stateid.data));
  889. WRITE32(arg->seqid->sequence->counter);
  890. return 0;
  891. }
  892. static int encode_open_downgrade(struct xdr_stream *xdr, const struct nfs_closeargs *arg)
  893. {
  894. uint32_t *p;
  895. RESERVE_SPACE(8+sizeof(arg->stateid->data));
  896. WRITE32(OP_OPEN_DOWNGRADE);
  897. WRITEMEM(arg->stateid->data, sizeof(arg->stateid->data));
  898. WRITE32(arg->seqid->sequence->counter);
  899. encode_share_access(xdr, arg->open_flags);
  900. return 0;
  901. }
  902. static int
  903. encode_putfh(struct xdr_stream *xdr, const struct nfs_fh *fh)
  904. {
  905. int len = fh->size;
  906. uint32_t *p;
  907. RESERVE_SPACE(8 + len);
  908. WRITE32(OP_PUTFH);
  909. WRITE32(len);
  910. WRITEMEM(fh->data, len);
  911. return 0;
  912. }
  913. static int encode_putrootfh(struct xdr_stream *xdr)
  914. {
  915. uint32_t *p;
  916. RESERVE_SPACE(4);
  917. WRITE32(OP_PUTROOTFH);
  918. return 0;
  919. }
  920. static void encode_stateid(struct xdr_stream *xdr, const struct nfs_open_context *ctx)
  921. {
  922. nfs4_stateid stateid;
  923. uint32_t *p;
  924. RESERVE_SPACE(16);
  925. if (ctx->state != NULL) {
  926. nfs4_copy_stateid(&stateid, ctx->state, ctx->lockowner);
  927. WRITEMEM(stateid.data, sizeof(stateid.data));
  928. } else
  929. WRITEMEM(zero_stateid.data, sizeof(zero_stateid.data));
  930. }
  931. static int encode_read(struct xdr_stream *xdr, const struct nfs_readargs *args)
  932. {
  933. uint32_t *p;
  934. RESERVE_SPACE(4);
  935. WRITE32(OP_READ);
  936. encode_stateid(xdr, args->context);
  937. RESERVE_SPACE(12);
  938. WRITE64(args->offset);
  939. WRITE32(args->count);
  940. return 0;
  941. }
  942. static int encode_readdir(struct xdr_stream *xdr, const struct nfs4_readdir_arg *readdir, struct rpc_rqst *req)
  943. {
  944. struct rpc_auth *auth = req->rq_task->tk_auth;
  945. uint32_t attrs[2] = {
  946. FATTR4_WORD0_RDATTR_ERROR|FATTR4_WORD0_FILEID,
  947. FATTR4_WORD1_MOUNTED_ON_FILEID,
  948. };
  949. int replen;
  950. uint32_t *p;
  951. RESERVE_SPACE(32+sizeof(nfs4_verifier));
  952. WRITE32(OP_READDIR);
  953. WRITE64(readdir->cookie);
  954. WRITEMEM(readdir->verifier.data, sizeof(readdir->verifier.data));
  955. WRITE32(readdir->count >> 1); /* We're not doing readdirplus */
  956. WRITE32(readdir->count);
  957. WRITE32(2);
  958. /* Switch to mounted_on_fileid if the server supports it */
  959. if (readdir->bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
  960. attrs[0] &= ~FATTR4_WORD0_FILEID;
  961. else
  962. attrs[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
  963. WRITE32(attrs[0] & readdir->bitmask[0]);
  964. WRITE32(attrs[1] & readdir->bitmask[1]);
  965. dprintk("%s: cookie = %Lu, verifier = 0x%x%x, bitmap = 0x%x%x\n",
  966. __FUNCTION__,
  967. (unsigned long long)readdir->cookie,
  968. ((u32 *)readdir->verifier.data)[0],
  969. ((u32 *)readdir->verifier.data)[1],
  970. attrs[0] & readdir->bitmask[0],
  971. attrs[1] & readdir->bitmask[1]);
  972. /* set up reply kvec
  973. * toplevel_status + taglen + rescount + OP_PUTFH + status
  974. * + OP_READDIR + status + verifer(2) = 9
  975. */
  976. replen = (RPC_REPHDRSIZE + auth->au_rslack + 9) << 2;
  977. xdr_inline_pages(&req->rq_rcv_buf, replen, readdir->pages,
  978. readdir->pgbase, readdir->count);
  979. dprintk("%s: inlined page args = (%u, %p, %u, %u)\n",
  980. __FUNCTION__, replen, readdir->pages,
  981. readdir->pgbase, readdir->count);
  982. return 0;
  983. }
  984. static int encode_readlink(struct xdr_stream *xdr, const struct nfs4_readlink *readlink, struct rpc_rqst *req)
  985. {
  986. struct rpc_auth *auth = req->rq_task->tk_auth;
  987. unsigned int replen;
  988. uint32_t *p;
  989. RESERVE_SPACE(4);
  990. WRITE32(OP_READLINK);
  991. /* set up reply kvec
  992. * toplevel_status + taglen + rescount + OP_PUTFH + status
  993. * + OP_READLINK + status + string length = 8
  994. */
  995. replen = (RPC_REPHDRSIZE + auth->au_rslack + 8) << 2;
  996. xdr_inline_pages(&req->rq_rcv_buf, replen, readlink->pages,
  997. readlink->pgbase, readlink->pglen);
  998. return 0;
  999. }
  1000. static int encode_remove(struct xdr_stream *xdr, const struct qstr *name)
  1001. {
  1002. uint32_t *p;
  1003. RESERVE_SPACE(8 + name->len);
  1004. WRITE32(OP_REMOVE);
  1005. WRITE32(name->len);
  1006. WRITEMEM(name->name, name->len);
  1007. return 0;
  1008. }
  1009. static int encode_rename(struct xdr_stream *xdr, const struct qstr *oldname, const struct qstr *newname)
  1010. {
  1011. uint32_t *p;
  1012. RESERVE_SPACE(8 + oldname->len);
  1013. WRITE32(OP_RENAME);
  1014. WRITE32(oldname->len);
  1015. WRITEMEM(oldname->name, oldname->len);
  1016. RESERVE_SPACE(4 + newname->len);
  1017. WRITE32(newname->len);
  1018. WRITEMEM(newname->name, newname->len);
  1019. return 0;
  1020. }
  1021. static int encode_renew(struct xdr_stream *xdr, const struct nfs4_client *client_stateid)
  1022. {
  1023. uint32_t *p;
  1024. RESERVE_SPACE(12);
  1025. WRITE32(OP_RENEW);
  1026. WRITE64(client_stateid->cl_clientid);
  1027. return 0;
  1028. }
  1029. static int
  1030. encode_restorefh(struct xdr_stream *xdr)
  1031. {
  1032. uint32_t *p;
  1033. RESERVE_SPACE(4);
  1034. WRITE32(OP_RESTOREFH);
  1035. return 0;
  1036. }
  1037. static int
  1038. encode_setacl(struct xdr_stream *xdr, struct nfs_setaclargs *arg)
  1039. {
  1040. uint32_t *p;
  1041. RESERVE_SPACE(4+sizeof(zero_stateid.data));
  1042. WRITE32(OP_SETATTR);
  1043. WRITEMEM(zero_stateid.data, sizeof(zero_stateid.data));
  1044. RESERVE_SPACE(2*4);
  1045. WRITE32(1);
  1046. WRITE32(FATTR4_WORD0_ACL);
  1047. if (arg->acl_len % 4)
  1048. return -EINVAL;
  1049. RESERVE_SPACE(4);
  1050. WRITE32(arg->acl_len);
  1051. xdr_write_pages(xdr, arg->acl_pages, arg->acl_pgbase, arg->acl_len);
  1052. return 0;
  1053. }
  1054. static int
  1055. encode_savefh(struct xdr_stream *xdr)
  1056. {
  1057. uint32_t *p;
  1058. RESERVE_SPACE(4);
  1059. WRITE32(OP_SAVEFH);
  1060. return 0;
  1061. }
  1062. static int encode_setattr(struct xdr_stream *xdr, const struct nfs_setattrargs *arg, const struct nfs_server *server)
  1063. {
  1064. int status;
  1065. uint32_t *p;
  1066. RESERVE_SPACE(4+sizeof(arg->stateid.data));
  1067. WRITE32(OP_SETATTR);
  1068. WRITEMEM(arg->stateid.data, sizeof(arg->stateid.data));
  1069. if ((status = encode_attrs(xdr, arg->iap, server)))
  1070. return status;
  1071. return 0;
  1072. }
  1073. static int encode_setclientid(struct xdr_stream *xdr, const struct nfs4_setclientid *setclientid)
  1074. {
  1075. uint32_t *p;
  1076. RESERVE_SPACE(4 + sizeof(setclientid->sc_verifier->data));
  1077. WRITE32(OP_SETCLIENTID);
  1078. WRITEMEM(setclientid->sc_verifier->data, sizeof(setclientid->sc_verifier->data));
  1079. encode_string(xdr, setclientid->sc_name_len, setclientid->sc_name);
  1080. RESERVE_SPACE(4);
  1081. WRITE32(setclientid->sc_prog);
  1082. encode_string(xdr, setclientid->sc_netid_len, setclientid->sc_netid);
  1083. encode_string(xdr, setclientid->sc_uaddr_len, setclientid->sc_uaddr);
  1084. RESERVE_SPACE(4);
  1085. WRITE32(setclientid->sc_cb_ident);
  1086. return 0;
  1087. }
  1088. static int encode_setclientid_confirm(struct xdr_stream *xdr, const struct nfs4_client *client_state)
  1089. {
  1090. uint32_t *p;
  1091. RESERVE_SPACE(12 + sizeof(client_state->cl_confirm.data));
  1092. WRITE32(OP_SETCLIENTID_CONFIRM);
  1093. WRITE64(client_state->cl_clientid);
  1094. WRITEMEM(client_state->cl_confirm.data, sizeof(client_state->cl_confirm.data));
  1095. return 0;
  1096. }
  1097. static int encode_write(struct xdr_stream *xdr, const struct nfs_writeargs *args)
  1098. {
  1099. uint32_t *p;
  1100. RESERVE_SPACE(4);
  1101. WRITE32(OP_WRITE);
  1102. encode_stateid(xdr, args->context);
  1103. RESERVE_SPACE(16);
  1104. WRITE64(args->offset);
  1105. WRITE32(args->stable);
  1106. WRITE32(args->count);
  1107. xdr_write_pages(xdr, args->pages, args->pgbase, args->count);
  1108. return 0;
  1109. }
  1110. static int encode_delegreturn(struct xdr_stream *xdr, const nfs4_stateid *stateid)
  1111. {
  1112. uint32_t *p;
  1113. RESERVE_SPACE(20);
  1114. WRITE32(OP_DELEGRETURN);
  1115. WRITEMEM(stateid->data, sizeof(stateid->data));
  1116. return 0;
  1117. }
  1118. /*
  1119. * END OF "GENERIC" ENCODE ROUTINES.
  1120. */
  1121. /*
  1122. * Encode an ACCESS request
  1123. */
  1124. static int nfs4_xdr_enc_access(struct rpc_rqst *req, uint32_t *p, const struct nfs4_accessargs *args)
  1125. {
  1126. struct xdr_stream xdr;
  1127. struct compound_hdr hdr = {
  1128. .nops = 2,
  1129. };
  1130. int status;
  1131. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  1132. encode_compound_hdr(&xdr, &hdr);
  1133. if ((status = encode_putfh(&xdr, args->fh)) == 0)
  1134. status = encode_access(&xdr, args->access);
  1135. return status;
  1136. }
  1137. /*
  1138. * Encode LOOKUP request
  1139. */
  1140. static int nfs4_xdr_enc_lookup(struct rpc_rqst *req, uint32_t *p, const struct nfs4_lookup_arg *args)
  1141. {
  1142. struct xdr_stream xdr;
  1143. struct compound_hdr hdr = {
  1144. .nops = 4,
  1145. };
  1146. int status;
  1147. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  1148. encode_compound_hdr(&xdr, &hdr);
  1149. if ((status = encode_putfh(&xdr, args->dir_fh)) != 0)
  1150. goto out;
  1151. if ((status = encode_lookup(&xdr, args->name)) != 0)
  1152. goto out;
  1153. if ((status = encode_getfh(&xdr)) != 0)
  1154. goto out;
  1155. status = encode_getfattr(&xdr, args->bitmask);
  1156. out:
  1157. return status;
  1158. }
  1159. /*
  1160. * Encode LOOKUP_ROOT request
  1161. */
  1162. static int nfs4_xdr_enc_lookup_root(struct rpc_rqst *req, uint32_t *p, const struct nfs4_lookup_root_arg *args)
  1163. {
  1164. struct xdr_stream xdr;
  1165. struct compound_hdr hdr = {
  1166. .nops = 3,
  1167. };
  1168. int status;
  1169. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  1170. encode_compound_hdr(&xdr, &hdr);
  1171. if ((status = encode_putrootfh(&xdr)) != 0)
  1172. goto out;
  1173. if ((status = encode_getfh(&xdr)) == 0)
  1174. status = encode_getfattr(&xdr, args->bitmask);
  1175. out:
  1176. return status;
  1177. }
  1178. /*
  1179. * Encode REMOVE request
  1180. */
  1181. static int nfs4_xdr_enc_remove(struct rpc_rqst *req, uint32_t *p, const struct nfs4_remove_arg *args)
  1182. {
  1183. struct xdr_stream xdr;
  1184. struct compound_hdr hdr = {
  1185. .nops = 3,
  1186. };
  1187. int status;
  1188. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  1189. encode_compound_hdr(&xdr, &hdr);
  1190. if ((status = encode_putfh(&xdr, args->fh)) != 0)
  1191. goto out;
  1192. if ((status = encode_remove(&xdr, args->name)) != 0)
  1193. goto out;
  1194. status = encode_getfattr(&xdr, args->bitmask);
  1195. out:
  1196. return status;
  1197. }
  1198. /*
  1199. * Encode RENAME request
  1200. */
  1201. static int nfs4_xdr_enc_rename(struct rpc_rqst *req, uint32_t *p, const struct nfs4_rename_arg *args)
  1202. {
  1203. struct xdr_stream xdr;
  1204. struct compound_hdr hdr = {
  1205. .nops = 7,
  1206. };
  1207. int status;
  1208. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  1209. encode_compound_hdr(&xdr, &hdr);
  1210. if ((status = encode_putfh(&xdr, args->old_dir)) != 0)
  1211. goto out;
  1212. if ((status = encode_savefh(&xdr)) != 0)
  1213. goto out;
  1214. if ((status = encode_putfh(&xdr, args->new_dir)) != 0)
  1215. goto out;
  1216. if ((status = encode_rename(&xdr, args->old_name, args->new_name)) != 0)
  1217. goto out;
  1218. if ((status = encode_getfattr(&xdr, args->bitmask)) != 0)
  1219. goto out;
  1220. if ((status = encode_restorefh(&xdr)) != 0)
  1221. goto out;
  1222. status = encode_getfattr(&xdr, args->bitmask);
  1223. out:
  1224. return status;
  1225. }
  1226. /*
  1227. * Encode LINK request
  1228. */
  1229. static int nfs4_xdr_enc_link(struct rpc_rqst *req, uint32_t *p, const struct nfs4_link_arg *args)
  1230. {
  1231. struct xdr_stream xdr;
  1232. struct compound_hdr hdr = {
  1233. .nops = 7,
  1234. };
  1235. int status;
  1236. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  1237. encode_compound_hdr(&xdr, &hdr);
  1238. if ((status = encode_putfh(&xdr, args->fh)) != 0)
  1239. goto out;
  1240. if ((status = encode_savefh(&xdr)) != 0)
  1241. goto out;
  1242. if ((status = encode_putfh(&xdr, args->dir_fh)) != 0)
  1243. goto out;
  1244. if ((status = encode_link(&xdr, args->name)) != 0)
  1245. goto out;
  1246. if ((status = encode_getfattr(&xdr, args->bitmask)) != 0)
  1247. goto out;
  1248. if ((status = encode_restorefh(&xdr)) != 0)
  1249. goto out;
  1250. status = encode_getfattr(&xdr, args->bitmask);
  1251. out:
  1252. return status;
  1253. }
  1254. /*
  1255. * Encode CREATE request
  1256. */
  1257. static int nfs4_xdr_enc_create(struct rpc_rqst *req, uint32_t *p, const struct nfs4_create_arg *args)
  1258. {
  1259. struct xdr_stream xdr;
  1260. struct compound_hdr hdr = {
  1261. .nops = 7,
  1262. };
  1263. int status;
  1264. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  1265. encode_compound_hdr(&xdr, &hdr);
  1266. if ((status = encode_putfh(&xdr, args->dir_fh)) != 0)
  1267. goto out;
  1268. if ((status = encode_savefh(&xdr)) != 0)
  1269. goto out;
  1270. if ((status = encode_create(&xdr, args)) != 0)
  1271. goto out;
  1272. if ((status = encode_getfh(&xdr)) != 0)
  1273. goto out;
  1274. if ((status = encode_getfattr(&xdr, args->bitmask)) != 0)
  1275. goto out;
  1276. if ((status = encode_restorefh(&xdr)) != 0)
  1277. goto out;
  1278. status = encode_getfattr(&xdr, args->bitmask);
  1279. out:
  1280. return status;
  1281. }
  1282. /*
  1283. * Encode SYMLINK request
  1284. */
  1285. static int nfs4_xdr_enc_symlink(struct rpc_rqst *req, uint32_t *p, const struct nfs4_create_arg *args)
  1286. {
  1287. return nfs4_xdr_enc_create(req, p, args);
  1288. }
  1289. /*
  1290. * Encode GETATTR request
  1291. */
  1292. static int nfs4_xdr_enc_getattr(struct rpc_rqst *req, uint32_t *p, const struct nfs4_getattr_arg *args)
  1293. {
  1294. struct xdr_stream xdr;
  1295. struct compound_hdr hdr = {
  1296. .nops = 2,
  1297. };
  1298. int status;
  1299. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  1300. encode_compound_hdr(&xdr, &hdr);
  1301. if ((status = encode_putfh(&xdr, args->fh)) == 0)
  1302. status = encode_getfattr(&xdr, args->bitmask);
  1303. return status;
  1304. }
  1305. /*
  1306. * Encode a CLOSE request
  1307. */
  1308. static int nfs4_xdr_enc_close(struct rpc_rqst *req, uint32_t *p, struct nfs_closeargs *args)
  1309. {
  1310. struct xdr_stream xdr;
  1311. struct compound_hdr hdr = {
  1312. .nops = 3,
  1313. };
  1314. int status;
  1315. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  1316. encode_compound_hdr(&xdr, &hdr);
  1317. status = encode_putfh(&xdr, args->fh);
  1318. if(status)
  1319. goto out;
  1320. status = encode_close(&xdr, args);
  1321. if (status != 0)
  1322. goto out;
  1323. status = encode_getfattr(&xdr, args->bitmask);
  1324. out:
  1325. return status;
  1326. }
  1327. /*
  1328. * Encode an OPEN request
  1329. */
  1330. static int nfs4_xdr_enc_open(struct rpc_rqst *req, uint32_t *p, struct nfs_openargs *args)
  1331. {
  1332. struct xdr_stream xdr;
  1333. struct compound_hdr hdr = {
  1334. .nops = 7,
  1335. };
  1336. int status;
  1337. status = nfs_wait_on_sequence(args->seqid, req->rq_task);
  1338. if (status != 0)
  1339. goto out;
  1340. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  1341. encode_compound_hdr(&xdr, &hdr);
  1342. status = encode_putfh(&xdr, args->fh);
  1343. if (status)
  1344. goto out;
  1345. status = encode_savefh(&xdr);
  1346. if (status)
  1347. goto out;
  1348. status = encode_open(&xdr, args);
  1349. if (status)
  1350. goto out;
  1351. status = encode_getfh(&xdr);
  1352. if (status)
  1353. goto out;
  1354. status = encode_getfattr(&xdr, args->bitmask);
  1355. if (status)
  1356. goto out;
  1357. status = encode_restorefh(&xdr);
  1358. if (status)
  1359. goto out;
  1360. status = encode_getfattr(&xdr, args->bitmask);
  1361. out:
  1362. return status;
  1363. }
  1364. /*
  1365. * Encode an OPEN_CONFIRM request
  1366. */
  1367. static int nfs4_xdr_enc_open_confirm(struct rpc_rqst *req, uint32_t *p, struct nfs_open_confirmargs *args)
  1368. {
  1369. struct xdr_stream xdr;
  1370. struct compound_hdr hdr = {
  1371. .nops = 2,
  1372. };
  1373. int status;
  1374. status = nfs_wait_on_sequence(args->seqid, req->rq_task);
  1375. if (status != 0)
  1376. goto out;
  1377. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  1378. encode_compound_hdr(&xdr, &hdr);
  1379. status = encode_putfh(&xdr, args->fh);
  1380. if(status)
  1381. goto out;
  1382. status = encode_open_confirm(&xdr, args);
  1383. out:
  1384. return status;
  1385. }
  1386. /*
  1387. * Encode an OPEN request with no attributes.
  1388. */
  1389. static int nfs4_xdr_enc_open_noattr(struct rpc_rqst *req, uint32_t *p, struct nfs_openargs *args)
  1390. {
  1391. struct xdr_stream xdr;
  1392. struct compound_hdr hdr = {
  1393. .nops = 2,
  1394. };
  1395. int status;
  1396. status = nfs_wait_on_sequence(args->seqid, req->rq_task);
  1397. if (status != 0)
  1398. goto out;
  1399. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  1400. encode_compound_hdr(&xdr, &hdr);
  1401. status = encode_putfh(&xdr, args->fh);
  1402. if (status)
  1403. goto out;
  1404. status = encode_open(&xdr, args);
  1405. out:
  1406. return status;
  1407. }
  1408. /*
  1409. * Encode an OPEN_DOWNGRADE request
  1410. */
  1411. static int nfs4_xdr_enc_open_downgrade(struct rpc_rqst *req, uint32_t *p, struct nfs_closeargs *args)
  1412. {
  1413. struct xdr_stream xdr;
  1414. struct compound_hdr hdr = {
  1415. .nops = 3,
  1416. };
  1417. int status;
  1418. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  1419. encode_compound_hdr(&xdr, &hdr);
  1420. status = encode_putfh(&xdr, args->fh);
  1421. if (status)
  1422. goto out;
  1423. status = encode_open_downgrade(&xdr, args);
  1424. if (status != 0)
  1425. goto out;
  1426. status = encode_getfattr(&xdr, args->bitmask);
  1427. out:
  1428. return status;
  1429. }
  1430. /*
  1431. * Encode a LOCK request
  1432. */
  1433. static int nfs4_xdr_enc_lock(struct rpc_rqst *req, uint32_t *p, struct nfs_lockargs *args)
  1434. {
  1435. struct xdr_stream xdr;
  1436. struct compound_hdr hdr = {
  1437. .nops = 2,
  1438. };
  1439. struct nfs_lock_opargs *opargs = args->u.lock;
  1440. int status;
  1441. status = nfs_wait_on_sequence(opargs->lock_seqid, req->rq_task);
  1442. if (status != 0)
  1443. goto out;
  1444. /* Do we need to do an open_to_lock_owner? */
  1445. if (opargs->lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)
  1446. opargs->new_lock_owner = 0;
  1447. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  1448. encode_compound_hdr(&xdr, &hdr);
  1449. status = encode_putfh(&xdr, args->fh);
  1450. if(status)
  1451. goto out;
  1452. status = encode_lock(&xdr, args);
  1453. out:
  1454. return status;
  1455. }
  1456. /*
  1457. * Encode a LOCKT request
  1458. */
  1459. static int nfs4_xdr_enc_lockt(struct rpc_rqst *req, uint32_t *p, struct nfs_lockargs *args)
  1460. {
  1461. struct xdr_stream xdr;
  1462. struct compound_hdr hdr = {
  1463. .nops = 2,
  1464. };
  1465. int status;
  1466. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  1467. encode_compound_hdr(&xdr, &hdr);
  1468. status = encode_putfh(&xdr, args->fh);
  1469. if(status)
  1470. goto out;
  1471. status = encode_lockt(&xdr, args);
  1472. out:
  1473. return status;
  1474. }
  1475. /*
  1476. * Encode a LOCKU request
  1477. */
  1478. static int nfs4_xdr_enc_locku(struct rpc_rqst *req, uint32_t *p, struct nfs_lockargs *args)
  1479. {
  1480. struct xdr_stream xdr;
  1481. struct compound_hdr hdr = {
  1482. .nops = 2,
  1483. };
  1484. int status;
  1485. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  1486. encode_compound_hdr(&xdr, &hdr);
  1487. status = encode_putfh(&xdr, args->fh);
  1488. if(status)
  1489. goto out;
  1490. status = encode_locku(&xdr, args);
  1491. out:
  1492. return status;
  1493. }
  1494. /*
  1495. * Encode a READLINK request
  1496. */
  1497. static int nfs4_xdr_enc_readlink(struct rpc_rqst *req, uint32_t *p, const struct nfs4_readlink *args)
  1498. {
  1499. struct xdr_stream xdr;
  1500. struct compound_hdr hdr = {
  1501. .nops = 2,
  1502. };
  1503. int status;
  1504. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  1505. encode_compound_hdr(&xdr, &hdr);
  1506. status = encode_putfh(&xdr, args->fh);
  1507. if(status)
  1508. goto out;
  1509. status = encode_readlink(&xdr, args, req);
  1510. out:
  1511. return status;
  1512. }
  1513. /*
  1514. * Encode a READDIR request
  1515. */
  1516. static int nfs4_xdr_enc_readdir(struct rpc_rqst *req, uint32_t *p, const struct nfs4_readdir_arg *args)
  1517. {
  1518. struct xdr_stream xdr;
  1519. struct compound_hdr hdr = {
  1520. .nops = 2,
  1521. };
  1522. int status;
  1523. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  1524. encode_compound_hdr(&xdr, &hdr);
  1525. status = encode_putfh(&xdr, args->fh);
  1526. if(status)
  1527. goto out;
  1528. status = encode_readdir(&xdr, args, req);
  1529. out:
  1530. return status;
  1531. }
  1532. /*
  1533. * Encode a READ request
  1534. */
  1535. static int nfs4_xdr_enc_read(struct rpc_rqst *req, uint32_t *p, struct nfs_readargs *args)
  1536. {
  1537. struct rpc_auth *auth = req->rq_task->tk_auth;
  1538. struct xdr_stream xdr;
  1539. struct compound_hdr hdr = {
  1540. .nops = 2,
  1541. };
  1542. int replen, status;
  1543. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  1544. encode_compound_hdr(&xdr, &hdr);
  1545. status = encode_putfh(&xdr, args->fh);
  1546. if (status)
  1547. goto out;
  1548. status = encode_read(&xdr, args);
  1549. if (status)
  1550. goto out;
  1551. /* set up reply kvec
  1552. * toplevel status + taglen=0 + rescount + OP_PUTFH + status
  1553. * + OP_READ + status + eof + datalen = 9
  1554. */
  1555. replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS4_dec_read_sz) << 2;
  1556. xdr_inline_pages(&req->rq_rcv_buf, replen,
  1557. args->pages, args->pgbase, args->count);
  1558. out:
  1559. return status;
  1560. }
  1561. /*
  1562. * Encode an SETATTR request
  1563. */
  1564. static int nfs4_xdr_enc_setattr(struct rpc_rqst *req, uint32_t *p, struct nfs_setattrargs *args)
  1565. {
  1566. struct xdr_stream xdr;
  1567. struct compound_hdr hdr = {
  1568. .nops = 3,
  1569. };
  1570. int status;
  1571. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  1572. encode_compound_hdr(&xdr, &hdr);
  1573. status = encode_putfh(&xdr, args->fh);
  1574. if(status)
  1575. goto out;
  1576. status = encode_setattr(&xdr, args, args->server);
  1577. if(status)
  1578. goto out;
  1579. status = encode_getfattr(&xdr, args->bitmask);
  1580. out:
  1581. return status;
  1582. }
  1583. /*
  1584. * Encode a GETACL request
  1585. */
  1586. static int
  1587. nfs4_xdr_enc_getacl(struct rpc_rqst *req, uint32_t *p,
  1588. struct nfs_getaclargs *args)
  1589. {
  1590. struct xdr_stream xdr;
  1591. struct rpc_auth *auth = req->rq_task->tk_auth;
  1592. struct compound_hdr hdr = {
  1593. .nops = 2,
  1594. };
  1595. int replen, status;
  1596. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  1597. encode_compound_hdr(&xdr, &hdr);
  1598. status = encode_putfh(&xdr, args->fh);
  1599. if (status)
  1600. goto out;
  1601. status = encode_getattr_two(&xdr, FATTR4_WORD0_ACL, 0);
  1602. /* set up reply buffer: */
  1603. replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS4_dec_getacl_sz) << 2;
  1604. xdr_inline_pages(&req->rq_rcv_buf, replen,
  1605. args->acl_pages, args->acl_pgbase, args->acl_len);
  1606. out:
  1607. return status;
  1608. }
  1609. /*
  1610. * Encode a WRITE request
  1611. */
  1612. static int nfs4_xdr_enc_write(struct rpc_rqst *req, uint32_t *p, struct nfs_writeargs *args)
  1613. {
  1614. struct xdr_stream xdr;
  1615. struct compound_hdr hdr = {
  1616. .nops = 3,
  1617. };
  1618. int status;
  1619. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  1620. encode_compound_hdr(&xdr, &hdr);
  1621. status = encode_putfh(&xdr, args->fh);
  1622. if (status)
  1623. goto out;
  1624. status = encode_write(&xdr, args);
  1625. if (status)
  1626. goto out;
  1627. status = encode_getfattr(&xdr, args->bitmask);
  1628. out:
  1629. return status;
  1630. }
  1631. /*
  1632. * a COMMIT request
  1633. */
  1634. static int nfs4_xdr_enc_commit(struct rpc_rqst *req, uint32_t *p, struct nfs_writeargs *args)
  1635. {
  1636. struct xdr_stream xdr;
  1637. struct compound_hdr hdr = {
  1638. .nops = 3,
  1639. };
  1640. int status;
  1641. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  1642. encode_compound_hdr(&xdr, &hdr);
  1643. status = encode_putfh(&xdr, args->fh);
  1644. if (status)
  1645. goto out;
  1646. status = encode_commit(&xdr, args);
  1647. if (status)
  1648. goto out;
  1649. status = encode_getfattr(&xdr, args->bitmask);
  1650. out:
  1651. return status;
  1652. }
  1653. /*
  1654. * FSINFO request
  1655. */
  1656. static int nfs4_xdr_enc_fsinfo(struct rpc_rqst *req, uint32_t *p, struct nfs4_fsinfo_arg *args)
  1657. {
  1658. struct xdr_stream xdr;
  1659. struct compound_hdr hdr = {
  1660. .nops = 2,
  1661. };
  1662. int status;
  1663. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  1664. encode_compound_hdr(&xdr, &hdr);
  1665. status = encode_putfh(&xdr, args->fh);
  1666. if (!status)
  1667. status = encode_fsinfo(&xdr, args->bitmask);
  1668. return status;
  1669. }
  1670. /*
  1671. * a PATHCONF request
  1672. */
  1673. static int nfs4_xdr_enc_pathconf(struct rpc_rqst *req, uint32_t *p, const struct nfs4_pathconf_arg *args)
  1674. {
  1675. struct xdr_stream xdr;
  1676. struct compound_hdr hdr = {
  1677. .nops = 2,
  1678. };
  1679. int status;
  1680. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  1681. encode_compound_hdr(&xdr, &hdr);
  1682. status = encode_putfh(&xdr, args->fh);
  1683. if (!status)
  1684. status = encode_getattr_one(&xdr,
  1685. args->bitmask[0] & nfs4_pathconf_bitmap[0]);
  1686. return status;
  1687. }
  1688. /*
  1689. * a STATFS request
  1690. */
  1691. static int nfs4_xdr_enc_statfs(struct rpc_rqst *req, uint32_t *p, const struct nfs4_statfs_arg *args)
  1692. {
  1693. struct xdr_stream xdr;
  1694. struct compound_hdr hdr = {
  1695. .nops = 2,
  1696. };
  1697. int status;
  1698. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  1699. encode_compound_hdr(&xdr, &hdr);
  1700. status = encode_putfh(&xdr, args->fh);
  1701. if (status == 0)
  1702. status = encode_getattr_two(&xdr,
  1703. args->bitmask[0] & nfs4_statfs_bitmap[0],
  1704. args->bitmask[1] & nfs4_statfs_bitmap[1]);
  1705. return status;
  1706. }
  1707. /*
  1708. * GETATTR_BITMAP request
  1709. */
  1710. static int nfs4_xdr_enc_server_caps(struct rpc_rqst *req, uint32_t *p, const struct nfs_fh *fhandle)
  1711. {
  1712. struct xdr_stream xdr;
  1713. struct compound_hdr hdr = {
  1714. .nops = 2,
  1715. };
  1716. int status;
  1717. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  1718. encode_compound_hdr(&xdr, &hdr);
  1719. status = encode_putfh(&xdr, fhandle);
  1720. if (status == 0)
  1721. status = encode_getattr_one(&xdr, FATTR4_WORD0_SUPPORTED_ATTRS|
  1722. FATTR4_WORD0_LINK_SUPPORT|
  1723. FATTR4_WORD0_SYMLINK_SUPPORT|
  1724. FATTR4_WORD0_ACLSUPPORT);
  1725. return status;
  1726. }
  1727. /*
  1728. * a RENEW request
  1729. */
  1730. static int nfs4_xdr_enc_renew(struct rpc_rqst *req, uint32_t *p, struct nfs4_client *clp)
  1731. {
  1732. struct xdr_stream xdr;
  1733. struct compound_hdr hdr = {
  1734. .nops = 1,
  1735. };
  1736. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  1737. encode_compound_hdr(&xdr, &hdr);
  1738. return encode_renew(&xdr, clp);
  1739. }
  1740. /*
  1741. * a SETCLIENTID request
  1742. */
  1743. static int nfs4_xdr_enc_setclientid(struct rpc_rqst *req, uint32_t *p, struct nfs4_setclientid *sc)
  1744. {
  1745. struct xdr_stream xdr;
  1746. struct compound_hdr hdr = {
  1747. .nops = 1,
  1748. };
  1749. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  1750. encode_compound_hdr(&xdr, &hdr);
  1751. return encode_setclientid(&xdr, sc);
  1752. }
  1753. /*
  1754. * a SETCLIENTID_CONFIRM request
  1755. */
  1756. static int nfs4_xdr_enc_setclientid_confirm(struct rpc_rqst *req, uint32_t *p, struct nfs4_client *clp)
  1757. {
  1758. struct xdr_stream xdr;
  1759. struct compound_hdr hdr = {
  1760. .nops = 3,
  1761. };
  1762. const u32 lease_bitmap[2] = { FATTR4_WORD0_LEASE_TIME, 0 };
  1763. int status;
  1764. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  1765. encode_compound_hdr(&xdr, &hdr);
  1766. status = encode_setclientid_confirm(&xdr, clp);
  1767. if (!status)
  1768. status = encode_putrootfh(&xdr);
  1769. if (!status)
  1770. status = encode_fsinfo(&xdr, lease_bitmap);
  1771. return status;
  1772. }
  1773. /*
  1774. * DELEGRETURN request
  1775. */
  1776. static int nfs4_xdr_enc_delegreturn(struct rpc_rqst *req, uint32_t *p, const struct nfs4_delegreturnargs *args)
  1777. {
  1778. struct xdr_stream xdr;
  1779. struct compound_hdr hdr = {
  1780. .nops = 2,
  1781. };
  1782. int status;
  1783. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  1784. encode_compound_hdr(&xdr, &hdr);
  1785. if ((status = encode_putfh(&xdr, args->fhandle)) == 0)
  1786. status = encode_delegreturn(&xdr, args->stateid);
  1787. return status;
  1788. }
  1789. /*
  1790. * START OF "GENERIC" DECODE ROUTINES.
  1791. * These may look a little ugly since they are imported from a "generic"
  1792. * set of XDR encode/decode routines which are intended to be shared by
  1793. * all of our NFSv4 implementations (OpenBSD, MacOS X...).
  1794. *
  1795. * If the pain of reading these is too great, it should be a straightforward
  1796. * task to translate them into Linux-specific versions which are more
  1797. * consistent with the style used in NFSv2/v3...
  1798. */
  1799. #define READ32(x) (x) = ntohl(*p++)
  1800. #define READ64(x) do { \
  1801. (x) = (u64)ntohl(*p++) << 32; \
  1802. (x) |= ntohl(*p++); \
  1803. } while (0)
  1804. #define READTIME(x) do { \
  1805. p++; \
  1806. (x.tv_sec) = ntohl(*p++); \
  1807. (x.tv_nsec) = ntohl(*p++); \
  1808. } while (0)
  1809. #define COPYMEM(x,nbytes) do { \
  1810. memcpy((x), p, nbytes); \
  1811. p += XDR_QUADLEN(nbytes); \
  1812. } while (0)
  1813. #define READ_BUF(nbytes) do { \
  1814. p = xdr_inline_decode(xdr, nbytes); \
  1815. if (!p) { \
  1816. printk(KERN_WARNING "%s: reply buffer overflowed in line %d.", \
  1817. __FUNCTION__, __LINE__); \
  1818. return -EIO; \
  1819. } \
  1820. } while (0)
  1821. static int decode_opaque_inline(struct xdr_stream *xdr, uint32_t *len, char **string)
  1822. {
  1823. uint32_t *p;
  1824. READ_BUF(4);
  1825. READ32(*len);
  1826. READ_BUF(*len);
  1827. *string = (char *)p;
  1828. return 0;
  1829. }
  1830. static int decode_compound_hdr(struct xdr_stream *xdr, struct compound_hdr *hdr)
  1831. {
  1832. uint32_t *p;
  1833. READ_BUF(8);
  1834. READ32(hdr->status);
  1835. READ32(hdr->taglen);
  1836. READ_BUF(hdr->taglen + 4);
  1837. hdr->tag = (char *)p;
  1838. p += XDR_QUADLEN(hdr->taglen);
  1839. READ32(hdr->nops);
  1840. return 0;
  1841. }
  1842. static int decode_op_hdr(struct xdr_stream *xdr, enum nfs_opnum4 expected)
  1843. {
  1844. uint32_t *p;
  1845. uint32_t opnum;
  1846. int32_t nfserr;
  1847. READ_BUF(8);
  1848. READ32(opnum);
  1849. if (opnum != expected) {
  1850. printk(KERN_NOTICE
  1851. "nfs4_decode_op_hdr: Server returned operation"
  1852. " %d but we issued a request for %d\n",
  1853. opnum, expected);
  1854. return -EIO;
  1855. }
  1856. READ32(nfserr);
  1857. if (nfserr != NFS_OK)
  1858. return -nfs_stat_to_errno(nfserr);
  1859. return 0;
  1860. }
  1861. /* Dummy routine */
  1862. static int decode_ace(struct xdr_stream *xdr, void *ace, struct nfs4_client *clp)
  1863. {
  1864. uint32_t *p;
  1865. uint32_t strlen;
  1866. char *str;
  1867. READ_BUF(12);
  1868. return decode_opaque_inline(xdr, &strlen, &str);
  1869. }
  1870. static int decode_attr_bitmap(struct xdr_stream *xdr, uint32_t *bitmap)
  1871. {
  1872. uint32_t bmlen, *p;
  1873. READ_BUF(4);
  1874. READ32(bmlen);
  1875. bitmap[0] = bitmap[1] = 0;
  1876. READ_BUF((bmlen << 2));
  1877. if (bmlen > 0) {
  1878. READ32(bitmap[0]);
  1879. if (bmlen > 1)
  1880. READ32(bitmap[1]);
  1881. }
  1882. return 0;
  1883. }
  1884. static inline int decode_attr_length(struct xdr_stream *xdr, uint32_t *attrlen, uint32_t **savep)
  1885. {
  1886. uint32_t *p;
  1887. READ_BUF(4);
  1888. READ32(*attrlen);
  1889. *savep = xdr->p;
  1890. return 0;
  1891. }
  1892. static int decode_attr_supported(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *bitmask)
  1893. {
  1894. if (likely(bitmap[0] & FATTR4_WORD0_SUPPORTED_ATTRS)) {
  1895. decode_attr_bitmap(xdr, bitmask);
  1896. bitmap[0] &= ~FATTR4_WORD0_SUPPORTED_ATTRS;
  1897. } else
  1898. bitmask[0] = bitmask[1] = 0;
  1899. dprintk("%s: bitmask=0x%x%x\n", __FUNCTION__, bitmask[0], bitmask[1]);
  1900. return 0;
  1901. }
  1902. static int decode_attr_type(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *type)
  1903. {
  1904. uint32_t *p;
  1905. *type = 0;
  1906. if (unlikely(bitmap[0] & (FATTR4_WORD0_TYPE - 1U)))
  1907. return -EIO;
  1908. if (likely(bitmap[0] & FATTR4_WORD0_TYPE)) {
  1909. READ_BUF(4);
  1910. READ32(*type);
  1911. if (*type < NF4REG || *type > NF4NAMEDATTR) {
  1912. dprintk("%s: bad type %d\n", __FUNCTION__, *type);
  1913. return -EIO;
  1914. }
  1915. bitmap[0] &= ~FATTR4_WORD0_TYPE;
  1916. }
  1917. dprintk("%s: type=0%o\n", __FUNCTION__, nfs_type2fmt[*type].nfs2type);
  1918. return 0;
  1919. }
  1920. static int decode_attr_change(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *change)
  1921. {
  1922. uint32_t *p;
  1923. *change = 0;
  1924. if (unlikely(bitmap[0] & (FATTR4_WORD0_CHANGE - 1U)))
  1925. return -EIO;
  1926. if (likely(bitmap[0] & FATTR4_WORD0_CHANGE)) {
  1927. READ_BUF(8);
  1928. READ64(*change);
  1929. bitmap[0] &= ~FATTR4_WORD0_CHANGE;
  1930. }
  1931. dprintk("%s: change attribute=%Lu\n", __FUNCTION__,
  1932. (unsigned long long)*change);
  1933. return 0;
  1934. }
  1935. static int decode_attr_size(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *size)
  1936. {
  1937. uint32_t *p;
  1938. *size = 0;
  1939. if (unlikely(bitmap[0] & (FATTR4_WORD0_SIZE - 1U)))
  1940. return -EIO;
  1941. if (likely(bitmap[0] & FATTR4_WORD0_SIZE)) {
  1942. READ_BUF(8);
  1943. READ64(*size);
  1944. bitmap[0] &= ~FATTR4_WORD0_SIZE;
  1945. }
  1946. dprintk("%s: file size=%Lu\n", __FUNCTION__, (unsigned long long)*size);
  1947. return 0;
  1948. }
  1949. static int decode_attr_link_support(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
  1950. {
  1951. uint32_t *p;
  1952. *res = 0;
  1953. if (unlikely(bitmap[0] & (FATTR4_WORD0_LINK_SUPPORT - 1U)))
  1954. return -EIO;
  1955. if (likely(bitmap[0] & FATTR4_WORD0_LINK_SUPPORT)) {
  1956. READ_BUF(4);
  1957. READ32(*res);
  1958. bitmap[0] &= ~FATTR4_WORD0_LINK_SUPPORT;
  1959. }
  1960. dprintk("%s: link support=%s\n", __FUNCTION__, *res == 0 ? "false" : "true");
  1961. return 0;
  1962. }
  1963. static int decode_attr_symlink_support(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
  1964. {
  1965. uint32_t *p;
  1966. *res = 0;
  1967. if (unlikely(bitmap[0] & (FATTR4_WORD0_SYMLINK_SUPPORT - 1U)))
  1968. return -EIO;
  1969. if (likely(bitmap[0] & FATTR4_WORD0_SYMLINK_SUPPORT)) {
  1970. READ_BUF(4);
  1971. READ32(*res);
  1972. bitmap[0] &= ~FATTR4_WORD0_SYMLINK_SUPPORT;
  1973. }
  1974. dprintk("%s: symlink support=%s\n", __FUNCTION__, *res == 0 ? "false" : "true");
  1975. return 0;
  1976. }
  1977. static int decode_attr_fsid(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs4_fsid *fsid)
  1978. {
  1979. uint32_t *p;
  1980. fsid->major = 0;
  1981. fsid->minor = 0;
  1982. if (unlikely(bitmap[0] & (FATTR4_WORD0_FSID - 1U)))
  1983. return -EIO;
  1984. if (likely(bitmap[0] & FATTR4_WORD0_FSID)) {
  1985. READ_BUF(16);
  1986. READ64(fsid->major);
  1987. READ64(fsid->minor);
  1988. bitmap[0] &= ~FATTR4_WORD0_FSID;
  1989. }
  1990. dprintk("%s: fsid=(0x%Lx/0x%Lx)\n", __FUNCTION__,
  1991. (unsigned long long)fsid->major,
  1992. (unsigned long long)fsid->minor);
  1993. return 0;
  1994. }
  1995. static int decode_attr_lease_time(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
  1996. {
  1997. uint32_t *p;
  1998. *res = 60;
  1999. if (unlikely(bitmap[0] & (FATTR4_WORD0_LEASE_TIME - 1U)))
  2000. return -EIO;
  2001. if (likely(bitmap[0] & FATTR4_WORD0_LEASE_TIME)) {
  2002. READ_BUF(4);
  2003. READ32(*res);
  2004. bitmap[0] &= ~FATTR4_WORD0_LEASE_TIME;
  2005. }
  2006. dprintk("%s: file size=%u\n", __FUNCTION__, (unsigned int)*res);
  2007. return 0;
  2008. }
  2009. static int decode_attr_aclsupport(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
  2010. {
  2011. uint32_t *p;
  2012. *res = ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL;
  2013. if (unlikely(bitmap[0] & (FATTR4_WORD0_ACLSUPPORT - 1U)))
  2014. return -EIO;
  2015. if (likely(bitmap[0] & FATTR4_WORD0_ACLSUPPORT)) {
  2016. READ_BUF(4);
  2017. READ32(*res);
  2018. bitmap[0] &= ~FATTR4_WORD0_ACLSUPPORT;
  2019. }
  2020. dprintk("%s: ACLs supported=%u\n", __FUNCTION__, (unsigned int)*res);
  2021. return 0;
  2022. }
  2023. static int decode_attr_fileid(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *fileid)
  2024. {
  2025. uint32_t *p;
  2026. *fileid = 0;
  2027. if (unlikely(bitmap[0] & (FATTR4_WORD0_FILEID - 1U)))
  2028. return -EIO;
  2029. if (likely(bitmap[0] & FATTR4_WORD0_FILEID)) {
  2030. READ_BUF(8);
  2031. READ64(*fileid);
  2032. bitmap[0] &= ~FATTR4_WORD0_FILEID;
  2033. }
  2034. dprintk("%s: fileid=%Lu\n", __FUNCTION__, (unsigned long long)*fileid);
  2035. return 0;
  2036. }
  2037. static int decode_attr_files_avail(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
  2038. {
  2039. uint32_t *p;
  2040. int status = 0;
  2041. *res = 0;
  2042. if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_AVAIL - 1U)))
  2043. return -EIO;
  2044. if (likely(bitmap[0] & FATTR4_WORD0_FILES_AVAIL)) {
  2045. READ_BUF(8);
  2046. READ64(*res);
  2047. bitmap[0] &= ~FATTR4_WORD0_FILES_AVAIL;
  2048. }
  2049. dprintk("%s: files avail=%Lu\n", __FUNCTION__, (unsigned long long)*res);
  2050. return status;
  2051. }
  2052. static int decode_attr_files_free(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
  2053. {
  2054. uint32_t *p;
  2055. int status = 0;
  2056. *res = 0;
  2057. if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_FREE - 1U)))
  2058. return -EIO;
  2059. if (likely(bitmap[0] & FATTR4_WORD0_FILES_FREE)) {
  2060. READ_BUF(8);
  2061. READ64(*res);
  2062. bitmap[0] &= ~FATTR4_WORD0_FILES_FREE;
  2063. }
  2064. dprintk("%s: files free=%Lu\n", __FUNCTION__, (unsigned long long)*res);
  2065. return status;
  2066. }
  2067. static int decode_attr_files_total(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
  2068. {
  2069. uint32_t *p;
  2070. int status = 0;
  2071. *res = 0;
  2072. if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_TOTAL - 1U)))
  2073. return -EIO;
  2074. if (likely(bitmap[0] & FATTR4_WORD0_FILES_TOTAL)) {
  2075. READ_BUF(8);
  2076. READ64(*res);
  2077. bitmap[0] &= ~FATTR4_WORD0_FILES_TOTAL;
  2078. }
  2079. dprintk("%s: files total=%Lu\n", __FUNCTION__, (unsigned long long)*res);
  2080. return status;
  2081. }
  2082. static int decode_attr_maxfilesize(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
  2083. {
  2084. uint32_t *p;
  2085. int status = 0;
  2086. *res = 0;
  2087. if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXFILESIZE - 1U)))
  2088. return -EIO;
  2089. if (likely(bitmap[0] & FATTR4_WORD0_MAXFILESIZE)) {
  2090. READ_BUF(8);
  2091. READ64(*res);
  2092. bitmap[0] &= ~FATTR4_WORD0_MAXFILESIZE;
  2093. }
  2094. dprintk("%s: maxfilesize=%Lu\n", __FUNCTION__, (unsigned long long)*res);
  2095. return status;
  2096. }
  2097. static int decode_attr_maxlink(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *maxlink)
  2098. {
  2099. uint32_t *p;
  2100. int status = 0;
  2101. *maxlink = 1;
  2102. if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXLINK - 1U)))
  2103. return -EIO;
  2104. if (likely(bitmap[0] & FATTR4_WORD0_MAXLINK)) {
  2105. READ_BUF(4);
  2106. READ32(*maxlink);
  2107. bitmap[0] &= ~FATTR4_WORD0_MAXLINK;
  2108. }
  2109. dprintk("%s: maxlink=%u\n", __FUNCTION__, *maxlink);
  2110. return status;
  2111. }
  2112. static int decode_attr_maxname(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *maxname)
  2113. {
  2114. uint32_t *p;
  2115. int status = 0;
  2116. *maxname = 1024;
  2117. if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXNAME - 1U)))
  2118. return -EIO;
  2119. if (likely(bitmap[0] & FATTR4_WORD0_MAXNAME)) {
  2120. READ_BUF(4);
  2121. READ32(*maxname);
  2122. bitmap[0] &= ~FATTR4_WORD0_MAXNAME;
  2123. }
  2124. dprintk("%s: maxname=%u\n", __FUNCTION__, *maxname);
  2125. return status;
  2126. }
  2127. static int decode_attr_maxread(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
  2128. {
  2129. uint32_t *p;
  2130. int status = 0;
  2131. *res = 1024;
  2132. if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXREAD - 1U)))
  2133. return -EIO;
  2134. if (likely(bitmap[0] & FATTR4_WORD0_MAXREAD)) {
  2135. uint64_t maxread;
  2136. READ_BUF(8);
  2137. READ64(maxread);
  2138. if (maxread > 0x7FFFFFFF)
  2139. maxread = 0x7FFFFFFF;
  2140. *res = (uint32_t)maxread;
  2141. bitmap[0] &= ~FATTR4_WORD0_MAXREAD;
  2142. }
  2143. dprintk("%s: maxread=%lu\n", __FUNCTION__, (unsigned long)*res);
  2144. return status;
  2145. }
  2146. static int decode_attr_maxwrite(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
  2147. {
  2148. uint32_t *p;
  2149. int status = 0;
  2150. *res = 1024;
  2151. if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXWRITE - 1U)))
  2152. return -EIO;
  2153. if (likely(bitmap[0] & FATTR4_WORD0_MAXWRITE)) {
  2154. uint64_t maxwrite;
  2155. READ_BUF(8);
  2156. READ64(maxwrite);
  2157. if (maxwrite > 0x7FFFFFFF)
  2158. maxwrite = 0x7FFFFFFF;
  2159. *res = (uint32_t)maxwrite;
  2160. bitmap[0] &= ~FATTR4_WORD0_MAXWRITE;
  2161. }
  2162. dprintk("%s: maxwrite=%lu\n", __FUNCTION__, (unsigned long)*res);
  2163. return status;
  2164. }
  2165. static int decode_attr_mode(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *mode)
  2166. {
  2167. uint32_t *p;
  2168. *mode = 0;
  2169. if (unlikely(bitmap[1] & (FATTR4_WORD1_MODE - 1U)))
  2170. return -EIO;
  2171. if (likely(bitmap[1] & FATTR4_WORD1_MODE)) {
  2172. READ_BUF(4);
  2173. READ32(*mode);
  2174. *mode &= ~S_IFMT;
  2175. bitmap[1] &= ~FATTR4_WORD1_MODE;
  2176. }
  2177. dprintk("%s: file mode=0%o\n", __FUNCTION__, (unsigned int)*mode);
  2178. return 0;
  2179. }
  2180. static int decode_attr_nlink(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *nlink)
  2181. {
  2182. uint32_t *p;
  2183. *nlink = 1;
  2184. if (unlikely(bitmap[1] & (FATTR4_WORD1_NUMLINKS - 1U)))
  2185. return -EIO;
  2186. if (likely(bitmap[1] & FATTR4_WORD1_NUMLINKS)) {
  2187. READ_BUF(4);
  2188. READ32(*nlink);
  2189. bitmap[1] &= ~FATTR4_WORD1_NUMLINKS;
  2190. }
  2191. dprintk("%s: nlink=%u\n", __FUNCTION__, (unsigned int)*nlink);
  2192. return 0;
  2193. }
  2194. static int decode_attr_owner(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs4_client *clp, int32_t *uid)
  2195. {
  2196. uint32_t len, *p;
  2197. *uid = -2;
  2198. if (unlikely(bitmap[1] & (FATTR4_WORD1_OWNER - 1U)))
  2199. return -EIO;
  2200. if (likely(bitmap[1] & FATTR4_WORD1_OWNER)) {
  2201. READ_BUF(4);
  2202. READ32(len);
  2203. READ_BUF(len);
  2204. if (len < XDR_MAX_NETOBJ) {
  2205. if (nfs_map_name_to_uid(clp, (char *)p, len, uid) != 0)
  2206. dprintk("%s: nfs_map_name_to_uid failed!\n",
  2207. __FUNCTION__);
  2208. } else
  2209. printk(KERN_WARNING "%s: name too long (%u)!\n",
  2210. __FUNCTION__, len);
  2211. bitmap[1] &= ~FATTR4_WORD1_OWNER;
  2212. }
  2213. dprintk("%s: uid=%d\n", __FUNCTION__, (int)*uid);
  2214. return 0;
  2215. }
  2216. static int decode_attr_group(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs4_client *clp, int32_t *gid)
  2217. {
  2218. uint32_t len, *p;
  2219. *gid = -2;
  2220. if (unlikely(bitmap[1] & (FATTR4_WORD1_OWNER_GROUP - 1U)))
  2221. return -EIO;
  2222. if (likely(bitmap[1] & FATTR4_WORD1_OWNER_GROUP)) {
  2223. READ_BUF(4);
  2224. READ32(len);
  2225. READ_BUF(len);
  2226. if (len < XDR_MAX_NETOBJ) {
  2227. if (nfs_map_group_to_gid(clp, (char *)p, len, gid) != 0)
  2228. dprintk("%s: nfs_map_group_to_gid failed!\n",
  2229. __FUNCTION__);
  2230. } else
  2231. printk(KERN_WARNING "%s: name too long (%u)!\n",
  2232. __FUNCTION__, len);
  2233. bitmap[1] &= ~FATTR4_WORD1_OWNER_GROUP;
  2234. }
  2235. dprintk("%s: gid=%d\n", __FUNCTION__, (int)*gid);
  2236. return 0;
  2237. }
  2238. static int decode_attr_rdev(struct xdr_stream *xdr, uint32_t *bitmap, dev_t *rdev)
  2239. {
  2240. uint32_t major = 0, minor = 0, *p;
  2241. *rdev = MKDEV(0,0);
  2242. if (unlikely(bitmap[1] & (FATTR4_WORD1_RAWDEV - 1U)))
  2243. return -EIO;
  2244. if (likely(bitmap[1] & FATTR4_WORD1_RAWDEV)) {
  2245. dev_t tmp;
  2246. READ_BUF(8);
  2247. READ32(major);
  2248. READ32(minor);
  2249. tmp = MKDEV(major, minor);
  2250. if (MAJOR(tmp) == major && MINOR(tmp) == minor)
  2251. *rdev = tmp;
  2252. bitmap[1] &= ~ FATTR4_WORD1_RAWDEV;
  2253. }
  2254. dprintk("%s: rdev=(0x%x:0x%x)\n", __FUNCTION__, major, minor);
  2255. return 0;
  2256. }
  2257. static int decode_attr_space_avail(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
  2258. {
  2259. uint32_t *p;
  2260. int status = 0;
  2261. *res = 0;
  2262. if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_AVAIL - 1U)))
  2263. return -EIO;
  2264. if (likely(bitmap[1] & FATTR4_WORD1_SPACE_AVAIL)) {
  2265. READ_BUF(8);
  2266. READ64(*res);
  2267. bitmap[1] &= ~FATTR4_WORD1_SPACE_AVAIL;
  2268. }
  2269. dprintk("%s: space avail=%Lu\n", __FUNCTION__, (unsigned long long)*res);
  2270. return status;
  2271. }
  2272. static int decode_attr_space_free(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
  2273. {
  2274. uint32_t *p;
  2275. int status = 0;
  2276. *res = 0;
  2277. if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_FREE - 1U)))
  2278. return -EIO;
  2279. if (likely(bitmap[1] & FATTR4_WORD1_SPACE_FREE)) {
  2280. READ_BUF(8);
  2281. READ64(*res);
  2282. bitmap[1] &= ~FATTR4_WORD1_SPACE_FREE;
  2283. }
  2284. dprintk("%s: space free=%Lu\n", __FUNCTION__, (unsigned long long)*res);
  2285. return status;
  2286. }
  2287. static int decode_attr_space_total(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
  2288. {
  2289. uint32_t *p;
  2290. int status = 0;
  2291. *res = 0;
  2292. if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_TOTAL - 1U)))
  2293. return -EIO;
  2294. if (likely(bitmap[1] & FATTR4_WORD1_SPACE_TOTAL)) {
  2295. READ_BUF(8);
  2296. READ64(*res);
  2297. bitmap[1] &= ~FATTR4_WORD1_SPACE_TOTAL;
  2298. }
  2299. dprintk("%s: space total=%Lu\n", __FUNCTION__, (unsigned long long)*res);
  2300. return status;
  2301. }
  2302. static int decode_attr_space_used(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *used)
  2303. {
  2304. uint32_t *p;
  2305. *used = 0;
  2306. if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_USED - 1U)))
  2307. return -EIO;
  2308. if (likely(bitmap[1] & FATTR4_WORD1_SPACE_USED)) {
  2309. READ_BUF(8);
  2310. READ64(*used);
  2311. bitmap[1] &= ~FATTR4_WORD1_SPACE_USED;
  2312. }
  2313. dprintk("%s: space used=%Lu\n", __FUNCTION__,
  2314. (unsigned long long)*used);
  2315. return 0;
  2316. }
  2317. static int decode_attr_time(struct xdr_stream *xdr, struct timespec *time)
  2318. {
  2319. uint32_t *p;
  2320. uint64_t sec;
  2321. uint32_t nsec;
  2322. READ_BUF(12);
  2323. READ64(sec);
  2324. READ32(nsec);
  2325. time->tv_sec = (time_t)sec;
  2326. time->tv_nsec = (long)nsec;
  2327. return 0;
  2328. }
  2329. static int decode_attr_time_access(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
  2330. {
  2331. int status = 0;
  2332. time->tv_sec = 0;
  2333. time->tv_nsec = 0;
  2334. if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_ACCESS - 1U)))
  2335. return -EIO;
  2336. if (likely(bitmap[1] & FATTR4_WORD1_TIME_ACCESS)) {
  2337. status = decode_attr_time(xdr, time);
  2338. bitmap[1] &= ~FATTR4_WORD1_TIME_ACCESS;
  2339. }
  2340. dprintk("%s: atime=%ld\n", __FUNCTION__, (long)time->tv_sec);
  2341. return status;
  2342. }
  2343. static int decode_attr_time_metadata(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
  2344. {
  2345. int status = 0;
  2346. time->tv_sec = 0;
  2347. time->tv_nsec = 0;
  2348. if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_METADATA - 1U)))
  2349. return -EIO;
  2350. if (likely(bitmap[1] & FATTR4_WORD1_TIME_METADATA)) {
  2351. status = decode_attr_time(xdr, time);
  2352. bitmap[1] &= ~FATTR4_WORD1_TIME_METADATA;
  2353. }
  2354. dprintk("%s: ctime=%ld\n", __FUNCTION__, (long)time->tv_sec);
  2355. return status;
  2356. }
  2357. static int decode_attr_time_modify(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
  2358. {
  2359. int status = 0;
  2360. time->tv_sec = 0;
  2361. time->tv_nsec = 0;
  2362. if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_MODIFY - 1U)))
  2363. return -EIO;
  2364. if (likely(bitmap[1] & FATTR4_WORD1_TIME_MODIFY)) {
  2365. status = decode_attr_time(xdr, time);
  2366. bitmap[1] &= ~FATTR4_WORD1_TIME_MODIFY;
  2367. }
  2368. dprintk("%s: mtime=%ld\n", __FUNCTION__, (long)time->tv_sec);
  2369. return status;
  2370. }
  2371. static int verify_attr_len(struct xdr_stream *xdr, uint32_t *savep, uint32_t attrlen)
  2372. {
  2373. unsigned int attrwords = XDR_QUADLEN(attrlen);
  2374. unsigned int nwords = xdr->p - savep;
  2375. if (unlikely(attrwords != nwords)) {
  2376. printk(KERN_WARNING "%s: server returned incorrect attribute length: %u %c %u\n",
  2377. __FUNCTION__,
  2378. attrwords << 2,
  2379. (attrwords < nwords) ? '<' : '>',
  2380. nwords << 2);
  2381. return -EIO;
  2382. }
  2383. return 0;
  2384. }
  2385. static int decode_change_info(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
  2386. {
  2387. uint32_t *p;
  2388. READ_BUF(20);
  2389. READ32(cinfo->atomic);
  2390. READ64(cinfo->before);
  2391. READ64(cinfo->after);
  2392. return 0;
  2393. }
  2394. static int decode_access(struct xdr_stream *xdr, struct nfs4_accessres *access)
  2395. {
  2396. uint32_t *p;
  2397. uint32_t supp, acc;
  2398. int status;
  2399. status = decode_op_hdr(xdr, OP_ACCESS);
  2400. if (status)
  2401. return status;
  2402. READ_BUF(8);
  2403. READ32(supp);
  2404. READ32(acc);
  2405. access->supported = supp;
  2406. access->access = acc;
  2407. return 0;
  2408. }
  2409. static int decode_close(struct xdr_stream *xdr, struct nfs_closeres *res)
  2410. {
  2411. uint32_t *p;
  2412. int status;
  2413. status = decode_op_hdr(xdr, OP_CLOSE);
  2414. if (status)
  2415. return status;
  2416. READ_BUF(sizeof(res->stateid.data));
  2417. COPYMEM(res->stateid.data, sizeof(res->stateid.data));
  2418. return 0;
  2419. }
  2420. static int decode_commit(struct xdr_stream *xdr, struct nfs_writeres *res)
  2421. {
  2422. uint32_t *p;
  2423. int status;
  2424. status = decode_op_hdr(xdr, OP_COMMIT);
  2425. if (status)
  2426. return status;
  2427. READ_BUF(8);
  2428. COPYMEM(res->verf->verifier, 8);
  2429. return 0;
  2430. }
  2431. static int decode_create(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
  2432. {
  2433. uint32_t *p;
  2434. uint32_t bmlen;
  2435. int status;
  2436. status = decode_op_hdr(xdr, OP_CREATE);
  2437. if (status)
  2438. return status;
  2439. if ((status = decode_change_info(xdr, cinfo)))
  2440. return status;
  2441. READ_BUF(4);
  2442. READ32(bmlen);
  2443. READ_BUF(bmlen << 2);
  2444. return 0;
  2445. }
  2446. static int decode_server_caps(struct xdr_stream *xdr, struct nfs4_server_caps_res *res)
  2447. {
  2448. uint32_t *savep;
  2449. uint32_t attrlen,
  2450. bitmap[2] = {0};
  2451. int status;
  2452. if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
  2453. goto xdr_error;
  2454. if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
  2455. goto xdr_error;
  2456. if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
  2457. goto xdr_error;
  2458. if ((status = decode_attr_supported(xdr, bitmap, res->attr_bitmask)) != 0)
  2459. goto xdr_error;
  2460. if ((status = decode_attr_link_support(xdr, bitmap, &res->has_links)) != 0)
  2461. goto xdr_error;
  2462. if ((status = decode_attr_symlink_support(xdr, bitmap, &res->has_symlinks)) != 0)
  2463. goto xdr_error;
  2464. if ((status = decode_attr_aclsupport(xdr, bitmap, &res->acl_bitmask)) != 0)
  2465. goto xdr_error;
  2466. status = verify_attr_len(xdr, savep, attrlen);
  2467. xdr_error:
  2468. dprintk("%s: xdr returned %d!\n", __FUNCTION__, -status);
  2469. return status;
  2470. }
  2471. static int decode_statfs(struct xdr_stream *xdr, struct nfs_fsstat *fsstat)
  2472. {
  2473. uint32_t *savep;
  2474. uint32_t attrlen,
  2475. bitmap[2] = {0};
  2476. int status;
  2477. if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
  2478. goto xdr_error;
  2479. if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
  2480. goto xdr_error;
  2481. if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
  2482. goto xdr_error;
  2483. if ((status = decode_attr_files_avail(xdr, bitmap, &fsstat->afiles)) != 0)
  2484. goto xdr_error;
  2485. if ((status = decode_attr_files_free(xdr, bitmap, &fsstat->ffiles)) != 0)
  2486. goto xdr_error;
  2487. if ((status = decode_attr_files_total(xdr, bitmap, &fsstat->tfiles)) != 0)
  2488. goto xdr_error;
  2489. if ((status = decode_attr_space_avail(xdr, bitmap, &fsstat->abytes)) != 0)
  2490. goto xdr_error;
  2491. if ((status = decode_attr_space_free(xdr, bitmap, &fsstat->fbytes)) != 0)
  2492. goto xdr_error;
  2493. if ((status = decode_attr_space_total(xdr, bitmap, &fsstat->tbytes)) != 0)
  2494. goto xdr_error;
  2495. status = verify_attr_len(xdr, savep, attrlen);
  2496. xdr_error:
  2497. dprintk("%s: xdr returned %d!\n", __FUNCTION__, -status);
  2498. return status;
  2499. }
  2500. static int decode_pathconf(struct xdr_stream *xdr, struct nfs_pathconf *pathconf)
  2501. {
  2502. uint32_t *savep;
  2503. uint32_t attrlen,
  2504. bitmap[2] = {0};
  2505. int status;
  2506. if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
  2507. goto xdr_error;
  2508. if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
  2509. goto xdr_error;
  2510. if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
  2511. goto xdr_error;
  2512. if ((status = decode_attr_maxlink(xdr, bitmap, &pathconf->max_link)) != 0)
  2513. goto xdr_error;
  2514. if ((status = decode_attr_maxname(xdr, bitmap, &pathconf->max_namelen)) != 0)
  2515. goto xdr_error;
  2516. status = verify_attr_len(xdr, savep, attrlen);
  2517. xdr_error:
  2518. dprintk("%s: xdr returned %d!\n", __FUNCTION__, -status);
  2519. return status;
  2520. }
  2521. static int decode_getfattr(struct xdr_stream *xdr, struct nfs_fattr *fattr, const struct nfs_server *server)
  2522. {
  2523. uint32_t *savep;
  2524. uint32_t attrlen,
  2525. bitmap[2] = {0},
  2526. type;
  2527. int status, fmode = 0;
  2528. if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
  2529. goto xdr_error;
  2530. if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
  2531. goto xdr_error;
  2532. fattr->bitmap[0] = bitmap[0];
  2533. fattr->bitmap[1] = bitmap[1];
  2534. if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
  2535. goto xdr_error;
  2536. if ((status = decode_attr_type(xdr, bitmap, &type)) != 0)
  2537. goto xdr_error;
  2538. fattr->type = nfs_type2fmt[type].nfs2type;
  2539. fmode = nfs_type2fmt[type].mode;
  2540. if ((status = decode_attr_change(xdr, bitmap, &fattr->change_attr)) != 0)
  2541. goto xdr_error;
  2542. if ((status = decode_attr_size(xdr, bitmap, &fattr->size)) != 0)
  2543. goto xdr_error;
  2544. if ((status = decode_attr_fsid(xdr, bitmap, &fattr->fsid_u.nfs4)) != 0)
  2545. goto xdr_error;
  2546. if ((status = decode_attr_fileid(xdr, bitmap, &fattr->fileid)) != 0)
  2547. goto xdr_error;
  2548. if ((status = decode_attr_mode(xdr, bitmap, &fattr->mode)) != 0)
  2549. goto xdr_error;
  2550. fattr->mode |= fmode;
  2551. if ((status = decode_attr_nlink(xdr, bitmap, &fattr->nlink)) != 0)
  2552. goto xdr_error;
  2553. if ((status = decode_attr_owner(xdr, bitmap, server->nfs4_state, &fattr->uid)) != 0)
  2554. goto xdr_error;
  2555. if ((status = decode_attr_group(xdr, bitmap, server->nfs4_state, &fattr->gid)) != 0)
  2556. goto xdr_error;
  2557. if ((status = decode_attr_rdev(xdr, bitmap, &fattr->rdev)) != 0)
  2558. goto xdr_error;
  2559. if ((status = decode_attr_space_used(xdr, bitmap, &fattr->du.nfs3.used)) != 0)
  2560. goto xdr_error;
  2561. if ((status = decode_attr_time_access(xdr, bitmap, &fattr->atime)) != 0)
  2562. goto xdr_error;
  2563. if ((status = decode_attr_time_metadata(xdr, bitmap, &fattr->ctime)) != 0)
  2564. goto xdr_error;
  2565. if ((status = decode_attr_time_modify(xdr, bitmap, &fattr->mtime)) != 0)
  2566. goto xdr_error;
  2567. if ((status = verify_attr_len(xdr, savep, attrlen)) == 0)
  2568. fattr->valid = NFS_ATTR_FATTR | NFS_ATTR_FATTR_V3 | NFS_ATTR_FATTR_V4;
  2569. xdr_error:
  2570. dprintk("%s: xdr returned %d\n", __FUNCTION__, -status);
  2571. return status;
  2572. }
  2573. static int decode_fsinfo(struct xdr_stream *xdr, struct nfs_fsinfo *fsinfo)
  2574. {
  2575. uint32_t *savep;
  2576. uint32_t attrlen, bitmap[2];
  2577. int status;
  2578. if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
  2579. goto xdr_error;
  2580. if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
  2581. goto xdr_error;
  2582. if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
  2583. goto xdr_error;
  2584. fsinfo->rtmult = fsinfo->wtmult = 512; /* ??? */
  2585. if ((status = decode_attr_lease_time(xdr, bitmap, &fsinfo->lease_time)) != 0)
  2586. goto xdr_error;
  2587. if ((status = decode_attr_maxfilesize(xdr, bitmap, &fsinfo->maxfilesize)) != 0)
  2588. goto xdr_error;
  2589. if ((status = decode_attr_maxread(xdr, bitmap, &fsinfo->rtmax)) != 0)
  2590. goto xdr_error;
  2591. fsinfo->rtpref = fsinfo->dtpref = fsinfo->rtmax;
  2592. if ((status = decode_attr_maxwrite(xdr, bitmap, &fsinfo->wtmax)) != 0)
  2593. goto xdr_error;
  2594. fsinfo->wtpref = fsinfo->wtmax;
  2595. status = verify_attr_len(xdr, savep, attrlen);
  2596. xdr_error:
  2597. dprintk("%s: xdr returned %d!\n", __FUNCTION__, -status);
  2598. return status;
  2599. }
  2600. static int decode_getfh(struct xdr_stream *xdr, struct nfs_fh *fh)
  2601. {
  2602. uint32_t *p;
  2603. uint32_t len;
  2604. int status;
  2605. status = decode_op_hdr(xdr, OP_GETFH);
  2606. if (status)
  2607. return status;
  2608. /* Zero handle first to allow comparisons */
  2609. memset(fh, 0, sizeof(*fh));
  2610. READ_BUF(4);
  2611. READ32(len);
  2612. if (len > NFS4_FHSIZE)
  2613. return -EIO;
  2614. fh->size = len;
  2615. READ_BUF(len);
  2616. COPYMEM(fh->data, len);
  2617. return 0;
  2618. }
  2619. static int decode_link(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
  2620. {
  2621. int status;
  2622. status = decode_op_hdr(xdr, OP_LINK);
  2623. if (status)
  2624. return status;
  2625. return decode_change_info(xdr, cinfo);
  2626. }
  2627. /*
  2628. * We create the owner, so we know a proper owner.id length is 4.
  2629. */
  2630. static int decode_lock_denied (struct xdr_stream *xdr, struct nfs_lock_denied *denied)
  2631. {
  2632. uint32_t *p;
  2633. uint32_t namelen;
  2634. READ_BUF(32);
  2635. READ64(denied->offset);
  2636. READ64(denied->length);
  2637. READ32(denied->type);
  2638. READ64(denied->owner.clientid);
  2639. READ32(namelen);
  2640. READ_BUF(namelen);
  2641. if (namelen == 4)
  2642. READ32(denied->owner.id);
  2643. return -NFS4ERR_DENIED;
  2644. }
  2645. static int decode_lock(struct xdr_stream *xdr, struct nfs_lockres *res)
  2646. {
  2647. uint32_t *p;
  2648. int status;
  2649. status = decode_op_hdr(xdr, OP_LOCK);
  2650. if (status == 0) {
  2651. READ_BUF(sizeof(res->u.stateid.data));
  2652. COPYMEM(res->u.stateid.data, sizeof(res->u.stateid.data));
  2653. } else if (status == -NFS4ERR_DENIED)
  2654. return decode_lock_denied(xdr, &res->u.denied);
  2655. return status;
  2656. }
  2657. static int decode_lockt(struct xdr_stream *xdr, struct nfs_lockres *res)
  2658. {
  2659. int status;
  2660. status = decode_op_hdr(xdr, OP_LOCKT);
  2661. if (status == -NFS4ERR_DENIED)
  2662. return decode_lock_denied(xdr, &res->u.denied);
  2663. return status;
  2664. }
  2665. static int decode_locku(struct xdr_stream *xdr, struct nfs_lockres *res)
  2666. {
  2667. uint32_t *p;
  2668. int status;
  2669. status = decode_op_hdr(xdr, OP_LOCKU);
  2670. if (status == 0) {
  2671. READ_BUF(sizeof(res->u.stateid.data));
  2672. COPYMEM(res->u.stateid.data, sizeof(res->u.stateid.data));
  2673. }
  2674. return status;
  2675. }
  2676. static int decode_lookup(struct xdr_stream *xdr)
  2677. {
  2678. return decode_op_hdr(xdr, OP_LOOKUP);
  2679. }
  2680. /* This is too sick! */
  2681. static int decode_space_limit(struct xdr_stream *xdr, u64 *maxsize)
  2682. {
  2683. uint32_t *p;
  2684. uint32_t limit_type, nblocks, blocksize;
  2685. READ_BUF(12);
  2686. READ32(limit_type);
  2687. switch (limit_type) {
  2688. case 1:
  2689. READ64(*maxsize);
  2690. break;
  2691. case 2:
  2692. READ32(nblocks);
  2693. READ32(blocksize);
  2694. *maxsize = (uint64_t)nblocks * (uint64_t)blocksize;
  2695. }
  2696. return 0;
  2697. }
  2698. static int decode_delegation(struct xdr_stream *xdr, struct nfs_openres *res)
  2699. {
  2700. uint32_t *p;
  2701. uint32_t delegation_type;
  2702. READ_BUF(4);
  2703. READ32(delegation_type);
  2704. if (delegation_type == NFS4_OPEN_DELEGATE_NONE) {
  2705. res->delegation_type = 0;
  2706. return 0;
  2707. }
  2708. READ_BUF(20);
  2709. COPYMEM(res->delegation.data, sizeof(res->delegation.data));
  2710. READ32(res->do_recall);
  2711. switch (delegation_type) {
  2712. case NFS4_OPEN_DELEGATE_READ:
  2713. res->delegation_type = FMODE_READ;
  2714. break;
  2715. case NFS4_OPEN_DELEGATE_WRITE:
  2716. res->delegation_type = FMODE_WRITE|FMODE_READ;
  2717. if (decode_space_limit(xdr, &res->maxsize) < 0)
  2718. return -EIO;
  2719. }
  2720. return decode_ace(xdr, NULL, res->server->nfs4_state);
  2721. }
  2722. static int decode_open(struct xdr_stream *xdr, struct nfs_openres *res)
  2723. {
  2724. uint32_t *p;
  2725. uint32_t bmlen;
  2726. int status;
  2727. status = decode_op_hdr(xdr, OP_OPEN);
  2728. if (status)
  2729. return status;
  2730. READ_BUF(sizeof(res->stateid.data));
  2731. COPYMEM(res->stateid.data, sizeof(res->stateid.data));
  2732. decode_change_info(xdr, &res->cinfo);
  2733. READ_BUF(8);
  2734. READ32(res->rflags);
  2735. READ32(bmlen);
  2736. if (bmlen > 10)
  2737. goto xdr_error;
  2738. READ_BUF(bmlen << 2);
  2739. p += bmlen;
  2740. return decode_delegation(xdr, res);
  2741. xdr_error:
  2742. dprintk("%s: Bitmap too large! Length = %u\n", __FUNCTION__, bmlen);
  2743. return -EIO;
  2744. }
  2745. static int decode_open_confirm(struct xdr_stream *xdr, struct nfs_open_confirmres *res)
  2746. {
  2747. uint32_t *p;
  2748. int status;
  2749. status = decode_op_hdr(xdr, OP_OPEN_CONFIRM);
  2750. if (status)
  2751. return status;
  2752. READ_BUF(sizeof(res->stateid.data));
  2753. COPYMEM(res->stateid.data, sizeof(res->stateid.data));
  2754. return 0;
  2755. }
  2756. static int decode_open_downgrade(struct xdr_stream *xdr, struct nfs_closeres *res)
  2757. {
  2758. uint32_t *p;
  2759. int status;
  2760. status = decode_op_hdr(xdr, OP_OPEN_DOWNGRADE);
  2761. if (status)
  2762. return status;
  2763. READ_BUF(sizeof(res->stateid.data));
  2764. COPYMEM(res->stateid.data, sizeof(res->stateid.data));
  2765. return 0;
  2766. }
  2767. static int decode_putfh(struct xdr_stream *xdr)
  2768. {
  2769. return decode_op_hdr(xdr, OP_PUTFH);
  2770. }
  2771. static int decode_putrootfh(struct xdr_stream *xdr)
  2772. {
  2773. return decode_op_hdr(xdr, OP_PUTROOTFH);
  2774. }
  2775. static int decode_read(struct xdr_stream *xdr, struct rpc_rqst *req, struct nfs_readres *res)
  2776. {
  2777. struct kvec *iov = req->rq_rcv_buf.head;
  2778. uint32_t *p;
  2779. uint32_t count, eof, recvd, hdrlen;
  2780. int status;
  2781. status = decode_op_hdr(xdr, OP_READ);
  2782. if (status)
  2783. return status;
  2784. READ_BUF(8);
  2785. READ32(eof);
  2786. READ32(count);
  2787. hdrlen = (u8 *) p - (u8 *) iov->iov_base;
  2788. recvd = req->rq_rcv_buf.len - hdrlen;
  2789. if (count > recvd) {
  2790. printk(KERN_WARNING "NFS: server cheating in read reply: "
  2791. "count %u > recvd %u\n", count, recvd);
  2792. count = recvd;
  2793. eof = 0;
  2794. }
  2795. xdr_read_pages(xdr, count);
  2796. res->eof = eof;
  2797. res->count = count;
  2798. return 0;
  2799. }
  2800. static int decode_readdir(struct xdr_stream *xdr, struct rpc_rqst *req, struct nfs4_readdir_res *readdir)
  2801. {
  2802. struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
  2803. struct page *page = *rcvbuf->pages;
  2804. struct kvec *iov = rcvbuf->head;
  2805. unsigned int nr, pglen = rcvbuf->page_len;
  2806. uint32_t *end, *entry, *p, *kaddr;
  2807. uint32_t len, attrlen;
  2808. int hdrlen, recvd, status;
  2809. status = decode_op_hdr(xdr, OP_READDIR);
  2810. if (status)
  2811. return status;
  2812. READ_BUF(8);
  2813. COPYMEM(readdir->verifier.data, 8);
  2814. dprintk("%s: verifier = 0x%x%x\n",
  2815. __FUNCTION__,
  2816. ((u32 *)readdir->verifier.data)[0],
  2817. ((u32 *)readdir->verifier.data)[1]);
  2818. hdrlen = (char *) p - (char *) iov->iov_base;
  2819. recvd = rcvbuf->len - hdrlen;
  2820. if (pglen > recvd)
  2821. pglen = recvd;
  2822. xdr_read_pages(xdr, pglen);
  2823. BUG_ON(pglen + readdir->pgbase > PAGE_CACHE_SIZE);
  2824. kaddr = p = (uint32_t *) kmap_atomic(page, KM_USER0);
  2825. end = (uint32_t *) ((char *)p + pglen + readdir->pgbase);
  2826. entry = p;
  2827. for (nr = 0; *p++; nr++) {
  2828. if (p + 3 > end)
  2829. goto short_pkt;
  2830. dprintk("cookie = %Lu, ", *((unsigned long long *)p));
  2831. p += 2; /* cookie */
  2832. len = ntohl(*p++); /* filename length */
  2833. if (len > NFS4_MAXNAMLEN) {
  2834. printk(KERN_WARNING "NFS: giant filename in readdir (len 0x%x)\n", len);
  2835. goto err_unmap;
  2836. }
  2837. dprintk("filename = %*s\n", len, (char *)p);
  2838. p += XDR_QUADLEN(len);
  2839. if (p + 1 > end)
  2840. goto short_pkt;
  2841. len = ntohl(*p++); /* bitmap length */
  2842. p += len;
  2843. if (p + 1 > end)
  2844. goto short_pkt;
  2845. attrlen = XDR_QUADLEN(ntohl(*p++));
  2846. p += attrlen; /* attributes */
  2847. if (p + 2 > end)
  2848. goto short_pkt;
  2849. entry = p;
  2850. }
  2851. if (!nr && (entry[0] != 0 || entry[1] == 0))
  2852. goto short_pkt;
  2853. out:
  2854. kunmap_atomic(kaddr, KM_USER0);
  2855. return 0;
  2856. short_pkt:
  2857. dprintk("%s: short packet at entry %d\n", __FUNCTION__, nr);
  2858. entry[0] = entry[1] = 0;
  2859. /* truncate listing ? */
  2860. if (!nr) {
  2861. printk(KERN_NOTICE "NFS: readdir reply truncated!\n");
  2862. entry[1] = 1;
  2863. }
  2864. goto out;
  2865. err_unmap:
  2866. kunmap_atomic(kaddr, KM_USER0);
  2867. return -errno_NFSERR_IO;
  2868. }
  2869. static int decode_readlink(struct xdr_stream *xdr, struct rpc_rqst *req)
  2870. {
  2871. struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
  2872. struct kvec *iov = rcvbuf->head;
  2873. int hdrlen, len, recvd;
  2874. uint32_t *p;
  2875. char *kaddr;
  2876. int status;
  2877. status = decode_op_hdr(xdr, OP_READLINK);
  2878. if (status)
  2879. return status;
  2880. /* Convert length of symlink */
  2881. READ_BUF(4);
  2882. READ32(len);
  2883. if (len >= rcvbuf->page_len || len <= 0) {
  2884. dprintk(KERN_WARNING "nfs: server returned giant symlink!\n");
  2885. return -ENAMETOOLONG;
  2886. }
  2887. hdrlen = (char *) xdr->p - (char *) iov->iov_base;
  2888. recvd = req->rq_rcv_buf.len - hdrlen;
  2889. if (recvd < len) {
  2890. printk(KERN_WARNING "NFS: server cheating in readlink reply: "
  2891. "count %u > recvd %u\n", len, recvd);
  2892. return -EIO;
  2893. }
  2894. xdr_read_pages(xdr, len);
  2895. /*
  2896. * The XDR encode routine has set things up so that
  2897. * the link text will be copied directly into the
  2898. * buffer. We just have to do overflow-checking,
  2899. * and and null-terminate the text (the VFS expects
  2900. * null-termination).
  2901. */
  2902. kaddr = (char *)kmap_atomic(rcvbuf->pages[0], KM_USER0);
  2903. kaddr[len+rcvbuf->page_base] = '\0';
  2904. kunmap_atomic(kaddr, KM_USER0);
  2905. return 0;
  2906. }
  2907. static int decode_remove(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
  2908. {
  2909. int status;
  2910. status = decode_op_hdr(xdr, OP_REMOVE);
  2911. if (status)
  2912. goto out;
  2913. status = decode_change_info(xdr, cinfo);
  2914. out:
  2915. return status;
  2916. }
  2917. static int decode_rename(struct xdr_stream *xdr, struct nfs4_change_info *old_cinfo,
  2918. struct nfs4_change_info *new_cinfo)
  2919. {
  2920. int status;
  2921. status = decode_op_hdr(xdr, OP_RENAME);
  2922. if (status)
  2923. goto out;
  2924. if ((status = decode_change_info(xdr, old_cinfo)))
  2925. goto out;
  2926. status = decode_change_info(xdr, new_cinfo);
  2927. out:
  2928. return status;
  2929. }
  2930. static int decode_renew(struct xdr_stream *xdr)
  2931. {
  2932. return decode_op_hdr(xdr, OP_RENEW);
  2933. }
  2934. static int
  2935. decode_restorefh(struct xdr_stream *xdr)
  2936. {
  2937. return decode_op_hdr(xdr, OP_RESTOREFH);
  2938. }
  2939. static int decode_getacl(struct xdr_stream *xdr, struct rpc_rqst *req,
  2940. size_t *acl_len)
  2941. {
  2942. uint32_t *savep;
  2943. uint32_t attrlen,
  2944. bitmap[2] = {0};
  2945. struct kvec *iov = req->rq_rcv_buf.head;
  2946. int status;
  2947. *acl_len = 0;
  2948. if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
  2949. goto out;
  2950. if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
  2951. goto out;
  2952. if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
  2953. goto out;
  2954. if (unlikely(bitmap[0] & (FATTR4_WORD0_ACL - 1U)))
  2955. return -EIO;
  2956. if (likely(bitmap[0] & FATTR4_WORD0_ACL)) {
  2957. int hdrlen, recvd;
  2958. /* We ignore &savep and don't do consistency checks on
  2959. * the attr length. Let userspace figure it out.... */
  2960. hdrlen = (u8 *)xdr->p - (u8 *)iov->iov_base;
  2961. recvd = req->rq_rcv_buf.len - hdrlen;
  2962. if (attrlen > recvd) {
  2963. printk(KERN_WARNING "NFS: server cheating in getattr"
  2964. " acl reply: attrlen %u > recvd %u\n",
  2965. attrlen, recvd);
  2966. return -EINVAL;
  2967. }
  2968. if (attrlen <= *acl_len)
  2969. xdr_read_pages(xdr, attrlen);
  2970. *acl_len = attrlen;
  2971. } else
  2972. status = -EOPNOTSUPP;
  2973. out:
  2974. return status;
  2975. }
  2976. static int
  2977. decode_savefh(struct xdr_stream *xdr)
  2978. {
  2979. return decode_op_hdr(xdr, OP_SAVEFH);
  2980. }
  2981. static int decode_setattr(struct xdr_stream *xdr, struct nfs_setattrres *res)
  2982. {
  2983. uint32_t *p;
  2984. uint32_t bmlen;
  2985. int status;
  2986. status = decode_op_hdr(xdr, OP_SETATTR);
  2987. if (status)
  2988. return status;
  2989. READ_BUF(4);
  2990. READ32(bmlen);
  2991. READ_BUF(bmlen << 2);
  2992. return 0;
  2993. }
  2994. static int decode_setclientid(struct xdr_stream *xdr, struct nfs4_client *clp)
  2995. {
  2996. uint32_t *p;
  2997. uint32_t opnum;
  2998. int32_t nfserr;
  2999. READ_BUF(8);
  3000. READ32(opnum);
  3001. if (opnum != OP_SETCLIENTID) {
  3002. printk(KERN_NOTICE
  3003. "nfs4_decode_setclientid: Server returned operation"
  3004. " %d\n", opnum);
  3005. return -EIO;
  3006. }
  3007. READ32(nfserr);
  3008. if (nfserr == NFS_OK) {
  3009. READ_BUF(8 + sizeof(clp->cl_confirm.data));
  3010. READ64(clp->cl_clientid);
  3011. COPYMEM(clp->cl_confirm.data, sizeof(clp->cl_confirm.data));
  3012. } else if (nfserr == NFSERR_CLID_INUSE) {
  3013. uint32_t len;
  3014. /* skip netid string */
  3015. READ_BUF(4);
  3016. READ32(len);
  3017. READ_BUF(len);
  3018. /* skip uaddr string */
  3019. READ_BUF(4);
  3020. READ32(len);
  3021. READ_BUF(len);
  3022. return -NFSERR_CLID_INUSE;
  3023. } else
  3024. return -nfs_stat_to_errno(nfserr);
  3025. return 0;
  3026. }
  3027. static int decode_setclientid_confirm(struct xdr_stream *xdr)
  3028. {
  3029. return decode_op_hdr(xdr, OP_SETCLIENTID_CONFIRM);
  3030. }
  3031. static int decode_write(struct xdr_stream *xdr, struct nfs_writeres *res)
  3032. {
  3033. uint32_t *p;
  3034. int status;
  3035. status = decode_op_hdr(xdr, OP_WRITE);
  3036. if (status)
  3037. return status;
  3038. READ_BUF(16);
  3039. READ32(res->count);
  3040. READ32(res->verf->committed);
  3041. COPYMEM(res->verf->verifier, 8);
  3042. return 0;
  3043. }
  3044. static int decode_delegreturn(struct xdr_stream *xdr)
  3045. {
  3046. return decode_op_hdr(xdr, OP_DELEGRETURN);
  3047. }
  3048. /*
  3049. * Decode OPEN_DOWNGRADE response
  3050. */
  3051. static int nfs4_xdr_dec_open_downgrade(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_closeres *res)
  3052. {
  3053. struct xdr_stream xdr;
  3054. struct compound_hdr hdr;
  3055. int status;
  3056. xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
  3057. status = decode_compound_hdr(&xdr, &hdr);
  3058. if (status)
  3059. goto out;
  3060. status = decode_putfh(&xdr);
  3061. if (status)
  3062. goto out;
  3063. status = decode_open_downgrade(&xdr, res);
  3064. if (status != 0)
  3065. goto out;
  3066. decode_getfattr(&xdr, res->fattr, res->server);
  3067. out:
  3068. return status;
  3069. }
  3070. /*
  3071. * END OF "GENERIC" DECODE ROUTINES.
  3072. */
  3073. /*
  3074. * Decode ACCESS response
  3075. */
  3076. static int nfs4_xdr_dec_access(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_accessres *res)
  3077. {
  3078. struct xdr_stream xdr;
  3079. struct compound_hdr hdr;
  3080. int status;
  3081. xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
  3082. if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
  3083. goto out;
  3084. if ((status = decode_putfh(&xdr)) == 0)
  3085. status = decode_access(&xdr, res);
  3086. out:
  3087. return status;
  3088. }
  3089. /*
  3090. * Decode LOOKUP response
  3091. */
  3092. static int nfs4_xdr_dec_lookup(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_lookup_res *res)
  3093. {
  3094. struct xdr_stream xdr;
  3095. struct compound_hdr hdr;
  3096. int status;
  3097. xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
  3098. if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
  3099. goto out;
  3100. if ((status = decode_putfh(&xdr)) != 0)
  3101. goto out;
  3102. if ((status = decode_lookup(&xdr)) != 0)
  3103. goto out;
  3104. if ((status = decode_getfh(&xdr, res->fh)) != 0)
  3105. goto out;
  3106. status = decode_getfattr(&xdr, res->fattr, res->server);
  3107. out:
  3108. return status;
  3109. }
  3110. /*
  3111. * Decode LOOKUP_ROOT response
  3112. */
  3113. static int nfs4_xdr_dec_lookup_root(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_lookup_res *res)
  3114. {
  3115. struct xdr_stream xdr;
  3116. struct compound_hdr hdr;
  3117. int status;
  3118. xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
  3119. if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
  3120. goto out;
  3121. if ((status = decode_putrootfh(&xdr)) != 0)
  3122. goto out;
  3123. if ((status = decode_getfh(&xdr, res->fh)) == 0)
  3124. status = decode_getfattr(&xdr, res->fattr, res->server);
  3125. out:
  3126. return status;
  3127. }
  3128. /*
  3129. * Decode REMOVE response
  3130. */
  3131. static int nfs4_xdr_dec_remove(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_remove_res *res)
  3132. {
  3133. struct xdr_stream xdr;
  3134. struct compound_hdr hdr;
  3135. int status;
  3136. xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
  3137. if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
  3138. goto out;
  3139. if ((status = decode_putfh(&xdr)) != 0)
  3140. goto out;
  3141. if ((status = decode_remove(&xdr, &res->cinfo)) != 0)
  3142. goto out;
  3143. decode_getfattr(&xdr, res->dir_attr, res->server);
  3144. out:
  3145. return status;
  3146. }
  3147. /*
  3148. * Decode RENAME response
  3149. */
  3150. static int nfs4_xdr_dec_rename(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_rename_res *res)
  3151. {
  3152. struct xdr_stream xdr;
  3153. struct compound_hdr hdr;
  3154. int status;
  3155. xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
  3156. if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
  3157. goto out;
  3158. if ((status = decode_putfh(&xdr)) != 0)
  3159. goto out;
  3160. if ((status = decode_savefh(&xdr)) != 0)
  3161. goto out;
  3162. if ((status = decode_putfh(&xdr)) != 0)
  3163. goto out;
  3164. if ((status = decode_rename(&xdr, &res->old_cinfo, &res->new_cinfo)) != 0)
  3165. goto out;
  3166. /* Current FH is target directory */
  3167. if (decode_getfattr(&xdr, res->new_fattr, res->server) != 0)
  3168. goto out;
  3169. if ((status = decode_restorefh(&xdr)) != 0)
  3170. goto out;
  3171. decode_getfattr(&xdr, res->old_fattr, res->server);
  3172. out:
  3173. return status;
  3174. }
  3175. /*
  3176. * Decode LINK response
  3177. */
  3178. static int nfs4_xdr_dec_link(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_link_res *res)
  3179. {
  3180. struct xdr_stream xdr;
  3181. struct compound_hdr hdr;
  3182. int status;
  3183. xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
  3184. if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
  3185. goto out;
  3186. if ((status = decode_putfh(&xdr)) != 0)
  3187. goto out;
  3188. if ((status = decode_savefh(&xdr)) != 0)
  3189. goto out;
  3190. if ((status = decode_putfh(&xdr)) != 0)
  3191. goto out;
  3192. if ((status = decode_link(&xdr, &res->cinfo)) != 0)
  3193. goto out;
  3194. /*
  3195. * Note order: OP_LINK leaves the directory as the current
  3196. * filehandle.
  3197. */
  3198. if (decode_getfattr(&xdr, res->dir_attr, res->server) != 0)
  3199. goto out;
  3200. if ((status = decode_restorefh(&xdr)) != 0)
  3201. goto out;
  3202. decode_getfattr(&xdr, res->fattr, res->server);
  3203. out:
  3204. return status;
  3205. }
  3206. /*
  3207. * Decode CREATE response
  3208. */
  3209. static int nfs4_xdr_dec_create(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_create_res *res)
  3210. {
  3211. struct xdr_stream xdr;
  3212. struct compound_hdr hdr;
  3213. int status;
  3214. xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
  3215. if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
  3216. goto out;
  3217. if ((status = decode_putfh(&xdr)) != 0)
  3218. goto out;
  3219. if ((status = decode_savefh(&xdr)) != 0)
  3220. goto out;
  3221. if ((status = decode_create(&xdr,&res->dir_cinfo)) != 0)
  3222. goto out;
  3223. if ((status = decode_getfh(&xdr, res->fh)) != 0)
  3224. goto out;
  3225. if (decode_getfattr(&xdr, res->fattr, res->server) != 0)
  3226. goto out;
  3227. if ((status = decode_restorefh(&xdr)) != 0)
  3228. goto out;
  3229. decode_getfattr(&xdr, res->dir_fattr, res->server);
  3230. out:
  3231. return status;
  3232. }
  3233. /*
  3234. * Decode SYMLINK response
  3235. */
  3236. static int nfs4_xdr_dec_symlink(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_create_res *res)
  3237. {
  3238. return nfs4_xdr_dec_create(rqstp, p, res);
  3239. }
  3240. /*
  3241. * Decode GETATTR response
  3242. */
  3243. static int nfs4_xdr_dec_getattr(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_getattr_res *res)
  3244. {
  3245. struct xdr_stream xdr;
  3246. struct compound_hdr hdr;
  3247. int status;
  3248. xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
  3249. status = decode_compound_hdr(&xdr, &hdr);
  3250. if (status)
  3251. goto out;
  3252. status = decode_putfh(&xdr);
  3253. if (status)
  3254. goto out;
  3255. status = decode_getfattr(&xdr, res->fattr, res->server);
  3256. out:
  3257. return status;
  3258. }
  3259. /*
  3260. * Encode an SETACL request
  3261. */
  3262. static int
  3263. nfs4_xdr_enc_setacl(struct rpc_rqst *req, uint32_t *p, struct nfs_setaclargs *args)
  3264. {
  3265. struct xdr_stream xdr;
  3266. struct compound_hdr hdr = {
  3267. .nops = 2,
  3268. };
  3269. int status;
  3270. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  3271. encode_compound_hdr(&xdr, &hdr);
  3272. status = encode_putfh(&xdr, args->fh);
  3273. if (status)
  3274. goto out;
  3275. status = encode_setacl(&xdr, args);
  3276. out:
  3277. return status;
  3278. }
  3279. /*
  3280. * Decode SETACL response
  3281. */
  3282. static int
  3283. nfs4_xdr_dec_setacl(struct rpc_rqst *rqstp, uint32_t *p, void *res)
  3284. {
  3285. struct xdr_stream xdr;
  3286. struct compound_hdr hdr;
  3287. int status;
  3288. xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
  3289. status = decode_compound_hdr(&xdr, &hdr);
  3290. if (status)
  3291. goto out;
  3292. status = decode_putfh(&xdr);
  3293. if (status)
  3294. goto out;
  3295. status = decode_setattr(&xdr, res);
  3296. out:
  3297. return status;
  3298. }
  3299. /*
  3300. * Decode GETACL response
  3301. */
  3302. static int
  3303. nfs4_xdr_dec_getacl(struct rpc_rqst *rqstp, uint32_t *p, size_t *acl_len)
  3304. {
  3305. struct xdr_stream xdr;
  3306. struct compound_hdr hdr;
  3307. int status;
  3308. xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
  3309. status = decode_compound_hdr(&xdr, &hdr);
  3310. if (status)
  3311. goto out;
  3312. status = decode_putfh(&xdr);
  3313. if (status)
  3314. goto out;
  3315. status = decode_getacl(&xdr, rqstp, acl_len);
  3316. out:
  3317. return status;
  3318. }
  3319. /*
  3320. * Decode CLOSE response
  3321. */
  3322. static int nfs4_xdr_dec_close(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_closeres *res)
  3323. {
  3324. struct xdr_stream xdr;
  3325. struct compound_hdr hdr;
  3326. int status;
  3327. xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
  3328. status = decode_compound_hdr(&xdr, &hdr);
  3329. if (status)
  3330. goto out;
  3331. status = decode_putfh(&xdr);
  3332. if (status)
  3333. goto out;
  3334. status = decode_close(&xdr, res);
  3335. if (status != 0)
  3336. goto out;
  3337. /*
  3338. * Note: Server may do delete on close for this file
  3339. * in which case the getattr call will fail with
  3340. * an ESTALE error. Shouldn't be a problem,
  3341. * though, since fattr->valid will remain unset.
  3342. */
  3343. decode_getfattr(&xdr, res->fattr, res->server);
  3344. out:
  3345. return status;
  3346. }
  3347. /*
  3348. * Decode OPEN response
  3349. */
  3350. static int nfs4_xdr_dec_open(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_openres *res)
  3351. {
  3352. struct xdr_stream xdr;
  3353. struct compound_hdr hdr;
  3354. int status;
  3355. xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
  3356. status = decode_compound_hdr(&xdr, &hdr);
  3357. if (status)
  3358. goto out;
  3359. status = decode_putfh(&xdr);
  3360. if (status)
  3361. goto out;
  3362. status = decode_savefh(&xdr);
  3363. if (status)
  3364. goto out;
  3365. status = decode_open(&xdr, res);
  3366. if (status)
  3367. goto out;
  3368. status = decode_getfh(&xdr, &res->fh);
  3369. if (status)
  3370. goto out;
  3371. if (decode_getfattr(&xdr, res->f_attr, res->server) != 0)
  3372. goto out;
  3373. if ((status = decode_restorefh(&xdr)) != 0)
  3374. goto out;
  3375. decode_getfattr(&xdr, res->dir_attr, res->server);
  3376. out:
  3377. return status;
  3378. }
  3379. /*
  3380. * Decode OPEN_CONFIRM response
  3381. */
  3382. static int nfs4_xdr_dec_open_confirm(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_open_confirmres *res)
  3383. {
  3384. struct xdr_stream xdr;
  3385. struct compound_hdr hdr;
  3386. int status;
  3387. xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
  3388. status = decode_compound_hdr(&xdr, &hdr);
  3389. if (status)
  3390. goto out;
  3391. status = decode_putfh(&xdr);
  3392. if (status)
  3393. goto out;
  3394. status = decode_open_confirm(&xdr, res);
  3395. out:
  3396. return status;
  3397. }
  3398. /*
  3399. * Decode OPEN response
  3400. */
  3401. static int nfs4_xdr_dec_open_noattr(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_openres *res)
  3402. {
  3403. struct xdr_stream xdr;
  3404. struct compound_hdr hdr;
  3405. int status;
  3406. xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
  3407. status = decode_compound_hdr(&xdr, &hdr);
  3408. if (status)
  3409. goto out;
  3410. status = decode_putfh(&xdr);
  3411. if (status)
  3412. goto out;
  3413. status = decode_open(&xdr, res);
  3414. out:
  3415. return status;
  3416. }
  3417. /*
  3418. * Decode SETATTR response
  3419. */
  3420. static int nfs4_xdr_dec_setattr(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_setattrres *res)
  3421. {
  3422. struct xdr_stream xdr;
  3423. struct compound_hdr hdr;
  3424. int status;
  3425. xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
  3426. status = decode_compound_hdr(&xdr, &hdr);
  3427. if (status)
  3428. goto out;
  3429. status = decode_putfh(&xdr);
  3430. if (status)
  3431. goto out;
  3432. status = decode_setattr(&xdr, res);
  3433. if (status)
  3434. goto out;
  3435. status = decode_getfattr(&xdr, res->fattr, res->server);
  3436. if (status == NFS4ERR_DELAY)
  3437. status = 0;
  3438. out:
  3439. return status;
  3440. }
  3441. /*
  3442. * Decode LOCK response
  3443. */
  3444. static int nfs4_xdr_dec_lock(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_lockres *res)
  3445. {
  3446. struct xdr_stream xdr;
  3447. struct compound_hdr hdr;
  3448. int status;
  3449. xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
  3450. status = decode_compound_hdr(&xdr, &hdr);
  3451. if (status)
  3452. goto out;
  3453. status = decode_putfh(&xdr);
  3454. if (status)
  3455. goto out;
  3456. status = decode_lock(&xdr, res);
  3457. out:
  3458. return status;
  3459. }
  3460. /*
  3461. * Decode LOCKT response
  3462. */
  3463. static int nfs4_xdr_dec_lockt(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_lockres *res)
  3464. {
  3465. struct xdr_stream xdr;
  3466. struct compound_hdr hdr;
  3467. int status;
  3468. xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
  3469. status = decode_compound_hdr(&xdr, &hdr);
  3470. if (status)
  3471. goto out;
  3472. status = decode_putfh(&xdr);
  3473. if (status)
  3474. goto out;
  3475. status = decode_lockt(&xdr, res);
  3476. out:
  3477. return status;
  3478. }
  3479. /*
  3480. * Decode LOCKU response
  3481. */
  3482. static int nfs4_xdr_dec_locku(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_lockres *res)
  3483. {
  3484. struct xdr_stream xdr;
  3485. struct compound_hdr hdr;
  3486. int status;
  3487. xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
  3488. status = decode_compound_hdr(&xdr, &hdr);
  3489. if (status)
  3490. goto out;
  3491. status = decode_putfh(&xdr);
  3492. if (status)
  3493. goto out;
  3494. status = decode_locku(&xdr, res);
  3495. out:
  3496. return status;
  3497. }
  3498. /*
  3499. * Decode READLINK response
  3500. */
  3501. static int nfs4_xdr_dec_readlink(struct rpc_rqst *rqstp, uint32_t *p, void *res)
  3502. {
  3503. struct xdr_stream xdr;
  3504. struct compound_hdr hdr;
  3505. int status;
  3506. xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
  3507. status = decode_compound_hdr(&xdr, &hdr);
  3508. if (status)
  3509. goto out;
  3510. status = decode_putfh(&xdr);
  3511. if (status)
  3512. goto out;
  3513. status = decode_readlink(&xdr, rqstp);
  3514. out:
  3515. return status;
  3516. }
  3517. /*
  3518. * Decode READDIR response
  3519. */
  3520. static int nfs4_xdr_dec_readdir(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_readdir_res *res)
  3521. {
  3522. struct xdr_stream xdr;
  3523. struct compound_hdr hdr;
  3524. int status;
  3525. xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
  3526. status = decode_compound_hdr(&xdr, &hdr);
  3527. if (status)
  3528. goto out;
  3529. status = decode_putfh(&xdr);
  3530. if (status)
  3531. goto out;
  3532. status = decode_readdir(&xdr, rqstp, res);
  3533. out:
  3534. return status;
  3535. }
  3536. /*
  3537. * Decode Read response
  3538. */
  3539. static int nfs4_xdr_dec_read(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_readres *res)
  3540. {
  3541. struct xdr_stream xdr;
  3542. struct compound_hdr hdr;
  3543. int status;
  3544. xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
  3545. status = decode_compound_hdr(&xdr, &hdr);
  3546. if (status)
  3547. goto out;
  3548. status = decode_putfh(&xdr);
  3549. if (status)
  3550. goto out;
  3551. status = decode_read(&xdr, rqstp, res);
  3552. if (!status)
  3553. status = res->count;
  3554. out:
  3555. return status;
  3556. }
  3557. /*
  3558. * Decode WRITE response
  3559. */
  3560. static int nfs4_xdr_dec_write(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_writeres *res)
  3561. {
  3562. struct xdr_stream xdr;
  3563. struct compound_hdr hdr;
  3564. int status;
  3565. xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
  3566. status = decode_compound_hdr(&xdr, &hdr);
  3567. if (status)
  3568. goto out;
  3569. status = decode_putfh(&xdr);
  3570. if (status)
  3571. goto out;
  3572. status = decode_write(&xdr, res);
  3573. if (status)
  3574. goto out;
  3575. decode_getfattr(&xdr, res->fattr, res->server);
  3576. if (!status)
  3577. status = res->count;
  3578. out:
  3579. return status;
  3580. }
  3581. /*
  3582. * Decode COMMIT response
  3583. */
  3584. static int nfs4_xdr_dec_commit(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_writeres *res)
  3585. {
  3586. struct xdr_stream xdr;
  3587. struct compound_hdr hdr;
  3588. int status;
  3589. xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
  3590. status = decode_compound_hdr(&xdr, &hdr);
  3591. if (status)
  3592. goto out;
  3593. status = decode_putfh(&xdr);
  3594. if (status)
  3595. goto out;
  3596. status = decode_commit(&xdr, res);
  3597. if (status)
  3598. goto out;
  3599. decode_getfattr(&xdr, res->fattr, res->server);
  3600. out:
  3601. return status;
  3602. }
  3603. /*
  3604. * FSINFO request
  3605. */
  3606. static int nfs4_xdr_dec_fsinfo(struct rpc_rqst *req, uint32_t *p, struct nfs_fsinfo *fsinfo)
  3607. {
  3608. struct xdr_stream xdr;
  3609. struct compound_hdr hdr;
  3610. int status;
  3611. xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
  3612. status = decode_compound_hdr(&xdr, &hdr);
  3613. if (!status)
  3614. status = decode_putfh(&xdr);
  3615. if (!status)
  3616. status = decode_fsinfo(&xdr, fsinfo);
  3617. if (!status)
  3618. status = -nfs_stat_to_errno(hdr.status);
  3619. return status;
  3620. }
  3621. /*
  3622. * PATHCONF request
  3623. */
  3624. static int nfs4_xdr_dec_pathconf(struct rpc_rqst *req, uint32_t *p, struct nfs_pathconf *pathconf)
  3625. {
  3626. struct xdr_stream xdr;
  3627. struct compound_hdr hdr;
  3628. int status;
  3629. xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
  3630. status = decode_compound_hdr(&xdr, &hdr);
  3631. if (!status)
  3632. status = decode_putfh(&xdr);
  3633. if (!status)
  3634. status = decode_pathconf(&xdr, pathconf);
  3635. return status;
  3636. }
  3637. /*
  3638. * STATFS request
  3639. */
  3640. static int nfs4_xdr_dec_statfs(struct rpc_rqst *req, uint32_t *p, struct nfs_fsstat *fsstat)
  3641. {
  3642. struct xdr_stream xdr;
  3643. struct compound_hdr hdr;
  3644. int status;
  3645. xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
  3646. status = decode_compound_hdr(&xdr, &hdr);
  3647. if (!status)
  3648. status = decode_putfh(&xdr);
  3649. if (!status)
  3650. status = decode_statfs(&xdr, fsstat);
  3651. return status;
  3652. }
  3653. /*
  3654. * GETATTR_BITMAP request
  3655. */
  3656. static int nfs4_xdr_dec_server_caps(struct rpc_rqst *req, uint32_t *p, struct nfs4_server_caps_res *res)
  3657. {
  3658. struct xdr_stream xdr;
  3659. struct compound_hdr hdr;
  3660. int status;
  3661. xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
  3662. if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
  3663. goto out;
  3664. if ((status = decode_putfh(&xdr)) != 0)
  3665. goto out;
  3666. status = decode_server_caps(&xdr, res);
  3667. out:
  3668. return status;
  3669. }
  3670. /*
  3671. * Decode RENEW response
  3672. */
  3673. static int nfs4_xdr_dec_renew(struct rpc_rqst *rqstp, uint32_t *p, void *dummy)
  3674. {
  3675. struct xdr_stream xdr;
  3676. struct compound_hdr hdr;
  3677. int status;
  3678. xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
  3679. status = decode_compound_hdr(&xdr, &hdr);
  3680. if (!status)
  3681. status = decode_renew(&xdr);
  3682. return status;
  3683. }
  3684. /*
  3685. * a SETCLIENTID request
  3686. */
  3687. static int nfs4_xdr_dec_setclientid(struct rpc_rqst *req, uint32_t *p,
  3688. struct nfs4_client *clp)
  3689. {
  3690. struct xdr_stream xdr;
  3691. struct compound_hdr hdr;
  3692. int status;
  3693. xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
  3694. status = decode_compound_hdr(&xdr, &hdr);
  3695. if (!status)
  3696. status = decode_setclientid(&xdr, clp);
  3697. if (!status)
  3698. status = -nfs_stat_to_errno(hdr.status);
  3699. return status;
  3700. }
  3701. /*
  3702. * a SETCLIENTID_CONFIRM request
  3703. */
  3704. static int nfs4_xdr_dec_setclientid_confirm(struct rpc_rqst *req, uint32_t *p, struct nfs_fsinfo *fsinfo)
  3705. {
  3706. struct xdr_stream xdr;
  3707. struct compound_hdr hdr;
  3708. int status;
  3709. xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
  3710. status = decode_compound_hdr(&xdr, &hdr);
  3711. if (!status)
  3712. status = decode_setclientid_confirm(&xdr);
  3713. if (!status)
  3714. status = decode_putrootfh(&xdr);
  3715. if (!status)
  3716. status = decode_fsinfo(&xdr, fsinfo);
  3717. if (!status)
  3718. status = -nfs_stat_to_errno(hdr.status);
  3719. return status;
  3720. }
  3721. /*
  3722. * DELEGRETURN request
  3723. */
  3724. static int nfs4_xdr_dec_delegreturn(struct rpc_rqst *rqstp, uint32_t *p, void *dummy)
  3725. {
  3726. struct xdr_stream xdr;
  3727. struct compound_hdr hdr;
  3728. int status;
  3729. xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
  3730. status = decode_compound_hdr(&xdr, &hdr);
  3731. if (status == 0) {
  3732. status = decode_putfh(&xdr);
  3733. if (status == 0)
  3734. status = decode_delegreturn(&xdr);
  3735. }
  3736. return status;
  3737. }
  3738. uint32_t *nfs4_decode_dirent(uint32_t *p, struct nfs_entry *entry, int plus)
  3739. {
  3740. uint32_t bitmap[2] = {0};
  3741. uint32_t len;
  3742. if (!*p++) {
  3743. if (!*p)
  3744. return ERR_PTR(-EAGAIN);
  3745. entry->eof = 1;
  3746. return ERR_PTR(-EBADCOOKIE);
  3747. }
  3748. entry->prev_cookie = entry->cookie;
  3749. p = xdr_decode_hyper(p, &entry->cookie);
  3750. entry->len = ntohl(*p++);
  3751. entry->name = (const char *) p;
  3752. p += XDR_QUADLEN(entry->len);
  3753. /*
  3754. * In case the server doesn't return an inode number,
  3755. * we fake one here. (We don't use inode number 0,
  3756. * since glibc seems to choke on it...)
  3757. */
  3758. entry->ino = 1;
  3759. len = ntohl(*p++); /* bitmap length */
  3760. if (len-- > 0) {
  3761. bitmap[0] = ntohl(*p++);
  3762. if (len-- > 0) {
  3763. bitmap[1] = ntohl(*p++);
  3764. p += len;
  3765. }
  3766. }
  3767. len = XDR_QUADLEN(ntohl(*p++)); /* attribute buffer length */
  3768. if (len > 0) {
  3769. if (bitmap[0] & FATTR4_WORD0_RDATTR_ERROR) {
  3770. bitmap[0] &= ~FATTR4_WORD0_RDATTR_ERROR;
  3771. /* Ignore the return value of rdattr_error for now */
  3772. p++;
  3773. len--;
  3774. }
  3775. if (bitmap[0] == 0 && bitmap[1] == FATTR4_WORD1_MOUNTED_ON_FILEID)
  3776. xdr_decode_hyper(p, &entry->ino);
  3777. else if (bitmap[0] == FATTR4_WORD0_FILEID)
  3778. xdr_decode_hyper(p, &entry->ino);
  3779. p += len;
  3780. }
  3781. entry->eof = !p[0] && p[1];
  3782. return p;
  3783. }
  3784. /*
  3785. * We need to translate between nfs status return values and
  3786. * the local errno values which may not be the same.
  3787. */
  3788. static struct {
  3789. int stat;
  3790. int errno;
  3791. } nfs_errtbl[] = {
  3792. { NFS4_OK, 0 },
  3793. { NFS4ERR_PERM, EPERM },
  3794. { NFS4ERR_NOENT, ENOENT },
  3795. { NFS4ERR_IO, errno_NFSERR_IO },
  3796. { NFS4ERR_NXIO, ENXIO },
  3797. { NFS4ERR_ACCESS, EACCES },
  3798. { NFS4ERR_EXIST, EEXIST },
  3799. { NFS4ERR_XDEV, EXDEV },
  3800. { NFS4ERR_NOTDIR, ENOTDIR },
  3801. { NFS4ERR_ISDIR, EISDIR },
  3802. { NFS4ERR_INVAL, EINVAL },
  3803. { NFS4ERR_FBIG, EFBIG },
  3804. { NFS4ERR_NOSPC, ENOSPC },
  3805. { NFS4ERR_ROFS, EROFS },
  3806. { NFS4ERR_MLINK, EMLINK },
  3807. { NFS4ERR_NAMETOOLONG, ENAMETOOLONG },
  3808. { NFS4ERR_NOTEMPTY, ENOTEMPTY },
  3809. { NFS4ERR_DQUOT, EDQUOT },
  3810. { NFS4ERR_STALE, ESTALE },
  3811. { NFS4ERR_BADHANDLE, EBADHANDLE },
  3812. { NFS4ERR_BADOWNER, EINVAL },
  3813. { NFS4ERR_BADNAME, EINVAL },
  3814. { NFS4ERR_BAD_COOKIE, EBADCOOKIE },
  3815. { NFS4ERR_NOTSUPP, ENOTSUPP },
  3816. { NFS4ERR_TOOSMALL, ETOOSMALL },
  3817. { NFS4ERR_SERVERFAULT, ESERVERFAULT },
  3818. { NFS4ERR_BADTYPE, EBADTYPE },
  3819. { NFS4ERR_LOCKED, EAGAIN },
  3820. { NFS4ERR_RESOURCE, EREMOTEIO },
  3821. { NFS4ERR_SYMLINK, ELOOP },
  3822. { NFS4ERR_OP_ILLEGAL, EOPNOTSUPP },
  3823. { NFS4ERR_DEADLOCK, EDEADLK },
  3824. { NFS4ERR_WRONGSEC, EPERM }, /* FIXME: this needs
  3825. * to be handled by a
  3826. * middle-layer.
  3827. */
  3828. { -1, EIO }
  3829. };
  3830. /*
  3831. * Convert an NFS error code to a local one.
  3832. * This one is used jointly by NFSv2 and NFSv3.
  3833. */
  3834. static int
  3835. nfs_stat_to_errno(int stat)
  3836. {
  3837. int i;
  3838. for (i = 0; nfs_errtbl[i].stat != -1; i++) {
  3839. if (nfs_errtbl[i].stat == stat)
  3840. return nfs_errtbl[i].errno;
  3841. }
  3842. if (stat <= 10000 || stat > 10100) {
  3843. /* The server is looney tunes. */
  3844. return ESERVERFAULT;
  3845. }
  3846. /* If we cannot translate the error, the recovery routines should
  3847. * handle it.
  3848. * Note: remaining NFSv4 error codes have values > 10000, so should
  3849. * not conflict with native Linux error codes.
  3850. */
  3851. return stat;
  3852. }
  3853. #ifndef MAX
  3854. # define MAX(a, b) (((a) > (b))? (a) : (b))
  3855. #endif
  3856. #define PROC(proc, argtype, restype) \
  3857. [NFSPROC4_CLNT_##proc] = { \
  3858. .p_proc = NFSPROC4_COMPOUND, \
  3859. .p_encode = (kxdrproc_t) nfs4_xdr_##argtype, \
  3860. .p_decode = (kxdrproc_t) nfs4_xdr_##restype, \
  3861. .p_bufsiz = MAX(NFS4_##argtype##_sz,NFS4_##restype##_sz) << 2, \
  3862. }
  3863. struct rpc_procinfo nfs4_procedures[] = {
  3864. PROC(READ, enc_read, dec_read),
  3865. PROC(WRITE, enc_write, dec_write),
  3866. PROC(COMMIT, enc_commit, dec_commit),
  3867. PROC(OPEN, enc_open, dec_open),
  3868. PROC(OPEN_CONFIRM, enc_open_confirm, dec_open_confirm),
  3869. PROC(OPEN_NOATTR, enc_open_noattr, dec_open_noattr),
  3870. PROC(OPEN_DOWNGRADE, enc_open_downgrade, dec_open_downgrade),
  3871. PROC(CLOSE, enc_close, dec_close),
  3872. PROC(SETATTR, enc_setattr, dec_setattr),
  3873. PROC(FSINFO, enc_fsinfo, dec_fsinfo),
  3874. PROC(RENEW, enc_renew, dec_renew),
  3875. PROC(SETCLIENTID, enc_setclientid, dec_setclientid),
  3876. PROC(SETCLIENTID_CONFIRM, enc_setclientid_confirm, dec_setclientid_confirm),
  3877. PROC(LOCK, enc_lock, dec_lock),
  3878. PROC(LOCKT, enc_lockt, dec_lockt),
  3879. PROC(LOCKU, enc_locku, dec_locku),
  3880. PROC(ACCESS, enc_access, dec_access),
  3881. PROC(GETATTR, enc_getattr, dec_getattr),
  3882. PROC(LOOKUP, enc_lookup, dec_lookup),
  3883. PROC(LOOKUP_ROOT, enc_lookup_root, dec_lookup_root),
  3884. PROC(REMOVE, enc_remove, dec_remove),
  3885. PROC(RENAME, enc_rename, dec_rename),
  3886. PROC(LINK, enc_link, dec_link),
  3887. PROC(SYMLINK, enc_symlink, dec_symlink),
  3888. PROC(CREATE, enc_create, dec_create),
  3889. PROC(PATHCONF, enc_pathconf, dec_pathconf),
  3890. PROC(STATFS, enc_statfs, dec_statfs),
  3891. PROC(READLINK, enc_readlink, dec_readlink),
  3892. PROC(READDIR, enc_readdir, dec_readdir),
  3893. PROC(SERVER_CAPS, enc_server_caps, dec_server_caps),
  3894. PROC(DELEGRETURN, enc_delegreturn, dec_delegreturn),
  3895. PROC(GETACL, enc_getacl, dec_getacl),
  3896. PROC(SETACL, enc_setacl, dec_setacl),
  3897. };
  3898. struct rpc_version nfs_version4 = {
  3899. .number = 4,
  3900. .nrprocs = sizeof(nfs4_procedures)/sizeof(nfs4_procedures[0]),
  3901. .procs = nfs4_procedures
  3902. };
  3903. /*
  3904. * Local variables:
  3905. * c-basic-offset: 8
  3906. * End:
  3907. */