dlmglue.c 103 KB

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