nfs4state.c 116 KB

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