dlmglue.c 125 KB

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