nfs4state.c 111 KB

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