dlmglue.c 81 KB

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