dlmglue.c 82 KB

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