dlmglue.c 99 KB

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