dlmglue.c 110 KB

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