dlmglue.c 116 KB

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