dlmglue.c 81 KB

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