nfs4state.c 126 KB

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