dlmglue.c 103 KB

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