nfs4state.c 120 KB

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