nfs4state.c 118 KB

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