dlmglue.c 81 KB

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