nfs4state.c 113 KB

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