dlmglue.c 113 KB

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