dlmglue.c 105 KB

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