dlmglue.c 90 KB

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