dlmglue.c 126 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * dlmglue.c
  4. *
  5. * Code which implements an OCFS2 specific interface to our DLM.
  6. *
  7. * Copyright (C) 2003, 2004 Oracle. All rights reserved.
  8. */
  9. #include <linux/types.h>
  10. #include <linux/slab.h>
  11. #include <linux/highmem.h>
  12. #include <linux/mm.h>
  13. #include <linux/kthread.h>
  14. #include <linux/pagemap.h>
  15. #include <linux/debugfs.h>
  16. #include <linux/seq_file.h>
  17. #include <linux/time.h>
  18. #include <linux/delay.h>
  19. #include <linux/quotaops.h>
  20. #include <linux/sched/signal.h>
  21. #include <linux/string_choices.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 const struct ocfs2_lock_res_ops ocfs2_inode_rw_lops = {
  197. .get_osb = ocfs2_get_inode_osb,
  198. .flags = 0,
  199. };
  200. static const 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 const struct ocfs2_lock_res_ops ocfs2_super_lops = {
  208. .flags = LOCK_TYPE_REQUIRES_REFRESH,
  209. };
  210. static const struct ocfs2_lock_res_ops ocfs2_rename_lops = {
  211. .flags = 0,
  212. };
  213. static const struct ocfs2_lock_res_ops ocfs2_nfs_sync_lops = {
  214. .flags = 0,
  215. };
  216. static const struct ocfs2_lock_res_ops ocfs2_trim_fs_lops = {
  217. .flags = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB,
  218. };
  219. static const struct ocfs2_lock_res_ops ocfs2_orphan_scan_lops = {
  220. .flags = LOCK_TYPE_REQUIRES_REFRESH|LOCK_TYPE_USES_LVB,
  221. };
  222. static const 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 const struct ocfs2_lock_res_ops ocfs2_inode_open_lops = {
  229. .get_osb = ocfs2_get_inode_osb,
  230. .flags = 0,
  231. };
  232. static const struct ocfs2_lock_res_ops ocfs2_flock_lops = {
  233. .get_osb = ocfs2_get_file_osb,
  234. .flags = 0,
  235. };
  236. static const 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 const 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. const 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. const 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 used 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 request 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. struct timespec64 ts;
  1829. lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
  1830. /*
  1831. * Invalidate the LVB of a deleted inode - this way other
  1832. * nodes are forced to go to disk and discover the new inode
  1833. * status.
  1834. */
  1835. if (oi->ip_flags & OCFS2_INODE_DELETED) {
  1836. lvb->lvb_version = 0;
  1837. goto out;
  1838. }
  1839. lvb->lvb_version = OCFS2_LVB_VERSION;
  1840. lvb->lvb_isize = cpu_to_be64(i_size_read(inode));
  1841. lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters);
  1842. lvb->lvb_iuid = cpu_to_be32(i_uid_read(inode));
  1843. lvb->lvb_igid = cpu_to_be32(i_gid_read(inode));
  1844. lvb->lvb_imode = cpu_to_be16(inode->i_mode);
  1845. lvb->lvb_inlink = cpu_to_be16(inode->i_nlink);
  1846. ts = inode_get_atime(inode);
  1847. lvb->lvb_iatime_packed = cpu_to_be64(ocfs2_pack_timespec(&ts));
  1848. ts = inode_get_ctime(inode);
  1849. lvb->lvb_ictime_packed = cpu_to_be64(ocfs2_pack_timespec(&ts));
  1850. ts = inode_get_mtime(inode);
  1851. lvb->lvb_imtime_packed = cpu_to_be64(ocfs2_pack_timespec(&ts));
  1852. lvb->lvb_iattr = cpu_to_be32(oi->ip_attr);
  1853. lvb->lvb_idynfeatures = cpu_to_be16(oi->ip_dyn_features);
  1854. lvb->lvb_igeneration = cpu_to_be32(inode->i_generation);
  1855. out:
  1856. mlog_meta_lvb(0, lockres);
  1857. }
  1858. static void ocfs2_unpack_timespec(struct timespec64 *spec,
  1859. u64 packed_time)
  1860. {
  1861. spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
  1862. spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
  1863. }
  1864. static int ocfs2_refresh_inode_from_lvb(struct inode *inode)
  1865. {
  1866. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1867. struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
  1868. struct ocfs2_meta_lvb *lvb;
  1869. struct timespec64 ts;
  1870. mlog_meta_lvb(0, lockres);
  1871. lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
  1872. if (inode_wrong_type(inode, be16_to_cpu(lvb->lvb_imode)))
  1873. return -ESTALE;
  1874. /* We're safe here without the lockres lock... */
  1875. spin_lock(&oi->ip_lock);
  1876. oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters);
  1877. i_size_write(inode, be64_to_cpu(lvb->lvb_isize));
  1878. oi->ip_attr = be32_to_cpu(lvb->lvb_iattr);
  1879. oi->ip_dyn_features = be16_to_cpu(lvb->lvb_idynfeatures);
  1880. ocfs2_set_inode_flags(inode);
  1881. /* fast-symlinks are a special case */
  1882. if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
  1883. inode->i_blocks = 0;
  1884. else
  1885. inode->i_blocks = ocfs2_inode_sector_count(inode);
  1886. i_uid_write(inode, be32_to_cpu(lvb->lvb_iuid));
  1887. i_gid_write(inode, be32_to_cpu(lvb->lvb_igid));
  1888. inode->i_mode = be16_to_cpu(lvb->lvb_imode);
  1889. set_nlink(inode, be16_to_cpu(lvb->lvb_inlink));
  1890. ocfs2_unpack_timespec(&ts, be64_to_cpu(lvb->lvb_iatime_packed));
  1891. inode_set_atime_to_ts(inode, ts);
  1892. ocfs2_unpack_timespec(&ts, be64_to_cpu(lvb->lvb_imtime_packed));
  1893. inode_set_mtime_to_ts(inode, ts);
  1894. ocfs2_unpack_timespec(&ts, be64_to_cpu(lvb->lvb_ictime_packed));
  1895. inode_set_ctime_to_ts(inode, ts);
  1896. spin_unlock(&oi->ip_lock);
  1897. return 0;
  1898. }
  1899. static inline int ocfs2_meta_lvb_is_trustable(struct inode *inode,
  1900. struct ocfs2_lock_res *lockres)
  1901. {
  1902. struct ocfs2_meta_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
  1903. if (ocfs2_dlm_lvb_valid(&lockres->l_lksb)
  1904. && lvb->lvb_version == OCFS2_LVB_VERSION
  1905. && be32_to_cpu(lvb->lvb_igeneration) == inode->i_generation)
  1906. return 1;
  1907. return 0;
  1908. }
  1909. /* Determine whether a lock resource needs to be refreshed, and
  1910. * arbitrate who gets to refresh it.
  1911. *
  1912. * 0 means no refresh needed.
  1913. *
  1914. * > 0 means you need to refresh this and you MUST call
  1915. * ocfs2_complete_lock_res_refresh afterwards. */
  1916. static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
  1917. {
  1918. unsigned long flags;
  1919. int status = 0;
  1920. refresh_check:
  1921. spin_lock_irqsave(&lockres->l_lock, flags);
  1922. if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
  1923. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1924. goto bail;
  1925. }
  1926. if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
  1927. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1928. ocfs2_wait_on_refreshing_lock(lockres);
  1929. goto refresh_check;
  1930. }
  1931. /* Ok, I'll be the one to refresh this lock. */
  1932. lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
  1933. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1934. status = 1;
  1935. bail:
  1936. mlog(0, "status %d\n", status);
  1937. return status;
  1938. }
  1939. /* If status is non zero, I'll mark it as not being in refresh
  1940. * anymroe, but i won't clear the needs refresh flag. */
  1941. static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
  1942. int status)
  1943. {
  1944. unsigned long flags;
  1945. spin_lock_irqsave(&lockres->l_lock, flags);
  1946. lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
  1947. if (!status)
  1948. lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
  1949. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1950. wake_up(&lockres->l_event);
  1951. }
  1952. /* may or may not return a bh if it went to disk. */
  1953. static int ocfs2_inode_lock_update(struct inode *inode,
  1954. struct buffer_head **bh)
  1955. {
  1956. int status = 0;
  1957. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1958. struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
  1959. struct ocfs2_dinode *fe;
  1960. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1961. if (ocfs2_mount_local(osb))
  1962. goto bail;
  1963. spin_lock(&oi->ip_lock);
  1964. if (oi->ip_flags & OCFS2_INODE_DELETED) {
  1965. mlog(0, "Orphaned inode %llu was deleted while we "
  1966. "were waiting on a lock. ip_flags = 0x%x\n",
  1967. (unsigned long long)oi->ip_blkno, oi->ip_flags);
  1968. spin_unlock(&oi->ip_lock);
  1969. status = -ENOENT;
  1970. goto bail;
  1971. }
  1972. spin_unlock(&oi->ip_lock);
  1973. if (!ocfs2_should_refresh_lock_res(lockres))
  1974. goto bail;
  1975. /* This will discard any caching information we might have had
  1976. * for the inode metadata. */
  1977. ocfs2_metadata_cache_purge(INODE_CACHE(inode));
  1978. ocfs2_extent_map_trunc(inode, 0);
  1979. if (ocfs2_meta_lvb_is_trustable(inode, lockres)) {
  1980. mlog(0, "Trusting LVB on inode %llu\n",
  1981. (unsigned long long)oi->ip_blkno);
  1982. status = ocfs2_refresh_inode_from_lvb(inode);
  1983. goto bail_refresh;
  1984. } else {
  1985. /* Boo, we have to go to disk. */
  1986. /* read bh, cast, ocfs2_refresh_inode */
  1987. status = ocfs2_read_inode_block(inode, bh);
  1988. if (status < 0) {
  1989. mlog_errno(status);
  1990. goto bail_refresh;
  1991. }
  1992. fe = (struct ocfs2_dinode *) (*bh)->b_data;
  1993. if (inode_wrong_type(inode, le16_to_cpu(fe->i_mode))) {
  1994. status = -ESTALE;
  1995. goto bail_refresh;
  1996. }
  1997. /* This is a good chance to make sure we're not
  1998. * locking an invalid object. ocfs2_read_inode_block()
  1999. * already checked that the inode block is sane.
  2000. *
  2001. * We bug on a stale inode here because we checked
  2002. * above whether it was wiped from disk. The wiping
  2003. * node provides a guarantee that we receive that
  2004. * message and can mark the inode before dropping any
  2005. * locks associated with it. */
  2006. mlog_bug_on_msg(inode->i_generation !=
  2007. le32_to_cpu(fe->i_generation),
  2008. "Invalid dinode %llu disk generation: %u "
  2009. "inode->i_generation: %u\n",
  2010. (unsigned long long)oi->ip_blkno,
  2011. le32_to_cpu(fe->i_generation),
  2012. inode->i_generation);
  2013. mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
  2014. !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
  2015. "Stale dinode %llu dtime: %llu flags: 0x%x\n",
  2016. (unsigned long long)oi->ip_blkno,
  2017. (unsigned long long)le64_to_cpu(fe->i_dtime),
  2018. le32_to_cpu(fe->i_flags));
  2019. ocfs2_refresh_inode(inode, fe);
  2020. ocfs2_track_lock_refresh(lockres);
  2021. }
  2022. status = 0;
  2023. bail_refresh:
  2024. ocfs2_complete_lock_res_refresh(lockres, status);
  2025. bail:
  2026. return status;
  2027. }
  2028. static int ocfs2_assign_bh(struct inode *inode,
  2029. struct buffer_head **ret_bh,
  2030. struct buffer_head *passed_bh)
  2031. {
  2032. int status;
  2033. if (passed_bh) {
  2034. /* Ok, the update went to disk for us, use the
  2035. * returned bh. */
  2036. *ret_bh = passed_bh;
  2037. get_bh(*ret_bh);
  2038. return 0;
  2039. }
  2040. status = ocfs2_read_inode_block(inode, ret_bh);
  2041. if (status < 0)
  2042. mlog_errno(status);
  2043. return status;
  2044. }
  2045. /*
  2046. * returns < 0 error if the callback will never be called, otherwise
  2047. * the result of the lock will be communicated via the callback.
  2048. */
  2049. int ocfs2_inode_lock_full_nested(struct inode *inode,
  2050. struct buffer_head **ret_bh,
  2051. int ex,
  2052. int arg_flags,
  2053. int subclass)
  2054. {
  2055. int status, level, acquired;
  2056. u32 dlm_flags;
  2057. struct ocfs2_lock_res *lockres = NULL;
  2058. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  2059. struct buffer_head *local_bh = NULL;
  2060. mlog(0, "inode %llu, take %s META lock\n",
  2061. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  2062. ex ? "EXMODE" : "PRMODE");
  2063. status = 0;
  2064. acquired = 0;
  2065. /* We'll allow faking a readonly metadata lock for
  2066. * rodevices. */
  2067. if (ocfs2_is_hard_readonly(osb)) {
  2068. if (ex)
  2069. status = -EROFS;
  2070. goto getbh;
  2071. }
  2072. if ((arg_flags & OCFS2_META_LOCK_GETBH) ||
  2073. ocfs2_mount_local(osb))
  2074. goto update;
  2075. if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
  2076. ocfs2_wait_for_recovery(osb);
  2077. lockres = &OCFS2_I(inode)->ip_inode_lockres;
  2078. level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
  2079. dlm_flags = 0;
  2080. if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
  2081. dlm_flags |= DLM_LKF_NOQUEUE;
  2082. status = __ocfs2_cluster_lock(osb, lockres, level, dlm_flags,
  2083. arg_flags, subclass, _RET_IP_);
  2084. if (status < 0) {
  2085. if (status != -EAGAIN)
  2086. mlog_errno(status);
  2087. goto bail;
  2088. }
  2089. /* Notify the error cleanup path to drop the cluster lock. */
  2090. acquired = 1;
  2091. /* We wait twice because a node may have died while we were in
  2092. * the lower dlm layers. The second time though, we've
  2093. * committed to owning this lock so we don't allow signals to
  2094. * abort the operation. */
  2095. if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
  2096. ocfs2_wait_for_recovery(osb);
  2097. update:
  2098. /*
  2099. * We only see this flag if we're being called from
  2100. * ocfs2_read_locked_inode(). It means we're locking an inode
  2101. * which hasn't been populated yet, so clear the refresh flag
  2102. * and let the caller handle it.
  2103. */
  2104. if (inode_state_read_once(inode) & I_NEW) {
  2105. status = 0;
  2106. if (lockres)
  2107. ocfs2_complete_lock_res_refresh(lockres, 0);
  2108. goto bail;
  2109. }
  2110. /* This is fun. The caller may want a bh back, or it may
  2111. * not. ocfs2_inode_lock_update definitely wants one in, but
  2112. * may or may not read one, depending on what's in the
  2113. * LVB. The result of all of this is that we've *only* gone to
  2114. * disk if we have to, so the complexity is worthwhile. */
  2115. status = ocfs2_inode_lock_update(inode, &local_bh);
  2116. if (status < 0) {
  2117. if (status != -ENOENT)
  2118. mlog_errno(status);
  2119. goto bail;
  2120. }
  2121. getbh:
  2122. if (ret_bh) {
  2123. status = ocfs2_assign_bh(inode, ret_bh, local_bh);
  2124. if (status < 0) {
  2125. mlog_errno(status);
  2126. goto bail;
  2127. }
  2128. }
  2129. bail:
  2130. if (status < 0) {
  2131. if (ret_bh && (*ret_bh)) {
  2132. brelse(*ret_bh);
  2133. *ret_bh = NULL;
  2134. }
  2135. if (acquired)
  2136. ocfs2_inode_unlock(inode, ex);
  2137. }
  2138. brelse(local_bh);
  2139. return status;
  2140. }
  2141. /*
  2142. * This is working around a lock inversion between tasks acquiring DLM
  2143. * locks while holding a folio lock and the downconvert thread which
  2144. * blocks dlm lock acquiry while acquiring folio locks.
  2145. *
  2146. * ** These _with_folio variants are only intended to be called from aop
  2147. * methods that hold folio locks and return a very specific *positive* error
  2148. * code that aop methods pass up to the VFS -- test for errors with != 0. **
  2149. *
  2150. * The DLM is called such that it returns -EAGAIN if it would have
  2151. * blocked waiting for the downconvert thread. In that case we unlock
  2152. * our folio so the downconvert thread can make progress. Once we've
  2153. * done this we have to return AOP_TRUNCATED_PAGE so the aop method
  2154. * that called us can bubble that back up into the VFS who will then
  2155. * immediately retry the aop call.
  2156. */
  2157. int ocfs2_inode_lock_with_folio(struct inode *inode,
  2158. struct buffer_head **ret_bh, int ex, struct folio *folio)
  2159. {
  2160. int ret;
  2161. ret = ocfs2_inode_lock_full(inode, ret_bh, ex, OCFS2_LOCK_NONBLOCK);
  2162. if (ret == -EAGAIN) {
  2163. folio_unlock(folio);
  2164. /*
  2165. * If we can't get inode lock immediately, we should not return
  2166. * directly here, since this will lead to a softlockup problem.
  2167. * The method is to get a blocking lock and immediately unlock
  2168. * before returning, this can avoid CPU resource waste due to
  2169. * lots of retries, and benefits fairness in getting lock.
  2170. */
  2171. if (ocfs2_inode_lock(inode, ret_bh, ex) == 0)
  2172. ocfs2_inode_unlock(inode, ex);
  2173. ret = AOP_TRUNCATED_PAGE;
  2174. }
  2175. return ret;
  2176. }
  2177. int ocfs2_inode_lock_atime(struct inode *inode,
  2178. struct vfsmount *vfsmnt,
  2179. int *level, int wait)
  2180. {
  2181. int ret;
  2182. if (wait)
  2183. ret = ocfs2_inode_lock(inode, NULL, 0);
  2184. else
  2185. ret = ocfs2_try_inode_lock(inode, NULL, 0);
  2186. if (ret < 0) {
  2187. if (ret != -EAGAIN)
  2188. mlog_errno(ret);
  2189. return ret;
  2190. }
  2191. /*
  2192. * If we should update atime, we will get EX lock,
  2193. * otherwise we just get PR lock.
  2194. */
  2195. if (ocfs2_should_update_atime(inode, vfsmnt)) {
  2196. struct buffer_head *bh = NULL;
  2197. ocfs2_inode_unlock(inode, 0);
  2198. if (wait)
  2199. ret = ocfs2_inode_lock(inode, &bh, 1);
  2200. else
  2201. ret = ocfs2_try_inode_lock(inode, &bh, 1);
  2202. if (ret < 0) {
  2203. if (ret != -EAGAIN)
  2204. mlog_errno(ret);
  2205. return ret;
  2206. }
  2207. *level = 1;
  2208. if (ocfs2_should_update_atime(inode, vfsmnt))
  2209. ocfs2_update_inode_atime(inode, bh);
  2210. brelse(bh);
  2211. } else
  2212. *level = 0;
  2213. return ret;
  2214. }
  2215. void ocfs2_inode_unlock(struct inode *inode,
  2216. int ex)
  2217. {
  2218. int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
  2219. struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_inode_lockres;
  2220. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  2221. mlog(0, "inode %llu drop %s META lock\n",
  2222. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  2223. ex ? "EXMODE" : "PRMODE");
  2224. if (!ocfs2_is_hard_readonly(osb) &&
  2225. !ocfs2_mount_local(osb))
  2226. ocfs2_cluster_unlock(osb, lockres, level);
  2227. }
  2228. /*
  2229. * This _tracker variants are introduced to deal with the recursive cluster
  2230. * locking issue. The idea is to keep track of a lock holder on the stack of
  2231. * the current process. If there's a lock holder on the stack, we know the
  2232. * task context is already protected by cluster locking. Currently, they're
  2233. * used in some VFS entry routines.
  2234. *
  2235. * return < 0 on error, return == 0 if there's no lock holder on the stack
  2236. * before this call, return == 1 if this call would be a recursive locking.
  2237. * return == -1 if this lock attempt will cause an upgrade which is forbidden.
  2238. *
  2239. * When taking lock levels into account,we face some different situations.
  2240. *
  2241. * 1. no lock is held
  2242. * In this case, just lock the inode as requested and return 0
  2243. *
  2244. * 2. We are holding a lock
  2245. * For this situation, things diverges into several cases
  2246. *
  2247. * wanted holding what to do
  2248. * ex ex see 2.1 below
  2249. * ex pr see 2.2 below
  2250. * pr ex see 2.1 below
  2251. * pr pr see 2.1 below
  2252. *
  2253. * 2.1 lock level that is been held is compatible
  2254. * with the wanted level, so no lock action will be tacken.
  2255. *
  2256. * 2.2 Otherwise, an upgrade is needed, but it is forbidden.
  2257. *
  2258. * Reason why upgrade within a process is forbidden is that
  2259. * lock upgrade may cause dead lock. The following illustrates
  2260. * how it happens.
  2261. *
  2262. * thread on node1 thread on node2
  2263. * ocfs2_inode_lock_tracker(ex=0)
  2264. *
  2265. * <====== ocfs2_inode_lock_tracker(ex=1)
  2266. *
  2267. * ocfs2_inode_lock_tracker(ex=1)
  2268. */
  2269. int ocfs2_inode_lock_tracker(struct inode *inode,
  2270. struct buffer_head **ret_bh,
  2271. int ex,
  2272. struct ocfs2_lock_holder *oh)
  2273. {
  2274. int status = 0;
  2275. struct ocfs2_lock_res *lockres;
  2276. struct ocfs2_lock_holder *tmp_oh;
  2277. struct pid *pid = task_pid(current);
  2278. lockres = &OCFS2_I(inode)->ip_inode_lockres;
  2279. tmp_oh = ocfs2_pid_holder(lockres, pid);
  2280. if (!tmp_oh) {
  2281. /*
  2282. * This corresponds to the case 1.
  2283. * We haven't got any lock before.
  2284. */
  2285. status = ocfs2_inode_lock_full(inode, ret_bh, ex, 0);
  2286. if (status < 0) {
  2287. if (status != -ENOENT)
  2288. mlog_errno(status);
  2289. return status;
  2290. }
  2291. oh->oh_ex = ex;
  2292. ocfs2_add_holder(lockres, oh);
  2293. return 0;
  2294. }
  2295. if (unlikely(ex && !tmp_oh->oh_ex)) {
  2296. /*
  2297. * case 2.2 upgrade may cause dead lock, forbid it.
  2298. */
  2299. mlog(ML_ERROR, "Recursive locking is not permitted to "
  2300. "upgrade to EX level from PR level.\n");
  2301. dump_stack();
  2302. return -EINVAL;
  2303. }
  2304. /*
  2305. * case 2.1 OCFS2_META_LOCK_GETBH flag make ocfs2_inode_lock_full.
  2306. * ignore the lock level and just update it.
  2307. */
  2308. if (ret_bh) {
  2309. status = ocfs2_inode_lock_full(inode, ret_bh, ex,
  2310. OCFS2_META_LOCK_GETBH);
  2311. if (status < 0) {
  2312. if (status != -ENOENT)
  2313. mlog_errno(status);
  2314. return status;
  2315. }
  2316. }
  2317. return 1;
  2318. }
  2319. void ocfs2_inode_unlock_tracker(struct inode *inode,
  2320. int ex,
  2321. struct ocfs2_lock_holder *oh,
  2322. int had_lock)
  2323. {
  2324. struct ocfs2_lock_res *lockres;
  2325. lockres = &OCFS2_I(inode)->ip_inode_lockres;
  2326. /* had_lock means that the current process already takes the cluster
  2327. * lock previously.
  2328. * If had_lock is 1, we have nothing to do here.
  2329. * If had_lock is 0, we will release the lock.
  2330. */
  2331. if (!had_lock) {
  2332. ocfs2_inode_unlock(inode, oh->oh_ex);
  2333. ocfs2_remove_holder(lockres, oh);
  2334. }
  2335. }
  2336. int ocfs2_orphan_scan_lock(struct ocfs2_super *osb, u32 *seqno)
  2337. {
  2338. struct ocfs2_lock_res *lockres;
  2339. struct ocfs2_orphan_scan_lvb *lvb;
  2340. int status = 0;
  2341. if (ocfs2_is_hard_readonly(osb))
  2342. return -EROFS;
  2343. if (ocfs2_mount_local(osb))
  2344. return 0;
  2345. lockres = &osb->osb_orphan_scan.os_lockres;
  2346. status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX, 0, 0);
  2347. if (status < 0)
  2348. return status;
  2349. lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
  2350. if (ocfs2_dlm_lvb_valid(&lockres->l_lksb) &&
  2351. lvb->lvb_version == OCFS2_ORPHAN_LVB_VERSION)
  2352. *seqno = be32_to_cpu(lvb->lvb_os_seqno);
  2353. else
  2354. *seqno = osb->osb_orphan_scan.os_seqno + 1;
  2355. return status;
  2356. }
  2357. void ocfs2_orphan_scan_unlock(struct ocfs2_super *osb, u32 seqno)
  2358. {
  2359. struct ocfs2_lock_res *lockres;
  2360. struct ocfs2_orphan_scan_lvb *lvb;
  2361. if (!ocfs2_is_hard_readonly(osb) && !ocfs2_mount_local(osb)) {
  2362. lockres = &osb->osb_orphan_scan.os_lockres;
  2363. lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
  2364. lvb->lvb_version = OCFS2_ORPHAN_LVB_VERSION;
  2365. lvb->lvb_os_seqno = cpu_to_be32(seqno);
  2366. ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
  2367. }
  2368. }
  2369. int ocfs2_super_lock(struct ocfs2_super *osb,
  2370. int ex)
  2371. {
  2372. int status = 0;
  2373. int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
  2374. struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
  2375. if (ocfs2_is_hard_readonly(osb))
  2376. return -EROFS;
  2377. if (ocfs2_mount_local(osb))
  2378. goto bail;
  2379. status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
  2380. if (status < 0) {
  2381. mlog_errno(status);
  2382. goto bail;
  2383. }
  2384. /* The super block lock path is really in the best position to
  2385. * know when resources covered by the lock need to be
  2386. * refreshed, so we do it here. Of course, making sense of
  2387. * everything is up to the caller :) */
  2388. status = ocfs2_should_refresh_lock_res(lockres);
  2389. if (status) {
  2390. status = ocfs2_refresh_slot_info(osb);
  2391. ocfs2_complete_lock_res_refresh(lockres, status);
  2392. if (status < 0) {
  2393. ocfs2_cluster_unlock(osb, lockres, level);
  2394. mlog_errno(status);
  2395. }
  2396. ocfs2_track_lock_refresh(lockres);
  2397. }
  2398. bail:
  2399. return status;
  2400. }
  2401. void ocfs2_super_unlock(struct ocfs2_super *osb,
  2402. int ex)
  2403. {
  2404. int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
  2405. struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
  2406. if (!ocfs2_mount_local(osb))
  2407. ocfs2_cluster_unlock(osb, lockres, level);
  2408. }
  2409. int ocfs2_rename_lock(struct ocfs2_super *osb)
  2410. {
  2411. int status;
  2412. struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
  2413. if (ocfs2_is_hard_readonly(osb))
  2414. return -EROFS;
  2415. if (ocfs2_mount_local(osb))
  2416. return 0;
  2417. status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX, 0, 0);
  2418. if (status < 0)
  2419. mlog_errno(status);
  2420. return status;
  2421. }
  2422. void ocfs2_rename_unlock(struct ocfs2_super *osb)
  2423. {
  2424. struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
  2425. if (!ocfs2_mount_local(osb))
  2426. ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
  2427. }
  2428. int ocfs2_nfs_sync_lock(struct ocfs2_super *osb, int ex)
  2429. {
  2430. int status;
  2431. struct ocfs2_lock_res *lockres = &osb->osb_nfs_sync_lockres;
  2432. if (ocfs2_is_hard_readonly(osb))
  2433. return -EROFS;
  2434. if (ex)
  2435. down_write(&osb->nfs_sync_rwlock);
  2436. else
  2437. down_read(&osb->nfs_sync_rwlock);
  2438. if (ocfs2_mount_local(osb))
  2439. return 0;
  2440. status = ocfs2_cluster_lock(osb, lockres, ex ? LKM_EXMODE : LKM_PRMODE,
  2441. 0, 0);
  2442. if (status < 0) {
  2443. mlog(ML_ERROR, "lock on nfs sync lock failed %d\n", status);
  2444. if (ex)
  2445. up_write(&osb->nfs_sync_rwlock);
  2446. else
  2447. up_read(&osb->nfs_sync_rwlock);
  2448. }
  2449. return status;
  2450. }
  2451. void ocfs2_nfs_sync_unlock(struct ocfs2_super *osb, int ex)
  2452. {
  2453. struct ocfs2_lock_res *lockres = &osb->osb_nfs_sync_lockres;
  2454. if (!ocfs2_mount_local(osb))
  2455. ocfs2_cluster_unlock(osb, lockres,
  2456. ex ? LKM_EXMODE : LKM_PRMODE);
  2457. if (ex)
  2458. up_write(&osb->nfs_sync_rwlock);
  2459. else
  2460. up_read(&osb->nfs_sync_rwlock);
  2461. }
  2462. int ocfs2_trim_fs_lock(struct ocfs2_super *osb,
  2463. struct ocfs2_trim_fs_info *info, int trylock)
  2464. {
  2465. int status;
  2466. struct ocfs2_trim_fs_lvb *lvb;
  2467. struct ocfs2_lock_res *lockres = &osb->osb_trim_fs_lockres;
  2468. if (info)
  2469. info->tf_valid = 0;
  2470. if (ocfs2_is_hard_readonly(osb))
  2471. return -EROFS;
  2472. if (ocfs2_mount_local(osb))
  2473. return 0;
  2474. status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX,
  2475. trylock ? DLM_LKF_NOQUEUE : 0, 0);
  2476. if (status < 0) {
  2477. if (status != -EAGAIN)
  2478. mlog_errno(status);
  2479. return status;
  2480. }
  2481. if (info) {
  2482. lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
  2483. if (ocfs2_dlm_lvb_valid(&lockres->l_lksb) &&
  2484. lvb->lvb_version == OCFS2_TRIMFS_LVB_VERSION) {
  2485. info->tf_valid = 1;
  2486. info->tf_success = lvb->lvb_success;
  2487. info->tf_nodenum = be32_to_cpu(lvb->lvb_nodenum);
  2488. info->tf_start = be64_to_cpu(lvb->lvb_start);
  2489. info->tf_len = be64_to_cpu(lvb->lvb_len);
  2490. info->tf_minlen = be64_to_cpu(lvb->lvb_minlen);
  2491. info->tf_trimlen = be64_to_cpu(lvb->lvb_trimlen);
  2492. }
  2493. }
  2494. return status;
  2495. }
  2496. void ocfs2_trim_fs_unlock(struct ocfs2_super *osb,
  2497. struct ocfs2_trim_fs_info *info)
  2498. {
  2499. struct ocfs2_trim_fs_lvb *lvb;
  2500. struct ocfs2_lock_res *lockres = &osb->osb_trim_fs_lockres;
  2501. if (ocfs2_mount_local(osb))
  2502. return;
  2503. if (info) {
  2504. lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
  2505. lvb->lvb_version = OCFS2_TRIMFS_LVB_VERSION;
  2506. lvb->lvb_success = info->tf_success;
  2507. lvb->lvb_nodenum = cpu_to_be32(info->tf_nodenum);
  2508. lvb->lvb_start = cpu_to_be64(info->tf_start);
  2509. lvb->lvb_len = cpu_to_be64(info->tf_len);
  2510. lvb->lvb_minlen = cpu_to_be64(info->tf_minlen);
  2511. lvb->lvb_trimlen = cpu_to_be64(info->tf_trimlen);
  2512. }
  2513. ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
  2514. }
  2515. int ocfs2_dentry_lock(struct dentry *dentry, int ex)
  2516. {
  2517. int ret;
  2518. int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
  2519. struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
  2520. struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
  2521. BUG_ON(!dl);
  2522. if (ocfs2_is_hard_readonly(osb)) {
  2523. if (ex)
  2524. return -EROFS;
  2525. return 0;
  2526. }
  2527. if (ocfs2_mount_local(osb))
  2528. return 0;
  2529. ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0);
  2530. if (ret < 0)
  2531. mlog_errno(ret);
  2532. return ret;
  2533. }
  2534. void ocfs2_dentry_unlock(struct dentry *dentry, int ex)
  2535. {
  2536. int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
  2537. struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
  2538. struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
  2539. if (!ocfs2_is_hard_readonly(osb) && !ocfs2_mount_local(osb))
  2540. ocfs2_cluster_unlock(osb, &dl->dl_lockres, level);
  2541. }
  2542. /* Reference counting of the dlm debug structure. We want this because
  2543. * open references on the debug inodes can live on after a mount, so
  2544. * we can't rely on the ocfs2_super to always exist. */
  2545. static void ocfs2_dlm_debug_free(struct kref *kref)
  2546. {
  2547. struct ocfs2_dlm_debug *dlm_debug;
  2548. dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
  2549. kfree(dlm_debug);
  2550. }
  2551. void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
  2552. {
  2553. if (dlm_debug)
  2554. kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
  2555. }
  2556. static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
  2557. {
  2558. kref_get(&debug->d_refcnt);
  2559. }
  2560. struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
  2561. {
  2562. struct ocfs2_dlm_debug *dlm_debug;
  2563. dlm_debug = kmalloc_obj(struct ocfs2_dlm_debug);
  2564. if (!dlm_debug) {
  2565. mlog_errno(-ENOMEM);
  2566. goto out;
  2567. }
  2568. kref_init(&dlm_debug->d_refcnt);
  2569. INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
  2570. dlm_debug->d_filter_secs = 0;
  2571. out:
  2572. return dlm_debug;
  2573. }
  2574. /* Access to this is arbitrated for us via seq_file->sem. */
  2575. struct ocfs2_dlm_seq_priv {
  2576. struct ocfs2_dlm_debug *p_dlm_debug;
  2577. struct ocfs2_lock_res p_iter_res;
  2578. struct ocfs2_lock_res p_tmp_res;
  2579. };
  2580. static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
  2581. struct ocfs2_dlm_seq_priv *priv)
  2582. {
  2583. struct ocfs2_lock_res *iter, *ret = NULL;
  2584. struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
  2585. assert_spin_locked(&ocfs2_dlm_tracking_lock);
  2586. list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
  2587. /* discover the head of the list */
  2588. if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
  2589. mlog(0, "End of list found, %p\n", ret);
  2590. break;
  2591. }
  2592. /* We track our "dummy" iteration lockres' by a NULL
  2593. * l_ops field. */
  2594. if (iter->l_ops != NULL) {
  2595. ret = iter;
  2596. break;
  2597. }
  2598. }
  2599. return ret;
  2600. }
  2601. static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
  2602. {
  2603. struct ocfs2_dlm_seq_priv *priv = m->private;
  2604. struct ocfs2_lock_res *iter;
  2605. spin_lock(&ocfs2_dlm_tracking_lock);
  2606. iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
  2607. if (iter) {
  2608. /* Since lockres' have the lifetime of their container
  2609. * (which can be inodes, ocfs2_supers, etc) we want to
  2610. * copy this out to a temporary lockres while still
  2611. * under the spinlock. Obviously after this we can't
  2612. * trust any pointers on the copy returned, but that's
  2613. * ok as the information we want isn't typically held
  2614. * in them. */
  2615. priv->p_tmp_res = *iter;
  2616. iter = &priv->p_tmp_res;
  2617. }
  2618. spin_unlock(&ocfs2_dlm_tracking_lock);
  2619. return iter;
  2620. }
  2621. static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
  2622. {
  2623. }
  2624. static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
  2625. {
  2626. struct ocfs2_dlm_seq_priv *priv = m->private;
  2627. struct ocfs2_lock_res *iter = v;
  2628. struct ocfs2_lock_res *dummy = &priv->p_iter_res;
  2629. (*pos)++;
  2630. spin_lock(&ocfs2_dlm_tracking_lock);
  2631. iter = ocfs2_dlm_next_res(iter, priv);
  2632. list_del_init(&dummy->l_debug_list);
  2633. if (iter) {
  2634. list_add(&dummy->l_debug_list, &iter->l_debug_list);
  2635. priv->p_tmp_res = *iter;
  2636. iter = &priv->p_tmp_res;
  2637. }
  2638. spin_unlock(&ocfs2_dlm_tracking_lock);
  2639. return iter;
  2640. }
  2641. /*
  2642. * Version is used by debugfs.ocfs2 to determine the format being used
  2643. *
  2644. * New in version 2
  2645. * - Lock stats printed
  2646. * New in version 3
  2647. * - Max time in lock stats is in usecs (instead of nsecs)
  2648. * New in version 4
  2649. * - Add last pr/ex unlock times and first lock wait time in usecs
  2650. */
  2651. #define OCFS2_DLM_DEBUG_STR_VERSION 4
  2652. static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
  2653. {
  2654. int i;
  2655. char *lvb;
  2656. struct ocfs2_lock_res *lockres = v;
  2657. #ifdef CONFIG_OCFS2_FS_STATS
  2658. u64 now, last;
  2659. struct ocfs2_dlm_debug *dlm_debug =
  2660. ((struct ocfs2_dlm_seq_priv *)m->private)->p_dlm_debug;
  2661. #endif
  2662. if (!lockres)
  2663. return -EINVAL;
  2664. #ifdef CONFIG_OCFS2_FS_STATS
  2665. if (!lockres->l_lock_wait && dlm_debug->d_filter_secs) {
  2666. now = ktime_to_us(ktime_get_real());
  2667. last = max(lockres->l_lock_prmode.ls_last,
  2668. lockres->l_lock_exmode.ls_last);
  2669. /*
  2670. * Use d_filter_secs field to filter lock resources dump,
  2671. * the default d_filter_secs(0) value filters nothing,
  2672. * otherwise, only dump the last N seconds active lock
  2673. * resources.
  2674. */
  2675. if (div_u64(now - last, 1000000) > dlm_debug->d_filter_secs)
  2676. return 0;
  2677. }
  2678. #endif
  2679. seq_printf(m, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION);
  2680. if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY)
  2681. seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1,
  2682. lockres->l_name,
  2683. (unsigned int)ocfs2_get_dentry_lock_ino(lockres));
  2684. else
  2685. seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name);
  2686. seq_printf(m, "%d\t"
  2687. "0x%lx\t"
  2688. "0x%x\t"
  2689. "0x%x\t"
  2690. "%u\t"
  2691. "%u\t"
  2692. "%d\t"
  2693. "%d\t",
  2694. lockres->l_level,
  2695. lockres->l_flags,
  2696. lockres->l_action,
  2697. lockres->l_unlock_action,
  2698. lockres->l_ro_holders,
  2699. lockres->l_ex_holders,
  2700. lockres->l_requested,
  2701. lockres->l_blocking);
  2702. /* Dump the raw LVB */
  2703. lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
  2704. for(i = 0; i < DLM_LVB_LEN; i++)
  2705. seq_printf(m, "0x%x\t", lvb[i]);
  2706. #ifdef CONFIG_OCFS2_FS_STATS
  2707. # define lock_num_prmode(_l) ((_l)->l_lock_prmode.ls_gets)
  2708. # define lock_num_exmode(_l) ((_l)->l_lock_exmode.ls_gets)
  2709. # define lock_num_prmode_failed(_l) ((_l)->l_lock_prmode.ls_fail)
  2710. # define lock_num_exmode_failed(_l) ((_l)->l_lock_exmode.ls_fail)
  2711. # define lock_total_prmode(_l) ((_l)->l_lock_prmode.ls_total)
  2712. # define lock_total_exmode(_l) ((_l)->l_lock_exmode.ls_total)
  2713. # define lock_max_prmode(_l) ((_l)->l_lock_prmode.ls_max)
  2714. # define lock_max_exmode(_l) ((_l)->l_lock_exmode.ls_max)
  2715. # define lock_refresh(_l) ((_l)->l_lock_refresh)
  2716. # define lock_last_prmode(_l) ((_l)->l_lock_prmode.ls_last)
  2717. # define lock_last_exmode(_l) ((_l)->l_lock_exmode.ls_last)
  2718. # define lock_wait(_l) ((_l)->l_lock_wait)
  2719. #else
  2720. # define lock_num_prmode(_l) (0)
  2721. # define lock_num_exmode(_l) (0)
  2722. # define lock_num_prmode_failed(_l) (0)
  2723. # define lock_num_exmode_failed(_l) (0)
  2724. # define lock_total_prmode(_l) (0ULL)
  2725. # define lock_total_exmode(_l) (0ULL)
  2726. # define lock_max_prmode(_l) (0)
  2727. # define lock_max_exmode(_l) (0)
  2728. # define lock_refresh(_l) (0)
  2729. # define lock_last_prmode(_l) (0ULL)
  2730. # define lock_last_exmode(_l) (0ULL)
  2731. # define lock_wait(_l) (0ULL)
  2732. #endif
  2733. /* The following seq_print was added in version 2 of this output */
  2734. seq_printf(m, "%u\t"
  2735. "%u\t"
  2736. "%u\t"
  2737. "%u\t"
  2738. "%llu\t"
  2739. "%llu\t"
  2740. "%u\t"
  2741. "%u\t"
  2742. "%u\t"
  2743. "%llu\t"
  2744. "%llu\t"
  2745. "%llu\t",
  2746. lock_num_prmode(lockres),
  2747. lock_num_exmode(lockres),
  2748. lock_num_prmode_failed(lockres),
  2749. lock_num_exmode_failed(lockres),
  2750. lock_total_prmode(lockres),
  2751. lock_total_exmode(lockres),
  2752. lock_max_prmode(lockres),
  2753. lock_max_exmode(lockres),
  2754. lock_refresh(lockres),
  2755. lock_last_prmode(lockres),
  2756. lock_last_exmode(lockres),
  2757. lock_wait(lockres));
  2758. /* End the line */
  2759. seq_printf(m, "\n");
  2760. return 0;
  2761. }
  2762. static const struct seq_operations ocfs2_dlm_seq_ops = {
  2763. .start = ocfs2_dlm_seq_start,
  2764. .stop = ocfs2_dlm_seq_stop,
  2765. .next = ocfs2_dlm_seq_next,
  2766. .show = ocfs2_dlm_seq_show,
  2767. };
  2768. static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
  2769. {
  2770. struct seq_file *seq = file->private_data;
  2771. struct ocfs2_dlm_seq_priv *priv = seq->private;
  2772. struct ocfs2_lock_res *res = &priv->p_iter_res;
  2773. ocfs2_remove_lockres_tracking(res);
  2774. ocfs2_put_dlm_debug(priv->p_dlm_debug);
  2775. return seq_release_private(inode, file);
  2776. }
  2777. static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
  2778. {
  2779. struct ocfs2_dlm_seq_priv *priv;
  2780. struct ocfs2_super *osb;
  2781. priv = __seq_open_private(file, &ocfs2_dlm_seq_ops, sizeof(*priv));
  2782. if (!priv) {
  2783. mlog_errno(-ENOMEM);
  2784. return -ENOMEM;
  2785. }
  2786. osb = inode->i_private;
  2787. ocfs2_get_dlm_debug(osb->osb_dlm_debug);
  2788. priv->p_dlm_debug = osb->osb_dlm_debug;
  2789. INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
  2790. ocfs2_add_lockres_tracking(&priv->p_iter_res,
  2791. priv->p_dlm_debug);
  2792. return 0;
  2793. }
  2794. static const struct file_operations ocfs2_dlm_debug_fops = {
  2795. .open = ocfs2_dlm_debug_open,
  2796. .release = ocfs2_dlm_debug_release,
  2797. .read = seq_read,
  2798. .llseek = seq_lseek,
  2799. };
  2800. static void ocfs2_dlm_init_debug(struct ocfs2_super *osb)
  2801. {
  2802. struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
  2803. debugfs_create_file("locking_state", S_IFREG|S_IRUSR,
  2804. osb->osb_debug_root, osb, &ocfs2_dlm_debug_fops);
  2805. debugfs_create_u32("locking_filter", 0600, osb->osb_debug_root,
  2806. &dlm_debug->d_filter_secs);
  2807. ocfs2_get_dlm_debug(dlm_debug);
  2808. }
  2809. static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
  2810. {
  2811. struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
  2812. if (dlm_debug)
  2813. ocfs2_put_dlm_debug(dlm_debug);
  2814. }
  2815. int ocfs2_dlm_init(struct ocfs2_super *osb)
  2816. {
  2817. int status = 0;
  2818. struct ocfs2_cluster_connection *conn = NULL;
  2819. if (ocfs2_mount_local(osb)) {
  2820. osb->node_num = 0;
  2821. goto local;
  2822. }
  2823. ocfs2_dlm_init_debug(osb);
  2824. /* launch downconvert thread */
  2825. osb->dc_task = kthread_run(ocfs2_downconvert_thread, osb, "ocfs2dc-%s",
  2826. osb->uuid_str);
  2827. if (IS_ERR(osb->dc_task)) {
  2828. status = PTR_ERR(osb->dc_task);
  2829. osb->dc_task = NULL;
  2830. mlog_errno(status);
  2831. goto bail;
  2832. }
  2833. /* for now, uuid == domain */
  2834. status = ocfs2_cluster_connect(osb->osb_cluster_stack,
  2835. osb->osb_cluster_name,
  2836. strlen(osb->osb_cluster_name),
  2837. osb->uuid_str,
  2838. strlen(osb->uuid_str),
  2839. &lproto, ocfs2_do_node_down, osb,
  2840. &conn);
  2841. if (status) {
  2842. mlog_errno(status);
  2843. goto bail;
  2844. }
  2845. status = ocfs2_cluster_this_node(conn, &osb->node_num);
  2846. if (status < 0) {
  2847. mlog_errno(status);
  2848. mlog(ML_ERROR,
  2849. "could not find this host's node number\n");
  2850. ocfs2_cluster_disconnect(conn, 0);
  2851. goto bail;
  2852. }
  2853. local:
  2854. ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
  2855. ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
  2856. ocfs2_nfs_sync_lock_init(osb);
  2857. ocfs2_orphan_scan_lock_res_init(&osb->osb_orphan_scan.os_lockres, osb);
  2858. osb->cconn = conn;
  2859. bail:
  2860. if (status < 0) {
  2861. ocfs2_dlm_shutdown_debug(osb);
  2862. if (osb->dc_task)
  2863. kthread_stop(osb->dc_task);
  2864. }
  2865. return status;
  2866. }
  2867. void ocfs2_dlm_shutdown(struct ocfs2_super *osb,
  2868. int hangup_pending)
  2869. {
  2870. ocfs2_drop_osb_locks(osb);
  2871. /*
  2872. * Now that we have dropped all locks and ocfs2_dismount_volume()
  2873. * has disabled recovery, the DLM won't be talking to us. It's
  2874. * safe to tear things down before disconnecting the cluster.
  2875. */
  2876. if (osb->dc_task) {
  2877. kthread_stop(osb->dc_task);
  2878. osb->dc_task = NULL;
  2879. }
  2880. ocfs2_lock_res_free(&osb->osb_super_lockres);
  2881. ocfs2_lock_res_free(&osb->osb_rename_lockres);
  2882. ocfs2_lock_res_free(&osb->osb_nfs_sync_lockres);
  2883. ocfs2_lock_res_free(&osb->osb_orphan_scan.os_lockres);
  2884. if (osb->cconn) {
  2885. ocfs2_cluster_disconnect(osb->cconn, hangup_pending);
  2886. osb->cconn = NULL;
  2887. ocfs2_dlm_shutdown_debug(osb);
  2888. }
  2889. }
  2890. static int ocfs2_drop_lock(struct ocfs2_super *osb,
  2891. struct ocfs2_lock_res *lockres)
  2892. {
  2893. int ret;
  2894. unsigned long flags;
  2895. u32 lkm_flags = 0;
  2896. /* We didn't get anywhere near actually using this lockres. */
  2897. if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
  2898. goto out;
  2899. if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
  2900. lkm_flags |= DLM_LKF_VALBLK;
  2901. spin_lock_irqsave(&lockres->l_lock, flags);
  2902. mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
  2903. "lockres %s, flags 0x%lx\n",
  2904. lockres->l_name, lockres->l_flags);
  2905. while (lockres->l_flags & OCFS2_LOCK_BUSY) {
  2906. mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
  2907. "%u, unlock_action = %u\n",
  2908. lockres->l_name, lockres->l_flags, lockres->l_action,
  2909. lockres->l_unlock_action);
  2910. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2911. /* XXX: Today we just wait on any busy
  2912. * locks... Perhaps we need to cancel converts in the
  2913. * future? */
  2914. ocfs2_wait_on_busy_lock(lockres);
  2915. spin_lock_irqsave(&lockres->l_lock, flags);
  2916. }
  2917. if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
  2918. if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
  2919. lockres->l_level == DLM_LOCK_EX &&
  2920. !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
  2921. lockres->l_ops->set_lvb(lockres);
  2922. }
  2923. if (lockres->l_flags & OCFS2_LOCK_BUSY)
  2924. mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
  2925. lockres->l_name);
  2926. if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
  2927. mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
  2928. if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
  2929. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2930. goto out;
  2931. }
  2932. lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
  2933. /* make sure we never get here while waiting for an ast to
  2934. * fire. */
  2935. BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
  2936. /* is this necessary? */
  2937. lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
  2938. lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
  2939. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2940. mlog(0, "lock %s\n", lockres->l_name);
  2941. ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb, lkm_flags);
  2942. if (ret) {
  2943. ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
  2944. mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
  2945. ocfs2_dlm_dump_lksb(&lockres->l_lksb);
  2946. BUG();
  2947. }
  2948. mlog(0, "lock %s, successful return from ocfs2_dlm_unlock\n",
  2949. lockres->l_name);
  2950. ocfs2_wait_on_busy_lock(lockres);
  2951. out:
  2952. return 0;
  2953. }
  2954. static void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
  2955. struct ocfs2_lock_res *lockres);
  2956. /* Mark the lockres as being dropped. It will no longer be
  2957. * queued if blocking, but we still may have to wait on it
  2958. * being dequeued from the downconvert thread before we can consider
  2959. * it safe to drop.
  2960. *
  2961. * You can *not* attempt to call cluster_lock on this lockres anymore. */
  2962. void ocfs2_mark_lockres_freeing(struct ocfs2_super *osb,
  2963. struct ocfs2_lock_res *lockres)
  2964. {
  2965. int status;
  2966. struct ocfs2_mask_waiter mw;
  2967. unsigned long flags, flags2;
  2968. ocfs2_init_mask_waiter(&mw);
  2969. spin_lock_irqsave(&lockres->l_lock, flags);
  2970. lockres->l_flags |= OCFS2_LOCK_FREEING;
  2971. if (lockres->l_flags & OCFS2_LOCK_QUEUED && current == osb->dc_task) {
  2972. /*
  2973. * We know the downconvert is queued but not in progress
  2974. * because we are the downconvert thread and processing
  2975. * different lock. So we can just remove the lock from the
  2976. * queue. This is not only an optimization but also a way
  2977. * to avoid the following deadlock:
  2978. * ocfs2_dentry_post_unlock()
  2979. * ocfs2_dentry_lock_put()
  2980. * ocfs2_drop_dentry_lock()
  2981. * iput()
  2982. * ocfs2_evict_inode()
  2983. * ocfs2_clear_inode()
  2984. * ocfs2_mark_lockres_freeing()
  2985. * ... blocks waiting for OCFS2_LOCK_QUEUED
  2986. * since we are the downconvert thread which
  2987. * should clear the flag.
  2988. */
  2989. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2990. spin_lock_irqsave(&osb->dc_task_lock, flags2);
  2991. list_del_init(&lockres->l_blocked_list);
  2992. osb->blocked_lock_count--;
  2993. spin_unlock_irqrestore(&osb->dc_task_lock, flags2);
  2994. /*
  2995. * Warn if we recurse into another post_unlock call. Strictly
  2996. * speaking it isn't a problem but we need to be careful if
  2997. * that happens (stack overflow, deadlocks, ...) so warn if
  2998. * ocfs2 grows a path for which this can happen.
  2999. */
  3000. WARN_ON_ONCE(lockres->l_ops->post_unlock);
  3001. /* Since the lock is freeing we don't do much in the fn below */
  3002. ocfs2_process_blocked_lock(osb, lockres);
  3003. return;
  3004. }
  3005. while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
  3006. lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
  3007. spin_unlock_irqrestore(&lockres->l_lock, flags);
  3008. mlog(0, "Waiting on lockres %s\n", lockres->l_name);
  3009. status = ocfs2_wait_for_mask(&mw);
  3010. if (status)
  3011. mlog_errno(status);
  3012. spin_lock_irqsave(&lockres->l_lock, flags);
  3013. }
  3014. spin_unlock_irqrestore(&lockres->l_lock, flags);
  3015. }
  3016. void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
  3017. struct ocfs2_lock_res *lockres)
  3018. {
  3019. int ret;
  3020. ocfs2_mark_lockres_freeing(osb, lockres);
  3021. ret = ocfs2_drop_lock(osb, lockres);
  3022. if (ret)
  3023. mlog_errno(ret);
  3024. }
  3025. static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
  3026. {
  3027. ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres);
  3028. ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres);
  3029. ocfs2_simple_drop_lockres(osb, &osb->osb_nfs_sync_lockres);
  3030. ocfs2_simple_drop_lockres(osb, &osb->osb_orphan_scan.os_lockres);
  3031. }
  3032. int ocfs2_drop_inode_locks(struct inode *inode)
  3033. {
  3034. int status, err;
  3035. /* No need to call ocfs2_mark_lockres_freeing here -
  3036. * ocfs2_clear_inode has done it for us. */
  3037. err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
  3038. &OCFS2_I(inode)->ip_open_lockres);
  3039. if (err < 0)
  3040. mlog_errno(err);
  3041. status = err;
  3042. err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
  3043. &OCFS2_I(inode)->ip_inode_lockres);
  3044. if (err < 0)
  3045. mlog_errno(err);
  3046. if (err < 0 && !status)
  3047. status = err;
  3048. err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
  3049. &OCFS2_I(inode)->ip_rw_lockres);
  3050. if (err < 0)
  3051. mlog_errno(err);
  3052. if (err < 0 && !status)
  3053. status = err;
  3054. return status;
  3055. }
  3056. static unsigned int ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
  3057. int new_level)
  3058. {
  3059. assert_spin_locked(&lockres->l_lock);
  3060. BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
  3061. if (lockres->l_level <= new_level) {
  3062. mlog(ML_ERROR, "lockres %s, lvl %d <= %d, blcklst %d, mask %d, "
  3063. "type %d, flags 0x%lx, hold %d %d, act %d %d, req %d, "
  3064. "block %d, pgen %d\n", lockres->l_name, lockres->l_level,
  3065. new_level, list_empty(&lockres->l_blocked_list),
  3066. list_empty(&lockres->l_mask_waiters), lockres->l_type,
  3067. lockres->l_flags, lockres->l_ro_holders,
  3068. lockres->l_ex_holders, lockres->l_action,
  3069. lockres->l_unlock_action, lockres->l_requested,
  3070. lockres->l_blocking, lockres->l_pending_gen);
  3071. BUG();
  3072. }
  3073. mlog(ML_BASTS, "lockres %s, level %d => %d, blocking %d\n",
  3074. lockres->l_name, lockres->l_level, new_level, lockres->l_blocking);
  3075. lockres->l_action = OCFS2_AST_DOWNCONVERT;
  3076. lockres->l_requested = new_level;
  3077. lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
  3078. return lockres_set_pending(lockres);
  3079. }
  3080. static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
  3081. struct ocfs2_lock_res *lockres,
  3082. int new_level,
  3083. int lvb,
  3084. unsigned int generation)
  3085. {
  3086. int ret;
  3087. u32 dlm_flags = DLM_LKF_CONVERT;
  3088. mlog(ML_BASTS, "lockres %s, level %d => %d\n", lockres->l_name,
  3089. lockres->l_level, new_level);
  3090. /*
  3091. * On DLM_LKF_VALBLK, fsdlm behaves differently with o2cb. It always
  3092. * expects DLM_LKF_VALBLK being set if the LKB has LVB, so that
  3093. * we can recover correctly from node failure. Otherwise, we may get
  3094. * invalid LVB in LKB, but without DLM_SBF_VALNOTVALID being set.
  3095. */
  3096. if (ocfs2_userspace_stack(osb) &&
  3097. lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
  3098. lvb = 1;
  3099. if (lvb)
  3100. dlm_flags |= DLM_LKF_VALBLK;
  3101. ret = ocfs2_dlm_lock(osb->cconn,
  3102. new_level,
  3103. &lockres->l_lksb,
  3104. dlm_flags,
  3105. lockres->l_name,
  3106. OCFS2_LOCK_ID_MAX_LEN - 1);
  3107. lockres_clear_pending(lockres, generation, osb);
  3108. if (ret) {
  3109. ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
  3110. ocfs2_recover_from_dlm_error(lockres, 1);
  3111. goto bail;
  3112. }
  3113. ret = 0;
  3114. bail:
  3115. return ret;
  3116. }
  3117. /* returns 1 when the caller should unlock and call ocfs2_dlm_unlock */
  3118. static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
  3119. struct ocfs2_lock_res *lockres)
  3120. {
  3121. assert_spin_locked(&lockres->l_lock);
  3122. if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
  3123. /* If we're already trying to cancel a lock conversion
  3124. * then just drop the spinlock and allow the caller to
  3125. * requeue this lock. */
  3126. mlog(ML_BASTS, "lockres %s, skip convert\n", lockres->l_name);
  3127. return 0;
  3128. }
  3129. /* were we in a convert when we got the bast fire? */
  3130. BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
  3131. lockres->l_action != OCFS2_AST_DOWNCONVERT);
  3132. /* set things up for the unlockast to know to just
  3133. * clear out the ast_action and unset busy, etc. */
  3134. lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
  3135. mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
  3136. "lock %s, invalid flags: 0x%lx\n",
  3137. lockres->l_name, lockres->l_flags);
  3138. mlog(ML_BASTS, "lockres %s\n", lockres->l_name);
  3139. return 1;
  3140. }
  3141. static int ocfs2_cancel_convert(struct ocfs2_super *osb,
  3142. struct ocfs2_lock_res *lockres)
  3143. {
  3144. int ret;
  3145. ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb,
  3146. DLM_LKF_CANCEL);
  3147. if (ret) {
  3148. ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
  3149. ocfs2_recover_from_dlm_error(lockres, 0);
  3150. }
  3151. mlog(ML_BASTS, "lockres %s\n", lockres->l_name);
  3152. return ret;
  3153. }
  3154. static int ocfs2_unblock_lock(struct ocfs2_super *osb,
  3155. struct ocfs2_lock_res *lockres,
  3156. struct ocfs2_unblock_ctl *ctl)
  3157. {
  3158. unsigned long flags;
  3159. int blocking;
  3160. int new_level;
  3161. int level;
  3162. int ret = 0;
  3163. int set_lvb = 0;
  3164. unsigned int gen;
  3165. spin_lock_irqsave(&lockres->l_lock, flags);
  3166. recheck:
  3167. /*
  3168. * Is it still blocking? If not, we have no more work to do.
  3169. */
  3170. if (!(lockres->l_flags & OCFS2_LOCK_BLOCKED)) {
  3171. BUG_ON(lockres->l_blocking != DLM_LOCK_NL);
  3172. spin_unlock_irqrestore(&lockres->l_lock, flags);
  3173. ret = 0;
  3174. goto leave;
  3175. }
  3176. if (lockres->l_flags & OCFS2_LOCK_BUSY) {
  3177. /* XXX
  3178. * This is a *big* race. The OCFS2_LOCK_PENDING flag
  3179. * exists entirely for one reason - another thread has set
  3180. * OCFS2_LOCK_BUSY, but has *NOT* yet called dlm_lock().
  3181. *
  3182. * If we do ocfs2_cancel_convert() before the other thread
  3183. * calls dlm_lock(), our cancel will do nothing. We will
  3184. * get no ast, and we will have no way of knowing the
  3185. * cancel failed. Meanwhile, the other thread will call
  3186. * into dlm_lock() and wait...forever.
  3187. *
  3188. * Why forever? Because another node has asked for the
  3189. * lock first; that's why we're here in unblock_lock().
  3190. *
  3191. * The solution is OCFS2_LOCK_PENDING. When PENDING is
  3192. * set, we just requeue the unblock. Only when the other
  3193. * thread has called dlm_lock() and cleared PENDING will
  3194. * we then cancel their request.
  3195. *
  3196. * All callers of dlm_lock() must set OCFS2_DLM_PENDING
  3197. * at the same time they set OCFS2_DLM_BUSY. They must
  3198. * clear OCFS2_DLM_PENDING after dlm_lock() returns.
  3199. */
  3200. if (lockres->l_flags & OCFS2_LOCK_PENDING) {
  3201. mlog(ML_BASTS, "lockres %s, ReQ: Pending\n",
  3202. lockres->l_name);
  3203. goto leave_requeue;
  3204. }
  3205. ctl->requeue = 1;
  3206. ret = ocfs2_prepare_cancel_convert(osb, lockres);
  3207. spin_unlock_irqrestore(&lockres->l_lock, flags);
  3208. if (ret) {
  3209. ret = ocfs2_cancel_convert(osb, lockres);
  3210. if (ret < 0)
  3211. mlog_errno(ret);
  3212. }
  3213. goto leave;
  3214. }
  3215. /*
  3216. * This prevents livelocks. OCFS2_LOCK_UPCONVERT_FINISHING flag is
  3217. * set when the ast is received for an upconvert just before the
  3218. * OCFS2_LOCK_BUSY flag is cleared. Now if the fs received a bast
  3219. * on the heels of the ast, we want to delay the downconvert just
  3220. * enough to allow the up requester to do its task. Because this
  3221. * lock is in the blocked queue, the lock will be downconverted
  3222. * as soon as the requester is done with the lock.
  3223. */
  3224. if (lockres->l_flags & OCFS2_LOCK_UPCONVERT_FINISHING)
  3225. goto leave_requeue;
  3226. /*
  3227. * How can we block and yet be at NL? We were trying to upconvert
  3228. * from NL and got canceled. The code comes back here, and now
  3229. * we notice and clear BLOCKING.
  3230. */
  3231. if (lockres->l_level == DLM_LOCK_NL) {
  3232. BUG_ON(lockres->l_ex_holders || lockres->l_ro_holders);
  3233. mlog(ML_BASTS, "lockres %s, Aborting dc\n", lockres->l_name);
  3234. lockres->l_blocking = DLM_LOCK_NL;
  3235. lockres_clear_flags(lockres, OCFS2_LOCK_BLOCKED);
  3236. spin_unlock_irqrestore(&lockres->l_lock, flags);
  3237. goto leave;
  3238. }
  3239. /* if we're blocking an exclusive and we have *any* holders,
  3240. * then requeue. */
  3241. if ((lockres->l_blocking == DLM_LOCK_EX)
  3242. && (lockres->l_ex_holders || lockres->l_ro_holders)) {
  3243. mlog(ML_BASTS, "lockres %s, ReQ: EX/PR Holders %u,%u\n",
  3244. lockres->l_name, lockres->l_ex_holders,
  3245. lockres->l_ro_holders);
  3246. goto leave_requeue;
  3247. }
  3248. /* If it's a PR we're blocking, then only
  3249. * requeue if we've got any EX holders */
  3250. if (lockres->l_blocking == DLM_LOCK_PR &&
  3251. lockres->l_ex_holders) {
  3252. mlog(ML_BASTS, "lockres %s, ReQ: EX Holders %u\n",
  3253. lockres->l_name, lockres->l_ex_holders);
  3254. goto leave_requeue;
  3255. }
  3256. /*
  3257. * Can we get a lock in this state if the holder counts are
  3258. * zero? The meta data unblock code used to check this.
  3259. */
  3260. if ((lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
  3261. && (lockres->l_flags & OCFS2_LOCK_REFRESHING)) {
  3262. mlog(ML_BASTS, "lockres %s, ReQ: Lock Refreshing\n",
  3263. lockres->l_name);
  3264. goto leave_requeue;
  3265. }
  3266. new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
  3267. if (lockres->l_ops->check_downconvert
  3268. && !lockres->l_ops->check_downconvert(lockres, new_level)) {
  3269. mlog(ML_BASTS, "lockres %s, ReQ: Checkpointing\n",
  3270. lockres->l_name);
  3271. goto leave_requeue;
  3272. }
  3273. /* If we get here, then we know that there are no more
  3274. * incompatible holders (and anyone asking for an incompatible
  3275. * lock is blocked). We can now downconvert the lock */
  3276. if (!lockres->l_ops->downconvert_worker)
  3277. goto downconvert;
  3278. /* Some lockres types want to do a bit of work before
  3279. * downconverting a lock. Allow that here. The worker function
  3280. * may sleep, so we save off a copy of what we're blocking as
  3281. * it may change while we're not holding the spin lock. */
  3282. blocking = lockres->l_blocking;
  3283. level = lockres->l_level;
  3284. spin_unlock_irqrestore(&lockres->l_lock, flags);
  3285. ctl->unblock_action = lockres->l_ops->downconvert_worker(lockres, blocking);
  3286. if (ctl->unblock_action == UNBLOCK_STOP_POST) {
  3287. mlog(ML_BASTS, "lockres %s, UNBLOCK_STOP_POST\n",
  3288. lockres->l_name);
  3289. goto leave;
  3290. }
  3291. spin_lock_irqsave(&lockres->l_lock, flags);
  3292. if ((blocking != lockres->l_blocking) || (level != lockres->l_level)) {
  3293. /* If this changed underneath us, then we can't drop
  3294. * it just yet. */
  3295. mlog(ML_BASTS, "lockres %s, block=%d:%d, level=%d:%d, "
  3296. "Recheck\n", lockres->l_name, blocking,
  3297. lockres->l_blocking, level, lockres->l_level);
  3298. goto recheck;
  3299. }
  3300. downconvert:
  3301. ctl->requeue = 0;
  3302. if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
  3303. if (lockres->l_level == DLM_LOCK_EX)
  3304. set_lvb = 1;
  3305. /*
  3306. * We only set the lvb if the lock has been fully
  3307. * refreshed - otherwise we risk setting stale
  3308. * data. Otherwise, there's no need to actually clear
  3309. * out the lvb here as it's value is still valid.
  3310. */
  3311. if (set_lvb && !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
  3312. lockres->l_ops->set_lvb(lockres);
  3313. }
  3314. gen = ocfs2_prepare_downconvert(lockres, new_level);
  3315. spin_unlock_irqrestore(&lockres->l_lock, flags);
  3316. ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb,
  3317. gen);
  3318. /* The dlm lock convert is being cancelled in background,
  3319. * ocfs2_cancel_convert() is asynchronous in fs/dlm,
  3320. * requeue it, try again later.
  3321. */
  3322. if (ret == -EBUSY) {
  3323. ctl->requeue = 1;
  3324. mlog(ML_BASTS, "lockres %s, ReQ: Downconvert busy\n",
  3325. lockres->l_name);
  3326. ret = 0;
  3327. msleep(20);
  3328. }
  3329. leave:
  3330. if (ret)
  3331. mlog_errno(ret);
  3332. return ret;
  3333. leave_requeue:
  3334. spin_unlock_irqrestore(&lockres->l_lock, flags);
  3335. ctl->requeue = 1;
  3336. return 0;
  3337. }
  3338. static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
  3339. int blocking)
  3340. {
  3341. struct inode *inode;
  3342. struct address_space *mapping;
  3343. struct ocfs2_inode_info *oi;
  3344. inode = ocfs2_lock_res_inode(lockres);
  3345. mapping = inode->i_mapping;
  3346. if (S_ISDIR(inode->i_mode)) {
  3347. oi = OCFS2_I(inode);
  3348. oi->ip_dir_lock_gen++;
  3349. mlog(0, "generation: %u\n", oi->ip_dir_lock_gen);
  3350. goto out_forget;
  3351. }
  3352. if (!S_ISREG(inode->i_mode))
  3353. goto out;
  3354. /*
  3355. * We need this before the filemap_fdatawrite() so that it can
  3356. * transfer the dirty bit from the PTE to the
  3357. * page. Unfortunately this means that even for EX->PR
  3358. * downconverts, we'll lose our mappings and have to build
  3359. * them up again.
  3360. */
  3361. unmap_mapping_range(mapping, 0, 0, 0);
  3362. if (filemap_fdatawrite(mapping)) {
  3363. mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
  3364. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  3365. }
  3366. sync_mapping_buffers(mapping);
  3367. if (blocking == DLM_LOCK_EX) {
  3368. truncate_inode_pages(mapping, 0);
  3369. } else {
  3370. /* We only need to wait on the I/O if we're not also
  3371. * truncating pages because truncate_inode_pages waits
  3372. * for us above. We don't truncate pages if we're
  3373. * blocking anything < EXMODE because we want to keep
  3374. * them around in that case. */
  3375. filemap_fdatawait(mapping);
  3376. }
  3377. out_forget:
  3378. forget_all_cached_acls(inode);
  3379. out:
  3380. return UNBLOCK_CONTINUE;
  3381. }
  3382. static int ocfs2_ci_checkpointed(struct ocfs2_caching_info *ci,
  3383. struct ocfs2_lock_res *lockres,
  3384. int new_level)
  3385. {
  3386. int checkpointed = ocfs2_ci_fully_checkpointed(ci);
  3387. BUG_ON(new_level != DLM_LOCK_NL && new_level != DLM_LOCK_PR);
  3388. BUG_ON(lockres->l_level != DLM_LOCK_EX && !checkpointed);
  3389. if (checkpointed)
  3390. return 1;
  3391. ocfs2_start_checkpoint(OCFS2_SB(ocfs2_metadata_cache_get_super(ci)));
  3392. return 0;
  3393. }
  3394. static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
  3395. int new_level)
  3396. {
  3397. struct inode *inode = ocfs2_lock_res_inode(lockres);
  3398. return ocfs2_ci_checkpointed(INODE_CACHE(inode), lockres, new_level);
  3399. }
  3400. static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres)
  3401. {
  3402. struct inode *inode = ocfs2_lock_res_inode(lockres);
  3403. __ocfs2_stuff_meta_lvb(inode);
  3404. }
  3405. /*
  3406. * Does the final reference drop on our dentry lock. Right now this
  3407. * happens in the downconvert thread, but we could choose to simplify the
  3408. * dlmglue API and push these off to the ocfs2_wq in the future.
  3409. */
  3410. static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
  3411. struct ocfs2_lock_res *lockres)
  3412. {
  3413. struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
  3414. ocfs2_dentry_lock_put(osb, dl);
  3415. }
  3416. /*
  3417. * d_delete() matching dentries before the lock downconvert.
  3418. *
  3419. * At this point, any process waiting to destroy the
  3420. * dentry_lock due to last ref count is stopped by the
  3421. * OCFS2_LOCK_QUEUED flag.
  3422. *
  3423. * We have two potential problems
  3424. *
  3425. * 1) If we do the last reference drop on our dentry_lock (via dput)
  3426. * we'll wind up in ocfs2_release_dentry_lock(), waiting on
  3427. * the downconvert to finish. Instead we take an elevated
  3428. * reference and push the drop until after we've completed our
  3429. * unblock processing.
  3430. *
  3431. * 2) There might be another process with a final reference,
  3432. * waiting on us to finish processing. If this is the case, we
  3433. * detect it and exit out - there's no more dentries anyway.
  3434. */
  3435. static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
  3436. int blocking)
  3437. {
  3438. struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
  3439. struct ocfs2_inode_info *oi = OCFS2_I(dl->dl_inode);
  3440. struct dentry *dentry;
  3441. unsigned long flags;
  3442. int extra_ref = 0;
  3443. /*
  3444. * This node is blocking another node from getting a read
  3445. * lock. This happens when we've renamed within a
  3446. * directory. We've forced the other nodes to d_delete(), but
  3447. * we never actually dropped our lock because it's still
  3448. * valid. The downconvert code will retain a PR for this node,
  3449. * so there's no further work to do.
  3450. */
  3451. if (blocking == DLM_LOCK_PR)
  3452. return UNBLOCK_CONTINUE;
  3453. /*
  3454. * Mark this inode as potentially orphaned. The code in
  3455. * ocfs2_delete_inode() will figure out whether it actually
  3456. * needs to be freed or not.
  3457. */
  3458. spin_lock(&oi->ip_lock);
  3459. oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
  3460. spin_unlock(&oi->ip_lock);
  3461. /*
  3462. * Yuck. We need to make sure however that the check of
  3463. * OCFS2_LOCK_FREEING and the extra reference are atomic with
  3464. * respect to a reference decrement or the setting of that
  3465. * flag.
  3466. */
  3467. spin_lock_irqsave(&lockres->l_lock, flags);
  3468. spin_lock(&dentry_attach_lock);
  3469. if (!(lockres->l_flags & OCFS2_LOCK_FREEING)
  3470. && dl->dl_count) {
  3471. dl->dl_count++;
  3472. extra_ref = 1;
  3473. }
  3474. spin_unlock(&dentry_attach_lock);
  3475. spin_unlock_irqrestore(&lockres->l_lock, flags);
  3476. mlog(0, "extra_ref = %d\n", extra_ref);
  3477. /*
  3478. * We have a process waiting on us in ocfs2_dentry_iput(),
  3479. * which means we can't have any more outstanding
  3480. * aliases. There's no need to do any more work.
  3481. */
  3482. if (!extra_ref)
  3483. return UNBLOCK_CONTINUE;
  3484. spin_lock(&dentry_attach_lock);
  3485. while (1) {
  3486. dentry = ocfs2_find_local_alias(dl->dl_inode,
  3487. dl->dl_parent_blkno, 1);
  3488. if (!dentry)
  3489. break;
  3490. spin_unlock(&dentry_attach_lock);
  3491. if (S_ISDIR(dl->dl_inode->i_mode))
  3492. shrink_dcache_parent(dentry);
  3493. mlog(0, "d_delete(%pd);\n", dentry);
  3494. /*
  3495. * The following dcache calls may do an
  3496. * iput(). Normally we don't want that from the
  3497. * downconverting thread, but in this case it's ok
  3498. * because the requesting node already has an
  3499. * exclusive lock on the inode, so it can't be queued
  3500. * for a downconvert.
  3501. */
  3502. d_delete(dentry);
  3503. dput(dentry);
  3504. spin_lock(&dentry_attach_lock);
  3505. }
  3506. spin_unlock(&dentry_attach_lock);
  3507. /*
  3508. * If we are the last holder of this dentry lock, there is no
  3509. * reason to downconvert so skip straight to the unlock.
  3510. */
  3511. if (dl->dl_count == 1)
  3512. return UNBLOCK_STOP_POST;
  3513. return UNBLOCK_CONTINUE_POST;
  3514. }
  3515. static int ocfs2_check_refcount_downconvert(struct ocfs2_lock_res *lockres,
  3516. int new_level)
  3517. {
  3518. struct ocfs2_refcount_tree *tree =
  3519. ocfs2_lock_res_refcount_tree(lockres);
  3520. return ocfs2_ci_checkpointed(&tree->rf_ci, lockres, new_level);
  3521. }
  3522. static int ocfs2_refcount_convert_worker(struct ocfs2_lock_res *lockres,
  3523. int blocking)
  3524. {
  3525. struct ocfs2_refcount_tree *tree =
  3526. ocfs2_lock_res_refcount_tree(lockres);
  3527. ocfs2_metadata_cache_purge(&tree->rf_ci);
  3528. return UNBLOCK_CONTINUE;
  3529. }
  3530. static void ocfs2_set_qinfo_lvb(struct ocfs2_lock_res *lockres)
  3531. {
  3532. struct ocfs2_qinfo_lvb *lvb;
  3533. struct ocfs2_mem_dqinfo *oinfo = ocfs2_lock_res_qinfo(lockres);
  3534. struct mem_dqinfo *info = sb_dqinfo(oinfo->dqi_gi.dqi_sb,
  3535. oinfo->dqi_gi.dqi_type);
  3536. lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
  3537. lvb->lvb_version = OCFS2_QINFO_LVB_VERSION;
  3538. lvb->lvb_bgrace = cpu_to_be32(info->dqi_bgrace);
  3539. lvb->lvb_igrace = cpu_to_be32(info->dqi_igrace);
  3540. lvb->lvb_syncms = cpu_to_be32(oinfo->dqi_syncms);
  3541. lvb->lvb_blocks = cpu_to_be32(oinfo->dqi_gi.dqi_blocks);
  3542. lvb->lvb_free_blk = cpu_to_be32(oinfo->dqi_gi.dqi_free_blk);
  3543. lvb->lvb_free_entry = cpu_to_be32(oinfo->dqi_gi.dqi_free_entry);
  3544. }
  3545. void ocfs2_qinfo_unlock(struct ocfs2_mem_dqinfo *oinfo, int ex)
  3546. {
  3547. struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
  3548. struct ocfs2_super *osb = OCFS2_SB(oinfo->dqi_gi.dqi_sb);
  3549. int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
  3550. if (!ocfs2_is_hard_readonly(osb) && !ocfs2_mount_local(osb))
  3551. ocfs2_cluster_unlock(osb, lockres, level);
  3552. }
  3553. static int ocfs2_refresh_qinfo(struct ocfs2_mem_dqinfo *oinfo)
  3554. {
  3555. struct mem_dqinfo *info = sb_dqinfo(oinfo->dqi_gi.dqi_sb,
  3556. oinfo->dqi_gi.dqi_type);
  3557. struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
  3558. struct ocfs2_qinfo_lvb *lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
  3559. struct buffer_head *bh = NULL;
  3560. struct ocfs2_global_disk_dqinfo *gdinfo;
  3561. int status = 0;
  3562. if (ocfs2_dlm_lvb_valid(&lockres->l_lksb) &&
  3563. lvb->lvb_version == OCFS2_QINFO_LVB_VERSION) {
  3564. info->dqi_bgrace = be32_to_cpu(lvb->lvb_bgrace);
  3565. info->dqi_igrace = be32_to_cpu(lvb->lvb_igrace);
  3566. oinfo->dqi_syncms = be32_to_cpu(lvb->lvb_syncms);
  3567. oinfo->dqi_gi.dqi_blocks = be32_to_cpu(lvb->lvb_blocks);
  3568. oinfo->dqi_gi.dqi_free_blk = be32_to_cpu(lvb->lvb_free_blk);
  3569. oinfo->dqi_gi.dqi_free_entry =
  3570. be32_to_cpu(lvb->lvb_free_entry);
  3571. } else {
  3572. status = ocfs2_read_quota_phys_block(oinfo->dqi_gqinode,
  3573. oinfo->dqi_giblk, &bh);
  3574. if (status) {
  3575. mlog_errno(status);
  3576. goto bail;
  3577. }
  3578. gdinfo = (struct ocfs2_global_disk_dqinfo *)
  3579. (bh->b_data + OCFS2_GLOBAL_INFO_OFF);
  3580. info->dqi_bgrace = le32_to_cpu(gdinfo->dqi_bgrace);
  3581. info->dqi_igrace = le32_to_cpu(gdinfo->dqi_igrace);
  3582. oinfo->dqi_syncms = le32_to_cpu(gdinfo->dqi_syncms);
  3583. oinfo->dqi_gi.dqi_blocks = le32_to_cpu(gdinfo->dqi_blocks);
  3584. oinfo->dqi_gi.dqi_free_blk = le32_to_cpu(gdinfo->dqi_free_blk);
  3585. oinfo->dqi_gi.dqi_free_entry =
  3586. le32_to_cpu(gdinfo->dqi_free_entry);
  3587. brelse(bh);
  3588. ocfs2_track_lock_refresh(lockres);
  3589. }
  3590. bail:
  3591. return status;
  3592. }
  3593. /* Lock quota info, this function expects at least shared lock on the quota file
  3594. * so that we can safely refresh quota info from disk. */
  3595. int ocfs2_qinfo_lock(struct ocfs2_mem_dqinfo *oinfo, int ex)
  3596. {
  3597. struct ocfs2_lock_res *lockres = &oinfo->dqi_gqlock;
  3598. struct ocfs2_super *osb = OCFS2_SB(oinfo->dqi_gi.dqi_sb);
  3599. int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
  3600. int status = 0;
  3601. /* On RO devices, locking really isn't needed... */
  3602. if (ocfs2_is_hard_readonly(osb)) {
  3603. if (ex)
  3604. status = -EROFS;
  3605. goto bail;
  3606. }
  3607. if (ocfs2_mount_local(osb))
  3608. goto bail;
  3609. status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
  3610. if (status < 0) {
  3611. mlog_errno(status);
  3612. goto bail;
  3613. }
  3614. if (!ocfs2_should_refresh_lock_res(lockres))
  3615. goto bail;
  3616. /* OK, we have the lock but we need to refresh the quota info */
  3617. status = ocfs2_refresh_qinfo(oinfo);
  3618. if (status)
  3619. ocfs2_qinfo_unlock(oinfo, ex);
  3620. ocfs2_complete_lock_res_refresh(lockres, status);
  3621. bail:
  3622. return status;
  3623. }
  3624. int ocfs2_refcount_lock(struct ocfs2_refcount_tree *ref_tree, int ex)
  3625. {
  3626. int status;
  3627. int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
  3628. struct ocfs2_lock_res *lockres = &ref_tree->rf_lockres;
  3629. struct ocfs2_super *osb = lockres->l_priv;
  3630. if (ocfs2_is_hard_readonly(osb))
  3631. return -EROFS;
  3632. if (ocfs2_mount_local(osb))
  3633. return 0;
  3634. status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
  3635. if (status < 0)
  3636. mlog_errno(status);
  3637. return status;
  3638. }
  3639. void ocfs2_refcount_unlock(struct ocfs2_refcount_tree *ref_tree, int ex)
  3640. {
  3641. int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
  3642. struct ocfs2_lock_res *lockres = &ref_tree->rf_lockres;
  3643. struct ocfs2_super *osb = lockres->l_priv;
  3644. if (!ocfs2_mount_local(osb))
  3645. ocfs2_cluster_unlock(osb, lockres, level);
  3646. }
  3647. static void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
  3648. struct ocfs2_lock_res *lockres)
  3649. {
  3650. int status;
  3651. struct ocfs2_unblock_ctl ctl = {0, 0,};
  3652. unsigned long flags;
  3653. /* Our reference to the lockres in this function can be
  3654. * considered valid until we remove the OCFS2_LOCK_QUEUED
  3655. * flag. */
  3656. BUG_ON(!lockres);
  3657. BUG_ON(!lockres->l_ops);
  3658. mlog(ML_BASTS, "lockres %s blocked\n", lockres->l_name);
  3659. /* Detect whether a lock has been marked as going away while
  3660. * the downconvert thread was processing other things. A lock can
  3661. * still be marked with OCFS2_LOCK_FREEING after this check,
  3662. * but short circuiting here will still save us some
  3663. * performance. */
  3664. spin_lock_irqsave(&lockres->l_lock, flags);
  3665. if (lockres->l_flags & OCFS2_LOCK_FREEING)
  3666. goto unqueue;
  3667. spin_unlock_irqrestore(&lockres->l_lock, flags);
  3668. status = ocfs2_unblock_lock(osb, lockres, &ctl);
  3669. if (status < 0)
  3670. mlog_errno(status);
  3671. spin_lock_irqsave(&lockres->l_lock, flags);
  3672. unqueue:
  3673. if (lockres->l_flags & OCFS2_LOCK_FREEING || !ctl.requeue) {
  3674. lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
  3675. } else
  3676. ocfs2_schedule_blocked_lock(osb, lockres);
  3677. mlog(ML_BASTS, "lockres %s, requeue = %s.\n", lockres->l_name,
  3678. str_yes_no(ctl.requeue));
  3679. spin_unlock_irqrestore(&lockres->l_lock, flags);
  3680. if (ctl.unblock_action != UNBLOCK_CONTINUE
  3681. && lockres->l_ops->post_unlock)
  3682. lockres->l_ops->post_unlock(osb, lockres);
  3683. }
  3684. static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
  3685. struct ocfs2_lock_res *lockres)
  3686. {
  3687. unsigned long flags;
  3688. assert_spin_locked(&lockres->l_lock);
  3689. if (lockres->l_flags & OCFS2_LOCK_FREEING) {
  3690. /* Do not schedule a lock for downconvert when it's on
  3691. * the way to destruction - any nodes wanting access
  3692. * to the resource will get it soon. */
  3693. mlog(ML_BASTS, "lockres %s won't be scheduled: flags 0x%lx\n",
  3694. lockres->l_name, lockres->l_flags);
  3695. return;
  3696. }
  3697. lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
  3698. spin_lock_irqsave(&osb->dc_task_lock, flags);
  3699. if (list_empty(&lockres->l_blocked_list)) {
  3700. list_add_tail(&lockres->l_blocked_list,
  3701. &osb->blocked_lock_list);
  3702. osb->blocked_lock_count++;
  3703. }
  3704. spin_unlock_irqrestore(&osb->dc_task_lock, flags);
  3705. }
  3706. static void ocfs2_downconvert_thread_do_work(struct ocfs2_super *osb)
  3707. {
  3708. unsigned long processed;
  3709. unsigned long flags;
  3710. struct ocfs2_lock_res *lockres;
  3711. spin_lock_irqsave(&osb->dc_task_lock, flags);
  3712. /* grab this early so we know to try again if a state change and
  3713. * wake happens part-way through our work */
  3714. osb->dc_work_sequence = osb->dc_wake_sequence;
  3715. processed = osb->blocked_lock_count;
  3716. /*
  3717. * blocked lock processing in this loop might call iput which can
  3718. * remove items off osb->blocked_lock_list. Downconvert up to
  3719. * 'processed' number of locks, but stop short if we had some
  3720. * removed in ocfs2_mark_lockres_freeing when downconverting.
  3721. */
  3722. while (processed && !list_empty(&osb->blocked_lock_list)) {
  3723. lockres = list_entry(osb->blocked_lock_list.next,
  3724. struct ocfs2_lock_res, l_blocked_list);
  3725. list_del_init(&lockres->l_blocked_list);
  3726. osb->blocked_lock_count--;
  3727. spin_unlock_irqrestore(&osb->dc_task_lock, flags);
  3728. BUG_ON(!processed);
  3729. processed--;
  3730. ocfs2_process_blocked_lock(osb, lockres);
  3731. spin_lock_irqsave(&osb->dc_task_lock, flags);
  3732. }
  3733. spin_unlock_irqrestore(&osb->dc_task_lock, flags);
  3734. }
  3735. static int ocfs2_downconvert_thread_lists_empty(struct ocfs2_super *osb)
  3736. {
  3737. int empty = 0;
  3738. unsigned long flags;
  3739. spin_lock_irqsave(&osb->dc_task_lock, flags);
  3740. if (list_empty(&osb->blocked_lock_list))
  3741. empty = 1;
  3742. spin_unlock_irqrestore(&osb->dc_task_lock, flags);
  3743. return empty;
  3744. }
  3745. static int ocfs2_downconvert_thread_should_wake(struct ocfs2_super *osb)
  3746. {
  3747. int should_wake = 0;
  3748. unsigned long flags;
  3749. spin_lock_irqsave(&osb->dc_task_lock, flags);
  3750. if (osb->dc_work_sequence != osb->dc_wake_sequence)
  3751. should_wake = 1;
  3752. spin_unlock_irqrestore(&osb->dc_task_lock, flags);
  3753. return should_wake;
  3754. }
  3755. static int ocfs2_downconvert_thread(void *arg)
  3756. {
  3757. struct ocfs2_super *osb = arg;
  3758. /* only quit once we've been asked to stop and there is no more
  3759. * work available */
  3760. while (!(kthread_should_stop() &&
  3761. ocfs2_downconvert_thread_lists_empty(osb))) {
  3762. wait_event_interruptible(osb->dc_event,
  3763. ocfs2_downconvert_thread_should_wake(osb) ||
  3764. kthread_should_stop());
  3765. mlog(0, "downconvert_thread: awoken\n");
  3766. ocfs2_downconvert_thread_do_work(osb);
  3767. }
  3768. osb->dc_task = NULL;
  3769. return 0;
  3770. }
  3771. void ocfs2_wake_downconvert_thread(struct ocfs2_super *osb)
  3772. {
  3773. unsigned long flags;
  3774. spin_lock_irqsave(&osb->dc_task_lock, flags);
  3775. /* make sure the voting thread gets a swipe at whatever changes
  3776. * the caller may have made to the voting state */
  3777. osb->dc_wake_sequence++;
  3778. spin_unlock_irqrestore(&osb->dc_task_lock, flags);
  3779. wake_up(&osb->dc_event);
  3780. }