dlmglue.c 95 KB

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