nfs4state.c 116 KB

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