dlmglue.c 82 KB

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