nfs4xdr.c 110 KB

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