nfs4state.c 109 KB

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