nfs4state.c 134 KB

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