nfs4state.c 133 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103
  1. /*
  2. * Copyright (c) 2001 The Regents of the University of Michigan.
  3. * All rights reserved.
  4. *
  5. * Kendrick Smith <kmsmith@umich.edu>
  6. * Andy Adamson <kandros@umich.edu>
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the University nor the names of its
  18. * contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  22. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  23. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  24. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  28. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  29. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  30. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. */
  34. #include <linux/file.h>
  35. #include <linux/fs.h>
  36. #include <linux/slab.h>
  37. #include <linux/namei.h>
  38. #include <linux/swap.h>
  39. #include <linux/pagemap.h>
  40. #include <linux/ratelimit.h>
  41. #include <linux/sunrpc/svcauth_gss.h>
  42. #include <linux/sunrpc/clnt.h>
  43. #include "xdr4.h"
  44. #include "vfs.h"
  45. #include "current_stateid.h"
  46. #include "netns.h"
  47. #define NFSDDBG_FACILITY NFSDDBG_PROC
  48. #define all_ones {{~0,~0},~0}
  49. static const stateid_t one_stateid = {
  50. .si_generation = ~0,
  51. .si_opaque = all_ones,
  52. };
  53. static const stateid_t zero_stateid = {
  54. /* all fields zero */
  55. };
  56. static const stateid_t currentstateid = {
  57. .si_generation = 1,
  58. };
  59. static u64 current_sessionid = 1;
  60. #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
  61. #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
  62. #define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
  63. /* forward declarations */
  64. static int check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner);
  65. /* Locking: */
  66. /* Currently used for almost all code touching nfsv4 state: */
  67. static DEFINE_MUTEX(client_mutex);
  68. /*
  69. * Currently used for the del_recall_lru and file hash table. In an
  70. * effort to decrease the scope of the client_mutex, this spinlock may
  71. * eventually cover more:
  72. */
  73. static DEFINE_SPINLOCK(recall_lock);
  74. static struct kmem_cache *openowner_slab = NULL;
  75. static struct kmem_cache *lockowner_slab = NULL;
  76. static struct kmem_cache *file_slab = NULL;
  77. static struct kmem_cache *stateid_slab = NULL;
  78. static struct kmem_cache *deleg_slab = NULL;
  79. void
  80. nfs4_lock_state(void)
  81. {
  82. mutex_lock(&client_mutex);
  83. }
  84. static void free_session(struct kref *);
  85. /* Must be called under the client_lock */
  86. static void nfsd4_put_session_locked(struct nfsd4_session *ses)
  87. {
  88. kref_put(&ses->se_ref, free_session);
  89. }
  90. static void nfsd4_get_session(struct nfsd4_session *ses)
  91. {
  92. kref_get(&ses->se_ref);
  93. }
  94. void
  95. nfs4_unlock_state(void)
  96. {
  97. mutex_unlock(&client_mutex);
  98. }
  99. static inline u32
  100. opaque_hashval(const void *ptr, int nbytes)
  101. {
  102. unsigned char *cptr = (unsigned char *) ptr;
  103. u32 x = 0;
  104. while (nbytes--) {
  105. x *= 37;
  106. x += *cptr++;
  107. }
  108. return x;
  109. }
  110. static struct list_head del_recall_lru;
  111. static void nfsd4_free_file(struct nfs4_file *f)
  112. {
  113. kmem_cache_free(file_slab, f);
  114. }
  115. static inline void
  116. put_nfs4_file(struct nfs4_file *fi)
  117. {
  118. if (atomic_dec_and_lock(&fi->fi_ref, &recall_lock)) {
  119. list_del(&fi->fi_hash);
  120. spin_unlock(&recall_lock);
  121. iput(fi->fi_inode);
  122. nfsd4_free_file(fi);
  123. }
  124. }
  125. static inline void
  126. get_nfs4_file(struct nfs4_file *fi)
  127. {
  128. atomic_inc(&fi->fi_ref);
  129. }
  130. static int num_delegations;
  131. unsigned int max_delegations;
  132. /*
  133. * Open owner state (share locks)
  134. */
  135. /* hash tables for lock and open owners */
  136. #define OWNER_HASH_BITS 8
  137. #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
  138. #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
  139. static unsigned int ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername)
  140. {
  141. unsigned int ret;
  142. ret = opaque_hashval(ownername->data, ownername->len);
  143. ret += clientid;
  144. return ret & OWNER_HASH_MASK;
  145. }
  146. /* hash table for nfs4_file */
  147. #define FILE_HASH_BITS 8
  148. #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
  149. static unsigned int file_hashval(struct inode *ino)
  150. {
  151. /* XXX: why are we hashing on inode pointer, anyway? */
  152. return hash_ptr(ino, FILE_HASH_BITS);
  153. }
  154. static struct list_head file_hashtbl[FILE_HASH_SIZE];
  155. static void __nfs4_file_get_access(struct nfs4_file *fp, int oflag)
  156. {
  157. WARN_ON_ONCE(!(fp->fi_fds[oflag] || fp->fi_fds[O_RDWR]));
  158. atomic_inc(&fp->fi_access[oflag]);
  159. }
  160. static void nfs4_file_get_access(struct nfs4_file *fp, int oflag)
  161. {
  162. if (oflag == O_RDWR) {
  163. __nfs4_file_get_access(fp, O_RDONLY);
  164. __nfs4_file_get_access(fp, O_WRONLY);
  165. } else
  166. __nfs4_file_get_access(fp, oflag);
  167. }
  168. static void nfs4_file_put_fd(struct nfs4_file *fp, int oflag)
  169. {
  170. if (fp->fi_fds[oflag]) {
  171. fput(fp->fi_fds[oflag]);
  172. fp->fi_fds[oflag] = NULL;
  173. }
  174. }
  175. static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
  176. {
  177. if (atomic_dec_and_test(&fp->fi_access[oflag])) {
  178. nfs4_file_put_fd(fp, oflag);
  179. /*
  180. * It's also safe to get rid of the RDWR open *if*
  181. * we no longer have need of the other kind of access
  182. * or if we already have the other kind of open:
  183. */
  184. if (fp->fi_fds[1-oflag]
  185. || atomic_read(&fp->fi_access[1 - oflag]) == 0)
  186. nfs4_file_put_fd(fp, O_RDWR);
  187. }
  188. }
  189. static void nfs4_file_put_access(struct nfs4_file *fp, int oflag)
  190. {
  191. if (oflag == O_RDWR) {
  192. __nfs4_file_put_access(fp, O_RDONLY);
  193. __nfs4_file_put_access(fp, O_WRONLY);
  194. } else
  195. __nfs4_file_put_access(fp, oflag);
  196. }
  197. static inline int get_new_stid(struct nfs4_stid *stid)
  198. {
  199. static int min_stateid = 0;
  200. struct idr *stateids = &stid->sc_client->cl_stateids;
  201. int new_stid;
  202. int error;
  203. error = idr_get_new_above(stateids, stid, min_stateid, &new_stid);
  204. /*
  205. * Note: the necessary preallocation was done in
  206. * nfs4_alloc_stateid(). The idr code caps the number of
  207. * preallocations that can exist at a time, but the state lock
  208. * prevents anyone from using ours before we get here:
  209. */
  210. WARN_ON_ONCE(error);
  211. /*
  212. * It shouldn't be a problem to reuse an opaque stateid value.
  213. * I don't think it is for 4.1. But with 4.0 I worry that, for
  214. * example, a stray write retransmission could be accepted by
  215. * the server when it should have been rejected. Therefore,
  216. * adopt a trick from the sctp code to attempt to maximize the
  217. * amount of time until an id is reused, by ensuring they always
  218. * "increase" (mod INT_MAX):
  219. */
  220. min_stateid = new_stid+1;
  221. if (min_stateid == INT_MAX)
  222. min_stateid = 0;
  223. return new_stid;
  224. }
  225. static void init_stid(struct nfs4_stid *stid, struct nfs4_client *cl, unsigned char type)
  226. {
  227. stateid_t *s = &stid->sc_stateid;
  228. int new_id;
  229. stid->sc_type = type;
  230. stid->sc_client = cl;
  231. s->si_opaque.so_clid = cl->cl_clientid;
  232. new_id = get_new_stid(stid);
  233. s->si_opaque.so_id = (u32)new_id;
  234. /* Will be incremented before return to client: */
  235. s->si_generation = 0;
  236. }
  237. static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab)
  238. {
  239. struct idr *stateids = &cl->cl_stateids;
  240. if (!idr_pre_get(stateids, GFP_KERNEL))
  241. return NULL;
  242. /*
  243. * Note: if we fail here (or any time between now and the time
  244. * we actually get the new idr), we won't need to undo the idr
  245. * preallocation, since the idr code caps the number of
  246. * preallocated entries.
  247. */
  248. return kmem_cache_alloc(slab, GFP_KERNEL);
  249. }
  250. static struct nfs4_ol_stateid * nfs4_alloc_stateid(struct nfs4_client *clp)
  251. {
  252. return openlockstateid(nfs4_alloc_stid(clp, stateid_slab));
  253. }
  254. static struct nfs4_delegation *
  255. alloc_init_deleg(struct nfs4_client *clp, struct nfs4_ol_stateid *stp, struct svc_fh *current_fh, u32 type)
  256. {
  257. struct nfs4_delegation *dp;
  258. struct nfs4_file *fp = stp->st_file;
  259. dprintk("NFSD alloc_init_deleg\n");
  260. /*
  261. * Major work on the lease subsystem (for example, to support
  262. * calbacks on stat) will be required before we can support
  263. * write delegations properly.
  264. */
  265. if (type != NFS4_OPEN_DELEGATE_READ)
  266. return NULL;
  267. if (fp->fi_had_conflict)
  268. return NULL;
  269. if (num_delegations > max_delegations)
  270. return NULL;
  271. dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
  272. if (dp == NULL)
  273. return dp;
  274. init_stid(&dp->dl_stid, clp, NFS4_DELEG_STID);
  275. /*
  276. * delegation seqid's are never incremented. The 4.1 special
  277. * meaning of seqid 0 isn't meaningful, really, but let's avoid
  278. * 0 anyway just for consistency and use 1:
  279. */
  280. dp->dl_stid.sc_stateid.si_generation = 1;
  281. num_delegations++;
  282. INIT_LIST_HEAD(&dp->dl_perfile);
  283. INIT_LIST_HEAD(&dp->dl_perclnt);
  284. INIT_LIST_HEAD(&dp->dl_recall_lru);
  285. get_nfs4_file(fp);
  286. dp->dl_file = fp;
  287. dp->dl_type = type;
  288. fh_copy_shallow(&dp->dl_fh, &current_fh->fh_handle);
  289. dp->dl_time = 0;
  290. atomic_set(&dp->dl_count, 1);
  291. nfsd4_init_callback(&dp->dl_recall);
  292. return dp;
  293. }
  294. void
  295. nfs4_put_delegation(struct nfs4_delegation *dp)
  296. {
  297. if (atomic_dec_and_test(&dp->dl_count)) {
  298. dprintk("NFSD: freeing dp %p\n",dp);
  299. put_nfs4_file(dp->dl_file);
  300. kmem_cache_free(deleg_slab, dp);
  301. num_delegations--;
  302. }
  303. }
  304. static void nfs4_put_deleg_lease(struct nfs4_file *fp)
  305. {
  306. if (atomic_dec_and_test(&fp->fi_delegees)) {
  307. vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
  308. fp->fi_lease = NULL;
  309. fput(fp->fi_deleg_file);
  310. fp->fi_deleg_file = NULL;
  311. }
  312. }
  313. static void unhash_stid(struct nfs4_stid *s)
  314. {
  315. struct idr *stateids = &s->sc_client->cl_stateids;
  316. idr_remove(stateids, s->sc_stateid.si_opaque.so_id);
  317. }
  318. /* Called under the state lock. */
  319. static void
  320. unhash_delegation(struct nfs4_delegation *dp)
  321. {
  322. unhash_stid(&dp->dl_stid);
  323. list_del_init(&dp->dl_perclnt);
  324. spin_lock(&recall_lock);
  325. list_del_init(&dp->dl_perfile);
  326. list_del_init(&dp->dl_recall_lru);
  327. spin_unlock(&recall_lock);
  328. nfs4_put_deleg_lease(dp->dl_file);
  329. nfs4_put_delegation(dp);
  330. }
  331. /*
  332. * SETCLIENTID state
  333. */
  334. static unsigned int clientid_hashval(u32 id)
  335. {
  336. return id & CLIENT_HASH_MASK;
  337. }
  338. static unsigned int clientstr_hashval(const char *name)
  339. {
  340. return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
  341. }
  342. /*
  343. * We store the NONE, READ, WRITE, and BOTH bits separately in the
  344. * st_{access,deny}_bmap field of the stateid, in order to track not
  345. * only what share bits are currently in force, but also what
  346. * combinations of share bits previous opens have used. This allows us
  347. * to enforce the recommendation of rfc 3530 14.2.19 that the server
  348. * return an error if the client attempt to downgrade to a combination
  349. * of share bits not explicable by closing some of its previous opens.
  350. *
  351. * XXX: This enforcement is actually incomplete, since we don't keep
  352. * track of access/deny bit combinations; so, e.g., we allow:
  353. *
  354. * OPEN allow read, deny write
  355. * OPEN allow both, deny none
  356. * DOWNGRADE allow read, deny none
  357. *
  358. * which we should reject.
  359. */
  360. static unsigned int
  361. bmap_to_share_mode(unsigned long bmap) {
  362. int i;
  363. unsigned int access = 0;
  364. for (i = 1; i < 4; i++) {
  365. if (test_bit(i, &bmap))
  366. access |= i;
  367. }
  368. return access;
  369. }
  370. static bool
  371. test_share(struct nfs4_ol_stateid *stp, struct nfsd4_open *open) {
  372. unsigned int access, deny;
  373. access = bmap_to_share_mode(stp->st_access_bmap);
  374. deny = bmap_to_share_mode(stp->st_deny_bmap);
  375. if ((access & open->op_share_deny) || (deny & open->op_share_access))
  376. return false;
  377. return true;
  378. }
  379. /* set share access for a given stateid */
  380. static inline void
  381. set_access(u32 access, struct nfs4_ol_stateid *stp)
  382. {
  383. __set_bit(access, &stp->st_access_bmap);
  384. }
  385. /* clear share access for a given stateid */
  386. static inline void
  387. clear_access(u32 access, struct nfs4_ol_stateid *stp)
  388. {
  389. __clear_bit(access, &stp->st_access_bmap);
  390. }
  391. /* test whether a given stateid has access */
  392. static inline bool
  393. test_access(u32 access, struct nfs4_ol_stateid *stp)
  394. {
  395. return test_bit(access, &stp->st_access_bmap);
  396. }
  397. /* set share deny for a given stateid */
  398. static inline void
  399. set_deny(u32 access, struct nfs4_ol_stateid *stp)
  400. {
  401. __set_bit(access, &stp->st_deny_bmap);
  402. }
  403. /* clear share deny for a given stateid */
  404. static inline void
  405. clear_deny(u32 access, struct nfs4_ol_stateid *stp)
  406. {
  407. __clear_bit(access, &stp->st_deny_bmap);
  408. }
  409. /* test whether a given stateid is denying specific access */
  410. static inline bool
  411. test_deny(u32 access, struct nfs4_ol_stateid *stp)
  412. {
  413. return test_bit(access, &stp->st_deny_bmap);
  414. }
  415. static int nfs4_access_to_omode(u32 access)
  416. {
  417. switch (access & NFS4_SHARE_ACCESS_BOTH) {
  418. case NFS4_SHARE_ACCESS_READ:
  419. return O_RDONLY;
  420. case NFS4_SHARE_ACCESS_WRITE:
  421. return O_WRONLY;
  422. case NFS4_SHARE_ACCESS_BOTH:
  423. return O_RDWR;
  424. }
  425. WARN_ON_ONCE(1);
  426. return O_RDONLY;
  427. }
  428. /* release all access and file references for a given stateid */
  429. static void
  430. release_all_access(struct nfs4_ol_stateid *stp)
  431. {
  432. int i;
  433. for (i = 1; i < 4; i++) {
  434. if (test_access(i, stp))
  435. nfs4_file_put_access(stp->st_file,
  436. nfs4_access_to_omode(i));
  437. clear_access(i, stp);
  438. }
  439. }
  440. static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
  441. {
  442. list_del(&stp->st_perfile);
  443. list_del(&stp->st_perstateowner);
  444. }
  445. static void close_generic_stateid(struct nfs4_ol_stateid *stp)
  446. {
  447. release_all_access(stp);
  448. put_nfs4_file(stp->st_file);
  449. stp->st_file = NULL;
  450. }
  451. static void free_generic_stateid(struct nfs4_ol_stateid *stp)
  452. {
  453. kmem_cache_free(stateid_slab, stp);
  454. }
  455. static void release_lock_stateid(struct nfs4_ol_stateid *stp)
  456. {
  457. struct file *file;
  458. unhash_generic_stateid(stp);
  459. unhash_stid(&stp->st_stid);
  460. file = find_any_file(stp->st_file);
  461. if (file)
  462. locks_remove_posix(file, (fl_owner_t)lockowner(stp->st_stateowner));
  463. close_generic_stateid(stp);
  464. free_generic_stateid(stp);
  465. }
  466. static void unhash_lockowner(struct nfs4_lockowner *lo)
  467. {
  468. struct nfs4_ol_stateid *stp;
  469. list_del(&lo->lo_owner.so_strhash);
  470. list_del(&lo->lo_perstateid);
  471. list_del(&lo->lo_owner_ino_hash);
  472. while (!list_empty(&lo->lo_owner.so_stateids)) {
  473. stp = list_first_entry(&lo->lo_owner.so_stateids,
  474. struct nfs4_ol_stateid, st_perstateowner);
  475. release_lock_stateid(stp);
  476. }
  477. }
  478. static void release_lockowner(struct nfs4_lockowner *lo)
  479. {
  480. unhash_lockowner(lo);
  481. nfs4_free_lockowner(lo);
  482. }
  483. static void
  484. release_stateid_lockowners(struct nfs4_ol_stateid *open_stp)
  485. {
  486. struct nfs4_lockowner *lo;
  487. while (!list_empty(&open_stp->st_lockowners)) {
  488. lo = list_entry(open_stp->st_lockowners.next,
  489. struct nfs4_lockowner, lo_perstateid);
  490. release_lockowner(lo);
  491. }
  492. }
  493. static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
  494. {
  495. unhash_generic_stateid(stp);
  496. release_stateid_lockowners(stp);
  497. close_generic_stateid(stp);
  498. }
  499. static void release_open_stateid(struct nfs4_ol_stateid *stp)
  500. {
  501. unhash_open_stateid(stp);
  502. unhash_stid(&stp->st_stid);
  503. free_generic_stateid(stp);
  504. }
  505. static void unhash_openowner(struct nfs4_openowner *oo)
  506. {
  507. struct nfs4_ol_stateid *stp;
  508. list_del(&oo->oo_owner.so_strhash);
  509. list_del(&oo->oo_perclient);
  510. while (!list_empty(&oo->oo_owner.so_stateids)) {
  511. stp = list_first_entry(&oo->oo_owner.so_stateids,
  512. struct nfs4_ol_stateid, st_perstateowner);
  513. release_open_stateid(stp);
  514. }
  515. }
  516. static void release_last_closed_stateid(struct nfs4_openowner *oo)
  517. {
  518. struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
  519. if (s) {
  520. unhash_stid(&s->st_stid);
  521. free_generic_stateid(s);
  522. oo->oo_last_closed_stid = NULL;
  523. }
  524. }
  525. static void release_openowner(struct nfs4_openowner *oo)
  526. {
  527. unhash_openowner(oo);
  528. list_del(&oo->oo_close_lru);
  529. release_last_closed_stateid(oo);
  530. nfs4_free_openowner(oo);
  531. }
  532. static inline int
  533. hash_sessionid(struct nfs4_sessionid *sessionid)
  534. {
  535. struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
  536. return sid->sequence % SESSION_HASH_SIZE;
  537. }
  538. #ifdef NFSD_DEBUG
  539. static inline void
  540. dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
  541. {
  542. u32 *ptr = (u32 *)(&sessionid->data[0]);
  543. dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
  544. }
  545. #else
  546. static inline void
  547. dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
  548. {
  549. }
  550. #endif
  551. static void
  552. gen_sessionid(struct nfsd4_session *ses)
  553. {
  554. struct nfs4_client *clp = ses->se_client;
  555. struct nfsd4_sessionid *sid;
  556. sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
  557. sid->clientid = clp->cl_clientid;
  558. sid->sequence = current_sessionid++;
  559. sid->reserved = 0;
  560. }
  561. /*
  562. * The protocol defines ca_maxresponssize_cached to include the size of
  563. * the rpc header, but all we need to cache is the data starting after
  564. * the end of the initial SEQUENCE operation--the rest we regenerate
  565. * each time. Therefore we can advertise a ca_maxresponssize_cached
  566. * value that is the number of bytes in our cache plus a few additional
  567. * bytes. In order to stay on the safe side, and not promise more than
  568. * we can cache, those additional bytes must be the minimum possible: 24
  569. * bytes of rpc header (xid through accept state, with AUTH_NULL
  570. * verifier), 12 for the compound header (with zero-length tag), and 44
  571. * for the SEQUENCE op response:
  572. */
  573. #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
  574. static void
  575. free_session_slots(struct nfsd4_session *ses)
  576. {
  577. int i;
  578. for (i = 0; i < ses->se_fchannel.maxreqs; i++)
  579. kfree(ses->se_slots[i]);
  580. }
  581. /*
  582. * We don't actually need to cache the rpc and session headers, so we
  583. * can allocate a little less for each slot:
  584. */
  585. static inline int slot_bytes(struct nfsd4_channel_attrs *ca)
  586. {
  587. return ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
  588. }
  589. static int nfsd4_sanitize_slot_size(u32 size)
  590. {
  591. size -= NFSD_MIN_HDR_SEQ_SZ; /* We don't cache the rpc header */
  592. size = min_t(u32, size, NFSD_SLOT_CACHE_SIZE);
  593. return size;
  594. }
  595. /*
  596. * XXX: If we run out of reserved DRC memory we could (up to a point)
  597. * re-negotiate active sessions and reduce their slot usage to make
  598. * room for new connections. For now we just fail the create session.
  599. */
  600. static int nfsd4_get_drc_mem(int slotsize, u32 num)
  601. {
  602. int avail;
  603. num = min_t(u32, num, NFSD_MAX_SLOTS_PER_SESSION);
  604. spin_lock(&nfsd_drc_lock);
  605. avail = min_t(int, NFSD_MAX_MEM_PER_SESSION,
  606. nfsd_drc_max_mem - nfsd_drc_mem_used);
  607. num = min_t(int, num, avail / slotsize);
  608. nfsd_drc_mem_used += num * slotsize;
  609. spin_unlock(&nfsd_drc_lock);
  610. return num;
  611. }
  612. static void nfsd4_put_drc_mem(int slotsize, int num)
  613. {
  614. spin_lock(&nfsd_drc_lock);
  615. nfsd_drc_mem_used -= slotsize * num;
  616. spin_unlock(&nfsd_drc_lock);
  617. }
  618. static struct nfsd4_session *__alloc_session(int slotsize, int numslots)
  619. {
  620. struct nfsd4_session *new;
  621. int mem, i;
  622. BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
  623. + sizeof(struct nfsd4_session) > PAGE_SIZE);
  624. mem = numslots * sizeof(struct nfsd4_slot *);
  625. new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
  626. if (!new)
  627. return NULL;
  628. /* allocate each struct nfsd4_slot and data cache in one piece */
  629. for (i = 0; i < numslots; i++) {
  630. mem = sizeof(struct nfsd4_slot) + slotsize;
  631. new->se_slots[i] = kzalloc(mem, GFP_KERNEL);
  632. if (!new->se_slots[i])
  633. goto out_free;
  634. }
  635. return new;
  636. out_free:
  637. while (i--)
  638. kfree(new->se_slots[i]);
  639. kfree(new);
  640. return NULL;
  641. }
  642. static void init_forechannel_attrs(struct nfsd4_channel_attrs *new,
  643. struct nfsd4_channel_attrs *req,
  644. int numslots, int slotsize,
  645. struct nfsd_net *nn)
  646. {
  647. u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
  648. new->maxreqs = numslots;
  649. new->maxresp_cached = min_t(u32, req->maxresp_cached,
  650. slotsize + NFSD_MIN_HDR_SEQ_SZ);
  651. new->maxreq_sz = min_t(u32, req->maxreq_sz, maxrpc);
  652. new->maxresp_sz = min_t(u32, req->maxresp_sz, maxrpc);
  653. new->maxops = min_t(u32, req->maxops, NFSD_MAX_OPS_PER_COMPOUND);
  654. }
  655. static void free_conn(struct nfsd4_conn *c)
  656. {
  657. svc_xprt_put(c->cn_xprt);
  658. kfree(c);
  659. }
  660. static void nfsd4_conn_lost(struct svc_xpt_user *u)
  661. {
  662. struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
  663. struct nfs4_client *clp = c->cn_session->se_client;
  664. spin_lock(&clp->cl_lock);
  665. if (!list_empty(&c->cn_persession)) {
  666. list_del(&c->cn_persession);
  667. free_conn(c);
  668. }
  669. spin_unlock(&clp->cl_lock);
  670. nfsd4_probe_callback(clp);
  671. }
  672. static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
  673. {
  674. struct nfsd4_conn *conn;
  675. conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
  676. if (!conn)
  677. return NULL;
  678. svc_xprt_get(rqstp->rq_xprt);
  679. conn->cn_xprt = rqstp->rq_xprt;
  680. conn->cn_flags = flags;
  681. INIT_LIST_HEAD(&conn->cn_xpt_user.list);
  682. return conn;
  683. }
  684. static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
  685. {
  686. conn->cn_session = ses;
  687. list_add(&conn->cn_persession, &ses->se_conns);
  688. }
  689. static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
  690. {
  691. struct nfs4_client *clp = ses->se_client;
  692. spin_lock(&clp->cl_lock);
  693. __nfsd4_hash_conn(conn, ses);
  694. spin_unlock(&clp->cl_lock);
  695. }
  696. static int nfsd4_register_conn(struct nfsd4_conn *conn)
  697. {
  698. conn->cn_xpt_user.callback = nfsd4_conn_lost;
  699. return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
  700. }
  701. static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
  702. {
  703. int ret;
  704. nfsd4_hash_conn(conn, ses);
  705. ret = nfsd4_register_conn(conn);
  706. if (ret)
  707. /* oops; xprt is already down: */
  708. nfsd4_conn_lost(&conn->cn_xpt_user);
  709. if (conn->cn_flags & NFS4_CDFC4_BACK) {
  710. /* callback channel may be back up */
  711. nfsd4_probe_callback(ses->se_client);
  712. }
  713. }
  714. static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
  715. {
  716. u32 dir = NFS4_CDFC4_FORE;
  717. if (cses->flags & SESSION4_BACK_CHAN)
  718. dir |= NFS4_CDFC4_BACK;
  719. return alloc_conn(rqstp, dir);
  720. }
  721. /* must be called under client_lock */
  722. static void nfsd4_del_conns(struct nfsd4_session *s)
  723. {
  724. struct nfs4_client *clp = s->se_client;
  725. struct nfsd4_conn *c;
  726. spin_lock(&clp->cl_lock);
  727. while (!list_empty(&s->se_conns)) {
  728. c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
  729. list_del_init(&c->cn_persession);
  730. spin_unlock(&clp->cl_lock);
  731. unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
  732. free_conn(c);
  733. spin_lock(&clp->cl_lock);
  734. }
  735. spin_unlock(&clp->cl_lock);
  736. }
  737. static void __free_session(struct nfsd4_session *ses)
  738. {
  739. nfsd4_put_drc_mem(slot_bytes(&ses->se_fchannel), ses->se_fchannel.maxreqs);
  740. free_session_slots(ses);
  741. kfree(ses);
  742. }
  743. static void free_session(struct kref *kref)
  744. {
  745. struct nfsd4_session *ses;
  746. struct nfsd_net *nn;
  747. ses = container_of(kref, struct nfsd4_session, se_ref);
  748. nn = net_generic(ses->se_client->net, nfsd_net_id);
  749. lockdep_assert_held(&nn->client_lock);
  750. nfsd4_del_conns(ses);
  751. __free_session(ses);
  752. }
  753. void nfsd4_put_session(struct nfsd4_session *ses)
  754. {
  755. struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id);
  756. spin_lock(&nn->client_lock);
  757. nfsd4_put_session_locked(ses);
  758. spin_unlock(&nn->client_lock);
  759. }
  760. static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fchan,
  761. struct nfsd_net *nn)
  762. {
  763. struct nfsd4_session *new;
  764. int numslots, slotsize;
  765. /*
  766. * Note decreasing slot size below client's request may
  767. * make it difficult for client to function correctly, whereas
  768. * decreasing the number of slots will (just?) affect
  769. * performance. When short on memory we therefore prefer to
  770. * decrease number of slots instead of their size.
  771. */
  772. slotsize = nfsd4_sanitize_slot_size(fchan->maxresp_cached);
  773. numslots = nfsd4_get_drc_mem(slotsize, fchan->maxreqs);
  774. if (numslots < 1)
  775. return NULL;
  776. new = __alloc_session(slotsize, numslots);
  777. if (!new) {
  778. nfsd4_put_drc_mem(slotsize, fchan->maxreqs);
  779. return NULL;
  780. }
  781. init_forechannel_attrs(&new->se_fchannel, fchan, numslots, slotsize, nn);
  782. return new;
  783. }
  784. static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
  785. {
  786. int idx;
  787. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  788. new->se_client = clp;
  789. gen_sessionid(new);
  790. INIT_LIST_HEAD(&new->se_conns);
  791. new->se_cb_seq_nr = 1;
  792. new->se_flags = cses->flags;
  793. new->se_cb_prog = cses->callback_prog;
  794. new->se_cb_sec = cses->cb_sec;
  795. kref_init(&new->se_ref);
  796. idx = hash_sessionid(&new->se_sessionid);
  797. spin_lock(&nn->client_lock);
  798. list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
  799. spin_lock(&clp->cl_lock);
  800. list_add(&new->se_perclnt, &clp->cl_sessions);
  801. spin_unlock(&clp->cl_lock);
  802. spin_unlock(&nn->client_lock);
  803. if (cses->flags & SESSION4_BACK_CHAN) {
  804. struct sockaddr *sa = svc_addr(rqstp);
  805. /*
  806. * This is a little silly; with sessions there's no real
  807. * use for the callback address. Use the peer address
  808. * as a reasonable default for now, but consider fixing
  809. * the rpc client not to require an address in the
  810. * future:
  811. */
  812. rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
  813. clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
  814. }
  815. }
  816. /* caller must hold client_lock */
  817. static struct nfsd4_session *
  818. find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
  819. {
  820. struct nfsd4_session *elem;
  821. int idx;
  822. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  823. dump_sessionid(__func__, sessionid);
  824. idx = hash_sessionid(sessionid);
  825. /* Search in the appropriate list */
  826. list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
  827. if (!memcmp(elem->se_sessionid.data, sessionid->data,
  828. NFS4_MAX_SESSIONID_LEN)) {
  829. return elem;
  830. }
  831. }
  832. dprintk("%s: session not found\n", __func__);
  833. return NULL;
  834. }
  835. /* caller must hold client_lock */
  836. static void
  837. unhash_session(struct nfsd4_session *ses)
  838. {
  839. list_del(&ses->se_hash);
  840. spin_lock(&ses->se_client->cl_lock);
  841. list_del(&ses->se_perclnt);
  842. spin_unlock(&ses->se_client->cl_lock);
  843. }
  844. /* must be called under the client_lock */
  845. static inline void
  846. renew_client_locked(struct nfs4_client *clp)
  847. {
  848. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  849. if (is_client_expired(clp)) {
  850. WARN_ON(1);
  851. printk("%s: client (clientid %08x/%08x) already expired\n",
  852. __func__,
  853. clp->cl_clientid.cl_boot,
  854. clp->cl_clientid.cl_id);
  855. return;
  856. }
  857. dprintk("renewing client (clientid %08x/%08x)\n",
  858. clp->cl_clientid.cl_boot,
  859. clp->cl_clientid.cl_id);
  860. list_move_tail(&clp->cl_lru, &nn->client_lru);
  861. clp->cl_time = get_seconds();
  862. }
  863. static inline void
  864. renew_client(struct nfs4_client *clp)
  865. {
  866. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  867. spin_lock(&nn->client_lock);
  868. renew_client_locked(clp);
  869. spin_unlock(&nn->client_lock);
  870. }
  871. /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
  872. static int
  873. STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
  874. {
  875. if (clid->cl_boot == nn->boot_time)
  876. return 0;
  877. dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
  878. clid->cl_boot, clid->cl_id, nn->boot_time);
  879. return 1;
  880. }
  881. /*
  882. * XXX Should we use a slab cache ?
  883. * This type of memory management is somewhat inefficient, but we use it
  884. * anyway since SETCLIENTID is not a common operation.
  885. */
  886. static struct nfs4_client *alloc_client(struct xdr_netobj name)
  887. {
  888. struct nfs4_client *clp;
  889. clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
  890. if (clp == NULL)
  891. return NULL;
  892. clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
  893. if (clp->cl_name.data == NULL) {
  894. kfree(clp);
  895. return NULL;
  896. }
  897. clp->cl_name.len = name.len;
  898. return clp;
  899. }
  900. static inline void
  901. free_client(struct nfs4_client *clp)
  902. {
  903. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  904. lockdep_assert_held(&nn->client_lock);
  905. while (!list_empty(&clp->cl_sessions)) {
  906. struct nfsd4_session *ses;
  907. ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
  908. se_perclnt);
  909. list_del(&ses->se_perclnt);
  910. nfsd4_put_session_locked(ses);
  911. }
  912. free_svc_cred(&clp->cl_cred);
  913. kfree(clp->cl_name.data);
  914. kfree(clp);
  915. }
  916. void
  917. release_session_client(struct nfsd4_session *session)
  918. {
  919. struct nfs4_client *clp = session->se_client;
  920. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  921. if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
  922. return;
  923. if (is_client_expired(clp)) {
  924. free_client(clp);
  925. session->se_client = NULL;
  926. } else
  927. renew_client_locked(clp);
  928. spin_unlock(&nn->client_lock);
  929. }
  930. /* must be called under the client_lock */
  931. static inline void
  932. unhash_client_locked(struct nfs4_client *clp)
  933. {
  934. struct nfsd4_session *ses;
  935. mark_client_expired(clp);
  936. list_del(&clp->cl_lru);
  937. spin_lock(&clp->cl_lock);
  938. list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
  939. list_del_init(&ses->se_hash);
  940. spin_unlock(&clp->cl_lock);
  941. }
  942. static void
  943. destroy_client(struct nfs4_client *clp)
  944. {
  945. struct nfs4_openowner *oo;
  946. struct nfs4_delegation *dp;
  947. struct list_head reaplist;
  948. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  949. INIT_LIST_HEAD(&reaplist);
  950. spin_lock(&recall_lock);
  951. while (!list_empty(&clp->cl_delegations)) {
  952. dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
  953. list_del_init(&dp->dl_perclnt);
  954. list_move(&dp->dl_recall_lru, &reaplist);
  955. }
  956. spin_unlock(&recall_lock);
  957. while (!list_empty(&reaplist)) {
  958. dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
  959. unhash_delegation(dp);
  960. }
  961. while (!list_empty(&clp->cl_openowners)) {
  962. oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
  963. release_openowner(oo);
  964. }
  965. nfsd4_shutdown_callback(clp);
  966. if (clp->cl_cb_conn.cb_xprt)
  967. svc_xprt_put(clp->cl_cb_conn.cb_xprt);
  968. list_del(&clp->cl_idhash);
  969. if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
  970. rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
  971. else
  972. rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
  973. spin_lock(&nn->client_lock);
  974. unhash_client_locked(clp);
  975. if (atomic_read(&clp->cl_refcount) == 0)
  976. free_client(clp);
  977. spin_unlock(&nn->client_lock);
  978. }
  979. static void expire_client(struct nfs4_client *clp)
  980. {
  981. nfsd4_client_record_remove(clp);
  982. destroy_client(clp);
  983. }
  984. static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
  985. {
  986. memcpy(target->cl_verifier.data, source->data,
  987. sizeof(target->cl_verifier.data));
  988. }
  989. static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
  990. {
  991. target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
  992. target->cl_clientid.cl_id = source->cl_clientid.cl_id;
  993. }
  994. static int copy_cred(struct svc_cred *target, struct svc_cred *source)
  995. {
  996. if (source->cr_principal) {
  997. target->cr_principal =
  998. kstrdup(source->cr_principal, GFP_KERNEL);
  999. if (target->cr_principal == NULL)
  1000. return -ENOMEM;
  1001. } else
  1002. target->cr_principal = NULL;
  1003. target->cr_flavor = source->cr_flavor;
  1004. target->cr_uid = source->cr_uid;
  1005. target->cr_gid = source->cr_gid;
  1006. target->cr_group_info = source->cr_group_info;
  1007. get_group_info(target->cr_group_info);
  1008. return 0;
  1009. }
  1010. static long long
  1011. compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
  1012. {
  1013. long long res;
  1014. res = o1->len - o2->len;
  1015. if (res)
  1016. return res;
  1017. return (long long)memcmp(o1->data, o2->data, o1->len);
  1018. }
  1019. static int same_name(const char *n1, const char *n2)
  1020. {
  1021. return 0 == memcmp(n1, n2, HEXDIR_LEN);
  1022. }
  1023. static int
  1024. same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
  1025. {
  1026. return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
  1027. }
  1028. static int
  1029. same_clid(clientid_t *cl1, clientid_t *cl2)
  1030. {
  1031. return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
  1032. }
  1033. static bool groups_equal(struct group_info *g1, struct group_info *g2)
  1034. {
  1035. int i;
  1036. if (g1->ngroups != g2->ngroups)
  1037. return false;
  1038. for (i=0; i<g1->ngroups; i++)
  1039. if (GROUP_AT(g1, i) != GROUP_AT(g2, i))
  1040. return false;
  1041. return true;
  1042. }
  1043. /*
  1044. * RFC 3530 language requires clid_inuse be returned when the
  1045. * "principal" associated with a requests differs from that previously
  1046. * used. We use uid, gid's, and gss principal string as our best
  1047. * approximation. We also don't want to allow non-gss use of a client
  1048. * established using gss: in theory cr_principal should catch that
  1049. * change, but in practice cr_principal can be null even in the gss case
  1050. * since gssd doesn't always pass down a principal string.
  1051. */
  1052. static bool is_gss_cred(struct svc_cred *cr)
  1053. {
  1054. /* Is cr_flavor one of the gss "pseudoflavors"?: */
  1055. return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
  1056. }
  1057. static bool
  1058. same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
  1059. {
  1060. if ((is_gss_cred(cr1) != is_gss_cred(cr2))
  1061. || (cr1->cr_uid != cr2->cr_uid)
  1062. || (cr1->cr_gid != cr2->cr_gid)
  1063. || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
  1064. return false;
  1065. if (cr1->cr_principal == cr2->cr_principal)
  1066. return true;
  1067. if (!cr1->cr_principal || !cr2->cr_principal)
  1068. return false;
  1069. return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
  1070. }
  1071. static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
  1072. {
  1073. static u32 current_clientid = 1;
  1074. clp->cl_clientid.cl_boot = nn->boot_time;
  1075. clp->cl_clientid.cl_id = current_clientid++;
  1076. }
  1077. static void gen_confirm(struct nfs4_client *clp)
  1078. {
  1079. __be32 verf[2];
  1080. static u32 i;
  1081. verf[0] = (__be32)get_seconds();
  1082. verf[1] = (__be32)i++;
  1083. memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
  1084. }
  1085. static struct nfs4_stid *find_stateid(struct nfs4_client *cl, stateid_t *t)
  1086. {
  1087. return idr_find(&cl->cl_stateids, t->si_opaque.so_id);
  1088. }
  1089. static struct nfs4_stid *find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
  1090. {
  1091. struct nfs4_stid *s;
  1092. s = find_stateid(cl, t);
  1093. if (!s)
  1094. return NULL;
  1095. if (typemask & s->sc_type)
  1096. return s;
  1097. return NULL;
  1098. }
  1099. static struct nfs4_client *create_client(struct xdr_netobj name,
  1100. struct svc_rqst *rqstp, nfs4_verifier *verf)
  1101. {
  1102. struct nfs4_client *clp;
  1103. struct sockaddr *sa = svc_addr(rqstp);
  1104. int ret;
  1105. struct net *net = SVC_NET(rqstp);
  1106. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1107. clp = alloc_client(name);
  1108. if (clp == NULL)
  1109. return NULL;
  1110. INIT_LIST_HEAD(&clp->cl_sessions);
  1111. ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
  1112. if (ret) {
  1113. spin_lock(&nn->client_lock);
  1114. free_client(clp);
  1115. spin_unlock(&nn->client_lock);
  1116. return NULL;
  1117. }
  1118. idr_init(&clp->cl_stateids);
  1119. atomic_set(&clp->cl_refcount, 0);
  1120. clp->cl_cb_state = NFSD4_CB_UNKNOWN;
  1121. INIT_LIST_HEAD(&clp->cl_idhash);
  1122. INIT_LIST_HEAD(&clp->cl_openowners);
  1123. INIT_LIST_HEAD(&clp->cl_delegations);
  1124. INIT_LIST_HEAD(&clp->cl_lru);
  1125. INIT_LIST_HEAD(&clp->cl_callbacks);
  1126. spin_lock_init(&clp->cl_lock);
  1127. nfsd4_init_callback(&clp->cl_cb_null);
  1128. clp->cl_time = get_seconds();
  1129. clear_bit(0, &clp->cl_cb_slot_busy);
  1130. rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
  1131. copy_verf(clp, verf);
  1132. rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
  1133. gen_confirm(clp);
  1134. clp->cl_cb_session = NULL;
  1135. clp->net = net;
  1136. return clp;
  1137. }
  1138. static void
  1139. add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
  1140. {
  1141. struct rb_node **new = &(root->rb_node), *parent = NULL;
  1142. struct nfs4_client *clp;
  1143. while (*new) {
  1144. clp = rb_entry(*new, struct nfs4_client, cl_namenode);
  1145. parent = *new;
  1146. if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
  1147. new = &((*new)->rb_left);
  1148. else
  1149. new = &((*new)->rb_right);
  1150. }
  1151. rb_link_node(&new_clp->cl_namenode, parent, new);
  1152. rb_insert_color(&new_clp->cl_namenode, root);
  1153. }
  1154. static struct nfs4_client *
  1155. find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
  1156. {
  1157. long long cmp;
  1158. struct rb_node *node = root->rb_node;
  1159. struct nfs4_client *clp;
  1160. while (node) {
  1161. clp = rb_entry(node, struct nfs4_client, cl_namenode);
  1162. cmp = compare_blob(&clp->cl_name, name);
  1163. if (cmp > 0)
  1164. node = node->rb_left;
  1165. else if (cmp < 0)
  1166. node = node->rb_right;
  1167. else
  1168. return clp;
  1169. }
  1170. return NULL;
  1171. }
  1172. static void
  1173. add_to_unconfirmed(struct nfs4_client *clp)
  1174. {
  1175. unsigned int idhashval;
  1176. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1177. clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
  1178. add_clp_to_name_tree(clp, &nn->unconf_name_tree);
  1179. idhashval = clientid_hashval(clp->cl_clientid.cl_id);
  1180. list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
  1181. renew_client(clp);
  1182. }
  1183. static void
  1184. move_to_confirmed(struct nfs4_client *clp)
  1185. {
  1186. unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
  1187. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1188. dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
  1189. list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
  1190. rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
  1191. add_clp_to_name_tree(clp, &nn->conf_name_tree);
  1192. set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
  1193. renew_client(clp);
  1194. }
  1195. static struct nfs4_client *
  1196. find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
  1197. {
  1198. struct nfs4_client *clp;
  1199. unsigned int idhashval = clientid_hashval(clid->cl_id);
  1200. list_for_each_entry(clp, &nn->conf_id_hashtbl[idhashval], cl_idhash) {
  1201. if (same_clid(&clp->cl_clientid, clid)) {
  1202. if ((bool)clp->cl_minorversion != sessions)
  1203. return NULL;
  1204. renew_client(clp);
  1205. return clp;
  1206. }
  1207. }
  1208. return NULL;
  1209. }
  1210. static struct nfs4_client *
  1211. find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
  1212. {
  1213. struct nfs4_client *clp;
  1214. unsigned int idhashval = clientid_hashval(clid->cl_id);
  1215. list_for_each_entry(clp, &nn->unconf_id_hashtbl[idhashval], cl_idhash) {
  1216. if (same_clid(&clp->cl_clientid, clid)) {
  1217. if ((bool)clp->cl_minorversion != sessions)
  1218. return NULL;
  1219. return clp;
  1220. }
  1221. }
  1222. return NULL;
  1223. }
  1224. static bool clp_used_exchangeid(struct nfs4_client *clp)
  1225. {
  1226. return clp->cl_exchange_flags != 0;
  1227. }
  1228. static struct nfs4_client *
  1229. find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
  1230. {
  1231. return find_clp_in_name_tree(name, &nn->conf_name_tree);
  1232. }
  1233. static struct nfs4_client *
  1234. find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
  1235. {
  1236. return find_clp_in_name_tree(name, &nn->unconf_name_tree);
  1237. }
  1238. static void
  1239. gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
  1240. {
  1241. struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
  1242. struct sockaddr *sa = svc_addr(rqstp);
  1243. u32 scopeid = rpc_get_scope_id(sa);
  1244. unsigned short expected_family;
  1245. /* Currently, we only support tcp and tcp6 for the callback channel */
  1246. if (se->se_callback_netid_len == 3 &&
  1247. !memcmp(se->se_callback_netid_val, "tcp", 3))
  1248. expected_family = AF_INET;
  1249. else if (se->se_callback_netid_len == 4 &&
  1250. !memcmp(se->se_callback_netid_val, "tcp6", 4))
  1251. expected_family = AF_INET6;
  1252. else
  1253. goto out_err;
  1254. conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
  1255. se->se_callback_addr_len,
  1256. (struct sockaddr *)&conn->cb_addr,
  1257. sizeof(conn->cb_addr));
  1258. if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
  1259. goto out_err;
  1260. if (conn->cb_addr.ss_family == AF_INET6)
  1261. ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
  1262. conn->cb_prog = se->se_callback_prog;
  1263. conn->cb_ident = se->se_callback_ident;
  1264. memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
  1265. return;
  1266. out_err:
  1267. conn->cb_addr.ss_family = AF_UNSPEC;
  1268. conn->cb_addrlen = 0;
  1269. dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
  1270. "will not receive delegations\n",
  1271. clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
  1272. return;
  1273. }
  1274. /*
  1275. * Cache a reply. nfsd4_check_drc_limit() has bounded the cache size.
  1276. */
  1277. void
  1278. nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
  1279. {
  1280. struct nfsd4_slot *slot = resp->cstate.slot;
  1281. unsigned int base;
  1282. dprintk("--> %s slot %p\n", __func__, slot);
  1283. slot->sl_opcnt = resp->opcnt;
  1284. slot->sl_status = resp->cstate.status;
  1285. slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
  1286. if (nfsd4_not_cached(resp)) {
  1287. slot->sl_datalen = 0;
  1288. return;
  1289. }
  1290. slot->sl_datalen = (char *)resp->p - (char *)resp->cstate.datap;
  1291. base = (char *)resp->cstate.datap -
  1292. (char *)resp->xbuf->head[0].iov_base;
  1293. if (read_bytes_from_xdr_buf(resp->xbuf, base, slot->sl_data,
  1294. slot->sl_datalen))
  1295. WARN("%s: sessions DRC could not cache compound\n", __func__);
  1296. return;
  1297. }
  1298. /*
  1299. * Encode the replay sequence operation from the slot values.
  1300. * If cachethis is FALSE encode the uncached rep error on the next
  1301. * operation which sets resp->p and increments resp->opcnt for
  1302. * nfs4svc_encode_compoundres.
  1303. *
  1304. */
  1305. static __be32
  1306. nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
  1307. struct nfsd4_compoundres *resp)
  1308. {
  1309. struct nfsd4_op *op;
  1310. struct nfsd4_slot *slot = resp->cstate.slot;
  1311. /* Encode the replayed sequence operation */
  1312. op = &args->ops[resp->opcnt - 1];
  1313. nfsd4_encode_operation(resp, op);
  1314. /* Return nfserr_retry_uncached_rep in next operation. */
  1315. if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
  1316. op = &args->ops[resp->opcnt++];
  1317. op->status = nfserr_retry_uncached_rep;
  1318. nfsd4_encode_operation(resp, op);
  1319. }
  1320. return op->status;
  1321. }
  1322. /*
  1323. * The sequence operation is not cached because we can use the slot and
  1324. * session values.
  1325. */
  1326. __be32
  1327. nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
  1328. struct nfsd4_sequence *seq)
  1329. {
  1330. struct nfsd4_slot *slot = resp->cstate.slot;
  1331. __be32 status;
  1332. dprintk("--> %s slot %p\n", __func__, slot);
  1333. /* Either returns 0 or nfserr_retry_uncached */
  1334. status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
  1335. if (status == nfserr_retry_uncached_rep)
  1336. return status;
  1337. /* The sequence operation has been encoded, cstate->datap set. */
  1338. memcpy(resp->cstate.datap, slot->sl_data, slot->sl_datalen);
  1339. resp->opcnt = slot->sl_opcnt;
  1340. resp->p = resp->cstate.datap + XDR_QUADLEN(slot->sl_datalen);
  1341. status = slot->sl_status;
  1342. return status;
  1343. }
  1344. /*
  1345. * Set the exchange_id flags returned by the server.
  1346. */
  1347. static void
  1348. nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
  1349. {
  1350. /* pNFS is not supported */
  1351. new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
  1352. /* Referrals are supported, Migration is not. */
  1353. new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
  1354. /* set the wire flags to return to client. */
  1355. clid->flags = new->cl_exchange_flags;
  1356. }
  1357. static bool client_has_state(struct nfs4_client *clp)
  1358. {
  1359. /*
  1360. * Note clp->cl_openowners check isn't quite right: there's no
  1361. * need to count owners without stateid's.
  1362. *
  1363. * Also note we should probably be using this in 4.0 case too.
  1364. */
  1365. return !list_empty(&clp->cl_openowners)
  1366. || !list_empty(&clp->cl_delegations)
  1367. || !list_empty(&clp->cl_sessions);
  1368. }
  1369. __be32
  1370. nfsd4_exchange_id(struct svc_rqst *rqstp,
  1371. struct nfsd4_compound_state *cstate,
  1372. struct nfsd4_exchange_id *exid)
  1373. {
  1374. struct nfs4_client *unconf, *conf, *new;
  1375. __be32 status;
  1376. char addr_str[INET6_ADDRSTRLEN];
  1377. nfs4_verifier verf = exid->verifier;
  1378. struct sockaddr *sa = svc_addr(rqstp);
  1379. bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
  1380. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1381. rpc_ntop(sa, addr_str, sizeof(addr_str));
  1382. dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
  1383. "ip_addr=%s flags %x, spa_how %d\n",
  1384. __func__, rqstp, exid, exid->clname.len, exid->clname.data,
  1385. addr_str, exid->flags, exid->spa_how);
  1386. if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
  1387. return nfserr_inval;
  1388. /* Currently only support SP4_NONE */
  1389. switch (exid->spa_how) {
  1390. case SP4_NONE:
  1391. break;
  1392. default: /* checked by xdr code */
  1393. WARN_ON_ONCE(1);
  1394. case SP4_SSV:
  1395. case SP4_MACH_CRED:
  1396. return nfserr_serverfault; /* no excuse :-/ */
  1397. }
  1398. /* Cases below refer to rfc 5661 section 18.35.4: */
  1399. nfs4_lock_state();
  1400. conf = find_confirmed_client_by_name(&exid->clname, nn);
  1401. if (conf) {
  1402. bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
  1403. bool verfs_match = same_verf(&verf, &conf->cl_verifier);
  1404. if (update) {
  1405. if (!clp_used_exchangeid(conf)) { /* buggy client */
  1406. status = nfserr_inval;
  1407. goto out;
  1408. }
  1409. if (!creds_match) { /* case 9 */
  1410. status = nfserr_perm;
  1411. goto out;
  1412. }
  1413. if (!verfs_match) { /* case 8 */
  1414. status = nfserr_not_same;
  1415. goto out;
  1416. }
  1417. /* case 6 */
  1418. exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
  1419. new = conf;
  1420. goto out_copy;
  1421. }
  1422. if (!creds_match) { /* case 3 */
  1423. if (client_has_state(conf)) {
  1424. status = nfserr_clid_inuse;
  1425. goto out;
  1426. }
  1427. expire_client(conf);
  1428. goto out_new;
  1429. }
  1430. if (verfs_match) { /* case 2 */
  1431. conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
  1432. new = conf;
  1433. goto out_copy;
  1434. }
  1435. /* case 5, client reboot */
  1436. goto out_new;
  1437. }
  1438. if (update) { /* case 7 */
  1439. status = nfserr_noent;
  1440. goto out;
  1441. }
  1442. unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
  1443. if (unconf) /* case 4, possible retry or client restart */
  1444. expire_client(unconf);
  1445. /* case 1 (normal case) */
  1446. out_new:
  1447. new = create_client(exid->clname, rqstp, &verf);
  1448. if (new == NULL) {
  1449. status = nfserr_jukebox;
  1450. goto out;
  1451. }
  1452. new->cl_minorversion = 1;
  1453. gen_clid(new, nn);
  1454. add_to_unconfirmed(new);
  1455. out_copy:
  1456. exid->clientid.cl_boot = new->cl_clientid.cl_boot;
  1457. exid->clientid.cl_id = new->cl_clientid.cl_id;
  1458. exid->seqid = new->cl_cs_slot.sl_seqid + 1;
  1459. nfsd4_set_ex_flags(new, exid);
  1460. dprintk("nfsd4_exchange_id seqid %d flags %x\n",
  1461. new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
  1462. status = nfs_ok;
  1463. out:
  1464. nfs4_unlock_state();
  1465. return status;
  1466. }
  1467. static __be32
  1468. check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
  1469. {
  1470. dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
  1471. slot_seqid);
  1472. /* The slot is in use, and no response has been sent. */
  1473. if (slot_inuse) {
  1474. if (seqid == slot_seqid)
  1475. return nfserr_jukebox;
  1476. else
  1477. return nfserr_seq_misordered;
  1478. }
  1479. /* Note unsigned 32-bit arithmetic handles wraparound: */
  1480. if (likely(seqid == slot_seqid + 1))
  1481. return nfs_ok;
  1482. if (seqid == slot_seqid)
  1483. return nfserr_replay_cache;
  1484. return nfserr_seq_misordered;
  1485. }
  1486. /*
  1487. * Cache the create session result into the create session single DRC
  1488. * slot cache by saving the xdr structure. sl_seqid has been set.
  1489. * Do this for solo or embedded create session operations.
  1490. */
  1491. static void
  1492. nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
  1493. struct nfsd4_clid_slot *slot, __be32 nfserr)
  1494. {
  1495. slot->sl_status = nfserr;
  1496. memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
  1497. }
  1498. static __be32
  1499. nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
  1500. struct nfsd4_clid_slot *slot)
  1501. {
  1502. memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
  1503. return slot->sl_status;
  1504. }
  1505. #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
  1506. 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
  1507. 1 + /* MIN tag is length with zero, only length */ \
  1508. 3 + /* version, opcount, opcode */ \
  1509. XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
  1510. /* seqid, slotID, slotID, cache */ \
  1511. 4 ) * sizeof(__be32))
  1512. #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
  1513. 2 + /* verifier: AUTH_NULL, length 0 */\
  1514. 1 + /* status */ \
  1515. 1 + /* MIN tag is length with zero, only length */ \
  1516. 3 + /* opcount, opcode, opstatus*/ \
  1517. XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
  1518. /* seqid, slotID, slotID, slotID, status */ \
  1519. 5 ) * sizeof(__be32))
  1520. static bool check_forechannel_attrs(struct nfsd4_channel_attrs fchannel)
  1521. {
  1522. return fchannel.maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ
  1523. || fchannel.maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ;
  1524. }
  1525. __be32
  1526. nfsd4_create_session(struct svc_rqst *rqstp,
  1527. struct nfsd4_compound_state *cstate,
  1528. struct nfsd4_create_session *cr_ses)
  1529. {
  1530. struct sockaddr *sa = svc_addr(rqstp);
  1531. struct nfs4_client *conf, *unconf;
  1532. struct nfsd4_session *new;
  1533. struct nfsd4_conn *conn;
  1534. struct nfsd4_clid_slot *cs_slot = NULL;
  1535. __be32 status = 0;
  1536. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1537. if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
  1538. return nfserr_inval;
  1539. if (check_forechannel_attrs(cr_ses->fore_channel))
  1540. return nfserr_toosmall;
  1541. new = alloc_session(&cr_ses->fore_channel, nn);
  1542. if (!new)
  1543. return nfserr_jukebox;
  1544. status = nfserr_jukebox;
  1545. conn = alloc_conn_from_crses(rqstp, cr_ses);
  1546. if (!conn)
  1547. goto out_free_session;
  1548. nfs4_lock_state();
  1549. unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
  1550. conf = find_confirmed_client(&cr_ses->clientid, true, nn);
  1551. if (conf) {
  1552. cs_slot = &conf->cl_cs_slot;
  1553. status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
  1554. if (status == nfserr_replay_cache) {
  1555. status = nfsd4_replay_create_session(cr_ses, cs_slot);
  1556. goto out_free_conn;
  1557. } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
  1558. status = nfserr_seq_misordered;
  1559. goto out_free_conn;
  1560. }
  1561. } else if (unconf) {
  1562. struct nfs4_client *old;
  1563. if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
  1564. !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
  1565. status = nfserr_clid_inuse;
  1566. goto out_free_conn;
  1567. }
  1568. cs_slot = &unconf->cl_cs_slot;
  1569. status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
  1570. if (status) {
  1571. /* an unconfirmed replay returns misordered */
  1572. status = nfserr_seq_misordered;
  1573. goto out_free_conn;
  1574. }
  1575. old = find_confirmed_client_by_name(&unconf->cl_name, nn);
  1576. if (old)
  1577. expire_client(old);
  1578. move_to_confirmed(unconf);
  1579. conf = unconf;
  1580. } else {
  1581. status = nfserr_stale_clientid;
  1582. goto out_free_conn;
  1583. }
  1584. status = nfs_ok;
  1585. /*
  1586. * We do not support RDMA or persistent sessions
  1587. */
  1588. cr_ses->flags &= ~SESSION4_PERSIST;
  1589. cr_ses->flags &= ~SESSION4_RDMA;
  1590. init_session(rqstp, new, conf, cr_ses);
  1591. nfsd4_init_conn(rqstp, conn, new);
  1592. memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
  1593. NFS4_MAX_SESSIONID_LEN);
  1594. memcpy(&cr_ses->fore_channel, &new->se_fchannel,
  1595. sizeof(struct nfsd4_channel_attrs));
  1596. cs_slot->sl_seqid++;
  1597. cr_ses->seqid = cs_slot->sl_seqid;
  1598. /* cache solo and embedded create sessions under the state lock */
  1599. nfsd4_cache_create_session(cr_ses, cs_slot, status);
  1600. out:
  1601. nfs4_unlock_state();
  1602. dprintk("%s returns %d\n", __func__, ntohl(status));
  1603. return status;
  1604. out_free_conn:
  1605. free_conn(conn);
  1606. out_free_session:
  1607. __free_session(new);
  1608. goto out;
  1609. }
  1610. static __be32 nfsd4_map_bcts_dir(u32 *dir)
  1611. {
  1612. switch (*dir) {
  1613. case NFS4_CDFC4_FORE:
  1614. case NFS4_CDFC4_BACK:
  1615. return nfs_ok;
  1616. case NFS4_CDFC4_FORE_OR_BOTH:
  1617. case NFS4_CDFC4_BACK_OR_BOTH:
  1618. *dir = NFS4_CDFC4_BOTH;
  1619. return nfs_ok;
  1620. };
  1621. return nfserr_inval;
  1622. }
  1623. __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
  1624. {
  1625. struct nfsd4_session *session = cstate->session;
  1626. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1627. spin_lock(&nn->client_lock);
  1628. session->se_cb_prog = bc->bc_cb_program;
  1629. session->se_cb_sec = bc->bc_cb_sec;
  1630. spin_unlock(&nn->client_lock);
  1631. nfsd4_probe_callback(session->se_client);
  1632. return nfs_ok;
  1633. }
  1634. __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
  1635. struct nfsd4_compound_state *cstate,
  1636. struct nfsd4_bind_conn_to_session *bcts)
  1637. {
  1638. __be32 status;
  1639. struct nfsd4_conn *conn;
  1640. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1641. if (!nfsd4_last_compound_op(rqstp))
  1642. return nfserr_not_only_op;
  1643. spin_lock(&nn->client_lock);
  1644. cstate->session = find_in_sessionid_hashtbl(&bcts->sessionid, SVC_NET(rqstp));
  1645. /* Sorta weird: we only need the refcnt'ing because new_conn acquires
  1646. * client_lock iself: */
  1647. if (cstate->session) {
  1648. nfsd4_get_session(cstate->session);
  1649. atomic_inc(&cstate->session->se_client->cl_refcount);
  1650. }
  1651. spin_unlock(&nn->client_lock);
  1652. if (!cstate->session)
  1653. return nfserr_badsession;
  1654. status = nfsd4_map_bcts_dir(&bcts->dir);
  1655. if (status)
  1656. return status;
  1657. conn = alloc_conn(rqstp, bcts->dir);
  1658. if (!conn)
  1659. return nfserr_jukebox;
  1660. nfsd4_init_conn(rqstp, conn, cstate->session);
  1661. return nfs_ok;
  1662. }
  1663. static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
  1664. {
  1665. if (!session)
  1666. return 0;
  1667. return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
  1668. }
  1669. __be32
  1670. nfsd4_destroy_session(struct svc_rqst *r,
  1671. struct nfsd4_compound_state *cstate,
  1672. struct nfsd4_destroy_session *sessionid)
  1673. {
  1674. struct nfsd4_session *ses;
  1675. __be32 status = nfserr_badsession;
  1676. struct nfsd_net *nn = net_generic(SVC_NET(r), nfsd_net_id);
  1677. /* Notes:
  1678. * - The confirmed nfs4_client->cl_sessionid holds destroyed sessinid
  1679. * - Should we return nfserr_back_chan_busy if waiting for
  1680. * callbacks on to-be-destroyed session?
  1681. * - Do we need to clear any callback info from previous session?
  1682. */
  1683. if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
  1684. if (!nfsd4_last_compound_op(r))
  1685. return nfserr_not_only_op;
  1686. }
  1687. dump_sessionid(__func__, &sessionid->sessionid);
  1688. spin_lock(&nn->client_lock);
  1689. ses = find_in_sessionid_hashtbl(&sessionid->sessionid, SVC_NET(r));
  1690. if (!ses) {
  1691. spin_unlock(&nn->client_lock);
  1692. goto out;
  1693. }
  1694. unhash_session(ses);
  1695. spin_unlock(&nn->client_lock);
  1696. nfs4_lock_state();
  1697. nfsd4_probe_callback_sync(ses->se_client);
  1698. nfs4_unlock_state();
  1699. spin_lock(&nn->client_lock);
  1700. nfsd4_del_conns(ses);
  1701. nfsd4_put_session_locked(ses);
  1702. spin_unlock(&nn->client_lock);
  1703. status = nfs_ok;
  1704. out:
  1705. dprintk("%s returns %d\n", __func__, ntohl(status));
  1706. return status;
  1707. }
  1708. static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
  1709. {
  1710. struct nfsd4_conn *c;
  1711. list_for_each_entry(c, &s->se_conns, cn_persession) {
  1712. if (c->cn_xprt == xpt) {
  1713. return c;
  1714. }
  1715. }
  1716. return NULL;
  1717. }
  1718. static void nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
  1719. {
  1720. struct nfs4_client *clp = ses->se_client;
  1721. struct nfsd4_conn *c;
  1722. int ret;
  1723. spin_lock(&clp->cl_lock);
  1724. c = __nfsd4_find_conn(new->cn_xprt, ses);
  1725. if (c) {
  1726. spin_unlock(&clp->cl_lock);
  1727. free_conn(new);
  1728. return;
  1729. }
  1730. __nfsd4_hash_conn(new, ses);
  1731. spin_unlock(&clp->cl_lock);
  1732. ret = nfsd4_register_conn(new);
  1733. if (ret)
  1734. /* oops; xprt is already down: */
  1735. nfsd4_conn_lost(&new->cn_xpt_user);
  1736. return;
  1737. }
  1738. static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
  1739. {
  1740. struct nfsd4_compoundargs *args = rqstp->rq_argp;
  1741. return args->opcnt > session->se_fchannel.maxops;
  1742. }
  1743. static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
  1744. struct nfsd4_session *session)
  1745. {
  1746. struct xdr_buf *xb = &rqstp->rq_arg;
  1747. return xb->len > session->se_fchannel.maxreq_sz;
  1748. }
  1749. __be32
  1750. nfsd4_sequence(struct svc_rqst *rqstp,
  1751. struct nfsd4_compound_state *cstate,
  1752. struct nfsd4_sequence *seq)
  1753. {
  1754. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  1755. struct nfsd4_session *session;
  1756. struct nfsd4_slot *slot;
  1757. struct nfsd4_conn *conn;
  1758. __be32 status;
  1759. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1760. if (resp->opcnt != 1)
  1761. return nfserr_sequence_pos;
  1762. /*
  1763. * Will be either used or freed by nfsd4_sequence_check_conn
  1764. * below.
  1765. */
  1766. conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
  1767. if (!conn)
  1768. return nfserr_jukebox;
  1769. spin_lock(&nn->client_lock);
  1770. status = nfserr_badsession;
  1771. session = find_in_sessionid_hashtbl(&seq->sessionid, SVC_NET(rqstp));
  1772. if (!session)
  1773. goto out;
  1774. status = nfserr_too_many_ops;
  1775. if (nfsd4_session_too_many_ops(rqstp, session))
  1776. goto out;
  1777. status = nfserr_req_too_big;
  1778. if (nfsd4_request_too_big(rqstp, session))
  1779. goto out;
  1780. status = nfserr_badslot;
  1781. if (seq->slotid >= session->se_fchannel.maxreqs)
  1782. goto out;
  1783. slot = session->se_slots[seq->slotid];
  1784. dprintk("%s: slotid %d\n", __func__, seq->slotid);
  1785. /* We do not negotiate the number of slots yet, so set the
  1786. * maxslots to the session maxreqs which is used to encode
  1787. * sr_highest_slotid and the sr_target_slot id to maxslots */
  1788. seq->maxslots = session->se_fchannel.maxreqs;
  1789. status = check_slot_seqid(seq->seqid, slot->sl_seqid,
  1790. slot->sl_flags & NFSD4_SLOT_INUSE);
  1791. if (status == nfserr_replay_cache) {
  1792. status = nfserr_seq_misordered;
  1793. if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
  1794. goto out;
  1795. cstate->slot = slot;
  1796. cstate->session = session;
  1797. /* Return the cached reply status and set cstate->status
  1798. * for nfsd4_proc_compound processing */
  1799. status = nfsd4_replay_cache_entry(resp, seq);
  1800. cstate->status = nfserr_replay_cache;
  1801. goto out;
  1802. }
  1803. if (status)
  1804. goto out;
  1805. nfsd4_sequence_check_conn(conn, session);
  1806. conn = NULL;
  1807. /* Success! bump slot seqid */
  1808. slot->sl_seqid = seq->seqid;
  1809. slot->sl_flags |= NFSD4_SLOT_INUSE;
  1810. if (seq->cachethis)
  1811. slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
  1812. else
  1813. slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
  1814. cstate->slot = slot;
  1815. cstate->session = session;
  1816. out:
  1817. /* Hold a session reference until done processing the compound. */
  1818. if (cstate->session) {
  1819. struct nfs4_client *clp = session->se_client;
  1820. nfsd4_get_session(cstate->session);
  1821. atomic_inc(&clp->cl_refcount);
  1822. switch (clp->cl_cb_state) {
  1823. case NFSD4_CB_DOWN:
  1824. seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
  1825. break;
  1826. case NFSD4_CB_FAULT:
  1827. seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
  1828. break;
  1829. default:
  1830. seq->status_flags = 0;
  1831. }
  1832. }
  1833. kfree(conn);
  1834. spin_unlock(&nn->client_lock);
  1835. dprintk("%s: return %d\n", __func__, ntohl(status));
  1836. return status;
  1837. }
  1838. __be32
  1839. nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
  1840. {
  1841. struct nfs4_client *conf, *unconf, *clp;
  1842. __be32 status = 0;
  1843. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1844. nfs4_lock_state();
  1845. unconf = find_unconfirmed_client(&dc->clientid, true, nn);
  1846. conf = find_confirmed_client(&dc->clientid, true, nn);
  1847. if (conf) {
  1848. clp = conf;
  1849. if (!is_client_expired(conf) && client_has_state(conf)) {
  1850. status = nfserr_clientid_busy;
  1851. goto out;
  1852. }
  1853. /* rfc5661 18.50.3 */
  1854. if (cstate->session && conf == cstate->session->se_client) {
  1855. status = nfserr_clientid_busy;
  1856. goto out;
  1857. }
  1858. } else if (unconf)
  1859. clp = unconf;
  1860. else {
  1861. status = nfserr_stale_clientid;
  1862. goto out;
  1863. }
  1864. expire_client(clp);
  1865. out:
  1866. nfs4_unlock_state();
  1867. dprintk("%s return %d\n", __func__, ntohl(status));
  1868. return status;
  1869. }
  1870. __be32
  1871. nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
  1872. {
  1873. __be32 status = 0;
  1874. if (rc->rca_one_fs) {
  1875. if (!cstate->current_fh.fh_dentry)
  1876. return nfserr_nofilehandle;
  1877. /*
  1878. * We don't take advantage of the rca_one_fs case.
  1879. * That's OK, it's optional, we can safely ignore it.
  1880. */
  1881. return nfs_ok;
  1882. }
  1883. nfs4_lock_state();
  1884. status = nfserr_complete_already;
  1885. if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
  1886. &cstate->session->se_client->cl_flags))
  1887. goto out;
  1888. status = nfserr_stale_clientid;
  1889. if (is_client_expired(cstate->session->se_client))
  1890. /*
  1891. * The following error isn't really legal.
  1892. * But we only get here if the client just explicitly
  1893. * destroyed the client. Surely it no longer cares what
  1894. * error it gets back on an operation for the dead
  1895. * client.
  1896. */
  1897. goto out;
  1898. status = nfs_ok;
  1899. nfsd4_client_record_create(cstate->session->se_client);
  1900. out:
  1901. nfs4_unlock_state();
  1902. return status;
  1903. }
  1904. __be32
  1905. nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  1906. struct nfsd4_setclientid *setclid)
  1907. {
  1908. struct xdr_netobj clname = setclid->se_name;
  1909. nfs4_verifier clverifier = setclid->se_verf;
  1910. struct nfs4_client *conf, *unconf, *new;
  1911. __be32 status;
  1912. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1913. /* Cases below refer to rfc 3530 section 14.2.33: */
  1914. nfs4_lock_state();
  1915. conf = find_confirmed_client_by_name(&clname, nn);
  1916. if (conf) {
  1917. /* case 0: */
  1918. status = nfserr_clid_inuse;
  1919. if (clp_used_exchangeid(conf))
  1920. goto out;
  1921. if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
  1922. char addr_str[INET6_ADDRSTRLEN];
  1923. rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
  1924. sizeof(addr_str));
  1925. dprintk("NFSD: setclientid: string in use by client "
  1926. "at %s\n", addr_str);
  1927. goto out;
  1928. }
  1929. }
  1930. unconf = find_unconfirmed_client_by_name(&clname, nn);
  1931. if (unconf)
  1932. expire_client(unconf);
  1933. status = nfserr_jukebox;
  1934. new = create_client(clname, rqstp, &clverifier);
  1935. if (new == NULL)
  1936. goto out;
  1937. if (conf && same_verf(&conf->cl_verifier, &clverifier))
  1938. /* case 1: probable callback update */
  1939. copy_clid(new, conf);
  1940. else /* case 4 (new client) or cases 2, 3 (client reboot): */
  1941. gen_clid(new, nn);
  1942. new->cl_minorversion = 0;
  1943. gen_callback(new, setclid, rqstp);
  1944. add_to_unconfirmed(new);
  1945. setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
  1946. setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
  1947. memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
  1948. status = nfs_ok;
  1949. out:
  1950. nfs4_unlock_state();
  1951. return status;
  1952. }
  1953. __be32
  1954. nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
  1955. struct nfsd4_compound_state *cstate,
  1956. struct nfsd4_setclientid_confirm *setclientid_confirm)
  1957. {
  1958. struct nfs4_client *conf, *unconf;
  1959. nfs4_verifier confirm = setclientid_confirm->sc_confirm;
  1960. clientid_t * clid = &setclientid_confirm->sc_clientid;
  1961. __be32 status;
  1962. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1963. if (STALE_CLIENTID(clid, nn))
  1964. return nfserr_stale_clientid;
  1965. nfs4_lock_state();
  1966. conf = find_confirmed_client(clid, false, nn);
  1967. unconf = find_unconfirmed_client(clid, false, nn);
  1968. /*
  1969. * We try hard to give out unique clientid's, so if we get an
  1970. * attempt to confirm the same clientid with a different cred,
  1971. * there's a bug somewhere. Let's charitably assume it's our
  1972. * bug.
  1973. */
  1974. status = nfserr_serverfault;
  1975. if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
  1976. goto out;
  1977. if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
  1978. goto out;
  1979. /* cases below refer to rfc 3530 section 14.2.34: */
  1980. if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
  1981. if (conf && !unconf) /* case 2: probable retransmit */
  1982. status = nfs_ok;
  1983. else /* case 4: client hasn't noticed we rebooted yet? */
  1984. status = nfserr_stale_clientid;
  1985. goto out;
  1986. }
  1987. status = nfs_ok;
  1988. if (conf) { /* case 1: callback update */
  1989. nfsd4_change_callback(conf, &unconf->cl_cb_conn);
  1990. nfsd4_probe_callback(conf);
  1991. expire_client(unconf);
  1992. } else { /* case 3: normal case; new or rebooted client */
  1993. conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
  1994. if (conf)
  1995. expire_client(conf);
  1996. move_to_confirmed(unconf);
  1997. nfsd4_probe_callback(unconf);
  1998. }
  1999. out:
  2000. nfs4_unlock_state();
  2001. return status;
  2002. }
  2003. static struct nfs4_file *nfsd4_alloc_file(void)
  2004. {
  2005. return kmem_cache_alloc(file_slab, GFP_KERNEL);
  2006. }
  2007. /* OPEN Share state helper functions */
  2008. static void nfsd4_init_file(struct nfs4_file *fp, struct inode *ino)
  2009. {
  2010. unsigned int hashval = file_hashval(ino);
  2011. atomic_set(&fp->fi_ref, 1);
  2012. INIT_LIST_HEAD(&fp->fi_hash);
  2013. INIT_LIST_HEAD(&fp->fi_stateids);
  2014. INIT_LIST_HEAD(&fp->fi_delegations);
  2015. fp->fi_inode = igrab(ino);
  2016. fp->fi_had_conflict = false;
  2017. fp->fi_lease = NULL;
  2018. memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
  2019. memset(fp->fi_access, 0, sizeof(fp->fi_access));
  2020. spin_lock(&recall_lock);
  2021. list_add(&fp->fi_hash, &file_hashtbl[hashval]);
  2022. spin_unlock(&recall_lock);
  2023. }
  2024. static void
  2025. nfsd4_free_slab(struct kmem_cache **slab)
  2026. {
  2027. if (*slab == NULL)
  2028. return;
  2029. kmem_cache_destroy(*slab);
  2030. *slab = NULL;
  2031. }
  2032. void
  2033. nfsd4_free_slabs(void)
  2034. {
  2035. nfsd4_free_slab(&openowner_slab);
  2036. nfsd4_free_slab(&lockowner_slab);
  2037. nfsd4_free_slab(&file_slab);
  2038. nfsd4_free_slab(&stateid_slab);
  2039. nfsd4_free_slab(&deleg_slab);
  2040. }
  2041. int
  2042. nfsd4_init_slabs(void)
  2043. {
  2044. openowner_slab = kmem_cache_create("nfsd4_openowners",
  2045. sizeof(struct nfs4_openowner), 0, 0, NULL);
  2046. if (openowner_slab == NULL)
  2047. goto out_nomem;
  2048. lockowner_slab = kmem_cache_create("nfsd4_lockowners",
  2049. sizeof(struct nfs4_lockowner), 0, 0, NULL);
  2050. if (lockowner_slab == NULL)
  2051. goto out_nomem;
  2052. file_slab = kmem_cache_create("nfsd4_files",
  2053. sizeof(struct nfs4_file), 0, 0, NULL);
  2054. if (file_slab == NULL)
  2055. goto out_nomem;
  2056. stateid_slab = kmem_cache_create("nfsd4_stateids",
  2057. sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
  2058. if (stateid_slab == NULL)
  2059. goto out_nomem;
  2060. deleg_slab = kmem_cache_create("nfsd4_delegations",
  2061. sizeof(struct nfs4_delegation), 0, 0, NULL);
  2062. if (deleg_slab == NULL)
  2063. goto out_nomem;
  2064. return 0;
  2065. out_nomem:
  2066. nfsd4_free_slabs();
  2067. dprintk("nfsd4: out of memory while initializing nfsv4\n");
  2068. return -ENOMEM;
  2069. }
  2070. void nfs4_free_openowner(struct nfs4_openowner *oo)
  2071. {
  2072. kfree(oo->oo_owner.so_owner.data);
  2073. kmem_cache_free(openowner_slab, oo);
  2074. }
  2075. void nfs4_free_lockowner(struct nfs4_lockowner *lo)
  2076. {
  2077. kfree(lo->lo_owner.so_owner.data);
  2078. kmem_cache_free(lockowner_slab, lo);
  2079. }
  2080. static void init_nfs4_replay(struct nfs4_replay *rp)
  2081. {
  2082. rp->rp_status = nfserr_serverfault;
  2083. rp->rp_buflen = 0;
  2084. rp->rp_buf = rp->rp_ibuf;
  2085. }
  2086. static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
  2087. {
  2088. struct nfs4_stateowner *sop;
  2089. sop = kmem_cache_alloc(slab, GFP_KERNEL);
  2090. if (!sop)
  2091. return NULL;
  2092. sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
  2093. if (!sop->so_owner.data) {
  2094. kmem_cache_free(slab, sop);
  2095. return NULL;
  2096. }
  2097. sop->so_owner.len = owner->len;
  2098. INIT_LIST_HEAD(&sop->so_stateids);
  2099. sop->so_client = clp;
  2100. init_nfs4_replay(&sop->so_replay);
  2101. return sop;
  2102. }
  2103. static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
  2104. {
  2105. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  2106. list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
  2107. list_add(&oo->oo_perclient, &clp->cl_openowners);
  2108. }
  2109. static struct nfs4_openowner *
  2110. alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
  2111. struct nfs4_openowner *oo;
  2112. oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
  2113. if (!oo)
  2114. return NULL;
  2115. oo->oo_owner.so_is_open_owner = 1;
  2116. oo->oo_owner.so_seqid = open->op_seqid;
  2117. oo->oo_flags = NFS4_OO_NEW;
  2118. oo->oo_time = 0;
  2119. oo->oo_last_closed_stid = NULL;
  2120. INIT_LIST_HEAD(&oo->oo_close_lru);
  2121. hash_openowner(oo, clp, strhashval);
  2122. return oo;
  2123. }
  2124. static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
  2125. struct nfs4_openowner *oo = open->op_openowner;
  2126. struct nfs4_client *clp = oo->oo_owner.so_client;
  2127. init_stid(&stp->st_stid, clp, NFS4_OPEN_STID);
  2128. INIT_LIST_HEAD(&stp->st_lockowners);
  2129. list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
  2130. list_add(&stp->st_perfile, &fp->fi_stateids);
  2131. stp->st_stateowner = &oo->oo_owner;
  2132. get_nfs4_file(fp);
  2133. stp->st_file = fp;
  2134. stp->st_access_bmap = 0;
  2135. stp->st_deny_bmap = 0;
  2136. set_access(open->op_share_access, stp);
  2137. set_deny(open->op_share_deny, stp);
  2138. stp->st_openstp = NULL;
  2139. }
  2140. static void
  2141. move_to_close_lru(struct nfs4_openowner *oo, struct net *net)
  2142. {
  2143. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  2144. dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
  2145. list_move_tail(&oo->oo_close_lru, &nn->close_lru);
  2146. oo->oo_time = get_seconds();
  2147. }
  2148. static int
  2149. same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
  2150. clientid_t *clid)
  2151. {
  2152. return (sop->so_owner.len == owner->len) &&
  2153. 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
  2154. (sop->so_client->cl_clientid.cl_id == clid->cl_id);
  2155. }
  2156. static struct nfs4_openowner *
  2157. find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
  2158. bool sessions, struct nfsd_net *nn)
  2159. {
  2160. struct nfs4_stateowner *so;
  2161. struct nfs4_openowner *oo;
  2162. struct nfs4_client *clp;
  2163. list_for_each_entry(so, &nn->ownerstr_hashtbl[hashval], so_strhash) {
  2164. if (!so->so_is_open_owner)
  2165. continue;
  2166. if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
  2167. oo = openowner(so);
  2168. clp = oo->oo_owner.so_client;
  2169. if ((bool)clp->cl_minorversion != sessions)
  2170. return NULL;
  2171. renew_client(oo->oo_owner.so_client);
  2172. return oo;
  2173. }
  2174. }
  2175. return NULL;
  2176. }
  2177. /* search file_hashtbl[] for file */
  2178. static struct nfs4_file *
  2179. find_file(struct inode *ino)
  2180. {
  2181. unsigned int hashval = file_hashval(ino);
  2182. struct nfs4_file *fp;
  2183. spin_lock(&recall_lock);
  2184. list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
  2185. if (fp->fi_inode == ino) {
  2186. get_nfs4_file(fp);
  2187. spin_unlock(&recall_lock);
  2188. return fp;
  2189. }
  2190. }
  2191. spin_unlock(&recall_lock);
  2192. return NULL;
  2193. }
  2194. /*
  2195. * Called to check deny when READ with all zero stateid or
  2196. * WRITE with all zero or all one stateid
  2197. */
  2198. static __be32
  2199. nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
  2200. {
  2201. struct inode *ino = current_fh->fh_dentry->d_inode;
  2202. struct nfs4_file *fp;
  2203. struct nfs4_ol_stateid *stp;
  2204. __be32 ret;
  2205. dprintk("NFSD: nfs4_share_conflict\n");
  2206. fp = find_file(ino);
  2207. if (!fp)
  2208. return nfs_ok;
  2209. ret = nfserr_locked;
  2210. /* Search for conflicting share reservations */
  2211. list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
  2212. if (test_deny(deny_type, stp) ||
  2213. test_deny(NFS4_SHARE_DENY_BOTH, stp))
  2214. goto out;
  2215. }
  2216. ret = nfs_ok;
  2217. out:
  2218. put_nfs4_file(fp);
  2219. return ret;
  2220. }
  2221. static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
  2222. {
  2223. /* We're assuming the state code never drops its reference
  2224. * without first removing the lease. Since we're in this lease
  2225. * callback (and since the lease code is serialized by the kernel
  2226. * lock) we know the server hasn't removed the lease yet, we know
  2227. * it's safe to take a reference: */
  2228. atomic_inc(&dp->dl_count);
  2229. list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
  2230. /* only place dl_time is set. protected by lock_flocks*/
  2231. dp->dl_time = get_seconds();
  2232. nfsd4_cb_recall(dp);
  2233. }
  2234. /* Called from break_lease() with lock_flocks() held. */
  2235. static void nfsd_break_deleg_cb(struct file_lock *fl)
  2236. {
  2237. struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
  2238. struct nfs4_delegation *dp;
  2239. if (!fp) {
  2240. WARN(1, "(%p)->fl_owner NULL\n", fl);
  2241. return;
  2242. }
  2243. if (fp->fi_had_conflict) {
  2244. WARN(1, "duplicate break on %p\n", fp);
  2245. return;
  2246. }
  2247. /*
  2248. * We don't want the locks code to timeout the lease for us;
  2249. * we'll remove it ourself if a delegation isn't returned
  2250. * in time:
  2251. */
  2252. fl->fl_break_time = 0;
  2253. spin_lock(&recall_lock);
  2254. fp->fi_had_conflict = true;
  2255. list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
  2256. nfsd_break_one_deleg(dp);
  2257. spin_unlock(&recall_lock);
  2258. }
  2259. static
  2260. int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
  2261. {
  2262. if (arg & F_UNLCK)
  2263. return lease_modify(onlist, arg);
  2264. else
  2265. return -EAGAIN;
  2266. }
  2267. static const struct lock_manager_operations nfsd_lease_mng_ops = {
  2268. .lm_break = nfsd_break_deleg_cb,
  2269. .lm_change = nfsd_change_deleg_cb,
  2270. };
  2271. static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
  2272. {
  2273. if (nfsd4_has_session(cstate))
  2274. return nfs_ok;
  2275. if (seqid == so->so_seqid - 1)
  2276. return nfserr_replay_me;
  2277. if (seqid == so->so_seqid)
  2278. return nfs_ok;
  2279. return nfserr_bad_seqid;
  2280. }
  2281. __be32
  2282. nfsd4_process_open1(struct nfsd4_compound_state *cstate,
  2283. struct nfsd4_open *open, struct nfsd_net *nn)
  2284. {
  2285. clientid_t *clientid = &open->op_clientid;
  2286. struct nfs4_client *clp = NULL;
  2287. unsigned int strhashval;
  2288. struct nfs4_openowner *oo = NULL;
  2289. __be32 status;
  2290. if (STALE_CLIENTID(&open->op_clientid, nn))
  2291. return nfserr_stale_clientid;
  2292. /*
  2293. * In case we need it later, after we've already created the
  2294. * file and don't want to risk a further failure:
  2295. */
  2296. open->op_file = nfsd4_alloc_file();
  2297. if (open->op_file == NULL)
  2298. return nfserr_jukebox;
  2299. strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
  2300. oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn);
  2301. open->op_openowner = oo;
  2302. if (!oo) {
  2303. clp = find_confirmed_client(clientid, cstate->minorversion,
  2304. nn);
  2305. if (clp == NULL)
  2306. return nfserr_expired;
  2307. goto new_owner;
  2308. }
  2309. if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
  2310. /* Replace unconfirmed owners without checking for replay. */
  2311. clp = oo->oo_owner.so_client;
  2312. release_openowner(oo);
  2313. open->op_openowner = NULL;
  2314. goto new_owner;
  2315. }
  2316. status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
  2317. if (status)
  2318. return status;
  2319. clp = oo->oo_owner.so_client;
  2320. goto alloc_stateid;
  2321. new_owner:
  2322. oo = alloc_init_open_stateowner(strhashval, clp, open);
  2323. if (oo == NULL)
  2324. return nfserr_jukebox;
  2325. open->op_openowner = oo;
  2326. alloc_stateid:
  2327. open->op_stp = nfs4_alloc_stateid(clp);
  2328. if (!open->op_stp)
  2329. return nfserr_jukebox;
  2330. return nfs_ok;
  2331. }
  2332. static inline __be32
  2333. nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
  2334. {
  2335. if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
  2336. return nfserr_openmode;
  2337. else
  2338. return nfs_ok;
  2339. }
  2340. static int share_access_to_flags(u32 share_access)
  2341. {
  2342. return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
  2343. }
  2344. static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
  2345. {
  2346. struct nfs4_stid *ret;
  2347. ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
  2348. if (!ret)
  2349. return NULL;
  2350. return delegstateid(ret);
  2351. }
  2352. static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
  2353. {
  2354. return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
  2355. open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
  2356. }
  2357. static __be32
  2358. nfs4_check_deleg(struct nfs4_client *cl, struct nfs4_file *fp, struct nfsd4_open *open,
  2359. struct nfs4_delegation **dp)
  2360. {
  2361. int flags;
  2362. __be32 status = nfserr_bad_stateid;
  2363. *dp = find_deleg_stateid(cl, &open->op_delegate_stateid);
  2364. if (*dp == NULL)
  2365. goto out;
  2366. flags = share_access_to_flags(open->op_share_access);
  2367. status = nfs4_check_delegmode(*dp, flags);
  2368. if (status)
  2369. *dp = NULL;
  2370. out:
  2371. if (!nfsd4_is_deleg_cur(open))
  2372. return nfs_ok;
  2373. if (status)
  2374. return status;
  2375. open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
  2376. return nfs_ok;
  2377. }
  2378. static __be32
  2379. nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_ol_stateid **stpp)
  2380. {
  2381. struct nfs4_ol_stateid *local;
  2382. struct nfs4_openowner *oo = open->op_openowner;
  2383. list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
  2384. /* ignore lock owners */
  2385. if (local->st_stateowner->so_is_open_owner == 0)
  2386. continue;
  2387. /* remember if we have seen this open owner */
  2388. if (local->st_stateowner == &oo->oo_owner)
  2389. *stpp = local;
  2390. /* check for conflicting share reservations */
  2391. if (!test_share(local, open))
  2392. return nfserr_share_denied;
  2393. }
  2394. return nfs_ok;
  2395. }
  2396. static inline int nfs4_access_to_access(u32 nfs4_access)
  2397. {
  2398. int flags = 0;
  2399. if (nfs4_access & NFS4_SHARE_ACCESS_READ)
  2400. flags |= NFSD_MAY_READ;
  2401. if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
  2402. flags |= NFSD_MAY_WRITE;
  2403. return flags;
  2404. }
  2405. static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
  2406. struct svc_fh *cur_fh, struct nfsd4_open *open)
  2407. {
  2408. __be32 status;
  2409. int oflag = nfs4_access_to_omode(open->op_share_access);
  2410. int access = nfs4_access_to_access(open->op_share_access);
  2411. if (!fp->fi_fds[oflag]) {
  2412. status = nfsd_open(rqstp, cur_fh, S_IFREG, access,
  2413. &fp->fi_fds[oflag]);
  2414. if (status)
  2415. return status;
  2416. }
  2417. nfs4_file_get_access(fp, oflag);
  2418. return nfs_ok;
  2419. }
  2420. static inline __be32
  2421. nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
  2422. struct nfsd4_open *open)
  2423. {
  2424. struct iattr iattr = {
  2425. .ia_valid = ATTR_SIZE,
  2426. .ia_size = 0,
  2427. };
  2428. if (!open->op_truncate)
  2429. return 0;
  2430. if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
  2431. return nfserr_inval;
  2432. return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
  2433. }
  2434. static __be32
  2435. nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, struct nfsd4_open *open)
  2436. {
  2437. u32 op_share_access = open->op_share_access;
  2438. bool new_access;
  2439. __be32 status;
  2440. new_access = !test_access(op_share_access, stp);
  2441. if (new_access) {
  2442. status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
  2443. if (status)
  2444. return status;
  2445. }
  2446. status = nfsd4_truncate(rqstp, cur_fh, open);
  2447. if (status) {
  2448. if (new_access) {
  2449. int oflag = nfs4_access_to_omode(op_share_access);
  2450. nfs4_file_put_access(fp, oflag);
  2451. }
  2452. return status;
  2453. }
  2454. /* remember the open */
  2455. set_access(op_share_access, stp);
  2456. set_deny(open->op_share_deny, stp);
  2457. return nfs_ok;
  2458. }
  2459. static void
  2460. nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
  2461. {
  2462. open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
  2463. }
  2464. /* Should we give out recallable state?: */
  2465. static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
  2466. {
  2467. if (clp->cl_cb_state == NFSD4_CB_UP)
  2468. return true;
  2469. /*
  2470. * In the sessions case, since we don't have to establish a
  2471. * separate connection for callbacks, we assume it's OK
  2472. * until we hear otherwise:
  2473. */
  2474. return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
  2475. }
  2476. static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag)
  2477. {
  2478. struct file_lock *fl;
  2479. fl = locks_alloc_lock();
  2480. if (!fl)
  2481. return NULL;
  2482. locks_init_lock(fl);
  2483. fl->fl_lmops = &nfsd_lease_mng_ops;
  2484. fl->fl_flags = FL_LEASE;
  2485. fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
  2486. fl->fl_end = OFFSET_MAX;
  2487. fl->fl_owner = (fl_owner_t)(dp->dl_file);
  2488. fl->fl_pid = current->tgid;
  2489. return fl;
  2490. }
  2491. static int nfs4_setlease(struct nfs4_delegation *dp, int flag)
  2492. {
  2493. struct nfs4_file *fp = dp->dl_file;
  2494. struct file_lock *fl;
  2495. int status;
  2496. fl = nfs4_alloc_init_lease(dp, flag);
  2497. if (!fl)
  2498. return -ENOMEM;
  2499. fl->fl_file = find_readable_file(fp);
  2500. list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
  2501. status = vfs_setlease(fl->fl_file, fl->fl_type, &fl);
  2502. if (status) {
  2503. list_del_init(&dp->dl_perclnt);
  2504. locks_free_lock(fl);
  2505. return -ENOMEM;
  2506. }
  2507. fp->fi_lease = fl;
  2508. fp->fi_deleg_file = get_file(fl->fl_file);
  2509. atomic_set(&fp->fi_delegees, 1);
  2510. list_add(&dp->dl_perfile, &fp->fi_delegations);
  2511. return 0;
  2512. }
  2513. static int nfs4_set_delegation(struct nfs4_delegation *dp, int flag)
  2514. {
  2515. struct nfs4_file *fp = dp->dl_file;
  2516. if (!fp->fi_lease)
  2517. return nfs4_setlease(dp, flag);
  2518. spin_lock(&recall_lock);
  2519. if (fp->fi_had_conflict) {
  2520. spin_unlock(&recall_lock);
  2521. return -EAGAIN;
  2522. }
  2523. atomic_inc(&fp->fi_delegees);
  2524. list_add(&dp->dl_perfile, &fp->fi_delegations);
  2525. spin_unlock(&recall_lock);
  2526. list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
  2527. return 0;
  2528. }
  2529. static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
  2530. {
  2531. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
  2532. if (status == -EAGAIN)
  2533. open->op_why_no_deleg = WND4_CONTENTION;
  2534. else {
  2535. open->op_why_no_deleg = WND4_RESOURCE;
  2536. switch (open->op_deleg_want) {
  2537. case NFS4_SHARE_WANT_READ_DELEG:
  2538. case NFS4_SHARE_WANT_WRITE_DELEG:
  2539. case NFS4_SHARE_WANT_ANY_DELEG:
  2540. break;
  2541. case NFS4_SHARE_WANT_CANCEL:
  2542. open->op_why_no_deleg = WND4_CANCELLED;
  2543. break;
  2544. case NFS4_SHARE_WANT_NO_DELEG:
  2545. WARN_ON_ONCE(1);
  2546. }
  2547. }
  2548. }
  2549. /*
  2550. * Attempt to hand out a delegation.
  2551. */
  2552. static void
  2553. nfs4_open_delegation(struct net *net, struct svc_fh *fh,
  2554. struct nfsd4_open *open, struct nfs4_ol_stateid *stp)
  2555. {
  2556. struct nfs4_delegation *dp;
  2557. struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner);
  2558. int cb_up;
  2559. int status = 0, flag = 0;
  2560. cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
  2561. flag = NFS4_OPEN_DELEGATE_NONE;
  2562. open->op_recall = 0;
  2563. switch (open->op_claim_type) {
  2564. case NFS4_OPEN_CLAIM_PREVIOUS:
  2565. if (!cb_up)
  2566. open->op_recall = 1;
  2567. flag = open->op_delegate_type;
  2568. if (flag == NFS4_OPEN_DELEGATE_NONE)
  2569. goto out;
  2570. break;
  2571. case NFS4_OPEN_CLAIM_NULL:
  2572. /* Let's not give out any delegations till everyone's
  2573. * had the chance to reclaim theirs.... */
  2574. if (locks_in_grace(net))
  2575. goto out;
  2576. if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
  2577. goto out;
  2578. if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
  2579. flag = NFS4_OPEN_DELEGATE_WRITE;
  2580. else
  2581. flag = NFS4_OPEN_DELEGATE_READ;
  2582. break;
  2583. default:
  2584. goto out;
  2585. }
  2586. dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh, flag);
  2587. if (dp == NULL)
  2588. goto out_no_deleg;
  2589. status = nfs4_set_delegation(dp, flag);
  2590. if (status)
  2591. goto out_free;
  2592. memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
  2593. dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
  2594. STATEID_VAL(&dp->dl_stid.sc_stateid));
  2595. out:
  2596. open->op_delegate_type = flag;
  2597. if (flag == NFS4_OPEN_DELEGATE_NONE) {
  2598. if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
  2599. open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
  2600. dprintk("NFSD: WARNING: refusing delegation reclaim\n");
  2601. /* 4.1 client asking for a delegation? */
  2602. if (open->op_deleg_want)
  2603. nfsd4_open_deleg_none_ext(open, status);
  2604. }
  2605. return;
  2606. out_free:
  2607. unhash_stid(&dp->dl_stid);
  2608. nfs4_put_delegation(dp);
  2609. out_no_deleg:
  2610. flag = NFS4_OPEN_DELEGATE_NONE;
  2611. goto out;
  2612. }
  2613. static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
  2614. struct nfs4_delegation *dp)
  2615. {
  2616. if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
  2617. dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
  2618. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
  2619. open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
  2620. } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
  2621. dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
  2622. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
  2623. open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
  2624. }
  2625. /* Otherwise the client must be confused wanting a delegation
  2626. * it already has, therefore we don't return
  2627. * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
  2628. */
  2629. }
  2630. /*
  2631. * called with nfs4_lock_state() held.
  2632. */
  2633. __be32
  2634. nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
  2635. {
  2636. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  2637. struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
  2638. struct nfs4_file *fp = NULL;
  2639. struct inode *ino = current_fh->fh_dentry->d_inode;
  2640. struct nfs4_ol_stateid *stp = NULL;
  2641. struct nfs4_delegation *dp = NULL;
  2642. __be32 status;
  2643. /*
  2644. * Lookup file; if found, lookup stateid and check open request,
  2645. * and check for delegations in the process of being recalled.
  2646. * If not found, create the nfs4_file struct
  2647. */
  2648. fp = find_file(ino);
  2649. if (fp) {
  2650. if ((status = nfs4_check_open(fp, open, &stp)))
  2651. goto out;
  2652. status = nfs4_check_deleg(cl, fp, open, &dp);
  2653. if (status)
  2654. goto out;
  2655. } else {
  2656. status = nfserr_bad_stateid;
  2657. if (nfsd4_is_deleg_cur(open))
  2658. goto out;
  2659. status = nfserr_jukebox;
  2660. fp = open->op_file;
  2661. open->op_file = NULL;
  2662. nfsd4_init_file(fp, ino);
  2663. }
  2664. /*
  2665. * OPEN the file, or upgrade an existing OPEN.
  2666. * If truncate fails, the OPEN fails.
  2667. */
  2668. if (stp) {
  2669. /* Stateid was found, this is an OPEN upgrade */
  2670. status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
  2671. if (status)
  2672. goto out;
  2673. } else {
  2674. status = nfs4_get_vfs_file(rqstp, fp, current_fh, open);
  2675. if (status)
  2676. goto out;
  2677. status = nfsd4_truncate(rqstp, current_fh, open);
  2678. if (status)
  2679. goto out;
  2680. stp = open->op_stp;
  2681. open->op_stp = NULL;
  2682. init_open_stateid(stp, fp, open);
  2683. }
  2684. update_stateid(&stp->st_stid.sc_stateid);
  2685. memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
  2686. if (nfsd4_has_session(&resp->cstate)) {
  2687. open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
  2688. if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
  2689. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
  2690. open->op_why_no_deleg = WND4_NOT_WANTED;
  2691. goto nodeleg;
  2692. }
  2693. }
  2694. /*
  2695. * Attempt to hand out a delegation. No error return, because the
  2696. * OPEN succeeds even if we fail.
  2697. */
  2698. nfs4_open_delegation(SVC_NET(rqstp), current_fh, open, stp);
  2699. nodeleg:
  2700. status = nfs_ok;
  2701. dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
  2702. STATEID_VAL(&stp->st_stid.sc_stateid));
  2703. out:
  2704. /* 4.1 client trying to upgrade/downgrade delegation? */
  2705. if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
  2706. open->op_deleg_want)
  2707. nfsd4_deleg_xgrade_none_ext(open, dp);
  2708. if (fp)
  2709. put_nfs4_file(fp);
  2710. if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
  2711. nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
  2712. /*
  2713. * To finish the open response, we just need to set the rflags.
  2714. */
  2715. open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
  2716. if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
  2717. !nfsd4_has_session(&resp->cstate))
  2718. open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
  2719. return status;
  2720. }
  2721. void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
  2722. {
  2723. if (open->op_openowner) {
  2724. struct nfs4_openowner *oo = open->op_openowner;
  2725. if (!list_empty(&oo->oo_owner.so_stateids))
  2726. list_del_init(&oo->oo_close_lru);
  2727. if (oo->oo_flags & NFS4_OO_NEW) {
  2728. if (status) {
  2729. release_openowner(oo);
  2730. open->op_openowner = NULL;
  2731. } else
  2732. oo->oo_flags &= ~NFS4_OO_NEW;
  2733. }
  2734. }
  2735. if (open->op_file)
  2736. nfsd4_free_file(open->op_file);
  2737. if (open->op_stp)
  2738. free_generic_stateid(open->op_stp);
  2739. }
  2740. static __be32 lookup_clientid(clientid_t *clid, bool session, struct nfsd_net *nn, struct nfs4_client **clp)
  2741. {
  2742. struct nfs4_client *found;
  2743. if (STALE_CLIENTID(clid, nn))
  2744. return nfserr_stale_clientid;
  2745. found = find_confirmed_client(clid, session, nn);
  2746. if (clp)
  2747. *clp = found;
  2748. return found ? nfs_ok : nfserr_expired;
  2749. }
  2750. __be32
  2751. nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  2752. clientid_t *clid)
  2753. {
  2754. struct nfs4_client *clp;
  2755. __be32 status;
  2756. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  2757. nfs4_lock_state();
  2758. dprintk("process_renew(%08x/%08x): starting\n",
  2759. clid->cl_boot, clid->cl_id);
  2760. status = lookup_clientid(clid, cstate->minorversion, nn, &clp);
  2761. if (status)
  2762. goto out;
  2763. status = nfserr_cb_path_down;
  2764. if (!list_empty(&clp->cl_delegations)
  2765. && clp->cl_cb_state != NFSD4_CB_UP)
  2766. goto out;
  2767. status = nfs_ok;
  2768. out:
  2769. nfs4_unlock_state();
  2770. return status;
  2771. }
  2772. static void
  2773. nfsd4_end_grace(struct nfsd_net *nn)
  2774. {
  2775. /* do nothing if grace period already ended */
  2776. if (nn->grace_ended)
  2777. return;
  2778. dprintk("NFSD: end of grace period\n");
  2779. nn->grace_ended = true;
  2780. nfsd4_record_grace_done(nn, nn->boot_time);
  2781. locks_end_grace(&nn->nfsd4_manager);
  2782. /*
  2783. * Now that every NFSv4 client has had the chance to recover and
  2784. * to see the (possibly new, possibly shorter) lease time, we
  2785. * can safely set the next grace time to the current lease time:
  2786. */
  2787. nn->nfsd4_grace = nn->nfsd4_lease;
  2788. }
  2789. static time_t
  2790. nfs4_laundromat(struct nfsd_net *nn)
  2791. {
  2792. struct nfs4_client *clp;
  2793. struct nfs4_openowner *oo;
  2794. struct nfs4_delegation *dp;
  2795. struct list_head *pos, *next, reaplist;
  2796. time_t cutoff = get_seconds() - nn->nfsd4_lease;
  2797. time_t t, clientid_val = nn->nfsd4_lease;
  2798. time_t u, test_val = nn->nfsd4_lease;
  2799. nfs4_lock_state();
  2800. dprintk("NFSD: laundromat service - starting\n");
  2801. nfsd4_end_grace(nn);
  2802. INIT_LIST_HEAD(&reaplist);
  2803. spin_lock(&nn->client_lock);
  2804. list_for_each_safe(pos, next, &nn->client_lru) {
  2805. clp = list_entry(pos, struct nfs4_client, cl_lru);
  2806. if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
  2807. t = clp->cl_time - cutoff;
  2808. if (clientid_val > t)
  2809. clientid_val = t;
  2810. break;
  2811. }
  2812. if (atomic_read(&clp->cl_refcount)) {
  2813. dprintk("NFSD: client in use (clientid %08x)\n",
  2814. clp->cl_clientid.cl_id);
  2815. continue;
  2816. }
  2817. unhash_client_locked(clp);
  2818. list_add(&clp->cl_lru, &reaplist);
  2819. }
  2820. spin_unlock(&nn->client_lock);
  2821. list_for_each_safe(pos, next, &reaplist) {
  2822. clp = list_entry(pos, struct nfs4_client, cl_lru);
  2823. dprintk("NFSD: purging unused client (clientid %08x)\n",
  2824. clp->cl_clientid.cl_id);
  2825. expire_client(clp);
  2826. }
  2827. spin_lock(&recall_lock);
  2828. list_for_each_safe(pos, next, &del_recall_lru) {
  2829. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  2830. if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
  2831. continue;
  2832. if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
  2833. u = dp->dl_time - cutoff;
  2834. if (test_val > u)
  2835. test_val = u;
  2836. break;
  2837. }
  2838. list_move(&dp->dl_recall_lru, &reaplist);
  2839. }
  2840. spin_unlock(&recall_lock);
  2841. list_for_each_safe(pos, next, &reaplist) {
  2842. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  2843. unhash_delegation(dp);
  2844. }
  2845. test_val = nn->nfsd4_lease;
  2846. list_for_each_safe(pos, next, &nn->close_lru) {
  2847. oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
  2848. if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
  2849. u = oo->oo_time - cutoff;
  2850. if (test_val > u)
  2851. test_val = u;
  2852. break;
  2853. }
  2854. release_openowner(oo);
  2855. }
  2856. if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
  2857. clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
  2858. nfs4_unlock_state();
  2859. return clientid_val;
  2860. }
  2861. static struct workqueue_struct *laundry_wq;
  2862. static void laundromat_main(struct work_struct *);
  2863. static void
  2864. laundromat_main(struct work_struct *laundry)
  2865. {
  2866. time_t t;
  2867. struct delayed_work *dwork = container_of(laundry, struct delayed_work,
  2868. work);
  2869. struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
  2870. laundromat_work);
  2871. t = nfs4_laundromat(nn);
  2872. dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
  2873. queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
  2874. }
  2875. static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
  2876. {
  2877. if (fhp->fh_dentry->d_inode != stp->st_file->fi_inode)
  2878. return nfserr_bad_stateid;
  2879. return nfs_ok;
  2880. }
  2881. static int
  2882. STALE_STATEID(stateid_t *stateid, struct nfsd_net *nn)
  2883. {
  2884. if (stateid->si_opaque.so_clid.cl_boot == nn->boot_time)
  2885. return 0;
  2886. dprintk("NFSD: stale stateid " STATEID_FMT "!\n",
  2887. STATEID_VAL(stateid));
  2888. return 1;
  2889. }
  2890. static inline int
  2891. access_permit_read(struct nfs4_ol_stateid *stp)
  2892. {
  2893. return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
  2894. test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
  2895. test_access(NFS4_SHARE_ACCESS_WRITE, stp);
  2896. }
  2897. static inline int
  2898. access_permit_write(struct nfs4_ol_stateid *stp)
  2899. {
  2900. return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
  2901. test_access(NFS4_SHARE_ACCESS_BOTH, stp);
  2902. }
  2903. static
  2904. __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
  2905. {
  2906. __be32 status = nfserr_openmode;
  2907. /* For lock stateid's, we test the parent open, not the lock: */
  2908. if (stp->st_openstp)
  2909. stp = stp->st_openstp;
  2910. if ((flags & WR_STATE) && !access_permit_write(stp))
  2911. goto out;
  2912. if ((flags & RD_STATE) && !access_permit_read(stp))
  2913. goto out;
  2914. status = nfs_ok;
  2915. out:
  2916. return status;
  2917. }
  2918. static inline __be32
  2919. check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
  2920. {
  2921. if (ONE_STATEID(stateid) && (flags & RD_STATE))
  2922. return nfs_ok;
  2923. else if (locks_in_grace(net)) {
  2924. /* Answer in remaining cases depends on existence of
  2925. * conflicting state; so we must wait out the grace period. */
  2926. return nfserr_grace;
  2927. } else if (flags & WR_STATE)
  2928. return nfs4_share_conflict(current_fh,
  2929. NFS4_SHARE_DENY_WRITE);
  2930. else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
  2931. return nfs4_share_conflict(current_fh,
  2932. NFS4_SHARE_DENY_READ);
  2933. }
  2934. /*
  2935. * Allow READ/WRITE during grace period on recovered state only for files
  2936. * that are not able to provide mandatory locking.
  2937. */
  2938. static inline int
  2939. grace_disallows_io(struct net *net, struct inode *inode)
  2940. {
  2941. return locks_in_grace(net) && mandatory_lock(inode);
  2942. }
  2943. /* Returns true iff a is later than b: */
  2944. static bool stateid_generation_after(stateid_t *a, stateid_t *b)
  2945. {
  2946. return (s32)a->si_generation - (s32)b->si_generation > 0;
  2947. }
  2948. static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
  2949. {
  2950. /*
  2951. * When sessions are used the stateid generation number is ignored
  2952. * when it is zero.
  2953. */
  2954. if (has_session && in->si_generation == 0)
  2955. return nfs_ok;
  2956. if (in->si_generation == ref->si_generation)
  2957. return nfs_ok;
  2958. /* If the client sends us a stateid from the future, it's buggy: */
  2959. if (stateid_generation_after(in, ref))
  2960. return nfserr_bad_stateid;
  2961. /*
  2962. * However, we could see a stateid from the past, even from a
  2963. * non-buggy client. For example, if the client sends a lock
  2964. * while some IO is outstanding, the lock may bump si_generation
  2965. * while the IO is still in flight. The client could avoid that
  2966. * situation by waiting for responses on all the IO requests,
  2967. * but better performance may result in retrying IO that
  2968. * receives an old_stateid error if requests are rarely
  2969. * reordered in flight:
  2970. */
  2971. return nfserr_old_stateid;
  2972. }
  2973. static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
  2974. {
  2975. struct nfs4_stid *s;
  2976. struct nfs4_ol_stateid *ols;
  2977. __be32 status;
  2978. if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
  2979. return nfserr_bad_stateid;
  2980. /* Client debugging aid. */
  2981. if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
  2982. char addr_str[INET6_ADDRSTRLEN];
  2983. rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
  2984. sizeof(addr_str));
  2985. pr_warn_ratelimited("NFSD: client %s testing state ID "
  2986. "with incorrect client ID\n", addr_str);
  2987. return nfserr_bad_stateid;
  2988. }
  2989. s = find_stateid(cl, stateid);
  2990. if (!s)
  2991. return nfserr_bad_stateid;
  2992. status = check_stateid_generation(stateid, &s->sc_stateid, 1);
  2993. if (status)
  2994. return status;
  2995. if (!(s->sc_type & (NFS4_OPEN_STID | NFS4_LOCK_STID)))
  2996. return nfs_ok;
  2997. ols = openlockstateid(s);
  2998. if (ols->st_stateowner->so_is_open_owner
  2999. && !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
  3000. return nfserr_bad_stateid;
  3001. return nfs_ok;
  3002. }
  3003. static __be32 nfsd4_lookup_stateid(stateid_t *stateid, unsigned char typemask,
  3004. struct nfs4_stid **s, bool sessions,
  3005. struct nfsd_net *nn)
  3006. {
  3007. struct nfs4_client *cl;
  3008. if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
  3009. return nfserr_bad_stateid;
  3010. if (STALE_STATEID(stateid, nn))
  3011. return nfserr_stale_stateid;
  3012. cl = find_confirmed_client(&stateid->si_opaque.so_clid, sessions, nn);
  3013. if (!cl)
  3014. return nfserr_expired;
  3015. *s = find_stateid_by_type(cl, stateid, typemask);
  3016. if (!*s)
  3017. return nfserr_bad_stateid;
  3018. return nfs_ok;
  3019. }
  3020. /*
  3021. * Checks for stateid operations
  3022. */
  3023. __be32
  3024. nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
  3025. stateid_t *stateid, int flags, struct file **filpp)
  3026. {
  3027. struct nfs4_stid *s;
  3028. struct nfs4_ol_stateid *stp = NULL;
  3029. struct nfs4_delegation *dp = NULL;
  3030. struct svc_fh *current_fh = &cstate->current_fh;
  3031. struct inode *ino = current_fh->fh_dentry->d_inode;
  3032. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  3033. __be32 status;
  3034. if (filpp)
  3035. *filpp = NULL;
  3036. if (grace_disallows_io(net, ino))
  3037. return nfserr_grace;
  3038. if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
  3039. return check_special_stateids(net, current_fh, stateid, flags);
  3040. status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
  3041. &s, cstate->minorversion, nn);
  3042. if (status)
  3043. return status;
  3044. status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
  3045. if (status)
  3046. goto out;
  3047. switch (s->sc_type) {
  3048. case NFS4_DELEG_STID:
  3049. dp = delegstateid(s);
  3050. status = nfs4_check_delegmode(dp, flags);
  3051. if (status)
  3052. goto out;
  3053. if (filpp) {
  3054. *filpp = dp->dl_file->fi_deleg_file;
  3055. if (!*filpp) {
  3056. WARN_ON_ONCE(1);
  3057. status = nfserr_serverfault;
  3058. goto out;
  3059. }
  3060. }
  3061. break;
  3062. case NFS4_OPEN_STID:
  3063. case NFS4_LOCK_STID:
  3064. stp = openlockstateid(s);
  3065. status = nfs4_check_fh(current_fh, stp);
  3066. if (status)
  3067. goto out;
  3068. if (stp->st_stateowner->so_is_open_owner
  3069. && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
  3070. goto out;
  3071. status = nfs4_check_openmode(stp, flags);
  3072. if (status)
  3073. goto out;
  3074. if (filpp) {
  3075. if (flags & RD_STATE)
  3076. *filpp = find_readable_file(stp->st_file);
  3077. else
  3078. *filpp = find_writeable_file(stp->st_file);
  3079. }
  3080. break;
  3081. default:
  3082. return nfserr_bad_stateid;
  3083. }
  3084. status = nfs_ok;
  3085. out:
  3086. return status;
  3087. }
  3088. static __be32
  3089. nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
  3090. {
  3091. if (check_for_locks(stp->st_file, lockowner(stp->st_stateowner)))
  3092. return nfserr_locks_held;
  3093. release_lock_stateid(stp);
  3094. return nfs_ok;
  3095. }
  3096. /*
  3097. * Test if the stateid is valid
  3098. */
  3099. __be32
  3100. nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3101. struct nfsd4_test_stateid *test_stateid)
  3102. {
  3103. struct nfsd4_test_stateid_id *stateid;
  3104. struct nfs4_client *cl = cstate->session->se_client;
  3105. nfs4_lock_state();
  3106. list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
  3107. stateid->ts_id_status =
  3108. nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
  3109. nfs4_unlock_state();
  3110. return nfs_ok;
  3111. }
  3112. __be32
  3113. nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3114. struct nfsd4_free_stateid *free_stateid)
  3115. {
  3116. stateid_t *stateid = &free_stateid->fr_stateid;
  3117. struct nfs4_stid *s;
  3118. struct nfs4_client *cl = cstate->session->se_client;
  3119. __be32 ret = nfserr_bad_stateid;
  3120. nfs4_lock_state();
  3121. s = find_stateid(cl, stateid);
  3122. if (!s)
  3123. goto out;
  3124. switch (s->sc_type) {
  3125. case NFS4_DELEG_STID:
  3126. ret = nfserr_locks_held;
  3127. goto out;
  3128. case NFS4_OPEN_STID:
  3129. case NFS4_LOCK_STID:
  3130. ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
  3131. if (ret)
  3132. goto out;
  3133. if (s->sc_type == NFS4_LOCK_STID)
  3134. ret = nfsd4_free_lock_stateid(openlockstateid(s));
  3135. else
  3136. ret = nfserr_locks_held;
  3137. break;
  3138. default:
  3139. ret = nfserr_bad_stateid;
  3140. }
  3141. out:
  3142. nfs4_unlock_state();
  3143. return ret;
  3144. }
  3145. static inline int
  3146. setlkflg (int type)
  3147. {
  3148. return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
  3149. RD_STATE : WR_STATE;
  3150. }
  3151. static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
  3152. {
  3153. struct svc_fh *current_fh = &cstate->current_fh;
  3154. struct nfs4_stateowner *sop = stp->st_stateowner;
  3155. __be32 status;
  3156. status = nfsd4_check_seqid(cstate, sop, seqid);
  3157. if (status)
  3158. return status;
  3159. if (stp->st_stid.sc_type == NFS4_CLOSED_STID)
  3160. /*
  3161. * "Closed" stateid's exist *only* to return
  3162. * nfserr_replay_me from the previous step.
  3163. */
  3164. return nfserr_bad_stateid;
  3165. status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
  3166. if (status)
  3167. return status;
  3168. return nfs4_check_fh(current_fh, stp);
  3169. }
  3170. /*
  3171. * Checks for sequence id mutating operations.
  3172. */
  3173. static __be32
  3174. nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
  3175. stateid_t *stateid, char typemask,
  3176. struct nfs4_ol_stateid **stpp,
  3177. struct nfsd_net *nn)
  3178. {
  3179. __be32 status;
  3180. struct nfs4_stid *s;
  3181. dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
  3182. seqid, STATEID_VAL(stateid));
  3183. *stpp = NULL;
  3184. status = nfsd4_lookup_stateid(stateid, typemask, &s,
  3185. cstate->minorversion, nn);
  3186. if (status)
  3187. return status;
  3188. *stpp = openlockstateid(s);
  3189. cstate->replay_owner = (*stpp)->st_stateowner;
  3190. return nfs4_seqid_op_checks(cstate, stateid, seqid, *stpp);
  3191. }
  3192. static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
  3193. stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
  3194. {
  3195. __be32 status;
  3196. struct nfs4_openowner *oo;
  3197. status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
  3198. NFS4_OPEN_STID, stpp, nn);
  3199. if (status)
  3200. return status;
  3201. oo = openowner((*stpp)->st_stateowner);
  3202. if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
  3203. return nfserr_bad_stateid;
  3204. return nfs_ok;
  3205. }
  3206. __be32
  3207. nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3208. struct nfsd4_open_confirm *oc)
  3209. {
  3210. __be32 status;
  3211. struct nfs4_openowner *oo;
  3212. struct nfs4_ol_stateid *stp;
  3213. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  3214. dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
  3215. (int)cstate->current_fh.fh_dentry->d_name.len,
  3216. cstate->current_fh.fh_dentry->d_name.name);
  3217. status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
  3218. if (status)
  3219. return status;
  3220. nfs4_lock_state();
  3221. status = nfs4_preprocess_seqid_op(cstate,
  3222. oc->oc_seqid, &oc->oc_req_stateid,
  3223. NFS4_OPEN_STID, &stp, nn);
  3224. if (status)
  3225. goto out;
  3226. oo = openowner(stp->st_stateowner);
  3227. status = nfserr_bad_stateid;
  3228. if (oo->oo_flags & NFS4_OO_CONFIRMED)
  3229. goto out;
  3230. oo->oo_flags |= NFS4_OO_CONFIRMED;
  3231. update_stateid(&stp->st_stid.sc_stateid);
  3232. memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
  3233. dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
  3234. __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
  3235. nfsd4_client_record_create(oo->oo_owner.so_client);
  3236. status = nfs_ok;
  3237. out:
  3238. if (!cstate->replay_owner)
  3239. nfs4_unlock_state();
  3240. return status;
  3241. }
  3242. static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
  3243. {
  3244. if (!test_access(access, stp))
  3245. return;
  3246. nfs4_file_put_access(stp->st_file, nfs4_access_to_omode(access));
  3247. clear_access(access, stp);
  3248. }
  3249. static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
  3250. {
  3251. switch (to_access) {
  3252. case NFS4_SHARE_ACCESS_READ:
  3253. nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
  3254. nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
  3255. break;
  3256. case NFS4_SHARE_ACCESS_WRITE:
  3257. nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
  3258. nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
  3259. break;
  3260. case NFS4_SHARE_ACCESS_BOTH:
  3261. break;
  3262. default:
  3263. WARN_ON_ONCE(1);
  3264. }
  3265. }
  3266. static void
  3267. reset_union_bmap_deny(unsigned long deny, struct nfs4_ol_stateid *stp)
  3268. {
  3269. int i;
  3270. for (i = 0; i < 4; i++) {
  3271. if ((i & deny) != i)
  3272. clear_deny(i, stp);
  3273. }
  3274. }
  3275. __be32
  3276. nfsd4_open_downgrade(struct svc_rqst *rqstp,
  3277. struct nfsd4_compound_state *cstate,
  3278. struct nfsd4_open_downgrade *od)
  3279. {
  3280. __be32 status;
  3281. struct nfs4_ol_stateid *stp;
  3282. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  3283. dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
  3284. (int)cstate->current_fh.fh_dentry->d_name.len,
  3285. cstate->current_fh.fh_dentry->d_name.name);
  3286. /* We don't yet support WANT bits: */
  3287. if (od->od_deleg_want)
  3288. dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
  3289. od->od_deleg_want);
  3290. nfs4_lock_state();
  3291. status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
  3292. &od->od_stateid, &stp, nn);
  3293. if (status)
  3294. goto out;
  3295. status = nfserr_inval;
  3296. if (!test_access(od->od_share_access, stp)) {
  3297. dprintk("NFSD: access not a subset current bitmap: 0x%lx, input access=%08x\n",
  3298. stp->st_access_bmap, od->od_share_access);
  3299. goto out;
  3300. }
  3301. if (!test_deny(od->od_share_deny, stp)) {
  3302. dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
  3303. stp->st_deny_bmap, od->od_share_deny);
  3304. goto out;
  3305. }
  3306. nfs4_stateid_downgrade(stp, od->od_share_access);
  3307. reset_union_bmap_deny(od->od_share_deny, stp);
  3308. update_stateid(&stp->st_stid.sc_stateid);
  3309. memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
  3310. status = nfs_ok;
  3311. out:
  3312. if (!cstate->replay_owner)
  3313. nfs4_unlock_state();
  3314. return status;
  3315. }
  3316. void nfsd4_purge_closed_stateid(struct nfs4_stateowner *so)
  3317. {
  3318. struct nfs4_openowner *oo;
  3319. struct nfs4_ol_stateid *s;
  3320. if (!so->so_is_open_owner)
  3321. return;
  3322. oo = openowner(so);
  3323. s = oo->oo_last_closed_stid;
  3324. if (!s)
  3325. return;
  3326. if (!(oo->oo_flags & NFS4_OO_PURGE_CLOSE)) {
  3327. /* Release the last_closed_stid on the next seqid bump: */
  3328. oo->oo_flags |= NFS4_OO_PURGE_CLOSE;
  3329. return;
  3330. }
  3331. oo->oo_flags &= ~NFS4_OO_PURGE_CLOSE;
  3332. release_last_closed_stateid(oo);
  3333. }
  3334. static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
  3335. {
  3336. unhash_open_stateid(s);
  3337. s->st_stid.sc_type = NFS4_CLOSED_STID;
  3338. }
  3339. /*
  3340. * nfs4_unlock_state() called after encode
  3341. */
  3342. __be32
  3343. nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3344. struct nfsd4_close *close)
  3345. {
  3346. __be32 status;
  3347. struct nfs4_openowner *oo;
  3348. struct nfs4_ol_stateid *stp;
  3349. struct net *net = SVC_NET(rqstp);
  3350. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  3351. dprintk("NFSD: nfsd4_close on file %.*s\n",
  3352. (int)cstate->current_fh.fh_dentry->d_name.len,
  3353. cstate->current_fh.fh_dentry->d_name.name);
  3354. nfs4_lock_state();
  3355. status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
  3356. &close->cl_stateid,
  3357. NFS4_OPEN_STID|NFS4_CLOSED_STID,
  3358. &stp, nn);
  3359. if (status)
  3360. goto out;
  3361. oo = openowner(stp->st_stateowner);
  3362. status = nfs_ok;
  3363. update_stateid(&stp->st_stid.sc_stateid);
  3364. memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
  3365. nfsd4_close_open_stateid(stp);
  3366. release_last_closed_stateid(oo);
  3367. oo->oo_last_closed_stid = stp;
  3368. if (list_empty(&oo->oo_owner.so_stateids)) {
  3369. if (cstate->minorversion) {
  3370. release_openowner(oo);
  3371. cstate->replay_owner = NULL;
  3372. } else {
  3373. /*
  3374. * In the 4.0 case we need to keep the owners around a
  3375. * little while to handle CLOSE replay.
  3376. */
  3377. if (list_empty(&oo->oo_owner.so_stateids))
  3378. move_to_close_lru(oo, SVC_NET(rqstp));
  3379. }
  3380. }
  3381. out:
  3382. if (!cstate->replay_owner)
  3383. nfs4_unlock_state();
  3384. return status;
  3385. }
  3386. __be32
  3387. nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3388. struct nfsd4_delegreturn *dr)
  3389. {
  3390. struct nfs4_delegation *dp;
  3391. stateid_t *stateid = &dr->dr_stateid;
  3392. struct nfs4_stid *s;
  3393. __be32 status;
  3394. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  3395. if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
  3396. return status;
  3397. nfs4_lock_state();
  3398. status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID, &s,
  3399. cstate->minorversion, nn);
  3400. if (status)
  3401. goto out;
  3402. dp = delegstateid(s);
  3403. status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
  3404. if (status)
  3405. goto out;
  3406. unhash_delegation(dp);
  3407. out:
  3408. nfs4_unlock_state();
  3409. return status;
  3410. }
  3411. #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
  3412. #define LOCKOWNER_INO_HASH_MASK (LOCKOWNER_INO_HASH_SIZE - 1)
  3413. static inline u64
  3414. end_offset(u64 start, u64 len)
  3415. {
  3416. u64 end;
  3417. end = start + len;
  3418. return end >= start ? end: NFS4_MAX_UINT64;
  3419. }
  3420. /* last octet in a range */
  3421. static inline u64
  3422. last_byte_offset(u64 start, u64 len)
  3423. {
  3424. u64 end;
  3425. WARN_ON_ONCE(!len);
  3426. end = start + len;
  3427. return end > start ? end - 1: NFS4_MAX_UINT64;
  3428. }
  3429. static unsigned int lockowner_ino_hashval(struct inode *inode, u32 cl_id, struct xdr_netobj *ownername)
  3430. {
  3431. return (file_hashval(inode) + cl_id
  3432. + opaque_hashval(ownername->data, ownername->len))
  3433. & LOCKOWNER_INO_HASH_MASK;
  3434. }
  3435. /*
  3436. * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
  3437. * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
  3438. * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
  3439. * locking, this prevents us from being completely protocol-compliant. The
  3440. * real solution to this problem is to start using unsigned file offsets in
  3441. * the VFS, but this is a very deep change!
  3442. */
  3443. static inline void
  3444. nfs4_transform_lock_offset(struct file_lock *lock)
  3445. {
  3446. if (lock->fl_start < 0)
  3447. lock->fl_start = OFFSET_MAX;
  3448. if (lock->fl_end < 0)
  3449. lock->fl_end = OFFSET_MAX;
  3450. }
  3451. /* Hack!: For now, we're defining this just so we can use a pointer to it
  3452. * as a unique cookie to identify our (NFSv4's) posix locks. */
  3453. static const struct lock_manager_operations nfsd_posix_mng_ops = {
  3454. };
  3455. static inline void
  3456. nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
  3457. {
  3458. struct nfs4_lockowner *lo;
  3459. if (fl->fl_lmops == &nfsd_posix_mng_ops) {
  3460. lo = (struct nfs4_lockowner *) fl->fl_owner;
  3461. deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
  3462. lo->lo_owner.so_owner.len, GFP_KERNEL);
  3463. if (!deny->ld_owner.data)
  3464. /* We just don't care that much */
  3465. goto nevermind;
  3466. deny->ld_owner.len = lo->lo_owner.so_owner.len;
  3467. deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
  3468. } else {
  3469. nevermind:
  3470. deny->ld_owner.len = 0;
  3471. deny->ld_owner.data = NULL;
  3472. deny->ld_clientid.cl_boot = 0;
  3473. deny->ld_clientid.cl_id = 0;
  3474. }
  3475. deny->ld_start = fl->fl_start;
  3476. deny->ld_length = NFS4_MAX_UINT64;
  3477. if (fl->fl_end != NFS4_MAX_UINT64)
  3478. deny->ld_length = fl->fl_end - fl->fl_start + 1;
  3479. deny->ld_type = NFS4_READ_LT;
  3480. if (fl->fl_type != F_RDLCK)
  3481. deny->ld_type = NFS4_WRITE_LT;
  3482. }
  3483. static bool same_lockowner_ino(struct nfs4_lockowner *lo, struct inode *inode, clientid_t *clid, struct xdr_netobj *owner)
  3484. {
  3485. struct nfs4_ol_stateid *lst;
  3486. if (!same_owner_str(&lo->lo_owner, owner, clid))
  3487. return false;
  3488. lst = list_first_entry(&lo->lo_owner.so_stateids,
  3489. struct nfs4_ol_stateid, st_perstateowner);
  3490. return lst->st_file->fi_inode == inode;
  3491. }
  3492. static struct nfs4_lockowner *
  3493. find_lockowner_str(struct inode *inode, clientid_t *clid,
  3494. struct xdr_netobj *owner, struct nfsd_net *nn)
  3495. {
  3496. unsigned int hashval = lockowner_ino_hashval(inode, clid->cl_id, owner);
  3497. struct nfs4_lockowner *lo;
  3498. list_for_each_entry(lo, &nn->lockowner_ino_hashtbl[hashval], lo_owner_ino_hash) {
  3499. if (same_lockowner_ino(lo, inode, clid, owner))
  3500. return lo;
  3501. }
  3502. return NULL;
  3503. }
  3504. static void hash_lockowner(struct nfs4_lockowner *lo, unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp)
  3505. {
  3506. struct inode *inode = open_stp->st_file->fi_inode;
  3507. unsigned int inohash = lockowner_ino_hashval(inode,
  3508. clp->cl_clientid.cl_id, &lo->lo_owner.so_owner);
  3509. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  3510. list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
  3511. list_add(&lo->lo_owner_ino_hash, &nn->lockowner_ino_hashtbl[inohash]);
  3512. list_add(&lo->lo_perstateid, &open_stp->st_lockowners);
  3513. }
  3514. /*
  3515. * Alloc a lock owner structure.
  3516. * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
  3517. * occurred.
  3518. *
  3519. * strhashval = ownerstr_hashval
  3520. */
  3521. static struct nfs4_lockowner *
  3522. alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp, struct nfsd4_lock *lock) {
  3523. struct nfs4_lockowner *lo;
  3524. lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
  3525. if (!lo)
  3526. return NULL;
  3527. INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
  3528. lo->lo_owner.so_is_open_owner = 0;
  3529. /* It is the openowner seqid that will be incremented in encode in the
  3530. * case of new lockowners; so increment the lock seqid manually: */
  3531. lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
  3532. hash_lockowner(lo, strhashval, clp, open_stp);
  3533. return lo;
  3534. }
  3535. static struct nfs4_ol_stateid *
  3536. alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct nfs4_ol_stateid *open_stp)
  3537. {
  3538. struct nfs4_ol_stateid *stp;
  3539. struct nfs4_client *clp = lo->lo_owner.so_client;
  3540. stp = nfs4_alloc_stateid(clp);
  3541. if (stp == NULL)
  3542. return NULL;
  3543. init_stid(&stp->st_stid, clp, NFS4_LOCK_STID);
  3544. list_add(&stp->st_perfile, &fp->fi_stateids);
  3545. list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
  3546. stp->st_stateowner = &lo->lo_owner;
  3547. get_nfs4_file(fp);
  3548. stp->st_file = fp;
  3549. stp->st_access_bmap = 0;
  3550. stp->st_deny_bmap = open_stp->st_deny_bmap;
  3551. stp->st_openstp = open_stp;
  3552. return stp;
  3553. }
  3554. static int
  3555. check_lock_length(u64 offset, u64 length)
  3556. {
  3557. return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
  3558. LOFF_OVERFLOW(offset, length)));
  3559. }
  3560. static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
  3561. {
  3562. struct nfs4_file *fp = lock_stp->st_file;
  3563. int oflag = nfs4_access_to_omode(access);
  3564. if (test_access(access, lock_stp))
  3565. return;
  3566. nfs4_file_get_access(fp, oflag);
  3567. set_access(access, lock_stp);
  3568. }
  3569. static __be32 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate, struct nfs4_ol_stateid *ost, struct nfsd4_lock *lock, struct nfs4_ol_stateid **lst, bool *new)
  3570. {
  3571. struct nfs4_file *fi = ost->st_file;
  3572. struct nfs4_openowner *oo = openowner(ost->st_stateowner);
  3573. struct nfs4_client *cl = oo->oo_owner.so_client;
  3574. struct nfs4_lockowner *lo;
  3575. unsigned int strhashval;
  3576. struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id);
  3577. lo = find_lockowner_str(fi->fi_inode, &cl->cl_clientid,
  3578. &lock->v.new.owner, nn);
  3579. if (lo) {
  3580. if (!cstate->minorversion)
  3581. return nfserr_bad_seqid;
  3582. /* XXX: a lockowner always has exactly one stateid: */
  3583. *lst = list_first_entry(&lo->lo_owner.so_stateids,
  3584. struct nfs4_ol_stateid, st_perstateowner);
  3585. return nfs_ok;
  3586. }
  3587. strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
  3588. &lock->v.new.owner);
  3589. lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
  3590. if (lo == NULL)
  3591. return nfserr_jukebox;
  3592. *lst = alloc_init_lock_stateid(lo, fi, ost);
  3593. if (*lst == NULL) {
  3594. release_lockowner(lo);
  3595. return nfserr_jukebox;
  3596. }
  3597. *new = true;
  3598. return nfs_ok;
  3599. }
  3600. /*
  3601. * LOCK operation
  3602. */
  3603. __be32
  3604. nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3605. struct nfsd4_lock *lock)
  3606. {
  3607. struct nfs4_openowner *open_sop = NULL;
  3608. struct nfs4_lockowner *lock_sop = NULL;
  3609. struct nfs4_ol_stateid *lock_stp;
  3610. struct file *filp = NULL;
  3611. struct file_lock *file_lock = NULL;
  3612. struct file_lock *conflock = NULL;
  3613. __be32 status = 0;
  3614. bool new_state = false;
  3615. int lkflg;
  3616. int err;
  3617. struct net *net = SVC_NET(rqstp);
  3618. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  3619. dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
  3620. (long long) lock->lk_offset,
  3621. (long long) lock->lk_length);
  3622. if (check_lock_length(lock->lk_offset, lock->lk_length))
  3623. return nfserr_inval;
  3624. if ((status = fh_verify(rqstp, &cstate->current_fh,
  3625. S_IFREG, NFSD_MAY_LOCK))) {
  3626. dprintk("NFSD: nfsd4_lock: permission denied!\n");
  3627. return status;
  3628. }
  3629. nfs4_lock_state();
  3630. if (lock->lk_is_new) {
  3631. struct nfs4_ol_stateid *open_stp = NULL;
  3632. if (nfsd4_has_session(cstate))
  3633. /* See rfc 5661 18.10.3: given clientid is ignored: */
  3634. memcpy(&lock->v.new.clientid,
  3635. &cstate->session->se_client->cl_clientid,
  3636. sizeof(clientid_t));
  3637. status = nfserr_stale_clientid;
  3638. if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
  3639. goto out;
  3640. /* validate and update open stateid and open seqid */
  3641. status = nfs4_preprocess_confirmed_seqid_op(cstate,
  3642. lock->lk_new_open_seqid,
  3643. &lock->lk_new_open_stateid,
  3644. &open_stp, nn);
  3645. if (status)
  3646. goto out;
  3647. open_sop = openowner(open_stp->st_stateowner);
  3648. status = nfserr_bad_stateid;
  3649. if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
  3650. &lock->v.new.clientid))
  3651. goto out;
  3652. status = lookup_or_create_lock_state(cstate, open_stp, lock,
  3653. &lock_stp, &new_state);
  3654. } else
  3655. status = nfs4_preprocess_seqid_op(cstate,
  3656. lock->lk_old_lock_seqid,
  3657. &lock->lk_old_lock_stateid,
  3658. NFS4_LOCK_STID, &lock_stp, nn);
  3659. if (status)
  3660. goto out;
  3661. lock_sop = lockowner(lock_stp->st_stateowner);
  3662. lkflg = setlkflg(lock->lk_type);
  3663. status = nfs4_check_openmode(lock_stp, lkflg);
  3664. if (status)
  3665. goto out;
  3666. status = nfserr_grace;
  3667. if (locks_in_grace(net) && !lock->lk_reclaim)
  3668. goto out;
  3669. status = nfserr_no_grace;
  3670. if (!locks_in_grace(net) && lock->lk_reclaim)
  3671. goto out;
  3672. file_lock = locks_alloc_lock();
  3673. if (!file_lock) {
  3674. dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
  3675. status = nfserr_jukebox;
  3676. goto out;
  3677. }
  3678. locks_init_lock(file_lock);
  3679. switch (lock->lk_type) {
  3680. case NFS4_READ_LT:
  3681. case NFS4_READW_LT:
  3682. filp = find_readable_file(lock_stp->st_file);
  3683. if (filp)
  3684. get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
  3685. file_lock->fl_type = F_RDLCK;
  3686. break;
  3687. case NFS4_WRITE_LT:
  3688. case NFS4_WRITEW_LT:
  3689. filp = find_writeable_file(lock_stp->st_file);
  3690. if (filp)
  3691. get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
  3692. file_lock->fl_type = F_WRLCK;
  3693. break;
  3694. default:
  3695. status = nfserr_inval;
  3696. goto out;
  3697. }
  3698. if (!filp) {
  3699. status = nfserr_openmode;
  3700. goto out;
  3701. }
  3702. file_lock->fl_owner = (fl_owner_t)lock_sop;
  3703. file_lock->fl_pid = current->tgid;
  3704. file_lock->fl_file = filp;
  3705. file_lock->fl_flags = FL_POSIX;
  3706. file_lock->fl_lmops = &nfsd_posix_mng_ops;
  3707. file_lock->fl_start = lock->lk_offset;
  3708. file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
  3709. nfs4_transform_lock_offset(file_lock);
  3710. conflock = locks_alloc_lock();
  3711. if (!conflock) {
  3712. dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
  3713. status = nfserr_jukebox;
  3714. goto out;
  3715. }
  3716. err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
  3717. switch (-err) {
  3718. case 0: /* success! */
  3719. update_stateid(&lock_stp->st_stid.sc_stateid);
  3720. memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
  3721. sizeof(stateid_t));
  3722. status = 0;
  3723. break;
  3724. case (EAGAIN): /* conflock holds conflicting lock */
  3725. status = nfserr_denied;
  3726. dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
  3727. nfs4_set_lock_denied(conflock, &lock->lk_denied);
  3728. break;
  3729. case (EDEADLK):
  3730. status = nfserr_deadlock;
  3731. break;
  3732. default:
  3733. dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
  3734. status = nfserrno(err);
  3735. break;
  3736. }
  3737. out:
  3738. if (status && new_state)
  3739. release_lockowner(lock_sop);
  3740. if (!cstate->replay_owner)
  3741. nfs4_unlock_state();
  3742. if (file_lock)
  3743. locks_free_lock(file_lock);
  3744. if (conflock)
  3745. locks_free_lock(conflock);
  3746. return status;
  3747. }
  3748. /*
  3749. * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
  3750. * so we do a temporary open here just to get an open file to pass to
  3751. * vfs_test_lock. (Arguably perhaps test_lock should be done with an
  3752. * inode operation.)
  3753. */
  3754. static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
  3755. {
  3756. struct file *file;
  3757. __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
  3758. if (!err) {
  3759. err = nfserrno(vfs_test_lock(file, lock));
  3760. nfsd_close(file);
  3761. }
  3762. return err;
  3763. }
  3764. /*
  3765. * LOCKT operation
  3766. */
  3767. __be32
  3768. nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3769. struct nfsd4_lockt *lockt)
  3770. {
  3771. struct inode *inode;
  3772. struct file_lock *file_lock = NULL;
  3773. struct nfs4_lockowner *lo;
  3774. __be32 status;
  3775. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  3776. if (locks_in_grace(SVC_NET(rqstp)))
  3777. return nfserr_grace;
  3778. if (check_lock_length(lockt->lt_offset, lockt->lt_length))
  3779. return nfserr_inval;
  3780. nfs4_lock_state();
  3781. if (!nfsd4_has_session(cstate)) {
  3782. status = lookup_clientid(&lockt->lt_clientid, false, nn, NULL);
  3783. if (status)
  3784. goto out;
  3785. }
  3786. if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
  3787. goto out;
  3788. inode = cstate->current_fh.fh_dentry->d_inode;
  3789. file_lock = locks_alloc_lock();
  3790. if (!file_lock) {
  3791. dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
  3792. status = nfserr_jukebox;
  3793. goto out;
  3794. }
  3795. locks_init_lock(file_lock);
  3796. switch (lockt->lt_type) {
  3797. case NFS4_READ_LT:
  3798. case NFS4_READW_LT:
  3799. file_lock->fl_type = F_RDLCK;
  3800. break;
  3801. case NFS4_WRITE_LT:
  3802. case NFS4_WRITEW_LT:
  3803. file_lock->fl_type = F_WRLCK;
  3804. break;
  3805. default:
  3806. dprintk("NFSD: nfs4_lockt: bad lock type!\n");
  3807. status = nfserr_inval;
  3808. goto out;
  3809. }
  3810. lo = find_lockowner_str(inode, &lockt->lt_clientid, &lockt->lt_owner, nn);
  3811. if (lo)
  3812. file_lock->fl_owner = (fl_owner_t)lo;
  3813. file_lock->fl_pid = current->tgid;
  3814. file_lock->fl_flags = FL_POSIX;
  3815. file_lock->fl_start = lockt->lt_offset;
  3816. file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
  3817. nfs4_transform_lock_offset(file_lock);
  3818. status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
  3819. if (status)
  3820. goto out;
  3821. if (file_lock->fl_type != F_UNLCK) {
  3822. status = nfserr_denied;
  3823. nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
  3824. }
  3825. out:
  3826. nfs4_unlock_state();
  3827. if (file_lock)
  3828. locks_free_lock(file_lock);
  3829. return status;
  3830. }
  3831. __be32
  3832. nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3833. struct nfsd4_locku *locku)
  3834. {
  3835. struct nfs4_ol_stateid *stp;
  3836. struct file *filp = NULL;
  3837. struct file_lock *file_lock = NULL;
  3838. __be32 status;
  3839. int err;
  3840. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  3841. dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
  3842. (long long) locku->lu_offset,
  3843. (long long) locku->lu_length);
  3844. if (check_lock_length(locku->lu_offset, locku->lu_length))
  3845. return nfserr_inval;
  3846. nfs4_lock_state();
  3847. status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
  3848. &locku->lu_stateid, NFS4_LOCK_STID,
  3849. &stp, nn);
  3850. if (status)
  3851. goto out;
  3852. filp = find_any_file(stp->st_file);
  3853. if (!filp) {
  3854. status = nfserr_lock_range;
  3855. goto out;
  3856. }
  3857. file_lock = locks_alloc_lock();
  3858. if (!file_lock) {
  3859. dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
  3860. status = nfserr_jukebox;
  3861. goto out;
  3862. }
  3863. locks_init_lock(file_lock);
  3864. file_lock->fl_type = F_UNLCK;
  3865. file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
  3866. file_lock->fl_pid = current->tgid;
  3867. file_lock->fl_file = filp;
  3868. file_lock->fl_flags = FL_POSIX;
  3869. file_lock->fl_lmops = &nfsd_posix_mng_ops;
  3870. file_lock->fl_start = locku->lu_offset;
  3871. file_lock->fl_end = last_byte_offset(locku->lu_offset,
  3872. locku->lu_length);
  3873. nfs4_transform_lock_offset(file_lock);
  3874. /*
  3875. * Try to unlock the file in the VFS.
  3876. */
  3877. err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
  3878. if (err) {
  3879. dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
  3880. goto out_nfserr;
  3881. }
  3882. /*
  3883. * OK, unlock succeeded; the only thing left to do is update the stateid.
  3884. */
  3885. update_stateid(&stp->st_stid.sc_stateid);
  3886. memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
  3887. out:
  3888. if (!cstate->replay_owner)
  3889. nfs4_unlock_state();
  3890. if (file_lock)
  3891. locks_free_lock(file_lock);
  3892. return status;
  3893. out_nfserr:
  3894. status = nfserrno(err);
  3895. goto out;
  3896. }
  3897. /*
  3898. * returns
  3899. * 1: locks held by lockowner
  3900. * 0: no locks held by lockowner
  3901. */
  3902. static int
  3903. check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner)
  3904. {
  3905. struct file_lock **flpp;
  3906. struct inode *inode = filp->fi_inode;
  3907. int status = 0;
  3908. lock_flocks();
  3909. for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
  3910. if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
  3911. status = 1;
  3912. goto out;
  3913. }
  3914. }
  3915. out:
  3916. unlock_flocks();
  3917. return status;
  3918. }
  3919. __be32
  3920. nfsd4_release_lockowner(struct svc_rqst *rqstp,
  3921. struct nfsd4_compound_state *cstate,
  3922. struct nfsd4_release_lockowner *rlockowner)
  3923. {
  3924. clientid_t *clid = &rlockowner->rl_clientid;
  3925. struct nfs4_stateowner *sop;
  3926. struct nfs4_lockowner *lo;
  3927. struct nfs4_ol_stateid *stp;
  3928. struct xdr_netobj *owner = &rlockowner->rl_owner;
  3929. struct list_head matches;
  3930. unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
  3931. __be32 status;
  3932. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  3933. dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
  3934. clid->cl_boot, clid->cl_id);
  3935. nfs4_lock_state();
  3936. status = lookup_clientid(clid, cstate->minorversion, nn, NULL);
  3937. if (status)
  3938. goto out;
  3939. status = nfserr_locks_held;
  3940. INIT_LIST_HEAD(&matches);
  3941. list_for_each_entry(sop, &nn->ownerstr_hashtbl[hashval], so_strhash) {
  3942. if (sop->so_is_open_owner)
  3943. continue;
  3944. if (!same_owner_str(sop, owner, clid))
  3945. continue;
  3946. list_for_each_entry(stp, &sop->so_stateids,
  3947. st_perstateowner) {
  3948. lo = lockowner(sop);
  3949. if (check_for_locks(stp->st_file, lo))
  3950. goto out;
  3951. list_add(&lo->lo_list, &matches);
  3952. }
  3953. }
  3954. /* Clients probably won't expect us to return with some (but not all)
  3955. * of the lockowner state released; so don't release any until all
  3956. * have been checked. */
  3957. status = nfs_ok;
  3958. while (!list_empty(&matches)) {
  3959. lo = list_entry(matches.next, struct nfs4_lockowner,
  3960. lo_list);
  3961. /* unhash_stateowner deletes so_perclient only
  3962. * for openowners. */
  3963. list_del(&lo->lo_list);
  3964. release_lockowner(lo);
  3965. }
  3966. out:
  3967. nfs4_unlock_state();
  3968. return status;
  3969. }
  3970. static inline struct nfs4_client_reclaim *
  3971. alloc_reclaim(void)
  3972. {
  3973. return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
  3974. }
  3975. bool
  3976. nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
  3977. {
  3978. struct nfs4_client_reclaim *crp;
  3979. crp = nfsd4_find_reclaim_client(name, nn);
  3980. return (crp && crp->cr_clp);
  3981. }
  3982. /*
  3983. * failure => all reset bets are off, nfserr_no_grace...
  3984. */
  3985. struct nfs4_client_reclaim *
  3986. nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
  3987. {
  3988. unsigned int strhashval;
  3989. struct nfs4_client_reclaim *crp;
  3990. dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
  3991. crp = alloc_reclaim();
  3992. if (crp) {
  3993. strhashval = clientstr_hashval(name);
  3994. INIT_LIST_HEAD(&crp->cr_strhash);
  3995. list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
  3996. memcpy(crp->cr_recdir, name, HEXDIR_LEN);
  3997. crp->cr_clp = NULL;
  3998. nn->reclaim_str_hashtbl_size++;
  3999. }
  4000. return crp;
  4001. }
  4002. void
  4003. nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
  4004. {
  4005. list_del(&crp->cr_strhash);
  4006. kfree(crp);
  4007. nn->reclaim_str_hashtbl_size--;
  4008. }
  4009. void
  4010. nfs4_release_reclaim(struct nfsd_net *nn)
  4011. {
  4012. struct nfs4_client_reclaim *crp = NULL;
  4013. int i;
  4014. for (i = 0; i < CLIENT_HASH_SIZE; i++) {
  4015. while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
  4016. crp = list_entry(nn->reclaim_str_hashtbl[i].next,
  4017. struct nfs4_client_reclaim, cr_strhash);
  4018. nfs4_remove_reclaim_record(crp, nn);
  4019. }
  4020. }
  4021. WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
  4022. }
  4023. /*
  4024. * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
  4025. struct nfs4_client_reclaim *
  4026. nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
  4027. {
  4028. unsigned int strhashval;
  4029. struct nfs4_client_reclaim *crp = NULL;
  4030. dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
  4031. strhashval = clientstr_hashval(recdir);
  4032. list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
  4033. if (same_name(crp->cr_recdir, recdir)) {
  4034. return crp;
  4035. }
  4036. }
  4037. return NULL;
  4038. }
  4039. /*
  4040. * Called from OPEN. Look for clientid in reclaim list.
  4041. */
  4042. __be32
  4043. nfs4_check_open_reclaim(clientid_t *clid, bool sessions, struct nfsd_net *nn)
  4044. {
  4045. struct nfs4_client *clp;
  4046. /* find clientid in conf_id_hashtbl */
  4047. clp = find_confirmed_client(clid, sessions, nn);
  4048. if (clp == NULL)
  4049. return nfserr_reclaim_bad;
  4050. return nfsd4_client_record_check(clp) ? nfserr_reclaim_bad : nfs_ok;
  4051. }
  4052. #ifdef CONFIG_NFSD_FAULT_INJECTION
  4053. u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
  4054. {
  4055. expire_client(clp);
  4056. return 1;
  4057. }
  4058. u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
  4059. {
  4060. char buf[INET6_ADDRSTRLEN];
  4061. rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
  4062. printk(KERN_INFO "NFS Client: %s\n", buf);
  4063. return 1;
  4064. }
  4065. static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
  4066. const char *type)
  4067. {
  4068. char buf[INET6_ADDRSTRLEN];
  4069. rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
  4070. printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
  4071. }
  4072. static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_lockowner *))
  4073. {
  4074. struct nfs4_openowner *oop;
  4075. struct nfs4_lockowner *lop, *lo_next;
  4076. struct nfs4_ol_stateid *stp, *st_next;
  4077. u64 count = 0;
  4078. list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
  4079. list_for_each_entry_safe(stp, st_next, &oop->oo_owner.so_stateids, st_perstateowner) {
  4080. list_for_each_entry_safe(lop, lo_next, &stp->st_lockowners, lo_perstateid) {
  4081. if (func)
  4082. func(lop);
  4083. if (++count == max)
  4084. return count;
  4085. }
  4086. }
  4087. }
  4088. return count;
  4089. }
  4090. u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
  4091. {
  4092. return nfsd_foreach_client_lock(clp, max, release_lockowner);
  4093. }
  4094. u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
  4095. {
  4096. u64 count = nfsd_foreach_client_lock(clp, max, NULL);
  4097. nfsd_print_count(clp, count, "locked files");
  4098. return count;
  4099. }
  4100. static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
  4101. {
  4102. struct nfs4_openowner *oop, *next;
  4103. u64 count = 0;
  4104. list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
  4105. if (func)
  4106. func(oop);
  4107. if (++count == max)
  4108. break;
  4109. }
  4110. return count;
  4111. }
  4112. u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
  4113. {
  4114. return nfsd_foreach_client_open(clp, max, release_openowner);
  4115. }
  4116. u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
  4117. {
  4118. u64 count = nfsd_foreach_client_open(clp, max, NULL);
  4119. nfsd_print_count(clp, count, "open files");
  4120. return count;
  4121. }
  4122. static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
  4123. struct list_head *victims)
  4124. {
  4125. struct nfs4_delegation *dp, *next;
  4126. u64 count = 0;
  4127. list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
  4128. if (victims)
  4129. list_move(&dp->dl_recall_lru, victims);
  4130. if (++count == max)
  4131. break;
  4132. }
  4133. return count;
  4134. }
  4135. u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
  4136. {
  4137. struct nfs4_delegation *dp, *next;
  4138. LIST_HEAD(victims);
  4139. u64 count;
  4140. spin_lock(&recall_lock);
  4141. count = nfsd_find_all_delegations(clp, max, &victims);
  4142. spin_unlock(&recall_lock);
  4143. list_for_each_entry_safe(dp, next, &victims, dl_recall_lru)
  4144. unhash_delegation(dp);
  4145. return count;
  4146. }
  4147. u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
  4148. {
  4149. struct nfs4_delegation *dp, *next;
  4150. LIST_HEAD(victims);
  4151. u64 count;
  4152. spin_lock(&recall_lock);
  4153. count = nfsd_find_all_delegations(clp, max, &victims);
  4154. list_for_each_entry_safe(dp, next, &victims, dl_recall_lru)
  4155. nfsd_break_one_deleg(dp);
  4156. spin_unlock(&recall_lock);
  4157. return count;
  4158. }
  4159. u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
  4160. {
  4161. u64 count = 0;
  4162. spin_lock(&recall_lock);
  4163. count = nfsd_find_all_delegations(clp, max, NULL);
  4164. spin_unlock(&recall_lock);
  4165. nfsd_print_count(clp, count, "delegations");
  4166. return count;
  4167. }
  4168. u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
  4169. {
  4170. struct nfs4_client *clp, *next;
  4171. u64 count = 0;
  4172. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
  4173. if (!nfsd_netns_ready(nn))
  4174. return 0;
  4175. list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
  4176. count += func(clp, max - count);
  4177. if ((max != 0) && (count >= max))
  4178. break;
  4179. }
  4180. return count;
  4181. }
  4182. struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
  4183. {
  4184. struct nfs4_client *clp;
  4185. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
  4186. if (!nfsd_netns_ready(nn))
  4187. return NULL;
  4188. list_for_each_entry(clp, &nn->client_lru, cl_lru) {
  4189. if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
  4190. return clp;
  4191. }
  4192. return NULL;
  4193. }
  4194. #endif /* CONFIG_NFSD_FAULT_INJECTION */
  4195. /* initialization to perform at module load time: */
  4196. void
  4197. nfs4_state_init(void)
  4198. {
  4199. int i;
  4200. for (i = 0; i < FILE_HASH_SIZE; i++) {
  4201. INIT_LIST_HEAD(&file_hashtbl[i]);
  4202. }
  4203. INIT_LIST_HEAD(&del_recall_lru);
  4204. }
  4205. /*
  4206. * Since the lifetime of a delegation isn't limited to that of an open, a
  4207. * client may quite reasonably hang on to a delegation as long as it has
  4208. * the inode cached. This becomes an obvious problem the first time a
  4209. * client's inode cache approaches the size of the server's total memory.
  4210. *
  4211. * For now we avoid this problem by imposing a hard limit on the number
  4212. * of delegations, which varies according to the server's memory size.
  4213. */
  4214. static void
  4215. set_max_delegations(void)
  4216. {
  4217. /*
  4218. * Allow at most 4 delegations per megabyte of RAM. Quick
  4219. * estimates suggest that in the worst case (where every delegation
  4220. * is for a different inode), a delegation could take about 1.5K,
  4221. * giving a worst case usage of about 6% of memory.
  4222. */
  4223. max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
  4224. }
  4225. static int nfs4_state_create_net(struct net *net)
  4226. {
  4227. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  4228. int i;
  4229. nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
  4230. CLIENT_HASH_SIZE, GFP_KERNEL);
  4231. if (!nn->conf_id_hashtbl)
  4232. goto err;
  4233. nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
  4234. CLIENT_HASH_SIZE, GFP_KERNEL);
  4235. if (!nn->unconf_id_hashtbl)
  4236. goto err_unconf_id;
  4237. nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
  4238. OWNER_HASH_SIZE, GFP_KERNEL);
  4239. if (!nn->ownerstr_hashtbl)
  4240. goto err_ownerstr;
  4241. nn->lockowner_ino_hashtbl = kmalloc(sizeof(struct list_head) *
  4242. LOCKOWNER_INO_HASH_SIZE, GFP_KERNEL);
  4243. if (!nn->lockowner_ino_hashtbl)
  4244. goto err_lockowner_ino;
  4245. nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
  4246. SESSION_HASH_SIZE, GFP_KERNEL);
  4247. if (!nn->sessionid_hashtbl)
  4248. goto err_sessionid;
  4249. for (i = 0; i < CLIENT_HASH_SIZE; i++) {
  4250. INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
  4251. INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
  4252. }
  4253. for (i = 0; i < OWNER_HASH_SIZE; i++)
  4254. INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]);
  4255. for (i = 0; i < LOCKOWNER_INO_HASH_SIZE; i++)
  4256. INIT_LIST_HEAD(&nn->lockowner_ino_hashtbl[i]);
  4257. for (i = 0; i < SESSION_HASH_SIZE; i++)
  4258. INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
  4259. nn->conf_name_tree = RB_ROOT;
  4260. nn->unconf_name_tree = RB_ROOT;
  4261. INIT_LIST_HEAD(&nn->client_lru);
  4262. INIT_LIST_HEAD(&nn->close_lru);
  4263. spin_lock_init(&nn->client_lock);
  4264. INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
  4265. get_net(net);
  4266. return 0;
  4267. err_sessionid:
  4268. kfree(nn->lockowner_ino_hashtbl);
  4269. err_lockowner_ino:
  4270. kfree(nn->ownerstr_hashtbl);
  4271. err_ownerstr:
  4272. kfree(nn->unconf_id_hashtbl);
  4273. err_unconf_id:
  4274. kfree(nn->conf_id_hashtbl);
  4275. err:
  4276. return -ENOMEM;
  4277. }
  4278. static void
  4279. nfs4_state_destroy_net(struct net *net)
  4280. {
  4281. int i;
  4282. struct nfs4_client *clp = NULL;
  4283. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  4284. struct rb_node *node, *tmp;
  4285. for (i = 0; i < CLIENT_HASH_SIZE; i++) {
  4286. while (!list_empty(&nn->conf_id_hashtbl[i])) {
  4287. clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
  4288. destroy_client(clp);
  4289. }
  4290. }
  4291. node = rb_first(&nn->unconf_name_tree);
  4292. while (node != NULL) {
  4293. tmp = node;
  4294. node = rb_next(tmp);
  4295. clp = rb_entry(tmp, struct nfs4_client, cl_namenode);
  4296. rb_erase(tmp, &nn->unconf_name_tree);
  4297. destroy_client(clp);
  4298. }
  4299. kfree(nn->sessionid_hashtbl);
  4300. kfree(nn->lockowner_ino_hashtbl);
  4301. kfree(nn->ownerstr_hashtbl);
  4302. kfree(nn->unconf_id_hashtbl);
  4303. kfree(nn->conf_id_hashtbl);
  4304. put_net(net);
  4305. }
  4306. int
  4307. nfs4_state_start_net(struct net *net)
  4308. {
  4309. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  4310. int ret;
  4311. /*
  4312. * FIXME: For now, we hang most of the pernet global stuff off of
  4313. * init_net until nfsd is fully containerized. Eventually, we'll
  4314. * need to pass a net pointer into this function, take a reference
  4315. * to that instead and then do most of the rest of this on a per-net
  4316. * basis.
  4317. */
  4318. if (net != &init_net)
  4319. return -EINVAL;
  4320. ret = nfs4_state_create_net(net);
  4321. if (ret)
  4322. return ret;
  4323. nfsd4_client_tracking_init(net);
  4324. nn->boot_time = get_seconds();
  4325. locks_start_grace(net, &nn->nfsd4_manager);
  4326. nn->grace_ended = false;
  4327. printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
  4328. nn->nfsd4_grace, net);
  4329. queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
  4330. return 0;
  4331. }
  4332. /* initialization to perform when the nfsd service is started: */
  4333. int
  4334. nfs4_state_start(void)
  4335. {
  4336. int ret;
  4337. ret = set_callback_cred();
  4338. if (ret)
  4339. return -ENOMEM;
  4340. laundry_wq = create_singlethread_workqueue("nfsd4");
  4341. if (laundry_wq == NULL) {
  4342. ret = -ENOMEM;
  4343. goto out_recovery;
  4344. }
  4345. ret = nfsd4_create_callback_queue();
  4346. if (ret)
  4347. goto out_free_laundry;
  4348. set_max_delegations();
  4349. return 0;
  4350. out_free_laundry:
  4351. destroy_workqueue(laundry_wq);
  4352. out_recovery:
  4353. return ret;
  4354. }
  4355. /* should be called with the state lock held */
  4356. void
  4357. nfs4_state_shutdown_net(struct net *net)
  4358. {
  4359. struct nfs4_delegation *dp = NULL;
  4360. struct list_head *pos, *next, reaplist;
  4361. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  4362. cancel_delayed_work_sync(&nn->laundromat_work);
  4363. locks_end_grace(&nn->nfsd4_manager);
  4364. INIT_LIST_HEAD(&reaplist);
  4365. spin_lock(&recall_lock);
  4366. list_for_each_safe(pos, next, &del_recall_lru) {
  4367. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  4368. if (dp->dl_stid.sc_client->net != net)
  4369. continue;
  4370. list_move(&dp->dl_recall_lru, &reaplist);
  4371. }
  4372. spin_unlock(&recall_lock);
  4373. list_for_each_safe(pos, next, &reaplist) {
  4374. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  4375. unhash_delegation(dp);
  4376. }
  4377. nfsd4_client_tracking_exit(net);
  4378. nfs4_state_destroy_net(net);
  4379. }
  4380. void
  4381. nfs4_state_shutdown(void)
  4382. {
  4383. destroy_workqueue(laundry_wq);
  4384. nfsd4_destroy_callback_queue();
  4385. }
  4386. static void
  4387. get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
  4388. {
  4389. if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
  4390. memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
  4391. }
  4392. static void
  4393. put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
  4394. {
  4395. if (cstate->minorversion) {
  4396. memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
  4397. SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
  4398. }
  4399. }
  4400. void
  4401. clear_current_stateid(struct nfsd4_compound_state *cstate)
  4402. {
  4403. CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
  4404. }
  4405. /*
  4406. * functions to set current state id
  4407. */
  4408. void
  4409. nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
  4410. {
  4411. put_stateid(cstate, &odp->od_stateid);
  4412. }
  4413. void
  4414. nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
  4415. {
  4416. put_stateid(cstate, &open->op_stateid);
  4417. }
  4418. void
  4419. nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
  4420. {
  4421. put_stateid(cstate, &close->cl_stateid);
  4422. }
  4423. void
  4424. nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
  4425. {
  4426. put_stateid(cstate, &lock->lk_resp_stateid);
  4427. }
  4428. /*
  4429. * functions to consume current state id
  4430. */
  4431. void
  4432. nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
  4433. {
  4434. get_stateid(cstate, &odp->od_stateid);
  4435. }
  4436. void
  4437. nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
  4438. {
  4439. get_stateid(cstate, &drp->dr_stateid);
  4440. }
  4441. void
  4442. nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
  4443. {
  4444. get_stateid(cstate, &fsp->fr_stateid);
  4445. }
  4446. void
  4447. nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
  4448. {
  4449. get_stateid(cstate, &setattr->sa_stateid);
  4450. }
  4451. void
  4452. nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
  4453. {
  4454. get_stateid(cstate, &close->cl_stateid);
  4455. }
  4456. void
  4457. nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
  4458. {
  4459. get_stateid(cstate, &locku->lu_stateid);
  4460. }
  4461. void
  4462. nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
  4463. {
  4464. get_stateid(cstate, &read->rd_stateid);
  4465. }
  4466. void
  4467. nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
  4468. {
  4469. get_stateid(cstate, &write->wr_stateid);
  4470. }