dlmglue.c 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914
  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 DEFINE_SPINLOCK(ocfs2_dlm_tracking_lock);
  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. OCFS2_LOCK_ID_MAX_LEN - 1,
  664. lockres->l_ops->ast,
  665. lockres,
  666. lockres->l_ops->bast);
  667. if (status != DLM_NORMAL) {
  668. ocfs2_log_dlm_error("dlmlock", status, lockres);
  669. ret = -EINVAL;
  670. ocfs2_recover_from_dlm_error(lockres, 1);
  671. }
  672. mlog(0, "lock %s, successfull return from dlmlock\n", lockres->l_name);
  673. bail:
  674. mlog_exit(ret);
  675. return ret;
  676. }
  677. static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res *lockres,
  678. int flag)
  679. {
  680. unsigned long flags;
  681. int ret;
  682. spin_lock_irqsave(&lockres->l_lock, flags);
  683. ret = lockres->l_flags & flag;
  684. spin_unlock_irqrestore(&lockres->l_lock, flags);
  685. return ret;
  686. }
  687. static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res *lockres)
  688. {
  689. wait_event(lockres->l_event,
  690. !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_BUSY));
  691. }
  692. static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res *lockres)
  693. {
  694. wait_event(lockres->l_event,
  695. !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_REFRESHING));
  696. }
  697. /* predict what lock level we'll be dropping down to on behalf
  698. * of another node, and return true if the currently wanted
  699. * level will be compatible with it. */
  700. static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
  701. int wanted)
  702. {
  703. BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
  704. return wanted <= ocfs2_highest_compat_lock_level(lockres->l_blocking);
  705. }
  706. static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter *mw)
  707. {
  708. INIT_LIST_HEAD(&mw->mw_item);
  709. init_completion(&mw->mw_complete);
  710. }
  711. static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter *mw)
  712. {
  713. wait_for_completion(&mw->mw_complete);
  714. /* Re-arm the completion in case we want to wait on it again */
  715. INIT_COMPLETION(mw->mw_complete);
  716. return mw->mw_status;
  717. }
  718. static void lockres_add_mask_waiter(struct ocfs2_lock_res *lockres,
  719. struct ocfs2_mask_waiter *mw,
  720. unsigned long mask,
  721. unsigned long goal)
  722. {
  723. BUG_ON(!list_empty(&mw->mw_item));
  724. assert_spin_locked(&lockres->l_lock);
  725. list_add_tail(&mw->mw_item, &lockres->l_mask_waiters);
  726. mw->mw_mask = mask;
  727. mw->mw_goal = goal;
  728. }
  729. /* returns 0 if the mw that was removed was already satisfied, -EBUSY
  730. * if the mask still hadn't reached its goal */
  731. static int lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
  732. struct ocfs2_mask_waiter *mw)
  733. {
  734. unsigned long flags;
  735. int ret = 0;
  736. spin_lock_irqsave(&lockres->l_lock, flags);
  737. if (!list_empty(&mw->mw_item)) {
  738. if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
  739. ret = -EBUSY;
  740. list_del_init(&mw->mw_item);
  741. init_completion(&mw->mw_complete);
  742. }
  743. spin_unlock_irqrestore(&lockres->l_lock, flags);
  744. return ret;
  745. }
  746. static int ocfs2_cluster_lock(struct ocfs2_super *osb,
  747. struct ocfs2_lock_res *lockres,
  748. int level,
  749. int lkm_flags,
  750. int arg_flags)
  751. {
  752. struct ocfs2_mask_waiter mw;
  753. enum dlm_status status;
  754. int wait, catch_signals = !(osb->s_mount_opt & OCFS2_MOUNT_NOINTR);
  755. int ret = 0; /* gcc doesn't realize wait = 1 guarantees ret is set */
  756. unsigned long flags;
  757. mlog_entry_void();
  758. ocfs2_init_mask_waiter(&mw);
  759. again:
  760. wait = 0;
  761. if (catch_signals && signal_pending(current)) {
  762. ret = -ERESTARTSYS;
  763. goto out;
  764. }
  765. spin_lock_irqsave(&lockres->l_lock, flags);
  766. mlog_bug_on_msg(lockres->l_flags & OCFS2_LOCK_FREEING,
  767. "Cluster lock called on freeing lockres %s! flags "
  768. "0x%lx\n", lockres->l_name, lockres->l_flags);
  769. /* We only compare against the currently granted level
  770. * here. If the lock is blocked waiting on a downconvert,
  771. * we'll get caught below. */
  772. if (lockres->l_flags & OCFS2_LOCK_BUSY &&
  773. level > lockres->l_level) {
  774. /* is someone sitting in dlm_lock? If so, wait on
  775. * them. */
  776. lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
  777. wait = 1;
  778. goto unlock;
  779. }
  780. if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
  781. /* lock has not been created yet. */
  782. spin_unlock_irqrestore(&lockres->l_lock, flags);
  783. ret = ocfs2_lock_create(osb, lockres, LKM_NLMODE, 0);
  784. if (ret < 0) {
  785. mlog_errno(ret);
  786. goto out;
  787. }
  788. goto again;
  789. }
  790. if (lockres->l_flags & OCFS2_LOCK_BLOCKED &&
  791. !ocfs2_may_continue_on_blocked_lock(lockres, level)) {
  792. /* is the lock is currently blocked on behalf of
  793. * another node */
  794. lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BLOCKED, 0);
  795. wait = 1;
  796. goto unlock;
  797. }
  798. if (level > lockres->l_level) {
  799. if (lockres->l_action != OCFS2_AST_INVALID)
  800. mlog(ML_ERROR, "lockres %s has action %u pending\n",
  801. lockres->l_name, lockres->l_action);
  802. lockres->l_action = OCFS2_AST_CONVERT;
  803. lockres->l_requested = level;
  804. lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
  805. spin_unlock_irqrestore(&lockres->l_lock, flags);
  806. BUG_ON(level == LKM_IVMODE);
  807. BUG_ON(level == LKM_NLMODE);
  808. mlog(0, "lock %s, convert from %d to level = %d\n",
  809. lockres->l_name, lockres->l_level, level);
  810. /* call dlm_lock to upgrade lock now */
  811. status = dlmlock(osb->dlm,
  812. level,
  813. &lockres->l_lksb,
  814. lkm_flags|LKM_CONVERT|LKM_VALBLK,
  815. lockres->l_name,
  816. OCFS2_LOCK_ID_MAX_LEN - 1,
  817. lockres->l_ops->ast,
  818. lockres,
  819. lockres->l_ops->bast);
  820. if (status != DLM_NORMAL) {
  821. if ((lkm_flags & LKM_NOQUEUE) &&
  822. (status == DLM_NOTQUEUED))
  823. ret = -EAGAIN;
  824. else {
  825. ocfs2_log_dlm_error("dlmlock", status,
  826. lockres);
  827. ret = -EINVAL;
  828. }
  829. ocfs2_recover_from_dlm_error(lockres, 1);
  830. goto out;
  831. }
  832. mlog(0, "lock %s, successfull return from dlmlock\n",
  833. lockres->l_name);
  834. /* At this point we've gone inside the dlm and need to
  835. * complete our work regardless. */
  836. catch_signals = 0;
  837. /* wait for busy to clear and carry on */
  838. goto again;
  839. }
  840. /* Ok, if we get here then we're good to go. */
  841. ocfs2_inc_holders(lockres, level);
  842. ret = 0;
  843. unlock:
  844. spin_unlock_irqrestore(&lockres->l_lock, flags);
  845. out:
  846. /*
  847. * This is helping work around a lock inversion between the page lock
  848. * and dlm locks. One path holds the page lock while calling aops
  849. * which block acquiring dlm locks. The voting thread holds dlm
  850. * locks while acquiring page locks while down converting data locks.
  851. * This block is helping an aop path notice the inversion and back
  852. * off to unlock its page lock before trying the dlm lock again.
  853. */
  854. if (wait && arg_flags & OCFS2_LOCK_NONBLOCK &&
  855. mw.mw_mask & (OCFS2_LOCK_BUSY|OCFS2_LOCK_BLOCKED)) {
  856. wait = 0;
  857. if (lockres_remove_mask_waiter(lockres, &mw))
  858. ret = -EAGAIN;
  859. else
  860. goto again;
  861. }
  862. if (wait) {
  863. ret = ocfs2_wait_for_mask(&mw);
  864. if (ret == 0)
  865. goto again;
  866. mlog_errno(ret);
  867. }
  868. mlog_exit(ret);
  869. return ret;
  870. }
  871. static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
  872. struct ocfs2_lock_res *lockres,
  873. int level)
  874. {
  875. unsigned long flags;
  876. mlog_entry_void();
  877. spin_lock_irqsave(&lockres->l_lock, flags);
  878. ocfs2_dec_holders(lockres, level);
  879. ocfs2_vote_on_unlock(osb, lockres);
  880. spin_unlock_irqrestore(&lockres->l_lock, flags);
  881. mlog_exit_void();
  882. }
  883. static int ocfs2_create_new_inode_lock(struct inode *inode,
  884. struct ocfs2_lock_res *lockres)
  885. {
  886. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  887. unsigned long flags;
  888. spin_lock_irqsave(&lockres->l_lock, flags);
  889. BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
  890. lockres_or_flags(lockres, OCFS2_LOCK_LOCAL);
  891. spin_unlock_irqrestore(&lockres->l_lock, flags);
  892. return ocfs2_lock_create(osb, lockres, LKM_EXMODE, LKM_LOCAL);
  893. }
  894. /* Grants us an EX lock on the data and metadata resources, skipping
  895. * the normal cluster directory lookup. Use this ONLY on newly created
  896. * inodes which other nodes can't possibly see, and which haven't been
  897. * hashed in the inode hash yet. This can give us a good performance
  898. * increase as it'll skip the network broadcast normally associated
  899. * with creating a new lock resource. */
  900. int ocfs2_create_new_inode_locks(struct inode *inode)
  901. {
  902. int ret;
  903. BUG_ON(!inode);
  904. BUG_ON(!ocfs2_inode_is_new(inode));
  905. mlog_entry_void();
  906. mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
  907. /* NOTE: That we don't increment any of the holder counts, nor
  908. * do we add anything to a journal handle. Since this is
  909. * supposed to be a new inode which the cluster doesn't know
  910. * about yet, there is no need to. As far as the LVB handling
  911. * is concerned, this is basically like acquiring an EX lock
  912. * on a resource which has an invalid one -- we'll set it
  913. * valid when we release the EX. */
  914. ret = ocfs2_create_new_inode_lock(inode,
  915. &OCFS2_I(inode)->ip_rw_lockres);
  916. if (ret) {
  917. mlog_errno(ret);
  918. goto bail;
  919. }
  920. ret = ocfs2_create_new_inode_lock(inode,
  921. &OCFS2_I(inode)->ip_meta_lockres);
  922. if (ret) {
  923. mlog_errno(ret);
  924. goto bail;
  925. }
  926. ret = ocfs2_create_new_inode_lock(inode,
  927. &OCFS2_I(inode)->ip_data_lockres);
  928. if (ret) {
  929. mlog_errno(ret);
  930. goto bail;
  931. }
  932. bail:
  933. mlog_exit(ret);
  934. return ret;
  935. }
  936. int ocfs2_rw_lock(struct inode *inode, int write)
  937. {
  938. int status, level;
  939. struct ocfs2_lock_res *lockres;
  940. BUG_ON(!inode);
  941. mlog_entry_void();
  942. mlog(0, "inode %llu take %s RW lock\n",
  943. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  944. write ? "EXMODE" : "PRMODE");
  945. lockres = &OCFS2_I(inode)->ip_rw_lockres;
  946. level = write ? LKM_EXMODE : LKM_PRMODE;
  947. status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level, 0,
  948. 0);
  949. if (status < 0)
  950. mlog_errno(status);
  951. mlog_exit(status);
  952. return status;
  953. }
  954. void ocfs2_rw_unlock(struct inode *inode, int write)
  955. {
  956. int level = write ? LKM_EXMODE : LKM_PRMODE;
  957. struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_rw_lockres;
  958. mlog_entry_void();
  959. mlog(0, "inode %llu drop %s RW lock\n",
  960. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  961. write ? "EXMODE" : "PRMODE");
  962. ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
  963. mlog_exit_void();
  964. }
  965. int ocfs2_data_lock_full(struct inode *inode,
  966. int write,
  967. int arg_flags)
  968. {
  969. int status = 0, level;
  970. struct ocfs2_lock_res *lockres;
  971. BUG_ON(!inode);
  972. mlog_entry_void();
  973. mlog(0, "inode %llu take %s DATA lock\n",
  974. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  975. write ? "EXMODE" : "PRMODE");
  976. /* We'll allow faking a readonly data lock for
  977. * rodevices. */
  978. if (ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb))) {
  979. if (write) {
  980. status = -EROFS;
  981. mlog_errno(status);
  982. }
  983. goto out;
  984. }
  985. lockres = &OCFS2_I(inode)->ip_data_lockres;
  986. level = write ? LKM_EXMODE : LKM_PRMODE;
  987. status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level,
  988. 0, arg_flags);
  989. if (status < 0 && status != -EAGAIN)
  990. mlog_errno(status);
  991. out:
  992. mlog_exit(status);
  993. return status;
  994. }
  995. /* see ocfs2_meta_lock_with_page() */
  996. int ocfs2_data_lock_with_page(struct inode *inode,
  997. int write,
  998. struct page *page)
  999. {
  1000. int ret;
  1001. ret = ocfs2_data_lock_full(inode, write, OCFS2_LOCK_NONBLOCK);
  1002. if (ret == -EAGAIN) {
  1003. unlock_page(page);
  1004. if (ocfs2_data_lock(inode, write) == 0)
  1005. ocfs2_data_unlock(inode, write);
  1006. ret = AOP_TRUNCATED_PAGE;
  1007. }
  1008. return ret;
  1009. }
  1010. static void ocfs2_vote_on_unlock(struct ocfs2_super *osb,
  1011. struct ocfs2_lock_res *lockres)
  1012. {
  1013. int kick = 0;
  1014. mlog_entry_void();
  1015. /* If we know that another node is waiting on our lock, kick
  1016. * the vote thread * pre-emptively when we reach a release
  1017. * condition. */
  1018. if (lockres->l_flags & OCFS2_LOCK_BLOCKED) {
  1019. switch(lockres->l_blocking) {
  1020. case LKM_EXMODE:
  1021. if (!lockres->l_ex_holders && !lockres->l_ro_holders)
  1022. kick = 1;
  1023. break;
  1024. case LKM_PRMODE:
  1025. if (!lockres->l_ex_holders)
  1026. kick = 1;
  1027. break;
  1028. default:
  1029. BUG();
  1030. }
  1031. }
  1032. if (kick)
  1033. ocfs2_kick_vote_thread(osb);
  1034. mlog_exit_void();
  1035. }
  1036. void ocfs2_data_unlock(struct inode *inode,
  1037. int write)
  1038. {
  1039. int level = write ? LKM_EXMODE : LKM_PRMODE;
  1040. struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_data_lockres;
  1041. mlog_entry_void();
  1042. mlog(0, "inode %llu drop %s DATA lock\n",
  1043. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1044. write ? "EXMODE" : "PRMODE");
  1045. if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)))
  1046. ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
  1047. mlog_exit_void();
  1048. }
  1049. #define OCFS2_SEC_BITS 34
  1050. #define OCFS2_SEC_SHIFT (64 - 34)
  1051. #define OCFS2_NSEC_MASK ((1ULL << OCFS2_SEC_SHIFT) - 1)
  1052. /* LVB only has room for 64 bits of time here so we pack it for
  1053. * now. */
  1054. static u64 ocfs2_pack_timespec(struct timespec *spec)
  1055. {
  1056. u64 res;
  1057. u64 sec = spec->tv_sec;
  1058. u32 nsec = spec->tv_nsec;
  1059. res = (sec << OCFS2_SEC_SHIFT) | (nsec & OCFS2_NSEC_MASK);
  1060. return res;
  1061. }
  1062. /* Call this with the lockres locked. I am reasonably sure we don't
  1063. * need ip_lock in this function as anyone who would be changing those
  1064. * values is supposed to be blocked in ocfs2_meta_lock right now. */
  1065. static void __ocfs2_stuff_meta_lvb(struct inode *inode)
  1066. {
  1067. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1068. struct ocfs2_lock_res *lockres = &oi->ip_meta_lockres;
  1069. struct ocfs2_meta_lvb *lvb;
  1070. mlog_entry_void();
  1071. lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
  1072. lvb->lvb_version = cpu_to_be32(OCFS2_LVB_VERSION);
  1073. lvb->lvb_isize = cpu_to_be64(i_size_read(inode));
  1074. lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters);
  1075. lvb->lvb_iuid = cpu_to_be32(inode->i_uid);
  1076. lvb->lvb_igid = cpu_to_be32(inode->i_gid);
  1077. lvb->lvb_imode = cpu_to_be16(inode->i_mode);
  1078. lvb->lvb_inlink = cpu_to_be16(inode->i_nlink);
  1079. lvb->lvb_iatime_packed =
  1080. cpu_to_be64(ocfs2_pack_timespec(&inode->i_atime));
  1081. lvb->lvb_ictime_packed =
  1082. cpu_to_be64(ocfs2_pack_timespec(&inode->i_ctime));
  1083. lvb->lvb_imtime_packed =
  1084. cpu_to_be64(ocfs2_pack_timespec(&inode->i_mtime));
  1085. lvb->lvb_iattr = cpu_to_be32(oi->ip_attr);
  1086. mlog_meta_lvb(0, lockres);
  1087. mlog_exit_void();
  1088. }
  1089. static void ocfs2_unpack_timespec(struct timespec *spec,
  1090. u64 packed_time)
  1091. {
  1092. spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
  1093. spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
  1094. }
  1095. static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
  1096. {
  1097. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1098. struct ocfs2_lock_res *lockres = &oi->ip_meta_lockres;
  1099. struct ocfs2_meta_lvb *lvb;
  1100. mlog_entry_void();
  1101. mlog_meta_lvb(0, lockres);
  1102. lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
  1103. /* We're safe here without the lockres lock... */
  1104. spin_lock(&oi->ip_lock);
  1105. oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters);
  1106. i_size_write(inode, be64_to_cpu(lvb->lvb_isize));
  1107. oi->ip_attr = be32_to_cpu(lvb->lvb_iattr);
  1108. ocfs2_set_inode_flags(inode);
  1109. /* fast-symlinks are a special case */
  1110. if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
  1111. inode->i_blocks = 0;
  1112. else
  1113. inode->i_blocks =
  1114. ocfs2_align_bytes_to_sectors(i_size_read(inode));
  1115. inode->i_uid = be32_to_cpu(lvb->lvb_iuid);
  1116. inode->i_gid = be32_to_cpu(lvb->lvb_igid);
  1117. inode->i_mode = be16_to_cpu(lvb->lvb_imode);
  1118. inode->i_nlink = be16_to_cpu(lvb->lvb_inlink);
  1119. ocfs2_unpack_timespec(&inode->i_atime,
  1120. be64_to_cpu(lvb->lvb_iatime_packed));
  1121. ocfs2_unpack_timespec(&inode->i_mtime,
  1122. be64_to_cpu(lvb->lvb_imtime_packed));
  1123. ocfs2_unpack_timespec(&inode->i_ctime,
  1124. be64_to_cpu(lvb->lvb_ictime_packed));
  1125. spin_unlock(&oi->ip_lock);
  1126. mlog_exit_void();
  1127. }
  1128. static inline int ocfs2_meta_lvb_is_trustable(struct ocfs2_lock_res *lockres)
  1129. {
  1130. struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
  1131. if (be32_to_cpu(lvb->lvb_version) == OCFS2_LVB_VERSION)
  1132. return 1;
  1133. return 0;
  1134. }
  1135. /* Determine whether a lock resource needs to be refreshed, and
  1136. * arbitrate who gets to refresh it.
  1137. *
  1138. * 0 means no refresh needed.
  1139. *
  1140. * > 0 means you need to refresh this and you MUST call
  1141. * ocfs2_complete_lock_res_refresh afterwards. */
  1142. static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
  1143. {
  1144. unsigned long flags;
  1145. int status = 0;
  1146. mlog_entry_void();
  1147. refresh_check:
  1148. spin_lock_irqsave(&lockres->l_lock, flags);
  1149. if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
  1150. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1151. goto bail;
  1152. }
  1153. if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
  1154. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1155. ocfs2_wait_on_refreshing_lock(lockres);
  1156. goto refresh_check;
  1157. }
  1158. /* Ok, I'll be the one to refresh this lock. */
  1159. lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
  1160. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1161. status = 1;
  1162. bail:
  1163. mlog_exit(status);
  1164. return status;
  1165. }
  1166. /* If status is non zero, I'll mark it as not being in refresh
  1167. * anymroe, but i won't clear the needs refresh flag. */
  1168. static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
  1169. int status)
  1170. {
  1171. unsigned long flags;
  1172. mlog_entry_void();
  1173. spin_lock_irqsave(&lockres->l_lock, flags);
  1174. lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
  1175. if (!status)
  1176. lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
  1177. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1178. wake_up(&lockres->l_event);
  1179. mlog_exit_void();
  1180. }
  1181. /* may or may not return a bh if it went to disk. */
  1182. static int ocfs2_meta_lock_update(struct inode *inode,
  1183. struct buffer_head **bh)
  1184. {
  1185. int status = 0;
  1186. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1187. struct ocfs2_lock_res *lockres;
  1188. struct ocfs2_dinode *fe;
  1189. mlog_entry_void();
  1190. spin_lock(&oi->ip_lock);
  1191. if (oi->ip_flags & OCFS2_INODE_DELETED) {
  1192. mlog(0, "Orphaned inode %llu was deleted while we "
  1193. "were waiting on a lock. ip_flags = 0x%x\n",
  1194. (unsigned long long)oi->ip_blkno, oi->ip_flags);
  1195. spin_unlock(&oi->ip_lock);
  1196. status = -ENOENT;
  1197. goto bail;
  1198. }
  1199. spin_unlock(&oi->ip_lock);
  1200. lockres = &oi->ip_meta_lockres;
  1201. if (!ocfs2_should_refresh_lock_res(lockres))
  1202. goto bail;
  1203. /* This will discard any caching information we might have had
  1204. * for the inode metadata. */
  1205. ocfs2_metadata_cache_purge(inode);
  1206. /* will do nothing for inode types that don't use the extent
  1207. * map (directories, bitmap files, etc) */
  1208. ocfs2_extent_map_trunc(inode, 0);
  1209. if (ocfs2_meta_lvb_is_trustable(lockres)) {
  1210. mlog(0, "Trusting LVB on inode %llu\n",
  1211. (unsigned long long)oi->ip_blkno);
  1212. ocfs2_refresh_inode_from_lvb(inode);
  1213. } else {
  1214. /* Boo, we have to go to disk. */
  1215. /* read bh, cast, ocfs2_refresh_inode */
  1216. status = ocfs2_read_block(OCFS2_SB(inode->i_sb), oi->ip_blkno,
  1217. bh, OCFS2_BH_CACHED, inode);
  1218. if (status < 0) {
  1219. mlog_errno(status);
  1220. goto bail_refresh;
  1221. }
  1222. fe = (struct ocfs2_dinode *) (*bh)->b_data;
  1223. /* This is a good chance to make sure we're not
  1224. * locking an invalid object.
  1225. *
  1226. * We bug on a stale inode here because we checked
  1227. * above whether it was wiped from disk. The wiping
  1228. * node provides a guarantee that we receive that
  1229. * message and can mark the inode before dropping any
  1230. * locks associated with it. */
  1231. if (!OCFS2_IS_VALID_DINODE(fe)) {
  1232. OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
  1233. status = -EIO;
  1234. goto bail_refresh;
  1235. }
  1236. mlog_bug_on_msg(inode->i_generation !=
  1237. le32_to_cpu(fe->i_generation),
  1238. "Invalid dinode %llu disk generation: %u "
  1239. "inode->i_generation: %u\n",
  1240. (unsigned long long)oi->ip_blkno,
  1241. le32_to_cpu(fe->i_generation),
  1242. inode->i_generation);
  1243. mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
  1244. !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
  1245. "Stale dinode %llu dtime: %llu flags: 0x%x\n",
  1246. (unsigned long long)oi->ip_blkno,
  1247. (unsigned long long)le64_to_cpu(fe->i_dtime),
  1248. le32_to_cpu(fe->i_flags));
  1249. ocfs2_refresh_inode(inode, fe);
  1250. }
  1251. status = 0;
  1252. bail_refresh:
  1253. ocfs2_complete_lock_res_refresh(lockres, status);
  1254. bail:
  1255. mlog_exit(status);
  1256. return status;
  1257. }
  1258. static int ocfs2_assign_bh(struct inode *inode,
  1259. struct buffer_head **ret_bh,
  1260. struct buffer_head *passed_bh)
  1261. {
  1262. int status;
  1263. if (passed_bh) {
  1264. /* Ok, the update went to disk for us, use the
  1265. * returned bh. */
  1266. *ret_bh = passed_bh;
  1267. get_bh(*ret_bh);
  1268. return 0;
  1269. }
  1270. status = ocfs2_read_block(OCFS2_SB(inode->i_sb),
  1271. OCFS2_I(inode)->ip_blkno,
  1272. ret_bh,
  1273. OCFS2_BH_CACHED,
  1274. inode);
  1275. if (status < 0)
  1276. mlog_errno(status);
  1277. return status;
  1278. }
  1279. /*
  1280. * returns < 0 error if the callback will never be called, otherwise
  1281. * the result of the lock will be communicated via the callback.
  1282. */
  1283. int ocfs2_meta_lock_full(struct inode *inode,
  1284. struct ocfs2_journal_handle *handle,
  1285. struct buffer_head **ret_bh,
  1286. int ex,
  1287. int arg_flags)
  1288. {
  1289. int status, level, dlm_flags, acquired;
  1290. struct ocfs2_lock_res *lockres;
  1291. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1292. struct buffer_head *local_bh = NULL;
  1293. BUG_ON(!inode);
  1294. mlog_entry_void();
  1295. mlog(0, "inode %llu, take %s META lock\n",
  1296. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1297. ex ? "EXMODE" : "PRMODE");
  1298. status = 0;
  1299. acquired = 0;
  1300. /* We'll allow faking a readonly metadata lock for
  1301. * rodevices. */
  1302. if (ocfs2_is_hard_readonly(osb)) {
  1303. if (ex)
  1304. status = -EROFS;
  1305. goto bail;
  1306. }
  1307. if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
  1308. wait_event(osb->recovery_event,
  1309. ocfs2_node_map_is_empty(osb, &osb->recovery_map));
  1310. acquired = 0;
  1311. lockres = &OCFS2_I(inode)->ip_meta_lockres;
  1312. level = ex ? LKM_EXMODE : LKM_PRMODE;
  1313. dlm_flags = 0;
  1314. if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
  1315. dlm_flags |= LKM_NOQUEUE;
  1316. status = ocfs2_cluster_lock(osb, lockres, level, dlm_flags, arg_flags);
  1317. if (status < 0) {
  1318. if (status != -EAGAIN && status != -EIOCBRETRY)
  1319. mlog_errno(status);
  1320. goto bail;
  1321. }
  1322. /* Notify the error cleanup path to drop the cluster lock. */
  1323. acquired = 1;
  1324. /* We wait twice because a node may have died while we were in
  1325. * the lower dlm layers. The second time though, we've
  1326. * committed to owning this lock so we don't allow signals to
  1327. * abort the operation. */
  1328. if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
  1329. wait_event(osb->recovery_event,
  1330. ocfs2_node_map_is_empty(osb, &osb->recovery_map));
  1331. /* This is fun. The caller may want a bh back, or it may
  1332. * not. ocfs2_meta_lock_update definitely wants one in, but
  1333. * may or may not read one, depending on what's in the
  1334. * LVB. The result of all of this is that we've *only* gone to
  1335. * disk if we have to, so the complexity is worthwhile. */
  1336. status = ocfs2_meta_lock_update(inode, &local_bh);
  1337. if (status < 0) {
  1338. if (status != -ENOENT)
  1339. mlog_errno(status);
  1340. goto bail;
  1341. }
  1342. if (ret_bh) {
  1343. status = ocfs2_assign_bh(inode, ret_bh, local_bh);
  1344. if (status < 0) {
  1345. mlog_errno(status);
  1346. goto bail;
  1347. }
  1348. }
  1349. if (handle) {
  1350. status = ocfs2_handle_add_lock(handle, inode);
  1351. if (status < 0)
  1352. mlog_errno(status);
  1353. }
  1354. bail:
  1355. if (status < 0) {
  1356. if (ret_bh && (*ret_bh)) {
  1357. brelse(*ret_bh);
  1358. *ret_bh = NULL;
  1359. }
  1360. if (acquired)
  1361. ocfs2_meta_unlock(inode, ex);
  1362. }
  1363. if (local_bh)
  1364. brelse(local_bh);
  1365. mlog_exit(status);
  1366. return status;
  1367. }
  1368. /*
  1369. * This is working around a lock inversion between tasks acquiring DLM locks
  1370. * while holding a page lock and the vote thread which blocks dlm lock acquiry
  1371. * while acquiring page locks.
  1372. *
  1373. * ** These _with_page variantes are only intended to be called from aop
  1374. * methods that hold page locks and return a very specific *positive* error
  1375. * code that aop methods pass up to the VFS -- test for errors with != 0. **
  1376. *
  1377. * The DLM is called such that it returns -EAGAIN if it would have blocked
  1378. * waiting for the vote thread. In that case we unlock our page so the vote
  1379. * thread can make progress. Once we've done this we have to return
  1380. * AOP_TRUNCATED_PAGE so the aop method that called us can bubble that back up
  1381. * into the VFS who will then immediately retry the aop call.
  1382. *
  1383. * We do a blocking lock and immediate unlock before returning, though, so that
  1384. * the lock has a great chance of being cached on this node by the time the VFS
  1385. * calls back to retry the aop. This has a potential to livelock as nodes
  1386. * ping locks back and forth, but that's a risk we're willing to take to avoid
  1387. * the lock inversion simply.
  1388. */
  1389. int ocfs2_meta_lock_with_page(struct inode *inode,
  1390. struct ocfs2_journal_handle *handle,
  1391. struct buffer_head **ret_bh,
  1392. int ex,
  1393. struct page *page)
  1394. {
  1395. int ret;
  1396. ret = ocfs2_meta_lock_full(inode, handle, ret_bh, ex,
  1397. OCFS2_LOCK_NONBLOCK);
  1398. if (ret == -EAGAIN) {
  1399. unlock_page(page);
  1400. if (ocfs2_meta_lock(inode, handle, ret_bh, ex) == 0)
  1401. ocfs2_meta_unlock(inode, ex);
  1402. ret = AOP_TRUNCATED_PAGE;
  1403. }
  1404. return ret;
  1405. }
  1406. void ocfs2_meta_unlock(struct inode *inode,
  1407. int ex)
  1408. {
  1409. int level = ex ? LKM_EXMODE : LKM_PRMODE;
  1410. struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_meta_lockres;
  1411. mlog_entry_void();
  1412. mlog(0, "inode %llu drop %s META lock\n",
  1413. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1414. ex ? "EXMODE" : "PRMODE");
  1415. if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)))
  1416. ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
  1417. mlog_exit_void();
  1418. }
  1419. int ocfs2_super_lock(struct ocfs2_super *osb,
  1420. int ex)
  1421. {
  1422. int status;
  1423. int level = ex ? LKM_EXMODE : LKM_PRMODE;
  1424. struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
  1425. struct buffer_head *bh;
  1426. struct ocfs2_slot_info *si = osb->slot_info;
  1427. mlog_entry_void();
  1428. if (ocfs2_is_hard_readonly(osb))
  1429. return -EROFS;
  1430. status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
  1431. if (status < 0) {
  1432. mlog_errno(status);
  1433. goto bail;
  1434. }
  1435. /* The super block lock path is really in the best position to
  1436. * know when resources covered by the lock need to be
  1437. * refreshed, so we do it here. Of course, making sense of
  1438. * everything is up to the caller :) */
  1439. status = ocfs2_should_refresh_lock_res(lockres);
  1440. if (status < 0) {
  1441. mlog_errno(status);
  1442. goto bail;
  1443. }
  1444. if (status) {
  1445. bh = si->si_bh;
  1446. status = ocfs2_read_block(osb, bh->b_blocknr, &bh, 0,
  1447. si->si_inode);
  1448. if (status == 0)
  1449. ocfs2_update_slot_info(si);
  1450. ocfs2_complete_lock_res_refresh(lockres, status);
  1451. if (status < 0)
  1452. mlog_errno(status);
  1453. }
  1454. bail:
  1455. mlog_exit(status);
  1456. return status;
  1457. }
  1458. void ocfs2_super_unlock(struct ocfs2_super *osb,
  1459. int ex)
  1460. {
  1461. int level = ex ? LKM_EXMODE : LKM_PRMODE;
  1462. struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
  1463. ocfs2_cluster_unlock(osb, lockres, level);
  1464. }
  1465. int ocfs2_rename_lock(struct ocfs2_super *osb)
  1466. {
  1467. int status;
  1468. struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
  1469. if (ocfs2_is_hard_readonly(osb))
  1470. return -EROFS;
  1471. status = ocfs2_cluster_lock(osb, lockres, LKM_EXMODE, 0, 0);
  1472. if (status < 0)
  1473. mlog_errno(status);
  1474. return status;
  1475. }
  1476. void ocfs2_rename_unlock(struct ocfs2_super *osb)
  1477. {
  1478. struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
  1479. ocfs2_cluster_unlock(osb, lockres, LKM_EXMODE);
  1480. }
  1481. /* Reference counting of the dlm debug structure. We want this because
  1482. * open references on the debug inodes can live on after a mount, so
  1483. * we can't rely on the ocfs2_super to always exist. */
  1484. static void ocfs2_dlm_debug_free(struct kref *kref)
  1485. {
  1486. struct ocfs2_dlm_debug *dlm_debug;
  1487. dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
  1488. kfree(dlm_debug);
  1489. }
  1490. void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
  1491. {
  1492. if (dlm_debug)
  1493. kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
  1494. }
  1495. static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
  1496. {
  1497. kref_get(&debug->d_refcnt);
  1498. }
  1499. struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
  1500. {
  1501. struct ocfs2_dlm_debug *dlm_debug;
  1502. dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
  1503. if (!dlm_debug) {
  1504. mlog_errno(-ENOMEM);
  1505. goto out;
  1506. }
  1507. kref_init(&dlm_debug->d_refcnt);
  1508. INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
  1509. dlm_debug->d_locking_state = NULL;
  1510. out:
  1511. return dlm_debug;
  1512. }
  1513. /* Access to this is arbitrated for us via seq_file->sem. */
  1514. struct ocfs2_dlm_seq_priv {
  1515. struct ocfs2_dlm_debug *p_dlm_debug;
  1516. struct ocfs2_lock_res p_iter_res;
  1517. struct ocfs2_lock_res p_tmp_res;
  1518. };
  1519. static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
  1520. struct ocfs2_dlm_seq_priv *priv)
  1521. {
  1522. struct ocfs2_lock_res *iter, *ret = NULL;
  1523. struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
  1524. assert_spin_locked(&ocfs2_dlm_tracking_lock);
  1525. list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
  1526. /* discover the head of the list */
  1527. if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
  1528. mlog(0, "End of list found, %p\n", ret);
  1529. break;
  1530. }
  1531. /* We track our "dummy" iteration lockres' by a NULL
  1532. * l_ops field. */
  1533. if (iter->l_ops != NULL) {
  1534. ret = iter;
  1535. break;
  1536. }
  1537. }
  1538. return ret;
  1539. }
  1540. static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
  1541. {
  1542. struct ocfs2_dlm_seq_priv *priv = m->private;
  1543. struct ocfs2_lock_res *iter;
  1544. spin_lock(&ocfs2_dlm_tracking_lock);
  1545. iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
  1546. if (iter) {
  1547. /* Since lockres' have the lifetime of their container
  1548. * (which can be inodes, ocfs2_supers, etc) we want to
  1549. * copy this out to a temporary lockres while still
  1550. * under the spinlock. Obviously after this we can't
  1551. * trust any pointers on the copy returned, but that's
  1552. * ok as the information we want isn't typically held
  1553. * in them. */
  1554. priv->p_tmp_res = *iter;
  1555. iter = &priv->p_tmp_res;
  1556. }
  1557. spin_unlock(&ocfs2_dlm_tracking_lock);
  1558. return iter;
  1559. }
  1560. static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
  1561. {
  1562. }
  1563. static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
  1564. {
  1565. struct ocfs2_dlm_seq_priv *priv = m->private;
  1566. struct ocfs2_lock_res *iter = v;
  1567. struct ocfs2_lock_res *dummy = &priv->p_iter_res;
  1568. spin_lock(&ocfs2_dlm_tracking_lock);
  1569. iter = ocfs2_dlm_next_res(iter, priv);
  1570. list_del_init(&dummy->l_debug_list);
  1571. if (iter) {
  1572. list_add(&dummy->l_debug_list, &iter->l_debug_list);
  1573. priv->p_tmp_res = *iter;
  1574. iter = &priv->p_tmp_res;
  1575. }
  1576. spin_unlock(&ocfs2_dlm_tracking_lock);
  1577. return iter;
  1578. }
  1579. /* So that debugfs.ocfs2 can determine which format is being used */
  1580. #define OCFS2_DLM_DEBUG_STR_VERSION 1
  1581. static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
  1582. {
  1583. int i;
  1584. char *lvb;
  1585. struct ocfs2_lock_res *lockres = v;
  1586. if (!lockres)
  1587. return -EINVAL;
  1588. seq_printf(m, "0x%x\t"
  1589. "%.*s\t"
  1590. "%d\t"
  1591. "0x%lx\t"
  1592. "0x%x\t"
  1593. "0x%x\t"
  1594. "%u\t"
  1595. "%u\t"
  1596. "%d\t"
  1597. "%d\t",
  1598. OCFS2_DLM_DEBUG_STR_VERSION,
  1599. OCFS2_LOCK_ID_MAX_LEN, lockres->l_name,
  1600. lockres->l_level,
  1601. lockres->l_flags,
  1602. lockres->l_action,
  1603. lockres->l_unlock_action,
  1604. lockres->l_ro_holders,
  1605. lockres->l_ex_holders,
  1606. lockres->l_requested,
  1607. lockres->l_blocking);
  1608. /* Dump the raw LVB */
  1609. lvb = lockres->l_lksb.lvb;
  1610. for(i = 0; i < DLM_LVB_LEN; i++)
  1611. seq_printf(m, "0x%x\t", lvb[i]);
  1612. /* End the line */
  1613. seq_printf(m, "\n");
  1614. return 0;
  1615. }
  1616. static struct seq_operations ocfs2_dlm_seq_ops = {
  1617. .start = ocfs2_dlm_seq_start,
  1618. .stop = ocfs2_dlm_seq_stop,
  1619. .next = ocfs2_dlm_seq_next,
  1620. .show = ocfs2_dlm_seq_show,
  1621. };
  1622. static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
  1623. {
  1624. struct seq_file *seq = (struct seq_file *) file->private_data;
  1625. struct ocfs2_dlm_seq_priv *priv = seq->private;
  1626. struct ocfs2_lock_res *res = &priv->p_iter_res;
  1627. ocfs2_remove_lockres_tracking(res);
  1628. ocfs2_put_dlm_debug(priv->p_dlm_debug);
  1629. return seq_release_private(inode, file);
  1630. }
  1631. static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
  1632. {
  1633. int ret;
  1634. struct ocfs2_dlm_seq_priv *priv;
  1635. struct seq_file *seq;
  1636. struct ocfs2_super *osb;
  1637. priv = kzalloc(sizeof(struct ocfs2_dlm_seq_priv), GFP_KERNEL);
  1638. if (!priv) {
  1639. ret = -ENOMEM;
  1640. mlog_errno(ret);
  1641. goto out;
  1642. }
  1643. osb = (struct ocfs2_super *) inode->u.generic_ip;
  1644. ocfs2_get_dlm_debug(osb->osb_dlm_debug);
  1645. priv->p_dlm_debug = osb->osb_dlm_debug;
  1646. INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
  1647. ret = seq_open(file, &ocfs2_dlm_seq_ops);
  1648. if (ret) {
  1649. kfree(priv);
  1650. mlog_errno(ret);
  1651. goto out;
  1652. }
  1653. seq = (struct seq_file *) file->private_data;
  1654. seq->private = priv;
  1655. ocfs2_add_lockres_tracking(&priv->p_iter_res,
  1656. priv->p_dlm_debug);
  1657. out:
  1658. return ret;
  1659. }
  1660. static const struct file_operations ocfs2_dlm_debug_fops = {
  1661. .open = ocfs2_dlm_debug_open,
  1662. .release = ocfs2_dlm_debug_release,
  1663. .read = seq_read,
  1664. .llseek = seq_lseek,
  1665. };
  1666. static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
  1667. {
  1668. int ret = 0;
  1669. struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
  1670. dlm_debug->d_locking_state = debugfs_create_file("locking_state",
  1671. S_IFREG|S_IRUSR,
  1672. osb->osb_debug_root,
  1673. osb,
  1674. &ocfs2_dlm_debug_fops);
  1675. if (!dlm_debug->d_locking_state) {
  1676. ret = -EINVAL;
  1677. mlog(ML_ERROR,
  1678. "Unable to create locking state debugfs file.\n");
  1679. goto out;
  1680. }
  1681. ocfs2_get_dlm_debug(dlm_debug);
  1682. out:
  1683. return ret;
  1684. }
  1685. static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
  1686. {
  1687. struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
  1688. if (dlm_debug) {
  1689. debugfs_remove(dlm_debug->d_locking_state);
  1690. ocfs2_put_dlm_debug(dlm_debug);
  1691. }
  1692. }
  1693. int ocfs2_dlm_init(struct ocfs2_super *osb)
  1694. {
  1695. int status;
  1696. u32 dlm_key;
  1697. struct dlm_ctxt *dlm;
  1698. mlog_entry_void();
  1699. status = ocfs2_dlm_init_debug(osb);
  1700. if (status < 0) {
  1701. mlog_errno(status);
  1702. goto bail;
  1703. }
  1704. /* launch vote thread */
  1705. osb->vote_task = kthread_run(ocfs2_vote_thread, osb, "ocfs2vote");
  1706. if (IS_ERR(osb->vote_task)) {
  1707. status = PTR_ERR(osb->vote_task);
  1708. osb->vote_task = NULL;
  1709. mlog_errno(status);
  1710. goto bail;
  1711. }
  1712. /* used by the dlm code to make message headers unique, each
  1713. * node in this domain must agree on this. */
  1714. dlm_key = crc32_le(0, osb->uuid_str, strlen(osb->uuid_str));
  1715. /* for now, uuid == domain */
  1716. dlm = dlm_register_domain(osb->uuid_str, dlm_key);
  1717. if (IS_ERR(dlm)) {
  1718. status = PTR_ERR(dlm);
  1719. mlog_errno(status);
  1720. goto bail;
  1721. }
  1722. ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
  1723. ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
  1724. dlm_register_eviction_cb(dlm, &osb->osb_eviction_cb);
  1725. osb->dlm = dlm;
  1726. status = 0;
  1727. bail:
  1728. if (status < 0) {
  1729. ocfs2_dlm_shutdown_debug(osb);
  1730. if (osb->vote_task)
  1731. kthread_stop(osb->vote_task);
  1732. }
  1733. mlog_exit(status);
  1734. return status;
  1735. }
  1736. void ocfs2_dlm_shutdown(struct ocfs2_super *osb)
  1737. {
  1738. mlog_entry_void();
  1739. dlm_unregister_eviction_cb(&osb->osb_eviction_cb);
  1740. ocfs2_drop_osb_locks(osb);
  1741. if (osb->vote_task) {
  1742. kthread_stop(osb->vote_task);
  1743. osb->vote_task = NULL;
  1744. }
  1745. ocfs2_lock_res_free(&osb->osb_super_lockres);
  1746. ocfs2_lock_res_free(&osb->osb_rename_lockres);
  1747. dlm_unregister_domain(osb->dlm);
  1748. osb->dlm = NULL;
  1749. ocfs2_dlm_shutdown_debug(osb);
  1750. mlog_exit_void();
  1751. }
  1752. static void ocfs2_unlock_ast_func(void *opaque, enum dlm_status status)
  1753. {
  1754. struct ocfs2_lock_res *lockres = opaque;
  1755. unsigned long flags;
  1756. mlog_entry_void();
  1757. mlog(0, "UNLOCK AST called on lock %s, action = %d\n", lockres->l_name,
  1758. lockres->l_unlock_action);
  1759. spin_lock_irqsave(&lockres->l_lock, flags);
  1760. /* We tried to cancel a convert request, but it was already
  1761. * granted. All we want to do here is clear our unlock
  1762. * state. The wake_up call done at the bottom is redundant
  1763. * (ocfs2_prepare_cancel_convert doesn't sleep on this) but doesn't
  1764. * hurt anything anyway */
  1765. if (status == DLM_CANCELGRANT &&
  1766. lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
  1767. mlog(0, "Got cancelgrant for %s\n", lockres->l_name);
  1768. /* We don't clear the busy flag in this case as it
  1769. * should have been cleared by the ast which the dlm
  1770. * has called. */
  1771. goto complete_unlock;
  1772. }
  1773. if (status != DLM_NORMAL) {
  1774. mlog(ML_ERROR, "Dlm passes status %d for lock %s, "
  1775. "unlock_action %d\n", status, lockres->l_name,
  1776. lockres->l_unlock_action);
  1777. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1778. return;
  1779. }
  1780. switch(lockres->l_unlock_action) {
  1781. case OCFS2_UNLOCK_CANCEL_CONVERT:
  1782. mlog(0, "Cancel convert success for %s\n", lockres->l_name);
  1783. lockres->l_action = OCFS2_AST_INVALID;
  1784. break;
  1785. case OCFS2_UNLOCK_DROP_LOCK:
  1786. lockres->l_level = LKM_IVMODE;
  1787. break;
  1788. default:
  1789. BUG();
  1790. }
  1791. lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
  1792. complete_unlock:
  1793. lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
  1794. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1795. wake_up(&lockres->l_event);
  1796. mlog_exit_void();
  1797. }
  1798. typedef void (ocfs2_pre_drop_cb_t)(struct ocfs2_lock_res *, void *);
  1799. struct drop_lock_cb {
  1800. ocfs2_pre_drop_cb_t *drop_func;
  1801. void *drop_data;
  1802. };
  1803. static int ocfs2_drop_lock(struct ocfs2_super *osb,
  1804. struct ocfs2_lock_res *lockres,
  1805. struct drop_lock_cb *dcb)
  1806. {
  1807. enum dlm_status status;
  1808. unsigned long flags;
  1809. /* We didn't get anywhere near actually using this lockres. */
  1810. if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
  1811. goto out;
  1812. spin_lock_irqsave(&lockres->l_lock, flags);
  1813. mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
  1814. "lockres %s, flags 0x%lx\n",
  1815. lockres->l_name, lockres->l_flags);
  1816. while (lockres->l_flags & OCFS2_LOCK_BUSY) {
  1817. mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
  1818. "%u, unlock_action = %u\n",
  1819. lockres->l_name, lockres->l_flags, lockres->l_action,
  1820. lockres->l_unlock_action);
  1821. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1822. /* XXX: Today we just wait on any busy
  1823. * locks... Perhaps we need to cancel converts in the
  1824. * future? */
  1825. ocfs2_wait_on_busy_lock(lockres);
  1826. spin_lock_irqsave(&lockres->l_lock, flags);
  1827. }
  1828. if (dcb)
  1829. dcb->drop_func(lockres, dcb->drop_data);
  1830. if (lockres->l_flags & OCFS2_LOCK_BUSY)
  1831. mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
  1832. lockres->l_name);
  1833. if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
  1834. mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
  1835. if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
  1836. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1837. goto out;
  1838. }
  1839. lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
  1840. /* make sure we never get here while waiting for an ast to
  1841. * fire. */
  1842. BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
  1843. /* is this necessary? */
  1844. lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
  1845. lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
  1846. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1847. mlog(0, "lock %s\n", lockres->l_name);
  1848. status = dlmunlock(osb->dlm, &lockres->l_lksb, LKM_VALBLK,
  1849. lockres->l_ops->unlock_ast, lockres);
  1850. if (status != DLM_NORMAL) {
  1851. ocfs2_log_dlm_error("dlmunlock", status, lockres);
  1852. mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
  1853. dlm_print_one_lock(lockres->l_lksb.lockid);
  1854. BUG();
  1855. }
  1856. mlog(0, "lock %s, successfull return from dlmunlock\n",
  1857. lockres->l_name);
  1858. ocfs2_wait_on_busy_lock(lockres);
  1859. out:
  1860. mlog_exit(0);
  1861. return 0;
  1862. }
  1863. /* Mark the lockres as being dropped. It will no longer be
  1864. * queued if blocking, but we still may have to wait on it
  1865. * being dequeued from the vote thread before we can consider
  1866. * it safe to drop.
  1867. *
  1868. * You can *not* attempt to call cluster_lock on this lockres anymore. */
  1869. void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres)
  1870. {
  1871. int status;
  1872. struct ocfs2_mask_waiter mw;
  1873. unsigned long flags;
  1874. ocfs2_init_mask_waiter(&mw);
  1875. spin_lock_irqsave(&lockres->l_lock, flags);
  1876. lockres->l_flags |= OCFS2_LOCK_FREEING;
  1877. while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
  1878. lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
  1879. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1880. mlog(0, "Waiting on lockres %s\n", lockres->l_name);
  1881. status = ocfs2_wait_for_mask(&mw);
  1882. if (status)
  1883. mlog_errno(status);
  1884. spin_lock_irqsave(&lockres->l_lock, flags);
  1885. }
  1886. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1887. }
  1888. static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
  1889. {
  1890. int status;
  1891. mlog_entry_void();
  1892. ocfs2_mark_lockres_freeing(&osb->osb_super_lockres);
  1893. status = ocfs2_drop_lock(osb, &osb->osb_super_lockres, NULL);
  1894. if (status < 0)
  1895. mlog_errno(status);
  1896. ocfs2_mark_lockres_freeing(&osb->osb_rename_lockres);
  1897. status = ocfs2_drop_lock(osb, &osb->osb_rename_lockres, NULL);
  1898. if (status < 0)
  1899. mlog_errno(status);
  1900. mlog_exit(status);
  1901. }
  1902. static void ocfs2_meta_pre_drop(struct ocfs2_lock_res *lockres, void *data)
  1903. {
  1904. struct inode *inode = data;
  1905. /* the metadata lock requires a bit more work as we have an
  1906. * LVB to worry about. */
  1907. if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
  1908. lockres->l_level == LKM_EXMODE &&
  1909. !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
  1910. __ocfs2_stuff_meta_lvb(inode);
  1911. }
  1912. int ocfs2_drop_inode_locks(struct inode *inode)
  1913. {
  1914. int status, err;
  1915. struct drop_lock_cb meta_dcb = { ocfs2_meta_pre_drop, inode, };
  1916. mlog_entry_void();
  1917. /* No need to call ocfs2_mark_lockres_freeing here -
  1918. * ocfs2_clear_inode has done it for us. */
  1919. err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
  1920. &OCFS2_I(inode)->ip_data_lockres,
  1921. NULL);
  1922. if (err < 0)
  1923. mlog_errno(err);
  1924. status = err;
  1925. err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
  1926. &OCFS2_I(inode)->ip_meta_lockres,
  1927. &meta_dcb);
  1928. if (err < 0)
  1929. mlog_errno(err);
  1930. if (err < 0 && !status)
  1931. status = err;
  1932. err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
  1933. &OCFS2_I(inode)->ip_rw_lockres,
  1934. NULL);
  1935. if (err < 0)
  1936. mlog_errno(err);
  1937. if (err < 0 && !status)
  1938. status = err;
  1939. mlog_exit(status);
  1940. return status;
  1941. }
  1942. static void ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
  1943. int new_level)
  1944. {
  1945. assert_spin_locked(&lockres->l_lock);
  1946. BUG_ON(lockres->l_blocking <= LKM_NLMODE);
  1947. if (lockres->l_level <= new_level) {
  1948. mlog(ML_ERROR, "lockres->l_level (%u) <= new_level (%u)\n",
  1949. lockres->l_level, new_level);
  1950. BUG();
  1951. }
  1952. mlog(0, "lock %s, new_level = %d, l_blocking = %d\n",
  1953. lockres->l_name, new_level, lockres->l_blocking);
  1954. lockres->l_action = OCFS2_AST_DOWNCONVERT;
  1955. lockres->l_requested = new_level;
  1956. lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
  1957. }
  1958. static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
  1959. struct ocfs2_lock_res *lockres,
  1960. int new_level,
  1961. int lvb)
  1962. {
  1963. int ret, dlm_flags = LKM_CONVERT;
  1964. enum dlm_status status;
  1965. mlog_entry_void();
  1966. if (lvb)
  1967. dlm_flags |= LKM_VALBLK;
  1968. status = dlmlock(osb->dlm,
  1969. new_level,
  1970. &lockres->l_lksb,
  1971. dlm_flags,
  1972. lockres->l_name,
  1973. OCFS2_LOCK_ID_MAX_LEN - 1,
  1974. lockres->l_ops->ast,
  1975. lockres,
  1976. lockres->l_ops->bast);
  1977. if (status != DLM_NORMAL) {
  1978. ocfs2_log_dlm_error("dlmlock", status, lockres);
  1979. ret = -EINVAL;
  1980. ocfs2_recover_from_dlm_error(lockres, 1);
  1981. goto bail;
  1982. }
  1983. ret = 0;
  1984. bail:
  1985. mlog_exit(ret);
  1986. return ret;
  1987. }
  1988. /* returns 1 when the caller should unlock and call dlmunlock */
  1989. static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
  1990. struct ocfs2_lock_res *lockres)
  1991. {
  1992. assert_spin_locked(&lockres->l_lock);
  1993. mlog_entry_void();
  1994. mlog(0, "lock %s\n", lockres->l_name);
  1995. if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
  1996. /* If we're already trying to cancel a lock conversion
  1997. * then just drop the spinlock and allow the caller to
  1998. * requeue this lock. */
  1999. mlog(0, "Lockres %s, skip convert\n", lockres->l_name);
  2000. return 0;
  2001. }
  2002. /* were we in a convert when we got the bast fire? */
  2003. BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
  2004. lockres->l_action != OCFS2_AST_DOWNCONVERT);
  2005. /* set things up for the unlockast to know to just
  2006. * clear out the ast_action and unset busy, etc. */
  2007. lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
  2008. mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
  2009. "lock %s, invalid flags: 0x%lx\n",
  2010. lockres->l_name, lockres->l_flags);
  2011. return 1;
  2012. }
  2013. static int ocfs2_cancel_convert(struct ocfs2_super *osb,
  2014. struct ocfs2_lock_res *lockres)
  2015. {
  2016. int ret;
  2017. enum dlm_status status;
  2018. mlog_entry_void();
  2019. mlog(0, "lock %s\n", lockres->l_name);
  2020. ret = 0;
  2021. status = dlmunlock(osb->dlm,
  2022. &lockres->l_lksb,
  2023. LKM_CANCEL,
  2024. lockres->l_ops->unlock_ast,
  2025. lockres);
  2026. if (status != DLM_NORMAL) {
  2027. ocfs2_log_dlm_error("dlmunlock", status, lockres);
  2028. ret = -EINVAL;
  2029. ocfs2_recover_from_dlm_error(lockres, 0);
  2030. }
  2031. mlog(0, "lock %s return from dlmunlock\n", lockres->l_name);
  2032. mlog_exit(ret);
  2033. return ret;
  2034. }
  2035. static inline int ocfs2_can_downconvert_meta_lock(struct inode *inode,
  2036. struct ocfs2_lock_res *lockres,
  2037. int new_level)
  2038. {
  2039. int ret;
  2040. mlog_entry_void();
  2041. BUG_ON(new_level != LKM_NLMODE && new_level != LKM_PRMODE);
  2042. if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
  2043. ret = 0;
  2044. mlog(0, "lockres %s currently being refreshed -- backing "
  2045. "off!\n", lockres->l_name);
  2046. } else if (new_level == LKM_PRMODE)
  2047. ret = !lockres->l_ex_holders &&
  2048. ocfs2_inode_fully_checkpointed(inode);
  2049. else /* Must be NLMODE we're converting to. */
  2050. ret = !lockres->l_ro_holders && !lockres->l_ex_holders &&
  2051. ocfs2_inode_fully_checkpointed(inode);
  2052. mlog_exit(ret);
  2053. return ret;
  2054. }
  2055. static int ocfs2_do_unblock_meta(struct inode *inode,
  2056. int *requeue)
  2057. {
  2058. int new_level;
  2059. int set_lvb = 0;
  2060. int ret = 0;
  2061. struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_meta_lockres;
  2062. unsigned long flags;
  2063. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  2064. mlog_entry_void();
  2065. spin_lock_irqsave(&lockres->l_lock, flags);
  2066. BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
  2067. mlog(0, "l_level=%d, l_blocking=%d\n", lockres->l_level,
  2068. lockres->l_blocking);
  2069. BUG_ON(lockres->l_level != LKM_EXMODE &&
  2070. lockres->l_level != LKM_PRMODE);
  2071. if (lockres->l_flags & OCFS2_LOCK_BUSY) {
  2072. *requeue = 1;
  2073. ret = ocfs2_prepare_cancel_convert(osb, lockres);
  2074. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2075. if (ret) {
  2076. ret = ocfs2_cancel_convert(osb, lockres);
  2077. if (ret < 0)
  2078. mlog_errno(ret);
  2079. }
  2080. goto leave;
  2081. }
  2082. new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
  2083. mlog(0, "l_level=%d, l_blocking=%d, new_level=%d\n",
  2084. lockres->l_level, lockres->l_blocking, new_level);
  2085. if (ocfs2_can_downconvert_meta_lock(inode, lockres, new_level)) {
  2086. if (lockres->l_level == LKM_EXMODE)
  2087. set_lvb = 1;
  2088. /* If the lock hasn't been refreshed yet (rare), then
  2089. * our memory inode values are old and we skip
  2090. * stuffing the lvb. There's no need to actually clear
  2091. * out the lvb here as it's value is still valid. */
  2092. if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
  2093. if (set_lvb)
  2094. __ocfs2_stuff_meta_lvb(inode);
  2095. } else
  2096. mlog(0, "lockres %s: downconverting stale lock!\n",
  2097. lockres->l_name);
  2098. mlog(0, "calling ocfs2_downconvert_lock with l_level=%d, "
  2099. "l_blocking=%d, new_level=%d\n",
  2100. lockres->l_level, lockres->l_blocking, new_level);
  2101. ocfs2_prepare_downconvert(lockres, new_level);
  2102. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2103. ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb);
  2104. goto leave;
  2105. }
  2106. if (!ocfs2_inode_fully_checkpointed(inode))
  2107. ocfs2_start_checkpoint(osb);
  2108. *requeue = 1;
  2109. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2110. ret = 0;
  2111. leave:
  2112. mlog_exit(ret);
  2113. return ret;
  2114. }
  2115. static int ocfs2_generic_unblock_lock(struct ocfs2_super *osb,
  2116. struct ocfs2_lock_res *lockres,
  2117. int *requeue,
  2118. ocfs2_convert_worker_t *worker)
  2119. {
  2120. unsigned long flags;
  2121. int blocking;
  2122. int new_level;
  2123. int ret = 0;
  2124. mlog_entry_void();
  2125. spin_lock_irqsave(&lockres->l_lock, flags);
  2126. BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
  2127. recheck:
  2128. if (lockres->l_flags & OCFS2_LOCK_BUSY) {
  2129. *requeue = 1;
  2130. ret = ocfs2_prepare_cancel_convert(osb, lockres);
  2131. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2132. if (ret) {
  2133. ret = ocfs2_cancel_convert(osb, lockres);
  2134. if (ret < 0)
  2135. mlog_errno(ret);
  2136. }
  2137. goto leave;
  2138. }
  2139. /* if we're blocking an exclusive and we have *any* holders,
  2140. * then requeue. */
  2141. if ((lockres->l_blocking == LKM_EXMODE)
  2142. && (lockres->l_ex_holders || lockres->l_ro_holders)) {
  2143. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2144. *requeue = 1;
  2145. ret = 0;
  2146. goto leave;
  2147. }
  2148. /* If it's a PR we're blocking, then only
  2149. * requeue if we've got any EX holders */
  2150. if (lockres->l_blocking == LKM_PRMODE &&
  2151. lockres->l_ex_holders) {
  2152. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2153. *requeue = 1;
  2154. ret = 0;
  2155. goto leave;
  2156. }
  2157. /* If we get here, then we know that there are no more
  2158. * incompatible holders (and anyone asking for an incompatible
  2159. * lock is blocked). We can now downconvert the lock */
  2160. if (!worker)
  2161. goto downconvert;
  2162. /* Some lockres types want to do a bit of work before
  2163. * downconverting a lock. Allow that here. The worker function
  2164. * may sleep, so we save off a copy of what we're blocking as
  2165. * it may change while we're not holding the spin lock. */
  2166. blocking = lockres->l_blocking;
  2167. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2168. worker(lockres, blocking);
  2169. spin_lock_irqsave(&lockres->l_lock, flags);
  2170. if (blocking != lockres->l_blocking) {
  2171. /* If this changed underneath us, then we can't drop
  2172. * it just yet. */
  2173. goto recheck;
  2174. }
  2175. downconvert:
  2176. *requeue = 0;
  2177. new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
  2178. ocfs2_prepare_downconvert(lockres, new_level);
  2179. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2180. ret = ocfs2_downconvert_lock(osb, lockres, new_level, 0);
  2181. leave:
  2182. mlog_exit(ret);
  2183. return ret;
  2184. }
  2185. static void ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
  2186. int blocking)
  2187. {
  2188. struct inode *inode;
  2189. struct address_space *mapping;
  2190. mlog_entry_void();
  2191. inode = ocfs2_lock_res_inode(lockres);
  2192. mapping = inode->i_mapping;
  2193. if (filemap_fdatawrite(mapping)) {
  2194. mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
  2195. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  2196. }
  2197. sync_mapping_buffers(mapping);
  2198. if (blocking == LKM_EXMODE) {
  2199. truncate_inode_pages(mapping, 0);
  2200. unmap_mapping_range(mapping, 0, 0, 0);
  2201. } else {
  2202. /* We only need to wait on the I/O if we're not also
  2203. * truncating pages because truncate_inode_pages waits
  2204. * for us above. We don't truncate pages if we're
  2205. * blocking anything < EXMODE because we want to keep
  2206. * them around in that case. */
  2207. filemap_fdatawait(mapping);
  2208. }
  2209. mlog_exit_void();
  2210. }
  2211. int ocfs2_unblock_data(struct ocfs2_lock_res *lockres,
  2212. int *requeue)
  2213. {
  2214. int status;
  2215. struct inode *inode;
  2216. struct ocfs2_super *osb;
  2217. mlog_entry_void();
  2218. inode = ocfs2_lock_res_inode(lockres);
  2219. osb = OCFS2_SB(inode->i_sb);
  2220. mlog(0, "unblock inode %llu\n",
  2221. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  2222. status = ocfs2_generic_unblock_lock(osb,
  2223. lockres,
  2224. requeue,
  2225. ocfs2_data_convert_worker);
  2226. if (status < 0)
  2227. mlog_errno(status);
  2228. mlog(0, "inode %llu, requeue = %d\n",
  2229. (unsigned long long)OCFS2_I(inode)->ip_blkno, *requeue);
  2230. mlog_exit(status);
  2231. return status;
  2232. }
  2233. static int ocfs2_unblock_inode_lock(struct ocfs2_lock_res *lockres,
  2234. int *requeue)
  2235. {
  2236. int status;
  2237. struct inode *inode;
  2238. mlog_entry_void();
  2239. mlog(0, "Unblock lockres %s\n", lockres->l_name);
  2240. inode = ocfs2_lock_res_inode(lockres);
  2241. status = ocfs2_generic_unblock_lock(OCFS2_SB(inode->i_sb),
  2242. lockres,
  2243. requeue,
  2244. NULL);
  2245. if (status < 0)
  2246. mlog_errno(status);
  2247. mlog_exit(status);
  2248. return status;
  2249. }
  2250. int ocfs2_unblock_meta(struct ocfs2_lock_res *lockres,
  2251. int *requeue)
  2252. {
  2253. int status;
  2254. struct inode *inode;
  2255. mlog_entry_void();
  2256. inode = ocfs2_lock_res_inode(lockres);
  2257. mlog(0, "unblock inode %llu\n",
  2258. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  2259. status = ocfs2_do_unblock_meta(inode, requeue);
  2260. if (status < 0)
  2261. mlog_errno(status);
  2262. mlog(0, "inode %llu, requeue = %d\n",
  2263. (unsigned long long)OCFS2_I(inode)->ip_blkno, *requeue);
  2264. mlog_exit(status);
  2265. return status;
  2266. }
  2267. /* Generic unblock function for any lockres whose private data is an
  2268. * ocfs2_super pointer. */
  2269. static int ocfs2_unblock_osb_lock(struct ocfs2_lock_res *lockres,
  2270. int *requeue)
  2271. {
  2272. int status;
  2273. struct ocfs2_super *osb;
  2274. mlog_entry_void();
  2275. mlog(0, "Unblock lockres %s\n", lockres->l_name);
  2276. osb = ocfs2_lock_res_super(lockres);
  2277. status = ocfs2_generic_unblock_lock(osb,
  2278. lockres,
  2279. requeue,
  2280. NULL);
  2281. if (status < 0)
  2282. mlog_errno(status);
  2283. mlog_exit(status);
  2284. return status;
  2285. }
  2286. void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
  2287. struct ocfs2_lock_res *lockres)
  2288. {
  2289. int status;
  2290. int requeue = 0;
  2291. unsigned long flags;
  2292. /* Our reference to the lockres in this function can be
  2293. * considered valid until we remove the OCFS2_LOCK_QUEUED
  2294. * flag. */
  2295. mlog_entry_void();
  2296. BUG_ON(!lockres);
  2297. BUG_ON(!lockres->l_ops);
  2298. BUG_ON(!lockres->l_ops->unblock);
  2299. mlog(0, "lockres %s blocked.\n", lockres->l_name);
  2300. /* Detect whether a lock has been marked as going away while
  2301. * the vote thread was processing other things. A lock can
  2302. * still be marked with OCFS2_LOCK_FREEING after this check,
  2303. * but short circuiting here will still save us some
  2304. * performance. */
  2305. spin_lock_irqsave(&lockres->l_lock, flags);
  2306. if (lockres->l_flags & OCFS2_LOCK_FREEING)
  2307. goto unqueue;
  2308. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2309. status = lockres->l_ops->unblock(lockres, &requeue);
  2310. if (status < 0)
  2311. mlog_errno(status);
  2312. spin_lock_irqsave(&lockres->l_lock, flags);
  2313. unqueue:
  2314. if (lockres->l_flags & OCFS2_LOCK_FREEING || !requeue) {
  2315. lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
  2316. } else
  2317. ocfs2_schedule_blocked_lock(osb, lockres);
  2318. mlog(0, "lockres %s, requeue = %s.\n", lockres->l_name,
  2319. requeue ? "yes" : "no");
  2320. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2321. mlog_exit_void();
  2322. }
  2323. static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
  2324. struct ocfs2_lock_res *lockres)
  2325. {
  2326. mlog_entry_void();
  2327. assert_spin_locked(&lockres->l_lock);
  2328. if (lockres->l_flags & OCFS2_LOCK_FREEING) {
  2329. /* Do not schedule a lock for downconvert when it's on
  2330. * the way to destruction - any nodes wanting access
  2331. * to the resource will get it soon. */
  2332. mlog(0, "Lockres %s won't be scheduled: flags 0x%lx\n",
  2333. lockres->l_name, lockres->l_flags);
  2334. return;
  2335. }
  2336. lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
  2337. spin_lock(&osb->vote_task_lock);
  2338. if (list_empty(&lockres->l_blocked_list)) {
  2339. list_add_tail(&lockres->l_blocked_list,
  2340. &osb->blocked_lock_list);
  2341. osb->blocked_lock_count++;
  2342. }
  2343. spin_unlock(&osb->vote_task_lock);
  2344. mlog_exit_void();
  2345. }
  2346. /* This aids in debugging situations where a bad LVB might be involved. */
  2347. void ocfs2_dump_meta_lvb_info(u64 level,
  2348. const char *function,
  2349. unsigned int line,
  2350. struct ocfs2_lock_res *lockres)
  2351. {
  2352. struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
  2353. mlog(level, "LVB information for %s (called from %s:%u):\n",
  2354. lockres->l_name, function, line);
  2355. mlog(level, "version: %u, clusters: %u\n",
  2356. be32_to_cpu(lvb->lvb_version), be32_to_cpu(lvb->lvb_iclusters));
  2357. mlog(level, "size: %llu, uid %u, gid %u, mode 0x%x\n",
  2358. (unsigned long long)be64_to_cpu(lvb->lvb_isize),
  2359. be32_to_cpu(lvb->lvb_iuid), be32_to_cpu(lvb->lvb_igid),
  2360. be16_to_cpu(lvb->lvb_imode));
  2361. mlog(level, "nlink %u, atime_packed 0x%llx, ctime_packed 0x%llx, "
  2362. "mtime_packed 0x%llx iattr 0x%x\n", be16_to_cpu(lvb->lvb_inlink),
  2363. (long long)be64_to_cpu(lvb->lvb_iatime_packed),
  2364. (long long)be64_to_cpu(lvb->lvb_ictime_packed),
  2365. (long long)be64_to_cpu(lvb->lvb_imtime_packed),
  2366. be32_to_cpu(lvb->lvb_iattr));
  2367. }