dlmglue.c 99 KB

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