dlmglue.c 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * dlmglue.c
  5. *
  6. * Code which implements an OCFS2 specific interface to our DLM.
  7. *
  8. * Copyright (C) 2003, 2004 Oracle. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2 of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public
  21. * License along with this program; if not, write to the
  22. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. * Boston, MA 021110-1307, USA.
  24. */
  25. #include <linux/types.h>
  26. #include <linux/slab.h>
  27. #include <linux/highmem.h>
  28. #include <linux/mm.h>
  29. #include <linux/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_BLOCKED &&
  815. !ocfs2_may_continue_on_blocked_lock(lockres, level)) {
  816. /* is the lock is currently blocked on behalf of
  817. * another node */
  818. lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BLOCKED, 0);
  819. wait = 1;
  820. goto unlock;
  821. }
  822. if (level > lockres->l_level) {
  823. if (lockres->l_action != OCFS2_AST_INVALID)
  824. mlog(ML_ERROR, "lockres %s has action %u pending\n",
  825. lockres->l_name, lockres->l_action);
  826. if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
  827. lockres->l_action = OCFS2_AST_ATTACH;
  828. lkm_flags &= ~LKM_CONVERT;
  829. } else {
  830. lockres->l_action = OCFS2_AST_CONVERT;
  831. lkm_flags |= LKM_CONVERT;
  832. }
  833. lockres->l_requested = level;
  834. lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
  835. spin_unlock_irqrestore(&lockres->l_lock, flags);
  836. BUG_ON(level == LKM_IVMODE);
  837. BUG_ON(level == LKM_NLMODE);
  838. mlog(0, "lock %s, convert from %d to level = %d\n",
  839. lockres->l_name, lockres->l_level, level);
  840. /* call dlm_lock to upgrade lock now */
  841. status = dlmlock(osb->dlm,
  842. level,
  843. &lockres->l_lksb,
  844. lkm_flags,
  845. lockres->l_name,
  846. OCFS2_LOCK_ID_MAX_LEN - 1,
  847. ocfs2_locking_ast,
  848. lockres,
  849. ocfs2_blocking_ast);
  850. if (status != DLM_NORMAL) {
  851. if ((lkm_flags & LKM_NOQUEUE) &&
  852. (status == DLM_NOTQUEUED))
  853. ret = -EAGAIN;
  854. else {
  855. ocfs2_log_dlm_error("dlmlock", status,
  856. lockres);
  857. ret = -EINVAL;
  858. }
  859. ocfs2_recover_from_dlm_error(lockres, 1);
  860. goto out;
  861. }
  862. mlog(0, "lock %s, successfull return from dlmlock\n",
  863. lockres->l_name);
  864. /* At this point we've gone inside the dlm and need to
  865. * complete our work regardless. */
  866. catch_signals = 0;
  867. /* wait for busy to clear and carry on */
  868. goto again;
  869. }
  870. /* Ok, if we get here then we're good to go. */
  871. ocfs2_inc_holders(lockres, level);
  872. ret = 0;
  873. unlock:
  874. spin_unlock_irqrestore(&lockres->l_lock, flags);
  875. out:
  876. /*
  877. * This is helping work around a lock inversion between the page lock
  878. * and dlm locks. One path holds the page lock while calling aops
  879. * which block acquiring dlm locks. The voting thread holds dlm
  880. * locks while acquiring page locks while down converting data locks.
  881. * This block is helping an aop path notice the inversion and back
  882. * off to unlock its page lock before trying the dlm lock again.
  883. */
  884. if (wait && arg_flags & OCFS2_LOCK_NONBLOCK &&
  885. mw.mw_mask & (OCFS2_LOCK_BUSY|OCFS2_LOCK_BLOCKED)) {
  886. wait = 0;
  887. if (lockres_remove_mask_waiter(lockres, &mw))
  888. ret = -EAGAIN;
  889. else
  890. goto again;
  891. }
  892. if (wait) {
  893. ret = ocfs2_wait_for_mask(&mw);
  894. if (ret == 0)
  895. goto again;
  896. mlog_errno(ret);
  897. }
  898. mlog_exit(ret);
  899. return ret;
  900. }
  901. static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
  902. struct ocfs2_lock_res *lockres,
  903. int level)
  904. {
  905. unsigned long flags;
  906. mlog_entry_void();
  907. spin_lock_irqsave(&lockres->l_lock, flags);
  908. ocfs2_dec_holders(lockres, level);
  909. ocfs2_vote_on_unlock(osb, lockres);
  910. spin_unlock_irqrestore(&lockres->l_lock, flags);
  911. mlog_exit_void();
  912. }
  913. static int ocfs2_create_new_lock(struct ocfs2_super *osb,
  914. struct ocfs2_lock_res *lockres,
  915. int ex,
  916. int local)
  917. {
  918. int level = ex ? LKM_EXMODE : LKM_PRMODE;
  919. unsigned long flags;
  920. int lkm_flags = local ? LKM_LOCAL : 0;
  921. spin_lock_irqsave(&lockres->l_lock, flags);
  922. BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
  923. lockres_or_flags(lockres, OCFS2_LOCK_LOCAL);
  924. spin_unlock_irqrestore(&lockres->l_lock, flags);
  925. return ocfs2_lock_create(osb, lockres, level, lkm_flags);
  926. }
  927. /* Grants us an EX lock on the data and metadata resources, skipping
  928. * the normal cluster directory lookup. Use this ONLY on newly created
  929. * inodes which other nodes can't possibly see, and which haven't been
  930. * hashed in the inode hash yet. This can give us a good performance
  931. * increase as it'll skip the network broadcast normally associated
  932. * with creating a new lock resource. */
  933. int ocfs2_create_new_inode_locks(struct inode *inode)
  934. {
  935. int ret;
  936. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  937. BUG_ON(!inode);
  938. BUG_ON(!ocfs2_inode_is_new(inode));
  939. mlog_entry_void();
  940. mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
  941. /* NOTE: That we don't increment any of the holder counts, nor
  942. * do we add anything to a journal handle. Since this is
  943. * supposed to be a new inode which the cluster doesn't know
  944. * about yet, there is no need to. As far as the LVB handling
  945. * is concerned, this is basically like acquiring an EX lock
  946. * on a resource which has an invalid one -- we'll set it
  947. * valid when we release the EX. */
  948. ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_rw_lockres, 1, 1);
  949. if (ret) {
  950. mlog_errno(ret);
  951. goto bail;
  952. }
  953. /*
  954. * We don't want to use LKM_LOCAL on a meta data lock as they
  955. * don't use a generation in their lock names.
  956. */
  957. ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_meta_lockres, 1, 0);
  958. if (ret) {
  959. mlog_errno(ret);
  960. goto bail;
  961. }
  962. ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_data_lockres, 1, 1);
  963. if (ret) {
  964. mlog_errno(ret);
  965. goto bail;
  966. }
  967. ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_open_lockres, 0, 0);
  968. if (ret) {
  969. mlog_errno(ret);
  970. goto bail;
  971. }
  972. bail:
  973. mlog_exit(ret);
  974. return ret;
  975. }
  976. int ocfs2_rw_lock(struct inode *inode, int write)
  977. {
  978. int status, level;
  979. struct ocfs2_lock_res *lockres;
  980. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  981. BUG_ON(!inode);
  982. mlog_entry_void();
  983. mlog(0, "inode %llu take %s RW lock\n",
  984. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  985. write ? "EXMODE" : "PRMODE");
  986. if (ocfs2_mount_local(osb))
  987. return 0;
  988. lockres = &OCFS2_I(inode)->ip_rw_lockres;
  989. level = write ? LKM_EXMODE : LKM_PRMODE;
  990. status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level, 0,
  991. 0);
  992. if (status < 0)
  993. mlog_errno(status);
  994. mlog_exit(status);
  995. return status;
  996. }
  997. void ocfs2_rw_unlock(struct inode *inode, int write)
  998. {
  999. int level = write ? LKM_EXMODE : LKM_PRMODE;
  1000. struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_rw_lockres;
  1001. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1002. mlog_entry_void();
  1003. mlog(0, "inode %llu drop %s RW lock\n",
  1004. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1005. write ? "EXMODE" : "PRMODE");
  1006. if (!ocfs2_mount_local(osb))
  1007. ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
  1008. mlog_exit_void();
  1009. }
  1010. /*
  1011. * ocfs2_open_lock always get PR mode lock.
  1012. */
  1013. int ocfs2_open_lock(struct inode *inode)
  1014. {
  1015. int status = 0;
  1016. struct ocfs2_lock_res *lockres;
  1017. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1018. BUG_ON(!inode);
  1019. mlog_entry_void();
  1020. mlog(0, "inode %llu take PRMODE open lock\n",
  1021. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  1022. if (ocfs2_mount_local(osb))
  1023. goto out;
  1024. lockres = &OCFS2_I(inode)->ip_open_lockres;
  1025. status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
  1026. LKM_PRMODE, 0, 0);
  1027. if (status < 0)
  1028. mlog_errno(status);
  1029. out:
  1030. mlog_exit(status);
  1031. return status;
  1032. }
  1033. int ocfs2_try_open_lock(struct inode *inode, int write)
  1034. {
  1035. int status = 0, level;
  1036. struct ocfs2_lock_res *lockres;
  1037. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1038. BUG_ON(!inode);
  1039. mlog_entry_void();
  1040. mlog(0, "inode %llu try to take %s open lock\n",
  1041. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1042. write ? "EXMODE" : "PRMODE");
  1043. if (ocfs2_mount_local(osb))
  1044. goto out;
  1045. lockres = &OCFS2_I(inode)->ip_open_lockres;
  1046. level = write ? LKM_EXMODE : LKM_PRMODE;
  1047. /*
  1048. * The file system may already holding a PRMODE/EXMODE open lock.
  1049. * Since we pass LKM_NOQUEUE, the request won't block waiting on
  1050. * other nodes and the -EAGAIN will indicate to the caller that
  1051. * this inode is still in use.
  1052. */
  1053. status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
  1054. level, LKM_NOQUEUE, 0);
  1055. out:
  1056. mlog_exit(status);
  1057. return status;
  1058. }
  1059. /*
  1060. * ocfs2_open_unlock unlock PR and EX mode open locks.
  1061. */
  1062. void ocfs2_open_unlock(struct inode *inode)
  1063. {
  1064. struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_open_lockres;
  1065. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1066. mlog_entry_void();
  1067. mlog(0, "inode %llu drop open lock\n",
  1068. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  1069. if (ocfs2_mount_local(osb))
  1070. goto out;
  1071. if(lockres->l_ro_holders)
  1072. ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
  1073. LKM_PRMODE);
  1074. if(lockres->l_ex_holders)
  1075. ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
  1076. LKM_EXMODE);
  1077. out:
  1078. mlog_exit_void();
  1079. }
  1080. int ocfs2_data_lock_full(struct inode *inode,
  1081. int write,
  1082. int arg_flags)
  1083. {
  1084. int status = 0, level;
  1085. struct ocfs2_lock_res *lockres;
  1086. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1087. BUG_ON(!inode);
  1088. mlog_entry_void();
  1089. mlog(0, "inode %llu take %s DATA lock\n",
  1090. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1091. write ? "EXMODE" : "PRMODE");
  1092. /* We'll allow faking a readonly data lock for
  1093. * rodevices. */
  1094. if (ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb))) {
  1095. if (write) {
  1096. status = -EROFS;
  1097. mlog_errno(status);
  1098. }
  1099. goto out;
  1100. }
  1101. if (ocfs2_mount_local(osb))
  1102. goto out;
  1103. lockres = &OCFS2_I(inode)->ip_data_lockres;
  1104. level = write ? LKM_EXMODE : LKM_PRMODE;
  1105. status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level,
  1106. 0, arg_flags);
  1107. if (status < 0 && status != -EAGAIN)
  1108. mlog_errno(status);
  1109. out:
  1110. mlog_exit(status);
  1111. return status;
  1112. }
  1113. /* see ocfs2_meta_lock_with_page() */
  1114. int ocfs2_data_lock_with_page(struct inode *inode,
  1115. int write,
  1116. struct page *page)
  1117. {
  1118. int ret;
  1119. ret = ocfs2_data_lock_full(inode, write, OCFS2_LOCK_NONBLOCK);
  1120. if (ret == -EAGAIN) {
  1121. unlock_page(page);
  1122. if (ocfs2_data_lock(inode, write) == 0)
  1123. ocfs2_data_unlock(inode, write);
  1124. ret = AOP_TRUNCATED_PAGE;
  1125. }
  1126. return ret;
  1127. }
  1128. static void ocfs2_vote_on_unlock(struct ocfs2_super *osb,
  1129. struct ocfs2_lock_res *lockres)
  1130. {
  1131. int kick = 0;
  1132. mlog_entry_void();
  1133. /* If we know that another node is waiting on our lock, kick
  1134. * the vote thread * pre-emptively when we reach a release
  1135. * condition. */
  1136. if (lockres->l_flags & OCFS2_LOCK_BLOCKED) {
  1137. switch(lockres->l_blocking) {
  1138. case LKM_EXMODE:
  1139. if (!lockres->l_ex_holders && !lockres->l_ro_holders)
  1140. kick = 1;
  1141. break;
  1142. case LKM_PRMODE:
  1143. if (!lockres->l_ex_holders)
  1144. kick = 1;
  1145. break;
  1146. default:
  1147. BUG();
  1148. }
  1149. }
  1150. if (kick)
  1151. ocfs2_kick_vote_thread(osb);
  1152. mlog_exit_void();
  1153. }
  1154. void ocfs2_data_unlock(struct inode *inode,
  1155. int write)
  1156. {
  1157. int level = write ? LKM_EXMODE : LKM_PRMODE;
  1158. struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_data_lockres;
  1159. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1160. mlog_entry_void();
  1161. mlog(0, "inode %llu drop %s DATA lock\n",
  1162. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1163. write ? "EXMODE" : "PRMODE");
  1164. if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)) &&
  1165. !ocfs2_mount_local(osb))
  1166. ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
  1167. mlog_exit_void();
  1168. }
  1169. #define OCFS2_SEC_BITS 34
  1170. #define OCFS2_SEC_SHIFT (64 - 34)
  1171. #define OCFS2_NSEC_MASK ((1ULL << OCFS2_SEC_SHIFT) - 1)
  1172. /* LVB only has room for 64 bits of time here so we pack it for
  1173. * now. */
  1174. static u64 ocfs2_pack_timespec(struct timespec *spec)
  1175. {
  1176. u64 res;
  1177. u64 sec = spec->tv_sec;
  1178. u32 nsec = spec->tv_nsec;
  1179. res = (sec << OCFS2_SEC_SHIFT) | (nsec & OCFS2_NSEC_MASK);
  1180. return res;
  1181. }
  1182. /* Call this with the lockres locked. I am reasonably sure we don't
  1183. * need ip_lock in this function as anyone who would be changing those
  1184. * values is supposed to be blocked in ocfs2_meta_lock right now. */
  1185. static void __ocfs2_stuff_meta_lvb(struct inode *inode)
  1186. {
  1187. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1188. struct ocfs2_lock_res *lockres = &oi->ip_meta_lockres;
  1189. struct ocfs2_meta_lvb *lvb;
  1190. mlog_entry_void();
  1191. lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
  1192. /*
  1193. * Invalidate the LVB of a deleted inode - this way other
  1194. * nodes are forced to go to disk and discover the new inode
  1195. * status.
  1196. */
  1197. if (oi->ip_flags & OCFS2_INODE_DELETED) {
  1198. lvb->lvb_version = 0;
  1199. goto out;
  1200. }
  1201. lvb->lvb_version = OCFS2_LVB_VERSION;
  1202. lvb->lvb_isize = cpu_to_be64(i_size_read(inode));
  1203. lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters);
  1204. lvb->lvb_iuid = cpu_to_be32(inode->i_uid);
  1205. lvb->lvb_igid = cpu_to_be32(inode->i_gid);
  1206. lvb->lvb_imode = cpu_to_be16(inode->i_mode);
  1207. lvb->lvb_inlink = cpu_to_be16(inode->i_nlink);
  1208. lvb->lvb_iatime_packed =
  1209. cpu_to_be64(ocfs2_pack_timespec(&inode->i_atime));
  1210. lvb->lvb_ictime_packed =
  1211. cpu_to_be64(ocfs2_pack_timespec(&inode->i_ctime));
  1212. lvb->lvb_imtime_packed =
  1213. cpu_to_be64(ocfs2_pack_timespec(&inode->i_mtime));
  1214. lvb->lvb_iattr = cpu_to_be32(oi->ip_attr);
  1215. lvb->lvb_idynfeatures = cpu_to_be16(oi->ip_dyn_features);
  1216. lvb->lvb_igeneration = cpu_to_be32(inode->i_generation);
  1217. out:
  1218. mlog_meta_lvb(0, lockres);
  1219. mlog_exit_void();
  1220. }
  1221. static void ocfs2_unpack_timespec(struct timespec *spec,
  1222. u64 packed_time)
  1223. {
  1224. spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
  1225. spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
  1226. }
  1227. static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
  1228. {
  1229. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1230. struct ocfs2_lock_res *lockres = &oi->ip_meta_lockres;
  1231. struct ocfs2_meta_lvb *lvb;
  1232. mlog_entry_void();
  1233. mlog_meta_lvb(0, lockres);
  1234. lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
  1235. /* We're safe here without the lockres lock... */
  1236. spin_lock(&oi->ip_lock);
  1237. oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters);
  1238. i_size_write(inode, be64_to_cpu(lvb->lvb_isize));
  1239. oi->ip_attr = be32_to_cpu(lvb->lvb_iattr);
  1240. oi->ip_dyn_features = be16_to_cpu(lvb->lvb_idynfeatures);
  1241. ocfs2_set_inode_flags(inode);
  1242. /* fast-symlinks are a special case */
  1243. if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
  1244. inode->i_blocks = 0;
  1245. else
  1246. inode->i_blocks = ocfs2_inode_sector_count(inode);
  1247. inode->i_uid = be32_to_cpu(lvb->lvb_iuid);
  1248. inode->i_gid = be32_to_cpu(lvb->lvb_igid);
  1249. inode->i_mode = be16_to_cpu(lvb->lvb_imode);
  1250. inode->i_nlink = be16_to_cpu(lvb->lvb_inlink);
  1251. ocfs2_unpack_timespec(&inode->i_atime,
  1252. be64_to_cpu(lvb->lvb_iatime_packed));
  1253. ocfs2_unpack_timespec(&inode->i_mtime,
  1254. be64_to_cpu(lvb->lvb_imtime_packed));
  1255. ocfs2_unpack_timespec(&inode->i_ctime,
  1256. be64_to_cpu(lvb->lvb_ictime_packed));
  1257. spin_unlock(&oi->ip_lock);
  1258. mlog_exit_void();
  1259. }
  1260. static inline int ocfs2_meta_lvb_is_trustable(struct inode *inode,
  1261. struct ocfs2_lock_res *lockres)
  1262. {
  1263. struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
  1264. if (lvb->lvb_version == OCFS2_LVB_VERSION
  1265. && be32_to_cpu(lvb->lvb_igeneration) == inode->i_generation)
  1266. return 1;
  1267. return 0;
  1268. }
  1269. /* Determine whether a lock resource needs to be refreshed, and
  1270. * arbitrate who gets to refresh it.
  1271. *
  1272. * 0 means no refresh needed.
  1273. *
  1274. * > 0 means you need to refresh this and you MUST call
  1275. * ocfs2_complete_lock_res_refresh afterwards. */
  1276. static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
  1277. {
  1278. unsigned long flags;
  1279. int status = 0;
  1280. mlog_entry_void();
  1281. refresh_check:
  1282. spin_lock_irqsave(&lockres->l_lock, flags);
  1283. if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
  1284. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1285. goto bail;
  1286. }
  1287. if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
  1288. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1289. ocfs2_wait_on_refreshing_lock(lockres);
  1290. goto refresh_check;
  1291. }
  1292. /* Ok, I'll be the one to refresh this lock. */
  1293. lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
  1294. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1295. status = 1;
  1296. bail:
  1297. mlog_exit(status);
  1298. return status;
  1299. }
  1300. /* If status is non zero, I'll mark it as not being in refresh
  1301. * anymroe, but i won't clear the needs refresh flag. */
  1302. static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
  1303. int status)
  1304. {
  1305. unsigned long flags;
  1306. mlog_entry_void();
  1307. spin_lock_irqsave(&lockres->l_lock, flags);
  1308. lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
  1309. if (!status)
  1310. lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
  1311. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1312. wake_up(&lockres->l_event);
  1313. mlog_exit_void();
  1314. }
  1315. /* may or may not return a bh if it went to disk. */
  1316. static int ocfs2_meta_lock_update(struct inode *inode,
  1317. struct buffer_head **bh)
  1318. {
  1319. int status = 0;
  1320. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1321. struct ocfs2_lock_res *lockres = &oi->ip_meta_lockres;
  1322. struct ocfs2_dinode *fe;
  1323. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1324. mlog_entry_void();
  1325. if (ocfs2_mount_local(osb))
  1326. goto bail;
  1327. spin_lock(&oi->ip_lock);
  1328. if (oi->ip_flags & OCFS2_INODE_DELETED) {
  1329. mlog(0, "Orphaned inode %llu was deleted while we "
  1330. "were waiting on a lock. ip_flags = 0x%x\n",
  1331. (unsigned long long)oi->ip_blkno, oi->ip_flags);
  1332. spin_unlock(&oi->ip_lock);
  1333. status = -ENOENT;
  1334. goto bail;
  1335. }
  1336. spin_unlock(&oi->ip_lock);
  1337. if (!ocfs2_should_refresh_lock_res(lockres))
  1338. goto bail;
  1339. /* This will discard any caching information we might have had
  1340. * for the inode metadata. */
  1341. ocfs2_metadata_cache_purge(inode);
  1342. ocfs2_extent_map_trunc(inode, 0);
  1343. if (ocfs2_meta_lvb_is_trustable(inode, lockres)) {
  1344. mlog(0, "Trusting LVB on inode %llu\n",
  1345. (unsigned long long)oi->ip_blkno);
  1346. ocfs2_refresh_inode_from_lvb(inode);
  1347. } else {
  1348. /* Boo, we have to go to disk. */
  1349. /* read bh, cast, ocfs2_refresh_inode */
  1350. status = ocfs2_read_block(OCFS2_SB(inode->i_sb), oi->ip_blkno,
  1351. bh, OCFS2_BH_CACHED, inode);
  1352. if (status < 0) {
  1353. mlog_errno(status);
  1354. goto bail_refresh;
  1355. }
  1356. fe = (struct ocfs2_dinode *) (*bh)->b_data;
  1357. /* This is a good chance to make sure we're not
  1358. * locking an invalid object.
  1359. *
  1360. * We bug on a stale inode here because we checked
  1361. * above whether it was wiped from disk. The wiping
  1362. * node provides a guarantee that we receive that
  1363. * message and can mark the inode before dropping any
  1364. * locks associated with it. */
  1365. if (!OCFS2_IS_VALID_DINODE(fe)) {
  1366. OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
  1367. status = -EIO;
  1368. goto bail_refresh;
  1369. }
  1370. mlog_bug_on_msg(inode->i_generation !=
  1371. le32_to_cpu(fe->i_generation),
  1372. "Invalid dinode %llu disk generation: %u "
  1373. "inode->i_generation: %u\n",
  1374. (unsigned long long)oi->ip_blkno,
  1375. le32_to_cpu(fe->i_generation),
  1376. inode->i_generation);
  1377. mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
  1378. !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
  1379. "Stale dinode %llu dtime: %llu flags: 0x%x\n",
  1380. (unsigned long long)oi->ip_blkno,
  1381. (unsigned long long)le64_to_cpu(fe->i_dtime),
  1382. le32_to_cpu(fe->i_flags));
  1383. ocfs2_refresh_inode(inode, fe);
  1384. }
  1385. status = 0;
  1386. bail_refresh:
  1387. ocfs2_complete_lock_res_refresh(lockres, status);
  1388. bail:
  1389. mlog_exit(status);
  1390. return status;
  1391. }
  1392. static int ocfs2_assign_bh(struct inode *inode,
  1393. struct buffer_head **ret_bh,
  1394. struct buffer_head *passed_bh)
  1395. {
  1396. int status;
  1397. if (passed_bh) {
  1398. /* Ok, the update went to disk for us, use the
  1399. * returned bh. */
  1400. *ret_bh = passed_bh;
  1401. get_bh(*ret_bh);
  1402. return 0;
  1403. }
  1404. status = ocfs2_read_block(OCFS2_SB(inode->i_sb),
  1405. OCFS2_I(inode)->ip_blkno,
  1406. ret_bh,
  1407. OCFS2_BH_CACHED,
  1408. inode);
  1409. if (status < 0)
  1410. mlog_errno(status);
  1411. return status;
  1412. }
  1413. /*
  1414. * returns < 0 error if the callback will never be called, otherwise
  1415. * the result of the lock will be communicated via the callback.
  1416. */
  1417. int ocfs2_meta_lock_full(struct inode *inode,
  1418. struct buffer_head **ret_bh,
  1419. int ex,
  1420. int arg_flags)
  1421. {
  1422. int status, level, dlm_flags, acquired;
  1423. struct ocfs2_lock_res *lockres = NULL;
  1424. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1425. struct buffer_head *local_bh = NULL;
  1426. BUG_ON(!inode);
  1427. mlog_entry_void();
  1428. mlog(0, "inode %llu, take %s META lock\n",
  1429. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1430. ex ? "EXMODE" : "PRMODE");
  1431. status = 0;
  1432. acquired = 0;
  1433. /* We'll allow faking a readonly metadata lock for
  1434. * rodevices. */
  1435. if (ocfs2_is_hard_readonly(osb)) {
  1436. if (ex)
  1437. status = -EROFS;
  1438. goto bail;
  1439. }
  1440. if (ocfs2_mount_local(osb))
  1441. goto local;
  1442. if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
  1443. wait_event(osb->recovery_event,
  1444. ocfs2_node_map_is_empty(osb, &osb->recovery_map));
  1445. lockres = &OCFS2_I(inode)->ip_meta_lockres;
  1446. level = ex ? LKM_EXMODE : LKM_PRMODE;
  1447. dlm_flags = 0;
  1448. if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
  1449. dlm_flags |= LKM_NOQUEUE;
  1450. status = ocfs2_cluster_lock(osb, lockres, level, dlm_flags, arg_flags);
  1451. if (status < 0) {
  1452. if (status != -EAGAIN && status != -EIOCBRETRY)
  1453. mlog_errno(status);
  1454. goto bail;
  1455. }
  1456. /* Notify the error cleanup path to drop the cluster lock. */
  1457. acquired = 1;
  1458. /* We wait twice because a node may have died while we were in
  1459. * the lower dlm layers. The second time though, we've
  1460. * committed to owning this lock so we don't allow signals to
  1461. * abort the operation. */
  1462. if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
  1463. wait_event(osb->recovery_event,
  1464. ocfs2_node_map_is_empty(osb, &osb->recovery_map));
  1465. local:
  1466. /*
  1467. * We only see this flag if we're being called from
  1468. * ocfs2_read_locked_inode(). It means we're locking an inode
  1469. * which hasn't been populated yet, so clear the refresh flag
  1470. * and let the caller handle it.
  1471. */
  1472. if (inode->i_state & I_NEW) {
  1473. status = 0;
  1474. if (lockres)
  1475. ocfs2_complete_lock_res_refresh(lockres, 0);
  1476. goto bail;
  1477. }
  1478. /* This is fun. The caller may want a bh back, or it may
  1479. * not. ocfs2_meta_lock_update definitely wants one in, but
  1480. * may or may not read one, depending on what's in the
  1481. * LVB. The result of all of this is that we've *only* gone to
  1482. * disk if we have to, so the complexity is worthwhile. */
  1483. status = ocfs2_meta_lock_update(inode, &local_bh);
  1484. if (status < 0) {
  1485. if (status != -ENOENT)
  1486. mlog_errno(status);
  1487. goto bail;
  1488. }
  1489. if (ret_bh) {
  1490. status = ocfs2_assign_bh(inode, ret_bh, local_bh);
  1491. if (status < 0) {
  1492. mlog_errno(status);
  1493. goto bail;
  1494. }
  1495. }
  1496. bail:
  1497. if (status < 0) {
  1498. if (ret_bh && (*ret_bh)) {
  1499. brelse(*ret_bh);
  1500. *ret_bh = NULL;
  1501. }
  1502. if (acquired)
  1503. ocfs2_meta_unlock(inode, ex);
  1504. }
  1505. if (local_bh)
  1506. brelse(local_bh);
  1507. mlog_exit(status);
  1508. return status;
  1509. }
  1510. /*
  1511. * This is working around a lock inversion between tasks acquiring DLM locks
  1512. * while holding a page lock and the vote thread which blocks dlm lock acquiry
  1513. * while acquiring page locks.
  1514. *
  1515. * ** These _with_page variantes are only intended to be called from aop
  1516. * methods that hold page locks and return a very specific *positive* error
  1517. * code that aop methods pass up to the VFS -- test for errors with != 0. **
  1518. *
  1519. * The DLM is called such that it returns -EAGAIN if it would have blocked
  1520. * waiting for the vote thread. In that case we unlock our page so the vote
  1521. * thread can make progress. Once we've done this we have to return
  1522. * AOP_TRUNCATED_PAGE so the aop method that called us can bubble that back up
  1523. * into the VFS who will then immediately retry the aop call.
  1524. *
  1525. * We do a blocking lock and immediate unlock before returning, though, so that
  1526. * the lock has a great chance of being cached on this node by the time the VFS
  1527. * calls back to retry the aop. This has a potential to livelock as nodes
  1528. * ping locks back and forth, but that's a risk we're willing to take to avoid
  1529. * the lock inversion simply.
  1530. */
  1531. int ocfs2_meta_lock_with_page(struct inode *inode,
  1532. struct buffer_head **ret_bh,
  1533. int ex,
  1534. struct page *page)
  1535. {
  1536. int ret;
  1537. ret = ocfs2_meta_lock_full(inode, ret_bh, ex, OCFS2_LOCK_NONBLOCK);
  1538. if (ret == -EAGAIN) {
  1539. unlock_page(page);
  1540. if (ocfs2_meta_lock(inode, ret_bh, ex) == 0)
  1541. ocfs2_meta_unlock(inode, ex);
  1542. ret = AOP_TRUNCATED_PAGE;
  1543. }
  1544. return ret;
  1545. }
  1546. int ocfs2_meta_lock_atime(struct inode *inode,
  1547. struct vfsmount *vfsmnt,
  1548. int *level)
  1549. {
  1550. int ret;
  1551. mlog_entry_void();
  1552. ret = ocfs2_meta_lock(inode, NULL, 0);
  1553. if (ret < 0) {
  1554. mlog_errno(ret);
  1555. return ret;
  1556. }
  1557. /*
  1558. * If we should update atime, we will get EX lock,
  1559. * otherwise we just get PR lock.
  1560. */
  1561. if (ocfs2_should_update_atime(inode, vfsmnt)) {
  1562. struct buffer_head *bh = NULL;
  1563. ocfs2_meta_unlock(inode, 0);
  1564. ret = ocfs2_meta_lock(inode, &bh, 1);
  1565. if (ret < 0) {
  1566. mlog_errno(ret);
  1567. return ret;
  1568. }
  1569. *level = 1;
  1570. if (ocfs2_should_update_atime(inode, vfsmnt))
  1571. ocfs2_update_inode_atime(inode, bh);
  1572. if (bh)
  1573. brelse(bh);
  1574. } else
  1575. *level = 0;
  1576. mlog_exit(ret);
  1577. return ret;
  1578. }
  1579. void ocfs2_meta_unlock(struct inode *inode,
  1580. int ex)
  1581. {
  1582. int level = ex ? LKM_EXMODE : LKM_PRMODE;
  1583. struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_meta_lockres;
  1584. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1585. mlog_entry_void();
  1586. mlog(0, "inode %llu drop %s META lock\n",
  1587. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1588. ex ? "EXMODE" : "PRMODE");
  1589. if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)) &&
  1590. !ocfs2_mount_local(osb))
  1591. ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
  1592. mlog_exit_void();
  1593. }
  1594. int ocfs2_super_lock(struct ocfs2_super *osb,
  1595. int ex)
  1596. {
  1597. int status = 0;
  1598. int level = ex ? LKM_EXMODE : LKM_PRMODE;
  1599. struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
  1600. struct buffer_head *bh;
  1601. struct ocfs2_slot_info *si = osb->slot_info;
  1602. mlog_entry_void();
  1603. if (ocfs2_is_hard_readonly(osb))
  1604. return -EROFS;
  1605. if (ocfs2_mount_local(osb))
  1606. goto bail;
  1607. status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
  1608. if (status < 0) {
  1609. mlog_errno(status);
  1610. goto bail;
  1611. }
  1612. /* The super block lock path is really in the best position to
  1613. * know when resources covered by the lock need to be
  1614. * refreshed, so we do it here. Of course, making sense of
  1615. * everything is up to the caller :) */
  1616. status = ocfs2_should_refresh_lock_res(lockres);
  1617. if (status < 0) {
  1618. mlog_errno(status);
  1619. goto bail;
  1620. }
  1621. if (status) {
  1622. bh = si->si_bh;
  1623. status = ocfs2_read_block(osb, bh->b_blocknr, &bh, 0,
  1624. si->si_inode);
  1625. if (status == 0)
  1626. ocfs2_update_slot_info(si);
  1627. ocfs2_complete_lock_res_refresh(lockres, status);
  1628. if (status < 0)
  1629. mlog_errno(status);
  1630. }
  1631. bail:
  1632. mlog_exit(status);
  1633. return status;
  1634. }
  1635. void ocfs2_super_unlock(struct ocfs2_super *osb,
  1636. int ex)
  1637. {
  1638. int level = ex ? LKM_EXMODE : LKM_PRMODE;
  1639. struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
  1640. if (!ocfs2_mount_local(osb))
  1641. ocfs2_cluster_unlock(osb, lockres, level);
  1642. }
  1643. int ocfs2_rename_lock(struct ocfs2_super *osb)
  1644. {
  1645. int status;
  1646. struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
  1647. if (ocfs2_is_hard_readonly(osb))
  1648. return -EROFS;
  1649. if (ocfs2_mount_local(osb))
  1650. return 0;
  1651. status = ocfs2_cluster_lock(osb, lockres, LKM_EXMODE, 0, 0);
  1652. if (status < 0)
  1653. mlog_errno(status);
  1654. return status;
  1655. }
  1656. void ocfs2_rename_unlock(struct ocfs2_super *osb)
  1657. {
  1658. struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
  1659. if (!ocfs2_mount_local(osb))
  1660. ocfs2_cluster_unlock(osb, lockres, LKM_EXMODE);
  1661. }
  1662. int ocfs2_dentry_lock(struct dentry *dentry, int ex)
  1663. {
  1664. int ret;
  1665. int level = ex ? LKM_EXMODE : LKM_PRMODE;
  1666. struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
  1667. struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
  1668. BUG_ON(!dl);
  1669. if (ocfs2_is_hard_readonly(osb))
  1670. return -EROFS;
  1671. if (ocfs2_mount_local(osb))
  1672. return 0;
  1673. ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0);
  1674. if (ret < 0)
  1675. mlog_errno(ret);
  1676. return ret;
  1677. }
  1678. void ocfs2_dentry_unlock(struct dentry *dentry, int ex)
  1679. {
  1680. int level = ex ? LKM_EXMODE : LKM_PRMODE;
  1681. struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
  1682. struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
  1683. if (!ocfs2_mount_local(osb))
  1684. ocfs2_cluster_unlock(osb, &dl->dl_lockres, level);
  1685. }
  1686. /* Reference counting of the dlm debug structure. We want this because
  1687. * open references on the debug inodes can live on after a mount, so
  1688. * we can't rely on the ocfs2_super to always exist. */
  1689. static void ocfs2_dlm_debug_free(struct kref *kref)
  1690. {
  1691. struct ocfs2_dlm_debug *dlm_debug;
  1692. dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
  1693. kfree(dlm_debug);
  1694. }
  1695. void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
  1696. {
  1697. if (dlm_debug)
  1698. kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
  1699. }
  1700. static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
  1701. {
  1702. kref_get(&debug->d_refcnt);
  1703. }
  1704. struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
  1705. {
  1706. struct ocfs2_dlm_debug *dlm_debug;
  1707. dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
  1708. if (!dlm_debug) {
  1709. mlog_errno(-ENOMEM);
  1710. goto out;
  1711. }
  1712. kref_init(&dlm_debug->d_refcnt);
  1713. INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
  1714. dlm_debug->d_locking_state = NULL;
  1715. out:
  1716. return dlm_debug;
  1717. }
  1718. /* Access to this is arbitrated for us via seq_file->sem. */
  1719. struct ocfs2_dlm_seq_priv {
  1720. struct ocfs2_dlm_debug *p_dlm_debug;
  1721. struct ocfs2_lock_res p_iter_res;
  1722. struct ocfs2_lock_res p_tmp_res;
  1723. };
  1724. static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
  1725. struct ocfs2_dlm_seq_priv *priv)
  1726. {
  1727. struct ocfs2_lock_res *iter, *ret = NULL;
  1728. struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
  1729. assert_spin_locked(&ocfs2_dlm_tracking_lock);
  1730. list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
  1731. /* discover the head of the list */
  1732. if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
  1733. mlog(0, "End of list found, %p\n", ret);
  1734. break;
  1735. }
  1736. /* We track our "dummy" iteration lockres' by a NULL
  1737. * l_ops field. */
  1738. if (iter->l_ops != NULL) {
  1739. ret = iter;
  1740. break;
  1741. }
  1742. }
  1743. return ret;
  1744. }
  1745. static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
  1746. {
  1747. struct ocfs2_dlm_seq_priv *priv = m->private;
  1748. struct ocfs2_lock_res *iter;
  1749. spin_lock(&ocfs2_dlm_tracking_lock);
  1750. iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
  1751. if (iter) {
  1752. /* Since lockres' have the lifetime of their container
  1753. * (which can be inodes, ocfs2_supers, etc) we want to
  1754. * copy this out to a temporary lockres while still
  1755. * under the spinlock. Obviously after this we can't
  1756. * trust any pointers on the copy returned, but that's
  1757. * ok as the information we want isn't typically held
  1758. * in them. */
  1759. priv->p_tmp_res = *iter;
  1760. iter = &priv->p_tmp_res;
  1761. }
  1762. spin_unlock(&ocfs2_dlm_tracking_lock);
  1763. return iter;
  1764. }
  1765. static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
  1766. {
  1767. }
  1768. static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
  1769. {
  1770. struct ocfs2_dlm_seq_priv *priv = m->private;
  1771. struct ocfs2_lock_res *iter = v;
  1772. struct ocfs2_lock_res *dummy = &priv->p_iter_res;
  1773. spin_lock(&ocfs2_dlm_tracking_lock);
  1774. iter = ocfs2_dlm_next_res(iter, priv);
  1775. list_del_init(&dummy->l_debug_list);
  1776. if (iter) {
  1777. list_add(&dummy->l_debug_list, &iter->l_debug_list);
  1778. priv->p_tmp_res = *iter;
  1779. iter = &priv->p_tmp_res;
  1780. }
  1781. spin_unlock(&ocfs2_dlm_tracking_lock);
  1782. return iter;
  1783. }
  1784. /* So that debugfs.ocfs2 can determine which format is being used */
  1785. #define OCFS2_DLM_DEBUG_STR_VERSION 1
  1786. static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
  1787. {
  1788. int i;
  1789. char *lvb;
  1790. struct ocfs2_lock_res *lockres = v;
  1791. if (!lockres)
  1792. return -EINVAL;
  1793. seq_printf(m, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION);
  1794. if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY)
  1795. seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1,
  1796. lockres->l_name,
  1797. (unsigned int)ocfs2_get_dentry_lock_ino(lockres));
  1798. else
  1799. seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name);
  1800. seq_printf(m, "%d\t"
  1801. "0x%lx\t"
  1802. "0x%x\t"
  1803. "0x%x\t"
  1804. "%u\t"
  1805. "%u\t"
  1806. "%d\t"
  1807. "%d\t",
  1808. lockres->l_level,
  1809. lockres->l_flags,
  1810. lockres->l_action,
  1811. lockres->l_unlock_action,
  1812. lockres->l_ro_holders,
  1813. lockres->l_ex_holders,
  1814. lockres->l_requested,
  1815. lockres->l_blocking);
  1816. /* Dump the raw LVB */
  1817. lvb = lockres->l_lksb.lvb;
  1818. for(i = 0; i < DLM_LVB_LEN; i++)
  1819. seq_printf(m, "0x%x\t", lvb[i]);
  1820. /* End the line */
  1821. seq_printf(m, "\n");
  1822. return 0;
  1823. }
  1824. static struct seq_operations ocfs2_dlm_seq_ops = {
  1825. .start = ocfs2_dlm_seq_start,
  1826. .stop = ocfs2_dlm_seq_stop,
  1827. .next = ocfs2_dlm_seq_next,
  1828. .show = ocfs2_dlm_seq_show,
  1829. };
  1830. static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
  1831. {
  1832. struct seq_file *seq = (struct seq_file *) file->private_data;
  1833. struct ocfs2_dlm_seq_priv *priv = seq->private;
  1834. struct ocfs2_lock_res *res = &priv->p_iter_res;
  1835. ocfs2_remove_lockres_tracking(res);
  1836. ocfs2_put_dlm_debug(priv->p_dlm_debug);
  1837. return seq_release_private(inode, file);
  1838. }
  1839. static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
  1840. {
  1841. int ret;
  1842. struct ocfs2_dlm_seq_priv *priv;
  1843. struct seq_file *seq;
  1844. struct ocfs2_super *osb;
  1845. priv = kzalloc(sizeof(struct ocfs2_dlm_seq_priv), GFP_KERNEL);
  1846. if (!priv) {
  1847. ret = -ENOMEM;
  1848. mlog_errno(ret);
  1849. goto out;
  1850. }
  1851. osb = inode->i_private;
  1852. ocfs2_get_dlm_debug(osb->osb_dlm_debug);
  1853. priv->p_dlm_debug = osb->osb_dlm_debug;
  1854. INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
  1855. ret = seq_open(file, &ocfs2_dlm_seq_ops);
  1856. if (ret) {
  1857. kfree(priv);
  1858. mlog_errno(ret);
  1859. goto out;
  1860. }
  1861. seq = (struct seq_file *) file->private_data;
  1862. seq->private = priv;
  1863. ocfs2_add_lockres_tracking(&priv->p_iter_res,
  1864. priv->p_dlm_debug);
  1865. out:
  1866. return ret;
  1867. }
  1868. static const struct file_operations ocfs2_dlm_debug_fops = {
  1869. .open = ocfs2_dlm_debug_open,
  1870. .release = ocfs2_dlm_debug_release,
  1871. .read = seq_read,
  1872. .llseek = seq_lseek,
  1873. };
  1874. static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
  1875. {
  1876. int ret = 0;
  1877. struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
  1878. dlm_debug->d_locking_state = debugfs_create_file("locking_state",
  1879. S_IFREG|S_IRUSR,
  1880. osb->osb_debug_root,
  1881. osb,
  1882. &ocfs2_dlm_debug_fops);
  1883. if (!dlm_debug->d_locking_state) {
  1884. ret = -EINVAL;
  1885. mlog(ML_ERROR,
  1886. "Unable to create locking state debugfs file.\n");
  1887. goto out;
  1888. }
  1889. ocfs2_get_dlm_debug(dlm_debug);
  1890. out:
  1891. return ret;
  1892. }
  1893. static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
  1894. {
  1895. struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
  1896. if (dlm_debug) {
  1897. debugfs_remove(dlm_debug->d_locking_state);
  1898. ocfs2_put_dlm_debug(dlm_debug);
  1899. }
  1900. }
  1901. int ocfs2_dlm_init(struct ocfs2_super *osb)
  1902. {
  1903. int status = 0;
  1904. u32 dlm_key;
  1905. struct dlm_ctxt *dlm = NULL;
  1906. mlog_entry_void();
  1907. if (ocfs2_mount_local(osb))
  1908. goto local;
  1909. status = ocfs2_dlm_init_debug(osb);
  1910. if (status < 0) {
  1911. mlog_errno(status);
  1912. goto bail;
  1913. }
  1914. /* launch vote thread */
  1915. osb->vote_task = kthread_run(ocfs2_vote_thread, osb, "ocfs2vote");
  1916. if (IS_ERR(osb->vote_task)) {
  1917. status = PTR_ERR(osb->vote_task);
  1918. osb->vote_task = NULL;
  1919. mlog_errno(status);
  1920. goto bail;
  1921. }
  1922. /* used by the dlm code to make message headers unique, each
  1923. * node in this domain must agree on this. */
  1924. dlm_key = crc32_le(0, osb->uuid_str, strlen(osb->uuid_str));
  1925. /* for now, uuid == domain */
  1926. dlm = dlm_register_domain(osb->uuid_str, dlm_key);
  1927. if (IS_ERR(dlm)) {
  1928. status = PTR_ERR(dlm);
  1929. mlog_errno(status);
  1930. goto bail;
  1931. }
  1932. dlm_register_eviction_cb(dlm, &osb->osb_eviction_cb);
  1933. local:
  1934. ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
  1935. ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
  1936. osb->dlm = dlm;
  1937. status = 0;
  1938. bail:
  1939. if (status < 0) {
  1940. ocfs2_dlm_shutdown_debug(osb);
  1941. if (osb->vote_task)
  1942. kthread_stop(osb->vote_task);
  1943. }
  1944. mlog_exit(status);
  1945. return status;
  1946. }
  1947. void ocfs2_dlm_shutdown(struct ocfs2_super *osb)
  1948. {
  1949. mlog_entry_void();
  1950. dlm_unregister_eviction_cb(&osb->osb_eviction_cb);
  1951. ocfs2_drop_osb_locks(osb);
  1952. if (osb->vote_task) {
  1953. kthread_stop(osb->vote_task);
  1954. osb->vote_task = NULL;
  1955. }
  1956. ocfs2_lock_res_free(&osb->osb_super_lockres);
  1957. ocfs2_lock_res_free(&osb->osb_rename_lockres);
  1958. dlm_unregister_domain(osb->dlm);
  1959. osb->dlm = NULL;
  1960. ocfs2_dlm_shutdown_debug(osb);
  1961. mlog_exit_void();
  1962. }
  1963. static void ocfs2_unlock_ast(void *opaque, enum dlm_status status)
  1964. {
  1965. struct ocfs2_lock_res *lockres = opaque;
  1966. unsigned long flags;
  1967. mlog_entry_void();
  1968. mlog(0, "UNLOCK AST called on lock %s, action = %d\n", lockres->l_name,
  1969. lockres->l_unlock_action);
  1970. spin_lock_irqsave(&lockres->l_lock, flags);
  1971. /* We tried to cancel a convert request, but it was already
  1972. * granted. All we want to do here is clear our unlock
  1973. * state. The wake_up call done at the bottom is redundant
  1974. * (ocfs2_prepare_cancel_convert doesn't sleep on this) but doesn't
  1975. * hurt anything anyway */
  1976. if (status == DLM_CANCELGRANT &&
  1977. lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
  1978. mlog(0, "Got cancelgrant for %s\n", lockres->l_name);
  1979. /* We don't clear the busy flag in this case as it
  1980. * should have been cleared by the ast which the dlm
  1981. * has called. */
  1982. goto complete_unlock;
  1983. }
  1984. if (status != DLM_NORMAL) {
  1985. mlog(ML_ERROR, "Dlm passes status %d for lock %s, "
  1986. "unlock_action %d\n", status, lockres->l_name,
  1987. lockres->l_unlock_action);
  1988. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1989. return;
  1990. }
  1991. switch(lockres->l_unlock_action) {
  1992. case OCFS2_UNLOCK_CANCEL_CONVERT:
  1993. mlog(0, "Cancel convert success for %s\n", lockres->l_name);
  1994. lockres->l_action = OCFS2_AST_INVALID;
  1995. break;
  1996. case OCFS2_UNLOCK_DROP_LOCK:
  1997. lockres->l_level = LKM_IVMODE;
  1998. break;
  1999. default:
  2000. BUG();
  2001. }
  2002. lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
  2003. complete_unlock:
  2004. lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
  2005. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2006. wake_up(&lockres->l_event);
  2007. mlog_exit_void();
  2008. }
  2009. static int ocfs2_drop_lock(struct ocfs2_super *osb,
  2010. struct ocfs2_lock_res *lockres)
  2011. {
  2012. enum dlm_status status;
  2013. unsigned long flags;
  2014. int lkm_flags = 0;
  2015. /* We didn't get anywhere near actually using this lockres. */
  2016. if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
  2017. goto out;
  2018. if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
  2019. lkm_flags |= LKM_VALBLK;
  2020. spin_lock_irqsave(&lockres->l_lock, flags);
  2021. mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
  2022. "lockres %s, flags 0x%lx\n",
  2023. lockres->l_name, lockres->l_flags);
  2024. while (lockres->l_flags & OCFS2_LOCK_BUSY) {
  2025. mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
  2026. "%u, unlock_action = %u\n",
  2027. lockres->l_name, lockres->l_flags, lockres->l_action,
  2028. lockres->l_unlock_action);
  2029. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2030. /* XXX: Today we just wait on any busy
  2031. * locks... Perhaps we need to cancel converts in the
  2032. * future? */
  2033. ocfs2_wait_on_busy_lock(lockres);
  2034. spin_lock_irqsave(&lockres->l_lock, flags);
  2035. }
  2036. if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
  2037. if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
  2038. lockres->l_level == LKM_EXMODE &&
  2039. !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
  2040. lockres->l_ops->set_lvb(lockres);
  2041. }
  2042. if (lockres->l_flags & OCFS2_LOCK_BUSY)
  2043. mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
  2044. lockres->l_name);
  2045. if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
  2046. mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
  2047. if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
  2048. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2049. goto out;
  2050. }
  2051. lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
  2052. /* make sure we never get here while waiting for an ast to
  2053. * fire. */
  2054. BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
  2055. /* is this necessary? */
  2056. lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
  2057. lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
  2058. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2059. mlog(0, "lock %s\n", lockres->l_name);
  2060. status = dlmunlock(osb->dlm, &lockres->l_lksb, lkm_flags,
  2061. ocfs2_unlock_ast, lockres);
  2062. if (status != DLM_NORMAL) {
  2063. ocfs2_log_dlm_error("dlmunlock", status, lockres);
  2064. mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
  2065. dlm_print_one_lock(lockres->l_lksb.lockid);
  2066. BUG();
  2067. }
  2068. mlog(0, "lock %s, successfull return from dlmunlock\n",
  2069. lockres->l_name);
  2070. ocfs2_wait_on_busy_lock(lockres);
  2071. out:
  2072. mlog_exit(0);
  2073. return 0;
  2074. }
  2075. /* Mark the lockres as being dropped. It will no longer be
  2076. * queued if blocking, but we still may have to wait on it
  2077. * being dequeued from the vote thread before we can consider
  2078. * it safe to drop.
  2079. *
  2080. * You can *not* attempt to call cluster_lock on this lockres anymore. */
  2081. void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres)
  2082. {
  2083. int status;
  2084. struct ocfs2_mask_waiter mw;
  2085. unsigned long flags;
  2086. ocfs2_init_mask_waiter(&mw);
  2087. spin_lock_irqsave(&lockres->l_lock, flags);
  2088. lockres->l_flags |= OCFS2_LOCK_FREEING;
  2089. while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
  2090. lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
  2091. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2092. mlog(0, "Waiting on lockres %s\n", lockres->l_name);
  2093. status = ocfs2_wait_for_mask(&mw);
  2094. if (status)
  2095. mlog_errno(status);
  2096. spin_lock_irqsave(&lockres->l_lock, flags);
  2097. }
  2098. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2099. }
  2100. void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
  2101. struct ocfs2_lock_res *lockres)
  2102. {
  2103. int ret;
  2104. ocfs2_mark_lockres_freeing(lockres);
  2105. ret = ocfs2_drop_lock(osb, lockres);
  2106. if (ret)
  2107. mlog_errno(ret);
  2108. }
  2109. static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
  2110. {
  2111. ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres);
  2112. ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres);
  2113. }
  2114. int ocfs2_drop_inode_locks(struct inode *inode)
  2115. {
  2116. int status, err;
  2117. mlog_entry_void();
  2118. /* No need to call ocfs2_mark_lockres_freeing here -
  2119. * ocfs2_clear_inode has done it for us. */
  2120. err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
  2121. &OCFS2_I(inode)->ip_open_lockres);
  2122. if (err < 0)
  2123. mlog_errno(err);
  2124. status = err;
  2125. err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
  2126. &OCFS2_I(inode)->ip_data_lockres);
  2127. if (err < 0)
  2128. mlog_errno(err);
  2129. if (err < 0 && !status)
  2130. status = err;
  2131. err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
  2132. &OCFS2_I(inode)->ip_meta_lockres);
  2133. if (err < 0)
  2134. mlog_errno(err);
  2135. if (err < 0 && !status)
  2136. status = err;
  2137. err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
  2138. &OCFS2_I(inode)->ip_rw_lockres);
  2139. if (err < 0)
  2140. mlog_errno(err);
  2141. if (err < 0 && !status)
  2142. status = err;
  2143. mlog_exit(status);
  2144. return status;
  2145. }
  2146. static void ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
  2147. int new_level)
  2148. {
  2149. assert_spin_locked(&lockres->l_lock);
  2150. BUG_ON(lockres->l_blocking <= LKM_NLMODE);
  2151. if (lockres->l_level <= new_level) {
  2152. mlog(ML_ERROR, "lockres->l_level (%u) <= new_level (%u)\n",
  2153. lockres->l_level, new_level);
  2154. BUG();
  2155. }
  2156. mlog(0, "lock %s, new_level = %d, l_blocking = %d\n",
  2157. lockres->l_name, new_level, lockres->l_blocking);
  2158. lockres->l_action = OCFS2_AST_DOWNCONVERT;
  2159. lockres->l_requested = new_level;
  2160. lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
  2161. }
  2162. static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
  2163. struct ocfs2_lock_res *lockres,
  2164. int new_level,
  2165. int lvb)
  2166. {
  2167. int ret, dlm_flags = LKM_CONVERT;
  2168. enum dlm_status status;
  2169. mlog_entry_void();
  2170. if (lvb)
  2171. dlm_flags |= LKM_VALBLK;
  2172. status = dlmlock(osb->dlm,
  2173. new_level,
  2174. &lockres->l_lksb,
  2175. dlm_flags,
  2176. lockres->l_name,
  2177. OCFS2_LOCK_ID_MAX_LEN - 1,
  2178. ocfs2_locking_ast,
  2179. lockres,
  2180. ocfs2_blocking_ast);
  2181. if (status != DLM_NORMAL) {
  2182. ocfs2_log_dlm_error("dlmlock", status, lockres);
  2183. ret = -EINVAL;
  2184. ocfs2_recover_from_dlm_error(lockres, 1);
  2185. goto bail;
  2186. }
  2187. ret = 0;
  2188. bail:
  2189. mlog_exit(ret);
  2190. return ret;
  2191. }
  2192. /* returns 1 when the caller should unlock and call dlmunlock */
  2193. static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
  2194. struct ocfs2_lock_res *lockres)
  2195. {
  2196. assert_spin_locked(&lockres->l_lock);
  2197. mlog_entry_void();
  2198. mlog(0, "lock %s\n", lockres->l_name);
  2199. if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
  2200. /* If we're already trying to cancel a lock conversion
  2201. * then just drop the spinlock and allow the caller to
  2202. * requeue this lock. */
  2203. mlog(0, "Lockres %s, skip convert\n", lockres->l_name);
  2204. return 0;
  2205. }
  2206. /* were we in a convert when we got the bast fire? */
  2207. BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
  2208. lockres->l_action != OCFS2_AST_DOWNCONVERT);
  2209. /* set things up for the unlockast to know to just
  2210. * clear out the ast_action and unset busy, etc. */
  2211. lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
  2212. mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
  2213. "lock %s, invalid flags: 0x%lx\n",
  2214. lockres->l_name, lockres->l_flags);
  2215. return 1;
  2216. }
  2217. static int ocfs2_cancel_convert(struct ocfs2_super *osb,
  2218. struct ocfs2_lock_res *lockres)
  2219. {
  2220. int ret;
  2221. enum dlm_status status;
  2222. mlog_entry_void();
  2223. mlog(0, "lock %s\n", lockres->l_name);
  2224. ret = 0;
  2225. status = dlmunlock(osb->dlm,
  2226. &lockres->l_lksb,
  2227. LKM_CANCEL,
  2228. ocfs2_unlock_ast,
  2229. lockres);
  2230. if (status != DLM_NORMAL) {
  2231. ocfs2_log_dlm_error("dlmunlock", status, lockres);
  2232. ret = -EINVAL;
  2233. ocfs2_recover_from_dlm_error(lockres, 0);
  2234. }
  2235. mlog(0, "lock %s return from dlmunlock\n", lockres->l_name);
  2236. mlog_exit(ret);
  2237. return ret;
  2238. }
  2239. static int ocfs2_unblock_lock(struct ocfs2_super *osb,
  2240. struct ocfs2_lock_res *lockres,
  2241. struct ocfs2_unblock_ctl *ctl)
  2242. {
  2243. unsigned long flags;
  2244. int blocking;
  2245. int new_level;
  2246. int ret = 0;
  2247. int set_lvb = 0;
  2248. mlog_entry_void();
  2249. spin_lock_irqsave(&lockres->l_lock, flags);
  2250. BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
  2251. recheck:
  2252. if (lockres->l_flags & OCFS2_LOCK_BUSY) {
  2253. ctl->requeue = 1;
  2254. ret = ocfs2_prepare_cancel_convert(osb, lockres);
  2255. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2256. if (ret) {
  2257. ret = ocfs2_cancel_convert(osb, lockres);
  2258. if (ret < 0)
  2259. mlog_errno(ret);
  2260. }
  2261. goto leave;
  2262. }
  2263. /* if we're blocking an exclusive and we have *any* holders,
  2264. * then requeue. */
  2265. if ((lockres->l_blocking == LKM_EXMODE)
  2266. && (lockres->l_ex_holders || lockres->l_ro_holders))
  2267. goto leave_requeue;
  2268. /* If it's a PR we're blocking, then only
  2269. * requeue if we've got any EX holders */
  2270. if (lockres->l_blocking == LKM_PRMODE &&
  2271. lockres->l_ex_holders)
  2272. goto leave_requeue;
  2273. /*
  2274. * Can we get a lock in this state if the holder counts are
  2275. * zero? The meta data unblock code used to check this.
  2276. */
  2277. if ((lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
  2278. && (lockres->l_flags & OCFS2_LOCK_REFRESHING))
  2279. goto leave_requeue;
  2280. new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
  2281. if (lockres->l_ops->check_downconvert
  2282. && !lockres->l_ops->check_downconvert(lockres, new_level))
  2283. goto leave_requeue;
  2284. /* If we get here, then we know that there are no more
  2285. * incompatible holders (and anyone asking for an incompatible
  2286. * lock is blocked). We can now downconvert the lock */
  2287. if (!lockres->l_ops->downconvert_worker)
  2288. goto downconvert;
  2289. /* Some lockres types want to do a bit of work before
  2290. * downconverting a lock. Allow that here. The worker function
  2291. * may sleep, so we save off a copy of what we're blocking as
  2292. * it may change while we're not holding the spin lock. */
  2293. blocking = lockres->l_blocking;
  2294. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2295. ctl->unblock_action = lockres->l_ops->downconvert_worker(lockres, blocking);
  2296. if (ctl->unblock_action == UNBLOCK_STOP_POST)
  2297. goto leave;
  2298. spin_lock_irqsave(&lockres->l_lock, flags);
  2299. if (blocking != lockres->l_blocking) {
  2300. /* If this changed underneath us, then we can't drop
  2301. * it just yet. */
  2302. goto recheck;
  2303. }
  2304. downconvert:
  2305. ctl->requeue = 0;
  2306. if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
  2307. if (lockres->l_level == LKM_EXMODE)
  2308. set_lvb = 1;
  2309. /*
  2310. * We only set the lvb if the lock has been fully
  2311. * refreshed - otherwise we risk setting stale
  2312. * data. Otherwise, there's no need to actually clear
  2313. * out the lvb here as it's value is still valid.
  2314. */
  2315. if (set_lvb && !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
  2316. lockres->l_ops->set_lvb(lockres);
  2317. }
  2318. ocfs2_prepare_downconvert(lockres, new_level);
  2319. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2320. ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb);
  2321. leave:
  2322. mlog_exit(ret);
  2323. return ret;
  2324. leave_requeue:
  2325. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2326. ctl->requeue = 1;
  2327. mlog_exit(0);
  2328. return 0;
  2329. }
  2330. static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
  2331. int blocking)
  2332. {
  2333. struct inode *inode;
  2334. struct address_space *mapping;
  2335. inode = ocfs2_lock_res_inode(lockres);
  2336. mapping = inode->i_mapping;
  2337. /*
  2338. * We need this before the filemap_fdatawrite() so that it can
  2339. * transfer the dirty bit from the PTE to the
  2340. * page. Unfortunately this means that even for EX->PR
  2341. * downconverts, we'll lose our mappings and have to build
  2342. * them up again.
  2343. */
  2344. unmap_mapping_range(mapping, 0, 0, 0);
  2345. if (filemap_fdatawrite(mapping)) {
  2346. mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
  2347. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  2348. }
  2349. sync_mapping_buffers(mapping);
  2350. if (blocking == LKM_EXMODE) {
  2351. truncate_inode_pages(mapping, 0);
  2352. } else {
  2353. /* We only need to wait on the I/O if we're not also
  2354. * truncating pages because truncate_inode_pages waits
  2355. * for us above. We don't truncate pages if we're
  2356. * blocking anything < EXMODE because we want to keep
  2357. * them around in that case. */
  2358. filemap_fdatawait(mapping);
  2359. }
  2360. return UNBLOCK_CONTINUE;
  2361. }
  2362. static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
  2363. int new_level)
  2364. {
  2365. struct inode *inode = ocfs2_lock_res_inode(lockres);
  2366. int checkpointed = ocfs2_inode_fully_checkpointed(inode);
  2367. BUG_ON(new_level != LKM_NLMODE && new_level != LKM_PRMODE);
  2368. BUG_ON(lockres->l_level != LKM_EXMODE && !checkpointed);
  2369. if (checkpointed)
  2370. return 1;
  2371. ocfs2_start_checkpoint(OCFS2_SB(inode->i_sb));
  2372. return 0;
  2373. }
  2374. static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres)
  2375. {
  2376. struct inode *inode = ocfs2_lock_res_inode(lockres);
  2377. __ocfs2_stuff_meta_lvb(inode);
  2378. }
  2379. /*
  2380. * Does the final reference drop on our dentry lock. Right now this
  2381. * happens in the vote thread, but we could choose to simplify the
  2382. * dlmglue API and push these off to the ocfs2_wq in the future.
  2383. */
  2384. static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
  2385. struct ocfs2_lock_res *lockres)
  2386. {
  2387. struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
  2388. ocfs2_dentry_lock_put(osb, dl);
  2389. }
  2390. /*
  2391. * d_delete() matching dentries before the lock downconvert.
  2392. *
  2393. * At this point, any process waiting to destroy the
  2394. * dentry_lock due to last ref count is stopped by the
  2395. * OCFS2_LOCK_QUEUED flag.
  2396. *
  2397. * We have two potential problems
  2398. *
  2399. * 1) If we do the last reference drop on our dentry_lock (via dput)
  2400. * we'll wind up in ocfs2_release_dentry_lock(), waiting on
  2401. * the downconvert to finish. Instead we take an elevated
  2402. * reference and push the drop until after we've completed our
  2403. * unblock processing.
  2404. *
  2405. * 2) There might be another process with a final reference,
  2406. * waiting on us to finish processing. If this is the case, we
  2407. * detect it and exit out - there's no more dentries anyway.
  2408. */
  2409. static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
  2410. int blocking)
  2411. {
  2412. struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
  2413. struct ocfs2_inode_info *oi = OCFS2_I(dl->dl_inode);
  2414. struct dentry *dentry;
  2415. unsigned long flags;
  2416. int extra_ref = 0;
  2417. /*
  2418. * This node is blocking another node from getting a read
  2419. * lock. This happens when we've renamed within a
  2420. * directory. We've forced the other nodes to d_delete(), but
  2421. * we never actually dropped our lock because it's still
  2422. * valid. The downconvert code will retain a PR for this node,
  2423. * so there's no further work to do.
  2424. */
  2425. if (blocking == LKM_PRMODE)
  2426. return UNBLOCK_CONTINUE;
  2427. /*
  2428. * Mark this inode as potentially orphaned. The code in
  2429. * ocfs2_delete_inode() will figure out whether it actually
  2430. * needs to be freed or not.
  2431. */
  2432. spin_lock(&oi->ip_lock);
  2433. oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
  2434. spin_unlock(&oi->ip_lock);
  2435. /*
  2436. * Yuck. We need to make sure however that the check of
  2437. * OCFS2_LOCK_FREEING and the extra reference are atomic with
  2438. * respect to a reference decrement or the setting of that
  2439. * flag.
  2440. */
  2441. spin_lock_irqsave(&lockres->l_lock, flags);
  2442. spin_lock(&dentry_attach_lock);
  2443. if (!(lockres->l_flags & OCFS2_LOCK_FREEING)
  2444. && dl->dl_count) {
  2445. dl->dl_count++;
  2446. extra_ref = 1;
  2447. }
  2448. spin_unlock(&dentry_attach_lock);
  2449. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2450. mlog(0, "extra_ref = %d\n", extra_ref);
  2451. /*
  2452. * We have a process waiting on us in ocfs2_dentry_iput(),
  2453. * which means we can't have any more outstanding
  2454. * aliases. There's no need to do any more work.
  2455. */
  2456. if (!extra_ref)
  2457. return UNBLOCK_CONTINUE;
  2458. spin_lock(&dentry_attach_lock);
  2459. while (1) {
  2460. dentry = ocfs2_find_local_alias(dl->dl_inode,
  2461. dl->dl_parent_blkno, 1);
  2462. if (!dentry)
  2463. break;
  2464. spin_unlock(&dentry_attach_lock);
  2465. mlog(0, "d_delete(%.*s);\n", dentry->d_name.len,
  2466. dentry->d_name.name);
  2467. /*
  2468. * The following dcache calls may do an
  2469. * iput(). Normally we don't want that from the
  2470. * downconverting thread, but in this case it's ok
  2471. * because the requesting node already has an
  2472. * exclusive lock on the inode, so it can't be queued
  2473. * for a downconvert.
  2474. */
  2475. d_delete(dentry);
  2476. dput(dentry);
  2477. spin_lock(&dentry_attach_lock);
  2478. }
  2479. spin_unlock(&dentry_attach_lock);
  2480. /*
  2481. * If we are the last holder of this dentry lock, there is no
  2482. * reason to downconvert so skip straight to the unlock.
  2483. */
  2484. if (dl->dl_count == 1)
  2485. return UNBLOCK_STOP_POST;
  2486. return UNBLOCK_CONTINUE_POST;
  2487. }
  2488. void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
  2489. struct ocfs2_lock_res *lockres)
  2490. {
  2491. int status;
  2492. struct ocfs2_unblock_ctl ctl = {0, 0,};
  2493. unsigned long flags;
  2494. /* Our reference to the lockres in this function can be
  2495. * considered valid until we remove the OCFS2_LOCK_QUEUED
  2496. * flag. */
  2497. mlog_entry_void();
  2498. BUG_ON(!lockres);
  2499. BUG_ON(!lockres->l_ops);
  2500. mlog(0, "lockres %s blocked.\n", lockres->l_name);
  2501. /* Detect whether a lock has been marked as going away while
  2502. * the vote thread was processing other things. A lock can
  2503. * still be marked with OCFS2_LOCK_FREEING after this check,
  2504. * but short circuiting here will still save us some
  2505. * performance. */
  2506. spin_lock_irqsave(&lockres->l_lock, flags);
  2507. if (lockres->l_flags & OCFS2_LOCK_FREEING)
  2508. goto unqueue;
  2509. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2510. status = ocfs2_unblock_lock(osb, lockres, &ctl);
  2511. if (status < 0)
  2512. mlog_errno(status);
  2513. spin_lock_irqsave(&lockres->l_lock, flags);
  2514. unqueue:
  2515. if (lockres->l_flags & OCFS2_LOCK_FREEING || !ctl.requeue) {
  2516. lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
  2517. } else
  2518. ocfs2_schedule_blocked_lock(osb, lockres);
  2519. mlog(0, "lockres %s, requeue = %s.\n", lockres->l_name,
  2520. ctl.requeue ? "yes" : "no");
  2521. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2522. if (ctl.unblock_action != UNBLOCK_CONTINUE
  2523. && lockres->l_ops->post_unlock)
  2524. lockres->l_ops->post_unlock(osb, lockres);
  2525. mlog_exit_void();
  2526. }
  2527. static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
  2528. struct ocfs2_lock_res *lockres)
  2529. {
  2530. mlog_entry_void();
  2531. assert_spin_locked(&lockres->l_lock);
  2532. if (lockres->l_flags & OCFS2_LOCK_FREEING) {
  2533. /* Do not schedule a lock for downconvert when it's on
  2534. * the way to destruction - any nodes wanting access
  2535. * to the resource will get it soon. */
  2536. mlog(0, "Lockres %s won't be scheduled: flags 0x%lx\n",
  2537. lockres->l_name, lockres->l_flags);
  2538. return;
  2539. }
  2540. lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
  2541. spin_lock(&osb->vote_task_lock);
  2542. if (list_empty(&lockres->l_blocked_list)) {
  2543. list_add_tail(&lockres->l_blocked_list,
  2544. &osb->blocked_lock_list);
  2545. osb->blocked_lock_count++;
  2546. }
  2547. spin_unlock(&osb->vote_task_lock);
  2548. mlog_exit_void();
  2549. }