dlmglue.c 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949
  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/smp_lock.h>
  30. #include <linux/crc32.h>
  31. #include <linux/kthread.h>
  32. #include <linux/pagemap.h>
  33. #include <linux/debugfs.h>
  34. #include <linux/seq_file.h>
  35. #include <cluster/heartbeat.h>
  36. #include <cluster/nodemanager.h>
  37. #include <cluster/tcp.h>
  38. #include <dlm/dlmapi.h>
  39. #define MLOG_MASK_PREFIX ML_DLM_GLUE
  40. #include <cluster/masklog.h>
  41. #include "ocfs2.h"
  42. #include "alloc.h"
  43. #include "dcache.h"
  44. #include "dlmglue.h"
  45. #include "extent_map.h"
  46. #include "file.h"
  47. #include "heartbeat.h"
  48. #include "inode.h"
  49. #include "journal.h"
  50. #include "slot_map.h"
  51. #include "super.h"
  52. #include "uptodate.h"
  53. #include "vote.h"
  54. #include "buffer_head_io.h"
  55. struct ocfs2_mask_waiter {
  56. struct list_head mw_item;
  57. int mw_status;
  58. struct completion mw_complete;
  59. unsigned long mw_mask;
  60. unsigned long mw_goal;
  61. };
  62. static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres);
  63. static struct ocfs2_super *ocfs2_get_inode_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. /*
  92. * OCFS2 Lock Resource Operations
  93. *
  94. * These fine tune the behavior of the generic dlmglue locking infrastructure.
  95. *
  96. * The most basic of lock types can point ->l_priv to their respective
  97. * struct ocfs2_super and allow the default actions to manage things.
  98. *
  99. * Right now, each lock type also needs to implement an init function,
  100. * and trivial lock/unlock wrappers. ocfs2_simple_drop_lockres()
  101. * should be called when the lock is no longer needed (i.e., object
  102. * destruction time).
  103. */
  104. struct ocfs2_lock_res_ops {
  105. /*
  106. * Translate an ocfs2_lock_res * into an ocfs2_super *. Define
  107. * this callback if ->l_priv is not an ocfs2_super pointer
  108. */
  109. struct ocfs2_super * (*get_osb)(struct ocfs2_lock_res *);
  110. /*
  111. * Optionally called in the downconvert (or "vote") thread
  112. * after a successful downconvert. The lockres will not be
  113. * referenced after this callback is called, so it is safe to
  114. * free memory, etc.
  115. *
  116. * The exact semantics of when this is called are controlled
  117. * by ->downconvert_worker()
  118. */
  119. void (*post_unlock)(struct ocfs2_super *, struct ocfs2_lock_res *);
  120. /*
  121. * Allow a lock type to add checks to determine whether it is
  122. * safe to downconvert a lock. Return 0 to re-queue the
  123. * downconvert at a later time, nonzero to continue.
  124. *
  125. * For most locks, the default checks that there are no
  126. * incompatible holders are sufficient.
  127. *
  128. * Called with the lockres spinlock held.
  129. */
  130. int (*check_downconvert)(struct ocfs2_lock_res *, int);
  131. /*
  132. * Allows a lock type to populate the lock value block. This
  133. * is called on downconvert, and when we drop a lock.
  134. *
  135. * Locks that want to use this should set LOCK_TYPE_USES_LVB
  136. * in the flags field.
  137. *
  138. * Called with the lockres spinlock held.
  139. */
  140. void (*set_lvb)(struct ocfs2_lock_res *);
  141. /*
  142. * Called from the downconvert thread when it is determined
  143. * that a lock will be downconverted. This is called without
  144. * any locks held so the function can do work that might
  145. * schedule (syncing out data, etc).
  146. *
  147. * This should return any one of the ocfs2_unblock_action
  148. * values, depending on what it wants the thread to do.
  149. */
  150. int (*downconvert_worker)(struct ocfs2_lock_res *, int);
  151. /*
  152. * LOCK_TYPE_* flags which describe the specific requirements
  153. * of a lock type. Descriptions of each individual flag follow.
  154. */
  155. int flags;
  156. };
  157. /*
  158. * Some locks want to "refresh" potentially stale data when a
  159. * meaningful (PRMODE or EXMODE) lock level is first obtained. If this
  160. * flag is set, the OCFS2_LOCK_NEEDS_REFRESH flag will be set on the
  161. * individual lockres l_flags member from the ast function. It is
  162. * expected that the locking wrapper will clear the
  163. * OCFS2_LOCK_NEEDS_REFRESH flag when done.
  164. */
  165. #define LOCK_TYPE_REQUIRES_REFRESH 0x1
  166. /*
  167. * Indicate that a lock type makes use of the lock value block. The
  168. * ->set_lvb lock type callback must be defined.
  169. */
  170. #define LOCK_TYPE_USES_LVB 0x2
  171. static struct ocfs2_lock_res_ops ocfs2_inode_rw_lops = {
  172. .get_osb = ocfs2_get_inode_osb,
  173. .flags = 0,
  174. };
  175. static struct ocfs2_lock_res_ops ocfs2_inode_meta_lops = {
  176. .get_osb = ocfs2_get_inode_osb,
  177. .check_downconvert = ocfs2_check_meta_downconvert,
  178. .set_lvb = ocfs2_set_meta_lvb,
  179. .flags = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB,
  180. };
  181. static struct ocfs2_lock_res_ops ocfs2_inode_data_lops = {
  182. .get_osb = ocfs2_get_inode_osb,
  183. .downconvert_worker = ocfs2_data_convert_worker,
  184. .flags = 0,
  185. };
  186. static struct ocfs2_lock_res_ops ocfs2_super_lops = {
  187. .flags = LOCK_TYPE_REQUIRES_REFRESH,
  188. };
  189. static struct ocfs2_lock_res_ops ocfs2_rename_lops = {
  190. .flags = 0,
  191. };
  192. static struct ocfs2_lock_res_ops ocfs2_dentry_lops = {
  193. .get_osb = ocfs2_get_dentry_osb,
  194. .post_unlock = ocfs2_dentry_post_unlock,
  195. .downconvert_worker = ocfs2_dentry_convert_worker,
  196. .flags = 0,
  197. };
  198. static inline int ocfs2_is_inode_lock(struct ocfs2_lock_res *lockres)
  199. {
  200. return lockres->l_type == OCFS2_LOCK_TYPE_META ||
  201. lockres->l_type == OCFS2_LOCK_TYPE_DATA ||
  202. lockres->l_type == OCFS2_LOCK_TYPE_RW;
  203. }
  204. static inline struct inode *ocfs2_lock_res_inode(struct ocfs2_lock_res *lockres)
  205. {
  206. BUG_ON(!ocfs2_is_inode_lock(lockres));
  207. return (struct inode *) lockres->l_priv;
  208. }
  209. static inline struct ocfs2_dentry_lock *ocfs2_lock_res_dl(struct ocfs2_lock_res *lockres)
  210. {
  211. BUG_ON(lockres->l_type != OCFS2_LOCK_TYPE_DENTRY);
  212. return (struct ocfs2_dentry_lock *)lockres->l_priv;
  213. }
  214. static inline struct ocfs2_super *ocfs2_get_lockres_osb(struct ocfs2_lock_res *lockres)
  215. {
  216. if (lockres->l_ops->get_osb)
  217. return lockres->l_ops->get_osb(lockres);
  218. return (struct ocfs2_super *)lockres->l_priv;
  219. }
  220. static int ocfs2_lock_create(struct ocfs2_super *osb,
  221. struct ocfs2_lock_res *lockres,
  222. int level,
  223. int dlm_flags);
  224. static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
  225. int wanted);
  226. static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
  227. struct ocfs2_lock_res *lockres,
  228. int level);
  229. static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres);
  230. static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres);
  231. static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres);
  232. static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres, int level);
  233. static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
  234. struct ocfs2_lock_res *lockres);
  235. static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
  236. int convert);
  237. #define ocfs2_log_dlm_error(_func, _stat, _lockres) do { \
  238. mlog(ML_ERROR, "Dlm error \"%s\" while calling %s on " \
  239. "resource %s: %s\n", dlm_errname(_stat), _func, \
  240. _lockres->l_name, dlm_errmsg(_stat)); \
  241. } while (0)
  242. static void ocfs2_vote_on_unlock(struct ocfs2_super *osb,
  243. struct ocfs2_lock_res *lockres);
  244. static int ocfs2_meta_lock_update(struct inode *inode,
  245. struct buffer_head **bh);
  246. static void ocfs2_drop_osb_locks(struct ocfs2_super *osb);
  247. static inline int ocfs2_highest_compat_lock_level(int level);
  248. static void ocfs2_build_lock_name(enum ocfs2_lock_type type,
  249. u64 blkno,
  250. u32 generation,
  251. char *name)
  252. {
  253. int len;
  254. mlog_entry_void();
  255. BUG_ON(type >= OCFS2_NUM_LOCK_TYPES);
  256. len = snprintf(name, OCFS2_LOCK_ID_MAX_LEN, "%c%s%016llx%08x",
  257. ocfs2_lock_type_char(type), OCFS2_LOCK_ID_PAD,
  258. (long long)blkno, generation);
  259. BUG_ON(len != (OCFS2_LOCK_ID_MAX_LEN - 1));
  260. mlog(0, "built lock resource with name: %s\n", name);
  261. mlog_exit_void();
  262. }
  263. static DEFINE_SPINLOCK(ocfs2_dlm_tracking_lock);
  264. static void ocfs2_add_lockres_tracking(struct ocfs2_lock_res *res,
  265. struct ocfs2_dlm_debug *dlm_debug)
  266. {
  267. mlog(0, "Add tracking for lockres %s\n", res->l_name);
  268. spin_lock(&ocfs2_dlm_tracking_lock);
  269. list_add(&res->l_debug_list, &dlm_debug->d_lockres_tracking);
  270. spin_unlock(&ocfs2_dlm_tracking_lock);
  271. }
  272. static void ocfs2_remove_lockres_tracking(struct ocfs2_lock_res *res)
  273. {
  274. spin_lock(&ocfs2_dlm_tracking_lock);
  275. if (!list_empty(&res->l_debug_list))
  276. list_del_init(&res->l_debug_list);
  277. spin_unlock(&ocfs2_dlm_tracking_lock);
  278. }
  279. static void ocfs2_lock_res_init_common(struct ocfs2_super *osb,
  280. struct ocfs2_lock_res *res,
  281. enum ocfs2_lock_type type,
  282. struct ocfs2_lock_res_ops *ops,
  283. void *priv)
  284. {
  285. res->l_type = type;
  286. res->l_ops = ops;
  287. res->l_priv = priv;
  288. res->l_level = LKM_IVMODE;
  289. res->l_requested = LKM_IVMODE;
  290. res->l_blocking = LKM_IVMODE;
  291. res->l_action = OCFS2_AST_INVALID;
  292. res->l_unlock_action = OCFS2_UNLOCK_INVALID;
  293. res->l_flags = OCFS2_LOCK_INITIALIZED;
  294. ocfs2_add_lockres_tracking(res, osb->osb_dlm_debug);
  295. }
  296. void ocfs2_lock_res_init_once(struct ocfs2_lock_res *res)
  297. {
  298. /* This also clears out the lock status block */
  299. memset(res, 0, sizeof(struct ocfs2_lock_res));
  300. spin_lock_init(&res->l_lock);
  301. init_waitqueue_head(&res->l_event);
  302. INIT_LIST_HEAD(&res->l_blocked_list);
  303. INIT_LIST_HEAD(&res->l_mask_waiters);
  304. }
  305. void ocfs2_inode_lock_res_init(struct ocfs2_lock_res *res,
  306. enum ocfs2_lock_type type,
  307. unsigned int generation,
  308. struct inode *inode)
  309. {
  310. struct ocfs2_lock_res_ops *ops;
  311. switch(type) {
  312. case OCFS2_LOCK_TYPE_RW:
  313. ops = &ocfs2_inode_rw_lops;
  314. break;
  315. case OCFS2_LOCK_TYPE_META:
  316. ops = &ocfs2_inode_meta_lops;
  317. break;
  318. case OCFS2_LOCK_TYPE_DATA:
  319. ops = &ocfs2_inode_data_lops;
  320. break;
  321. default:
  322. mlog_bug_on_msg(1, "type: %d\n", type);
  323. ops = NULL; /* thanks, gcc */
  324. break;
  325. };
  326. ocfs2_build_lock_name(type, OCFS2_I(inode)->ip_blkno,
  327. generation, res->l_name);
  328. ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), res, type, ops, inode);
  329. }
  330. static struct ocfs2_super *ocfs2_get_inode_osb(struct ocfs2_lock_res *lockres)
  331. {
  332. struct inode *inode = ocfs2_lock_res_inode(lockres);
  333. return OCFS2_SB(inode->i_sb);
  334. }
  335. static __u64 ocfs2_get_dentry_lock_ino(struct ocfs2_lock_res *lockres)
  336. {
  337. __be64 inode_blkno_be;
  338. memcpy(&inode_blkno_be, &lockres->l_name[OCFS2_DENTRY_LOCK_INO_START],
  339. sizeof(__be64));
  340. return be64_to_cpu(inode_blkno_be);
  341. }
  342. static struct ocfs2_super *ocfs2_get_dentry_osb(struct ocfs2_lock_res *lockres)
  343. {
  344. struct ocfs2_dentry_lock *dl = lockres->l_priv;
  345. return OCFS2_SB(dl->dl_inode->i_sb);
  346. }
  347. void ocfs2_dentry_lock_res_init(struct ocfs2_dentry_lock *dl,
  348. u64 parent, struct inode *inode)
  349. {
  350. int len;
  351. u64 inode_blkno = OCFS2_I(inode)->ip_blkno;
  352. __be64 inode_blkno_be = cpu_to_be64(inode_blkno);
  353. struct ocfs2_lock_res *lockres = &dl->dl_lockres;
  354. ocfs2_lock_res_init_once(lockres);
  355. /*
  356. * Unfortunately, the standard lock naming scheme won't work
  357. * here because we have two 16 byte values to use. Instead,
  358. * we'll stuff the inode number as a binary value. We still
  359. * want error prints to show something without garbling the
  360. * display, so drop a null byte in there before the inode
  361. * number. A future version of OCFS2 will likely use all
  362. * binary lock names. The stringified names have been a
  363. * tremendous aid in debugging, but now that the debugfs
  364. * interface exists, we can mangle things there if need be.
  365. *
  366. * NOTE: We also drop the standard "pad" value (the total lock
  367. * name size stays the same though - the last part is all
  368. * zeros due to the memset in ocfs2_lock_res_init_once()
  369. */
  370. len = snprintf(lockres->l_name, OCFS2_DENTRY_LOCK_INO_START,
  371. "%c%016llx",
  372. ocfs2_lock_type_char(OCFS2_LOCK_TYPE_DENTRY),
  373. (long long)parent);
  374. BUG_ON(len != (OCFS2_DENTRY_LOCK_INO_START - 1));
  375. memcpy(&lockres->l_name[OCFS2_DENTRY_LOCK_INO_START], &inode_blkno_be,
  376. sizeof(__be64));
  377. ocfs2_lock_res_init_common(OCFS2_SB(inode->i_sb), lockres,
  378. OCFS2_LOCK_TYPE_DENTRY, &ocfs2_dentry_lops,
  379. dl);
  380. }
  381. static void ocfs2_super_lock_res_init(struct ocfs2_lock_res *res,
  382. struct ocfs2_super *osb)
  383. {
  384. /* Superblock lockres doesn't come from a slab so we call init
  385. * once on it manually. */
  386. ocfs2_lock_res_init_once(res);
  387. ocfs2_build_lock_name(OCFS2_LOCK_TYPE_SUPER, OCFS2_SUPER_BLOCK_BLKNO,
  388. 0, res->l_name);
  389. ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_SUPER,
  390. &ocfs2_super_lops, osb);
  391. }
  392. static void ocfs2_rename_lock_res_init(struct ocfs2_lock_res *res,
  393. struct ocfs2_super *osb)
  394. {
  395. /* Rename lockres doesn't come from a slab so we call init
  396. * once on it manually. */
  397. ocfs2_lock_res_init_once(res);
  398. ocfs2_build_lock_name(OCFS2_LOCK_TYPE_RENAME, 0, 0, res->l_name);
  399. ocfs2_lock_res_init_common(osb, res, OCFS2_LOCK_TYPE_RENAME,
  400. &ocfs2_rename_lops, osb);
  401. }
  402. void ocfs2_lock_res_free(struct ocfs2_lock_res *res)
  403. {
  404. mlog_entry_void();
  405. if (!(res->l_flags & OCFS2_LOCK_INITIALIZED))
  406. return;
  407. ocfs2_remove_lockres_tracking(res);
  408. mlog_bug_on_msg(!list_empty(&res->l_blocked_list),
  409. "Lockres %s is on the blocked list\n",
  410. res->l_name);
  411. mlog_bug_on_msg(!list_empty(&res->l_mask_waiters),
  412. "Lockres %s has mask waiters pending\n",
  413. res->l_name);
  414. mlog_bug_on_msg(spin_is_locked(&res->l_lock),
  415. "Lockres %s is locked\n",
  416. res->l_name);
  417. mlog_bug_on_msg(res->l_ro_holders,
  418. "Lockres %s has %u ro holders\n",
  419. res->l_name, res->l_ro_holders);
  420. mlog_bug_on_msg(res->l_ex_holders,
  421. "Lockres %s has %u ex holders\n",
  422. res->l_name, res->l_ex_holders);
  423. /* Need to clear out the lock status block for the dlm */
  424. memset(&res->l_lksb, 0, sizeof(res->l_lksb));
  425. res->l_flags = 0UL;
  426. mlog_exit_void();
  427. }
  428. static inline void ocfs2_inc_holders(struct ocfs2_lock_res *lockres,
  429. int level)
  430. {
  431. mlog_entry_void();
  432. BUG_ON(!lockres);
  433. switch(level) {
  434. case LKM_EXMODE:
  435. lockres->l_ex_holders++;
  436. break;
  437. case LKM_PRMODE:
  438. lockres->l_ro_holders++;
  439. break;
  440. default:
  441. BUG();
  442. }
  443. mlog_exit_void();
  444. }
  445. static inline void ocfs2_dec_holders(struct ocfs2_lock_res *lockres,
  446. int level)
  447. {
  448. mlog_entry_void();
  449. BUG_ON(!lockres);
  450. switch(level) {
  451. case LKM_EXMODE:
  452. BUG_ON(!lockres->l_ex_holders);
  453. lockres->l_ex_holders--;
  454. break;
  455. case LKM_PRMODE:
  456. BUG_ON(!lockres->l_ro_holders);
  457. lockres->l_ro_holders--;
  458. break;
  459. default:
  460. BUG();
  461. }
  462. mlog_exit_void();
  463. }
  464. /* WARNING: This function lives in a world where the only three lock
  465. * levels are EX, PR, and NL. It *will* have to be adjusted when more
  466. * lock types are added. */
  467. static inline int ocfs2_highest_compat_lock_level(int level)
  468. {
  469. int new_level = LKM_EXMODE;
  470. if (level == LKM_EXMODE)
  471. new_level = LKM_NLMODE;
  472. else if (level == LKM_PRMODE)
  473. new_level = LKM_PRMODE;
  474. return new_level;
  475. }
  476. static void lockres_set_flags(struct ocfs2_lock_res *lockres,
  477. unsigned long newflags)
  478. {
  479. struct list_head *pos, *tmp;
  480. struct ocfs2_mask_waiter *mw;
  481. assert_spin_locked(&lockres->l_lock);
  482. lockres->l_flags = newflags;
  483. list_for_each_safe(pos, tmp, &lockres->l_mask_waiters) {
  484. mw = list_entry(pos, struct ocfs2_mask_waiter, mw_item);
  485. if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
  486. continue;
  487. list_del_init(&mw->mw_item);
  488. mw->mw_status = 0;
  489. complete(&mw->mw_complete);
  490. }
  491. }
  492. static void lockres_or_flags(struct ocfs2_lock_res *lockres, unsigned long or)
  493. {
  494. lockres_set_flags(lockres, lockres->l_flags | or);
  495. }
  496. static void lockres_clear_flags(struct ocfs2_lock_res *lockres,
  497. unsigned long clear)
  498. {
  499. lockres_set_flags(lockres, lockres->l_flags & ~clear);
  500. }
  501. static inline void ocfs2_generic_handle_downconvert_action(struct ocfs2_lock_res *lockres)
  502. {
  503. mlog_entry_void();
  504. BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
  505. BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
  506. BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
  507. BUG_ON(lockres->l_blocking <= LKM_NLMODE);
  508. lockres->l_level = lockres->l_requested;
  509. if (lockres->l_level <=
  510. ocfs2_highest_compat_lock_level(lockres->l_blocking)) {
  511. lockres->l_blocking = LKM_NLMODE;
  512. lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
  513. }
  514. lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
  515. mlog_exit_void();
  516. }
  517. static inline void ocfs2_generic_handle_convert_action(struct ocfs2_lock_res *lockres)
  518. {
  519. mlog_entry_void();
  520. BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BUSY));
  521. BUG_ON(!(lockres->l_flags & OCFS2_LOCK_ATTACHED));
  522. /* Convert from RO to EX doesn't really need anything as our
  523. * information is already up to data. Convert from NL to
  524. * *anything* however should mark ourselves as needing an
  525. * update */
  526. if (lockres->l_level == LKM_NLMODE &&
  527. lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
  528. lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
  529. lockres->l_level = lockres->l_requested;
  530. lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
  531. mlog_exit_void();
  532. }
  533. static inline void ocfs2_generic_handle_attach_action(struct ocfs2_lock_res *lockres)
  534. {
  535. mlog_entry_void();
  536. BUG_ON((!lockres->l_flags & OCFS2_LOCK_BUSY));
  537. BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
  538. if (lockres->l_requested > LKM_NLMODE &&
  539. !(lockres->l_flags & OCFS2_LOCK_LOCAL) &&
  540. lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
  541. lockres_or_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
  542. lockres->l_level = lockres->l_requested;
  543. lockres_or_flags(lockres, OCFS2_LOCK_ATTACHED);
  544. lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
  545. mlog_exit_void();
  546. }
  547. static int ocfs2_generic_handle_bast(struct ocfs2_lock_res *lockres,
  548. int level)
  549. {
  550. int needs_downconvert = 0;
  551. mlog_entry_void();
  552. assert_spin_locked(&lockres->l_lock);
  553. lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
  554. if (level > lockres->l_blocking) {
  555. /* only schedule a downconvert if we haven't already scheduled
  556. * one that goes low enough to satisfy the level we're
  557. * blocking. this also catches the case where we get
  558. * duplicate BASTs */
  559. if (ocfs2_highest_compat_lock_level(level) <
  560. ocfs2_highest_compat_lock_level(lockres->l_blocking))
  561. needs_downconvert = 1;
  562. lockres->l_blocking = level;
  563. }
  564. mlog_exit(needs_downconvert);
  565. return needs_downconvert;
  566. }
  567. static void ocfs2_blocking_ast(void *opaque, int level)
  568. {
  569. struct ocfs2_lock_res *lockres = opaque;
  570. struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
  571. int needs_downconvert;
  572. unsigned long flags;
  573. BUG_ON(level <= LKM_NLMODE);
  574. mlog(0, "BAST fired for lockres %s, blocking %d, level %d type %s\n",
  575. lockres->l_name, level, lockres->l_level,
  576. ocfs2_lock_type_string(lockres->l_type));
  577. spin_lock_irqsave(&lockres->l_lock, flags);
  578. needs_downconvert = ocfs2_generic_handle_bast(lockres, level);
  579. if (needs_downconvert)
  580. ocfs2_schedule_blocked_lock(osb, lockres);
  581. spin_unlock_irqrestore(&lockres->l_lock, flags);
  582. wake_up(&lockres->l_event);
  583. ocfs2_kick_vote_thread(osb);
  584. }
  585. static void ocfs2_locking_ast(void *opaque)
  586. {
  587. struct ocfs2_lock_res *lockres = opaque;
  588. struct dlm_lockstatus *lksb = &lockres->l_lksb;
  589. unsigned long flags;
  590. spin_lock_irqsave(&lockres->l_lock, flags);
  591. if (lksb->status != DLM_NORMAL) {
  592. mlog(ML_ERROR, "lockres %s: lksb status value of %u!\n",
  593. lockres->l_name, lksb->status);
  594. spin_unlock_irqrestore(&lockres->l_lock, flags);
  595. return;
  596. }
  597. switch(lockres->l_action) {
  598. case OCFS2_AST_ATTACH:
  599. ocfs2_generic_handle_attach_action(lockres);
  600. lockres_clear_flags(lockres, OCFS2_LOCK_LOCAL);
  601. break;
  602. case OCFS2_AST_CONVERT:
  603. ocfs2_generic_handle_convert_action(lockres);
  604. break;
  605. case OCFS2_AST_DOWNCONVERT:
  606. ocfs2_generic_handle_downconvert_action(lockres);
  607. break;
  608. default:
  609. mlog(ML_ERROR, "lockres %s: ast fired with invalid action: %u "
  610. "lockres flags = 0x%lx, unlock action: %u\n",
  611. lockres->l_name, lockres->l_action, lockres->l_flags,
  612. lockres->l_unlock_action);
  613. BUG();
  614. }
  615. /* set it to something invalid so if we get called again we
  616. * can catch it. */
  617. lockres->l_action = OCFS2_AST_INVALID;
  618. wake_up(&lockres->l_event);
  619. spin_unlock_irqrestore(&lockres->l_lock, flags);
  620. }
  621. static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
  622. int convert)
  623. {
  624. unsigned long flags;
  625. mlog_entry_void();
  626. spin_lock_irqsave(&lockres->l_lock, flags);
  627. lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
  628. if (convert)
  629. lockres->l_action = OCFS2_AST_INVALID;
  630. else
  631. lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
  632. spin_unlock_irqrestore(&lockres->l_lock, flags);
  633. wake_up(&lockres->l_event);
  634. mlog_exit_void();
  635. }
  636. /* Note: If we detect another process working on the lock (i.e.,
  637. * OCFS2_LOCK_BUSY), we'll bail out returning 0. It's up to the caller
  638. * to do the right thing in that case.
  639. */
  640. static int ocfs2_lock_create(struct ocfs2_super *osb,
  641. struct ocfs2_lock_res *lockres,
  642. int level,
  643. int dlm_flags)
  644. {
  645. int ret = 0;
  646. enum dlm_status status;
  647. unsigned long flags;
  648. mlog_entry_void();
  649. mlog(0, "lock %s, level = %d, flags = %d\n", lockres->l_name, level,
  650. dlm_flags);
  651. spin_lock_irqsave(&lockres->l_lock, flags);
  652. if ((lockres->l_flags & OCFS2_LOCK_ATTACHED) ||
  653. (lockres->l_flags & OCFS2_LOCK_BUSY)) {
  654. spin_unlock_irqrestore(&lockres->l_lock, flags);
  655. goto bail;
  656. }
  657. lockres->l_action = OCFS2_AST_ATTACH;
  658. lockres->l_requested = level;
  659. lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
  660. spin_unlock_irqrestore(&lockres->l_lock, flags);
  661. status = dlmlock(osb->dlm,
  662. level,
  663. &lockres->l_lksb,
  664. dlm_flags,
  665. lockres->l_name,
  666. OCFS2_LOCK_ID_MAX_LEN - 1,
  667. ocfs2_locking_ast,
  668. lockres,
  669. ocfs2_blocking_ast);
  670. if (status != DLM_NORMAL) {
  671. ocfs2_log_dlm_error("dlmlock", status, lockres);
  672. ret = -EINVAL;
  673. ocfs2_recover_from_dlm_error(lockres, 1);
  674. }
  675. mlog(0, "lock %s, successfull return from dlmlock\n", lockres->l_name);
  676. bail:
  677. mlog_exit(ret);
  678. return ret;
  679. }
  680. static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res *lockres,
  681. int flag)
  682. {
  683. unsigned long flags;
  684. int ret;
  685. spin_lock_irqsave(&lockres->l_lock, flags);
  686. ret = lockres->l_flags & flag;
  687. spin_unlock_irqrestore(&lockres->l_lock, flags);
  688. return ret;
  689. }
  690. static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res *lockres)
  691. {
  692. wait_event(lockres->l_event,
  693. !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_BUSY));
  694. }
  695. static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res *lockres)
  696. {
  697. wait_event(lockres->l_event,
  698. !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_REFRESHING));
  699. }
  700. /* predict what lock level we'll be dropping down to on behalf
  701. * of another node, and return true if the currently wanted
  702. * level will be compatible with it. */
  703. static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
  704. int wanted)
  705. {
  706. BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
  707. return wanted <= ocfs2_highest_compat_lock_level(lockres->l_blocking);
  708. }
  709. static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter *mw)
  710. {
  711. INIT_LIST_HEAD(&mw->mw_item);
  712. init_completion(&mw->mw_complete);
  713. }
  714. static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter *mw)
  715. {
  716. wait_for_completion(&mw->mw_complete);
  717. /* Re-arm the completion in case we want to wait on it again */
  718. INIT_COMPLETION(mw->mw_complete);
  719. return mw->mw_status;
  720. }
  721. static void lockres_add_mask_waiter(struct ocfs2_lock_res *lockres,
  722. struct ocfs2_mask_waiter *mw,
  723. unsigned long mask,
  724. unsigned long goal)
  725. {
  726. BUG_ON(!list_empty(&mw->mw_item));
  727. assert_spin_locked(&lockres->l_lock);
  728. list_add_tail(&mw->mw_item, &lockres->l_mask_waiters);
  729. mw->mw_mask = mask;
  730. mw->mw_goal = goal;
  731. }
  732. /* returns 0 if the mw that was removed was already satisfied, -EBUSY
  733. * if the mask still hadn't reached its goal */
  734. static int lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
  735. struct ocfs2_mask_waiter *mw)
  736. {
  737. unsigned long flags;
  738. int ret = 0;
  739. spin_lock_irqsave(&lockres->l_lock, flags);
  740. if (!list_empty(&mw->mw_item)) {
  741. if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
  742. ret = -EBUSY;
  743. list_del_init(&mw->mw_item);
  744. init_completion(&mw->mw_complete);
  745. }
  746. spin_unlock_irqrestore(&lockres->l_lock, flags);
  747. return ret;
  748. }
  749. static int ocfs2_cluster_lock(struct ocfs2_super *osb,
  750. struct ocfs2_lock_res *lockres,
  751. int level,
  752. int lkm_flags,
  753. int arg_flags)
  754. {
  755. struct ocfs2_mask_waiter mw;
  756. enum dlm_status status;
  757. int wait, catch_signals = !(osb->s_mount_opt & OCFS2_MOUNT_NOINTR);
  758. int ret = 0; /* gcc doesn't realize wait = 1 guarantees ret is set */
  759. unsigned long flags;
  760. mlog_entry_void();
  761. ocfs2_init_mask_waiter(&mw);
  762. if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
  763. lkm_flags |= LKM_VALBLK;
  764. again:
  765. wait = 0;
  766. if (catch_signals && signal_pending(current)) {
  767. ret = -ERESTARTSYS;
  768. goto out;
  769. }
  770. spin_lock_irqsave(&lockres->l_lock, flags);
  771. mlog_bug_on_msg(lockres->l_flags & OCFS2_LOCK_FREEING,
  772. "Cluster lock called on freeing lockres %s! flags "
  773. "0x%lx\n", lockres->l_name, lockres->l_flags);
  774. /* We only compare against the currently granted level
  775. * here. If the lock is blocked waiting on a downconvert,
  776. * we'll get caught below. */
  777. if (lockres->l_flags & OCFS2_LOCK_BUSY &&
  778. level > lockres->l_level) {
  779. /* is someone sitting in dlm_lock? If so, wait on
  780. * them. */
  781. lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
  782. wait = 1;
  783. goto unlock;
  784. }
  785. if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
  786. /* lock has not been created yet. */
  787. spin_unlock_irqrestore(&lockres->l_lock, flags);
  788. ret = ocfs2_lock_create(osb, lockres, LKM_NLMODE, 0);
  789. if (ret < 0) {
  790. mlog_errno(ret);
  791. goto out;
  792. }
  793. goto again;
  794. }
  795. if (lockres->l_flags & OCFS2_LOCK_BLOCKED &&
  796. !ocfs2_may_continue_on_blocked_lock(lockres, level)) {
  797. /* is the lock is currently blocked on behalf of
  798. * another node */
  799. lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BLOCKED, 0);
  800. wait = 1;
  801. goto unlock;
  802. }
  803. if (level > lockres->l_level) {
  804. if (lockres->l_action != OCFS2_AST_INVALID)
  805. mlog(ML_ERROR, "lockres %s has action %u pending\n",
  806. lockres->l_name, lockres->l_action);
  807. lockres->l_action = OCFS2_AST_CONVERT;
  808. lockres->l_requested = level;
  809. lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
  810. spin_unlock_irqrestore(&lockres->l_lock, flags);
  811. BUG_ON(level == LKM_IVMODE);
  812. BUG_ON(level == LKM_NLMODE);
  813. mlog(0, "lock %s, convert from %d to level = %d\n",
  814. lockres->l_name, lockres->l_level, level);
  815. /* call dlm_lock to upgrade lock now */
  816. status = dlmlock(osb->dlm,
  817. level,
  818. &lockres->l_lksb,
  819. lkm_flags|LKM_CONVERT,
  820. lockres->l_name,
  821. OCFS2_LOCK_ID_MAX_LEN - 1,
  822. ocfs2_locking_ast,
  823. lockres,
  824. ocfs2_blocking_ast);
  825. if (status != DLM_NORMAL) {
  826. if ((lkm_flags & LKM_NOQUEUE) &&
  827. (status == DLM_NOTQUEUED))
  828. ret = -EAGAIN;
  829. else {
  830. ocfs2_log_dlm_error("dlmlock", status,
  831. lockres);
  832. ret = -EINVAL;
  833. }
  834. ocfs2_recover_from_dlm_error(lockres, 1);
  835. goto out;
  836. }
  837. mlog(0, "lock %s, successfull return from dlmlock\n",
  838. lockres->l_name);
  839. /* At this point we've gone inside the dlm and need to
  840. * complete our work regardless. */
  841. catch_signals = 0;
  842. /* wait for busy to clear and carry on */
  843. goto again;
  844. }
  845. /* Ok, if we get here then we're good to go. */
  846. ocfs2_inc_holders(lockres, level);
  847. ret = 0;
  848. unlock:
  849. spin_unlock_irqrestore(&lockres->l_lock, flags);
  850. out:
  851. /*
  852. * This is helping work around a lock inversion between the page lock
  853. * and dlm locks. One path holds the page lock while calling aops
  854. * which block acquiring dlm locks. The voting thread holds dlm
  855. * locks while acquiring page locks while down converting data locks.
  856. * This block is helping an aop path notice the inversion and back
  857. * off to unlock its page lock before trying the dlm lock again.
  858. */
  859. if (wait && arg_flags & OCFS2_LOCK_NONBLOCK &&
  860. mw.mw_mask & (OCFS2_LOCK_BUSY|OCFS2_LOCK_BLOCKED)) {
  861. wait = 0;
  862. if (lockres_remove_mask_waiter(lockres, &mw))
  863. ret = -EAGAIN;
  864. else
  865. goto again;
  866. }
  867. if (wait) {
  868. ret = ocfs2_wait_for_mask(&mw);
  869. if (ret == 0)
  870. goto again;
  871. mlog_errno(ret);
  872. }
  873. mlog_exit(ret);
  874. return ret;
  875. }
  876. static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
  877. struct ocfs2_lock_res *lockres,
  878. int level)
  879. {
  880. unsigned long flags;
  881. mlog_entry_void();
  882. spin_lock_irqsave(&lockres->l_lock, flags);
  883. ocfs2_dec_holders(lockres, level);
  884. ocfs2_vote_on_unlock(osb, lockres);
  885. spin_unlock_irqrestore(&lockres->l_lock, flags);
  886. mlog_exit_void();
  887. }
  888. static int ocfs2_create_new_lock(struct ocfs2_super *osb,
  889. struct ocfs2_lock_res *lockres,
  890. int ex,
  891. int local)
  892. {
  893. int level = ex ? LKM_EXMODE : LKM_PRMODE;
  894. unsigned long flags;
  895. int lkm_flags = local ? LKM_LOCAL : 0;
  896. spin_lock_irqsave(&lockres->l_lock, flags);
  897. BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
  898. lockres_or_flags(lockres, OCFS2_LOCK_LOCAL);
  899. spin_unlock_irqrestore(&lockres->l_lock, flags);
  900. return ocfs2_lock_create(osb, lockres, level, lkm_flags);
  901. }
  902. /* Grants us an EX lock on the data and metadata resources, skipping
  903. * the normal cluster directory lookup. Use this ONLY on newly created
  904. * inodes which other nodes can't possibly see, and which haven't been
  905. * hashed in the inode hash yet. This can give us a good performance
  906. * increase as it'll skip the network broadcast normally associated
  907. * with creating a new lock resource. */
  908. int ocfs2_create_new_inode_locks(struct inode *inode)
  909. {
  910. int ret;
  911. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  912. BUG_ON(!inode);
  913. BUG_ON(!ocfs2_inode_is_new(inode));
  914. mlog_entry_void();
  915. mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
  916. /* NOTE: That we don't increment any of the holder counts, nor
  917. * do we add anything to a journal handle. Since this is
  918. * supposed to be a new inode which the cluster doesn't know
  919. * about yet, there is no need to. As far as the LVB handling
  920. * is concerned, this is basically like acquiring an EX lock
  921. * on a resource which has an invalid one -- we'll set it
  922. * valid when we release the EX. */
  923. ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_rw_lockres, 1, 1);
  924. if (ret) {
  925. mlog_errno(ret);
  926. goto bail;
  927. }
  928. /*
  929. * We don't want to use LKM_LOCAL on a meta data lock as they
  930. * don't use a generation in their lock names.
  931. */
  932. ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_meta_lockres, 1, 0);
  933. if (ret) {
  934. mlog_errno(ret);
  935. goto bail;
  936. }
  937. ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_data_lockres, 1, 1);
  938. if (ret) {
  939. mlog_errno(ret);
  940. goto bail;
  941. }
  942. bail:
  943. mlog_exit(ret);
  944. return ret;
  945. }
  946. int ocfs2_rw_lock(struct inode *inode, int write)
  947. {
  948. int status, level;
  949. struct ocfs2_lock_res *lockres;
  950. BUG_ON(!inode);
  951. mlog_entry_void();
  952. mlog(0, "inode %llu take %s RW lock\n",
  953. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  954. write ? "EXMODE" : "PRMODE");
  955. lockres = &OCFS2_I(inode)->ip_rw_lockres;
  956. level = write ? LKM_EXMODE : LKM_PRMODE;
  957. status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level, 0,
  958. 0);
  959. if (status < 0)
  960. mlog_errno(status);
  961. mlog_exit(status);
  962. return status;
  963. }
  964. void ocfs2_rw_unlock(struct inode *inode, int write)
  965. {
  966. int level = write ? LKM_EXMODE : LKM_PRMODE;
  967. struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_rw_lockres;
  968. mlog_entry_void();
  969. mlog(0, "inode %llu drop %s RW lock\n",
  970. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  971. write ? "EXMODE" : "PRMODE");
  972. ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
  973. mlog_exit_void();
  974. }
  975. int ocfs2_data_lock_full(struct inode *inode,
  976. int write,
  977. int arg_flags)
  978. {
  979. int status = 0, level;
  980. struct ocfs2_lock_res *lockres;
  981. BUG_ON(!inode);
  982. mlog_entry_void();
  983. mlog(0, "inode %llu take %s DATA lock\n",
  984. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  985. write ? "EXMODE" : "PRMODE");
  986. /* We'll allow faking a readonly data lock for
  987. * rodevices. */
  988. if (ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb))) {
  989. if (write) {
  990. status = -EROFS;
  991. mlog_errno(status);
  992. }
  993. goto out;
  994. }
  995. lockres = &OCFS2_I(inode)->ip_data_lockres;
  996. level = write ? LKM_EXMODE : LKM_PRMODE;
  997. status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level,
  998. 0, arg_flags);
  999. if (status < 0 && status != -EAGAIN)
  1000. mlog_errno(status);
  1001. out:
  1002. mlog_exit(status);
  1003. return status;
  1004. }
  1005. /* see ocfs2_meta_lock_with_page() */
  1006. int ocfs2_data_lock_with_page(struct inode *inode,
  1007. int write,
  1008. struct page *page)
  1009. {
  1010. int ret;
  1011. ret = ocfs2_data_lock_full(inode, write, OCFS2_LOCK_NONBLOCK);
  1012. if (ret == -EAGAIN) {
  1013. unlock_page(page);
  1014. if (ocfs2_data_lock(inode, write) == 0)
  1015. ocfs2_data_unlock(inode, write);
  1016. ret = AOP_TRUNCATED_PAGE;
  1017. }
  1018. return ret;
  1019. }
  1020. static void ocfs2_vote_on_unlock(struct ocfs2_super *osb,
  1021. struct ocfs2_lock_res *lockres)
  1022. {
  1023. int kick = 0;
  1024. mlog_entry_void();
  1025. /* If we know that another node is waiting on our lock, kick
  1026. * the vote thread * pre-emptively when we reach a release
  1027. * condition. */
  1028. if (lockres->l_flags & OCFS2_LOCK_BLOCKED) {
  1029. switch(lockres->l_blocking) {
  1030. case LKM_EXMODE:
  1031. if (!lockres->l_ex_holders && !lockres->l_ro_holders)
  1032. kick = 1;
  1033. break;
  1034. case LKM_PRMODE:
  1035. if (!lockres->l_ex_holders)
  1036. kick = 1;
  1037. break;
  1038. default:
  1039. BUG();
  1040. }
  1041. }
  1042. if (kick)
  1043. ocfs2_kick_vote_thread(osb);
  1044. mlog_exit_void();
  1045. }
  1046. void ocfs2_data_unlock(struct inode *inode,
  1047. int write)
  1048. {
  1049. int level = write ? LKM_EXMODE : LKM_PRMODE;
  1050. struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_data_lockres;
  1051. mlog_entry_void();
  1052. mlog(0, "inode %llu drop %s DATA lock\n",
  1053. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1054. write ? "EXMODE" : "PRMODE");
  1055. if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)))
  1056. ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
  1057. mlog_exit_void();
  1058. }
  1059. #define OCFS2_SEC_BITS 34
  1060. #define OCFS2_SEC_SHIFT (64 - 34)
  1061. #define OCFS2_NSEC_MASK ((1ULL << OCFS2_SEC_SHIFT) - 1)
  1062. /* LVB only has room for 64 bits of time here so we pack it for
  1063. * now. */
  1064. static u64 ocfs2_pack_timespec(struct timespec *spec)
  1065. {
  1066. u64 res;
  1067. u64 sec = spec->tv_sec;
  1068. u32 nsec = spec->tv_nsec;
  1069. res = (sec << OCFS2_SEC_SHIFT) | (nsec & OCFS2_NSEC_MASK);
  1070. return res;
  1071. }
  1072. /* Call this with the lockres locked. I am reasonably sure we don't
  1073. * need ip_lock in this function as anyone who would be changing those
  1074. * values is supposed to be blocked in ocfs2_meta_lock right now. */
  1075. static void __ocfs2_stuff_meta_lvb(struct inode *inode)
  1076. {
  1077. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1078. struct ocfs2_lock_res *lockres = &oi->ip_meta_lockres;
  1079. struct ocfs2_meta_lvb *lvb;
  1080. mlog_entry_void();
  1081. lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
  1082. /*
  1083. * Invalidate the LVB of a deleted inode - this way other
  1084. * nodes are forced to go to disk and discover the new inode
  1085. * status.
  1086. */
  1087. if (oi->ip_flags & OCFS2_INODE_DELETED) {
  1088. lvb->lvb_version = 0;
  1089. goto out;
  1090. }
  1091. lvb->lvb_version = OCFS2_LVB_VERSION;
  1092. lvb->lvb_isize = cpu_to_be64(i_size_read(inode));
  1093. lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters);
  1094. lvb->lvb_iuid = cpu_to_be32(inode->i_uid);
  1095. lvb->lvb_igid = cpu_to_be32(inode->i_gid);
  1096. lvb->lvb_imode = cpu_to_be16(inode->i_mode);
  1097. lvb->lvb_inlink = cpu_to_be16(inode->i_nlink);
  1098. lvb->lvb_iatime_packed =
  1099. cpu_to_be64(ocfs2_pack_timespec(&inode->i_atime));
  1100. lvb->lvb_ictime_packed =
  1101. cpu_to_be64(ocfs2_pack_timespec(&inode->i_ctime));
  1102. lvb->lvb_imtime_packed =
  1103. cpu_to_be64(ocfs2_pack_timespec(&inode->i_mtime));
  1104. lvb->lvb_iattr = cpu_to_be32(oi->ip_attr);
  1105. lvb->lvb_igeneration = cpu_to_be32(inode->i_generation);
  1106. out:
  1107. mlog_meta_lvb(0, lockres);
  1108. mlog_exit_void();
  1109. }
  1110. static void ocfs2_unpack_timespec(struct timespec *spec,
  1111. u64 packed_time)
  1112. {
  1113. spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
  1114. spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
  1115. }
  1116. static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
  1117. {
  1118. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1119. struct ocfs2_lock_res *lockres = &oi->ip_meta_lockres;
  1120. struct ocfs2_meta_lvb *lvb;
  1121. mlog_entry_void();
  1122. mlog_meta_lvb(0, lockres);
  1123. lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
  1124. /* We're safe here without the lockres lock... */
  1125. spin_lock(&oi->ip_lock);
  1126. oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters);
  1127. i_size_write(inode, be64_to_cpu(lvb->lvb_isize));
  1128. oi->ip_attr = be32_to_cpu(lvb->lvb_iattr);
  1129. ocfs2_set_inode_flags(inode);
  1130. /* fast-symlinks are a special case */
  1131. if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
  1132. inode->i_blocks = 0;
  1133. else
  1134. inode->i_blocks =
  1135. ocfs2_align_bytes_to_sectors(i_size_read(inode));
  1136. inode->i_uid = be32_to_cpu(lvb->lvb_iuid);
  1137. inode->i_gid = be32_to_cpu(lvb->lvb_igid);
  1138. inode->i_mode = be16_to_cpu(lvb->lvb_imode);
  1139. inode->i_nlink = be16_to_cpu(lvb->lvb_inlink);
  1140. ocfs2_unpack_timespec(&inode->i_atime,
  1141. be64_to_cpu(lvb->lvb_iatime_packed));
  1142. ocfs2_unpack_timespec(&inode->i_mtime,
  1143. be64_to_cpu(lvb->lvb_imtime_packed));
  1144. ocfs2_unpack_timespec(&inode->i_ctime,
  1145. be64_to_cpu(lvb->lvb_ictime_packed));
  1146. spin_unlock(&oi->ip_lock);
  1147. mlog_exit_void();
  1148. }
  1149. static inline int ocfs2_meta_lvb_is_trustable(struct inode *inode,
  1150. struct ocfs2_lock_res *lockres)
  1151. {
  1152. struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
  1153. if (lvb->lvb_version == OCFS2_LVB_VERSION
  1154. && be32_to_cpu(lvb->lvb_igeneration) == inode->i_generation)
  1155. return 1;
  1156. return 0;
  1157. }
  1158. /* Determine whether a lock resource needs to be refreshed, and
  1159. * arbitrate who gets to refresh it.
  1160. *
  1161. * 0 means no refresh needed.
  1162. *
  1163. * > 0 means you need to refresh this and you MUST call
  1164. * ocfs2_complete_lock_res_refresh afterwards. */
  1165. static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
  1166. {
  1167. unsigned long flags;
  1168. int status = 0;
  1169. mlog_entry_void();
  1170. refresh_check:
  1171. spin_lock_irqsave(&lockres->l_lock, flags);
  1172. if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
  1173. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1174. goto bail;
  1175. }
  1176. if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
  1177. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1178. ocfs2_wait_on_refreshing_lock(lockres);
  1179. goto refresh_check;
  1180. }
  1181. /* Ok, I'll be the one to refresh this lock. */
  1182. lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
  1183. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1184. status = 1;
  1185. bail:
  1186. mlog_exit(status);
  1187. return status;
  1188. }
  1189. /* If status is non zero, I'll mark it as not being in refresh
  1190. * anymroe, but i won't clear the needs refresh flag. */
  1191. static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
  1192. int status)
  1193. {
  1194. unsigned long flags;
  1195. mlog_entry_void();
  1196. spin_lock_irqsave(&lockres->l_lock, flags);
  1197. lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
  1198. if (!status)
  1199. lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
  1200. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1201. wake_up(&lockres->l_event);
  1202. mlog_exit_void();
  1203. }
  1204. /* may or may not return a bh if it went to disk. */
  1205. static int ocfs2_meta_lock_update(struct inode *inode,
  1206. struct buffer_head **bh)
  1207. {
  1208. int status = 0;
  1209. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1210. struct ocfs2_lock_res *lockres;
  1211. struct ocfs2_dinode *fe;
  1212. mlog_entry_void();
  1213. spin_lock(&oi->ip_lock);
  1214. if (oi->ip_flags & OCFS2_INODE_DELETED) {
  1215. mlog(0, "Orphaned inode %llu was deleted while we "
  1216. "were waiting on a lock. ip_flags = 0x%x\n",
  1217. (unsigned long long)oi->ip_blkno, oi->ip_flags);
  1218. spin_unlock(&oi->ip_lock);
  1219. status = -ENOENT;
  1220. goto bail;
  1221. }
  1222. spin_unlock(&oi->ip_lock);
  1223. lockres = &oi->ip_meta_lockres;
  1224. if (!ocfs2_should_refresh_lock_res(lockres))
  1225. goto bail;
  1226. /* This will discard any caching information we might have had
  1227. * for the inode metadata. */
  1228. ocfs2_metadata_cache_purge(inode);
  1229. /* will do nothing for inode types that don't use the extent
  1230. * map (directories, bitmap files, etc) */
  1231. ocfs2_extent_map_trunc(inode, 0);
  1232. if (ocfs2_meta_lvb_is_trustable(inode, lockres)) {
  1233. mlog(0, "Trusting LVB on inode %llu\n",
  1234. (unsigned long long)oi->ip_blkno);
  1235. ocfs2_refresh_inode_from_lvb(inode);
  1236. } else {
  1237. /* Boo, we have to go to disk. */
  1238. /* read bh, cast, ocfs2_refresh_inode */
  1239. status = ocfs2_read_block(OCFS2_SB(inode->i_sb), oi->ip_blkno,
  1240. bh, OCFS2_BH_CACHED, inode);
  1241. if (status < 0) {
  1242. mlog_errno(status);
  1243. goto bail_refresh;
  1244. }
  1245. fe = (struct ocfs2_dinode *) (*bh)->b_data;
  1246. /* This is a good chance to make sure we're not
  1247. * locking an invalid object.
  1248. *
  1249. * We bug on a stale inode here because we checked
  1250. * above whether it was wiped from disk. The wiping
  1251. * node provides a guarantee that we receive that
  1252. * message and can mark the inode before dropping any
  1253. * locks associated with it. */
  1254. if (!OCFS2_IS_VALID_DINODE(fe)) {
  1255. OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
  1256. status = -EIO;
  1257. goto bail_refresh;
  1258. }
  1259. mlog_bug_on_msg(inode->i_generation !=
  1260. le32_to_cpu(fe->i_generation),
  1261. "Invalid dinode %llu disk generation: %u "
  1262. "inode->i_generation: %u\n",
  1263. (unsigned long long)oi->ip_blkno,
  1264. le32_to_cpu(fe->i_generation),
  1265. inode->i_generation);
  1266. mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
  1267. !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
  1268. "Stale dinode %llu dtime: %llu flags: 0x%x\n",
  1269. (unsigned long long)oi->ip_blkno,
  1270. (unsigned long long)le64_to_cpu(fe->i_dtime),
  1271. le32_to_cpu(fe->i_flags));
  1272. ocfs2_refresh_inode(inode, fe);
  1273. }
  1274. status = 0;
  1275. bail_refresh:
  1276. ocfs2_complete_lock_res_refresh(lockres, status);
  1277. bail:
  1278. mlog_exit(status);
  1279. return status;
  1280. }
  1281. static int ocfs2_assign_bh(struct inode *inode,
  1282. struct buffer_head **ret_bh,
  1283. struct buffer_head *passed_bh)
  1284. {
  1285. int status;
  1286. if (passed_bh) {
  1287. /* Ok, the update went to disk for us, use the
  1288. * returned bh. */
  1289. *ret_bh = passed_bh;
  1290. get_bh(*ret_bh);
  1291. return 0;
  1292. }
  1293. status = ocfs2_read_block(OCFS2_SB(inode->i_sb),
  1294. OCFS2_I(inode)->ip_blkno,
  1295. ret_bh,
  1296. OCFS2_BH_CACHED,
  1297. inode);
  1298. if (status < 0)
  1299. mlog_errno(status);
  1300. return status;
  1301. }
  1302. /*
  1303. * returns < 0 error if the callback will never be called, otherwise
  1304. * the result of the lock will be communicated via the callback.
  1305. */
  1306. int ocfs2_meta_lock_full(struct inode *inode,
  1307. struct buffer_head **ret_bh,
  1308. int ex,
  1309. int arg_flags)
  1310. {
  1311. int status, level, dlm_flags, acquired;
  1312. struct ocfs2_lock_res *lockres;
  1313. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1314. struct buffer_head *local_bh = NULL;
  1315. BUG_ON(!inode);
  1316. mlog_entry_void();
  1317. mlog(0, "inode %llu, take %s META lock\n",
  1318. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1319. ex ? "EXMODE" : "PRMODE");
  1320. status = 0;
  1321. acquired = 0;
  1322. /* We'll allow faking a readonly metadata lock for
  1323. * rodevices. */
  1324. if (ocfs2_is_hard_readonly(osb)) {
  1325. if (ex)
  1326. status = -EROFS;
  1327. goto bail;
  1328. }
  1329. if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
  1330. wait_event(osb->recovery_event,
  1331. ocfs2_node_map_is_empty(osb, &osb->recovery_map));
  1332. acquired = 0;
  1333. lockres = &OCFS2_I(inode)->ip_meta_lockres;
  1334. level = ex ? LKM_EXMODE : LKM_PRMODE;
  1335. dlm_flags = 0;
  1336. if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
  1337. dlm_flags |= LKM_NOQUEUE;
  1338. status = ocfs2_cluster_lock(osb, lockres, level, dlm_flags, arg_flags);
  1339. if (status < 0) {
  1340. if (status != -EAGAIN && status != -EIOCBRETRY)
  1341. mlog_errno(status);
  1342. goto bail;
  1343. }
  1344. /* Notify the error cleanup path to drop the cluster lock. */
  1345. acquired = 1;
  1346. /* We wait twice because a node may have died while we were in
  1347. * the lower dlm layers. The second time though, we've
  1348. * committed to owning this lock so we don't allow signals to
  1349. * abort the operation. */
  1350. if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
  1351. wait_event(osb->recovery_event,
  1352. ocfs2_node_map_is_empty(osb, &osb->recovery_map));
  1353. /*
  1354. * We only see this flag if we're being called from
  1355. * ocfs2_read_locked_inode(). It means we're locking an inode
  1356. * which hasn't been populated yet, so clear the refresh flag
  1357. * and let the caller handle it.
  1358. */
  1359. if (inode->i_state & I_NEW) {
  1360. status = 0;
  1361. ocfs2_complete_lock_res_refresh(lockres, 0);
  1362. goto bail;
  1363. }
  1364. /* This is fun. The caller may want a bh back, or it may
  1365. * not. ocfs2_meta_lock_update definitely wants one in, but
  1366. * may or may not read one, depending on what's in the
  1367. * LVB. The result of all of this is that we've *only* gone to
  1368. * disk if we have to, so the complexity is worthwhile. */
  1369. status = ocfs2_meta_lock_update(inode, &local_bh);
  1370. if (status < 0) {
  1371. if (status != -ENOENT)
  1372. mlog_errno(status);
  1373. goto bail;
  1374. }
  1375. if (ret_bh) {
  1376. status = ocfs2_assign_bh(inode, ret_bh, local_bh);
  1377. if (status < 0) {
  1378. mlog_errno(status);
  1379. goto bail;
  1380. }
  1381. }
  1382. bail:
  1383. if (status < 0) {
  1384. if (ret_bh && (*ret_bh)) {
  1385. brelse(*ret_bh);
  1386. *ret_bh = NULL;
  1387. }
  1388. if (acquired)
  1389. ocfs2_meta_unlock(inode, ex);
  1390. }
  1391. if (local_bh)
  1392. brelse(local_bh);
  1393. mlog_exit(status);
  1394. return status;
  1395. }
  1396. /*
  1397. * This is working around a lock inversion between tasks acquiring DLM locks
  1398. * while holding a page lock and the vote thread which blocks dlm lock acquiry
  1399. * while acquiring page locks.
  1400. *
  1401. * ** These _with_page variantes are only intended to be called from aop
  1402. * methods that hold page locks and return a very specific *positive* error
  1403. * code that aop methods pass up to the VFS -- test for errors with != 0. **
  1404. *
  1405. * The DLM is called such that it returns -EAGAIN if it would have blocked
  1406. * waiting for the vote thread. In that case we unlock our page so the vote
  1407. * thread can make progress. Once we've done this we have to return
  1408. * AOP_TRUNCATED_PAGE so the aop method that called us can bubble that back up
  1409. * into the VFS who will then immediately retry the aop call.
  1410. *
  1411. * We do a blocking lock and immediate unlock before returning, though, so that
  1412. * the lock has a great chance of being cached on this node by the time the VFS
  1413. * calls back to retry the aop. This has a potential to livelock as nodes
  1414. * ping locks back and forth, but that's a risk we're willing to take to avoid
  1415. * the lock inversion simply.
  1416. */
  1417. int ocfs2_meta_lock_with_page(struct inode *inode,
  1418. struct buffer_head **ret_bh,
  1419. int ex,
  1420. struct page *page)
  1421. {
  1422. int ret;
  1423. ret = ocfs2_meta_lock_full(inode, ret_bh, ex, OCFS2_LOCK_NONBLOCK);
  1424. if (ret == -EAGAIN) {
  1425. unlock_page(page);
  1426. if (ocfs2_meta_lock(inode, ret_bh, ex) == 0)
  1427. ocfs2_meta_unlock(inode, ex);
  1428. ret = AOP_TRUNCATED_PAGE;
  1429. }
  1430. return ret;
  1431. }
  1432. int ocfs2_meta_lock_atime(struct inode *inode,
  1433. struct vfsmount *vfsmnt,
  1434. int *level)
  1435. {
  1436. int ret;
  1437. mlog_entry_void();
  1438. ret = ocfs2_meta_lock(inode, NULL, 0);
  1439. if (ret < 0) {
  1440. mlog_errno(ret);
  1441. return ret;
  1442. }
  1443. /*
  1444. * If we should update atime, we will get EX lock,
  1445. * otherwise we just get PR lock.
  1446. */
  1447. if (ocfs2_should_update_atime(inode, vfsmnt)) {
  1448. struct buffer_head *bh = NULL;
  1449. ocfs2_meta_unlock(inode, 0);
  1450. ret = ocfs2_meta_lock(inode, &bh, 1);
  1451. if (ret < 0) {
  1452. mlog_errno(ret);
  1453. return ret;
  1454. }
  1455. *level = 1;
  1456. if (ocfs2_should_update_atime(inode, vfsmnt))
  1457. ocfs2_update_inode_atime(inode, bh);
  1458. if (bh)
  1459. brelse(bh);
  1460. } else
  1461. *level = 0;
  1462. mlog_exit(ret);
  1463. return ret;
  1464. }
  1465. void ocfs2_meta_unlock(struct inode *inode,
  1466. int ex)
  1467. {
  1468. int level = ex ? LKM_EXMODE : LKM_PRMODE;
  1469. struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_meta_lockres;
  1470. mlog_entry_void();
  1471. mlog(0, "inode %llu drop %s META lock\n",
  1472. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1473. ex ? "EXMODE" : "PRMODE");
  1474. if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)))
  1475. ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
  1476. mlog_exit_void();
  1477. }
  1478. int ocfs2_super_lock(struct ocfs2_super *osb,
  1479. int ex)
  1480. {
  1481. int status;
  1482. int level = ex ? LKM_EXMODE : LKM_PRMODE;
  1483. struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
  1484. struct buffer_head *bh;
  1485. struct ocfs2_slot_info *si = osb->slot_info;
  1486. mlog_entry_void();
  1487. if (ocfs2_is_hard_readonly(osb))
  1488. return -EROFS;
  1489. status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
  1490. if (status < 0) {
  1491. mlog_errno(status);
  1492. goto bail;
  1493. }
  1494. /* The super block lock path is really in the best position to
  1495. * know when resources covered by the lock need to be
  1496. * refreshed, so we do it here. Of course, making sense of
  1497. * everything is up to the caller :) */
  1498. status = ocfs2_should_refresh_lock_res(lockres);
  1499. if (status < 0) {
  1500. mlog_errno(status);
  1501. goto bail;
  1502. }
  1503. if (status) {
  1504. bh = si->si_bh;
  1505. status = ocfs2_read_block(osb, bh->b_blocknr, &bh, 0,
  1506. si->si_inode);
  1507. if (status == 0)
  1508. ocfs2_update_slot_info(si);
  1509. ocfs2_complete_lock_res_refresh(lockres, status);
  1510. if (status < 0)
  1511. mlog_errno(status);
  1512. }
  1513. bail:
  1514. mlog_exit(status);
  1515. return status;
  1516. }
  1517. void ocfs2_super_unlock(struct ocfs2_super *osb,
  1518. int ex)
  1519. {
  1520. int level = ex ? LKM_EXMODE : LKM_PRMODE;
  1521. struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
  1522. ocfs2_cluster_unlock(osb, lockres, level);
  1523. }
  1524. int ocfs2_rename_lock(struct ocfs2_super *osb)
  1525. {
  1526. int status;
  1527. struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
  1528. if (ocfs2_is_hard_readonly(osb))
  1529. return -EROFS;
  1530. status = ocfs2_cluster_lock(osb, lockres, LKM_EXMODE, 0, 0);
  1531. if (status < 0)
  1532. mlog_errno(status);
  1533. return status;
  1534. }
  1535. void ocfs2_rename_unlock(struct ocfs2_super *osb)
  1536. {
  1537. struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
  1538. ocfs2_cluster_unlock(osb, lockres, LKM_EXMODE);
  1539. }
  1540. int ocfs2_dentry_lock(struct dentry *dentry, int ex)
  1541. {
  1542. int ret;
  1543. int level = ex ? LKM_EXMODE : LKM_PRMODE;
  1544. struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
  1545. struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
  1546. BUG_ON(!dl);
  1547. if (ocfs2_is_hard_readonly(osb))
  1548. return -EROFS;
  1549. ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0);
  1550. if (ret < 0)
  1551. mlog_errno(ret);
  1552. return ret;
  1553. }
  1554. void ocfs2_dentry_unlock(struct dentry *dentry, int ex)
  1555. {
  1556. int level = ex ? LKM_EXMODE : LKM_PRMODE;
  1557. struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
  1558. struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
  1559. ocfs2_cluster_unlock(osb, &dl->dl_lockres, level);
  1560. }
  1561. /* Reference counting of the dlm debug structure. We want this because
  1562. * open references on the debug inodes can live on after a mount, so
  1563. * we can't rely on the ocfs2_super to always exist. */
  1564. static void ocfs2_dlm_debug_free(struct kref *kref)
  1565. {
  1566. struct ocfs2_dlm_debug *dlm_debug;
  1567. dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
  1568. kfree(dlm_debug);
  1569. }
  1570. void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
  1571. {
  1572. if (dlm_debug)
  1573. kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
  1574. }
  1575. static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
  1576. {
  1577. kref_get(&debug->d_refcnt);
  1578. }
  1579. struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
  1580. {
  1581. struct ocfs2_dlm_debug *dlm_debug;
  1582. dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
  1583. if (!dlm_debug) {
  1584. mlog_errno(-ENOMEM);
  1585. goto out;
  1586. }
  1587. kref_init(&dlm_debug->d_refcnt);
  1588. INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
  1589. dlm_debug->d_locking_state = NULL;
  1590. out:
  1591. return dlm_debug;
  1592. }
  1593. /* Access to this is arbitrated for us via seq_file->sem. */
  1594. struct ocfs2_dlm_seq_priv {
  1595. struct ocfs2_dlm_debug *p_dlm_debug;
  1596. struct ocfs2_lock_res p_iter_res;
  1597. struct ocfs2_lock_res p_tmp_res;
  1598. };
  1599. static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
  1600. struct ocfs2_dlm_seq_priv *priv)
  1601. {
  1602. struct ocfs2_lock_res *iter, *ret = NULL;
  1603. struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
  1604. assert_spin_locked(&ocfs2_dlm_tracking_lock);
  1605. list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
  1606. /* discover the head of the list */
  1607. if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
  1608. mlog(0, "End of list found, %p\n", ret);
  1609. break;
  1610. }
  1611. /* We track our "dummy" iteration lockres' by a NULL
  1612. * l_ops field. */
  1613. if (iter->l_ops != NULL) {
  1614. ret = iter;
  1615. break;
  1616. }
  1617. }
  1618. return ret;
  1619. }
  1620. static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
  1621. {
  1622. struct ocfs2_dlm_seq_priv *priv = m->private;
  1623. struct ocfs2_lock_res *iter;
  1624. spin_lock(&ocfs2_dlm_tracking_lock);
  1625. iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
  1626. if (iter) {
  1627. /* Since lockres' have the lifetime of their container
  1628. * (which can be inodes, ocfs2_supers, etc) we want to
  1629. * copy this out to a temporary lockres while still
  1630. * under the spinlock. Obviously after this we can't
  1631. * trust any pointers on the copy returned, but that's
  1632. * ok as the information we want isn't typically held
  1633. * in them. */
  1634. priv->p_tmp_res = *iter;
  1635. iter = &priv->p_tmp_res;
  1636. }
  1637. spin_unlock(&ocfs2_dlm_tracking_lock);
  1638. return iter;
  1639. }
  1640. static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
  1641. {
  1642. }
  1643. static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
  1644. {
  1645. struct ocfs2_dlm_seq_priv *priv = m->private;
  1646. struct ocfs2_lock_res *iter = v;
  1647. struct ocfs2_lock_res *dummy = &priv->p_iter_res;
  1648. spin_lock(&ocfs2_dlm_tracking_lock);
  1649. iter = ocfs2_dlm_next_res(iter, priv);
  1650. list_del_init(&dummy->l_debug_list);
  1651. if (iter) {
  1652. list_add(&dummy->l_debug_list, &iter->l_debug_list);
  1653. priv->p_tmp_res = *iter;
  1654. iter = &priv->p_tmp_res;
  1655. }
  1656. spin_unlock(&ocfs2_dlm_tracking_lock);
  1657. return iter;
  1658. }
  1659. /* So that debugfs.ocfs2 can determine which format is being used */
  1660. #define OCFS2_DLM_DEBUG_STR_VERSION 1
  1661. static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
  1662. {
  1663. int i;
  1664. char *lvb;
  1665. struct ocfs2_lock_res *lockres = v;
  1666. if (!lockres)
  1667. return -EINVAL;
  1668. seq_printf(m, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION);
  1669. if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY)
  1670. seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1,
  1671. lockres->l_name,
  1672. (unsigned int)ocfs2_get_dentry_lock_ino(lockres));
  1673. else
  1674. seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name);
  1675. seq_printf(m, "%d\t"
  1676. "0x%lx\t"
  1677. "0x%x\t"
  1678. "0x%x\t"
  1679. "%u\t"
  1680. "%u\t"
  1681. "%d\t"
  1682. "%d\t",
  1683. lockres->l_level,
  1684. lockres->l_flags,
  1685. lockres->l_action,
  1686. lockres->l_unlock_action,
  1687. lockres->l_ro_holders,
  1688. lockres->l_ex_holders,
  1689. lockres->l_requested,
  1690. lockres->l_blocking);
  1691. /* Dump the raw LVB */
  1692. lvb = lockres->l_lksb.lvb;
  1693. for(i = 0; i < DLM_LVB_LEN; i++)
  1694. seq_printf(m, "0x%x\t", lvb[i]);
  1695. /* End the line */
  1696. seq_printf(m, "\n");
  1697. return 0;
  1698. }
  1699. static struct seq_operations ocfs2_dlm_seq_ops = {
  1700. .start = ocfs2_dlm_seq_start,
  1701. .stop = ocfs2_dlm_seq_stop,
  1702. .next = ocfs2_dlm_seq_next,
  1703. .show = ocfs2_dlm_seq_show,
  1704. };
  1705. static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
  1706. {
  1707. struct seq_file *seq = (struct seq_file *) file->private_data;
  1708. struct ocfs2_dlm_seq_priv *priv = seq->private;
  1709. struct ocfs2_lock_res *res = &priv->p_iter_res;
  1710. ocfs2_remove_lockres_tracking(res);
  1711. ocfs2_put_dlm_debug(priv->p_dlm_debug);
  1712. return seq_release_private(inode, file);
  1713. }
  1714. static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
  1715. {
  1716. int ret;
  1717. struct ocfs2_dlm_seq_priv *priv;
  1718. struct seq_file *seq;
  1719. struct ocfs2_super *osb;
  1720. priv = kzalloc(sizeof(struct ocfs2_dlm_seq_priv), GFP_KERNEL);
  1721. if (!priv) {
  1722. ret = -ENOMEM;
  1723. mlog_errno(ret);
  1724. goto out;
  1725. }
  1726. osb = inode->i_private;
  1727. ocfs2_get_dlm_debug(osb->osb_dlm_debug);
  1728. priv->p_dlm_debug = osb->osb_dlm_debug;
  1729. INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
  1730. ret = seq_open(file, &ocfs2_dlm_seq_ops);
  1731. if (ret) {
  1732. kfree(priv);
  1733. mlog_errno(ret);
  1734. goto out;
  1735. }
  1736. seq = (struct seq_file *) file->private_data;
  1737. seq->private = priv;
  1738. ocfs2_add_lockres_tracking(&priv->p_iter_res,
  1739. priv->p_dlm_debug);
  1740. out:
  1741. return ret;
  1742. }
  1743. static const struct file_operations ocfs2_dlm_debug_fops = {
  1744. .open = ocfs2_dlm_debug_open,
  1745. .release = ocfs2_dlm_debug_release,
  1746. .read = seq_read,
  1747. .llseek = seq_lseek,
  1748. };
  1749. static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
  1750. {
  1751. int ret = 0;
  1752. struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
  1753. dlm_debug->d_locking_state = debugfs_create_file("locking_state",
  1754. S_IFREG|S_IRUSR,
  1755. osb->osb_debug_root,
  1756. osb,
  1757. &ocfs2_dlm_debug_fops);
  1758. if (!dlm_debug->d_locking_state) {
  1759. ret = -EINVAL;
  1760. mlog(ML_ERROR,
  1761. "Unable to create locking state debugfs file.\n");
  1762. goto out;
  1763. }
  1764. ocfs2_get_dlm_debug(dlm_debug);
  1765. out:
  1766. return ret;
  1767. }
  1768. static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
  1769. {
  1770. struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
  1771. if (dlm_debug) {
  1772. debugfs_remove(dlm_debug->d_locking_state);
  1773. ocfs2_put_dlm_debug(dlm_debug);
  1774. }
  1775. }
  1776. int ocfs2_dlm_init(struct ocfs2_super *osb)
  1777. {
  1778. int status;
  1779. u32 dlm_key;
  1780. struct dlm_ctxt *dlm;
  1781. mlog_entry_void();
  1782. status = ocfs2_dlm_init_debug(osb);
  1783. if (status < 0) {
  1784. mlog_errno(status);
  1785. goto bail;
  1786. }
  1787. /* launch vote thread */
  1788. osb->vote_task = kthread_run(ocfs2_vote_thread, osb, "ocfs2vote");
  1789. if (IS_ERR(osb->vote_task)) {
  1790. status = PTR_ERR(osb->vote_task);
  1791. osb->vote_task = NULL;
  1792. mlog_errno(status);
  1793. goto bail;
  1794. }
  1795. /* used by the dlm code to make message headers unique, each
  1796. * node in this domain must agree on this. */
  1797. dlm_key = crc32_le(0, osb->uuid_str, strlen(osb->uuid_str));
  1798. /* for now, uuid == domain */
  1799. dlm = dlm_register_domain(osb->uuid_str, dlm_key);
  1800. if (IS_ERR(dlm)) {
  1801. status = PTR_ERR(dlm);
  1802. mlog_errno(status);
  1803. goto bail;
  1804. }
  1805. ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
  1806. ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
  1807. dlm_register_eviction_cb(dlm, &osb->osb_eviction_cb);
  1808. osb->dlm = dlm;
  1809. status = 0;
  1810. bail:
  1811. if (status < 0) {
  1812. ocfs2_dlm_shutdown_debug(osb);
  1813. if (osb->vote_task)
  1814. kthread_stop(osb->vote_task);
  1815. }
  1816. mlog_exit(status);
  1817. return status;
  1818. }
  1819. void ocfs2_dlm_shutdown(struct ocfs2_super *osb)
  1820. {
  1821. mlog_entry_void();
  1822. dlm_unregister_eviction_cb(&osb->osb_eviction_cb);
  1823. ocfs2_drop_osb_locks(osb);
  1824. if (osb->vote_task) {
  1825. kthread_stop(osb->vote_task);
  1826. osb->vote_task = NULL;
  1827. }
  1828. ocfs2_lock_res_free(&osb->osb_super_lockres);
  1829. ocfs2_lock_res_free(&osb->osb_rename_lockres);
  1830. dlm_unregister_domain(osb->dlm);
  1831. osb->dlm = NULL;
  1832. ocfs2_dlm_shutdown_debug(osb);
  1833. mlog_exit_void();
  1834. }
  1835. static void ocfs2_unlock_ast(void *opaque, enum dlm_status status)
  1836. {
  1837. struct ocfs2_lock_res *lockres = opaque;
  1838. unsigned long flags;
  1839. mlog_entry_void();
  1840. mlog(0, "UNLOCK AST called on lock %s, action = %d\n", lockres->l_name,
  1841. lockres->l_unlock_action);
  1842. spin_lock_irqsave(&lockres->l_lock, flags);
  1843. /* We tried to cancel a convert request, but it was already
  1844. * granted. All we want to do here is clear our unlock
  1845. * state. The wake_up call done at the bottom is redundant
  1846. * (ocfs2_prepare_cancel_convert doesn't sleep on this) but doesn't
  1847. * hurt anything anyway */
  1848. if (status == DLM_CANCELGRANT &&
  1849. lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
  1850. mlog(0, "Got cancelgrant for %s\n", lockres->l_name);
  1851. /* We don't clear the busy flag in this case as it
  1852. * should have been cleared by the ast which the dlm
  1853. * has called. */
  1854. goto complete_unlock;
  1855. }
  1856. if (status != DLM_NORMAL) {
  1857. mlog(ML_ERROR, "Dlm passes status %d for lock %s, "
  1858. "unlock_action %d\n", status, lockres->l_name,
  1859. lockres->l_unlock_action);
  1860. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1861. return;
  1862. }
  1863. switch(lockres->l_unlock_action) {
  1864. case OCFS2_UNLOCK_CANCEL_CONVERT:
  1865. mlog(0, "Cancel convert success for %s\n", lockres->l_name);
  1866. lockres->l_action = OCFS2_AST_INVALID;
  1867. break;
  1868. case OCFS2_UNLOCK_DROP_LOCK:
  1869. lockres->l_level = LKM_IVMODE;
  1870. break;
  1871. default:
  1872. BUG();
  1873. }
  1874. lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
  1875. complete_unlock:
  1876. lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
  1877. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1878. wake_up(&lockres->l_event);
  1879. mlog_exit_void();
  1880. }
  1881. static int ocfs2_drop_lock(struct ocfs2_super *osb,
  1882. struct ocfs2_lock_res *lockres)
  1883. {
  1884. enum dlm_status status;
  1885. unsigned long flags;
  1886. int lkm_flags = 0;
  1887. /* We didn't get anywhere near actually using this lockres. */
  1888. if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
  1889. goto out;
  1890. if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
  1891. lkm_flags |= LKM_VALBLK;
  1892. spin_lock_irqsave(&lockres->l_lock, flags);
  1893. mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
  1894. "lockres %s, flags 0x%lx\n",
  1895. lockres->l_name, lockres->l_flags);
  1896. while (lockres->l_flags & OCFS2_LOCK_BUSY) {
  1897. mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
  1898. "%u, unlock_action = %u\n",
  1899. lockres->l_name, lockres->l_flags, lockres->l_action,
  1900. lockres->l_unlock_action);
  1901. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1902. /* XXX: Today we just wait on any busy
  1903. * locks... Perhaps we need to cancel converts in the
  1904. * future? */
  1905. ocfs2_wait_on_busy_lock(lockres);
  1906. spin_lock_irqsave(&lockres->l_lock, flags);
  1907. }
  1908. if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
  1909. if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
  1910. lockres->l_level == LKM_EXMODE &&
  1911. !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
  1912. lockres->l_ops->set_lvb(lockres);
  1913. }
  1914. if (lockres->l_flags & OCFS2_LOCK_BUSY)
  1915. mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
  1916. lockres->l_name);
  1917. if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
  1918. mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
  1919. if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
  1920. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1921. goto out;
  1922. }
  1923. lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
  1924. /* make sure we never get here while waiting for an ast to
  1925. * fire. */
  1926. BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
  1927. /* is this necessary? */
  1928. lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
  1929. lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
  1930. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1931. mlog(0, "lock %s\n", lockres->l_name);
  1932. status = dlmunlock(osb->dlm, &lockres->l_lksb, lkm_flags,
  1933. ocfs2_unlock_ast, lockres);
  1934. if (status != DLM_NORMAL) {
  1935. ocfs2_log_dlm_error("dlmunlock", status, lockres);
  1936. mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
  1937. dlm_print_one_lock(lockres->l_lksb.lockid);
  1938. BUG();
  1939. }
  1940. mlog(0, "lock %s, successfull return from dlmunlock\n",
  1941. lockres->l_name);
  1942. ocfs2_wait_on_busy_lock(lockres);
  1943. out:
  1944. mlog_exit(0);
  1945. return 0;
  1946. }
  1947. /* Mark the lockres as being dropped. It will no longer be
  1948. * queued if blocking, but we still may have to wait on it
  1949. * being dequeued from the vote thread before we can consider
  1950. * it safe to drop.
  1951. *
  1952. * You can *not* attempt to call cluster_lock on this lockres anymore. */
  1953. void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres)
  1954. {
  1955. int status;
  1956. struct ocfs2_mask_waiter mw;
  1957. unsigned long flags;
  1958. ocfs2_init_mask_waiter(&mw);
  1959. spin_lock_irqsave(&lockres->l_lock, flags);
  1960. lockres->l_flags |= OCFS2_LOCK_FREEING;
  1961. while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
  1962. lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
  1963. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1964. mlog(0, "Waiting on lockres %s\n", lockres->l_name);
  1965. status = ocfs2_wait_for_mask(&mw);
  1966. if (status)
  1967. mlog_errno(status);
  1968. spin_lock_irqsave(&lockres->l_lock, flags);
  1969. }
  1970. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1971. }
  1972. void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
  1973. struct ocfs2_lock_res *lockres)
  1974. {
  1975. int ret;
  1976. ocfs2_mark_lockres_freeing(lockres);
  1977. ret = ocfs2_drop_lock(osb, lockres);
  1978. if (ret)
  1979. mlog_errno(ret);
  1980. }
  1981. static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
  1982. {
  1983. ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres);
  1984. ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres);
  1985. }
  1986. int ocfs2_drop_inode_locks(struct inode *inode)
  1987. {
  1988. int status, err;
  1989. mlog_entry_void();
  1990. /* No need to call ocfs2_mark_lockres_freeing here -
  1991. * ocfs2_clear_inode has done it for us. */
  1992. err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
  1993. &OCFS2_I(inode)->ip_data_lockres);
  1994. if (err < 0)
  1995. mlog_errno(err);
  1996. status = err;
  1997. err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
  1998. &OCFS2_I(inode)->ip_meta_lockres);
  1999. if (err < 0)
  2000. mlog_errno(err);
  2001. if (err < 0 && !status)
  2002. status = err;
  2003. err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
  2004. &OCFS2_I(inode)->ip_rw_lockres);
  2005. if (err < 0)
  2006. mlog_errno(err);
  2007. if (err < 0 && !status)
  2008. status = err;
  2009. mlog_exit(status);
  2010. return status;
  2011. }
  2012. static void ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
  2013. int new_level)
  2014. {
  2015. assert_spin_locked(&lockres->l_lock);
  2016. BUG_ON(lockres->l_blocking <= LKM_NLMODE);
  2017. if (lockres->l_level <= new_level) {
  2018. mlog(ML_ERROR, "lockres->l_level (%u) <= new_level (%u)\n",
  2019. lockres->l_level, new_level);
  2020. BUG();
  2021. }
  2022. mlog(0, "lock %s, new_level = %d, l_blocking = %d\n",
  2023. lockres->l_name, new_level, lockres->l_blocking);
  2024. lockres->l_action = OCFS2_AST_DOWNCONVERT;
  2025. lockres->l_requested = new_level;
  2026. lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
  2027. }
  2028. static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
  2029. struct ocfs2_lock_res *lockres,
  2030. int new_level,
  2031. int lvb)
  2032. {
  2033. int ret, dlm_flags = LKM_CONVERT;
  2034. enum dlm_status status;
  2035. mlog_entry_void();
  2036. if (lvb)
  2037. dlm_flags |= LKM_VALBLK;
  2038. status = dlmlock(osb->dlm,
  2039. new_level,
  2040. &lockres->l_lksb,
  2041. dlm_flags,
  2042. lockres->l_name,
  2043. OCFS2_LOCK_ID_MAX_LEN - 1,
  2044. ocfs2_locking_ast,
  2045. lockres,
  2046. ocfs2_blocking_ast);
  2047. if (status != DLM_NORMAL) {
  2048. ocfs2_log_dlm_error("dlmlock", status, lockres);
  2049. ret = -EINVAL;
  2050. ocfs2_recover_from_dlm_error(lockres, 1);
  2051. goto bail;
  2052. }
  2053. ret = 0;
  2054. bail:
  2055. mlog_exit(ret);
  2056. return ret;
  2057. }
  2058. /* returns 1 when the caller should unlock and call dlmunlock */
  2059. static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
  2060. struct ocfs2_lock_res *lockres)
  2061. {
  2062. assert_spin_locked(&lockres->l_lock);
  2063. mlog_entry_void();
  2064. mlog(0, "lock %s\n", lockres->l_name);
  2065. if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
  2066. /* If we're already trying to cancel a lock conversion
  2067. * then just drop the spinlock and allow the caller to
  2068. * requeue this lock. */
  2069. mlog(0, "Lockres %s, skip convert\n", lockres->l_name);
  2070. return 0;
  2071. }
  2072. /* were we in a convert when we got the bast fire? */
  2073. BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
  2074. lockres->l_action != OCFS2_AST_DOWNCONVERT);
  2075. /* set things up for the unlockast to know to just
  2076. * clear out the ast_action and unset busy, etc. */
  2077. lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
  2078. mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
  2079. "lock %s, invalid flags: 0x%lx\n",
  2080. lockres->l_name, lockres->l_flags);
  2081. return 1;
  2082. }
  2083. static int ocfs2_cancel_convert(struct ocfs2_super *osb,
  2084. struct ocfs2_lock_res *lockres)
  2085. {
  2086. int ret;
  2087. enum dlm_status status;
  2088. mlog_entry_void();
  2089. mlog(0, "lock %s\n", lockres->l_name);
  2090. ret = 0;
  2091. status = dlmunlock(osb->dlm,
  2092. &lockres->l_lksb,
  2093. LKM_CANCEL,
  2094. ocfs2_unlock_ast,
  2095. lockres);
  2096. if (status != DLM_NORMAL) {
  2097. ocfs2_log_dlm_error("dlmunlock", status, lockres);
  2098. ret = -EINVAL;
  2099. ocfs2_recover_from_dlm_error(lockres, 0);
  2100. }
  2101. mlog(0, "lock %s return from dlmunlock\n", lockres->l_name);
  2102. mlog_exit(ret);
  2103. return ret;
  2104. }
  2105. static int ocfs2_unblock_lock(struct ocfs2_super *osb,
  2106. struct ocfs2_lock_res *lockres,
  2107. struct ocfs2_unblock_ctl *ctl)
  2108. {
  2109. unsigned long flags;
  2110. int blocking;
  2111. int new_level;
  2112. int ret = 0;
  2113. int set_lvb = 0;
  2114. mlog_entry_void();
  2115. spin_lock_irqsave(&lockres->l_lock, flags);
  2116. BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
  2117. recheck:
  2118. if (lockres->l_flags & OCFS2_LOCK_BUSY) {
  2119. ctl->requeue = 1;
  2120. ret = ocfs2_prepare_cancel_convert(osb, lockres);
  2121. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2122. if (ret) {
  2123. ret = ocfs2_cancel_convert(osb, lockres);
  2124. if (ret < 0)
  2125. mlog_errno(ret);
  2126. }
  2127. goto leave;
  2128. }
  2129. /* if we're blocking an exclusive and we have *any* holders,
  2130. * then requeue. */
  2131. if ((lockres->l_blocking == LKM_EXMODE)
  2132. && (lockres->l_ex_holders || lockres->l_ro_holders))
  2133. goto leave_requeue;
  2134. /* If it's a PR we're blocking, then only
  2135. * requeue if we've got any EX holders */
  2136. if (lockres->l_blocking == LKM_PRMODE &&
  2137. lockres->l_ex_holders)
  2138. goto leave_requeue;
  2139. /*
  2140. * Can we get a lock in this state if the holder counts are
  2141. * zero? The meta data unblock code used to check this.
  2142. */
  2143. if ((lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
  2144. && (lockres->l_flags & OCFS2_LOCK_REFRESHING))
  2145. goto leave_requeue;
  2146. new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
  2147. if (lockres->l_ops->check_downconvert
  2148. && !lockres->l_ops->check_downconvert(lockres, new_level))
  2149. goto leave_requeue;
  2150. /* If we get here, then we know that there are no more
  2151. * incompatible holders (and anyone asking for an incompatible
  2152. * lock is blocked). We can now downconvert the lock */
  2153. if (!lockres->l_ops->downconvert_worker)
  2154. goto downconvert;
  2155. /* Some lockres types want to do a bit of work before
  2156. * downconverting a lock. Allow that here. The worker function
  2157. * may sleep, so we save off a copy of what we're blocking as
  2158. * it may change while we're not holding the spin lock. */
  2159. blocking = lockres->l_blocking;
  2160. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2161. ctl->unblock_action = lockres->l_ops->downconvert_worker(lockres, blocking);
  2162. if (ctl->unblock_action == UNBLOCK_STOP_POST)
  2163. goto leave;
  2164. spin_lock_irqsave(&lockres->l_lock, flags);
  2165. if (blocking != lockres->l_blocking) {
  2166. /* If this changed underneath us, then we can't drop
  2167. * it just yet. */
  2168. goto recheck;
  2169. }
  2170. downconvert:
  2171. ctl->requeue = 0;
  2172. if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
  2173. if (lockres->l_level == LKM_EXMODE)
  2174. set_lvb = 1;
  2175. /*
  2176. * We only set the lvb if the lock has been fully
  2177. * refreshed - otherwise we risk setting stale
  2178. * data. Otherwise, there's no need to actually clear
  2179. * out the lvb here as it's value is still valid.
  2180. */
  2181. if (set_lvb && !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
  2182. lockres->l_ops->set_lvb(lockres);
  2183. }
  2184. ocfs2_prepare_downconvert(lockres, new_level);
  2185. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2186. ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb);
  2187. leave:
  2188. mlog_exit(ret);
  2189. return ret;
  2190. leave_requeue:
  2191. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2192. ctl->requeue = 1;
  2193. mlog_exit(0);
  2194. return 0;
  2195. }
  2196. static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
  2197. int blocking)
  2198. {
  2199. struct inode *inode;
  2200. struct address_space *mapping;
  2201. inode = ocfs2_lock_res_inode(lockres);
  2202. mapping = inode->i_mapping;
  2203. if (filemap_fdatawrite(mapping)) {
  2204. mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
  2205. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  2206. }
  2207. sync_mapping_buffers(mapping);
  2208. if (blocking == LKM_EXMODE) {
  2209. truncate_inode_pages(mapping, 0);
  2210. unmap_mapping_range(mapping, 0, 0, 0);
  2211. } else {
  2212. /* We only need to wait on the I/O if we're not also
  2213. * truncating pages because truncate_inode_pages waits
  2214. * for us above. We don't truncate pages if we're
  2215. * blocking anything < EXMODE because we want to keep
  2216. * them around in that case. */
  2217. filemap_fdatawait(mapping);
  2218. }
  2219. return UNBLOCK_CONTINUE;
  2220. }
  2221. static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
  2222. int new_level)
  2223. {
  2224. struct inode *inode = ocfs2_lock_res_inode(lockres);
  2225. int checkpointed = ocfs2_inode_fully_checkpointed(inode);
  2226. BUG_ON(new_level != LKM_NLMODE && new_level != LKM_PRMODE);
  2227. BUG_ON(lockres->l_level != LKM_EXMODE && !checkpointed);
  2228. if (checkpointed)
  2229. return 1;
  2230. ocfs2_start_checkpoint(OCFS2_SB(inode->i_sb));
  2231. return 0;
  2232. }
  2233. static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres)
  2234. {
  2235. struct inode *inode = ocfs2_lock_res_inode(lockres);
  2236. __ocfs2_stuff_meta_lvb(inode);
  2237. }
  2238. /*
  2239. * Does the final reference drop on our dentry lock. Right now this
  2240. * happens in the vote thread, but we could choose to simplify the
  2241. * dlmglue API and push these off to the ocfs2_wq in the future.
  2242. */
  2243. static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
  2244. struct ocfs2_lock_res *lockres)
  2245. {
  2246. struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
  2247. ocfs2_dentry_lock_put(osb, dl);
  2248. }
  2249. /*
  2250. * d_delete() matching dentries before the lock downconvert.
  2251. *
  2252. * At this point, any process waiting to destroy the
  2253. * dentry_lock due to last ref count is stopped by the
  2254. * OCFS2_LOCK_QUEUED flag.
  2255. *
  2256. * We have two potential problems
  2257. *
  2258. * 1) If we do the last reference drop on our dentry_lock (via dput)
  2259. * we'll wind up in ocfs2_release_dentry_lock(), waiting on
  2260. * the downconvert to finish. Instead we take an elevated
  2261. * reference and push the drop until after we've completed our
  2262. * unblock processing.
  2263. *
  2264. * 2) There might be another process with a final reference,
  2265. * waiting on us to finish processing. If this is the case, we
  2266. * detect it and exit out - there's no more dentries anyway.
  2267. */
  2268. static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
  2269. int blocking)
  2270. {
  2271. struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
  2272. struct ocfs2_inode_info *oi = OCFS2_I(dl->dl_inode);
  2273. struct dentry *dentry;
  2274. unsigned long flags;
  2275. int extra_ref = 0;
  2276. /*
  2277. * This node is blocking another node from getting a read
  2278. * lock. This happens when we've renamed within a
  2279. * directory. We've forced the other nodes to d_delete(), but
  2280. * we never actually dropped our lock because it's still
  2281. * valid. The downconvert code will retain a PR for this node,
  2282. * so there's no further work to do.
  2283. */
  2284. if (blocking == LKM_PRMODE)
  2285. return UNBLOCK_CONTINUE;
  2286. /*
  2287. * Mark this inode as potentially orphaned. The code in
  2288. * ocfs2_delete_inode() will figure out whether it actually
  2289. * needs to be freed or not.
  2290. */
  2291. spin_lock(&oi->ip_lock);
  2292. oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
  2293. spin_unlock(&oi->ip_lock);
  2294. /*
  2295. * Yuck. We need to make sure however that the check of
  2296. * OCFS2_LOCK_FREEING and the extra reference are atomic with
  2297. * respect to a reference decrement or the setting of that
  2298. * flag.
  2299. */
  2300. spin_lock_irqsave(&lockres->l_lock, flags);
  2301. spin_lock(&dentry_attach_lock);
  2302. if (!(lockres->l_flags & OCFS2_LOCK_FREEING)
  2303. && dl->dl_count) {
  2304. dl->dl_count++;
  2305. extra_ref = 1;
  2306. }
  2307. spin_unlock(&dentry_attach_lock);
  2308. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2309. mlog(0, "extra_ref = %d\n", extra_ref);
  2310. /*
  2311. * We have a process waiting on us in ocfs2_dentry_iput(),
  2312. * which means we can't have any more outstanding
  2313. * aliases. There's no need to do any more work.
  2314. */
  2315. if (!extra_ref)
  2316. return UNBLOCK_CONTINUE;
  2317. spin_lock(&dentry_attach_lock);
  2318. while (1) {
  2319. dentry = ocfs2_find_local_alias(dl->dl_inode,
  2320. dl->dl_parent_blkno, 1);
  2321. if (!dentry)
  2322. break;
  2323. spin_unlock(&dentry_attach_lock);
  2324. mlog(0, "d_delete(%.*s);\n", dentry->d_name.len,
  2325. dentry->d_name.name);
  2326. /*
  2327. * The following dcache calls may do an
  2328. * iput(). Normally we don't want that from the
  2329. * downconverting thread, but in this case it's ok
  2330. * because the requesting node already has an
  2331. * exclusive lock on the inode, so it can't be queued
  2332. * for a downconvert.
  2333. */
  2334. d_delete(dentry);
  2335. dput(dentry);
  2336. spin_lock(&dentry_attach_lock);
  2337. }
  2338. spin_unlock(&dentry_attach_lock);
  2339. /*
  2340. * If we are the last holder of this dentry lock, there is no
  2341. * reason to downconvert so skip straight to the unlock.
  2342. */
  2343. if (dl->dl_count == 1)
  2344. return UNBLOCK_STOP_POST;
  2345. return UNBLOCK_CONTINUE_POST;
  2346. }
  2347. void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
  2348. struct ocfs2_lock_res *lockres)
  2349. {
  2350. int status;
  2351. struct ocfs2_unblock_ctl ctl = {0, 0,};
  2352. unsigned long flags;
  2353. /* Our reference to the lockres in this function can be
  2354. * considered valid until we remove the OCFS2_LOCK_QUEUED
  2355. * flag. */
  2356. mlog_entry_void();
  2357. BUG_ON(!lockres);
  2358. BUG_ON(!lockres->l_ops);
  2359. mlog(0, "lockres %s blocked.\n", lockres->l_name);
  2360. /* Detect whether a lock has been marked as going away while
  2361. * the vote thread was processing other things. A lock can
  2362. * still be marked with OCFS2_LOCK_FREEING after this check,
  2363. * but short circuiting here will still save us some
  2364. * performance. */
  2365. spin_lock_irqsave(&lockres->l_lock, flags);
  2366. if (lockres->l_flags & OCFS2_LOCK_FREEING)
  2367. goto unqueue;
  2368. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2369. status = ocfs2_unblock_lock(osb, lockres, &ctl);
  2370. if (status < 0)
  2371. mlog_errno(status);
  2372. spin_lock_irqsave(&lockres->l_lock, flags);
  2373. unqueue:
  2374. if (lockres->l_flags & OCFS2_LOCK_FREEING || !ctl.requeue) {
  2375. lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
  2376. } else
  2377. ocfs2_schedule_blocked_lock(osb, lockres);
  2378. mlog(0, "lockres %s, requeue = %s.\n", lockres->l_name,
  2379. ctl.requeue ? "yes" : "no");
  2380. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2381. if (ctl.unblock_action != UNBLOCK_CONTINUE
  2382. && lockres->l_ops->post_unlock)
  2383. lockres->l_ops->post_unlock(osb, lockres);
  2384. mlog_exit_void();
  2385. }
  2386. static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
  2387. struct ocfs2_lock_res *lockres)
  2388. {
  2389. mlog_entry_void();
  2390. assert_spin_locked(&lockres->l_lock);
  2391. if (lockres->l_flags & OCFS2_LOCK_FREEING) {
  2392. /* Do not schedule a lock for downconvert when it's on
  2393. * the way to destruction - any nodes wanting access
  2394. * to the resource will get it soon. */
  2395. mlog(0, "Lockres %s won't be scheduled: flags 0x%lx\n",
  2396. lockres->l_name, lockres->l_flags);
  2397. return;
  2398. }
  2399. lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
  2400. spin_lock(&osb->vote_task_lock);
  2401. if (list_empty(&lockres->l_blocked_list)) {
  2402. list_add_tail(&lockres->l_blocked_list,
  2403. &osb->blocked_lock_list);
  2404. osb->blocked_lock_count++;
  2405. }
  2406. spin_unlock(&osb->vote_task_lock);
  2407. mlog_exit_void();
  2408. }
  2409. /* This aids in debugging situations where a bad LVB might be involved. */
  2410. void ocfs2_dump_meta_lvb_info(u64 level,
  2411. const char *function,
  2412. unsigned int line,
  2413. struct ocfs2_lock_res *lockres)
  2414. {
  2415. struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
  2416. mlog(level, "LVB information for %s (called from %s:%u):\n",
  2417. lockres->l_name, function, line);
  2418. mlog(level, "version: %u, clusters: %u, generation: 0x%x\n",
  2419. lvb->lvb_version, be32_to_cpu(lvb->lvb_iclusters),
  2420. be32_to_cpu(lvb->lvb_igeneration));
  2421. mlog(level, "size: %llu, uid %u, gid %u, mode 0x%x\n",
  2422. (unsigned long long)be64_to_cpu(lvb->lvb_isize),
  2423. be32_to_cpu(lvb->lvb_iuid), be32_to_cpu(lvb->lvb_igid),
  2424. be16_to_cpu(lvb->lvb_imode));
  2425. mlog(level, "nlink %u, atime_packed 0x%llx, ctime_packed 0x%llx, "
  2426. "mtime_packed 0x%llx iattr 0x%x\n", be16_to_cpu(lvb->lvb_inlink),
  2427. (long long)be64_to_cpu(lvb->lvb_iatime_packed),
  2428. (long long)be64_to_cpu(lvb->lvb_ictime_packed),
  2429. (long long)be64_to_cpu(lvb->lvb_imtime_packed),
  2430. be32_to_cpu(lvb->lvb_iattr));
  2431. }