dlmglue.c 90 KB

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