dlmglue.c 108 KB

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