dlmglue.c 74 KB

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