nfs4state.c 116 KB

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