dlmglue.c 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907
  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%016llx%08x",
  204. ocfs2_lock_type_char(type), OCFS2_LOCK_ID_PAD,
  205. (long long)blkno, 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 %llu, l_action = %u, type = %s\n",
  450. (unsigned long long)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 %llu\n", lksb->status,
  458. (unsigned long long)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 %llu, blocking %d, level %d type %s\n",
  538. (unsigned long long)OCFS2_I(inode)->ip_blkno, level,
  539. lockres->l_level, ocfs2_lock_type_string(lockres->l_type));
  540. ocfs2_generic_bast_func(osb, lockres, level);
  541. mlog_exit_void();
  542. }
  543. static void ocfs2_generic_ast_func(struct ocfs2_lock_res *lockres,
  544. int ignore_refresh)
  545. {
  546. struct dlm_lockstatus *lksb = &lockres->l_lksb;
  547. unsigned long flags;
  548. spin_lock_irqsave(&lockres->l_lock, flags);
  549. if (lksb->status != DLM_NORMAL) {
  550. mlog(ML_ERROR, "lockres %s: lksb status value of %u!\n",
  551. lockres->l_name, lksb->status);
  552. spin_unlock_irqrestore(&lockres->l_lock, flags);
  553. return;
  554. }
  555. switch(lockres->l_action) {
  556. case OCFS2_AST_ATTACH:
  557. ocfs2_generic_handle_attach_action(lockres);
  558. break;
  559. case OCFS2_AST_CONVERT:
  560. ocfs2_generic_handle_convert_action(lockres);
  561. break;
  562. case OCFS2_AST_DOWNCONVERT:
  563. ocfs2_generic_handle_downconvert_action(lockres);
  564. break;
  565. default:
  566. BUG();
  567. }
  568. if (ignore_refresh)
  569. lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
  570. /* set it to something invalid so if we get called again we
  571. * can catch it. */
  572. lockres->l_action = OCFS2_AST_INVALID;
  573. spin_unlock_irqrestore(&lockres->l_lock, flags);
  574. wake_up(&lockres->l_event);
  575. }
  576. static void ocfs2_super_ast_func(void *opaque)
  577. {
  578. struct ocfs2_lock_res *lockres = opaque;
  579. mlog_entry_void();
  580. mlog(0, "Superblock AST fired\n");
  581. BUG_ON(!ocfs2_is_super_lock(lockres));
  582. ocfs2_generic_ast_func(lockres, 0);
  583. mlog_exit_void();
  584. }
  585. static void ocfs2_super_bast_func(void *opaque,
  586. int level)
  587. {
  588. struct ocfs2_lock_res *lockres = opaque;
  589. struct ocfs2_super *osb;
  590. mlog_entry_void();
  591. mlog(0, "Superblock BAST fired\n");
  592. BUG_ON(!ocfs2_is_super_lock(lockres));
  593. osb = ocfs2_lock_res_super(lockres);
  594. ocfs2_generic_bast_func(osb, lockres, level);
  595. mlog_exit_void();
  596. }
  597. static void ocfs2_rename_ast_func(void *opaque)
  598. {
  599. struct ocfs2_lock_res *lockres = opaque;
  600. mlog_entry_void();
  601. mlog(0, "Rename AST fired\n");
  602. BUG_ON(!ocfs2_is_rename_lock(lockres));
  603. ocfs2_generic_ast_func(lockres, 1);
  604. mlog_exit_void();
  605. }
  606. static void ocfs2_rename_bast_func(void *opaque,
  607. int level)
  608. {
  609. struct ocfs2_lock_res *lockres = opaque;
  610. struct ocfs2_super *osb;
  611. mlog_entry_void();
  612. mlog(0, "Rename BAST fired\n");
  613. BUG_ON(!ocfs2_is_rename_lock(lockres));
  614. osb = ocfs2_lock_res_super(lockres);
  615. ocfs2_generic_bast_func(osb, lockres, level);
  616. mlog_exit_void();
  617. }
  618. static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
  619. int convert)
  620. {
  621. unsigned long flags;
  622. mlog_entry_void();
  623. spin_lock_irqsave(&lockres->l_lock, flags);
  624. lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
  625. if (convert)
  626. lockres->l_action = OCFS2_AST_INVALID;
  627. else
  628. lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
  629. spin_unlock_irqrestore(&lockres->l_lock, flags);
  630. wake_up(&lockres->l_event);
  631. mlog_exit_void();
  632. }
  633. /* Note: If we detect another process working on the lock (i.e.,
  634. * OCFS2_LOCK_BUSY), we'll bail out returning 0. It's up to the caller
  635. * to do the right thing in that case.
  636. */
  637. static int ocfs2_lock_create(struct ocfs2_super *osb,
  638. struct ocfs2_lock_res *lockres,
  639. int level,
  640. int dlm_flags)
  641. {
  642. int ret = 0;
  643. enum dlm_status status;
  644. unsigned long flags;
  645. mlog_entry_void();
  646. mlog(0, "lock %s, level = %d, flags = %d\n", lockres->l_name, level,
  647. dlm_flags);
  648. spin_lock_irqsave(&lockres->l_lock, flags);
  649. if ((lockres->l_flags & OCFS2_LOCK_ATTACHED) ||
  650. (lockres->l_flags & OCFS2_LOCK_BUSY)) {
  651. spin_unlock_irqrestore(&lockres->l_lock, flags);
  652. goto bail;
  653. }
  654. lockres->l_action = OCFS2_AST_ATTACH;
  655. lockres->l_requested = level;
  656. lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
  657. spin_unlock_irqrestore(&lockres->l_lock, flags);
  658. status = dlmlock(osb->dlm,
  659. level,
  660. &lockres->l_lksb,
  661. dlm_flags,
  662. lockres->l_name,
  663. lockres->l_ops->ast,
  664. lockres,
  665. lockres->l_ops->bast);
  666. if (status != DLM_NORMAL) {
  667. ocfs2_log_dlm_error("dlmlock", status, lockres);
  668. ret = -EINVAL;
  669. ocfs2_recover_from_dlm_error(lockres, 1);
  670. }
  671. mlog(0, "lock %s, successfull return from dlmlock\n", lockres->l_name);
  672. bail:
  673. mlog_exit(ret);
  674. return ret;
  675. }
  676. static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res *lockres,
  677. int flag)
  678. {
  679. unsigned long flags;
  680. int ret;
  681. spin_lock_irqsave(&lockres->l_lock, flags);
  682. ret = lockres->l_flags & flag;
  683. spin_unlock_irqrestore(&lockres->l_lock, flags);
  684. return ret;
  685. }
  686. static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res *lockres)
  687. {
  688. wait_event(lockres->l_event,
  689. !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_BUSY));
  690. }
  691. static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res *lockres)
  692. {
  693. wait_event(lockres->l_event,
  694. !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_REFRESHING));
  695. }
  696. /* predict what lock level we'll be dropping down to on behalf
  697. * of another node, and return true if the currently wanted
  698. * level will be compatible with it. */
  699. static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
  700. int wanted)
  701. {
  702. BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
  703. return wanted <= ocfs2_highest_compat_lock_level(lockres->l_blocking);
  704. }
  705. static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter *mw)
  706. {
  707. INIT_LIST_HEAD(&mw->mw_item);
  708. init_completion(&mw->mw_complete);
  709. }
  710. static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter *mw)
  711. {
  712. wait_for_completion(&mw->mw_complete);
  713. /* Re-arm the completion in case we want to wait on it again */
  714. INIT_COMPLETION(mw->mw_complete);
  715. return mw->mw_status;
  716. }
  717. static void lockres_add_mask_waiter(struct ocfs2_lock_res *lockres,
  718. struct ocfs2_mask_waiter *mw,
  719. unsigned long mask,
  720. unsigned long goal)
  721. {
  722. BUG_ON(!list_empty(&mw->mw_item));
  723. assert_spin_locked(&lockres->l_lock);
  724. list_add_tail(&mw->mw_item, &lockres->l_mask_waiters);
  725. mw->mw_mask = mask;
  726. mw->mw_goal = goal;
  727. }
  728. /* returns 0 if the mw that was removed was already satisfied, -EBUSY
  729. * if the mask still hadn't reached its goal */
  730. static int lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
  731. struct ocfs2_mask_waiter *mw)
  732. {
  733. unsigned long flags;
  734. int ret = 0;
  735. spin_lock_irqsave(&lockres->l_lock, flags);
  736. if (!list_empty(&mw->mw_item)) {
  737. if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
  738. ret = -EBUSY;
  739. list_del_init(&mw->mw_item);
  740. init_completion(&mw->mw_complete);
  741. }
  742. spin_unlock_irqrestore(&lockres->l_lock, flags);
  743. return ret;
  744. }
  745. static int ocfs2_cluster_lock(struct ocfs2_super *osb,
  746. struct ocfs2_lock_res *lockres,
  747. int level,
  748. int lkm_flags,
  749. int arg_flags)
  750. {
  751. struct ocfs2_mask_waiter mw;
  752. enum dlm_status status;
  753. int wait, catch_signals = !(osb->s_mount_opt & OCFS2_MOUNT_NOINTR);
  754. int ret = 0; /* gcc doesn't realize wait = 1 guarantees ret is set */
  755. unsigned long flags;
  756. mlog_entry_void();
  757. ocfs2_init_mask_waiter(&mw);
  758. again:
  759. wait = 0;
  760. if (catch_signals && signal_pending(current)) {
  761. ret = -ERESTARTSYS;
  762. goto out;
  763. }
  764. spin_lock_irqsave(&lockres->l_lock, flags);
  765. mlog_bug_on_msg(lockres->l_flags & OCFS2_LOCK_FREEING,
  766. "Cluster lock called on freeing lockres %s! flags "
  767. "0x%lx\n", lockres->l_name, lockres->l_flags);
  768. /* We only compare against the currently granted level
  769. * here. If the lock is blocked waiting on a downconvert,
  770. * we'll get caught below. */
  771. if (lockres->l_flags & OCFS2_LOCK_BUSY &&
  772. level > lockres->l_level) {
  773. /* is someone sitting in dlm_lock? If so, wait on
  774. * them. */
  775. lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
  776. wait = 1;
  777. goto unlock;
  778. }
  779. if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
  780. /* lock has not been created yet. */
  781. spin_unlock_irqrestore(&lockres->l_lock, flags);
  782. ret = ocfs2_lock_create(osb, lockres, LKM_NLMODE, 0);
  783. if (ret < 0) {
  784. mlog_errno(ret);
  785. goto out;
  786. }
  787. goto again;
  788. }
  789. if (lockres->l_flags & OCFS2_LOCK_BLOCKED &&
  790. !ocfs2_may_continue_on_blocked_lock(lockres, level)) {
  791. /* is the lock is currently blocked on behalf of
  792. * another node */
  793. lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BLOCKED, 0);
  794. wait = 1;
  795. goto unlock;
  796. }
  797. if (level > lockres->l_level) {
  798. if (lockres->l_action != OCFS2_AST_INVALID)
  799. mlog(ML_ERROR, "lockres %s has action %u pending\n",
  800. lockres->l_name, lockres->l_action);
  801. lockres->l_action = OCFS2_AST_CONVERT;
  802. lockres->l_requested = level;
  803. lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
  804. spin_unlock_irqrestore(&lockres->l_lock, flags);
  805. BUG_ON(level == LKM_IVMODE);
  806. BUG_ON(level == LKM_NLMODE);
  807. mlog(0, "lock %s, convert from %d to level = %d\n",
  808. lockres->l_name, lockres->l_level, level);
  809. /* call dlm_lock to upgrade lock now */
  810. status = dlmlock(osb->dlm,
  811. level,
  812. &lockres->l_lksb,
  813. lkm_flags|LKM_CONVERT|LKM_VALBLK,
  814. lockres->l_name,
  815. lockres->l_ops->ast,
  816. lockres,
  817. lockres->l_ops->bast);
  818. if (status != DLM_NORMAL) {
  819. if ((lkm_flags & LKM_NOQUEUE) &&
  820. (status == DLM_NOTQUEUED))
  821. ret = -EAGAIN;
  822. else {
  823. ocfs2_log_dlm_error("dlmlock", status,
  824. lockres);
  825. ret = -EINVAL;
  826. }
  827. ocfs2_recover_from_dlm_error(lockres, 1);
  828. goto out;
  829. }
  830. mlog(0, "lock %s, successfull return from dlmlock\n",
  831. lockres->l_name);
  832. /* At this point we've gone inside the dlm and need to
  833. * complete our work regardless. */
  834. catch_signals = 0;
  835. /* wait for busy to clear and carry on */
  836. goto again;
  837. }
  838. /* Ok, if we get here then we're good to go. */
  839. ocfs2_inc_holders(lockres, level);
  840. ret = 0;
  841. unlock:
  842. spin_unlock_irqrestore(&lockres->l_lock, flags);
  843. out:
  844. /*
  845. * This is helping work around a lock inversion between the page lock
  846. * and dlm locks. One path holds the page lock while calling aops
  847. * which block acquiring dlm locks. The voting thread holds dlm
  848. * locks while acquiring page locks while down converting data locks.
  849. * This block is helping an aop path notice the inversion and back
  850. * off to unlock its page lock before trying the dlm lock again.
  851. */
  852. if (wait && arg_flags & OCFS2_LOCK_NONBLOCK &&
  853. mw.mw_mask & (OCFS2_LOCK_BUSY|OCFS2_LOCK_BLOCKED)) {
  854. wait = 0;
  855. if (lockres_remove_mask_waiter(lockres, &mw))
  856. ret = -EAGAIN;
  857. else
  858. goto again;
  859. }
  860. if (wait) {
  861. ret = ocfs2_wait_for_mask(&mw);
  862. if (ret == 0)
  863. goto again;
  864. mlog_errno(ret);
  865. }
  866. mlog_exit(ret);
  867. return ret;
  868. }
  869. static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
  870. struct ocfs2_lock_res *lockres,
  871. int level)
  872. {
  873. unsigned long flags;
  874. mlog_entry_void();
  875. spin_lock_irqsave(&lockres->l_lock, flags);
  876. ocfs2_dec_holders(lockres, level);
  877. ocfs2_vote_on_unlock(osb, lockres);
  878. spin_unlock_irqrestore(&lockres->l_lock, flags);
  879. mlog_exit_void();
  880. }
  881. static int ocfs2_create_new_inode_lock(struct inode *inode,
  882. struct ocfs2_lock_res *lockres)
  883. {
  884. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  885. unsigned long flags;
  886. spin_lock_irqsave(&lockres->l_lock, flags);
  887. BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
  888. lockres_or_flags(lockres, OCFS2_LOCK_LOCAL);
  889. spin_unlock_irqrestore(&lockres->l_lock, flags);
  890. return ocfs2_lock_create(osb, lockres, LKM_EXMODE, LKM_LOCAL);
  891. }
  892. /* Grants us an EX lock on the data and metadata resources, skipping
  893. * the normal cluster directory lookup. Use this ONLY on newly created
  894. * inodes which other nodes can't possibly see, and which haven't been
  895. * hashed in the inode hash yet. This can give us a good performance
  896. * increase as it'll skip the network broadcast normally associated
  897. * with creating a new lock resource. */
  898. int ocfs2_create_new_inode_locks(struct inode *inode)
  899. {
  900. int ret;
  901. BUG_ON(!inode);
  902. BUG_ON(!ocfs2_inode_is_new(inode));
  903. mlog_entry_void();
  904. mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
  905. /* NOTE: That we don't increment any of the holder counts, nor
  906. * do we add anything to a journal handle. Since this is
  907. * supposed to be a new inode which the cluster doesn't know
  908. * about yet, there is no need to. As far as the LVB handling
  909. * is concerned, this is basically like acquiring an EX lock
  910. * on a resource which has an invalid one -- we'll set it
  911. * valid when we release the EX. */
  912. ret = ocfs2_create_new_inode_lock(inode,
  913. &OCFS2_I(inode)->ip_rw_lockres);
  914. if (ret) {
  915. mlog_errno(ret);
  916. goto bail;
  917. }
  918. ret = ocfs2_create_new_inode_lock(inode,
  919. &OCFS2_I(inode)->ip_meta_lockres);
  920. if (ret) {
  921. mlog_errno(ret);
  922. goto bail;
  923. }
  924. ret = ocfs2_create_new_inode_lock(inode,
  925. &OCFS2_I(inode)->ip_data_lockres);
  926. if (ret) {
  927. mlog_errno(ret);
  928. goto bail;
  929. }
  930. bail:
  931. mlog_exit(ret);
  932. return ret;
  933. }
  934. int ocfs2_rw_lock(struct inode *inode, int write)
  935. {
  936. int status, level;
  937. struct ocfs2_lock_res *lockres;
  938. BUG_ON(!inode);
  939. mlog_entry_void();
  940. mlog(0, "inode %llu take %s RW lock\n",
  941. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  942. write ? "EXMODE" : "PRMODE");
  943. lockres = &OCFS2_I(inode)->ip_rw_lockres;
  944. level = write ? LKM_EXMODE : LKM_PRMODE;
  945. status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level, 0,
  946. 0);
  947. if (status < 0)
  948. mlog_errno(status);
  949. mlog_exit(status);
  950. return status;
  951. }
  952. void ocfs2_rw_unlock(struct inode *inode, int write)
  953. {
  954. int level = write ? LKM_EXMODE : LKM_PRMODE;
  955. struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_rw_lockres;
  956. mlog_entry_void();
  957. mlog(0, "inode %llu drop %s RW lock\n",
  958. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  959. write ? "EXMODE" : "PRMODE");
  960. ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
  961. mlog_exit_void();
  962. }
  963. int ocfs2_data_lock_full(struct inode *inode,
  964. int write,
  965. int arg_flags)
  966. {
  967. int status = 0, level;
  968. struct ocfs2_lock_res *lockres;
  969. BUG_ON(!inode);
  970. mlog_entry_void();
  971. mlog(0, "inode %llu take %s DATA lock\n",
  972. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  973. write ? "EXMODE" : "PRMODE");
  974. /* We'll allow faking a readonly data lock for
  975. * rodevices. */
  976. if (ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb))) {
  977. if (write) {
  978. status = -EROFS;
  979. mlog_errno(status);
  980. }
  981. goto out;
  982. }
  983. lockres = &OCFS2_I(inode)->ip_data_lockres;
  984. level = write ? LKM_EXMODE : LKM_PRMODE;
  985. status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level,
  986. 0, arg_flags);
  987. if (status < 0 && status != -EAGAIN)
  988. mlog_errno(status);
  989. out:
  990. mlog_exit(status);
  991. return status;
  992. }
  993. /* see ocfs2_meta_lock_with_page() */
  994. int ocfs2_data_lock_with_page(struct inode *inode,
  995. int write,
  996. struct page *page)
  997. {
  998. int ret;
  999. ret = ocfs2_data_lock_full(inode, write, OCFS2_LOCK_NONBLOCK);
  1000. if (ret == -EAGAIN) {
  1001. unlock_page(page);
  1002. if (ocfs2_data_lock(inode, write) == 0)
  1003. ocfs2_data_unlock(inode, write);
  1004. ret = AOP_TRUNCATED_PAGE;
  1005. }
  1006. return ret;
  1007. }
  1008. static void ocfs2_vote_on_unlock(struct ocfs2_super *osb,
  1009. struct ocfs2_lock_res *lockres)
  1010. {
  1011. int kick = 0;
  1012. mlog_entry_void();
  1013. /* If we know that another node is waiting on our lock, kick
  1014. * the vote thread * pre-emptively when we reach a release
  1015. * condition. */
  1016. if (lockres->l_flags & OCFS2_LOCK_BLOCKED) {
  1017. switch(lockres->l_blocking) {
  1018. case LKM_EXMODE:
  1019. if (!lockres->l_ex_holders && !lockres->l_ro_holders)
  1020. kick = 1;
  1021. break;
  1022. case LKM_PRMODE:
  1023. if (!lockres->l_ex_holders)
  1024. kick = 1;
  1025. break;
  1026. default:
  1027. BUG();
  1028. }
  1029. }
  1030. if (kick)
  1031. ocfs2_kick_vote_thread(osb);
  1032. mlog_exit_void();
  1033. }
  1034. void ocfs2_data_unlock(struct inode *inode,
  1035. int write)
  1036. {
  1037. int level = write ? LKM_EXMODE : LKM_PRMODE;
  1038. struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_data_lockres;
  1039. mlog_entry_void();
  1040. mlog(0, "inode %llu drop %s DATA lock\n",
  1041. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1042. write ? "EXMODE" : "PRMODE");
  1043. if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)))
  1044. ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
  1045. mlog_exit_void();
  1046. }
  1047. #define OCFS2_SEC_BITS 34
  1048. #define OCFS2_SEC_SHIFT (64 - 34)
  1049. #define OCFS2_NSEC_MASK ((1ULL << OCFS2_SEC_SHIFT) - 1)
  1050. /* LVB only has room for 64 bits of time here so we pack it for
  1051. * now. */
  1052. static u64 ocfs2_pack_timespec(struct timespec *spec)
  1053. {
  1054. u64 res;
  1055. u64 sec = spec->tv_sec;
  1056. u32 nsec = spec->tv_nsec;
  1057. res = (sec << OCFS2_SEC_SHIFT) | (nsec & OCFS2_NSEC_MASK);
  1058. return res;
  1059. }
  1060. /* Call this with the lockres locked. I am reasonably sure we don't
  1061. * need ip_lock in this function as anyone who would be changing those
  1062. * values is supposed to be blocked in ocfs2_meta_lock right now. */
  1063. static void __ocfs2_stuff_meta_lvb(struct inode *inode)
  1064. {
  1065. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1066. struct ocfs2_lock_res *lockres = &oi->ip_meta_lockres;
  1067. struct ocfs2_meta_lvb *lvb;
  1068. mlog_entry_void();
  1069. lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
  1070. lvb->lvb_version = cpu_to_be32(OCFS2_LVB_VERSION);
  1071. lvb->lvb_isize = cpu_to_be64(i_size_read(inode));
  1072. lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters);
  1073. lvb->lvb_iuid = cpu_to_be32(inode->i_uid);
  1074. lvb->lvb_igid = cpu_to_be32(inode->i_gid);
  1075. lvb->lvb_imode = cpu_to_be16(inode->i_mode);
  1076. lvb->lvb_inlink = cpu_to_be16(inode->i_nlink);
  1077. lvb->lvb_iatime_packed =
  1078. cpu_to_be64(ocfs2_pack_timespec(&inode->i_atime));
  1079. lvb->lvb_ictime_packed =
  1080. cpu_to_be64(ocfs2_pack_timespec(&inode->i_ctime));
  1081. lvb->lvb_imtime_packed =
  1082. cpu_to_be64(ocfs2_pack_timespec(&inode->i_mtime));
  1083. mlog_meta_lvb(0, lockres);
  1084. mlog_exit_void();
  1085. }
  1086. static void ocfs2_unpack_timespec(struct timespec *spec,
  1087. u64 packed_time)
  1088. {
  1089. spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
  1090. spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
  1091. }
  1092. static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
  1093. {
  1094. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1095. struct ocfs2_lock_res *lockres = &oi->ip_meta_lockres;
  1096. struct ocfs2_meta_lvb *lvb;
  1097. mlog_entry_void();
  1098. mlog_meta_lvb(0, lockres);
  1099. lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
  1100. /* We're safe here without the lockres lock... */
  1101. spin_lock(&oi->ip_lock);
  1102. oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters);
  1103. i_size_write(inode, be64_to_cpu(lvb->lvb_isize));
  1104. /* fast-symlinks are a special case */
  1105. if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
  1106. inode->i_blocks = 0;
  1107. else
  1108. inode->i_blocks =
  1109. ocfs2_align_bytes_to_sectors(i_size_read(inode));
  1110. inode->i_uid = be32_to_cpu(lvb->lvb_iuid);
  1111. inode->i_gid = be32_to_cpu(lvb->lvb_igid);
  1112. inode->i_mode = be16_to_cpu(lvb->lvb_imode);
  1113. inode->i_nlink = be16_to_cpu(lvb->lvb_inlink);
  1114. ocfs2_unpack_timespec(&inode->i_atime,
  1115. be64_to_cpu(lvb->lvb_iatime_packed));
  1116. ocfs2_unpack_timespec(&inode->i_mtime,
  1117. be64_to_cpu(lvb->lvb_imtime_packed));
  1118. ocfs2_unpack_timespec(&inode->i_ctime,
  1119. be64_to_cpu(lvb->lvb_ictime_packed));
  1120. spin_unlock(&oi->ip_lock);
  1121. mlog_exit_void();
  1122. }
  1123. static inline int ocfs2_meta_lvb_is_trustable(struct ocfs2_lock_res *lockres)
  1124. {
  1125. struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
  1126. if (be32_to_cpu(lvb->lvb_version) == OCFS2_LVB_VERSION)
  1127. return 1;
  1128. return 0;
  1129. }
  1130. /* Determine whether a lock resource needs to be refreshed, and
  1131. * arbitrate who gets to refresh it.
  1132. *
  1133. * 0 means no refresh needed.
  1134. *
  1135. * > 0 means you need to refresh this and you MUST call
  1136. * ocfs2_complete_lock_res_refresh afterwards. */
  1137. static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
  1138. {
  1139. unsigned long flags;
  1140. int status = 0;
  1141. mlog_entry_void();
  1142. refresh_check:
  1143. spin_lock_irqsave(&lockres->l_lock, flags);
  1144. if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
  1145. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1146. goto bail;
  1147. }
  1148. if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
  1149. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1150. ocfs2_wait_on_refreshing_lock(lockres);
  1151. goto refresh_check;
  1152. }
  1153. /* Ok, I'll be the one to refresh this lock. */
  1154. lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
  1155. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1156. status = 1;
  1157. bail:
  1158. mlog_exit(status);
  1159. return status;
  1160. }
  1161. /* If status is non zero, I'll mark it as not being in refresh
  1162. * anymroe, but i won't clear the needs refresh flag. */
  1163. static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
  1164. int status)
  1165. {
  1166. unsigned long flags;
  1167. mlog_entry_void();
  1168. spin_lock_irqsave(&lockres->l_lock, flags);
  1169. lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
  1170. if (!status)
  1171. lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
  1172. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1173. wake_up(&lockres->l_event);
  1174. mlog_exit_void();
  1175. }
  1176. /* may or may not return a bh if it went to disk. */
  1177. static int ocfs2_meta_lock_update(struct inode *inode,
  1178. struct buffer_head **bh)
  1179. {
  1180. int status = 0;
  1181. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1182. struct ocfs2_lock_res *lockres;
  1183. struct ocfs2_dinode *fe;
  1184. mlog_entry_void();
  1185. spin_lock(&oi->ip_lock);
  1186. if (oi->ip_flags & OCFS2_INODE_DELETED) {
  1187. mlog(0, "Orphaned inode %llu was deleted while we "
  1188. "were waiting on a lock. ip_flags = 0x%x\n",
  1189. (unsigned long long)oi->ip_blkno, oi->ip_flags);
  1190. spin_unlock(&oi->ip_lock);
  1191. status = -ENOENT;
  1192. goto bail;
  1193. }
  1194. spin_unlock(&oi->ip_lock);
  1195. lockres = &oi->ip_meta_lockres;
  1196. if (!ocfs2_should_refresh_lock_res(lockres))
  1197. goto bail;
  1198. /* This will discard any caching information we might have had
  1199. * for the inode metadata. */
  1200. ocfs2_metadata_cache_purge(inode);
  1201. /* will do nothing for inode types that don't use the extent
  1202. * map (directories, bitmap files, etc) */
  1203. ocfs2_extent_map_trunc(inode, 0);
  1204. if (ocfs2_meta_lvb_is_trustable(lockres)) {
  1205. mlog(0, "Trusting LVB on inode %llu\n",
  1206. (unsigned long long)oi->ip_blkno);
  1207. ocfs2_refresh_inode_from_lvb(inode);
  1208. } else {
  1209. /* Boo, we have to go to disk. */
  1210. /* read bh, cast, ocfs2_refresh_inode */
  1211. status = ocfs2_read_block(OCFS2_SB(inode->i_sb), oi->ip_blkno,
  1212. bh, OCFS2_BH_CACHED, inode);
  1213. if (status < 0) {
  1214. mlog_errno(status);
  1215. goto bail_refresh;
  1216. }
  1217. fe = (struct ocfs2_dinode *) (*bh)->b_data;
  1218. /* This is a good chance to make sure we're not
  1219. * locking an invalid object.
  1220. *
  1221. * We bug on a stale inode here because we checked
  1222. * above whether it was wiped from disk. The wiping
  1223. * node provides a guarantee that we receive that
  1224. * message and can mark the inode before dropping any
  1225. * locks associated with it. */
  1226. if (!OCFS2_IS_VALID_DINODE(fe)) {
  1227. OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
  1228. status = -EIO;
  1229. goto bail_refresh;
  1230. }
  1231. mlog_bug_on_msg(inode->i_generation !=
  1232. le32_to_cpu(fe->i_generation),
  1233. "Invalid dinode %llu disk generation: %u "
  1234. "inode->i_generation: %u\n",
  1235. (unsigned long long)oi->ip_blkno,
  1236. 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 %llu dtime: %llu flags: 0x%x\n",
  1241. (unsigned long long)oi->ip_blkno,
  1242. (unsigned long long)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 %llu, take %s META lock\n",
  1291. (unsigned long long)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 %llu drop %s META lock\n",
  1408. (unsigned long long)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 const 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 %llu for downconvert!",
  2190. (unsigned long long)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 %llu\n",
  2216. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  2217. status = ocfs2_generic_unblock_lock(osb,
  2218. lockres,
  2219. requeue,
  2220. ocfs2_data_convert_worker);
  2221. if (status < 0)
  2222. mlog_errno(status);
  2223. mlog(0, "inode %llu, requeue = %d\n",
  2224. (unsigned long long)OCFS2_I(inode)->ip_blkno, *requeue);
  2225. mlog_exit(status);
  2226. return status;
  2227. }
  2228. static int ocfs2_unblock_inode_lock(struct ocfs2_lock_res *lockres,
  2229. int *requeue)
  2230. {
  2231. int status;
  2232. struct inode *inode;
  2233. mlog_entry_void();
  2234. mlog(0, "Unblock lockres %s\n", lockres->l_name);
  2235. inode = ocfs2_lock_res_inode(lockres);
  2236. status = ocfs2_generic_unblock_lock(OCFS2_SB(inode->i_sb),
  2237. lockres,
  2238. requeue,
  2239. NULL);
  2240. if (status < 0)
  2241. mlog_errno(status);
  2242. mlog_exit(status);
  2243. return status;
  2244. }
  2245. int ocfs2_unblock_meta(struct ocfs2_lock_res *lockres,
  2246. int *requeue)
  2247. {
  2248. int status;
  2249. struct inode *inode;
  2250. mlog_entry_void();
  2251. inode = ocfs2_lock_res_inode(lockres);
  2252. mlog(0, "unblock inode %llu\n",
  2253. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  2254. status = ocfs2_do_unblock_meta(inode, requeue);
  2255. if (status < 0)
  2256. mlog_errno(status);
  2257. mlog(0, "inode %llu, requeue = %d\n",
  2258. (unsigned long long)OCFS2_I(inode)->ip_blkno, *requeue);
  2259. mlog_exit(status);
  2260. return status;
  2261. }
  2262. /* Generic unblock function for any lockres whose private data is an
  2263. * ocfs2_super pointer. */
  2264. static int ocfs2_unblock_osb_lock(struct ocfs2_lock_res *lockres,
  2265. int *requeue)
  2266. {
  2267. int status;
  2268. struct ocfs2_super *osb;
  2269. mlog_entry_void();
  2270. mlog(0, "Unblock lockres %s\n", lockres->l_name);
  2271. osb = ocfs2_lock_res_super(lockres);
  2272. status = ocfs2_generic_unblock_lock(osb,
  2273. lockres,
  2274. requeue,
  2275. NULL);
  2276. if (status < 0)
  2277. mlog_errno(status);
  2278. mlog_exit(status);
  2279. return status;
  2280. }
  2281. void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
  2282. struct ocfs2_lock_res *lockres)
  2283. {
  2284. int status;
  2285. int requeue = 0;
  2286. unsigned long flags;
  2287. /* Our reference to the lockres in this function can be
  2288. * considered valid until we remove the OCFS2_LOCK_QUEUED
  2289. * flag. */
  2290. mlog_entry_void();
  2291. BUG_ON(!lockres);
  2292. BUG_ON(!lockres->l_ops);
  2293. BUG_ON(!lockres->l_ops->unblock);
  2294. mlog(0, "lockres %s blocked.\n", lockres->l_name);
  2295. /* Detect whether a lock has been marked as going away while
  2296. * the vote thread was processing other things. A lock can
  2297. * still be marked with OCFS2_LOCK_FREEING after this check,
  2298. * but short circuiting here will still save us some
  2299. * performance. */
  2300. spin_lock_irqsave(&lockres->l_lock, flags);
  2301. if (lockres->l_flags & OCFS2_LOCK_FREEING)
  2302. goto unqueue;
  2303. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2304. status = lockres->l_ops->unblock(lockres, &requeue);
  2305. if (status < 0)
  2306. mlog_errno(status);
  2307. spin_lock_irqsave(&lockres->l_lock, flags);
  2308. unqueue:
  2309. if (lockres->l_flags & OCFS2_LOCK_FREEING || !requeue) {
  2310. lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
  2311. } else
  2312. ocfs2_schedule_blocked_lock(osb, lockres);
  2313. mlog(0, "lockres %s, requeue = %s.\n", lockres->l_name,
  2314. requeue ? "yes" : "no");
  2315. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2316. mlog_exit_void();
  2317. }
  2318. static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
  2319. struct ocfs2_lock_res *lockres)
  2320. {
  2321. mlog_entry_void();
  2322. assert_spin_locked(&lockres->l_lock);
  2323. if (lockres->l_flags & OCFS2_LOCK_FREEING) {
  2324. /* Do not schedule a lock for downconvert when it's on
  2325. * the way to destruction - any nodes wanting access
  2326. * to the resource will get it soon. */
  2327. mlog(0, "Lockres %s won't be scheduled: flags 0x%lx\n",
  2328. lockres->l_name, lockres->l_flags);
  2329. return;
  2330. }
  2331. lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
  2332. spin_lock(&osb->vote_task_lock);
  2333. if (list_empty(&lockres->l_blocked_list)) {
  2334. list_add_tail(&lockres->l_blocked_list,
  2335. &osb->blocked_lock_list);
  2336. osb->blocked_lock_count++;
  2337. }
  2338. spin_unlock(&osb->vote_task_lock);
  2339. mlog_exit_void();
  2340. }
  2341. /* This aids in debugging situations where a bad LVB might be involved. */
  2342. void ocfs2_dump_meta_lvb_info(u64 level,
  2343. const char *function,
  2344. unsigned int line,
  2345. struct ocfs2_lock_res *lockres)
  2346. {
  2347. struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
  2348. mlog(level, "LVB information for %s (called from %s:%u):\n",
  2349. lockres->l_name, function, line);
  2350. mlog(level, "version: %u, clusters: %u\n",
  2351. be32_to_cpu(lvb->lvb_version), be32_to_cpu(lvb->lvb_iclusters));
  2352. mlog(level, "size: %llu, uid %u, gid %u, mode 0x%x\n",
  2353. (unsigned long long)be64_to_cpu(lvb->lvb_isize),
  2354. be32_to_cpu(lvb->lvb_iuid), be32_to_cpu(lvb->lvb_igid),
  2355. be16_to_cpu(lvb->lvb_imode));
  2356. mlog(level, "nlink %u, atime_packed 0x%llx, ctime_packed 0x%llx, "
  2357. "mtime_packed 0x%llx\n", be16_to_cpu(lvb->lvb_inlink),
  2358. (long long)be64_to_cpu(lvb->lvb_iatime_packed),
  2359. (long long)be64_to_cpu(lvb->lvb_ictime_packed),
  2360. (long long)be64_to_cpu(lvb->lvb_imtime_packed));
  2361. }