dlmglue.c 114 KB

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