nfs4state.c 114 KB

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