dlmglue.c 119 KB

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