dlmglue.c 81 KB

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