nfs4state.c 130 KB

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