nfs4state.c 108 KB

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