dlmglue.c 82 KB

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