dlmglue.c 116 KB

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