nfs4state.c 115 KB

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