dlmglue.c 116 KB

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