nfs4state.c 129 KB

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