dlmglue.c 99 KB

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