nfs4state.c 109 KB

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