nfs4state.c 113 KB

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