nfs4state.c 126 KB

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