nfs4state.c 115 KB

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