dlmglue.c 124 KB

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