nfs4state.c 109 KB

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