dlmglue.c 90 KB

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