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