nfs4state.c 130 KB

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