dlmglue.c 122 KB

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