nfs4state.c 109 KB

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