dlmglue.c 124 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * dlmglue.c
  5. *
  6. * Code which implements an OCFS2 specific interface to our DLM.
  7. *
  8. * Copyright (C) 2003, 2004 Oracle. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2 of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public
  21. * License along with this program; if not, write to the
  22. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. * Boston, MA 021110-1307, USA.
  24. */
  25. #include <linux/types.h>
  26. #include <linux/slab.h>
  27. #include <linux/highmem.h>
  28. #include <linux/mm.h>
  29. #include <linux/kthread.h>
  30. #include <linux/pagemap.h>
  31. #include <linux/debugfs.h>
  32. #include <linux/seq_file.h>
  33. #include <linux/time.h>
  34. #include <linux/quotaops.h>
  35. #include <linux/sched/signal.h>
  36. #define MLOG_MASK_PREFIX ML_DLM_GLUE
  37. #include <cluster/masklog.h>
  38. #include "ocfs2.h"
  39. #include "ocfs2_lockingver.h"
  40. #include "alloc.h"
  41. #include "dcache.h"
  42. #include "dlmglue.h"
  43. #include "extent_map.h"
  44. #include "file.h"
  45. #include "heartbeat.h"
  46. #include "inode.h"
  47. #include "journal.h"
  48. #include "stackglue.h"
  49. #include "slot_map.h"
  50. #include "super.h"
  51. #include "uptodate.h"
  52. #include "quota.h"
  53. #include "refcounttree.h"
  54. #include "acl.h"
  55. #include "buffer_head_io.h"
  56. struct ocfs2_mask_waiter {
  57. struct list_head mw_item;
  58. int mw_status;
  59. struct completion mw_complete;
  60. unsigned long mw_mask;
  61. unsigned long mw_goal;
  62. #ifdef CONFIG_OCFS2_FS_STATS
  63. ktime_t mw_lock_start;
  64. #endif
  65. };
  66. static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres);
  67. static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres);
  68. static struct ocfs2_super *ocfs2_get_file_osb(struct ocfs2_lock_res *lockres);
  69. static struct ocfs2_super *ocfs2_get_qinfo_osb(struct ocfs2_lock_res *lockres);
  70. /*
  71. * Return value from ->downconvert_worker functions.
  72. *
  73. * These control the precise actions of ocfs2_unblock_lock()
  74. * and ocfs2_process_blocked_lock()
  75. *
  76. */
  77. enum ocfs2_unblock_action {
  78. UNBLOCK_CONTINUE = 0, /* Continue downconvert */
  79. UNBLOCK_CONTINUE_POST = 1, /* Continue downconvert, fire
  80. * ->post_unlock callback */
  81. UNBLOCK_STOP_POST = 2, /* Do not downconvert, fire
  82. * ->post_unlock() callback. */
  83. };
  84. struct ocfs2_unblock_ctl {
  85. int requeue;
  86. enum ocfs2_unblock_action unblock_action;
  87. };
  88. /* Lockdep class keys */
  89. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  90. static struct lock_class_key lockdep_keys[OCFS2_NUM_LOCK_TYPES];
  91. #endif
  92. static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
  93. int new_level);
  94. static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres);
  95. static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
  96. int blocking);
  97. static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
  98. int blocking);
  99. static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
  100. struct ocfs2_lock_res *lockres);
  101. static void ocfs2_set_qinfo_lvb(struct ocfs2_lock_res *lockres);
  102. static int ocfs2_check_refcount_downconvert(struct ocfs2_lock_res *lockres,
  103. int new_level);
  104. static int ocfs2_refcount_convert_worker(struct ocfs2_lock_res *lockres,
  105. int blocking);
  106. #define mlog_meta_lvb(__level, __lockres) ocfs2_dump_meta_lvb_info(__level, __PRETTY_FUNCTION__, __LINE__, __lockres)
  107. /* This aids in debugging situations where a bad LVB might be involved. */
  108. static void ocfs2_dump_meta_lvb_info(u64 level,
  109. const char *function,
  110. unsigned int line,
  111. struct ocfs2_lock_res *lockres)
  112. {
  113. struct ocfs2_meta_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
  114. mlog(level, "LVB information for %s (called from %s:%u):\n",
  115. lockres->l_name, function, line);
  116. mlog(level, "version: %u, clusters: %u, generation: 0x%x\n",
  117. lvb->lvb_version, be32_to_cpu(lvb->lvb_iclusters),
  118. be32_to_cpu(lvb->lvb_igeneration));
  119. mlog(level, "size: %llu, uid %u, gid %u, mode 0x%x\n",
  120. (unsigned long long)be64_to_cpu(lvb->lvb_isize),
  121. be32_to_cpu(lvb->lvb_iuid), be32_to_cpu(lvb->lvb_igid),
  122. be16_to_cpu(lvb->lvb_imode));
  123. mlog(level, "nlink %u, atime_packed 0x%llx, ctime_packed 0x%llx, "
  124. "mtime_packed 0x%llx iattr 0x%x\n", be16_to_cpu(lvb->lvb_inlink),
  125. (long long)be64_to_cpu(lvb->lvb_iatime_packed),
  126. (long long)be64_to_cpu(lvb->lvb_ictime_packed),
  127. (long long)be64_to_cpu(lvb->lvb_imtime_packed),
  128. be32_to_cpu(lvb->lvb_iattr));
  129. }
  130. /*
  131. * OCFS2 Lock Resource Operations
  132. *
  133. * These fine tune the behavior of the generic dlmglue locking infrastructure.
  134. *
  135. * The most basic of lock types can point ->l_priv to their respective
  136. * struct ocfs2_super and allow the default actions to manage things.
  137. *
  138. * Right now, each lock type also needs to implement an init function,
  139. * and trivial lock/unlock wrappers. ocfs2_simple_drop_lockres()
  140. * should be called when the lock is no longer needed (i.e., object
  141. * destruction time).
  142. */
  143. struct ocfs2_lock_res_ops {
  144. /*
  145. * Translate an ocfs2_lock_res * into an ocfs2_super *. Define
  146. * this callback if ->l_priv is not an ocfs2_super pointer
  147. */
  148. struct ocfs2_super * (*get_osb)(struct ocfs2_lock_res *);
  149. /*
  150. * Optionally called in the downconvert thread after a
  151. * successful downconvert. The lockres will not be referenced
  152. * after this callback is called, so it is safe to free
  153. * memory, etc.
  154. *
  155. * The exact semantics of when this is called are controlled
  156. * by ->downconvert_worker()
  157. */
  158. void (*post_unlock)(struct ocfs2_super *, struct ocfs2_lock_res *);
  159. /*
  160. * Allow a lock type to add checks to determine whether it is
  161. * safe to downconvert a lock. Return 0 to re-queue the
  162. * downconvert at a later time, nonzero to continue.
  163. *
  164. * For most locks, the default checks that there are no
  165. * incompatible holders are sufficient.
  166. *
  167. * Called with the lockres spinlock held.
  168. */
  169. int (*check_downconvert)(struct ocfs2_lock_res *, int);
  170. /*
  171. * Allows a lock type to populate the lock value block. This
  172. * is called on downconvert, and when we drop a lock.
  173. *
  174. * Locks that want to use this should set LOCK_TYPE_USES_LVB
  175. * in the flags field.
  176. *
  177. * Called with the lockres spinlock held.
  178. */
  179. void (*set_lvb)(struct ocfs2_lock_res *);
  180. /*
  181. * Called from the downconvert thread when it is determined
  182. * that a lock will be downconverted. This is called without
  183. * any locks held so the function can do work that might
  184. * schedule (syncing out data, etc).
  185. *
  186. * This should return any one of the ocfs2_unblock_action
  187. * values, depending on what it wants the thread to do.
  188. */
  189. int (*downconvert_worker)(struct ocfs2_lock_res *, int);
  190. /*
  191. * LOCK_TYPE_* flags which describe the specific requirements
  192. * of a lock type. Descriptions of each individual flag follow.
  193. */
  194. int flags;
  195. };
  196. /*
  197. * Some locks want to "refresh" potentially stale data when a
  198. * meaningful (PRMODE or EXMODE) lock level is first obtained. If this
  199. * flag is set, the OCFS2_LOCK_NEEDS_REFRESH flag will be set on the
  200. * individual lockres l_flags member from the ast function. It is
  201. * expected that the locking wrapper will clear the
  202. * OCFS2_LOCK_NEEDS_REFRESH flag when done.
  203. */
  204. #define LOCK_TYPE_REQUIRES_REFRESH 0x1
  205. /*
  206. * Indicate that a lock type makes use of the lock value block. The
  207. * ->set_lvb lock type callback must be defined.
  208. */
  209. #define LOCK_TYPE_USES_LVB 0x2
  210. static struct ocfs2_lock_res_ops ocfs2_inode_rw_lops = {
  211. .get_osb = ocfs2_get_inode_osb,
  212. .flags = 0,
  213. };
  214. static struct ocfs2_lock_res_ops ocfs2_inode_inode_lops = {
  215. .get_osb = ocfs2_get_inode_osb,
  216. .check_downconvert = ocfs2_check_meta_downconvert,
  217. .set_lvb = ocfs2_set_meta_lvb,
  218. .downconvert_worker = ocfs2_data_convert_worker,
  219. .flags = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB,
  220. };
  221. static struct ocfs2_lock_res_ops ocfs2_super_lops = {
  222. .flags = LOCK_TYPE_REQUIRES_REFRESH,
  223. };
  224. static struct ocfs2_lock_res_ops ocfs2_rename_lops = {
  225. .flags = 0,
  226. };
  227. static struct ocfs2_lock_res_ops ocfs2_nfs_sync_lops = {
  228. .flags = 0,
  229. };
  230. static struct ocfs2_lock_res_ops ocfs2_trim_fs_lops = {
  231. .flags = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB,
  232. };
  233. static struct ocfs2_lock_res_ops ocfs2_orphan_scan_lops = {
  234. .flags = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB,
  235. };
  236. static struct ocfs2_lock_res_ops ocfs2_dentry_lops = {
  237. .get_osb = ocfs2_get_dentry_osb,
  238. .post_unlock = ocfs2_dentry_post_unlock,
  239. .downconvert_worker = ocfs2_dentry_convert_worker,
  240. .flags = 0,
  241. };
  242. static struct ocfs2_lock_res_ops ocfs2_inode_open_lops = {
  243. .get_osb = ocfs2_get_inode_osb,
  244. .flags = 0,
  245. };
  246. static struct ocfs2_lock_res_ops ocfs2_flock_lops = {
  247. .get_osb = ocfs2_get_file_osb,
  248. .flags = 0,
  249. };
  250. static struct ocfs2_lock_res_ops ocfs2_qinfo_lops = {
  251. .set_lvb = ocfs2_set_qinfo_lvb,
  252. .get_osb = ocfs2_get_qinfo_osb,
  253. .flags = LOCK_TYPE_REQUIRES_REFRESH | LOCK_TYPE_USES_LVB,
  254. };
  255. static struct ocfs2_lock_res_ops ocfs2_refcount_block_lops = {
  256. .check_downconvert = ocfs2_check_refcount_downconvert,
  257. .downconvert_worker = ocfs2_refcount_convert_worker,
  258. .flags = 0,
  259. };
  260. static inline int ocfs2_is_inode_lock(struct ocfs2_lock_res *lockres)
  261. {
  262. return lockres->l_type == OCFS2_LOCK_TYPE_META ||
  263. lockres->l_type == OCFS2_LOCK_TYPE_RW ||
  264. lockres->l_type == OCFS2_LOCK_TYPE_OPEN;
  265. }
  266. static inline struct ocfs2_lock_res *ocfs2_lksb_to_lock_res(struct ocfs2_dlm_lksb *lksb)
  267. {
  268. return container_of(lksb, struct ocfs2_lock_res, l_lksb);
  269. }
  270. static inline struct inode *ocfs2_lock_res_inode(struct ocfs2_lock_res *lockres)
  271. {
  272. BUG_ON(!ocfs2_is_inode_lock(lockres));
  273. return (struct inode *) lockres->l_priv;
  274. }
  275. static inline struct ocfs2_dentry_lock *ocfs2_lock_res_dl(struct ocfs2_lock_res *lockres)
  276. {
  277. BUG_ON(lockres->l_type != OCFS2_LOCK_TYPE_DENTRY);
  278. return (struct ocfs2_dentry_lock *)lockres->l_priv;
  279. }
  280. static inline struct ocfs2_mem_dqinfo *ocfs2_lock_res_qinfo(struct ocfs2_lock_res *lockres)
  281. {
  282. BUG_ON(lockres->l_type != OCFS2_LOCK_TYPE_QINFO);
  283. return (struct ocfs2_mem_dqinfo *)lockres->l_priv;
  284. }
  285. static inline struct ocfs2_refcount_tree *
  286. ocfs2_lock_res_refcount_tree(struct ocfs2_lock_res *res)
  287. {
  288. return container_of(res, struct ocfs2_refcount_tree, rf_lockres);
  289. }
  290. static inline struct ocfs2_super *ocfs2_get_lockres_osb(struct ocfs2_lock_res *lockres)
  291. {
  292. if (lockres->l_ops->get_osb)
  293. return lockres->l_ops->get_osb(lockres);
  294. return (struct ocfs2_super *)lockres->l_priv;
  295. }
  296. static int ocfs2_lock_create(struct ocfs2_super *osb,
  297. struct ocfs2_lock_res *lockres,
  298. int level,
  299. u32 dlm_flags);
  300. static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
  301. int wanted);
  302. static void __ocfs2_cluster_unlock(struct ocfs2_super *osb,
  303. struct ocfs2_lock_res *lockres,
  304. int level, unsigned long caller_ip);
  305. static inline void ocfs2_cluster_unlock(struct ocfs2_super *osb,
  306. struct ocfs2_lock_res *lockres,
  307. int level)
  308. {
  309. __ocfs2_cluster_unlock(osb, lockres, level, _RET_IP_);
  310. }
  311. static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres);
  312. static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres);
  313. static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres);
  314. static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres, int level);
  315. static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
  316. struct ocfs2_lock_res *lockres);
  317. static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
  318. int convert);
  319. #define ocfs2_log_dlm_error(_func, _err, _lockres) do { \
  320. if ((_lockres)->l_type != OCFS2_LOCK_TYPE_DENTRY) \
  321. mlog(ML_ERROR, "DLM error %d while calling %s on resource %s\n", \
  322. _err, _func, _lockres->l_name); \
  323. else \
  324. mlog(ML_ERROR, "DLM error %d while calling %s on resource %.*s%08x\n", \
  325. _err, _func, OCFS2_DENTRY_LOCK_INO_START - 1, (_lockres)->l_name, \
  326. (unsigned int)ocfs2_get_dentry_lock_ino(_lockres)); \
  327. } while (0)
  328. static int ocfs2_downconvert_thread(void *arg);
  329. static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
  330. struct ocfs2_lock_res *lockres);
  331. static int ocfs2_inode_lock_update(struct inode *inode,
  332. struct buffer_head **bh);
  333. static void ocfs2_drop_osb_locks(struct ocfs2_super *osb);
  334. static inline int ocfs2_highest_compat_lock_level(int level);
  335. static unsigned int ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
  336. int new_level);
  337. static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
  338. struct ocfs2_lock_res *lockres,
  339. int new_level,
  340. int lvb,
  341. unsigned int generation);
  342. static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
  343. struct ocfs2_lock_res *lockres);
  344. static int ocfs2_cancel_convert(struct ocfs2_super *osb,
  345. struct ocfs2_lock_res *lockres);
  346. static void ocfs2_build_lock_name(enum ocfs2_lock_type type,
  347. u64 blkno,
  348. u32 generation,
  349. char *name)
  350. {
  351. int len;
  352. BUG_ON(type >= OCFS2_NUM_LOCK_TYPES);
  353. len = snprintf(name, OCFS2_LOCK_ID_MAX_LEN, "%c%s%016llx%08x",
  354. ocfs2_lock_type_char(type), OCFS2_LOCK_ID_PAD,
  355. (long long)blkno, generation);
  356. BUG_ON(len != (OCFS2_LOCK_ID_MAX_LEN - 1));
  357. mlog(0, "built lock resource with name: %s\n", name);
  358. }
  359. static DEFINE_SPINLOCK(ocfs2_dlm_tracking_lock);
  360. static void ocfs2_add_lockres_tracking(struct ocfs2_lock_res *res,
  361. struct ocfs2_dlm_debug *dlm_debug)
  362. {
  363. mlog(0, "Add tracking for lockres %s\n", res->l_name);
  364. spin_lock(&ocfs2_dlm_tracking_lock);
  365. list_add(&res->l_debug_list, &dlm_debug->d_lockres_tracking);
  366. spin_unlock(&ocfs2_dlm_tracking_lock);
  367. }
  368. static void ocfs2_remove_lockres_tracking(struct ocfs2_lock_res *res)
  369. {
  370. spin_lock(&ocfs2_dlm_tracking_lock);
  371. if (!list_empty(&res->l_debug_list))
  372. list_del_init(&res->l_debug_list);
  373. spin_unlock(&ocfs2_dlm_tracking_lock);
  374. }
  375. #ifdef CONFIG_OCFS2_FS_STATS
  376. static void ocfs2_init_lock_stats(struct ocfs2_lock_res *res)
  377. {
  378. res->l_lock_refresh = 0;
  379. memset(&res->l_lock_prmode, 0, sizeof(struct ocfs2_lock_stats));
  380. memset(&res->l_lock_exmode, 0, sizeof(struct ocfs2_lock_stats));
  381. }
  382. static void ocfs2_update_lock_stats(struct ocfs2_lock_res *res, int level,
  383. struct ocfs2_mask_waiter *mw, int ret)
  384. {
  385. u32 usec;
  386. ktime_t kt;
  387. struct ocfs2_lock_stats *stats;
  388. if (level == LKM_PRMODE)
  389. stats = &res->l_lock_prmode;
  390. else if (level == LKM_EXMODE)
  391. stats = &res->l_lock_exmode;
  392. else
  393. return;
  394. kt = ktime_sub(ktime_get(), mw->mw_lock_start);
  395. usec = ktime_to_us(kt);
  396. stats->ls_gets++;
  397. stats->ls_total += ktime_to_ns(kt);
  398. /* overflow */
  399. if (unlikely(stats->ls_gets == 0)) {
  400. stats->ls_gets++;
  401. stats->ls_total = ktime_to_ns(kt);
  402. }
  403. if (stats->ls_max < usec)
  404. stats->ls_max = usec;
  405. if (ret)
  406. stats->ls_fail++;
  407. }
  408. static inline void ocfs2_track_lock_refresh(struct ocfs2_lock_res *lockres)
  409. {
  410. lockres->l_lock_refresh++;
  411. }
  412. static inline void ocfs2_init_start_time(struct ocfs2_mask_waiter *mw)
  413. {
  414. mw->mw_lock_start = ktime_get();
  415. }
  416. #else
  417. static inline void ocfs2_init_lock_stats(struct ocfs2_lock_res *res)
  418. {
  419. }
  420. static inline void ocfs2_update_lock_stats(struct ocfs2_lock_res *res,
  421. int level, struct ocfs2_mask_waiter *mw, int ret)
  422. {
  423. }
  424. static inline void ocfs2_track_lock_refresh(struct ocfs2_lock_res *lockres)
  425. {
  426. }
  427. static inline void ocfs2_init_start_time(struct ocfs2_mask_waiter *mw)
  428. {
  429. }
  430. #endif
  431. static void ocfs2_lock_res_init_common(struct ocfs2_super *osb,
  432. struct ocfs2_lock_res *res,
  433. enum ocfs2_lock_type type,
  434. struct ocfs2_lock_res_ops *ops,
  435. void *priv)
  436. {
  437. res->l_type = type;
  438. res->l_ops = ops;
  439. res->l_priv = priv;
  440. res->l_level = DLM_LOCK_IV;
  441. res->l_requested = DLM_LOCK_IV;
  442. res->l_blocking = DLM_LOCK_IV;
  443. res->l_action = OCFS2_AST_INVALID;
  444. res->l_unlock_action = OCFS2_UNLOCK_INVALID;
  445. res->l_flags = OCFS2_LOCK_INITIALIZED;
  446. ocfs2_add_lockres_tracking(res, osb->osb_dlm_debug);
  447. ocfs2_init_lock_stats(res);
  448. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  449. if (type != OCFS2_LOCK_TYPE_OPEN)
  450. lockdep_init_map(&res->l_lockdep_map, ocfs2_lock_type_strings[type],
  451. &lockdep_keys[type], 0);
  452. else
  453. res->l_lockdep_map.key = NULL;
  454. #endif
  455. }
  456. void ocfs2_lock_res_init_once(struct ocfs2_lock_res *res)
  457. {
  458. /* This also clears out the lock status block */
  459. memset(res, 0, sizeof(struct ocfs2_lock_res));
  460. spin_lock_init(&res->l_lock);
  461. init_waitqueue_head(&res->l_event);
  462. INIT_LIST_HEAD(&res->l_blocked_list);
  463. INIT_LIST_HEAD(&res->l_mask_waiters);
  464. INIT_LIST_HEAD(&res->l_holders);
  465. }
  466. void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res,
  467. enum ocfs2_lock_type type,
  468. unsigned int generation,
  469. struct inode *inode)
  470. {
  471. struct ocfs2_lock_res_ops *ops;
  472. switch(type) {
  473. case OCFS2_LOCK_TYPE_RW:
  474. ops = &ocfs2_inode_rw_lops;
  475. break;
  476. case OCFS2_LOCK_TYPE_META:
  477. ops = &ocfs2_inode_inode_lops;
  478. break;
  479. case OCFS2_LOCK_TYPE_OPEN:
  480. ops = &ocfs2_inode_open_lops;
  481. break;
  482. default:
  483. mlog_bug_on_msg(1, "type: %d\n", type);
  484. ops = NULL; /* thanks, gcc */
  485. break;
  486. };
  487. ocfs2_build_lock_name(type, OCFS2_I(inode)->ip_blkno,
  488. generation, res->l_name);
  489. ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), res, type, ops, inode);
  490. }
  491. static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres)
  492. {
  493. struct inode *inode = ocfs2_lock_res_inode(lockres);
  494. return OCFS2_SB(inode->i_sb);
  495. }
  496. static struct ocfs2_super *ocfs2_get_qinfo_osb(struct ocfs2_lock_res *lockres)
  497. {
  498. struct ocfs2_mem_dqinfo *info = lockres->l_priv;
  499. return OCFS2_SB(info->dqi_gi.dqi_sb);
  500. }
  501. static struct ocfs2_super *ocfs2_get_file_osb(struct ocfs2_lock_res *lockres)
  502. {
  503. struct ocfs2_file_private *fp = lockres->l_priv;
  504. return OCFS2_SB(fp->fp_file->f_mapping->host->i_sb);
  505. }
  506. static __u64 ocfs2_get_dentry_lock_ino(struct ocfs2_lock_res *lockres)
  507. {
  508. __be64 inode_blkno_be;
  509. memcpy(&inode_blkno_be, &lockres->l_name[OCFS2_DENTRY_LOCK_INO_START],
  510. sizeof(__be64));
  511. return be64_to_cpu(inode_blkno_be);
  512. }
  513. static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres)
  514. {
  515. struct ocfs2_dentry_lock *dl = lockres->l_priv;
  516. return OCFS2_SB(dl->dl_inode->i_sb);
  517. }
  518. void ocfs2_dentry_lock_res_init(struct ocfs2_dentry_lock *dl,
  519. u64 parent, struct inode *inode)
  520. {
  521. int len;
  522. u64 inode_blkno = OCFS2_I(inode)->ip_blkno;
  523. __be64 inode_blkno_be = cpu_to_be64(inode_blkno);
  524. struct ocfs2_lock_res *lockres = &dl->dl_lockres;
  525. ocfs2_lock_res_init_once(lockres);
  526. /*
  527. * Unfortunately, the standard lock naming scheme won't work
  528. * here because we have two 16 byte values to use. Instead,
  529. * we'll stuff the inode number as a binary value. We still
  530. * want error prints to show something without garbling the
  531. * display, so drop a null byte in there before the inode
  532. * number. A future version of OCFS2 will likely use all
  533. * binary lock names. The stringified names have been a
  534. * tremendous aid in debugging, but now that the debugfs
  535. * interface exists, we can mangle things there if need be.
  536. *
  537. * NOTE: We also drop the standard "pad" value (the total lock
  538. * name size stays the same though - the last part is all
  539. * zeros due to the memset in ocfs2_lock_res_init_once()
  540. */
  541. len = snprintf(lockres->l_name, OCFS2_DENTRY_LOCK_INO_START,
  542. "%c%016llx",
  543. ocfs2_lock_type_char(OCFS2_LOCK_TYPE_DENTRY),
  544. (long long)parent);
  545. BUG_ON(len != (OCFS2_DENTRY_LOCK_INO_START - 1));
  546. memcpy(&lockres->l_name[OCFS2_DENTRY_LOCK_INO_START], &inode_blkno_be,
  547. sizeof(__be64));
  548. ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
  549. OCFS2_LOCK_TYPE_DENTRY, &ocfs2_dentry_lops,
  550. dl);
  551. }
  552. static void ocfs2_super_lock_res_init(struct ocfs2_lock_res *res,
  553. struct ocfs2_super *osb)
  554. {
  555. /* Superblock lockres doesn't come from a slab so we call init
  556. * once on it manually. */
  557. ocfs2_lock_res_init_once(res);
  558. ocfs2_build_lock_name(OCFS2_LOCK_TYPE_SUPER, OCFS2_SUPER_BLOCK_BLKNO,
  559. 0, res->l_name);
  560. ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_SUPER,
  561. &ocfs2_super_lops, osb);
  562. }
  563. static void ocfs2_rename_lock_res_init(struct ocfs2_lock_res *res,
  564. struct ocfs2_super *osb)
  565. {
  566. /* Rename lockres doesn't come from a slab so we call init
  567. * once on it manually. */
  568. ocfs2_lock_res_init_once(res);
  569. ocfs2_build_lock_name(OCFS2_LOCK_TYPE_RENAME, 0, 0, res->l_name);
  570. ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_RENAME,
  571. &ocfs2_rename_lops, osb);
  572. }
  573. static void ocfs2_nfs_sync_lock_res_init(struct ocfs2_lock_res *res,
  574. struct ocfs2_super *osb)
  575. {
  576. /* nfs_sync lockres doesn't come from a slab so we call init
  577. * once on it manually. */
  578. ocfs2_lock_res_init_once(res);
  579. ocfs2_build_lock_name(OCFS2_LOCK_TYPE_NFS_SYNC, 0, 0, res->l_name);
  580. ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_NFS_SYNC,
  581. &ocfs2_nfs_sync_lops, osb);
  582. }
  583. void ocfs2_trim_fs_lock_res_init(struct ocfs2_super *osb)
  584. {
  585. struct ocfs2_lock_res *lockres = &osb->osb_trim_fs_lockres;
  586. ocfs2_lock_res_init_once(lockres);
  587. ocfs2_build_lock_name(OCFS2_LOCK_TYPE_TRIM_FS, 0, 0, lockres->l_name);
  588. ocfs2_lock_res_init_common(osb, lockres, OCFS2_LOCK_TYPE_TRIM_FS,
  589. &ocfs2_trim_fs_lops, osb);
  590. }
  591. void ocfs2_trim_fs_lock_res_uninit(struct ocfs2_super *osb)
  592. {
  593. struct ocfs2_lock_res *lockres = &osb->osb_trim_fs_lockres;
  594. ocfs2_simple_drop_lockres(osb, lockres);
  595. ocfs2_lock_res_free(lockres);
  596. }
  597. static void ocfs2_orphan_scan_lock_res_init(struct ocfs2_lock_res *res,
  598. struct ocfs2_super *osb)
  599. {
  600. ocfs2_lock_res_init_once(res);
  601. ocfs2_build_lock_name(OCFS2_LOCK_TYPE_ORPHAN_SCAN, 0, 0, res->l_name);
  602. ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_ORPHAN_SCAN,
  603. &ocfs2_orphan_scan_lops, osb);
  604. }
  605. void ocfs2_file_lock_res_init(struct ocfs2_lock_res *lockres,
  606. struct ocfs2_file_private *fp)
  607. {
  608. struct inode *inode = fp->fp_file->f_mapping->host;
  609. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  610. ocfs2_lock_res_init_once(lockres);
  611. ocfs2_build_lock_name(OCFS2_LOCK_TYPE_FLOCK, oi->ip_blkno,
  612. inode->i_generation, lockres->l_name);
  613. ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
  614. OCFS2_LOCK_TYPE_FLOCK, &ocfs2_flock_lops,
  615. fp);
  616. lockres->l_flags |= OCFS2_LOCK_NOCACHE;
  617. }
  618. void ocfs2_qinfo_lock_res_init(struct ocfs2_lock_res *lockres,
  619. struct ocfs2_mem_dqinfo *info)
  620. {
  621. ocfs2_lock_res_init_once(lockres);
  622. ocfs2_build_lock_name(OCFS2_LOCK_TYPE_QINFO, info->dqi_gi.dqi_type,
  623. 0, lockres->l_name);
  624. ocfs2_lock_res_init_common(OCFS2_SB(info->dqi_gi.dqi_sb), lockres,
  625. OCFS2_LOCK_TYPE_QINFO, &ocfs2_qinfo_lops,
  626. info);
  627. }
  628. void ocfs2_refcount_lock_res_init(struct ocfs2_lock_res *lockres,
  629. struct ocfs2_super *osb, u64 ref_blkno,
  630. unsigned int generation)
  631. {
  632. ocfs2_lock_res_init_once(lockres);
  633. ocfs2_build_lock_name(OCFS2_LOCK_TYPE_REFCOUNT, ref_blkno,
  634. generation, lockres->l_name);
  635. ocfs2_lock_res_init_common(osb, lockres, OCFS2_LOCK_TYPE_REFCOUNT,
  636. &ocfs2_refcount_block_lops, osb);
  637. }
  638. void ocfs2_lock_res_free(struct ocfs2_lock_res *res)
  639. {
  640. if (!(res->l_flags & OCFS2_LOCK_INITIALIZED))
  641. return;
  642. ocfs2_remove_lockres_tracking(res);
  643. mlog_bug_on_msg(!list_empty(&res->l_blocked_list),
  644. "Lockres %s is on the blocked list\n",
  645. res->l_name);
  646. mlog_bug_on_msg(!list_empty(&res->l_mask_waiters),
  647. "Lockres %s has mask waiters pending\n",
  648. res->l_name);
  649. mlog_bug_on_msg(spin_is_locked(&res->l_lock),
  650. "Lockres %s is locked\n",
  651. res->l_name);
  652. mlog_bug_on_msg(res->l_ro_holders,
  653. "Lockres %s has %u ro holders\n",
  654. res->l_name, res->l_ro_holders);
  655. mlog_bug_on_msg(res->l_ex_holders,
  656. "Lockres %s has %u ex holders\n",
  657. res->l_name, res->l_ex_holders);
  658. /* Need to clear out the lock status block for the dlm */
  659. memset(&res->l_lksb, 0, sizeof(res->l_lksb));
  660. res->l_flags = 0UL;
  661. }
  662. /*
  663. * Keep a list of processes who have interest in a lockres.
  664. * Note: this is now only uesed for check recursive cluster locking.
  665. */
  666. static inline void ocfs2_add_holder(struct ocfs2_lock_res *lockres,
  667. struct ocfs2_lock_holder *oh)
  668. {
  669. INIT_LIST_HEAD(&oh->oh_list);
  670. oh->oh_owner_pid = get_pid(task_pid(current));
  671. spin_lock(&lockres->l_lock);
  672. list_add_tail(&oh->oh_list, &lockres->l_holders);
  673. spin_unlock(&lockres->l_lock);
  674. }
  675. static struct ocfs2_lock_holder *
  676. ocfs2_pid_holder(struct ocfs2_lock_res *lockres,
  677. struct pid *pid)
  678. {
  679. struct ocfs2_lock_holder *oh;
  680. spin_lock(&lockres->l_lock);
  681. list_for_each_entry(oh, &lockres->l_holders, oh_list) {
  682. if (oh->oh_owner_pid == pid) {
  683. spin_unlock(&lockres->l_lock);
  684. return oh;
  685. }
  686. }
  687. spin_unlock(&lockres->l_lock);
  688. return NULL;
  689. }
  690. static inline void ocfs2_remove_holder(struct ocfs2_lock_res *lockres,
  691. struct ocfs2_lock_holder *oh)
  692. {
  693. spin_lock(&lockres->l_lock);
  694. list_del(&oh->oh_list);
  695. spin_unlock(&lockres->l_lock);
  696. put_pid(oh->oh_owner_pid);
  697. }
  698. static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres,
  699. int level)
  700. {
  701. BUG_ON(!lockres);
  702. switch(level) {
  703. case DLM_LOCK_EX:
  704. lockres->l_ex_holders++;
  705. break;
  706. case DLM_LOCK_PR:
  707. lockres->l_ro_holders++;
  708. break;
  709. default:
  710. BUG();
  711. }
  712. }
  713. static inline void ocfs2_dec_holders(struct ocfs2_lock_res *lockres,
  714. int level)
  715. {
  716. BUG_ON(!lockres);
  717. switch(level) {
  718. case DLM_LOCK_EX:
  719. BUG_ON(!lockres->l_ex_holders);
  720. lockres->l_ex_holders--;
  721. break;
  722. case DLM_LOCK_PR:
  723. BUG_ON(!lockres->l_ro_holders);
  724. lockres->l_ro_holders--;
  725. break;
  726. default:
  727. BUG();
  728. }
  729. }
  730. /* WARNING: This function lives in a world where the only three lock
  731. * levels are EX, PR, and NL. It *will* have to be adjusted when more
  732. * lock types are added. */
  733. static inline int ocfs2_highest_compat_lock_level(int level)
  734. {
  735. int new_level = DLM_LOCK_EX;
  736. if (level == DLM_LOCK_EX)
  737. new_level = DLM_LOCK_NL;
  738. else if (level == DLM_LOCK_PR)
  739. new_level = DLM_LOCK_PR;
  740. return new_level;
  741. }
  742. static void lockres_set_flags(struct ocfs2_lock_res *lockres,
  743. unsigned long newflags)
  744. {
  745. struct ocfs2_mask_waiter *mw, *tmp;
  746. assert_spin_locked(&lockres->l_lock);
  747. lockres->l_flags = newflags;
  748. list_for_each_entry_safe(mw, tmp, &lockres->l_mask_waiters, mw_item) {
  749. if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
  750. continue;
  751. list_del_init(&mw->mw_item);
  752. mw->mw_status = 0;
  753. complete(&mw->mw_complete);
  754. }
  755. }
  756. static void lockres_or_flags(struct ocfs2_lock_res *lockres, unsigned long or)
  757. {
  758. lockres_set_flags(lockres, lockres->l_flags | or);
  759. }
  760. static void lockres_clear_flags(struct ocfs2_lock_res *lockres,
  761. unsigned long clear)
  762. {
  763. lockres_set_flags(lockres, lockres->l_flags & ~clear);
  764. }
  765. static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres)
  766. {
  767. BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
  768. BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
  769. BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
  770. BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
  771. lockres->l_level = lockres->l_requested;
  772. if (lockres->l_level <=
  773. ocfs2_highest_compat_lock_level(lockres->l_blocking)) {
  774. lockres->l_blocking = DLM_LOCK_NL;
  775. lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
  776. }
  777. lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
  778. }
  779. static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres)
  780. {
  781. BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
  782. BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
  783. /* Convert from RO to EX doesn't really need anything as our
  784. * information is already up to data. Convert from NL to
  785. * *anything* however should mark ourselves as needing an
  786. * update */
  787. if (lockres->l_level == DLM_LOCK_NL &&
  788. lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
  789. lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
  790. lockres->l_level = lockres->l_requested;
  791. /*
  792. * We set the OCFS2_LOCK_UPCONVERT_FINISHING flag before clearing
  793. * the OCFS2_LOCK_BUSY flag to prevent the dc thread from
  794. * downconverting the lock before the upconvert has fully completed.
  795. * Do not prevent the dc thread from downconverting if NONBLOCK lock
  796. * had already returned.
  797. */
  798. if (!(lockres->l_flags & OCFS2_LOCK_NONBLOCK_FINISHED))
  799. lockres_or_flags(lockres, OCFS2_LOCK_UPCONVERT_FINISHING);
  800. else
  801. lockres_clear_flags(lockres, OCFS2_LOCK_NONBLOCK_FINISHED);
  802. lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
  803. }
  804. static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres)
  805. {
  806. BUG_ON((!(lockres->l_flags & OCFS2_LOCK_BUSY)));
  807. BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
  808. if (lockres->l_requested > DLM_LOCK_NL &&
  809. !(lockres->l_flags & OCFS2_LOCK_LOCAL) &&
  810. lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
  811. lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
  812. lockres->l_level = lockres->l_requested;
  813. lockres_or_flags(lockres, OCFS2_LOCK_ATTACHED);
  814. lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
  815. }
  816. static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres,
  817. int level)
  818. {
  819. int needs_downconvert = 0;
  820. assert_spin_locked(&lockres->l_lock);
  821. if (level > lockres->l_blocking) {
  822. /* only schedule a downconvert if we haven't already scheduled
  823. * one that goes low enough to satisfy the level we're
  824. * blocking. this also catches the case where we get
  825. * duplicate BASTs */
  826. if (ocfs2_highest_compat_lock_level(level) <
  827. ocfs2_highest_compat_lock_level(lockres->l_blocking))
  828. needs_downconvert = 1;
  829. lockres->l_blocking = level;
  830. }
  831. mlog(ML_BASTS, "lockres %s, block %d, level %d, l_block %d, dwn %d\n",
  832. lockres->l_name, level, lockres->l_level, lockres->l_blocking,
  833. needs_downconvert);
  834. if (needs_downconvert)
  835. lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
  836. mlog(0, "needs_downconvert = %d\n", needs_downconvert);
  837. return needs_downconvert;
  838. }
  839. /*
  840. * OCFS2_LOCK_PENDING and l_pending_gen.
  841. *
  842. * Why does OCFS2_LOCK_PENDING exist? To close a race between setting
  843. * OCFS2_LOCK_BUSY and calling ocfs2_dlm_lock(). See ocfs2_unblock_lock()
  844. * for more details on the race.
  845. *
  846. * OCFS2_LOCK_PENDING closes the race quite nicely. However, it introduces
  847. * a race on itself. In o2dlm, we can get the ast before ocfs2_dlm_lock()
  848. * returns. The ast clears OCFS2_LOCK_BUSY, and must therefore clear
  849. * OCFS2_LOCK_PENDING at the same time. When ocfs2_dlm_lock() returns,
  850. * the caller is going to try to clear PENDING again. If nothing else is
  851. * happening, __lockres_clear_pending() sees PENDING is unset and does
  852. * nothing.
  853. *
  854. * But what if another path (eg downconvert thread) has just started a
  855. * new locking action? The other path has re-set PENDING. Our path
  856. * cannot clear PENDING, because that will re-open the original race
  857. * window.
  858. *
  859. * [Example]
  860. *
  861. * ocfs2_meta_lock()
  862. * ocfs2_cluster_lock()
  863. * set BUSY
  864. * set PENDING
  865. * drop l_lock
  866. * ocfs2_dlm_lock()
  867. * ocfs2_locking_ast() ocfs2_downconvert_thread()
  868. * clear PENDING ocfs2_unblock_lock()
  869. * take_l_lock
  870. * !BUSY
  871. * ocfs2_prepare_downconvert()
  872. * set BUSY
  873. * set PENDING
  874. * drop l_lock
  875. * take l_lock
  876. * clear PENDING
  877. * drop l_lock
  878. * <window>
  879. * ocfs2_dlm_lock()
  880. *
  881. * So as you can see, we now have a window where l_lock is not held,
  882. * PENDING is not set, and ocfs2_dlm_lock() has not been called.
  883. *
  884. * The core problem is that ocfs2_cluster_lock() has cleared the PENDING
  885. * set by ocfs2_prepare_downconvert(). That wasn't nice.
  886. *
  887. * To solve this we introduce l_pending_gen. A call to
  888. * lockres_clear_pending() will only do so when it is passed a generation
  889. * number that matches the lockres. lockres_set_pending() will return the
  890. * current generation number. When ocfs2_cluster_lock() goes to clear
  891. * PENDING, it passes the generation it got from set_pending(). In our
  892. * example above, the generation numbers will *not* match. Thus,
  893. * ocfs2_cluster_lock() will not clear the PENDING set by
  894. * ocfs2_prepare_downconvert().
  895. */
  896. /* Unlocked version for ocfs2_locking_ast() */
  897. static void __lockres_clear_pending(struct ocfs2_lock_res *lockres,
  898. unsigned int generation,
  899. struct ocfs2_super *osb)
  900. {
  901. assert_spin_locked(&lockres->l_lock);
  902. /*
  903. * The ast and locking functions can race us here. The winner
  904. * will clear pending, the loser will not.
  905. */
  906. if (!(lockres->l_flags & OCFS2_LOCK_PENDING) ||
  907. (lockres->l_pending_gen != generation))
  908. return;
  909. lockres_clear_flags(lockres, OCFS2_LOCK_PENDING);
  910. lockres->l_pending_gen++;
  911. /*
  912. * The downconvert thread may have skipped us because we
  913. * were PENDING. Wake it up.
  914. */
  915. if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
  916. ocfs2_wake_downconvert_thread(osb);
  917. }
  918. /* Locked version for callers of ocfs2_dlm_lock() */
  919. static void lockres_clear_pending(struct ocfs2_lock_res *lockres,
  920. unsigned int generation,
  921. struct ocfs2_super *osb)
  922. {
  923. unsigned long flags;
  924. spin_lock_irqsave(&lockres->l_lock, flags);
  925. __lockres_clear_pending(lockres, generation, osb);
  926. spin_unlock_irqrestore(&lockres->l_lock, flags);
  927. }
  928. static unsigned int lockres_set_pending(struct ocfs2_lock_res *lockres)
  929. {
  930. assert_spin_locked(&lockres->l_lock);
  931. BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
  932. lockres_or_flags(lockres, OCFS2_LOCK_PENDING);
  933. return lockres->l_pending_gen;
  934. }
  935. static void ocfs2_blocking_ast(struct ocfs2_dlm_lksb *lksb, int level)
  936. {
  937. struct ocfs2_lock_res *lockres = ocfs2_lksb_to_lock_res(lksb);
  938. struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
  939. int needs_downconvert;
  940. unsigned long flags;
  941. BUG_ON(level <= DLM_LOCK_NL);
  942. mlog(ML_BASTS, "BAST fired for lockres %s, blocking %d, level %d, "
  943. "type %s\n", lockres->l_name, level, lockres->l_level,
  944. ocfs2_lock_type_string(lockres->l_type));
  945. /*
  946. * We can skip the bast for locks which don't enable caching -
  947. * they'll be dropped at the earliest possible time anyway.
  948. */
  949. if (lockres->l_flags & OCFS2_LOCK_NOCACHE)
  950. return;
  951. spin_lock_irqsave(&lockres->l_lock, flags);
  952. needs_downconvert = ocfs2_generic_handle_bast(lockres, level);
  953. if (needs_downconvert)
  954. ocfs2_schedule_blocked_lock(osb, lockres);
  955. spin_unlock_irqrestore(&lockres->l_lock, flags);
  956. wake_up(&lockres->l_event);
  957. ocfs2_wake_downconvert_thread(osb);
  958. }
  959. static void ocfs2_locking_ast(struct ocfs2_dlm_lksb *lksb)
  960. {
  961. struct ocfs2_lock_res *lockres = ocfs2_lksb_to_lock_res(lksb);
  962. struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
  963. unsigned long flags;
  964. int status;
  965. spin_lock_irqsave(&lockres->l_lock, flags);
  966. status = ocfs2_dlm_lock_status(&lockres->l_lksb);
  967. if (status == -EAGAIN) {
  968. lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
  969. goto out;
  970. }
  971. if (status) {
  972. mlog(ML_ERROR, "lockres %s: lksb status value of %d!\n",
  973. lockres->l_name, status);
  974. spin_unlock_irqrestore(&lockres->l_lock, flags);
  975. return;
  976. }
  977. mlog(ML_BASTS, "AST fired for lockres %s, action %d, unlock %d, "
  978. "level %d => %d\n", lockres->l_name, lockres->l_action,
  979. lockres->l_unlock_action, lockres->l_level, lockres->l_requested);
  980. switch(lockres->l_action) {
  981. case OCFS2_AST_ATTACH:
  982. ocfs2_generic_handle_attach_action(lockres);
  983. lockres_clear_flags(lockres, OCFS2_LOCK_LOCAL);
  984. break;
  985. case OCFS2_AST_CONVERT:
  986. ocfs2_generic_handle_convert_action(lockres);
  987. break;
  988. case OCFS2_AST_DOWNCONVERT:
  989. ocfs2_generic_handle_downconvert_action(lockres);
  990. break;
  991. default:
  992. mlog(ML_ERROR, "lockres %s: AST fired with invalid action: %u, "
  993. "flags 0x%lx, unlock: %u\n",
  994. lockres->l_name, lockres->l_action, lockres->l_flags,
  995. lockres->l_unlock_action);
  996. BUG();
  997. }
  998. out:
  999. /* set it to something invalid so if we get called again we
  1000. * can catch it. */
  1001. lockres->l_action = OCFS2_AST_INVALID;
  1002. /* Did we try to cancel this lock? Clear that state */
  1003. if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT)
  1004. lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
  1005. /*
  1006. * We may have beaten the locking functions here. We certainly
  1007. * know that dlm_lock() has been called :-)
  1008. * Because we can't have two lock calls in flight at once, we
  1009. * can use lockres->l_pending_gen.
  1010. */
  1011. __lockres_clear_pending(lockres, lockres->l_pending_gen, osb);
  1012. wake_up(&lockres->l_event);
  1013. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1014. }
  1015. static void ocfs2_unlock_ast(struct ocfs2_dlm_lksb *lksb, int error)
  1016. {
  1017. struct ocfs2_lock_res *lockres = ocfs2_lksb_to_lock_res(lksb);
  1018. unsigned long flags;
  1019. mlog(ML_BASTS, "UNLOCK AST fired for lockres %s, action = %d\n",
  1020. lockres->l_name, lockres->l_unlock_action);
  1021. spin_lock_irqsave(&lockres->l_lock, flags);
  1022. if (error) {
  1023. mlog(ML_ERROR, "Dlm passes error %d for lock %s, "
  1024. "unlock_action %d\n", error, lockres->l_name,
  1025. lockres->l_unlock_action);
  1026. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1027. return;
  1028. }
  1029. switch(lockres->l_unlock_action) {
  1030. case OCFS2_UNLOCK_CANCEL_CONVERT:
  1031. mlog(0, "Cancel convert success for %s\n", lockres->l_name);
  1032. lockres->l_action = OCFS2_AST_INVALID;
  1033. /* Downconvert thread may have requeued this lock, we
  1034. * need to wake it. */
  1035. if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
  1036. ocfs2_wake_downconvert_thread(ocfs2_get_lockres_osb(lockres));
  1037. break;
  1038. case OCFS2_UNLOCK_DROP_LOCK:
  1039. lockres->l_level = DLM_LOCK_IV;
  1040. break;
  1041. default:
  1042. BUG();
  1043. }
  1044. lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
  1045. lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
  1046. wake_up(&lockres->l_event);
  1047. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1048. }
  1049. /*
  1050. * This is the filesystem locking protocol. It provides the lock handling
  1051. * hooks for the underlying DLM. It has a maximum version number.
  1052. * The version number allows interoperability with systems running at
  1053. * the same major number and an equal or smaller minor number.
  1054. *
  1055. * Whenever the filesystem does new things with locks (adds or removes a
  1056. * lock, orders them differently, does different things underneath a lock),
  1057. * the version must be changed. The protocol is negotiated when joining
  1058. * the dlm domain. A node may join the domain if its major version is
  1059. * identical to all other nodes and its minor version is greater than
  1060. * or equal to all other nodes. When its minor version is greater than
  1061. * the other nodes, it will run at the minor version specified by the
  1062. * other nodes.
  1063. *
  1064. * If a locking change is made that will not be compatible with older
  1065. * versions, the major number must be increased and the minor version set
  1066. * to zero. If a change merely adds a behavior that can be disabled when
  1067. * speaking to older versions, the minor version must be increased. If a
  1068. * change adds a fully backwards compatible change (eg, LVB changes that
  1069. * are just ignored by older versions), the version does not need to be
  1070. * updated.
  1071. */
  1072. static struct ocfs2_locking_protocol lproto = {
  1073. .lp_max_version = {
  1074. .pv_major = OCFS2_LOCKING_PROTOCOL_MAJOR,
  1075. .pv_minor = OCFS2_LOCKING_PROTOCOL_MINOR,
  1076. },
  1077. .lp_lock_ast = ocfs2_locking_ast,
  1078. .lp_blocking_ast = ocfs2_blocking_ast,
  1079. .lp_unlock_ast = ocfs2_unlock_ast,
  1080. };
  1081. void ocfs2_set_locking_protocol(void)
  1082. {
  1083. ocfs2_stack_glue_set_max_proto_version(&lproto.lp_max_version);
  1084. }
  1085. static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
  1086. int convert)
  1087. {
  1088. unsigned long flags;
  1089. spin_lock_irqsave(&lockres->l_lock, flags);
  1090. lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
  1091. lockres_clear_flags(lockres, OCFS2_LOCK_UPCONVERT_FINISHING);
  1092. if (convert)
  1093. lockres->l_action = OCFS2_AST_INVALID;
  1094. else
  1095. lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
  1096. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1097. wake_up(&lockres->l_event);
  1098. }
  1099. /* Note: If we detect another process working on the lock (i.e.,
  1100. * OCFS2_LOCK_BUSY), we'll bail out returning 0. It's up to the caller
  1101. * to do the right thing in that case.
  1102. */
  1103. static int ocfs2_lock_create(struct ocfs2_super *osb,
  1104. struct ocfs2_lock_res *lockres,
  1105. int level,
  1106. u32 dlm_flags)
  1107. {
  1108. int ret = 0;
  1109. unsigned long flags;
  1110. unsigned int gen;
  1111. mlog(0, "lock %s, level = %d, flags = %u\n", lockres->l_name, level,
  1112. dlm_flags);
  1113. spin_lock_irqsave(&lockres->l_lock, flags);
  1114. if ((lockres->l_flags & OCFS2_LOCK_ATTACHED) ||
  1115. (lockres->l_flags & OCFS2_LOCK_BUSY)) {
  1116. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1117. goto bail;
  1118. }
  1119. lockres->l_action = OCFS2_AST_ATTACH;
  1120. lockres->l_requested = level;
  1121. lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
  1122. gen = lockres_set_pending(lockres);
  1123. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1124. ret = ocfs2_dlm_lock(osb->cconn,
  1125. level,
  1126. &lockres->l_lksb,
  1127. dlm_flags,
  1128. lockres->l_name,
  1129. OCFS2_LOCK_ID_MAX_LEN - 1);
  1130. lockres_clear_pending(lockres, gen, osb);
  1131. if (ret) {
  1132. ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
  1133. ocfs2_recover_from_dlm_error(lockres, 1);
  1134. }
  1135. mlog(0, "lock %s, return from ocfs2_dlm_lock\n", lockres->l_name);
  1136. bail:
  1137. return ret;
  1138. }
  1139. static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res *lockres,
  1140. int flag)
  1141. {
  1142. unsigned long flags;
  1143. int ret;
  1144. spin_lock_irqsave(&lockres->l_lock, flags);
  1145. ret = lockres->l_flags & flag;
  1146. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1147. return ret;
  1148. }
  1149. static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res *lockres)
  1150. {
  1151. wait_event(lockres->l_event,
  1152. !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_BUSY));
  1153. }
  1154. static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res *lockres)
  1155. {
  1156. wait_event(lockres->l_event,
  1157. !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_REFRESHING));
  1158. }
  1159. /* predict what lock level we'll be dropping down to on behalf
  1160. * of another node, and return true if the currently wanted
  1161. * level will be compatible with it. */
  1162. static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
  1163. int wanted)
  1164. {
  1165. BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
  1166. return wanted <= ocfs2_highest_compat_lock_level(lockres->l_blocking);
  1167. }
  1168. static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter *mw)
  1169. {
  1170. INIT_LIST_HEAD(&mw->mw_item);
  1171. init_completion(&mw->mw_complete);
  1172. ocfs2_init_start_time(mw);
  1173. }
  1174. static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter *mw)
  1175. {
  1176. wait_for_completion(&mw->mw_complete);
  1177. /* Re-arm the completion in case we want to wait on it again */
  1178. reinit_completion(&mw->mw_complete);
  1179. return mw->mw_status;
  1180. }
  1181. static void lockres_add_mask_waiter(struct ocfs2_lock_res *lockres,
  1182. struct ocfs2_mask_waiter *mw,
  1183. unsigned long mask,
  1184. unsigned long goal)
  1185. {
  1186. BUG_ON(!list_empty(&mw->mw_item));
  1187. assert_spin_locked(&lockres->l_lock);
  1188. list_add_tail(&mw->mw_item, &lockres->l_mask_waiters);
  1189. mw->mw_mask = mask;
  1190. mw->mw_goal = goal;
  1191. }
  1192. /* returns 0 if the mw that was removed was already satisfied, -EBUSY
  1193. * if the mask still hadn't reached its goal */
  1194. static int __lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
  1195. struct ocfs2_mask_waiter *mw)
  1196. {
  1197. int ret = 0;
  1198. assert_spin_locked(&lockres->l_lock);
  1199. if (!list_empty(&mw->mw_item)) {
  1200. if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
  1201. ret = -EBUSY;
  1202. list_del_init(&mw->mw_item);
  1203. init_completion(&mw->mw_complete);
  1204. }
  1205. return ret;
  1206. }
  1207. static int lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
  1208. struct ocfs2_mask_waiter *mw)
  1209. {
  1210. unsigned long flags;
  1211. int ret = 0;
  1212. spin_lock_irqsave(&lockres->l_lock, flags);
  1213. ret = __lockres_remove_mask_waiter(lockres, mw);
  1214. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1215. return ret;
  1216. }
  1217. static int ocfs2_wait_for_mask_interruptible(struct ocfs2_mask_waiter *mw,
  1218. struct ocfs2_lock_res *lockres)
  1219. {
  1220. int ret;
  1221. ret = wait_for_completion_interruptible(&mw->mw_complete);
  1222. if (ret)
  1223. lockres_remove_mask_waiter(lockres, mw);
  1224. else
  1225. ret = mw->mw_status;
  1226. /* Re-arm the completion in case we want to wait on it again */
  1227. reinit_completion(&mw->mw_complete);
  1228. return ret;
  1229. }
  1230. static int __ocfs2_cluster_lock(struct ocfs2_super *osb,
  1231. struct ocfs2_lock_res *lockres,
  1232. int level,
  1233. u32 lkm_flags,
  1234. int arg_flags,
  1235. int l_subclass,
  1236. unsigned long caller_ip)
  1237. {
  1238. struct ocfs2_mask_waiter mw;
  1239. int wait, catch_signals = !(osb->s_mount_opt & OCFS2_MOUNT_NOINTR);
  1240. int ret = 0; /* gcc doesn't realize wait = 1 guarantees ret is set */
  1241. unsigned long flags;
  1242. unsigned int gen;
  1243. int noqueue_attempted = 0;
  1244. int dlm_locked = 0;
  1245. int kick_dc = 0;
  1246. if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED)) {
  1247. mlog_errno(-EINVAL);
  1248. return -EINVAL;
  1249. }
  1250. ocfs2_init_mask_waiter(&mw);
  1251. if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
  1252. lkm_flags |= DLM_LKF_VALBLK;
  1253. again:
  1254. wait = 0;
  1255. spin_lock_irqsave(&lockres->l_lock, flags);
  1256. if (catch_signals && signal_pending(current)) {
  1257. ret = -ERESTARTSYS;
  1258. goto unlock;
  1259. }
  1260. mlog_bug_on_msg(lockres->l_flags & OCFS2_LOCK_FREEING,
  1261. "Cluster lock called on freeing lockres %s! flags "
  1262. "0x%lx\n", lockres->l_name, lockres->l_flags);
  1263. /* We only compare against the currently granted level
  1264. * here. If the lock is blocked waiting on a downconvert,
  1265. * we'll get caught below. */
  1266. if (lockres->l_flags & OCFS2_LOCK_BUSY &&
  1267. level > lockres->l_level) {
  1268. /* is someone sitting in dlm_lock? If so, wait on
  1269. * them. */
  1270. lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
  1271. wait = 1;
  1272. goto unlock;
  1273. }
  1274. if (lockres->l_flags & OCFS2_LOCK_UPCONVERT_FINISHING) {
  1275. /*
  1276. * We've upconverted. If the lock now has a level we can
  1277. * work with, we take it. If, however, the lock is not at the
  1278. * required level, we go thru the full cycle. One way this could
  1279. * happen is if a process requesting an upconvert to PR is
  1280. * closely followed by another requesting upconvert to an EX.
  1281. * If the process requesting EX lands here, we want it to
  1282. * continue attempting to upconvert and let the process
  1283. * requesting PR take the lock.
  1284. * If multiple processes request upconvert to PR, the first one
  1285. * here will take the lock. The others will have to go thru the
  1286. * OCFS2_LOCK_BLOCKED check to ensure that there is no pending
  1287. * downconvert request.
  1288. */
  1289. if (level <= lockres->l_level)
  1290. goto update_holders;
  1291. }
  1292. if (lockres->l_flags & OCFS2_LOCK_BLOCKED &&
  1293. !ocfs2_may_continue_on_blocked_lock(lockres, level)) {
  1294. /* is the lock is currently blocked on behalf of
  1295. * another node */
  1296. lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BLOCKED, 0);
  1297. wait = 1;
  1298. goto unlock;
  1299. }
  1300. if (level > lockres->l_level) {
  1301. if (noqueue_attempted > 0) {
  1302. ret = -EAGAIN;
  1303. goto unlock;
  1304. }
  1305. if (lkm_flags & DLM_LKF_NOQUEUE)
  1306. noqueue_attempted = 1;
  1307. if (lockres->l_action != OCFS2_AST_INVALID)
  1308. mlog(ML_ERROR, "lockres %s has action %u pending\n",
  1309. lockres->l_name, lockres->l_action);
  1310. if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
  1311. lockres->l_action = OCFS2_AST_ATTACH;
  1312. lkm_flags &= ~DLM_LKF_CONVERT;
  1313. } else {
  1314. lockres->l_action = OCFS2_AST_CONVERT;
  1315. lkm_flags |= DLM_LKF_CONVERT;
  1316. }
  1317. lockres->l_requested = level;
  1318. lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
  1319. gen = lockres_set_pending(lockres);
  1320. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1321. BUG_ON(level == DLM_LOCK_IV);
  1322. BUG_ON(level == DLM_LOCK_NL);
  1323. mlog(ML_BASTS, "lockres %s, convert from %d to %d\n",
  1324. lockres->l_name, lockres->l_level, level);
  1325. /* call dlm_lock to upgrade lock now */
  1326. ret = ocfs2_dlm_lock(osb->cconn,
  1327. level,
  1328. &lockres->l_lksb,
  1329. lkm_flags,
  1330. lockres->l_name,
  1331. OCFS2_LOCK_ID_MAX_LEN - 1);
  1332. lockres_clear_pending(lockres, gen, osb);
  1333. if (ret) {
  1334. if (!(lkm_flags & DLM_LKF_NOQUEUE) ||
  1335. (ret != -EAGAIN)) {
  1336. ocfs2_log_dlm_error("ocfs2_dlm_lock",
  1337. ret, lockres);
  1338. }
  1339. ocfs2_recover_from_dlm_error(lockres, 1);
  1340. goto out;
  1341. }
  1342. dlm_locked = 1;
  1343. mlog(0, "lock %s, successful return from ocfs2_dlm_lock\n",
  1344. lockres->l_name);
  1345. /* At this point we've gone inside the dlm and need to
  1346. * complete our work regardless. */
  1347. catch_signals = 0;
  1348. /* wait for busy to clear and carry on */
  1349. goto again;
  1350. }
  1351. update_holders:
  1352. /* Ok, if we get here then we're good to go. */
  1353. ocfs2_inc_holders(lockres, level);
  1354. ret = 0;
  1355. unlock:
  1356. lockres_clear_flags(lockres, OCFS2_LOCK_UPCONVERT_FINISHING);
  1357. /* ocfs2_unblock_lock reques on seeing OCFS2_LOCK_UPCONVERT_FINISHING */
  1358. kick_dc = (lockres->l_flags & OCFS2_LOCK_BLOCKED);
  1359. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1360. if (kick_dc)
  1361. ocfs2_wake_downconvert_thread(osb);
  1362. out:
  1363. /*
  1364. * This is helping work around a lock inversion between the page lock
  1365. * and dlm locks. One path holds the page lock while calling aops
  1366. * which block acquiring dlm locks. The voting thread holds dlm
  1367. * locks while acquiring page locks while down converting data locks.
  1368. * This block is helping an aop path notice the inversion and back
  1369. * off to unlock its page lock before trying the dlm lock again.
  1370. */
  1371. if (wait && arg_flags & OCFS2_LOCK_NONBLOCK &&
  1372. mw.mw_mask & (OCFS2_LOCK_BUSY|OCFS2_LOCK_BLOCKED)) {
  1373. wait = 0;
  1374. spin_lock_irqsave(&lockres->l_lock, flags);
  1375. if (__lockres_remove_mask_waiter(lockres, &mw)) {
  1376. if (dlm_locked)
  1377. lockres_or_flags(lockres,
  1378. OCFS2_LOCK_NONBLOCK_FINISHED);
  1379. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1380. ret = -EAGAIN;
  1381. } else {
  1382. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1383. goto again;
  1384. }
  1385. }
  1386. if (wait) {
  1387. ret = ocfs2_wait_for_mask(&mw);
  1388. if (ret == 0)
  1389. goto again;
  1390. mlog_errno(ret);
  1391. }
  1392. ocfs2_update_lock_stats(lockres, level, &mw, ret);
  1393. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  1394. if (!ret && lockres->l_lockdep_map.key != NULL) {
  1395. if (level == DLM_LOCK_PR)
  1396. rwsem_acquire_read(&lockres->l_lockdep_map, l_subclass,
  1397. !!(arg_flags & OCFS2_META_LOCK_NOQUEUE),
  1398. caller_ip);
  1399. else
  1400. rwsem_acquire(&lockres->l_lockdep_map, l_subclass,
  1401. !!(arg_flags & OCFS2_META_LOCK_NOQUEUE),
  1402. caller_ip);
  1403. }
  1404. #endif
  1405. return ret;
  1406. }
  1407. static inline int ocfs2_cluster_lock(struct ocfs2_super *osb,
  1408. struct ocfs2_lock_res *lockres,
  1409. int level,
  1410. u32 lkm_flags,
  1411. int arg_flags)
  1412. {
  1413. return __ocfs2_cluster_lock(osb, lockres, level, lkm_flags, arg_flags,
  1414. 0, _RET_IP_);
  1415. }
  1416. static void __ocfs2_cluster_unlock(struct ocfs2_super *osb,
  1417. struct ocfs2_lock_res *lockres,
  1418. int level,
  1419. unsigned long caller_ip)
  1420. {
  1421. unsigned long flags;
  1422. spin_lock_irqsave(&lockres->l_lock, flags);
  1423. ocfs2_dec_holders(lockres, level);
  1424. ocfs2_downconvert_on_unlock(osb, lockres);
  1425. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1426. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  1427. if (lockres->l_lockdep_map.key != NULL)
  1428. rwsem_release(&lockres->l_lockdep_map, 1, caller_ip);
  1429. #endif
  1430. }
  1431. static int ocfs2_create_new_lock(struct ocfs2_super *osb,
  1432. struct ocfs2_lock_res *lockres,
  1433. int ex,
  1434. int local)
  1435. {
  1436. int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
  1437. unsigned long flags;
  1438. u32 lkm_flags = local ? DLM_LKF_LOCAL : 0;
  1439. spin_lock_irqsave(&lockres->l_lock, flags);
  1440. BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
  1441. lockres_or_flags(lockres, OCFS2_LOCK_LOCAL);
  1442. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1443. return ocfs2_lock_create(osb, lockres, level, lkm_flags);
  1444. }
  1445. /* Grants us an EX lock on the data and metadata resources, skipping
  1446. * the normal cluster directory lookup. Use this ONLY on newly created
  1447. * inodes which other nodes can't possibly see, and which haven't been
  1448. * hashed in the inode hash yet. This can give us a good performance
  1449. * increase as it'll skip the network broadcast normally associated
  1450. * with creating a new lock resource. */
  1451. int ocfs2_create_new_inode_locks(struct inode *inode)
  1452. {
  1453. int ret;
  1454. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1455. BUG_ON(!ocfs2_inode_is_new(inode));
  1456. mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
  1457. /* NOTE: That we don't increment any of the holder counts, nor
  1458. * do we add anything to a journal handle. Since this is
  1459. * supposed to be a new inode which the cluster doesn't know
  1460. * about yet, there is no need to. As far as the LVB handling
  1461. * is concerned, this is basically like acquiring an EX lock
  1462. * on a resource which has an invalid one -- we'll set it
  1463. * valid when we release the EX. */
  1464. ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_rw_lockres, 1, 1);
  1465. if (ret) {
  1466. mlog_errno(ret);
  1467. goto bail;
  1468. }
  1469. /*
  1470. * We don't want to use DLM_LKF_LOCAL on a meta data lock as they
  1471. * don't use a generation in their lock names.
  1472. */
  1473. ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_inode_lockres, 1, 0);
  1474. if (ret) {
  1475. mlog_errno(ret);
  1476. goto bail;
  1477. }
  1478. ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_open_lockres, 0, 0);
  1479. if (ret)
  1480. mlog_errno(ret);
  1481. bail:
  1482. return ret;
  1483. }
  1484. int ocfs2_rw_lock(struct inode *inode, int write)
  1485. {
  1486. int status, level;
  1487. struct ocfs2_lock_res *lockres;
  1488. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1489. mlog(0, "inode %llu take %s RW lock\n",
  1490. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1491. write ? "EXMODE" : "PRMODE");
  1492. if (ocfs2_mount_local(osb))
  1493. return 0;
  1494. lockres = &OCFS2_I(inode)->ip_rw_lockres;
  1495. level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
  1496. status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
  1497. if (status < 0)
  1498. mlog_errno(status);
  1499. return status;
  1500. }
  1501. int ocfs2_try_rw_lock(struct inode *inode, int write)
  1502. {
  1503. int status, level;
  1504. struct ocfs2_lock_res *lockres;
  1505. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1506. mlog(0, "inode %llu try to take %s RW lock\n",
  1507. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1508. write ? "EXMODE" : "PRMODE");
  1509. if (ocfs2_mount_local(osb))
  1510. return 0;
  1511. lockres = &OCFS2_I(inode)->ip_rw_lockres;
  1512. level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
  1513. status = ocfs2_cluster_lock(osb, lockres, level, DLM_LKF_NOQUEUE, 0);
  1514. return status;
  1515. }
  1516. void ocfs2_rw_unlock(struct inode *inode, int write)
  1517. {
  1518. int level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
  1519. struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_rw_lockres;
  1520. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1521. mlog(0, "inode %llu drop %s RW lock\n",
  1522. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1523. write ? "EXMODE" : "PRMODE");
  1524. if (!ocfs2_mount_local(osb))
  1525. ocfs2_cluster_unlock(osb, lockres, level);
  1526. }
  1527. /*
  1528. * ocfs2_open_lock always get PR mode lock.
  1529. */
  1530. int ocfs2_open_lock(struct inode *inode)
  1531. {
  1532. int status = 0;
  1533. struct ocfs2_lock_res *lockres;
  1534. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1535. mlog(0, "inode %llu take PRMODE open lock\n",
  1536. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  1537. if (ocfs2_is_hard_readonly(osb) || ocfs2_mount_local(osb))
  1538. goto out;
  1539. lockres = &OCFS2_I(inode)->ip_open_lockres;
  1540. status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_PR, 0, 0);
  1541. if (status < 0)
  1542. mlog_errno(status);
  1543. out:
  1544. return status;
  1545. }
  1546. int ocfs2_try_open_lock(struct inode *inode, int write)
  1547. {
  1548. int status = 0, level;
  1549. struct ocfs2_lock_res *lockres;
  1550. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1551. mlog(0, "inode %llu try to take %s open lock\n",
  1552. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1553. write ? "EXMODE" : "PRMODE");
  1554. if (ocfs2_is_hard_readonly(osb)) {
  1555. if (write)
  1556. status = -EROFS;
  1557. goto out;
  1558. }
  1559. if (ocfs2_mount_local(osb))
  1560. goto out;
  1561. lockres = &OCFS2_I(inode)->ip_open_lockres;
  1562. level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
  1563. /*
  1564. * The file system may already holding a PRMODE/EXMODE open lock.
  1565. * Since we pass DLM_LKF_NOQUEUE, the request won't block waiting on
  1566. * other nodes and the -EAGAIN will indicate to the caller that
  1567. * this inode is still in use.
  1568. */
  1569. status = ocfs2_cluster_lock(osb, lockres, level, DLM_LKF_NOQUEUE, 0);
  1570. out:
  1571. return status;
  1572. }
  1573. /*
  1574. * ocfs2_open_unlock unlock PR and EX mode open locks.
  1575. */
  1576. void ocfs2_open_unlock(struct inode *inode)
  1577. {
  1578. struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_open_lockres;
  1579. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1580. mlog(0, "inode %llu drop open lock\n",
  1581. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  1582. if (ocfs2_mount_local(osb))
  1583. goto out;
  1584. if(lockres->l_ro_holders)
  1585. ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_PR);
  1586. if(lockres->l_ex_holders)
  1587. ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
  1588. out:
  1589. return;
  1590. }
  1591. static int ocfs2_flock_handle_signal(struct ocfs2_lock_res *lockres,
  1592. int level)
  1593. {
  1594. int ret;
  1595. struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
  1596. unsigned long flags;
  1597. struct ocfs2_mask_waiter mw;
  1598. ocfs2_init_mask_waiter(&mw);
  1599. retry_cancel:
  1600. spin_lock_irqsave(&lockres->l_lock, flags);
  1601. if (lockres->l_flags & OCFS2_LOCK_BUSY) {
  1602. ret = ocfs2_prepare_cancel_convert(osb, lockres);
  1603. if (ret) {
  1604. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1605. ret = ocfs2_cancel_convert(osb, lockres);
  1606. if (ret < 0) {
  1607. mlog_errno(ret);
  1608. goto out;
  1609. }
  1610. goto retry_cancel;
  1611. }
  1612. lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
  1613. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1614. ocfs2_wait_for_mask(&mw);
  1615. goto retry_cancel;
  1616. }
  1617. ret = -ERESTARTSYS;
  1618. /*
  1619. * We may still have gotten the lock, in which case there's no
  1620. * point to restarting the syscall.
  1621. */
  1622. if (lockres->l_level == level)
  1623. ret = 0;
  1624. mlog(0, "Cancel returning %d. flags: 0x%lx, level: %d, act: %d\n", ret,
  1625. lockres->l_flags, lockres->l_level, lockres->l_action);
  1626. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1627. out:
  1628. return ret;
  1629. }
  1630. /*
  1631. * ocfs2_file_lock() and ocfs2_file_unlock() map to a single pair of
  1632. * flock() calls. The locking approach this requires is sufficiently
  1633. * different from all other cluster lock types that we implement a
  1634. * separate path to the "low-level" dlm calls. In particular:
  1635. *
  1636. * - No optimization of lock levels is done - we take at exactly
  1637. * what's been requested.
  1638. *
  1639. * - No lock caching is employed. We immediately downconvert to
  1640. * no-lock at unlock time. This also means flock locks never go on
  1641. * the blocking list).
  1642. *
  1643. * - Since userspace can trivially deadlock itself with flock, we make
  1644. * sure to allow cancellation of a misbehaving applications flock()
  1645. * request.
  1646. *
  1647. * - Access to any flock lockres doesn't require concurrency, so we
  1648. * can simplify the code by requiring the caller to guarantee
  1649. * serialization of dlmglue flock calls.
  1650. */
  1651. int ocfs2_file_lock(struct file *file, int ex, int trylock)
  1652. {
  1653. int ret, level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
  1654. unsigned int lkm_flags = trylock ? DLM_LKF_NOQUEUE : 0;
  1655. unsigned long flags;
  1656. struct ocfs2_file_private *fp = file->private_data;
  1657. struct ocfs2_lock_res *lockres = &fp->fp_flock;
  1658. struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
  1659. struct ocfs2_mask_waiter mw;
  1660. ocfs2_init_mask_waiter(&mw);
  1661. if ((lockres->l_flags & OCFS2_LOCK_BUSY) ||
  1662. (lockres->l_level > DLM_LOCK_NL)) {
  1663. mlog(ML_ERROR,
  1664. "File lock \"%s\" has busy or locked state: flags: 0x%lx, "
  1665. "level: %u\n", lockres->l_name, lockres->l_flags,
  1666. lockres->l_level);
  1667. return -EINVAL;
  1668. }
  1669. spin_lock_irqsave(&lockres->l_lock, flags);
  1670. if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
  1671. lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
  1672. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1673. /*
  1674. * Get the lock at NLMODE to start - that way we
  1675. * can cancel the upconvert request if need be.
  1676. */
  1677. ret = ocfs2_lock_create(osb, lockres, DLM_LOCK_NL, 0);
  1678. if (ret < 0) {
  1679. mlog_errno(ret);
  1680. goto out;
  1681. }
  1682. ret = ocfs2_wait_for_mask(&mw);
  1683. if (ret) {
  1684. mlog_errno(ret);
  1685. goto out;
  1686. }
  1687. spin_lock_irqsave(&lockres->l_lock, flags);
  1688. }
  1689. lockres->l_action = OCFS2_AST_CONVERT;
  1690. lkm_flags |= DLM_LKF_CONVERT;
  1691. lockres->l_requested = level;
  1692. lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
  1693. lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
  1694. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1695. ret = ocfs2_dlm_lock(osb->cconn, level, &lockres->l_lksb, lkm_flags,
  1696. lockres->l_name, OCFS2_LOCK_ID_MAX_LEN - 1);
  1697. if (ret) {
  1698. if (!trylock || (ret != -EAGAIN)) {
  1699. ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
  1700. ret = -EINVAL;
  1701. }
  1702. ocfs2_recover_from_dlm_error(lockres, 1);
  1703. lockres_remove_mask_waiter(lockres, &mw);
  1704. goto out;
  1705. }
  1706. ret = ocfs2_wait_for_mask_interruptible(&mw, lockres);
  1707. if (ret == -ERESTARTSYS) {
  1708. /*
  1709. * Userspace can cause deadlock itself with
  1710. * flock(). Current behavior locally is to allow the
  1711. * deadlock, but abort the system call if a signal is
  1712. * received. We follow this example, otherwise a
  1713. * poorly written program could sit in kernel until
  1714. * reboot.
  1715. *
  1716. * Handling this is a bit more complicated for Ocfs2
  1717. * though. We can't exit this function with an
  1718. * outstanding lock request, so a cancel convert is
  1719. * required. We intentionally overwrite 'ret' - if the
  1720. * cancel fails and the lock was granted, it's easier
  1721. * to just bubble success back up to the user.
  1722. */
  1723. ret = ocfs2_flock_handle_signal(lockres, level);
  1724. } else if (!ret && (level > lockres->l_level)) {
  1725. /* Trylock failed asynchronously */
  1726. BUG_ON(!trylock);
  1727. ret = -EAGAIN;
  1728. }
  1729. out:
  1730. mlog(0, "Lock: \"%s\" ex: %d, trylock: %d, returns: %d\n",
  1731. lockres->l_name, ex, trylock, ret);
  1732. return ret;
  1733. }
  1734. void ocfs2_file_unlock(struct file *file)
  1735. {
  1736. int ret;
  1737. unsigned int gen;
  1738. unsigned long flags;
  1739. struct ocfs2_file_private *fp = file->private_data;
  1740. struct ocfs2_lock_res *lockres = &fp->fp_flock;
  1741. struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
  1742. struct ocfs2_mask_waiter mw;
  1743. ocfs2_init_mask_waiter(&mw);
  1744. if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED))
  1745. return;
  1746. if (lockres->l_level == DLM_LOCK_NL)
  1747. return;
  1748. mlog(0, "Unlock: \"%s\" flags: 0x%lx, level: %d, act: %d\n",
  1749. lockres->l_name, lockres->l_flags, lockres->l_level,
  1750. lockres->l_action);
  1751. spin_lock_irqsave(&lockres->l_lock, flags);
  1752. /*
  1753. * Fake a blocking ast for the downconvert code.
  1754. */
  1755. lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
  1756. lockres->l_blocking = DLM_LOCK_EX;
  1757. gen = ocfs2_prepare_downconvert(lockres, DLM_LOCK_NL);
  1758. lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
  1759. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1760. ret = ocfs2_downconvert_lock(osb, lockres, DLM_LOCK_NL, 0, gen);
  1761. if (ret) {
  1762. mlog_errno(ret);
  1763. return;
  1764. }
  1765. ret = ocfs2_wait_for_mask(&mw);
  1766. if (ret)
  1767. mlog_errno(ret);
  1768. }
  1769. static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
  1770. struct ocfs2_lock_res *lockres)
  1771. {
  1772. int kick = 0;
  1773. /* If we know that another node is waiting on our lock, kick
  1774. * the downconvert thread * pre-emptively when we reach a release
  1775. * condition. */
  1776. if (lockres->l_flags & OCFS2_LOCK_BLOCKED) {
  1777. switch(lockres->l_blocking) {
  1778. case DLM_LOCK_EX:
  1779. if (!lockres->l_ex_holders && !lockres->l_ro_holders)
  1780. kick = 1;
  1781. break;
  1782. case DLM_LOCK_PR:
  1783. if (!lockres->l_ex_holders)
  1784. kick = 1;
  1785. break;
  1786. default:
  1787. BUG();
  1788. }
  1789. }
  1790. if (kick)
  1791. ocfs2_wake_downconvert_thread(osb);
  1792. }
  1793. #define OCFS2_SEC_BITS 34
  1794. #define OCFS2_SEC_SHIFT (64 - 34)
  1795. #define OCFS2_NSEC_MASK ((1ULL << OCFS2_SEC_SHIFT) - 1)
  1796. /* LVB only has room for 64 bits of time here so we pack it for
  1797. * now. */
  1798. static u64 ocfs2_pack_timespec(struct timespec *spec)
  1799. {
  1800. u64 res;
  1801. u64 sec = spec->tv_sec;
  1802. u32 nsec = spec->tv_nsec;
  1803. res = (sec << OCFS2_SEC_SHIFT) | (nsec & OCFS2_NSEC_MASK);
  1804. return res;
  1805. }
  1806. /* Call this with the lockres locked. I am reasonably sure we don't
  1807. * need ip_lock in this function as anyone who would be changing those
  1808. * values is supposed to be blocked in ocfs2_inode_lock right now. */
  1809. static void __ocfs2_stuff_meta_lvb(struct inode *inode)
  1810. {
  1811. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1812. struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
  1813. struct ocfs2_meta_lvb *lvb;
  1814. struct timespec ts;
  1815. lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
  1816. /*
  1817. * Invalidate the LVB of a deleted inode - this way other
  1818. * nodes are forced to go to disk and discover the new inode
  1819. * status.
  1820. */
  1821. if (oi->ip_flags & OCFS2_INODE_DELETED) {
  1822. lvb->lvb_version = 0;
  1823. goto out;
  1824. }
  1825. lvb->lvb_version = OCFS2_LVB_VERSION;
  1826. lvb->lvb_isize = cpu_to_be64(i_size_read(inode));
  1827. lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters);
  1828. lvb->lvb_iuid = cpu_to_be32(i_uid_read(inode));
  1829. lvb->lvb_igid = cpu_to_be32(i_gid_read(inode));
  1830. lvb->lvb_imode = cpu_to_be16(inode->i_mode);
  1831. lvb->lvb_inlink = cpu_to_be16(inode->i_nlink);
  1832. ts = timespec64_to_timespec(inode->i_atime);
  1833. lvb->lvb_iatime_packed =
  1834. cpu_to_be64(ocfs2_pack_timespec(&ts));
  1835. ts = timespec64_to_timespec(inode->i_ctime);
  1836. lvb->lvb_ictime_packed =
  1837. cpu_to_be64(ocfs2_pack_timespec(&ts));
  1838. ts = timespec64_to_timespec(inode->i_mtime);
  1839. lvb->lvb_imtime_packed =
  1840. cpu_to_be64(ocfs2_pack_timespec(&ts));
  1841. lvb->lvb_iattr = cpu_to_be32(oi->ip_attr);
  1842. lvb->lvb_idynfeatures = cpu_to_be16(oi->ip_dyn_features);
  1843. lvb->lvb_igeneration = cpu_to_be32(inode->i_generation);
  1844. out:
  1845. mlog_meta_lvb(0, lockres);
  1846. }
  1847. static void ocfs2_unpack_timespec(struct timespec *spec,
  1848. u64 packed_time)
  1849. {
  1850. spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
  1851. spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
  1852. }
  1853. static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
  1854. {
  1855. struct timespec ts;
  1856. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1857. struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
  1858. struct ocfs2_meta_lvb *lvb;
  1859. mlog_meta_lvb(0, lockres);
  1860. lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
  1861. /* We're safe here without the lockres lock... */
  1862. spin_lock(&oi->ip_lock);
  1863. oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters);
  1864. i_size_write(inode, be64_to_cpu(lvb->lvb_isize));
  1865. oi->ip_attr = be32_to_cpu(lvb->lvb_iattr);
  1866. oi->ip_dyn_features = be16_to_cpu(lvb->lvb_idynfeatures);
  1867. ocfs2_set_inode_flags(inode);
  1868. /* fast-symlinks are a special case */
  1869. if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
  1870. inode->i_blocks = 0;
  1871. else
  1872. inode->i_blocks = ocfs2_inode_sector_count(inode);
  1873. i_uid_write(inode, be32_to_cpu(lvb->lvb_iuid));
  1874. i_gid_write(inode, be32_to_cpu(lvb->lvb_igid));
  1875. inode->i_mode = be16_to_cpu(lvb->lvb_imode);
  1876. set_nlink(inode, be16_to_cpu(lvb->lvb_inlink));
  1877. ocfs2_unpack_timespec(&ts,
  1878. be64_to_cpu(lvb->lvb_iatime_packed));
  1879. inode->i_atime = timespec_to_timespec64(ts);
  1880. ocfs2_unpack_timespec(&ts,
  1881. be64_to_cpu(lvb->lvb_imtime_packed));
  1882. inode->i_mtime = timespec_to_timespec64(ts);
  1883. ocfs2_unpack_timespec(&ts,
  1884. be64_to_cpu(lvb->lvb_ictime_packed));
  1885. inode->i_ctime = timespec_to_timespec64(ts);
  1886. spin_unlock(&oi->ip_lock);
  1887. }
  1888. static inline int ocfs2_meta_lvb_is_trustable(struct inode *inode,
  1889. struct ocfs2_lock_res *lockres)
  1890. {
  1891. struct ocfs2_meta_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
  1892. if (ocfs2_dlm_lvb_valid(&lockres->l_lksb)
  1893. && lvb->lvb_version == OCFS2_LVB_VERSION
  1894. && be32_to_cpu(lvb->lvb_igeneration) == inode->i_generation)
  1895. return 1;
  1896. return 0;
  1897. }
  1898. /* Determine whether a lock resource needs to be refreshed, and
  1899. * arbitrate who gets to refresh it.
  1900. *
  1901. * 0 means no refresh needed.
  1902. *
  1903. * > 0 means you need to refresh this and you MUST call
  1904. * ocfs2_complete_lock_res_refresh afterwards. */
  1905. static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
  1906. {
  1907. unsigned long flags;
  1908. int status = 0;
  1909. refresh_check:
  1910. spin_lock_irqsave(&lockres->l_lock, flags);
  1911. if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
  1912. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1913. goto bail;
  1914. }
  1915. if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
  1916. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1917. ocfs2_wait_on_refreshing_lock(lockres);
  1918. goto refresh_check;
  1919. }
  1920. /* Ok, I'll be the one to refresh this lock. */
  1921. lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
  1922. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1923. status = 1;
  1924. bail:
  1925. mlog(0, "status %d\n", status);
  1926. return status;
  1927. }
  1928. /* If status is non zero, I'll mark it as not being in refresh
  1929. * anymroe, but i won't clear the needs refresh flag. */
  1930. static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
  1931. int status)
  1932. {
  1933. unsigned long flags;
  1934. spin_lock_irqsave(&lockres->l_lock, flags);
  1935. lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
  1936. if (!status)
  1937. lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
  1938. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1939. wake_up(&lockres->l_event);
  1940. }
  1941. /* may or may not return a bh if it went to disk. */
  1942. static int ocfs2_inode_lock_update(struct inode *inode,
  1943. struct buffer_head **bh)
  1944. {
  1945. int status = 0;
  1946. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1947. struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
  1948. struct ocfs2_dinode *fe;
  1949. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1950. if (ocfs2_mount_local(osb))
  1951. goto bail;
  1952. spin_lock(&oi->ip_lock);
  1953. if (oi->ip_flags & OCFS2_INODE_DELETED) {
  1954. mlog(0, "Orphaned inode %llu was deleted while we "
  1955. "were waiting on a lock. ip_flags = 0x%x\n",
  1956. (unsigned long long)oi->ip_blkno, oi->ip_flags);
  1957. spin_unlock(&oi->ip_lock);
  1958. status = -ENOENT;
  1959. goto bail;
  1960. }
  1961. spin_unlock(&oi->ip_lock);
  1962. if (!ocfs2_should_refresh_lock_res(lockres))
  1963. goto bail;
  1964. /* This will discard any caching information we might have had
  1965. * for the inode metadata. */
  1966. ocfs2_metadata_cache_purge(INODE_CACHE(inode));
  1967. ocfs2_extent_map_trunc(inode, 0);
  1968. if (ocfs2_meta_lvb_is_trustable(inode, lockres)) {
  1969. mlog(0, "Trusting LVB on inode %llu\n",
  1970. (unsigned long long)oi->ip_blkno);
  1971. ocfs2_refresh_inode_from_lvb(inode);
  1972. } else {
  1973. /* Boo, we have to go to disk. */
  1974. /* read bh, cast, ocfs2_refresh_inode */
  1975. status = ocfs2_read_inode_block(inode, bh);
  1976. if (status < 0) {
  1977. mlog_errno(status);
  1978. goto bail_refresh;
  1979. }
  1980. fe = (struct ocfs2_dinode *) (*bh)->b_data;
  1981. /* This is a good chance to make sure we're not
  1982. * locking an invalid object. ocfs2_read_inode_block()
  1983. * already checked that the inode block is sane.
  1984. *
  1985. * We bug on a stale inode here because we checked
  1986. * above whether it was wiped from disk. The wiping
  1987. * node provides a guarantee that we receive that
  1988. * message and can mark the inode before dropping any
  1989. * locks associated with it. */
  1990. mlog_bug_on_msg(inode->i_generation !=
  1991. le32_to_cpu(fe->i_generation),
  1992. "Invalid dinode %llu disk generation: %u "
  1993. "inode->i_generation: %u\n",
  1994. (unsigned long long)oi->ip_blkno,
  1995. le32_to_cpu(fe->i_generation),
  1996. inode->i_generation);
  1997. mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
  1998. !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
  1999. "Stale dinode %llu dtime: %llu flags: 0x%x\n",
  2000. (unsigned long long)oi->ip_blkno,
  2001. (unsigned long long)le64_to_cpu(fe->i_dtime),
  2002. le32_to_cpu(fe->i_flags));
  2003. ocfs2_refresh_inode(inode, fe);
  2004. ocfs2_track_lock_refresh(lockres);
  2005. }
  2006. status = 0;
  2007. bail_refresh:
  2008. ocfs2_complete_lock_res_refresh(lockres, status);
  2009. bail:
  2010. return status;
  2011. }
  2012. static int ocfs2_assign_bh(struct inode *inode,
  2013. struct buffer_head **ret_bh,
  2014. struct buffer_head *passed_bh)
  2015. {
  2016. int status;
  2017. if (passed_bh) {
  2018. /* Ok, the update went to disk for us, use the
  2019. * returned bh. */
  2020. *ret_bh = passed_bh;
  2021. get_bh(*ret_bh);
  2022. return 0;
  2023. }
  2024. status = ocfs2_read_inode_block(inode, ret_bh);
  2025. if (status < 0)
  2026. mlog_errno(status);
  2027. return status;
  2028. }
  2029. /*
  2030. * returns < 0 error if the callback will never be called, otherwise
  2031. * the result of the lock will be communicated via the callback.
  2032. */
  2033. int ocfs2_inode_lock_full_nested(struct inode *inode,
  2034. struct buffer_head **ret_bh,
  2035. int ex,
  2036. int arg_flags,
  2037. int subclass)
  2038. {
  2039. int status, level, acquired;
  2040. u32 dlm_flags;
  2041. struct ocfs2_lock_res *lockres = NULL;
  2042. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  2043. struct buffer_head *local_bh = NULL;
  2044. mlog(0, "inode %llu, take %s META lock\n",
  2045. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  2046. ex ? "EXMODE" : "PRMODE");
  2047. status = 0;
  2048. acquired = 0;
  2049. /* We'll allow faking a readonly metadata lock for
  2050. * rodevices. */
  2051. if (ocfs2_is_hard_readonly(osb)) {
  2052. if (ex)
  2053. status = -EROFS;
  2054. goto getbh;
  2055. }
  2056. if ((arg_flags & OCFS2_META_LOCK_GETBH) ||
  2057. ocfs2_mount_local(osb))
  2058. goto update;
  2059. if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
  2060. ocfs2_wait_for_recovery(osb);
  2061. lockres = &OCFS2_I(inode)->ip_inode_lockres;
  2062. level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
  2063. dlm_flags = 0;
  2064. if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
  2065. dlm_flags |= DLM_LKF_NOQUEUE;
  2066. status = __ocfs2_cluster_lock(osb, lockres, level, dlm_flags,
  2067. arg_flags, subclass, _RET_IP_);
  2068. if (status < 0) {
  2069. if (status != -EAGAIN)
  2070. mlog_errno(status);
  2071. goto bail;
  2072. }
  2073. /* Notify the error cleanup path to drop the cluster lock. */
  2074. acquired = 1;
  2075. /* We wait twice because a node may have died while we were in
  2076. * the lower dlm layers. The second time though, we've
  2077. * committed to owning this lock so we don't allow signals to
  2078. * abort the operation. */
  2079. if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
  2080. ocfs2_wait_for_recovery(osb);
  2081. update:
  2082. /*
  2083. * We only see this flag if we're being called from
  2084. * ocfs2_read_locked_inode(). It means we're locking an inode
  2085. * which hasn't been populated yet, so clear the refresh flag
  2086. * and let the caller handle it.
  2087. */
  2088. if (inode->i_state & I_NEW) {
  2089. status = 0;
  2090. if (lockres)
  2091. ocfs2_complete_lock_res_refresh(lockres, 0);
  2092. goto bail;
  2093. }
  2094. /* This is fun. The caller may want a bh back, or it may
  2095. * not. ocfs2_inode_lock_update definitely wants one in, but
  2096. * may or may not read one, depending on what's in the
  2097. * LVB. The result of all of this is that we've *only* gone to
  2098. * disk if we have to, so the complexity is worthwhile. */
  2099. status = ocfs2_inode_lock_update(inode, &local_bh);
  2100. if (status < 0) {
  2101. if (status != -ENOENT)
  2102. mlog_errno(status);
  2103. goto bail;
  2104. }
  2105. getbh:
  2106. if (ret_bh) {
  2107. status = ocfs2_assign_bh(inode, ret_bh, local_bh);
  2108. if (status < 0) {
  2109. mlog_errno(status);
  2110. goto bail;
  2111. }
  2112. }
  2113. bail:
  2114. if (status < 0) {
  2115. if (ret_bh && (*ret_bh)) {
  2116. brelse(*ret_bh);
  2117. *ret_bh = NULL;
  2118. }
  2119. if (acquired)
  2120. ocfs2_inode_unlock(inode, ex);
  2121. }
  2122. if (local_bh)
  2123. brelse(local_bh);
  2124. return status;
  2125. }
  2126. /*
  2127. * This is working around a lock inversion between tasks acquiring DLM
  2128. * locks while holding a page lock and the downconvert thread which
  2129. * blocks dlm lock acquiry while acquiring page locks.
  2130. *
  2131. * ** These _with_page variantes are only intended to be called from aop
  2132. * methods that hold page locks and return a very specific *positive* error
  2133. * code that aop methods pass up to the VFS -- test for errors with != 0. **
  2134. *
  2135. * The DLM is called such that it returns -EAGAIN if it would have
  2136. * blocked waiting for the downconvert thread. In that case we unlock
  2137. * our page so the downconvert thread can make progress. Once we've
  2138. * done this we have to return AOP_TRUNCATED_PAGE so the aop method
  2139. * that called us can bubble that back up into the VFS who will then
  2140. * immediately retry the aop call.
  2141. */
  2142. int ocfs2_inode_lock_with_page(struct inode *inode,
  2143. struct buffer_head **ret_bh,
  2144. int ex,
  2145. struct page *page)
  2146. {
  2147. int ret;
  2148. ret = ocfs2_inode_lock_full(inode, ret_bh, ex, OCFS2_LOCK_NONBLOCK);
  2149. if (ret == -EAGAIN) {
  2150. unlock_page(page);
  2151. /*
  2152. * If we can't get inode lock immediately, we should not return
  2153. * directly here, since this will lead to a softlockup problem.
  2154. * The method is to get a blocking lock and immediately unlock
  2155. * before returning, this can avoid CPU resource waste due to
  2156. * lots of retries, and benefits fairness in getting lock.
  2157. */
  2158. if (ocfs2_inode_lock(inode, ret_bh, ex) == 0)
  2159. ocfs2_inode_unlock(inode, ex);
  2160. ret = AOP_TRUNCATED_PAGE;
  2161. }
  2162. return ret;
  2163. }
  2164. int ocfs2_inode_lock_atime(struct inode *inode,
  2165. struct vfsmount *vfsmnt,
  2166. int *level, int wait)
  2167. {
  2168. int ret;
  2169. if (wait)
  2170. ret = ocfs2_inode_lock(inode, NULL, 0);
  2171. else
  2172. ret = ocfs2_try_inode_lock(inode, NULL, 0);
  2173. if (ret < 0) {
  2174. if (ret != -EAGAIN)
  2175. mlog_errno(ret);
  2176. return ret;
  2177. }
  2178. /*
  2179. * If we should update atime, we will get EX lock,
  2180. * otherwise we just get PR lock.
  2181. */
  2182. if (ocfs2_should_update_atime(inode, vfsmnt)) {
  2183. struct buffer_head *bh = NULL;
  2184. ocfs2_inode_unlock(inode, 0);
  2185. if (wait)
  2186. ret = ocfs2_inode_lock(inode, &bh, 1);
  2187. else
  2188. ret = ocfs2_try_inode_lock(inode, &bh, 1);
  2189. if (ret < 0) {
  2190. if (ret != -EAGAIN)
  2191. mlog_errno(ret);
  2192. return ret;
  2193. }
  2194. *level = 1;
  2195. if (ocfs2_should_update_atime(inode, vfsmnt))
  2196. ocfs2_update_inode_atime(inode, bh);
  2197. if (bh)
  2198. brelse(bh);
  2199. } else
  2200. *level = 0;
  2201. return ret;
  2202. }
  2203. void ocfs2_inode_unlock(struct inode *inode,
  2204. int ex)
  2205. {
  2206. int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
  2207. struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_inode_lockres;
  2208. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  2209. mlog(0, "inode %llu drop %s META lock\n",
  2210. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  2211. ex ? "EXMODE" : "PRMODE");
  2212. if (!ocfs2_is_hard_readonly(osb) &&
  2213. !ocfs2_mount_local(osb))
  2214. ocfs2_cluster_unlock(osb, lockres, level);
  2215. }
  2216. /*
  2217. * This _tracker variantes are introduced to deal with the recursive cluster
  2218. * locking issue. The idea is to keep track of a lock holder on the stack of
  2219. * the current process. If there's a lock holder on the stack, we know the
  2220. * task context is already protected by cluster locking. Currently, they're
  2221. * used in some VFS entry routines.
  2222. *
  2223. * return < 0 on error, return == 0 if there's no lock holder on the stack
  2224. * before this call, return == 1 if this call would be a recursive locking.
  2225. * return == -1 if this lock attempt will cause an upgrade which is forbidden.
  2226. *
  2227. * When taking lock levels into account,we face some different situations.
  2228. *
  2229. * 1. no lock is held
  2230. * In this case, just lock the inode as requested and return 0
  2231. *
  2232. * 2. We are holding a lock
  2233. * For this situation, things diverges into several cases
  2234. *
  2235. * wanted holding what to do
  2236. * ex ex see 2.1 below
  2237. * ex pr see 2.2 below
  2238. * pr ex see 2.1 below
  2239. * pr pr see 2.1 below
  2240. *
  2241. * 2.1 lock level that is been held is compatible
  2242. * with the wanted level, so no lock action will be tacken.
  2243. *
  2244. * 2.2 Otherwise, an upgrade is needed, but it is forbidden.
  2245. *
  2246. * Reason why upgrade within a process is forbidden is that
  2247. * lock upgrade may cause dead lock. The following illustrates
  2248. * how it happens.
  2249. *
  2250. * thread on node1 thread on node2
  2251. * ocfs2_inode_lock_tracker(ex=0)
  2252. *
  2253. * <====== ocfs2_inode_lock_tracker(ex=1)
  2254. *
  2255. * ocfs2_inode_lock_tracker(ex=1)
  2256. */
  2257. int ocfs2_inode_lock_tracker(struct inode *inode,
  2258. struct buffer_head **ret_bh,
  2259. int ex,
  2260. struct ocfs2_lock_holder *oh)
  2261. {
  2262. int status = 0;
  2263. struct ocfs2_lock_res *lockres;
  2264. struct ocfs2_lock_holder *tmp_oh;
  2265. struct pid *pid = task_pid(current);
  2266. lockres = &OCFS2_I(inode)->ip_inode_lockres;
  2267. tmp_oh = ocfs2_pid_holder(lockres, pid);
  2268. if (!tmp_oh) {
  2269. /*
  2270. * This corresponds to the case 1.
  2271. * We haven't got any lock before.
  2272. */
  2273. status = ocfs2_inode_lock_full(inode, ret_bh, ex, 0);
  2274. if (status < 0) {
  2275. if (status != -ENOENT)
  2276. mlog_errno(status);
  2277. return status;
  2278. }
  2279. oh->oh_ex = ex;
  2280. ocfs2_add_holder(lockres, oh);
  2281. return 0;
  2282. }
  2283. if (unlikely(ex && !tmp_oh->oh_ex)) {
  2284. /*
  2285. * case 2.2 upgrade may cause dead lock, forbid it.
  2286. */
  2287. mlog(ML_ERROR, "Recursive locking is not permitted to "
  2288. "upgrade to EX level from PR level.\n");
  2289. dump_stack();
  2290. return -EINVAL;
  2291. }
  2292. /*
  2293. * case 2.1 OCFS2_META_LOCK_GETBH flag make ocfs2_inode_lock_full.
  2294. * ignore the lock level and just update it.
  2295. */
  2296. if (ret_bh) {
  2297. status = ocfs2_inode_lock_full(inode, ret_bh, ex,
  2298. OCFS2_META_LOCK_GETBH);
  2299. if (status < 0) {
  2300. if (status != -ENOENT)
  2301. mlog_errno(status);
  2302. return status;
  2303. }
  2304. }
  2305. return tmp_oh ? 1 : 0;
  2306. }
  2307. void ocfs2_inode_unlock_tracker(struct inode *inode,
  2308. int ex,
  2309. struct ocfs2_lock_holder *oh,
  2310. int had_lock)
  2311. {
  2312. struct ocfs2_lock_res *lockres;
  2313. lockres = &OCFS2_I(inode)->ip_inode_lockres;
  2314. /* had_lock means that the currect process already takes the cluster
  2315. * lock previously.
  2316. * If had_lock is 1, we have nothing to do here.
  2317. * If had_lock is 0, we will release the lock.
  2318. */
  2319. if (!had_lock) {
  2320. ocfs2_inode_unlock(inode, oh->oh_ex);
  2321. ocfs2_remove_holder(lockres, oh);
  2322. }
  2323. }
  2324. int ocfs2_orphan_scan_lock(struct ocfs2_super *osb, u32 *seqno)
  2325. {
  2326. struct ocfs2_lock_res *lockres;
  2327. struct ocfs2_orphan_scan_lvb *lvb;
  2328. int status = 0;
  2329. if (ocfs2_is_hard_readonly(osb))
  2330. return -EROFS;
  2331. if (ocfs2_mount_local(osb))
  2332. return 0;
  2333. lockres = &osb->osb_orphan_scan.os_lockres;
  2334. status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX, 0, 0);
  2335. if (status < 0)
  2336. return status;
  2337. lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
  2338. if (ocfs2_dlm_lvb_valid(&lockres->l_lksb) &&
  2339. lvb->lvb_version == OCFS2_ORPHAN_LVB_VERSION)
  2340. *seqno = be32_to_cpu(lvb->lvb_os_seqno);
  2341. else
  2342. *seqno = osb->osb_orphan_scan.os_seqno + 1;
  2343. return status;
  2344. }
  2345. void ocfs2_orphan_scan_unlock(struct ocfs2_super *osb, u32 seqno)
  2346. {
  2347. struct ocfs2_lock_res *lockres;
  2348. struct ocfs2_orphan_scan_lvb *lvb;
  2349. if (!ocfs2_is_hard_readonly(osb) && !ocfs2_mount_local(osb)) {
  2350. lockres = &osb->osb_orphan_scan.os_lockres;
  2351. lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
  2352. lvb->lvb_version = OCFS2_ORPHAN_LVB_VERSION;
  2353. lvb->lvb_os_seqno = cpu_to_be32(seqno);
  2354. ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
  2355. }
  2356. }
  2357. int ocfs2_super_lock(struct ocfs2_super *osb,
  2358. int ex)
  2359. {
  2360. int status = 0;
  2361. int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
  2362. struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
  2363. if (ocfs2_is_hard_readonly(osb))
  2364. return -EROFS;
  2365. if (ocfs2_mount_local(osb))
  2366. goto bail;
  2367. status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
  2368. if (status < 0) {
  2369. mlog_errno(status);
  2370. goto bail;
  2371. }
  2372. /* The super block lock path is really in the best position to
  2373. * know when resources covered by the lock need to be
  2374. * refreshed, so we do it here. Of course, making sense of
  2375. * everything is up to the caller :) */
  2376. status = ocfs2_should_refresh_lock_res(lockres);
  2377. if (status) {
  2378. status = ocfs2_refresh_slot_info(osb);
  2379. ocfs2_complete_lock_res_refresh(lockres, status);
  2380. if (status < 0) {
  2381. ocfs2_cluster_unlock(osb, lockres, level);
  2382. mlog_errno(status);
  2383. }
  2384. ocfs2_track_lock_refresh(lockres);
  2385. }
  2386. bail:
  2387. return status;
  2388. }
  2389. void ocfs2_super_unlock(struct ocfs2_super *osb,
  2390. int ex)
  2391. {
  2392. int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
  2393. struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
  2394. if (!ocfs2_mount_local(osb))
  2395. ocfs2_cluster_unlock(osb, lockres, level);
  2396. }
  2397. int ocfs2_rename_lock(struct ocfs2_super *osb)
  2398. {
  2399. int status;
  2400. struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
  2401. if (ocfs2_is_hard_readonly(osb))
  2402. return -EROFS;
  2403. if (ocfs2_mount_local(osb))
  2404. return 0;
  2405. status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX, 0, 0);
  2406. if (status < 0)
  2407. mlog_errno(status);
  2408. return status;
  2409. }
  2410. void ocfs2_rename_unlock(struct ocfs2_super *osb)
  2411. {
  2412. struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
  2413. if (!ocfs2_mount_local(osb))
  2414. ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
  2415. }
  2416. int ocfs2_nfs_sync_lock(struct ocfs2_super *osb, int ex)
  2417. {
  2418. int status;
  2419. struct ocfs2_lock_res *lockres = &osb->osb_nfs_sync_lockres;
  2420. if (ocfs2_is_hard_readonly(osb))
  2421. return -EROFS;
  2422. if (ocfs2_mount_local(osb))
  2423. return 0;
  2424. status = ocfs2_cluster_lock(osb, lockres, ex ? LKM_EXMODE : LKM_PRMODE,
  2425. 0, 0);
  2426. if (status < 0)
  2427. mlog(ML_ERROR, "lock on nfs sync lock failed %d\n", status);
  2428. return status;
  2429. }
  2430. void ocfs2_nfs_sync_unlock(struct ocfs2_super *osb, int ex)
  2431. {
  2432. struct ocfs2_lock_res *lockres = &osb->osb_nfs_sync_lockres;
  2433. if (!ocfs2_mount_local(osb))
  2434. ocfs2_cluster_unlock(osb, lockres,
  2435. ex ? LKM_EXMODE : LKM_PRMODE);
  2436. }
  2437. int ocfs2_trim_fs_lock(struct ocfs2_super *osb,
  2438. struct ocfs2_trim_fs_info *info, int trylock)
  2439. {
  2440. int status;
  2441. struct ocfs2_trim_fs_lvb *lvb;
  2442. struct ocfs2_lock_res *lockres = &osb->osb_trim_fs_lockres;
  2443. if (info)
  2444. info->tf_valid = 0;
  2445. if (ocfs2_is_hard_readonly(osb))
  2446. return -EROFS;
  2447. if (ocfs2_mount_local(osb))
  2448. return 0;
  2449. status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX,
  2450. trylock ? DLM_LKF_NOQUEUE : 0, 0);
  2451. if (status < 0) {
  2452. if (status != -EAGAIN)
  2453. mlog_errno(status);
  2454. return status;
  2455. }
  2456. if (info) {
  2457. lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
  2458. if (ocfs2_dlm_lvb_valid(&lockres->l_lksb) &&
  2459. lvb->lvb_version == OCFS2_TRIMFS_LVB_VERSION) {
  2460. info->tf_valid = 1;
  2461. info->tf_success = lvb->lvb_success;
  2462. info->tf_nodenum = be32_to_cpu(lvb->lvb_nodenum);
  2463. info->tf_start = be64_to_cpu(lvb->lvb_start);
  2464. info->tf_len = be64_to_cpu(lvb->lvb_len);
  2465. info->tf_minlen = be64_to_cpu(lvb->lvb_minlen);
  2466. info->tf_trimlen = be64_to_cpu(lvb->lvb_trimlen);
  2467. }
  2468. }
  2469. return status;
  2470. }
  2471. void ocfs2_trim_fs_unlock(struct ocfs2_super *osb,
  2472. struct ocfs2_trim_fs_info *info)
  2473. {
  2474. struct ocfs2_trim_fs_lvb *lvb;
  2475. struct ocfs2_lock_res *lockres = &osb->osb_trim_fs_lockres;
  2476. if (ocfs2_mount_local(osb))
  2477. return;
  2478. if (info) {
  2479. lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
  2480. lvb->lvb_version = OCFS2_TRIMFS_LVB_VERSION;
  2481. lvb->lvb_success = info->tf_success;
  2482. lvb->lvb_nodenum = cpu_to_be32(info->tf_nodenum);
  2483. lvb->lvb_start = cpu_to_be64(info->tf_start);
  2484. lvb->lvb_len = cpu_to_be64(info->tf_len);
  2485. lvb->lvb_minlen = cpu_to_be64(info->tf_minlen);
  2486. lvb->lvb_trimlen = cpu_to_be64(info->tf_trimlen);
  2487. }
  2488. ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
  2489. }
  2490. int ocfs2_dentry_lock(struct dentry *dentry, int ex)
  2491. {
  2492. int ret;
  2493. int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
  2494. struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
  2495. struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
  2496. BUG_ON(!dl);
  2497. if (ocfs2_is_hard_readonly(osb)) {
  2498. if (ex)
  2499. return -EROFS;
  2500. return 0;
  2501. }
  2502. if (ocfs2_mount_local(osb))
  2503. return 0;
  2504. ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0);
  2505. if (ret < 0)
  2506. mlog_errno(ret);
  2507. return ret;
  2508. }
  2509. void ocfs2_dentry_unlock(struct dentry *dentry, int ex)
  2510. {
  2511. int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
  2512. struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
  2513. struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
  2514. if (!ocfs2_is_hard_readonly(osb) && !ocfs2_mount_local(osb))
  2515. ocfs2_cluster_unlock(osb, &dl->dl_lockres, level);
  2516. }
  2517. /* Reference counting of the dlm debug structure. We want this because
  2518. * open references on the debug inodes can live on after a mount, so
  2519. * we can't rely on the ocfs2_super to always exist. */
  2520. static void ocfs2_dlm_debug_free(struct kref *kref)
  2521. {
  2522. struct ocfs2_dlm_debug *dlm_debug;
  2523. dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
  2524. kfree(dlm_debug);
  2525. }
  2526. void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
  2527. {
  2528. if (dlm_debug)
  2529. kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
  2530. }
  2531. static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
  2532. {
  2533. kref_get(&debug->d_refcnt);
  2534. }
  2535. struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
  2536. {
  2537. struct ocfs2_dlm_debug *dlm_debug;
  2538. dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
  2539. if (!dlm_debug) {
  2540. mlog_errno(-ENOMEM);
  2541. goto out;
  2542. }
  2543. kref_init(&dlm_debug->d_refcnt);
  2544. INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
  2545. dlm_debug->d_locking_state = NULL;
  2546. out:
  2547. return dlm_debug;
  2548. }
  2549. /* Access to this is arbitrated for us via seq_file->sem. */
  2550. struct ocfs2_dlm_seq_priv {
  2551. struct ocfs2_dlm_debug *p_dlm_debug;
  2552. struct ocfs2_lock_res p_iter_res;
  2553. struct ocfs2_lock_res p_tmp_res;
  2554. };
  2555. static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
  2556. struct ocfs2_dlm_seq_priv *priv)
  2557. {
  2558. struct ocfs2_lock_res *iter, *ret = NULL;
  2559. struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
  2560. assert_spin_locked(&ocfs2_dlm_tracking_lock);
  2561. list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
  2562. /* discover the head of the list */
  2563. if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
  2564. mlog(0, "End of list found, %p\n", ret);
  2565. break;
  2566. }
  2567. /* We track our "dummy" iteration lockres' by a NULL
  2568. * l_ops field. */
  2569. if (iter->l_ops != NULL) {
  2570. ret = iter;
  2571. break;
  2572. }
  2573. }
  2574. return ret;
  2575. }
  2576. static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
  2577. {
  2578. struct ocfs2_dlm_seq_priv *priv = m->private;
  2579. struct ocfs2_lock_res *iter;
  2580. spin_lock(&ocfs2_dlm_tracking_lock);
  2581. iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
  2582. if (iter) {
  2583. /* Since lockres' have the lifetime of their container
  2584. * (which can be inodes, ocfs2_supers, etc) we want to
  2585. * copy this out to a temporary lockres while still
  2586. * under the spinlock. Obviously after this we can't
  2587. * trust any pointers on the copy returned, but that's
  2588. * ok as the information we want isn't typically held
  2589. * in them. */
  2590. priv->p_tmp_res = *iter;
  2591. iter = &priv->p_tmp_res;
  2592. }
  2593. spin_unlock(&ocfs2_dlm_tracking_lock);
  2594. return iter;
  2595. }
  2596. static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
  2597. {
  2598. }
  2599. static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
  2600. {
  2601. struct ocfs2_dlm_seq_priv *priv = m->private;
  2602. struct ocfs2_lock_res *iter = v;
  2603. struct ocfs2_lock_res *dummy = &priv->p_iter_res;
  2604. spin_lock(&ocfs2_dlm_tracking_lock);
  2605. iter = ocfs2_dlm_next_res(iter, priv);
  2606. list_del_init(&dummy->l_debug_list);
  2607. if (iter) {
  2608. list_add(&dummy->l_debug_list, &iter->l_debug_list);
  2609. priv->p_tmp_res = *iter;
  2610. iter = &priv->p_tmp_res;
  2611. }
  2612. spin_unlock(&ocfs2_dlm_tracking_lock);
  2613. return iter;
  2614. }
  2615. /*
  2616. * Version is used by debugfs.ocfs2 to determine the format being used
  2617. *
  2618. * New in version 2
  2619. * - Lock stats printed
  2620. * New in version 3
  2621. * - Max time in lock stats is in usecs (instead of nsecs)
  2622. */
  2623. #define OCFS2_DLM_DEBUG_STR_VERSION 3
  2624. static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
  2625. {
  2626. int i;
  2627. char *lvb;
  2628. struct ocfs2_lock_res *lockres = v;
  2629. if (!lockres)
  2630. return -EINVAL;
  2631. seq_printf(m, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION);
  2632. if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY)
  2633. seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1,
  2634. lockres->l_name,
  2635. (unsigned int)ocfs2_get_dentry_lock_ino(lockres));
  2636. else
  2637. seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name);
  2638. seq_printf(m, "%d\t"
  2639. "0x%lx\t"
  2640. "0x%x\t"
  2641. "0x%x\t"
  2642. "%u\t"
  2643. "%u\t"
  2644. "%d\t"
  2645. "%d\t",
  2646. lockres->l_level,
  2647. lockres->l_flags,
  2648. lockres->l_action,
  2649. lockres->l_unlock_action,
  2650. lockres->l_ro_holders,
  2651. lockres->l_ex_holders,
  2652. lockres->l_requested,
  2653. lockres->l_blocking);
  2654. /* Dump the raw LVB */
  2655. lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
  2656. for(i = 0; i < DLM_LVB_LEN; i++)
  2657. seq_printf(m, "0x%x\t", lvb[i]);
  2658. #ifdef CONFIG_OCFS2_FS_STATS
  2659. # define lock_num_prmode(_l) ((_l)->l_lock_prmode.ls_gets)
  2660. # define lock_num_exmode(_l) ((_l)->l_lock_exmode.ls_gets)
  2661. # define lock_num_prmode_failed(_l) ((_l)->l_lock_prmode.ls_fail)
  2662. # define lock_num_exmode_failed(_l) ((_l)->l_lock_exmode.ls_fail)
  2663. # define lock_total_prmode(_l) ((_l)->l_lock_prmode.ls_total)
  2664. # define lock_total_exmode(_l) ((_l)->l_lock_exmode.ls_total)
  2665. # define lock_max_prmode(_l) ((_l)->l_lock_prmode.ls_max)
  2666. # define lock_max_exmode(_l) ((_l)->l_lock_exmode.ls_max)
  2667. # define lock_refresh(_l) ((_l)->l_lock_refresh)
  2668. #else
  2669. # define lock_num_prmode(_l) (0)
  2670. # define lock_num_exmode(_l) (0)
  2671. # define lock_num_prmode_failed(_l) (0)
  2672. # define lock_num_exmode_failed(_l) (0)
  2673. # define lock_total_prmode(_l) (0ULL)
  2674. # define lock_total_exmode(_l) (0ULL)
  2675. # define lock_max_prmode(_l) (0)
  2676. # define lock_max_exmode(_l) (0)
  2677. # define lock_refresh(_l) (0)
  2678. #endif
  2679. /* The following seq_print was added in version 2 of this output */
  2680. seq_printf(m, "%u\t"
  2681. "%u\t"
  2682. "%u\t"
  2683. "%u\t"
  2684. "%llu\t"
  2685. "%llu\t"
  2686. "%u\t"
  2687. "%u\t"
  2688. "%u\t",
  2689. lock_num_prmode(lockres),
  2690. lock_num_exmode(lockres),
  2691. lock_num_prmode_failed(lockres),
  2692. lock_num_exmode_failed(lockres),
  2693. lock_total_prmode(lockres),
  2694. lock_total_exmode(lockres),
  2695. lock_max_prmode(lockres),
  2696. lock_max_exmode(lockres),
  2697. lock_refresh(lockres));
  2698. /* End the line */
  2699. seq_printf(m, "\n");
  2700. return 0;
  2701. }
  2702. static const struct seq_operations ocfs2_dlm_seq_ops = {
  2703. .start = ocfs2_dlm_seq_start,
  2704. .stop = ocfs2_dlm_seq_stop,
  2705. .next = ocfs2_dlm_seq_next,
  2706. .show = ocfs2_dlm_seq_show,
  2707. };
  2708. static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
  2709. {
  2710. struct seq_file *seq = file->private_data;
  2711. struct ocfs2_dlm_seq_priv *priv = seq->private;
  2712. struct ocfs2_lock_res *res = &priv->p_iter_res;
  2713. ocfs2_remove_lockres_tracking(res);
  2714. ocfs2_put_dlm_debug(priv->p_dlm_debug);
  2715. return seq_release_private(inode, file);
  2716. }
  2717. static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
  2718. {
  2719. struct ocfs2_dlm_seq_priv *priv;
  2720. struct ocfs2_super *osb;
  2721. priv = __seq_open_private(file, &ocfs2_dlm_seq_ops, sizeof(*priv));
  2722. if (!priv) {
  2723. mlog_errno(-ENOMEM);
  2724. return -ENOMEM;
  2725. }
  2726. osb = inode->i_private;
  2727. ocfs2_get_dlm_debug(osb->osb_dlm_debug);
  2728. priv->p_dlm_debug = osb->osb_dlm_debug;
  2729. INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
  2730. ocfs2_add_lockres_tracking(&priv->p_iter_res,
  2731. priv->p_dlm_debug);
  2732. return 0;
  2733. }
  2734. static const struct file_operations ocfs2_dlm_debug_fops = {
  2735. .open = ocfs2_dlm_debug_open,
  2736. .release = ocfs2_dlm_debug_release,
  2737. .read = seq_read,
  2738. .llseek = seq_lseek,
  2739. };
  2740. static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
  2741. {
  2742. int ret = 0;
  2743. struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
  2744. dlm_debug->d_locking_state = debugfs_create_file("locking_state",
  2745. S_IFREG|S_IRUSR,
  2746. osb->osb_debug_root,
  2747. osb,
  2748. &ocfs2_dlm_debug_fops);
  2749. if (!dlm_debug->d_locking_state) {
  2750. ret = -EINVAL;
  2751. mlog(ML_ERROR,
  2752. "Unable to create locking state debugfs file.\n");
  2753. goto out;
  2754. }
  2755. ocfs2_get_dlm_debug(dlm_debug);
  2756. out:
  2757. return ret;
  2758. }
  2759. static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
  2760. {
  2761. struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
  2762. if (dlm_debug) {
  2763. debugfs_remove(dlm_debug->d_locking_state);
  2764. ocfs2_put_dlm_debug(dlm_debug);
  2765. }
  2766. }
  2767. int ocfs2_dlm_init(struct ocfs2_super *osb)
  2768. {
  2769. int status = 0;
  2770. struct ocfs2_cluster_connection *conn = NULL;
  2771. if (ocfs2_mount_local(osb)) {
  2772. osb->node_num = 0;
  2773. goto local;
  2774. }
  2775. status = ocfs2_dlm_init_debug(osb);
  2776. if (status < 0) {
  2777. mlog_errno(status);
  2778. goto bail;
  2779. }
  2780. /* launch downconvert thread */
  2781. osb->dc_task = kthread_run(ocfs2_downconvert_thread, osb, "ocfs2dc-%s",
  2782. osb->uuid_str);
  2783. if (IS_ERR(osb->dc_task)) {
  2784. status = PTR_ERR(osb->dc_task);
  2785. osb->dc_task = NULL;
  2786. mlog_errno(status);
  2787. goto bail;
  2788. }
  2789. /* for now, uuid == domain */
  2790. status = ocfs2_cluster_connect(osb->osb_cluster_stack,
  2791. osb->osb_cluster_name,
  2792. strlen(osb->osb_cluster_name),
  2793. osb->uuid_str,
  2794. strlen(osb->uuid_str),
  2795. &lproto, ocfs2_do_node_down, osb,
  2796. &conn);
  2797. if (status) {
  2798. mlog_errno(status);
  2799. goto bail;
  2800. }
  2801. status = ocfs2_cluster_this_node(conn, &osb->node_num);
  2802. if (status < 0) {
  2803. mlog_errno(status);
  2804. mlog(ML_ERROR,
  2805. "could not find this host's node number\n");
  2806. ocfs2_cluster_disconnect(conn, 0);
  2807. goto bail;
  2808. }
  2809. local:
  2810. ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
  2811. ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
  2812. ocfs2_nfs_sync_lock_res_init(&osb->osb_nfs_sync_lockres, osb);
  2813. ocfs2_orphan_scan_lock_res_init(&osb->osb_orphan_scan.os_lockres, osb);
  2814. osb->cconn = conn;
  2815. bail:
  2816. if (status < 0) {
  2817. ocfs2_dlm_shutdown_debug(osb);
  2818. if (osb->dc_task)
  2819. kthread_stop(osb->dc_task);
  2820. }
  2821. return status;
  2822. }
  2823. void ocfs2_dlm_shutdown(struct ocfs2_super *osb,
  2824. int hangup_pending)
  2825. {
  2826. ocfs2_drop_osb_locks(osb);
  2827. /*
  2828. * Now that we have dropped all locks and ocfs2_dismount_volume()
  2829. * has disabled recovery, the DLM won't be talking to us. It's
  2830. * safe to tear things down before disconnecting the cluster.
  2831. */
  2832. if (osb->dc_task) {
  2833. kthread_stop(osb->dc_task);
  2834. osb->dc_task = NULL;
  2835. }
  2836. ocfs2_lock_res_free(&osb->osb_super_lockres);
  2837. ocfs2_lock_res_free(&osb->osb_rename_lockres);
  2838. ocfs2_lock_res_free(&osb->osb_nfs_sync_lockres);
  2839. ocfs2_lock_res_free(&osb->osb_orphan_scan.os_lockres);
  2840. ocfs2_cluster_disconnect(osb->cconn, hangup_pending);
  2841. osb->cconn = NULL;
  2842. ocfs2_dlm_shutdown_debug(osb);
  2843. }
  2844. static int ocfs2_drop_lock(struct ocfs2_super *osb,
  2845. struct ocfs2_lock_res *lockres)
  2846. {
  2847. int ret;
  2848. unsigned long flags;
  2849. u32 lkm_flags = 0;
  2850. /* We didn't get anywhere near actually using this lockres. */
  2851. if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
  2852. goto out;
  2853. if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
  2854. lkm_flags |= DLM_LKF_VALBLK;
  2855. spin_lock_irqsave(&lockres->l_lock, flags);
  2856. mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
  2857. "lockres %s, flags 0x%lx\n",
  2858. lockres->l_name, lockres->l_flags);
  2859. while (lockres->l_flags & OCFS2_LOCK_BUSY) {
  2860. mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
  2861. "%u, unlock_action = %u\n",
  2862. lockres->l_name, lockres->l_flags, lockres->l_action,
  2863. lockres->l_unlock_action);
  2864. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2865. /* XXX: Today we just wait on any busy
  2866. * locks... Perhaps we need to cancel converts in the
  2867. * future? */
  2868. ocfs2_wait_on_busy_lock(lockres);
  2869. spin_lock_irqsave(&lockres->l_lock, flags);
  2870. }
  2871. if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
  2872. if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
  2873. lockres->l_level == DLM_LOCK_EX &&
  2874. !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
  2875. lockres->l_ops->set_lvb(lockres);
  2876. }
  2877. if (lockres->l_flags & OCFS2_LOCK_BUSY)
  2878. mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
  2879. lockres->l_name);
  2880. if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
  2881. mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
  2882. if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
  2883. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2884. goto out;
  2885. }
  2886. lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
  2887. /* make sure we never get here while waiting for an ast to
  2888. * fire. */
  2889. BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
  2890. /* is this necessary? */
  2891. lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
  2892. lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
  2893. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2894. mlog(0, "lock %s\n", lockres->l_name);
  2895. ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb, lkm_flags);
  2896. if (ret) {
  2897. ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
  2898. mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
  2899. ocfs2_dlm_dump_lksb(&lockres->l_lksb);
  2900. BUG();
  2901. }
  2902. mlog(0, "lock %s, successful return from ocfs2_dlm_unlock\n",
  2903. lockres->l_name);
  2904. ocfs2_wait_on_busy_lock(lockres);
  2905. out:
  2906. return 0;
  2907. }
  2908. static void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
  2909. struct ocfs2_lock_res *lockres);
  2910. /* Mark the lockres as being dropped. It will no longer be
  2911. * queued if blocking, but we still may have to wait on it
  2912. * being dequeued from the downconvert thread before we can consider
  2913. * it safe to drop.
  2914. *
  2915. * You can *not* attempt to call cluster_lock on this lockres anymore. */
  2916. void ocfs2_mark_lockres_freeing(struct ocfs2_super *osb,
  2917. struct ocfs2_lock_res *lockres)
  2918. {
  2919. int status;
  2920. struct ocfs2_mask_waiter mw;
  2921. unsigned long flags, flags2;
  2922. ocfs2_init_mask_waiter(&mw);
  2923. spin_lock_irqsave(&lockres->l_lock, flags);
  2924. lockres->l_flags |= OCFS2_LOCK_FREEING;
  2925. if (lockres->l_flags & OCFS2_LOCK_QUEUED && current == osb->dc_task) {
  2926. /*
  2927. * We know the downconvert is queued but not in progress
  2928. * because we are the downconvert thread and processing
  2929. * different lock. So we can just remove the lock from the
  2930. * queue. This is not only an optimization but also a way
  2931. * to avoid the following deadlock:
  2932. * ocfs2_dentry_post_unlock()
  2933. * ocfs2_dentry_lock_put()
  2934. * ocfs2_drop_dentry_lock()
  2935. * iput()
  2936. * ocfs2_evict_inode()
  2937. * ocfs2_clear_inode()
  2938. * ocfs2_mark_lockres_freeing()
  2939. * ... blocks waiting for OCFS2_LOCK_QUEUED
  2940. * since we are the downconvert thread which
  2941. * should clear the flag.
  2942. */
  2943. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2944. spin_lock_irqsave(&osb->dc_task_lock, flags2);
  2945. list_del_init(&lockres->l_blocked_list);
  2946. osb->blocked_lock_count--;
  2947. spin_unlock_irqrestore(&osb->dc_task_lock, flags2);
  2948. /*
  2949. * Warn if we recurse into another post_unlock call. Strictly
  2950. * speaking it isn't a problem but we need to be careful if
  2951. * that happens (stack overflow, deadlocks, ...) so warn if
  2952. * ocfs2 grows a path for which this can happen.
  2953. */
  2954. WARN_ON_ONCE(lockres->l_ops->post_unlock);
  2955. /* Since the lock is freeing we don't do much in the fn below */
  2956. ocfs2_process_blocked_lock(osb, lockres);
  2957. return;
  2958. }
  2959. while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
  2960. lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
  2961. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2962. mlog(0, "Waiting on lockres %s\n", lockres->l_name);
  2963. status = ocfs2_wait_for_mask(&mw);
  2964. if (status)
  2965. mlog_errno(status);
  2966. spin_lock_irqsave(&lockres->l_lock, flags);
  2967. }
  2968. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2969. }
  2970. void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
  2971. struct ocfs2_lock_res *lockres)
  2972. {
  2973. int ret;
  2974. ocfs2_mark_lockres_freeing(osb, lockres);
  2975. ret = ocfs2_drop_lock(osb, lockres);
  2976. if (ret)
  2977. mlog_errno(ret);
  2978. }
  2979. static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
  2980. {
  2981. ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres);
  2982. ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres);
  2983. ocfs2_simple_drop_lockres(osb, &osb->osb_nfs_sync_lockres);
  2984. ocfs2_simple_drop_lockres(osb, &osb->osb_orphan_scan.os_lockres);
  2985. }
  2986. int ocfs2_drop_inode_locks(struct inode *inode)
  2987. {
  2988. int status, err;
  2989. /* No need to call ocfs2_mark_lockres_freeing here -
  2990. * ocfs2_clear_inode has done it for us. */
  2991. err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
  2992. &OCFS2_I(inode)->ip_open_lockres);
  2993. if (err < 0)
  2994. mlog_errno(err);
  2995. status = err;
  2996. err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
  2997. &OCFS2_I(inode)->ip_inode_lockres);
  2998. if (err < 0)
  2999. mlog_errno(err);
  3000. if (err < 0 && !status)
  3001. status = err;
  3002. err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
  3003. &OCFS2_I(inode)->ip_rw_lockres);
  3004. if (err < 0)
  3005. mlog_errno(err);
  3006. if (err < 0 && !status)
  3007. status = err;
  3008. return status;
  3009. }
  3010. static unsigned int ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
  3011. int new_level)
  3012. {
  3013. assert_spin_locked(&lockres->l_lock);
  3014. BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
  3015. if (lockres->l_level <= new_level) {
  3016. mlog(ML_ERROR, "lockres %s, lvl %d <= %d, blcklst %d, mask %d, "
  3017. "type %d, flags 0x%lx, hold %d %d, act %d %d, req %d, "
  3018. "block %d, pgen %d\n", lockres->l_name, lockres->l_level,
  3019. new_level, list_empty(&lockres->l_blocked_list),
  3020. list_empty(&lockres->l_mask_waiters), lockres->l_type,
  3021. lockres->l_flags, lockres->l_ro_holders,
  3022. lockres->l_ex_holders, lockres->l_action,
  3023. lockres->l_unlock_action, lockres->l_requested,
  3024. lockres->l_blocking, lockres->l_pending_gen);
  3025. BUG();
  3026. }
  3027. mlog(ML_BASTS, "lockres %s, level %d => %d, blocking %d\n",
  3028. lockres->l_name, lockres->l_level, new_level, lockres->l_blocking);
  3029. lockres->l_action = OCFS2_AST_DOWNCONVERT;
  3030. lockres->l_requested = new_level;
  3031. lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
  3032. return lockres_set_pending(lockres);
  3033. }
  3034. static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
  3035. struct ocfs2_lock_res *lockres,
  3036. int new_level,
  3037. int lvb,
  3038. unsigned int generation)
  3039. {
  3040. int ret;
  3041. u32 dlm_flags = DLM_LKF_CONVERT;
  3042. mlog(ML_BASTS, "lockres %s, level %d => %d\n", lockres->l_name,
  3043. lockres->l_level, new_level);
  3044. /*
  3045. * On DLM_LKF_VALBLK, fsdlm behaves differently with o2cb. It always
  3046. * expects DLM_LKF_VALBLK being set if the LKB has LVB, so that
  3047. * we can recover correctly from node failure. Otherwise, we may get
  3048. * invalid LVB in LKB, but without DLM_SBF_VALNOTVALID being set.
  3049. */
  3050. if (ocfs2_userspace_stack(osb) &&
  3051. lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
  3052. lvb = 1;
  3053. if (lvb)
  3054. dlm_flags |= DLM_LKF_VALBLK;
  3055. ret = ocfs2_dlm_lock(osb->cconn,
  3056. new_level,
  3057. &lockres->l_lksb,
  3058. dlm_flags,
  3059. lockres->l_name,
  3060. OCFS2_LOCK_ID_MAX_LEN - 1);
  3061. lockres_clear_pending(lockres, generation, osb);
  3062. if (ret) {
  3063. ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
  3064. ocfs2_recover_from_dlm_error(lockres, 1);
  3065. goto bail;
  3066. }
  3067. ret = 0;
  3068. bail:
  3069. return ret;
  3070. }
  3071. /* returns 1 when the caller should unlock and call ocfs2_dlm_unlock */
  3072. static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
  3073. struct ocfs2_lock_res *lockres)
  3074. {
  3075. assert_spin_locked(&lockres->l_lock);
  3076. if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
  3077. /* If we're already trying to cancel a lock conversion
  3078. * then just drop the spinlock and allow the caller to
  3079. * requeue this lock. */
  3080. mlog(ML_BASTS, "lockres %s, skip convert\n", lockres->l_name);
  3081. return 0;
  3082. }
  3083. /* were we in a convert when we got the bast fire? */
  3084. BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
  3085. lockres->l_action != OCFS2_AST_DOWNCONVERT);
  3086. /* set things up for the unlockast to know to just
  3087. * clear out the ast_action and unset busy, etc. */
  3088. lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
  3089. mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
  3090. "lock %s, invalid flags: 0x%lx\n",
  3091. lockres->l_name, lockres->l_flags);
  3092. mlog(ML_BASTS, "lockres %s\n", lockres->l_name);
  3093. return 1;
  3094. }
  3095. static int ocfs2_cancel_convert(struct ocfs2_super *osb,
  3096. struct ocfs2_lock_res *lockres)
  3097. {
  3098. int ret;
  3099. ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb,
  3100. DLM_LKF_CANCEL);
  3101. if (ret) {
  3102. ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
  3103. ocfs2_recover_from_dlm_error(lockres, 0);
  3104. }
  3105. mlog(ML_BASTS, "lockres %s\n", lockres->l_name);
  3106. return ret;
  3107. }
  3108. static int ocfs2_unblock_lock(struct ocfs2_super *osb,
  3109. struct ocfs2_lock_res *lockres,
  3110. struct ocfs2_unblock_ctl *ctl)
  3111. {
  3112. unsigned long flags;
  3113. int blocking;
  3114. int new_level;
  3115. int level;
  3116. int ret = 0;
  3117. int set_lvb = 0;
  3118. unsigned int gen;
  3119. spin_lock_irqsave(&lockres->l_lock, flags);
  3120. recheck:
  3121. /*
  3122. * Is it still blocking? If not, we have no more work to do.
  3123. */
  3124. if (!(lockres->l_flags & OCFS2_LOCK_BLOCKED)) {
  3125. BUG_ON(lockres->l_blocking != DLM_LOCK_NL);
  3126. spin_unlock_irqrestore(&lockres->l_lock, flags);
  3127. ret = 0;
  3128. goto leave;
  3129. }
  3130. if (lockres->l_flags & OCFS2_LOCK_BUSY) {
  3131. /* XXX
  3132. * This is a *big* race. The OCFS2_LOCK_PENDING flag
  3133. * exists entirely for one reason - another thread has set
  3134. * OCFS2_LOCK_BUSY, but has *NOT* yet called dlm_lock().
  3135. *
  3136. * If we do ocfs2_cancel_convert() before the other thread
  3137. * calls dlm_lock(), our cancel will do nothing. We will
  3138. * get no ast, and we will have no way of knowing the
  3139. * cancel failed. Meanwhile, the other thread will call
  3140. * into dlm_lock() and wait...forever.
  3141. *
  3142. * Why forever? Because another node has asked for the
  3143. * lock first; that's why we're here in unblock_lock().
  3144. *
  3145. * The solution is OCFS2_LOCK_PENDING. When PENDING is
  3146. * set, we just requeue the unblock. Only when the other
  3147. * thread has called dlm_lock() and cleared PENDING will
  3148. * we then cancel their request.
  3149. *
  3150. * All callers of dlm_lock() must set OCFS2_DLM_PENDING
  3151. * at the same time they set OCFS2_DLM_BUSY. They must
  3152. * clear OCFS2_DLM_PENDING after dlm_lock() returns.
  3153. */
  3154. if (lockres->l_flags & OCFS2_LOCK_PENDING) {
  3155. mlog(ML_BASTS, "lockres %s, ReQ: Pending\n",
  3156. lockres->l_name);
  3157. goto leave_requeue;
  3158. }
  3159. ctl->requeue = 1;
  3160. ret = ocfs2_prepare_cancel_convert(osb, lockres);
  3161. spin_unlock_irqrestore(&lockres->l_lock, flags);
  3162. if (ret) {
  3163. ret = ocfs2_cancel_convert(osb, lockres);
  3164. if (ret < 0)
  3165. mlog_errno(ret);
  3166. }
  3167. goto leave;
  3168. }
  3169. /*
  3170. * This prevents livelocks. OCFS2_LOCK_UPCONVERT_FINISHING flag is
  3171. * set when the ast is received for an upconvert just before the
  3172. * OCFS2_LOCK_BUSY flag is cleared. Now if the fs received a bast
  3173. * on the heels of the ast, we want to delay the downconvert just
  3174. * enough to allow the up requestor to do its task. Because this
  3175. * lock is in the blocked queue, the lock will be downconverted
  3176. * as soon as the requestor is done with the lock.
  3177. */
  3178. if (lockres->l_flags & OCFS2_LOCK_UPCONVERT_FINISHING)
  3179. goto leave_requeue;
  3180. /*
  3181. * How can we block and yet be at NL? We were trying to upconvert
  3182. * from NL and got canceled. The code comes back here, and now
  3183. * we notice and clear BLOCKING.
  3184. */
  3185. if (lockres->l_level == DLM_LOCK_NL) {
  3186. BUG_ON(lockres->l_ex_holders || lockres->l_ro_holders);
  3187. mlog(ML_BASTS, "lockres %s, Aborting dc\n", lockres->l_name);
  3188. lockres->l_blocking = DLM_LOCK_NL;
  3189. lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
  3190. spin_unlock_irqrestore(&lockres->l_lock, flags);
  3191. goto leave;
  3192. }
  3193. /* if we're blocking an exclusive and we have *any* holders,
  3194. * then requeue. */
  3195. if ((lockres->l_blocking == DLM_LOCK_EX)
  3196. && (lockres->l_ex_holders || lockres->l_ro_holders)) {
  3197. mlog(ML_BASTS, "lockres %s, ReQ: EX/PR Holders %u,%u\n",
  3198. lockres->l_name, lockres->l_ex_holders,
  3199. lockres->l_ro_holders);
  3200. goto leave_requeue;
  3201. }
  3202. /* If it's a PR we're blocking, then only
  3203. * requeue if we've got any EX holders */
  3204. if (lockres->l_blocking == DLM_LOCK_PR &&
  3205. lockres->l_ex_holders) {
  3206. mlog(ML_BASTS, "lockres %s, ReQ: EX Holders %u\n",
  3207. lockres->l_name, lockres->l_ex_holders);
  3208. goto leave_requeue;
  3209. }
  3210. /*
  3211. * Can we get a lock in this state if the holder counts are
  3212. * zero? The meta data unblock code used to check this.
  3213. */
  3214. if ((lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
  3215. && (lockres->l_flags & OCFS2_LOCK_REFRESHING)) {
  3216. mlog(ML_BASTS, "lockres %s, ReQ: Lock Refreshing\n",
  3217. lockres->l_name);
  3218. goto leave_requeue;
  3219. }
  3220. new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
  3221. if (lockres->l_ops->check_downconvert
  3222. && !lockres->l_ops->check_downconvert(lockres, new_level)) {
  3223. mlog(ML_BASTS, "lockres %s, ReQ: Checkpointing\n",
  3224. lockres->l_name);
  3225. goto leave_requeue;
  3226. }
  3227. /* If we get here, then we know that there are no more
  3228. * incompatible holders (and anyone asking for an incompatible
  3229. * lock is blocked). We can now downconvert the lock */
  3230. if (!lockres->l_ops->downconvert_worker)
  3231. goto downconvert;
  3232. /* Some lockres types want to do a bit of work before
  3233. * downconverting a lock. Allow that here. The worker function
  3234. * may sleep, so we save off a copy of what we're blocking as
  3235. * it may change while we're not holding the spin lock. */
  3236. blocking = lockres->l_blocking;
  3237. level = lockres->l_level;
  3238. spin_unlock_irqrestore(&lockres->l_lock, flags);
  3239. ctl->unblock_action = lockres->l_ops->downconvert_worker(lockres, blocking);
  3240. if (ctl->unblock_action == UNBLOCK_STOP_POST) {
  3241. mlog(ML_BASTS, "lockres %s, UNBLOCK_STOP_POST\n",
  3242. lockres->l_name);
  3243. goto leave;
  3244. }
  3245. spin_lock_irqsave(&lockres->l_lock, flags);
  3246. if ((blocking != lockres->l_blocking) || (level != lockres->l_level)) {
  3247. /* If this changed underneath us, then we can't drop
  3248. * it just yet. */
  3249. mlog(ML_BASTS, "lockres %s, block=%d:%d, level=%d:%d, "
  3250. "Recheck\n", lockres->l_name, blocking,
  3251. lockres->l_blocking, level, lockres->l_level);
  3252. goto recheck;
  3253. }
  3254. downconvert:
  3255. ctl->requeue = 0;
  3256. if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
  3257. if (lockres->l_level == DLM_LOCK_EX)
  3258. set_lvb = 1;
  3259. /*
  3260. * We only set the lvb if the lock has been fully
  3261. * refreshed - otherwise we risk setting stale
  3262. * data. Otherwise, there's no need to actually clear
  3263. * out the lvb here as it's value is still valid.
  3264. */
  3265. if (set_lvb && !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
  3266. lockres->l_ops->set_lvb(lockres);
  3267. }
  3268. gen = ocfs2_prepare_downconvert(lockres, new_level);
  3269. spin_unlock_irqrestore(&lockres->l_lock, flags);
  3270. ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb,
  3271. gen);
  3272. leave:
  3273. if (ret)
  3274. mlog_errno(ret);
  3275. return ret;
  3276. leave_requeue:
  3277. spin_unlock_irqrestore(&lockres->l_lock, flags);
  3278. ctl->requeue = 1;
  3279. return 0;
  3280. }
  3281. static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
  3282. int blocking)
  3283. {
  3284. struct inode *inode;
  3285. struct address_space *mapping;
  3286. struct ocfs2_inode_info *oi;
  3287. inode = ocfs2_lock_res_inode(lockres);
  3288. mapping = inode->i_mapping;
  3289. if (S_ISDIR(inode->i_mode)) {
  3290. oi = OCFS2_I(inode);
  3291. oi->ip_dir_lock_gen++;
  3292. mlog(0, "generation: %u\n", oi->ip_dir_lock_gen);
  3293. goto out;
  3294. }
  3295. if (!S_ISREG(inode->i_mode))
  3296. goto out;
  3297. /*
  3298. * We need this before the filemap_fdatawrite() so that it can
  3299. * transfer the dirty bit from the PTE to the
  3300. * page. Unfortunately this means that even for EX->PR
  3301. * downconverts, we'll lose our mappings and have to build
  3302. * them up again.
  3303. */
  3304. unmap_mapping_range(mapping, 0, 0, 0);
  3305. if (filemap_fdatawrite(mapping)) {
  3306. mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
  3307. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  3308. }
  3309. sync_mapping_buffers(mapping);
  3310. if (blocking == DLM_LOCK_EX) {
  3311. truncate_inode_pages(mapping, 0);
  3312. } else {
  3313. /* We only need to wait on the I/O if we're not also
  3314. * truncating pages because truncate_inode_pages waits
  3315. * for us above. We don't truncate pages if we're
  3316. * blocking anything < EXMODE because we want to keep
  3317. * them around in that case. */
  3318. filemap_fdatawait(mapping);
  3319. }
  3320. forget_all_cached_acls(inode);
  3321. out:
  3322. return UNBLOCK_CONTINUE;
  3323. }
  3324. static int ocfs2_ci_checkpointed(struct ocfs2_caching_info *ci,
  3325. struct ocfs2_lock_res *lockres,
  3326. int new_level)
  3327. {
  3328. int checkpointed = ocfs2_ci_fully_checkpointed(ci);
  3329. BUG_ON(new_level != DLM_LOCK_NL && new_level != DLM_LOCK_PR);
  3330. BUG_ON(lockres->l_level != DLM_LOCK_EX && !checkpointed);
  3331. if (checkpointed)
  3332. return 1;
  3333. ocfs2_start_checkpoint(OCFS2_SB(ocfs2_metadata_cache_get_super(ci)));
  3334. return 0;
  3335. }
  3336. static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
  3337. int new_level)
  3338. {
  3339. struct inode *inode = ocfs2_lock_res_inode(lockres);
  3340. return ocfs2_ci_checkpointed(INODE_CACHE(inode), lockres, new_level);
  3341. }
  3342. static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres)
  3343. {
  3344. struct inode *inode = ocfs2_lock_res_inode(lockres);
  3345. __ocfs2_stuff_meta_lvb(inode);
  3346. }
  3347. /*
  3348. * Does the final reference drop on our dentry lock. Right now this
  3349. * happens in the downconvert thread, but we could choose to simplify the
  3350. * dlmglue API and push these off to the ocfs2_wq in the future.
  3351. */
  3352. static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
  3353. struct ocfs2_lock_res *lockres)
  3354. {
  3355. struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
  3356. ocfs2_dentry_lock_put(osb, dl);
  3357. }
  3358. /*
  3359. * d_delete() matching dentries before the lock downconvert.
  3360. *
  3361. * At this point, any process waiting to destroy the
  3362. * dentry_lock due to last ref count is stopped by the
  3363. * OCFS2_LOCK_QUEUED flag.
  3364. *
  3365. * We have two potential problems
  3366. *
  3367. * 1) If we do the last reference drop on our dentry_lock (via dput)
  3368. * we'll wind up in ocfs2_release_dentry_lock(), waiting on
  3369. * the downconvert to finish. Instead we take an elevated
  3370. * reference and push the drop until after we've completed our
  3371. * unblock processing.
  3372. *
  3373. * 2) There might be another process with a final reference,
  3374. * waiting on us to finish processing. If this is the case, we
  3375. * detect it and exit out - there's no more dentries anyway.
  3376. */
  3377. static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
  3378. int blocking)
  3379. {
  3380. struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
  3381. struct ocfs2_inode_info *oi = OCFS2_I(dl->dl_inode);
  3382. struct dentry *dentry;
  3383. unsigned long flags;
  3384. int extra_ref = 0;
  3385. /*
  3386. * This node is blocking another node from getting a read
  3387. * lock. This happens when we've renamed within a
  3388. * directory. We've forced the other nodes to d_delete(), but
  3389. * we never actually dropped our lock because it's still
  3390. * valid. The downconvert code will retain a PR for this node,
  3391. * so there's no further work to do.
  3392. */
  3393. if (blocking == DLM_LOCK_PR)
  3394. return UNBLOCK_CONTINUE;
  3395. /*
  3396. * Mark this inode as potentially orphaned. The code in
  3397. * ocfs2_delete_inode() will figure out whether it actually
  3398. * needs to be freed or not.
  3399. */
  3400. spin_lock(&oi->ip_lock);
  3401. oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
  3402. spin_unlock(&oi->ip_lock);
  3403. /*
  3404. * Yuck. We need to make sure however that the check of
  3405. * OCFS2_LOCK_FREEING and the extra reference are atomic with
  3406. * respect to a reference decrement or the setting of that
  3407. * flag.
  3408. */
  3409. spin_lock_irqsave(&lockres->l_lock, flags);
  3410. spin_lock(&dentry_attach_lock);
  3411. if (!(lockres->l_flags & OCFS2_LOCK_FREEING)
  3412. && dl->dl_count) {
  3413. dl->dl_count++;
  3414. extra_ref = 1;
  3415. }
  3416. spin_unlock(&dentry_attach_lock);
  3417. spin_unlock_irqrestore(&lockres->l_lock, flags);
  3418. mlog(0, "extra_ref = %d\n", extra_ref);
  3419. /*
  3420. * We have a process waiting on us in ocfs2_dentry_iput(),
  3421. * which means we can't have any more outstanding
  3422. * aliases. There's no need to do any more work.
  3423. */
  3424. if (!extra_ref)
  3425. return UNBLOCK_CONTINUE;
  3426. spin_lock(&dentry_attach_lock);
  3427. while (1) {
  3428. dentry = ocfs2_find_local_alias(dl->dl_inode,
  3429. dl->dl_parent_blkno, 1);
  3430. if (!dentry)
  3431. break;
  3432. spin_unlock(&dentry_attach_lock);
  3433. if (S_ISDIR(dl->dl_inode->i_mode))
  3434. shrink_dcache_parent(dentry);
  3435. mlog(0, "d_delete(%pd);\n", dentry);
  3436. /*
  3437. * The following dcache calls may do an
  3438. * iput(). Normally we don't want that from the
  3439. * downconverting thread, but in this case it's ok
  3440. * because the requesting node already has an
  3441. * exclusive lock on the inode, so it can't be queued
  3442. * for a downconvert.
  3443. */
  3444. d_delete(dentry);
  3445. dput(dentry);
  3446. spin_lock(&dentry_attach_lock);
  3447. }
  3448. spin_unlock(&dentry_attach_lock);
  3449. /*
  3450. * If we are the last holder of this dentry lock, there is no
  3451. * reason to downconvert so skip straight to the unlock.
  3452. */
  3453. if (dl->dl_count == 1)
  3454. return UNBLOCK_STOP_POST;
  3455. return UNBLOCK_CONTINUE_POST;
  3456. }
  3457. static int ocfs2_check_refcount_downconvert(struct ocfs2_lock_res *lockres,
  3458. int new_level)
  3459. {
  3460. struct ocfs2_refcount_tree *tree =
  3461. ocfs2_lock_res_refcount_tree(lockres);
  3462. return ocfs2_ci_checkpointed(&tree->rf_ci, lockres, new_level);
  3463. }
  3464. static int ocfs2_refcount_convert_worker(struct ocfs2_lock_res *lockres,
  3465. int blocking)
  3466. {
  3467. struct ocfs2_refcount_tree *tree =
  3468. ocfs2_lock_res_refcount_tree(lockres);
  3469. ocfs2_metadata_cache_purge(&tree->rf_ci);
  3470. return UNBLOCK_CONTINUE;
  3471. }
  3472. static void ocfs2_set_qinfo_lvb(struct ocfs2_lock_res *lockres)
  3473. {
  3474. struct ocfs2_qinfo_lvb *lvb;
  3475. struct ocfs2_mem_dqinfo *oinfo = ocfs2_lock_res_qinfo(lockres);
  3476. struct mem_dqinfo *info = sb_dqinfo(oinfo->dqi_gi.dqi_sb,
  3477. oinfo->dqi_gi.dqi_type);
  3478. lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
  3479. lvb->lvb_version = OCFS2_QINFO_LVB_VERSION;
  3480. lvb->lvb_bgrace = cpu_to_be32(info->dqi_bgrace);
  3481. lvb->lvb_igrace = cpu_to_be32(info->dqi_igrace);
  3482. lvb->lvb_syncms = cpu_to_be32(oinfo->dqi_syncms);
  3483. lvb->lvb_blocks = cpu_to_be32(oinfo->dqi_gi.dqi_blocks);
  3484. lvb->lvb_free_blk = cpu_to_be32(oinfo->dqi_gi.dqi_free_blk);
  3485. lvb->lvb_free_entry = cpu_to_be32(oinfo->dqi_gi.dqi_free_entry);
  3486. }
  3487. void ocfs2_qinfo_unlock(struct ocfs2_mem_dqinfo *oinfo, int ex)
  3488. {
  3489. struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
  3490. struct ocfs2_super *osb = OCFS2_SB(oinfo->dqi_gi.dqi_sb);
  3491. int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
  3492. if (!ocfs2_is_hard_readonly(osb) && !ocfs2_mount_local(osb))
  3493. ocfs2_cluster_unlock(osb, lockres, level);
  3494. }
  3495. static int ocfs2_refresh_qinfo(struct ocfs2_mem_dqinfo *oinfo)
  3496. {
  3497. struct mem_dqinfo *info = sb_dqinfo(oinfo->dqi_gi.dqi_sb,
  3498. oinfo->dqi_gi.dqi_type);
  3499. struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
  3500. struct ocfs2_qinfo_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
  3501. struct buffer_head *bh = NULL;
  3502. struct ocfs2_global_disk_dqinfo *gdinfo;
  3503. int status = 0;
  3504. if (ocfs2_dlm_lvb_valid(&lockres->l_lksb) &&
  3505. lvb->lvb_version == OCFS2_QINFO_LVB_VERSION) {
  3506. info->dqi_bgrace = be32_to_cpu(lvb->lvb_bgrace);
  3507. info->dqi_igrace = be32_to_cpu(lvb->lvb_igrace);
  3508. oinfo->dqi_syncms = be32_to_cpu(lvb->lvb_syncms);
  3509. oinfo->dqi_gi.dqi_blocks = be32_to_cpu(lvb->lvb_blocks);
  3510. oinfo->dqi_gi.dqi_free_blk = be32_to_cpu(lvb->lvb_free_blk);
  3511. oinfo->dqi_gi.dqi_free_entry =
  3512. be32_to_cpu(lvb->lvb_free_entry);
  3513. } else {
  3514. status = ocfs2_read_quota_phys_block(oinfo->dqi_gqinode,
  3515. oinfo->dqi_giblk, &bh);
  3516. if (status) {
  3517. mlog_errno(status);
  3518. goto bail;
  3519. }
  3520. gdinfo = (struct ocfs2_global_disk_dqinfo *)
  3521. (bh->b_data + OCFS2_GLOBAL_INFO_OFF);
  3522. info->dqi_bgrace = le32_to_cpu(gdinfo->dqi_bgrace);
  3523. info->dqi_igrace = le32_to_cpu(gdinfo->dqi_igrace);
  3524. oinfo->dqi_syncms = le32_to_cpu(gdinfo->dqi_syncms);
  3525. oinfo->dqi_gi.dqi_blocks = le32_to_cpu(gdinfo->dqi_blocks);
  3526. oinfo->dqi_gi.dqi_free_blk = le32_to_cpu(gdinfo->dqi_free_blk);
  3527. oinfo->dqi_gi.dqi_free_entry =
  3528. le32_to_cpu(gdinfo->dqi_free_entry);
  3529. brelse(bh);
  3530. ocfs2_track_lock_refresh(lockres);
  3531. }
  3532. bail:
  3533. return status;
  3534. }
  3535. /* Lock quota info, this function expects at least shared lock on the quota file
  3536. * so that we can safely refresh quota info from disk. */
  3537. int ocfs2_qinfo_lock(struct ocfs2_mem_dqinfo *oinfo, int ex)
  3538. {
  3539. struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
  3540. struct ocfs2_super *osb = OCFS2_SB(oinfo->dqi_gi.dqi_sb);
  3541. int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
  3542. int status = 0;
  3543. /* On RO devices, locking really isn't needed... */
  3544. if (ocfs2_is_hard_readonly(osb)) {
  3545. if (ex)
  3546. status = -EROFS;
  3547. goto bail;
  3548. }
  3549. if (ocfs2_mount_local(osb))
  3550. goto bail;
  3551. status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
  3552. if (status < 0) {
  3553. mlog_errno(status);
  3554. goto bail;
  3555. }
  3556. if (!ocfs2_should_refresh_lock_res(lockres))
  3557. goto bail;
  3558. /* OK, we have the lock but we need to refresh the quota info */
  3559. status = ocfs2_refresh_qinfo(oinfo);
  3560. if (status)
  3561. ocfs2_qinfo_unlock(oinfo, ex);
  3562. ocfs2_complete_lock_res_refresh(lockres, status);
  3563. bail:
  3564. return status;
  3565. }
  3566. int ocfs2_refcount_lock(struct ocfs2_refcount_tree *ref_tree, int ex)
  3567. {
  3568. int status;
  3569. int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
  3570. struct ocfs2_lock_res *lockres = &ref_tree->rf_lockres;
  3571. struct ocfs2_super *osb = lockres->l_priv;
  3572. if (ocfs2_is_hard_readonly(osb))
  3573. return -EROFS;
  3574. if (ocfs2_mount_local(osb))
  3575. return 0;
  3576. status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
  3577. if (status < 0)
  3578. mlog_errno(status);
  3579. return status;
  3580. }
  3581. void ocfs2_refcount_unlock(struct ocfs2_refcount_tree *ref_tree, int ex)
  3582. {
  3583. int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
  3584. struct ocfs2_lock_res *lockres = &ref_tree->rf_lockres;
  3585. struct ocfs2_super *osb = lockres->l_priv;
  3586. if (!ocfs2_mount_local(osb))
  3587. ocfs2_cluster_unlock(osb, lockres, level);
  3588. }
  3589. static void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
  3590. struct ocfs2_lock_res *lockres)
  3591. {
  3592. int status;
  3593. struct ocfs2_unblock_ctl ctl = {0, 0,};
  3594. unsigned long flags;
  3595. /* Our reference to the lockres in this function can be
  3596. * considered valid until we remove the OCFS2_LOCK_QUEUED
  3597. * flag. */
  3598. BUG_ON(!lockres);
  3599. BUG_ON(!lockres->l_ops);
  3600. mlog(ML_BASTS, "lockres %s blocked\n", lockres->l_name);
  3601. /* Detect whether a lock has been marked as going away while
  3602. * the downconvert thread was processing other things. A lock can
  3603. * still be marked with OCFS2_LOCK_FREEING after this check,
  3604. * but short circuiting here will still save us some
  3605. * performance. */
  3606. spin_lock_irqsave(&lockres->l_lock, flags);
  3607. if (lockres->l_flags & OCFS2_LOCK_FREEING)
  3608. goto unqueue;
  3609. spin_unlock_irqrestore(&lockres->l_lock, flags);
  3610. status = ocfs2_unblock_lock(osb, lockres, &ctl);
  3611. if (status < 0)
  3612. mlog_errno(status);
  3613. spin_lock_irqsave(&lockres->l_lock, flags);
  3614. unqueue:
  3615. if (lockres->l_flags & OCFS2_LOCK_FREEING || !ctl.requeue) {
  3616. lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
  3617. } else
  3618. ocfs2_schedule_blocked_lock(osb, lockres);
  3619. mlog(ML_BASTS, "lockres %s, requeue = %s.\n", lockres->l_name,
  3620. ctl.requeue ? "yes" : "no");
  3621. spin_unlock_irqrestore(&lockres->l_lock, flags);
  3622. if (ctl.unblock_action != UNBLOCK_CONTINUE
  3623. && lockres->l_ops->post_unlock)
  3624. lockres->l_ops->post_unlock(osb, lockres);
  3625. }
  3626. static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
  3627. struct ocfs2_lock_res *lockres)
  3628. {
  3629. unsigned long flags;
  3630. assert_spin_locked(&lockres->l_lock);
  3631. if (lockres->l_flags & OCFS2_LOCK_FREEING) {
  3632. /* Do not schedule a lock for downconvert when it's on
  3633. * the way to destruction - any nodes wanting access
  3634. * to the resource will get it soon. */
  3635. mlog(ML_BASTS, "lockres %s won't be scheduled: flags 0x%lx\n",
  3636. lockres->l_name, lockres->l_flags);
  3637. return;
  3638. }
  3639. lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
  3640. spin_lock_irqsave(&osb->dc_task_lock, flags);
  3641. if (list_empty(&lockres->l_blocked_list)) {
  3642. list_add_tail(&lockres->l_blocked_list,
  3643. &osb->blocked_lock_list);
  3644. osb->blocked_lock_count++;
  3645. }
  3646. spin_unlock_irqrestore(&osb->dc_task_lock, flags);
  3647. }
  3648. static void ocfs2_downconvert_thread_do_work(struct ocfs2_super *osb)
  3649. {
  3650. unsigned long processed;
  3651. unsigned long flags;
  3652. struct ocfs2_lock_res *lockres;
  3653. spin_lock_irqsave(&osb->dc_task_lock, flags);
  3654. /* grab this early so we know to try again if a state change and
  3655. * wake happens part-way through our work */
  3656. osb->dc_work_sequence = osb->dc_wake_sequence;
  3657. processed = osb->blocked_lock_count;
  3658. /*
  3659. * blocked lock processing in this loop might call iput which can
  3660. * remove items off osb->blocked_lock_list. Downconvert up to
  3661. * 'processed' number of locks, but stop short if we had some
  3662. * removed in ocfs2_mark_lockres_freeing when downconverting.
  3663. */
  3664. while (processed && !list_empty(&osb->blocked_lock_list)) {
  3665. lockres = list_entry(osb->blocked_lock_list.next,
  3666. struct ocfs2_lock_res, l_blocked_list);
  3667. list_del_init(&lockres->l_blocked_list);
  3668. osb->blocked_lock_count--;
  3669. spin_unlock_irqrestore(&osb->dc_task_lock, flags);
  3670. BUG_ON(!processed);
  3671. processed--;
  3672. ocfs2_process_blocked_lock(osb, lockres);
  3673. spin_lock_irqsave(&osb->dc_task_lock, flags);
  3674. }
  3675. spin_unlock_irqrestore(&osb->dc_task_lock, flags);
  3676. }
  3677. static int ocfs2_downconvert_thread_lists_empty(struct ocfs2_super *osb)
  3678. {
  3679. int empty = 0;
  3680. unsigned long flags;
  3681. spin_lock_irqsave(&osb->dc_task_lock, flags);
  3682. if (list_empty(&osb->blocked_lock_list))
  3683. empty = 1;
  3684. spin_unlock_irqrestore(&osb->dc_task_lock, flags);
  3685. return empty;
  3686. }
  3687. static int ocfs2_downconvert_thread_should_wake(struct ocfs2_super *osb)
  3688. {
  3689. int should_wake = 0;
  3690. unsigned long flags;
  3691. spin_lock_irqsave(&osb->dc_task_lock, flags);
  3692. if (osb->dc_work_sequence != osb->dc_wake_sequence)
  3693. should_wake = 1;
  3694. spin_unlock_irqrestore(&osb->dc_task_lock, flags);
  3695. return should_wake;
  3696. }
  3697. static int ocfs2_downconvert_thread(void *arg)
  3698. {
  3699. int status = 0;
  3700. struct ocfs2_super *osb = arg;
  3701. /* only quit once we've been asked to stop and there is no more
  3702. * work available */
  3703. while (!(kthread_should_stop() &&
  3704. ocfs2_downconvert_thread_lists_empty(osb))) {
  3705. wait_event_interruptible(osb->dc_event,
  3706. ocfs2_downconvert_thread_should_wake(osb) ||
  3707. kthread_should_stop());
  3708. mlog(0, "downconvert_thread: awoken\n");
  3709. ocfs2_downconvert_thread_do_work(osb);
  3710. }
  3711. osb->dc_task = NULL;
  3712. return status;
  3713. }
  3714. void ocfs2_wake_downconvert_thread(struct ocfs2_super *osb)
  3715. {
  3716. unsigned long flags;
  3717. spin_lock_irqsave(&osb->dc_task_lock, flags);
  3718. /* make sure the voting thread gets a swipe at whatever changes
  3719. * the caller may have made to the voting state */
  3720. osb->dc_wake_sequence++;
  3721. spin_unlock_irqrestore(&osb->dc_task_lock, flags);
  3722. wake_up(&osb->dc_event);
  3723. }