nfs4state.c 119 KB

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