dlmglue.c 114 KB

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