dlmglue.c 119 KB

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