nfs4state.c 133 KB

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