nfs4state.c 126 KB

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