nfs4state.c 110 KB

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