nfs4state.c 126 KB

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