dlmglue.c 103 KB

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