dlmglue.c 95 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545
  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. int status;
  745. spin_lock_irqsave(&lockres->l_lock, flags);
  746. status = ocfs2_dlm_lock_status(&lockres->l_lksb);
  747. if (status == -EAGAIN) {
  748. lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
  749. goto out;
  750. }
  751. if (status) {
  752. mlog(ML_ERROR, "lockres %s: lksb status value of %d!\n",
  753. lockres->l_name, status);
  754. spin_unlock_irqrestore(&lockres->l_lock, flags);
  755. return;
  756. }
  757. switch(lockres->l_action) {
  758. case OCFS2_AST_ATTACH:
  759. ocfs2_generic_handle_attach_action(lockres);
  760. lockres_clear_flags(lockres, OCFS2_LOCK_LOCAL);
  761. break;
  762. case OCFS2_AST_CONVERT:
  763. ocfs2_generic_handle_convert_action(lockres);
  764. break;
  765. case OCFS2_AST_DOWNCONVERT:
  766. ocfs2_generic_handle_downconvert_action(lockres);
  767. break;
  768. default:
  769. mlog(ML_ERROR, "lockres %s: ast fired with invalid action: %u "
  770. "lockres flags = 0x%lx, unlock action: %u\n",
  771. lockres->l_name, lockres->l_action, lockres->l_flags,
  772. lockres->l_unlock_action);
  773. BUG();
  774. }
  775. out:
  776. /* set it to something invalid so if we get called again we
  777. * can catch it. */
  778. lockres->l_action = OCFS2_AST_INVALID;
  779. /* Did we try to cancel this lock? Clear that state */
  780. if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT)
  781. lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
  782. /*
  783. * We may have beaten the locking functions here. We certainly
  784. * know that dlm_lock() has been called :-)
  785. * Because we can't have two lock calls in flight at once, we
  786. * can use lockres->l_pending_gen.
  787. */
  788. __lockres_clear_pending(lockres, lockres->l_pending_gen, osb);
  789. wake_up(&lockres->l_event);
  790. spin_unlock_irqrestore(&lockres->l_lock, flags);
  791. }
  792. static inline void ocfs2_recover_from_dlm_error(struct ocfs2_lock_res *lockres,
  793. int convert)
  794. {
  795. unsigned long flags;
  796. mlog_entry_void();
  797. spin_lock_irqsave(&lockres->l_lock, flags);
  798. lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
  799. if (convert)
  800. lockres->l_action = OCFS2_AST_INVALID;
  801. else
  802. lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
  803. spin_unlock_irqrestore(&lockres->l_lock, flags);
  804. wake_up(&lockres->l_event);
  805. mlog_exit_void();
  806. }
  807. /* Note: If we detect another process working on the lock (i.e.,
  808. * OCFS2_LOCK_BUSY), we'll bail out returning 0. It's up to the caller
  809. * to do the right thing in that case.
  810. */
  811. static int ocfs2_lock_create(struct ocfs2_super *osb,
  812. struct ocfs2_lock_res *lockres,
  813. int level,
  814. u32 dlm_flags)
  815. {
  816. int ret = 0;
  817. unsigned long flags;
  818. unsigned int gen;
  819. mlog_entry_void();
  820. mlog(0, "lock %s, level = %d, flags = %u\n", lockres->l_name, level,
  821. dlm_flags);
  822. spin_lock_irqsave(&lockres->l_lock, flags);
  823. if ((lockres->l_flags & OCFS2_LOCK_ATTACHED) ||
  824. (lockres->l_flags & OCFS2_LOCK_BUSY)) {
  825. spin_unlock_irqrestore(&lockres->l_lock, flags);
  826. goto bail;
  827. }
  828. lockres->l_action = OCFS2_AST_ATTACH;
  829. lockres->l_requested = level;
  830. lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
  831. gen = lockres_set_pending(lockres);
  832. spin_unlock_irqrestore(&lockres->l_lock, flags);
  833. ret = ocfs2_dlm_lock(osb->cconn,
  834. level,
  835. &lockres->l_lksb,
  836. dlm_flags,
  837. lockres->l_name,
  838. OCFS2_LOCK_ID_MAX_LEN - 1,
  839. lockres);
  840. lockres_clear_pending(lockres, gen, osb);
  841. if (ret) {
  842. ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
  843. ocfs2_recover_from_dlm_error(lockres, 1);
  844. }
  845. mlog(0, "lock %s, return from ocfs2_dlm_lock\n", lockres->l_name);
  846. bail:
  847. mlog_exit(ret);
  848. return ret;
  849. }
  850. static inline int ocfs2_check_wait_flag(struct ocfs2_lock_res *lockres,
  851. int flag)
  852. {
  853. unsigned long flags;
  854. int ret;
  855. spin_lock_irqsave(&lockres->l_lock, flags);
  856. ret = lockres->l_flags & flag;
  857. spin_unlock_irqrestore(&lockres->l_lock, flags);
  858. return ret;
  859. }
  860. static inline void ocfs2_wait_on_busy_lock(struct ocfs2_lock_res *lockres)
  861. {
  862. wait_event(lockres->l_event,
  863. !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_BUSY));
  864. }
  865. static inline void ocfs2_wait_on_refreshing_lock(struct ocfs2_lock_res *lockres)
  866. {
  867. wait_event(lockres->l_event,
  868. !ocfs2_check_wait_flag(lockres, OCFS2_LOCK_REFRESHING));
  869. }
  870. /* predict what lock level we'll be dropping down to on behalf
  871. * of another node, and return true if the currently wanted
  872. * level will be compatible with it. */
  873. static inline int ocfs2_may_continue_on_blocked_lock(struct ocfs2_lock_res *lockres,
  874. int wanted)
  875. {
  876. BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
  877. return wanted <= ocfs2_highest_compat_lock_level(lockres->l_blocking);
  878. }
  879. static void ocfs2_init_mask_waiter(struct ocfs2_mask_waiter *mw)
  880. {
  881. INIT_LIST_HEAD(&mw->mw_item);
  882. init_completion(&mw->mw_complete);
  883. }
  884. static int ocfs2_wait_for_mask(struct ocfs2_mask_waiter *mw)
  885. {
  886. wait_for_completion(&mw->mw_complete);
  887. /* Re-arm the completion in case we want to wait on it again */
  888. INIT_COMPLETION(mw->mw_complete);
  889. return mw->mw_status;
  890. }
  891. static void lockres_add_mask_waiter(struct ocfs2_lock_res *lockres,
  892. struct ocfs2_mask_waiter *mw,
  893. unsigned long mask,
  894. unsigned long goal)
  895. {
  896. BUG_ON(!list_empty(&mw->mw_item));
  897. assert_spin_locked(&lockres->l_lock);
  898. list_add_tail(&mw->mw_item, &lockres->l_mask_waiters);
  899. mw->mw_mask = mask;
  900. mw->mw_goal = goal;
  901. }
  902. /* returns 0 if the mw that was removed was already satisfied, -EBUSY
  903. * if the mask still hadn't reached its goal */
  904. static int lockres_remove_mask_waiter(struct ocfs2_lock_res *lockres,
  905. struct ocfs2_mask_waiter *mw)
  906. {
  907. unsigned long flags;
  908. int ret = 0;
  909. spin_lock_irqsave(&lockres->l_lock, flags);
  910. if (!list_empty(&mw->mw_item)) {
  911. if ((lockres->l_flags & mw->mw_mask) != mw->mw_goal)
  912. ret = -EBUSY;
  913. list_del_init(&mw->mw_item);
  914. init_completion(&mw->mw_complete);
  915. }
  916. spin_unlock_irqrestore(&lockres->l_lock, flags);
  917. return ret;
  918. }
  919. static int ocfs2_wait_for_mask_interruptible(struct ocfs2_mask_waiter *mw,
  920. struct ocfs2_lock_res *lockres)
  921. {
  922. int ret;
  923. ret = wait_for_completion_interruptible(&mw->mw_complete);
  924. if (ret)
  925. lockres_remove_mask_waiter(lockres, mw);
  926. else
  927. ret = mw->mw_status;
  928. /* Re-arm the completion in case we want to wait on it again */
  929. INIT_COMPLETION(mw->mw_complete);
  930. return ret;
  931. }
  932. static int ocfs2_cluster_lock(struct ocfs2_super *osb,
  933. struct ocfs2_lock_res *lockres,
  934. int level,
  935. u32 lkm_flags,
  936. int arg_flags)
  937. {
  938. struct ocfs2_mask_waiter mw;
  939. int wait, catch_signals = !(osb->s_mount_opt & OCFS2_MOUNT_NOINTR);
  940. int ret = 0; /* gcc doesn't realize wait = 1 guarantees ret is set */
  941. unsigned long flags;
  942. unsigned int gen;
  943. int noqueue_attempted = 0;
  944. mlog_entry_void();
  945. ocfs2_init_mask_waiter(&mw);
  946. if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
  947. lkm_flags |= DLM_LKF_VALBLK;
  948. again:
  949. wait = 0;
  950. if (catch_signals && signal_pending(current)) {
  951. ret = -ERESTARTSYS;
  952. goto out;
  953. }
  954. spin_lock_irqsave(&lockres->l_lock, flags);
  955. mlog_bug_on_msg(lockres->l_flags & OCFS2_LOCK_FREEING,
  956. "Cluster lock called on freeing lockres %s! flags "
  957. "0x%lx\n", lockres->l_name, lockres->l_flags);
  958. /* We only compare against the currently granted level
  959. * here. If the lock is blocked waiting on a downconvert,
  960. * we'll get caught below. */
  961. if (lockres->l_flags & OCFS2_LOCK_BUSY &&
  962. level > lockres->l_level) {
  963. /* is someone sitting in dlm_lock? If so, wait on
  964. * them. */
  965. lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
  966. wait = 1;
  967. goto unlock;
  968. }
  969. if (lockres->l_flags & OCFS2_LOCK_BLOCKED &&
  970. !ocfs2_may_continue_on_blocked_lock(lockres, level)) {
  971. /* is the lock is currently blocked on behalf of
  972. * another node */
  973. lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BLOCKED, 0);
  974. wait = 1;
  975. goto unlock;
  976. }
  977. if (level > lockres->l_level) {
  978. if (noqueue_attempted > 0) {
  979. ret = -EAGAIN;
  980. goto unlock;
  981. }
  982. if (lkm_flags & DLM_LKF_NOQUEUE)
  983. noqueue_attempted = 1;
  984. if (lockres->l_action != OCFS2_AST_INVALID)
  985. mlog(ML_ERROR, "lockres %s has action %u pending\n",
  986. lockres->l_name, lockres->l_action);
  987. if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
  988. lockres->l_action = OCFS2_AST_ATTACH;
  989. lkm_flags &= ~DLM_LKF_CONVERT;
  990. } else {
  991. lockres->l_action = OCFS2_AST_CONVERT;
  992. lkm_flags |= DLM_LKF_CONVERT;
  993. }
  994. lockres->l_requested = level;
  995. lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
  996. gen = lockres_set_pending(lockres);
  997. spin_unlock_irqrestore(&lockres->l_lock, flags);
  998. BUG_ON(level == DLM_LOCK_IV);
  999. BUG_ON(level == DLM_LOCK_NL);
  1000. mlog(0, "lock %s, convert from %d to level = %d\n",
  1001. lockres->l_name, lockres->l_level, level);
  1002. /* call dlm_lock to upgrade lock now */
  1003. ret = ocfs2_dlm_lock(osb->cconn,
  1004. level,
  1005. &lockres->l_lksb,
  1006. lkm_flags,
  1007. lockres->l_name,
  1008. OCFS2_LOCK_ID_MAX_LEN - 1,
  1009. lockres);
  1010. lockres_clear_pending(lockres, gen, osb);
  1011. if (ret) {
  1012. if (!(lkm_flags & DLM_LKF_NOQUEUE) ||
  1013. (ret != -EAGAIN)) {
  1014. ocfs2_log_dlm_error("ocfs2_dlm_lock",
  1015. ret, lockres);
  1016. }
  1017. ocfs2_recover_from_dlm_error(lockres, 1);
  1018. goto out;
  1019. }
  1020. mlog(0, "lock %s, successfull return from ocfs2_dlm_lock\n",
  1021. lockres->l_name);
  1022. /* At this point we've gone inside the dlm and need to
  1023. * complete our work regardless. */
  1024. catch_signals = 0;
  1025. /* wait for busy to clear and carry on */
  1026. goto again;
  1027. }
  1028. /* Ok, if we get here then we're good to go. */
  1029. ocfs2_inc_holders(lockres, level);
  1030. ret = 0;
  1031. unlock:
  1032. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1033. out:
  1034. /*
  1035. * This is helping work around a lock inversion between the page lock
  1036. * and dlm locks. One path holds the page lock while calling aops
  1037. * which block acquiring dlm locks. The voting thread holds dlm
  1038. * locks while acquiring page locks while down converting data locks.
  1039. * This block is helping an aop path notice the inversion and back
  1040. * off to unlock its page lock before trying the dlm lock again.
  1041. */
  1042. if (wait && arg_flags & OCFS2_LOCK_NONBLOCK &&
  1043. mw.mw_mask & (OCFS2_LOCK_BUSY|OCFS2_LOCK_BLOCKED)) {
  1044. wait = 0;
  1045. if (lockres_remove_mask_waiter(lockres, &mw))
  1046. ret = -EAGAIN;
  1047. else
  1048. goto again;
  1049. }
  1050. if (wait) {
  1051. ret = ocfs2_wait_for_mask(&mw);
  1052. if (ret == 0)
  1053. goto again;
  1054. mlog_errno(ret);
  1055. }
  1056. mlog_exit(ret);
  1057. return ret;
  1058. }
  1059. static void ocfs2_cluster_unlock(struct ocfs2_super *osb,
  1060. struct ocfs2_lock_res *lockres,
  1061. int level)
  1062. {
  1063. unsigned long flags;
  1064. mlog_entry_void();
  1065. spin_lock_irqsave(&lockres->l_lock, flags);
  1066. ocfs2_dec_holders(lockres, level);
  1067. ocfs2_downconvert_on_unlock(osb, lockres);
  1068. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1069. mlog_exit_void();
  1070. }
  1071. static int ocfs2_create_new_lock(struct ocfs2_super *osb,
  1072. struct ocfs2_lock_res *lockres,
  1073. int ex,
  1074. int local)
  1075. {
  1076. int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
  1077. unsigned long flags;
  1078. u32 lkm_flags = local ? DLM_LKF_LOCAL : 0;
  1079. spin_lock_irqsave(&lockres->l_lock, flags);
  1080. BUG_ON(lockres->l_flags & OCFS2_LOCK_ATTACHED);
  1081. lockres_or_flags(lockres, OCFS2_LOCK_LOCAL);
  1082. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1083. return ocfs2_lock_create(osb, lockres, level, lkm_flags);
  1084. }
  1085. /* Grants us an EX lock on the data and metadata resources, skipping
  1086. * the normal cluster directory lookup. Use this ONLY on newly created
  1087. * inodes which other nodes can't possibly see, and which haven't been
  1088. * hashed in the inode hash yet. This can give us a good performance
  1089. * increase as it'll skip the network broadcast normally associated
  1090. * with creating a new lock resource. */
  1091. int ocfs2_create_new_inode_locks(struct inode *inode)
  1092. {
  1093. int ret;
  1094. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1095. BUG_ON(!inode);
  1096. BUG_ON(!ocfs2_inode_is_new(inode));
  1097. mlog_entry_void();
  1098. mlog(0, "Inode %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
  1099. /* NOTE: That we don't increment any of the holder counts, nor
  1100. * do we add anything to a journal handle. Since this is
  1101. * supposed to be a new inode which the cluster doesn't know
  1102. * about yet, there is no need to. As far as the LVB handling
  1103. * is concerned, this is basically like acquiring an EX lock
  1104. * on a resource which has an invalid one -- we'll set it
  1105. * valid when we release the EX. */
  1106. ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_rw_lockres, 1, 1);
  1107. if (ret) {
  1108. mlog_errno(ret);
  1109. goto bail;
  1110. }
  1111. /*
  1112. * We don't want to use DLM_LKF_LOCAL on a meta data lock as they
  1113. * don't use a generation in their lock names.
  1114. */
  1115. ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_inode_lockres, 1, 0);
  1116. if (ret) {
  1117. mlog_errno(ret);
  1118. goto bail;
  1119. }
  1120. ret = ocfs2_create_new_lock(osb, &OCFS2_I(inode)->ip_open_lockres, 0, 0);
  1121. if (ret) {
  1122. mlog_errno(ret);
  1123. goto bail;
  1124. }
  1125. bail:
  1126. mlog_exit(ret);
  1127. return ret;
  1128. }
  1129. int ocfs2_rw_lock(struct inode *inode, int write)
  1130. {
  1131. int status, level;
  1132. struct ocfs2_lock_res *lockres;
  1133. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1134. BUG_ON(!inode);
  1135. mlog_entry_void();
  1136. mlog(0, "inode %llu take %s RW lock\n",
  1137. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1138. write ? "EXMODE" : "PRMODE");
  1139. if (ocfs2_mount_local(osb))
  1140. return 0;
  1141. lockres = &OCFS2_I(inode)->ip_rw_lockres;
  1142. level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
  1143. status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres, level, 0,
  1144. 0);
  1145. if (status < 0)
  1146. mlog_errno(status);
  1147. mlog_exit(status);
  1148. return status;
  1149. }
  1150. void ocfs2_rw_unlock(struct inode *inode, int write)
  1151. {
  1152. int level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
  1153. struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_rw_lockres;
  1154. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1155. mlog_entry_void();
  1156. mlog(0, "inode %llu drop %s RW lock\n",
  1157. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1158. write ? "EXMODE" : "PRMODE");
  1159. if (!ocfs2_mount_local(osb))
  1160. ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
  1161. mlog_exit_void();
  1162. }
  1163. /*
  1164. * ocfs2_open_lock always get PR mode lock.
  1165. */
  1166. int ocfs2_open_lock(struct inode *inode)
  1167. {
  1168. int status = 0;
  1169. struct ocfs2_lock_res *lockres;
  1170. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1171. BUG_ON(!inode);
  1172. mlog_entry_void();
  1173. mlog(0, "inode %llu take PRMODE open lock\n",
  1174. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  1175. if (ocfs2_mount_local(osb))
  1176. goto out;
  1177. lockres = &OCFS2_I(inode)->ip_open_lockres;
  1178. status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
  1179. DLM_LOCK_PR, 0, 0);
  1180. if (status < 0)
  1181. mlog_errno(status);
  1182. out:
  1183. mlog_exit(status);
  1184. return status;
  1185. }
  1186. int ocfs2_try_open_lock(struct inode *inode, int write)
  1187. {
  1188. int status = 0, level;
  1189. struct ocfs2_lock_res *lockres;
  1190. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1191. BUG_ON(!inode);
  1192. mlog_entry_void();
  1193. mlog(0, "inode %llu try to take %s open lock\n",
  1194. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1195. write ? "EXMODE" : "PRMODE");
  1196. if (ocfs2_mount_local(osb))
  1197. goto out;
  1198. lockres = &OCFS2_I(inode)->ip_open_lockres;
  1199. level = write ? DLM_LOCK_EX : DLM_LOCK_PR;
  1200. /*
  1201. * The file system may already holding a PRMODE/EXMODE open lock.
  1202. * Since we pass DLM_LKF_NOQUEUE, the request won't block waiting on
  1203. * other nodes and the -EAGAIN will indicate to the caller that
  1204. * this inode is still in use.
  1205. */
  1206. status = ocfs2_cluster_lock(OCFS2_SB(inode->i_sb), lockres,
  1207. level, DLM_LKF_NOQUEUE, 0);
  1208. out:
  1209. mlog_exit(status);
  1210. return status;
  1211. }
  1212. /*
  1213. * ocfs2_open_unlock unlock PR and EX mode open locks.
  1214. */
  1215. void ocfs2_open_unlock(struct inode *inode)
  1216. {
  1217. struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_open_lockres;
  1218. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1219. mlog_entry_void();
  1220. mlog(0, "inode %llu drop open lock\n",
  1221. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  1222. if (ocfs2_mount_local(osb))
  1223. goto out;
  1224. if(lockres->l_ro_holders)
  1225. ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
  1226. DLM_LOCK_PR);
  1227. if(lockres->l_ex_holders)
  1228. ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres,
  1229. DLM_LOCK_EX);
  1230. out:
  1231. mlog_exit_void();
  1232. }
  1233. static int ocfs2_flock_handle_signal(struct ocfs2_lock_res *lockres,
  1234. int level)
  1235. {
  1236. int ret;
  1237. struct ocfs2_super *osb = ocfs2_get_lockres_osb(lockres);
  1238. unsigned long flags;
  1239. struct ocfs2_mask_waiter mw;
  1240. ocfs2_init_mask_waiter(&mw);
  1241. retry_cancel:
  1242. spin_lock_irqsave(&lockres->l_lock, flags);
  1243. if (lockres->l_flags & OCFS2_LOCK_BUSY) {
  1244. ret = ocfs2_prepare_cancel_convert(osb, lockres);
  1245. if (ret) {
  1246. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1247. ret = ocfs2_cancel_convert(osb, lockres);
  1248. if (ret < 0) {
  1249. mlog_errno(ret);
  1250. goto out;
  1251. }
  1252. goto retry_cancel;
  1253. }
  1254. lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
  1255. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1256. ocfs2_wait_for_mask(&mw);
  1257. goto retry_cancel;
  1258. }
  1259. ret = -ERESTARTSYS;
  1260. /*
  1261. * We may still have gotten the lock, in which case there's no
  1262. * point to restarting the syscall.
  1263. */
  1264. if (lockres->l_level == level)
  1265. ret = 0;
  1266. mlog(0, "Cancel returning %d. flags: 0x%lx, level: %d, act: %d\n", ret,
  1267. lockres->l_flags, lockres->l_level, lockres->l_action);
  1268. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1269. out:
  1270. return ret;
  1271. }
  1272. /*
  1273. * ocfs2_file_lock() and ocfs2_file_unlock() map to a single pair of
  1274. * flock() calls. The locking approach this requires is sufficiently
  1275. * different from all other cluster lock types that we implement a
  1276. * seperate path to the "low-level" dlm calls. In particular:
  1277. *
  1278. * - No optimization of lock levels is done - we take at exactly
  1279. * what's been requested.
  1280. *
  1281. * - No lock caching is employed. We immediately downconvert to
  1282. * no-lock at unlock time. This also means flock locks never go on
  1283. * the blocking list).
  1284. *
  1285. * - Since userspace can trivially deadlock itself with flock, we make
  1286. * sure to allow cancellation of a misbehaving applications flock()
  1287. * request.
  1288. *
  1289. * - Access to any flock lockres doesn't require concurrency, so we
  1290. * can simplify the code by requiring the caller to guarantee
  1291. * serialization of dlmglue flock calls.
  1292. */
  1293. int ocfs2_file_lock(struct file *file, int ex, int trylock)
  1294. {
  1295. int ret, level = ex ? LKM_EXMODE : LKM_PRMODE;
  1296. unsigned int lkm_flags = trylock ? LKM_NOQUEUE : 0;
  1297. unsigned long flags;
  1298. struct ocfs2_file_private *fp = file->private_data;
  1299. struct ocfs2_lock_res *lockres = &fp->fp_flock;
  1300. struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
  1301. struct ocfs2_mask_waiter mw;
  1302. ocfs2_init_mask_waiter(&mw);
  1303. if ((lockres->l_flags & OCFS2_LOCK_BUSY) ||
  1304. (lockres->l_level > DLM_LOCK_NL)) {
  1305. mlog(ML_ERROR,
  1306. "File lock \"%s\" has busy or locked state: flags: 0x%lx, "
  1307. "level: %u\n", lockres->l_name, lockres->l_flags,
  1308. lockres->l_level);
  1309. return -EINVAL;
  1310. }
  1311. spin_lock_irqsave(&lockres->l_lock, flags);
  1312. if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
  1313. lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
  1314. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1315. /*
  1316. * Get the lock at NLMODE to start - that way we
  1317. * can cancel the upconvert request if need be.
  1318. */
  1319. ret = ocfs2_lock_create(osb, lockres, LKM_NLMODE, 0);
  1320. if (ret < 0) {
  1321. mlog_errno(ret);
  1322. goto out;
  1323. }
  1324. ret = ocfs2_wait_for_mask(&mw);
  1325. if (ret) {
  1326. mlog_errno(ret);
  1327. goto out;
  1328. }
  1329. spin_lock_irqsave(&lockres->l_lock, flags);
  1330. }
  1331. lockres->l_action = OCFS2_AST_CONVERT;
  1332. lkm_flags |= LKM_CONVERT;
  1333. lockres->l_requested = level;
  1334. lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
  1335. lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
  1336. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1337. ret = ocfs2_dlm_lock(osb->cconn, level, &lockres->l_lksb, lkm_flags,
  1338. lockres->l_name, OCFS2_LOCK_ID_MAX_LEN - 1,
  1339. lockres);
  1340. if (ret) {
  1341. if (!trylock || (ret != -EAGAIN)) {
  1342. ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
  1343. ret = -EINVAL;
  1344. }
  1345. ocfs2_recover_from_dlm_error(lockres, 1);
  1346. lockres_remove_mask_waiter(lockres, &mw);
  1347. goto out;
  1348. }
  1349. ret = ocfs2_wait_for_mask_interruptible(&mw, lockres);
  1350. if (ret == -ERESTARTSYS) {
  1351. /*
  1352. * Userspace can cause deadlock itself with
  1353. * flock(). Current behavior locally is to allow the
  1354. * deadlock, but abort the system call if a signal is
  1355. * received. We follow this example, otherwise a
  1356. * poorly written program could sit in kernel until
  1357. * reboot.
  1358. *
  1359. * Handling this is a bit more complicated for Ocfs2
  1360. * though. We can't exit this function with an
  1361. * outstanding lock request, so a cancel convert is
  1362. * required. We intentionally overwrite 'ret' - if the
  1363. * cancel fails and the lock was granted, it's easier
  1364. * to just bubble sucess back up to the user.
  1365. */
  1366. ret = ocfs2_flock_handle_signal(lockres, level);
  1367. } else if (!ret && (level > lockres->l_level)) {
  1368. /* Trylock failed asynchronously */
  1369. BUG_ON(!trylock);
  1370. ret = -EAGAIN;
  1371. }
  1372. out:
  1373. mlog(0, "Lock: \"%s\" ex: %d, trylock: %d, returns: %d\n",
  1374. lockres->l_name, ex, trylock, ret);
  1375. return ret;
  1376. }
  1377. void ocfs2_file_unlock(struct file *file)
  1378. {
  1379. int ret;
  1380. unsigned int gen;
  1381. unsigned long flags;
  1382. struct ocfs2_file_private *fp = file->private_data;
  1383. struct ocfs2_lock_res *lockres = &fp->fp_flock;
  1384. struct ocfs2_super *osb = OCFS2_SB(file->f_mapping->host->i_sb);
  1385. struct ocfs2_mask_waiter mw;
  1386. ocfs2_init_mask_waiter(&mw);
  1387. if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED))
  1388. return;
  1389. if (lockres->l_level == LKM_NLMODE)
  1390. return;
  1391. mlog(0, "Unlock: \"%s\" flags: 0x%lx, level: %d, act: %d\n",
  1392. lockres->l_name, lockres->l_flags, lockres->l_level,
  1393. lockres->l_action);
  1394. spin_lock_irqsave(&lockres->l_lock, flags);
  1395. /*
  1396. * Fake a blocking ast for the downconvert code.
  1397. */
  1398. lockres_or_flags(lockres, OCFS2_LOCK_BLOCKED);
  1399. lockres->l_blocking = DLM_LOCK_EX;
  1400. gen = ocfs2_prepare_downconvert(lockres, LKM_NLMODE);
  1401. lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_BUSY, 0);
  1402. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1403. ret = ocfs2_downconvert_lock(osb, lockres, LKM_NLMODE, 0, gen);
  1404. if (ret) {
  1405. mlog_errno(ret);
  1406. return;
  1407. }
  1408. ret = ocfs2_wait_for_mask(&mw);
  1409. if (ret)
  1410. mlog_errno(ret);
  1411. }
  1412. static void ocfs2_downconvert_on_unlock(struct ocfs2_super *osb,
  1413. struct ocfs2_lock_res *lockres)
  1414. {
  1415. int kick = 0;
  1416. mlog_entry_void();
  1417. /* If we know that another node is waiting on our lock, kick
  1418. * the downconvert thread * pre-emptively when we reach a release
  1419. * condition. */
  1420. if (lockres->l_flags & OCFS2_LOCK_BLOCKED) {
  1421. switch(lockres->l_blocking) {
  1422. case DLM_LOCK_EX:
  1423. if (!lockres->l_ex_holders && !lockres->l_ro_holders)
  1424. kick = 1;
  1425. break;
  1426. case DLM_LOCK_PR:
  1427. if (!lockres->l_ex_holders)
  1428. kick = 1;
  1429. break;
  1430. default:
  1431. BUG();
  1432. }
  1433. }
  1434. if (kick)
  1435. ocfs2_wake_downconvert_thread(osb);
  1436. mlog_exit_void();
  1437. }
  1438. #define OCFS2_SEC_BITS 34
  1439. #define OCFS2_SEC_SHIFT (64 - 34)
  1440. #define OCFS2_NSEC_MASK ((1ULL << OCFS2_SEC_SHIFT) - 1)
  1441. /* LVB only has room for 64 bits of time here so we pack it for
  1442. * now. */
  1443. static u64 ocfs2_pack_timespec(struct timespec *spec)
  1444. {
  1445. u64 res;
  1446. u64 sec = spec->tv_sec;
  1447. u32 nsec = spec->tv_nsec;
  1448. res = (sec << OCFS2_SEC_SHIFT) | (nsec & OCFS2_NSEC_MASK);
  1449. return res;
  1450. }
  1451. /* Call this with the lockres locked. I am reasonably sure we don't
  1452. * need ip_lock in this function as anyone who would be changing those
  1453. * values is supposed to be blocked in ocfs2_inode_lock right now. */
  1454. static void __ocfs2_stuff_meta_lvb(struct inode *inode)
  1455. {
  1456. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1457. struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
  1458. struct ocfs2_meta_lvb *lvb;
  1459. mlog_entry_void();
  1460. lvb = (struct ocfs2_meta_lvb *)ocfs2_dlm_lvb(&lockres->l_lksb);
  1461. /*
  1462. * Invalidate the LVB of a deleted inode - this way other
  1463. * nodes are forced to go to disk and discover the new inode
  1464. * status.
  1465. */
  1466. if (oi->ip_flags & OCFS2_INODE_DELETED) {
  1467. lvb->lvb_version = 0;
  1468. goto out;
  1469. }
  1470. lvb->lvb_version = OCFS2_LVB_VERSION;
  1471. lvb->lvb_isize = cpu_to_be64(i_size_read(inode));
  1472. lvb->lvb_iclusters = cpu_to_be32(oi->ip_clusters);
  1473. lvb->lvb_iuid = cpu_to_be32(inode->i_uid);
  1474. lvb->lvb_igid = cpu_to_be32(inode->i_gid);
  1475. lvb->lvb_imode = cpu_to_be16(inode->i_mode);
  1476. lvb->lvb_inlink = cpu_to_be16(inode->i_nlink);
  1477. lvb->lvb_iatime_packed =
  1478. cpu_to_be64(ocfs2_pack_timespec(&inode->i_atime));
  1479. lvb->lvb_ictime_packed =
  1480. cpu_to_be64(ocfs2_pack_timespec(&inode->i_ctime));
  1481. lvb->lvb_imtime_packed =
  1482. cpu_to_be64(ocfs2_pack_timespec(&inode->i_mtime));
  1483. lvb->lvb_iattr = cpu_to_be32(oi->ip_attr);
  1484. lvb->lvb_idynfeatures = cpu_to_be16(oi->ip_dyn_features);
  1485. lvb->lvb_igeneration = cpu_to_be32(inode->i_generation);
  1486. out:
  1487. mlog_meta_lvb(0, lockres);
  1488. mlog_exit_void();
  1489. }
  1490. static void ocfs2_unpack_timespec(struct timespec *spec,
  1491. u64 packed_time)
  1492. {
  1493. spec->tv_sec = packed_time >> OCFS2_SEC_SHIFT;
  1494. spec->tv_nsec = packed_time & OCFS2_NSEC_MASK;
  1495. }
  1496. static void ocfs2_refresh_inode_from_lvb(struct inode *inode)
  1497. {
  1498. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1499. struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
  1500. struct ocfs2_meta_lvb *lvb;
  1501. mlog_entry_void();
  1502. mlog_meta_lvb(0, lockres);
  1503. lvb = (struct ocfs2_meta_lvb *)ocfs2_dlm_lvb(&lockres->l_lksb);
  1504. /* We're safe here without the lockres lock... */
  1505. spin_lock(&oi->ip_lock);
  1506. oi->ip_clusters = be32_to_cpu(lvb->lvb_iclusters);
  1507. i_size_write(inode, be64_to_cpu(lvb->lvb_isize));
  1508. oi->ip_attr = be32_to_cpu(lvb->lvb_iattr);
  1509. oi->ip_dyn_features = be16_to_cpu(lvb->lvb_idynfeatures);
  1510. ocfs2_set_inode_flags(inode);
  1511. /* fast-symlinks are a special case */
  1512. if (S_ISLNK(inode->i_mode) && !oi->ip_clusters)
  1513. inode->i_blocks = 0;
  1514. else
  1515. inode->i_blocks = ocfs2_inode_sector_count(inode);
  1516. inode->i_uid = be32_to_cpu(lvb->lvb_iuid);
  1517. inode->i_gid = be32_to_cpu(lvb->lvb_igid);
  1518. inode->i_mode = be16_to_cpu(lvb->lvb_imode);
  1519. inode->i_nlink = be16_to_cpu(lvb->lvb_inlink);
  1520. ocfs2_unpack_timespec(&inode->i_atime,
  1521. be64_to_cpu(lvb->lvb_iatime_packed));
  1522. ocfs2_unpack_timespec(&inode->i_mtime,
  1523. be64_to_cpu(lvb->lvb_imtime_packed));
  1524. ocfs2_unpack_timespec(&inode->i_ctime,
  1525. be64_to_cpu(lvb->lvb_ictime_packed));
  1526. spin_unlock(&oi->ip_lock);
  1527. mlog_exit_void();
  1528. }
  1529. static inline int ocfs2_meta_lvb_is_trustable(struct inode *inode,
  1530. struct ocfs2_lock_res *lockres)
  1531. {
  1532. struct ocfs2_meta_lvb *lvb =
  1533. (struct ocfs2_meta_lvb *)ocfs2_dlm_lvb(&lockres->l_lksb);
  1534. if (lvb->lvb_version == OCFS2_LVB_VERSION
  1535. && be32_to_cpu(lvb->lvb_igeneration) == inode->i_generation)
  1536. return 1;
  1537. return 0;
  1538. }
  1539. /* Determine whether a lock resource needs to be refreshed, and
  1540. * arbitrate who gets to refresh it.
  1541. *
  1542. * 0 means no refresh needed.
  1543. *
  1544. * > 0 means you need to refresh this and you MUST call
  1545. * ocfs2_complete_lock_res_refresh afterwards. */
  1546. static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
  1547. {
  1548. unsigned long flags;
  1549. int status = 0;
  1550. mlog_entry_void();
  1551. refresh_check:
  1552. spin_lock_irqsave(&lockres->l_lock, flags);
  1553. if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
  1554. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1555. goto bail;
  1556. }
  1557. if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
  1558. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1559. ocfs2_wait_on_refreshing_lock(lockres);
  1560. goto refresh_check;
  1561. }
  1562. /* Ok, I'll be the one to refresh this lock. */
  1563. lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
  1564. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1565. status = 1;
  1566. bail:
  1567. mlog_exit(status);
  1568. return status;
  1569. }
  1570. /* If status is non zero, I'll mark it as not being in refresh
  1571. * anymroe, but i won't clear the needs refresh flag. */
  1572. static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
  1573. int status)
  1574. {
  1575. unsigned long flags;
  1576. mlog_entry_void();
  1577. spin_lock_irqsave(&lockres->l_lock, flags);
  1578. lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
  1579. if (!status)
  1580. lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
  1581. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1582. wake_up(&lockres->l_event);
  1583. mlog_exit_void();
  1584. }
  1585. /* may or may not return a bh if it went to disk. */
  1586. static int ocfs2_inode_lock_update(struct inode *inode,
  1587. struct buffer_head **bh)
  1588. {
  1589. int status = 0;
  1590. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1591. struct ocfs2_lock_res *lockres = &oi->ip_inode_lockres;
  1592. struct ocfs2_dinode *fe;
  1593. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1594. mlog_entry_void();
  1595. if (ocfs2_mount_local(osb))
  1596. goto bail;
  1597. spin_lock(&oi->ip_lock);
  1598. if (oi->ip_flags & OCFS2_INODE_DELETED) {
  1599. mlog(0, "Orphaned inode %llu was deleted while we "
  1600. "were waiting on a lock. ip_flags = 0x%x\n",
  1601. (unsigned long long)oi->ip_blkno, oi->ip_flags);
  1602. spin_unlock(&oi->ip_lock);
  1603. status = -ENOENT;
  1604. goto bail;
  1605. }
  1606. spin_unlock(&oi->ip_lock);
  1607. if (!ocfs2_should_refresh_lock_res(lockres))
  1608. goto bail;
  1609. /* This will discard any caching information we might have had
  1610. * for the inode metadata. */
  1611. ocfs2_metadata_cache_purge(inode);
  1612. ocfs2_extent_map_trunc(inode, 0);
  1613. if (ocfs2_meta_lvb_is_trustable(inode, lockres)) {
  1614. mlog(0, "Trusting LVB on inode %llu\n",
  1615. (unsigned long long)oi->ip_blkno);
  1616. ocfs2_refresh_inode_from_lvb(inode);
  1617. } else {
  1618. /* Boo, we have to go to disk. */
  1619. /* read bh, cast, ocfs2_refresh_inode */
  1620. status = ocfs2_read_block(OCFS2_SB(inode->i_sb), oi->ip_blkno,
  1621. bh, OCFS2_BH_CACHED, inode);
  1622. if (status < 0) {
  1623. mlog_errno(status);
  1624. goto bail_refresh;
  1625. }
  1626. fe = (struct ocfs2_dinode *) (*bh)->b_data;
  1627. /* This is a good chance to make sure we're not
  1628. * locking an invalid object.
  1629. *
  1630. * We bug on a stale inode here because we checked
  1631. * above whether it was wiped from disk. The wiping
  1632. * node provides a guarantee that we receive that
  1633. * message and can mark the inode before dropping any
  1634. * locks associated with it. */
  1635. if (!OCFS2_IS_VALID_DINODE(fe)) {
  1636. OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
  1637. status = -EIO;
  1638. goto bail_refresh;
  1639. }
  1640. mlog_bug_on_msg(inode->i_generation !=
  1641. le32_to_cpu(fe->i_generation),
  1642. "Invalid dinode %llu disk generation: %u "
  1643. "inode->i_generation: %u\n",
  1644. (unsigned long long)oi->ip_blkno,
  1645. le32_to_cpu(fe->i_generation),
  1646. inode->i_generation);
  1647. mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
  1648. !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
  1649. "Stale dinode %llu dtime: %llu flags: 0x%x\n",
  1650. (unsigned long long)oi->ip_blkno,
  1651. (unsigned long long)le64_to_cpu(fe->i_dtime),
  1652. le32_to_cpu(fe->i_flags));
  1653. ocfs2_refresh_inode(inode, fe);
  1654. }
  1655. status = 0;
  1656. bail_refresh:
  1657. ocfs2_complete_lock_res_refresh(lockres, status);
  1658. bail:
  1659. mlog_exit(status);
  1660. return status;
  1661. }
  1662. static int ocfs2_assign_bh(struct inode *inode,
  1663. struct buffer_head **ret_bh,
  1664. struct buffer_head *passed_bh)
  1665. {
  1666. int status;
  1667. if (passed_bh) {
  1668. /* Ok, the update went to disk for us, use the
  1669. * returned bh. */
  1670. *ret_bh = passed_bh;
  1671. get_bh(*ret_bh);
  1672. return 0;
  1673. }
  1674. status = ocfs2_read_block(OCFS2_SB(inode->i_sb),
  1675. OCFS2_I(inode)->ip_blkno,
  1676. ret_bh,
  1677. OCFS2_BH_CACHED,
  1678. inode);
  1679. if (status < 0)
  1680. mlog_errno(status);
  1681. return status;
  1682. }
  1683. /*
  1684. * returns < 0 error if the callback will never be called, otherwise
  1685. * the result of the lock will be communicated via the callback.
  1686. */
  1687. int ocfs2_inode_lock_full(struct inode *inode,
  1688. struct buffer_head **ret_bh,
  1689. int ex,
  1690. int arg_flags)
  1691. {
  1692. int status, level, acquired;
  1693. u32 dlm_flags;
  1694. struct ocfs2_lock_res *lockres = NULL;
  1695. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1696. struct buffer_head *local_bh = NULL;
  1697. BUG_ON(!inode);
  1698. mlog_entry_void();
  1699. mlog(0, "inode %llu, take %s META lock\n",
  1700. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1701. ex ? "EXMODE" : "PRMODE");
  1702. status = 0;
  1703. acquired = 0;
  1704. /* We'll allow faking a readonly metadata lock for
  1705. * rodevices. */
  1706. if (ocfs2_is_hard_readonly(osb)) {
  1707. if (ex)
  1708. status = -EROFS;
  1709. goto bail;
  1710. }
  1711. if (ocfs2_mount_local(osb))
  1712. goto local;
  1713. if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
  1714. ocfs2_wait_for_recovery(osb);
  1715. lockres = &OCFS2_I(inode)->ip_inode_lockres;
  1716. level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
  1717. dlm_flags = 0;
  1718. if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
  1719. dlm_flags |= DLM_LKF_NOQUEUE;
  1720. status = ocfs2_cluster_lock(osb, lockres, level, dlm_flags, arg_flags);
  1721. if (status < 0) {
  1722. if (status != -EAGAIN && status != -EIOCBRETRY)
  1723. mlog_errno(status);
  1724. goto bail;
  1725. }
  1726. /* Notify the error cleanup path to drop the cluster lock. */
  1727. acquired = 1;
  1728. /* We wait twice because a node may have died while we were in
  1729. * the lower dlm layers. The second time though, we've
  1730. * committed to owning this lock so we don't allow signals to
  1731. * abort the operation. */
  1732. if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
  1733. ocfs2_wait_for_recovery(osb);
  1734. local:
  1735. /*
  1736. * We only see this flag if we're being called from
  1737. * ocfs2_read_locked_inode(). It means we're locking an inode
  1738. * which hasn't been populated yet, so clear the refresh flag
  1739. * and let the caller handle it.
  1740. */
  1741. if (inode->i_state & I_NEW) {
  1742. status = 0;
  1743. if (lockres)
  1744. ocfs2_complete_lock_res_refresh(lockres, 0);
  1745. goto bail;
  1746. }
  1747. /* This is fun. The caller may want a bh back, or it may
  1748. * not. ocfs2_inode_lock_update definitely wants one in, but
  1749. * may or may not read one, depending on what's in the
  1750. * LVB. The result of all of this is that we've *only* gone to
  1751. * disk if we have to, so the complexity is worthwhile. */
  1752. status = ocfs2_inode_lock_update(inode, &local_bh);
  1753. if (status < 0) {
  1754. if (status != -ENOENT)
  1755. mlog_errno(status);
  1756. goto bail;
  1757. }
  1758. if (ret_bh) {
  1759. status = ocfs2_assign_bh(inode, ret_bh, local_bh);
  1760. if (status < 0) {
  1761. mlog_errno(status);
  1762. goto bail;
  1763. }
  1764. }
  1765. bail:
  1766. if (status < 0) {
  1767. if (ret_bh && (*ret_bh)) {
  1768. brelse(*ret_bh);
  1769. *ret_bh = NULL;
  1770. }
  1771. if (acquired)
  1772. ocfs2_inode_unlock(inode, ex);
  1773. }
  1774. if (local_bh)
  1775. brelse(local_bh);
  1776. mlog_exit(status);
  1777. return status;
  1778. }
  1779. /*
  1780. * This is working around a lock inversion between tasks acquiring DLM
  1781. * locks while holding a page lock and the downconvert thread which
  1782. * blocks dlm lock acquiry while acquiring page locks.
  1783. *
  1784. * ** These _with_page variantes are only intended to be called from aop
  1785. * methods that hold page locks and return a very specific *positive* error
  1786. * code that aop methods pass up to the VFS -- test for errors with != 0. **
  1787. *
  1788. * The DLM is called such that it returns -EAGAIN if it would have
  1789. * blocked waiting for the downconvert thread. In that case we unlock
  1790. * our page so the downconvert thread can make progress. Once we've
  1791. * done this we have to return AOP_TRUNCATED_PAGE so the aop method
  1792. * that called us can bubble that back up into the VFS who will then
  1793. * immediately retry the aop call.
  1794. *
  1795. * We do a blocking lock and immediate unlock before returning, though, so that
  1796. * the lock has a great chance of being cached on this node by the time the VFS
  1797. * calls back to retry the aop. This has a potential to livelock as nodes
  1798. * ping locks back and forth, but that's a risk we're willing to take to avoid
  1799. * the lock inversion simply.
  1800. */
  1801. int ocfs2_inode_lock_with_page(struct inode *inode,
  1802. struct buffer_head **ret_bh,
  1803. int ex,
  1804. struct page *page)
  1805. {
  1806. int ret;
  1807. ret = ocfs2_inode_lock_full(inode, ret_bh, ex, OCFS2_LOCK_NONBLOCK);
  1808. if (ret == -EAGAIN) {
  1809. unlock_page(page);
  1810. if (ocfs2_inode_lock(inode, ret_bh, ex) == 0)
  1811. ocfs2_inode_unlock(inode, ex);
  1812. ret = AOP_TRUNCATED_PAGE;
  1813. }
  1814. return ret;
  1815. }
  1816. int ocfs2_inode_lock_atime(struct inode *inode,
  1817. struct vfsmount *vfsmnt,
  1818. int *level)
  1819. {
  1820. int ret;
  1821. mlog_entry_void();
  1822. ret = ocfs2_inode_lock(inode, NULL, 0);
  1823. if (ret < 0) {
  1824. mlog_errno(ret);
  1825. return ret;
  1826. }
  1827. /*
  1828. * If we should update atime, we will get EX lock,
  1829. * otherwise we just get PR lock.
  1830. */
  1831. if (ocfs2_should_update_atime(inode, vfsmnt)) {
  1832. struct buffer_head *bh = NULL;
  1833. ocfs2_inode_unlock(inode, 0);
  1834. ret = ocfs2_inode_lock(inode, &bh, 1);
  1835. if (ret < 0) {
  1836. mlog_errno(ret);
  1837. return ret;
  1838. }
  1839. *level = 1;
  1840. if (ocfs2_should_update_atime(inode, vfsmnt))
  1841. ocfs2_update_inode_atime(inode, bh);
  1842. if (bh)
  1843. brelse(bh);
  1844. } else
  1845. *level = 0;
  1846. mlog_exit(ret);
  1847. return ret;
  1848. }
  1849. void ocfs2_inode_unlock(struct inode *inode,
  1850. int ex)
  1851. {
  1852. int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
  1853. struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_inode_lockres;
  1854. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1855. mlog_entry_void();
  1856. mlog(0, "inode %llu drop %s META lock\n",
  1857. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1858. ex ? "EXMODE" : "PRMODE");
  1859. if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)) &&
  1860. !ocfs2_mount_local(osb))
  1861. ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
  1862. mlog_exit_void();
  1863. }
  1864. int ocfs2_super_lock(struct ocfs2_super *osb,
  1865. int ex)
  1866. {
  1867. int status = 0;
  1868. int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
  1869. struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
  1870. mlog_entry_void();
  1871. if (ocfs2_is_hard_readonly(osb))
  1872. return -EROFS;
  1873. if (ocfs2_mount_local(osb))
  1874. goto bail;
  1875. status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
  1876. if (status < 0) {
  1877. mlog_errno(status);
  1878. goto bail;
  1879. }
  1880. /* The super block lock path is really in the best position to
  1881. * know when resources covered by the lock need to be
  1882. * refreshed, so we do it here. Of course, making sense of
  1883. * everything is up to the caller :) */
  1884. status = ocfs2_should_refresh_lock_res(lockres);
  1885. if (status < 0) {
  1886. mlog_errno(status);
  1887. goto bail;
  1888. }
  1889. if (status) {
  1890. status = ocfs2_refresh_slot_info(osb);
  1891. ocfs2_complete_lock_res_refresh(lockres, status);
  1892. if (status < 0)
  1893. mlog_errno(status);
  1894. }
  1895. bail:
  1896. mlog_exit(status);
  1897. return status;
  1898. }
  1899. void ocfs2_super_unlock(struct ocfs2_super *osb,
  1900. int ex)
  1901. {
  1902. int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
  1903. struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
  1904. if (!ocfs2_mount_local(osb))
  1905. ocfs2_cluster_unlock(osb, lockres, level);
  1906. }
  1907. int ocfs2_rename_lock(struct ocfs2_super *osb)
  1908. {
  1909. int status;
  1910. struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
  1911. if (ocfs2_is_hard_readonly(osb))
  1912. return -EROFS;
  1913. if (ocfs2_mount_local(osb))
  1914. return 0;
  1915. status = ocfs2_cluster_lock(osb, lockres, DLM_LOCK_EX, 0, 0);
  1916. if (status < 0)
  1917. mlog_errno(status);
  1918. return status;
  1919. }
  1920. void ocfs2_rename_unlock(struct ocfs2_super *osb)
  1921. {
  1922. struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
  1923. if (!ocfs2_mount_local(osb))
  1924. ocfs2_cluster_unlock(osb, lockres, DLM_LOCK_EX);
  1925. }
  1926. int ocfs2_dentry_lock(struct dentry *dentry, int ex)
  1927. {
  1928. int ret;
  1929. int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
  1930. struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
  1931. struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
  1932. BUG_ON(!dl);
  1933. if (ocfs2_is_hard_readonly(osb))
  1934. return -EROFS;
  1935. if (ocfs2_mount_local(osb))
  1936. return 0;
  1937. ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0);
  1938. if (ret < 0)
  1939. mlog_errno(ret);
  1940. return ret;
  1941. }
  1942. void ocfs2_dentry_unlock(struct dentry *dentry, int ex)
  1943. {
  1944. int level = ex ? DLM_LOCK_EX : DLM_LOCK_PR;
  1945. struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
  1946. struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
  1947. if (!ocfs2_mount_local(osb))
  1948. ocfs2_cluster_unlock(osb, &dl->dl_lockres, level);
  1949. }
  1950. /* Reference counting of the dlm debug structure. We want this because
  1951. * open references on the debug inodes can live on after a mount, so
  1952. * we can't rely on the ocfs2_super to always exist. */
  1953. static void ocfs2_dlm_debug_free(struct kref *kref)
  1954. {
  1955. struct ocfs2_dlm_debug *dlm_debug;
  1956. dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
  1957. kfree(dlm_debug);
  1958. }
  1959. void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
  1960. {
  1961. if (dlm_debug)
  1962. kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
  1963. }
  1964. static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
  1965. {
  1966. kref_get(&debug->d_refcnt);
  1967. }
  1968. struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
  1969. {
  1970. struct ocfs2_dlm_debug *dlm_debug;
  1971. dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
  1972. if (!dlm_debug) {
  1973. mlog_errno(-ENOMEM);
  1974. goto out;
  1975. }
  1976. kref_init(&dlm_debug->d_refcnt);
  1977. INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
  1978. dlm_debug->d_locking_state = NULL;
  1979. out:
  1980. return dlm_debug;
  1981. }
  1982. /* Access to this is arbitrated for us via seq_file->sem. */
  1983. struct ocfs2_dlm_seq_priv {
  1984. struct ocfs2_dlm_debug *p_dlm_debug;
  1985. struct ocfs2_lock_res p_iter_res;
  1986. struct ocfs2_lock_res p_tmp_res;
  1987. };
  1988. static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
  1989. struct ocfs2_dlm_seq_priv *priv)
  1990. {
  1991. struct ocfs2_lock_res *iter, *ret = NULL;
  1992. struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
  1993. assert_spin_locked(&ocfs2_dlm_tracking_lock);
  1994. list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
  1995. /* discover the head of the list */
  1996. if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
  1997. mlog(0, "End of list found, %p\n", ret);
  1998. break;
  1999. }
  2000. /* We track our "dummy" iteration lockres' by a NULL
  2001. * l_ops field. */
  2002. if (iter->l_ops != NULL) {
  2003. ret = iter;
  2004. break;
  2005. }
  2006. }
  2007. return ret;
  2008. }
  2009. static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
  2010. {
  2011. struct ocfs2_dlm_seq_priv *priv = m->private;
  2012. struct ocfs2_lock_res *iter;
  2013. spin_lock(&ocfs2_dlm_tracking_lock);
  2014. iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
  2015. if (iter) {
  2016. /* Since lockres' have the lifetime of their container
  2017. * (which can be inodes, ocfs2_supers, etc) we want to
  2018. * copy this out to a temporary lockres while still
  2019. * under the spinlock. Obviously after this we can't
  2020. * trust any pointers on the copy returned, but that's
  2021. * ok as the information we want isn't typically held
  2022. * in them. */
  2023. priv->p_tmp_res = *iter;
  2024. iter = &priv->p_tmp_res;
  2025. }
  2026. spin_unlock(&ocfs2_dlm_tracking_lock);
  2027. return iter;
  2028. }
  2029. static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
  2030. {
  2031. }
  2032. static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
  2033. {
  2034. struct ocfs2_dlm_seq_priv *priv = m->private;
  2035. struct ocfs2_lock_res *iter = v;
  2036. struct ocfs2_lock_res *dummy = &priv->p_iter_res;
  2037. spin_lock(&ocfs2_dlm_tracking_lock);
  2038. iter = ocfs2_dlm_next_res(iter, priv);
  2039. list_del_init(&dummy->l_debug_list);
  2040. if (iter) {
  2041. list_add(&dummy->l_debug_list, &iter->l_debug_list);
  2042. priv->p_tmp_res = *iter;
  2043. iter = &priv->p_tmp_res;
  2044. }
  2045. spin_unlock(&ocfs2_dlm_tracking_lock);
  2046. return iter;
  2047. }
  2048. /* So that debugfs.ocfs2 can determine which format is being used */
  2049. #define OCFS2_DLM_DEBUG_STR_VERSION 1
  2050. static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
  2051. {
  2052. int i;
  2053. char *lvb;
  2054. struct ocfs2_lock_res *lockres = v;
  2055. if (!lockres)
  2056. return -EINVAL;
  2057. seq_printf(m, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION);
  2058. if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY)
  2059. seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1,
  2060. lockres->l_name,
  2061. (unsigned int)ocfs2_get_dentry_lock_ino(lockres));
  2062. else
  2063. seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name);
  2064. seq_printf(m, "%d\t"
  2065. "0x%lx\t"
  2066. "0x%x\t"
  2067. "0x%x\t"
  2068. "%u\t"
  2069. "%u\t"
  2070. "%d\t"
  2071. "%d\t",
  2072. lockres->l_level,
  2073. lockres->l_flags,
  2074. lockres->l_action,
  2075. lockres->l_unlock_action,
  2076. lockres->l_ro_holders,
  2077. lockres->l_ex_holders,
  2078. lockres->l_requested,
  2079. lockres->l_blocking);
  2080. /* Dump the raw LVB */
  2081. lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
  2082. for(i = 0; i < DLM_LVB_LEN; i++)
  2083. seq_printf(m, "0x%x\t", lvb[i]);
  2084. /* End the line */
  2085. seq_printf(m, "\n");
  2086. return 0;
  2087. }
  2088. static const struct seq_operations ocfs2_dlm_seq_ops = {
  2089. .start = ocfs2_dlm_seq_start,
  2090. .stop = ocfs2_dlm_seq_stop,
  2091. .next = ocfs2_dlm_seq_next,
  2092. .show = ocfs2_dlm_seq_show,
  2093. };
  2094. static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
  2095. {
  2096. struct seq_file *seq = (struct seq_file *) file->private_data;
  2097. struct ocfs2_dlm_seq_priv *priv = seq->private;
  2098. struct ocfs2_lock_res *res = &priv->p_iter_res;
  2099. ocfs2_remove_lockres_tracking(res);
  2100. ocfs2_put_dlm_debug(priv->p_dlm_debug);
  2101. return seq_release_private(inode, file);
  2102. }
  2103. static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
  2104. {
  2105. int ret;
  2106. struct ocfs2_dlm_seq_priv *priv;
  2107. struct seq_file *seq;
  2108. struct ocfs2_super *osb;
  2109. priv = kzalloc(sizeof(struct ocfs2_dlm_seq_priv), GFP_KERNEL);
  2110. if (!priv) {
  2111. ret = -ENOMEM;
  2112. mlog_errno(ret);
  2113. goto out;
  2114. }
  2115. osb = inode->i_private;
  2116. ocfs2_get_dlm_debug(osb->osb_dlm_debug);
  2117. priv->p_dlm_debug = osb->osb_dlm_debug;
  2118. INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
  2119. ret = seq_open(file, &ocfs2_dlm_seq_ops);
  2120. if (ret) {
  2121. kfree(priv);
  2122. mlog_errno(ret);
  2123. goto out;
  2124. }
  2125. seq = (struct seq_file *) file->private_data;
  2126. seq->private = priv;
  2127. ocfs2_add_lockres_tracking(&priv->p_iter_res,
  2128. priv->p_dlm_debug);
  2129. out:
  2130. return ret;
  2131. }
  2132. static const struct file_operations ocfs2_dlm_debug_fops = {
  2133. .open = ocfs2_dlm_debug_open,
  2134. .release = ocfs2_dlm_debug_release,
  2135. .read = seq_read,
  2136. .llseek = seq_lseek,
  2137. };
  2138. static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
  2139. {
  2140. int ret = 0;
  2141. struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
  2142. dlm_debug->d_locking_state = debugfs_create_file("locking_state",
  2143. S_IFREG|S_IRUSR,
  2144. osb->osb_debug_root,
  2145. osb,
  2146. &ocfs2_dlm_debug_fops);
  2147. if (!dlm_debug->d_locking_state) {
  2148. ret = -EINVAL;
  2149. mlog(ML_ERROR,
  2150. "Unable to create locking state debugfs file.\n");
  2151. goto out;
  2152. }
  2153. ocfs2_get_dlm_debug(dlm_debug);
  2154. out:
  2155. return ret;
  2156. }
  2157. static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
  2158. {
  2159. struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
  2160. if (dlm_debug) {
  2161. debugfs_remove(dlm_debug->d_locking_state);
  2162. ocfs2_put_dlm_debug(dlm_debug);
  2163. }
  2164. }
  2165. int ocfs2_dlm_init(struct ocfs2_super *osb)
  2166. {
  2167. int status = 0;
  2168. struct ocfs2_cluster_connection *conn = NULL;
  2169. mlog_entry_void();
  2170. if (ocfs2_mount_local(osb)) {
  2171. osb->node_num = 0;
  2172. goto local;
  2173. }
  2174. status = ocfs2_dlm_init_debug(osb);
  2175. if (status < 0) {
  2176. mlog_errno(status);
  2177. goto bail;
  2178. }
  2179. /* launch downconvert thread */
  2180. osb->dc_task = kthread_run(ocfs2_downconvert_thread, osb, "ocfs2dc");
  2181. if (IS_ERR(osb->dc_task)) {
  2182. status = PTR_ERR(osb->dc_task);
  2183. osb->dc_task = NULL;
  2184. mlog_errno(status);
  2185. goto bail;
  2186. }
  2187. /* for now, uuid == domain */
  2188. status = ocfs2_cluster_connect(osb->osb_cluster_stack,
  2189. osb->uuid_str,
  2190. strlen(osb->uuid_str),
  2191. ocfs2_do_node_down, osb,
  2192. &conn);
  2193. if (status) {
  2194. mlog_errno(status);
  2195. goto bail;
  2196. }
  2197. status = ocfs2_cluster_this_node(&osb->node_num);
  2198. if (status < 0) {
  2199. mlog_errno(status);
  2200. mlog(ML_ERROR,
  2201. "could not find this host's node number\n");
  2202. ocfs2_cluster_disconnect(conn, 0);
  2203. goto bail;
  2204. }
  2205. local:
  2206. ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
  2207. ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
  2208. osb->cconn = conn;
  2209. status = 0;
  2210. bail:
  2211. if (status < 0) {
  2212. ocfs2_dlm_shutdown_debug(osb);
  2213. if (osb->dc_task)
  2214. kthread_stop(osb->dc_task);
  2215. }
  2216. mlog_exit(status);
  2217. return status;
  2218. }
  2219. void ocfs2_dlm_shutdown(struct ocfs2_super *osb,
  2220. int hangup_pending)
  2221. {
  2222. mlog_entry_void();
  2223. ocfs2_drop_osb_locks(osb);
  2224. /*
  2225. * Now that we have dropped all locks and ocfs2_dismount_volume()
  2226. * has disabled recovery, the DLM won't be talking to us. It's
  2227. * safe to tear things down before disconnecting the cluster.
  2228. */
  2229. if (osb->dc_task) {
  2230. kthread_stop(osb->dc_task);
  2231. osb->dc_task = NULL;
  2232. }
  2233. ocfs2_lock_res_free(&osb->osb_super_lockres);
  2234. ocfs2_lock_res_free(&osb->osb_rename_lockres);
  2235. ocfs2_cluster_disconnect(osb->cconn, hangup_pending);
  2236. osb->cconn = NULL;
  2237. ocfs2_dlm_shutdown_debug(osb);
  2238. mlog_exit_void();
  2239. }
  2240. static void ocfs2_unlock_ast(void *opaque, int error)
  2241. {
  2242. struct ocfs2_lock_res *lockres = opaque;
  2243. unsigned long flags;
  2244. mlog_entry_void();
  2245. mlog(0, "UNLOCK AST called on lock %s, action = %d\n", lockres->l_name,
  2246. lockres->l_unlock_action);
  2247. spin_lock_irqsave(&lockres->l_lock, flags);
  2248. if (error) {
  2249. mlog(ML_ERROR, "Dlm passes error %d for lock %s, "
  2250. "unlock_action %d\n", error, lockres->l_name,
  2251. lockres->l_unlock_action);
  2252. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2253. return;
  2254. }
  2255. switch(lockres->l_unlock_action) {
  2256. case OCFS2_UNLOCK_CANCEL_CONVERT:
  2257. mlog(0, "Cancel convert success for %s\n", lockres->l_name);
  2258. lockres->l_action = OCFS2_AST_INVALID;
  2259. break;
  2260. case OCFS2_UNLOCK_DROP_LOCK:
  2261. lockres->l_level = DLM_LOCK_IV;
  2262. break;
  2263. default:
  2264. BUG();
  2265. }
  2266. lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
  2267. lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
  2268. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2269. wake_up(&lockres->l_event);
  2270. mlog_exit_void();
  2271. }
  2272. static int ocfs2_drop_lock(struct ocfs2_super *osb,
  2273. struct ocfs2_lock_res *lockres)
  2274. {
  2275. int ret;
  2276. unsigned long flags;
  2277. u32 lkm_flags = 0;
  2278. /* We didn't get anywhere near actually using this lockres. */
  2279. if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
  2280. goto out;
  2281. if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
  2282. lkm_flags |= DLM_LKF_VALBLK;
  2283. spin_lock_irqsave(&lockres->l_lock, flags);
  2284. mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
  2285. "lockres %s, flags 0x%lx\n",
  2286. lockres->l_name, lockres->l_flags);
  2287. while (lockres->l_flags & OCFS2_LOCK_BUSY) {
  2288. mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
  2289. "%u, unlock_action = %u\n",
  2290. lockres->l_name, lockres->l_flags, lockres->l_action,
  2291. lockres->l_unlock_action);
  2292. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2293. /* XXX: Today we just wait on any busy
  2294. * locks... Perhaps we need to cancel converts in the
  2295. * future? */
  2296. ocfs2_wait_on_busy_lock(lockres);
  2297. spin_lock_irqsave(&lockres->l_lock, flags);
  2298. }
  2299. if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
  2300. if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
  2301. lockres->l_level == DLM_LOCK_EX &&
  2302. !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
  2303. lockres->l_ops->set_lvb(lockres);
  2304. }
  2305. if (lockres->l_flags & OCFS2_LOCK_BUSY)
  2306. mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
  2307. lockres->l_name);
  2308. if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
  2309. mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
  2310. if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
  2311. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2312. goto out;
  2313. }
  2314. lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
  2315. /* make sure we never get here while waiting for an ast to
  2316. * fire. */
  2317. BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
  2318. /* is this necessary? */
  2319. lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
  2320. lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
  2321. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2322. mlog(0, "lock %s\n", lockres->l_name);
  2323. ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb, lkm_flags,
  2324. lockres);
  2325. if (ret) {
  2326. ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
  2327. mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
  2328. ocfs2_dlm_dump_lksb(&lockres->l_lksb);
  2329. BUG();
  2330. }
  2331. mlog(0, "lock %s, successfull return from ocfs2_dlm_unlock\n",
  2332. lockres->l_name);
  2333. ocfs2_wait_on_busy_lock(lockres);
  2334. out:
  2335. mlog_exit(0);
  2336. return 0;
  2337. }
  2338. /* Mark the lockres as being dropped. It will no longer be
  2339. * queued if blocking, but we still may have to wait on it
  2340. * being dequeued from the downconvert thread before we can consider
  2341. * it safe to drop.
  2342. *
  2343. * You can *not* attempt to call cluster_lock on this lockres anymore. */
  2344. void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres)
  2345. {
  2346. int status;
  2347. struct ocfs2_mask_waiter mw;
  2348. unsigned long flags;
  2349. ocfs2_init_mask_waiter(&mw);
  2350. spin_lock_irqsave(&lockres->l_lock, flags);
  2351. lockres->l_flags |= OCFS2_LOCK_FREEING;
  2352. while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
  2353. lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
  2354. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2355. mlog(0, "Waiting on lockres %s\n", lockres->l_name);
  2356. status = ocfs2_wait_for_mask(&mw);
  2357. if (status)
  2358. mlog_errno(status);
  2359. spin_lock_irqsave(&lockres->l_lock, flags);
  2360. }
  2361. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2362. }
  2363. void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
  2364. struct ocfs2_lock_res *lockres)
  2365. {
  2366. int ret;
  2367. ocfs2_mark_lockres_freeing(lockres);
  2368. ret = ocfs2_drop_lock(osb, lockres);
  2369. if (ret)
  2370. mlog_errno(ret);
  2371. }
  2372. static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
  2373. {
  2374. ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres);
  2375. ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres);
  2376. }
  2377. int ocfs2_drop_inode_locks(struct inode *inode)
  2378. {
  2379. int status, err;
  2380. mlog_entry_void();
  2381. /* No need to call ocfs2_mark_lockres_freeing here -
  2382. * ocfs2_clear_inode has done it for us. */
  2383. err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
  2384. &OCFS2_I(inode)->ip_open_lockres);
  2385. if (err < 0)
  2386. mlog_errno(err);
  2387. status = err;
  2388. err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
  2389. &OCFS2_I(inode)->ip_inode_lockres);
  2390. if (err < 0)
  2391. mlog_errno(err);
  2392. if (err < 0 && !status)
  2393. status = err;
  2394. err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
  2395. &OCFS2_I(inode)->ip_rw_lockres);
  2396. if (err < 0)
  2397. mlog_errno(err);
  2398. if (err < 0 && !status)
  2399. status = err;
  2400. mlog_exit(status);
  2401. return status;
  2402. }
  2403. static unsigned int ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
  2404. int new_level)
  2405. {
  2406. assert_spin_locked(&lockres->l_lock);
  2407. BUG_ON(lockres->l_blocking <= DLM_LOCK_NL);
  2408. if (lockres->l_level <= new_level) {
  2409. mlog(ML_ERROR, "lockres->l_level (%d) <= new_level (%d)\n",
  2410. lockres->l_level, new_level);
  2411. BUG();
  2412. }
  2413. mlog(0, "lock %s, new_level = %d, l_blocking = %d\n",
  2414. lockres->l_name, new_level, lockres->l_blocking);
  2415. lockres->l_action = OCFS2_AST_DOWNCONVERT;
  2416. lockres->l_requested = new_level;
  2417. lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
  2418. return lockres_set_pending(lockres);
  2419. }
  2420. static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
  2421. struct ocfs2_lock_res *lockres,
  2422. int new_level,
  2423. int lvb,
  2424. unsigned int generation)
  2425. {
  2426. int ret;
  2427. u32 dlm_flags = DLM_LKF_CONVERT;
  2428. mlog_entry_void();
  2429. if (lvb)
  2430. dlm_flags |= DLM_LKF_VALBLK;
  2431. ret = ocfs2_dlm_lock(osb->cconn,
  2432. new_level,
  2433. &lockres->l_lksb,
  2434. dlm_flags,
  2435. lockres->l_name,
  2436. OCFS2_LOCK_ID_MAX_LEN - 1,
  2437. lockres);
  2438. lockres_clear_pending(lockres, generation, osb);
  2439. if (ret) {
  2440. ocfs2_log_dlm_error("ocfs2_dlm_lock", ret, lockres);
  2441. ocfs2_recover_from_dlm_error(lockres, 1);
  2442. goto bail;
  2443. }
  2444. ret = 0;
  2445. bail:
  2446. mlog_exit(ret);
  2447. return ret;
  2448. }
  2449. /* returns 1 when the caller should unlock and call ocfs2_dlm_unlock */
  2450. static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
  2451. struct ocfs2_lock_res *lockres)
  2452. {
  2453. assert_spin_locked(&lockres->l_lock);
  2454. mlog_entry_void();
  2455. mlog(0, "lock %s\n", lockres->l_name);
  2456. if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
  2457. /* If we're already trying to cancel a lock conversion
  2458. * then just drop the spinlock and allow the caller to
  2459. * requeue this lock. */
  2460. mlog(0, "Lockres %s, skip convert\n", lockres->l_name);
  2461. return 0;
  2462. }
  2463. /* were we in a convert when we got the bast fire? */
  2464. BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
  2465. lockres->l_action != OCFS2_AST_DOWNCONVERT);
  2466. /* set things up for the unlockast to know to just
  2467. * clear out the ast_action and unset busy, etc. */
  2468. lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
  2469. mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
  2470. "lock %s, invalid flags: 0x%lx\n",
  2471. lockres->l_name, lockres->l_flags);
  2472. return 1;
  2473. }
  2474. static int ocfs2_cancel_convert(struct ocfs2_super *osb,
  2475. struct ocfs2_lock_res *lockres)
  2476. {
  2477. int ret;
  2478. mlog_entry_void();
  2479. mlog(0, "lock %s\n", lockres->l_name);
  2480. ret = ocfs2_dlm_unlock(osb->cconn, &lockres->l_lksb,
  2481. DLM_LKF_CANCEL, lockres);
  2482. if (ret) {
  2483. ocfs2_log_dlm_error("ocfs2_dlm_unlock", ret, lockres);
  2484. ocfs2_recover_from_dlm_error(lockres, 0);
  2485. }
  2486. mlog(0, "lock %s return from ocfs2_dlm_unlock\n", lockres->l_name);
  2487. mlog_exit(ret);
  2488. return ret;
  2489. }
  2490. static int ocfs2_unblock_lock(struct ocfs2_super *osb,
  2491. struct ocfs2_lock_res *lockres,
  2492. struct ocfs2_unblock_ctl *ctl)
  2493. {
  2494. unsigned long flags;
  2495. int blocking;
  2496. int new_level;
  2497. int ret = 0;
  2498. int set_lvb = 0;
  2499. unsigned int gen;
  2500. mlog_entry_void();
  2501. spin_lock_irqsave(&lockres->l_lock, flags);
  2502. BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
  2503. recheck:
  2504. if (lockres->l_flags & OCFS2_LOCK_BUSY) {
  2505. /* XXX
  2506. * This is a *big* race. The OCFS2_LOCK_PENDING flag
  2507. * exists entirely for one reason - another thread has set
  2508. * OCFS2_LOCK_BUSY, but has *NOT* yet called dlm_lock().
  2509. *
  2510. * If we do ocfs2_cancel_convert() before the other thread
  2511. * calls dlm_lock(), our cancel will do nothing. We will
  2512. * get no ast, and we will have no way of knowing the
  2513. * cancel failed. Meanwhile, the other thread will call
  2514. * into dlm_lock() and wait...forever.
  2515. *
  2516. * Why forever? Because another node has asked for the
  2517. * lock first; that's why we're here in unblock_lock().
  2518. *
  2519. * The solution is OCFS2_LOCK_PENDING. When PENDING is
  2520. * set, we just requeue the unblock. Only when the other
  2521. * thread has called dlm_lock() and cleared PENDING will
  2522. * we then cancel their request.
  2523. *
  2524. * All callers of dlm_lock() must set OCFS2_DLM_PENDING
  2525. * at the same time they set OCFS2_DLM_BUSY. They must
  2526. * clear OCFS2_DLM_PENDING after dlm_lock() returns.
  2527. */
  2528. if (lockres->l_flags & OCFS2_LOCK_PENDING)
  2529. goto leave_requeue;
  2530. ctl->requeue = 1;
  2531. ret = ocfs2_prepare_cancel_convert(osb, lockres);
  2532. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2533. if (ret) {
  2534. ret = ocfs2_cancel_convert(osb, lockres);
  2535. if (ret < 0)
  2536. mlog_errno(ret);
  2537. }
  2538. goto leave;
  2539. }
  2540. /* if we're blocking an exclusive and we have *any* holders,
  2541. * then requeue. */
  2542. if ((lockres->l_blocking == DLM_LOCK_EX)
  2543. && (lockres->l_ex_holders || lockres->l_ro_holders))
  2544. goto leave_requeue;
  2545. /* If it's a PR we're blocking, then only
  2546. * requeue if we've got any EX holders */
  2547. if (lockres->l_blocking == DLM_LOCK_PR &&
  2548. lockres->l_ex_holders)
  2549. goto leave_requeue;
  2550. /*
  2551. * Can we get a lock in this state if the holder counts are
  2552. * zero? The meta data unblock code used to check this.
  2553. */
  2554. if ((lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
  2555. && (lockres->l_flags & OCFS2_LOCK_REFRESHING))
  2556. goto leave_requeue;
  2557. new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
  2558. if (lockres->l_ops->check_downconvert
  2559. && !lockres->l_ops->check_downconvert(lockres, new_level))
  2560. goto leave_requeue;
  2561. /* If we get here, then we know that there are no more
  2562. * incompatible holders (and anyone asking for an incompatible
  2563. * lock is blocked). We can now downconvert the lock */
  2564. if (!lockres->l_ops->downconvert_worker)
  2565. goto downconvert;
  2566. /* Some lockres types want to do a bit of work before
  2567. * downconverting a lock. Allow that here. The worker function
  2568. * may sleep, so we save off a copy of what we're blocking as
  2569. * it may change while we're not holding the spin lock. */
  2570. blocking = lockres->l_blocking;
  2571. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2572. ctl->unblock_action = lockres->l_ops->downconvert_worker(lockres, blocking);
  2573. if (ctl->unblock_action == UNBLOCK_STOP_POST)
  2574. goto leave;
  2575. spin_lock_irqsave(&lockres->l_lock, flags);
  2576. if (blocking != lockres->l_blocking) {
  2577. /* If this changed underneath us, then we can't drop
  2578. * it just yet. */
  2579. goto recheck;
  2580. }
  2581. downconvert:
  2582. ctl->requeue = 0;
  2583. if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
  2584. if (lockres->l_level == DLM_LOCK_EX)
  2585. set_lvb = 1;
  2586. /*
  2587. * We only set the lvb if the lock has been fully
  2588. * refreshed - otherwise we risk setting stale
  2589. * data. Otherwise, there's no need to actually clear
  2590. * out the lvb here as it's value is still valid.
  2591. */
  2592. if (set_lvb && !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
  2593. lockres->l_ops->set_lvb(lockres);
  2594. }
  2595. gen = ocfs2_prepare_downconvert(lockres, new_level);
  2596. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2597. ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb,
  2598. gen);
  2599. leave:
  2600. mlog_exit(ret);
  2601. return ret;
  2602. leave_requeue:
  2603. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2604. ctl->requeue = 1;
  2605. mlog_exit(0);
  2606. return 0;
  2607. }
  2608. static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
  2609. int blocking)
  2610. {
  2611. struct inode *inode;
  2612. struct address_space *mapping;
  2613. inode = ocfs2_lock_res_inode(lockres);
  2614. mapping = inode->i_mapping;
  2615. if (!S_ISREG(inode->i_mode))
  2616. goto out;
  2617. /*
  2618. * We need this before the filemap_fdatawrite() so that it can
  2619. * transfer the dirty bit from the PTE to the
  2620. * page. Unfortunately this means that even for EX->PR
  2621. * downconverts, we'll lose our mappings and have to build
  2622. * them up again.
  2623. */
  2624. unmap_mapping_range(mapping, 0, 0, 0);
  2625. if (filemap_fdatawrite(mapping)) {
  2626. mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
  2627. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  2628. }
  2629. sync_mapping_buffers(mapping);
  2630. if (blocking == DLM_LOCK_EX) {
  2631. truncate_inode_pages(mapping, 0);
  2632. } else {
  2633. /* We only need to wait on the I/O if we're not also
  2634. * truncating pages because truncate_inode_pages waits
  2635. * for us above. We don't truncate pages if we're
  2636. * blocking anything < EXMODE because we want to keep
  2637. * them around in that case. */
  2638. filemap_fdatawait(mapping);
  2639. }
  2640. out:
  2641. return UNBLOCK_CONTINUE;
  2642. }
  2643. static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
  2644. int new_level)
  2645. {
  2646. struct inode *inode = ocfs2_lock_res_inode(lockres);
  2647. int checkpointed = ocfs2_inode_fully_checkpointed(inode);
  2648. BUG_ON(new_level != DLM_LOCK_NL && new_level != DLM_LOCK_PR);
  2649. BUG_ON(lockres->l_level != DLM_LOCK_EX && !checkpointed);
  2650. if (checkpointed)
  2651. return 1;
  2652. ocfs2_start_checkpoint(OCFS2_SB(inode->i_sb));
  2653. return 0;
  2654. }
  2655. static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres)
  2656. {
  2657. struct inode *inode = ocfs2_lock_res_inode(lockres);
  2658. __ocfs2_stuff_meta_lvb(inode);
  2659. }
  2660. /*
  2661. * Does the final reference drop on our dentry lock. Right now this
  2662. * happens in the downconvert thread, but we could choose to simplify the
  2663. * dlmglue API and push these off to the ocfs2_wq in the future.
  2664. */
  2665. static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
  2666. struct ocfs2_lock_res *lockres)
  2667. {
  2668. struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
  2669. ocfs2_dentry_lock_put(osb, dl);
  2670. }
  2671. /*
  2672. * d_delete() matching dentries before the lock downconvert.
  2673. *
  2674. * At this point, any process waiting to destroy the
  2675. * dentry_lock due to last ref count is stopped by the
  2676. * OCFS2_LOCK_QUEUED flag.
  2677. *
  2678. * We have two potential problems
  2679. *
  2680. * 1) If we do the last reference drop on our dentry_lock (via dput)
  2681. * we'll wind up in ocfs2_release_dentry_lock(), waiting on
  2682. * the downconvert to finish. Instead we take an elevated
  2683. * reference and push the drop until after we've completed our
  2684. * unblock processing.
  2685. *
  2686. * 2) There might be another process with a final reference,
  2687. * waiting on us to finish processing. If this is the case, we
  2688. * detect it and exit out - there's no more dentries anyway.
  2689. */
  2690. static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
  2691. int blocking)
  2692. {
  2693. struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
  2694. struct ocfs2_inode_info *oi = OCFS2_I(dl->dl_inode);
  2695. struct dentry *dentry;
  2696. unsigned long flags;
  2697. int extra_ref = 0;
  2698. /*
  2699. * This node is blocking another node from getting a read
  2700. * lock. This happens when we've renamed within a
  2701. * directory. We've forced the other nodes to d_delete(), but
  2702. * we never actually dropped our lock because it's still
  2703. * valid. The downconvert code will retain a PR for this node,
  2704. * so there's no further work to do.
  2705. */
  2706. if (blocking == DLM_LOCK_PR)
  2707. return UNBLOCK_CONTINUE;
  2708. /*
  2709. * Mark this inode as potentially orphaned. The code in
  2710. * ocfs2_delete_inode() will figure out whether it actually
  2711. * needs to be freed or not.
  2712. */
  2713. spin_lock(&oi->ip_lock);
  2714. oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
  2715. spin_unlock(&oi->ip_lock);
  2716. /*
  2717. * Yuck. We need to make sure however that the check of
  2718. * OCFS2_LOCK_FREEING and the extra reference are atomic with
  2719. * respect to a reference decrement or the setting of that
  2720. * flag.
  2721. */
  2722. spin_lock_irqsave(&lockres->l_lock, flags);
  2723. spin_lock(&dentry_attach_lock);
  2724. if (!(lockres->l_flags & OCFS2_LOCK_FREEING)
  2725. && dl->dl_count) {
  2726. dl->dl_count++;
  2727. extra_ref = 1;
  2728. }
  2729. spin_unlock(&dentry_attach_lock);
  2730. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2731. mlog(0, "extra_ref = %d\n", extra_ref);
  2732. /*
  2733. * We have a process waiting on us in ocfs2_dentry_iput(),
  2734. * which means we can't have any more outstanding
  2735. * aliases. There's no need to do any more work.
  2736. */
  2737. if (!extra_ref)
  2738. return UNBLOCK_CONTINUE;
  2739. spin_lock(&dentry_attach_lock);
  2740. while (1) {
  2741. dentry = ocfs2_find_local_alias(dl->dl_inode,
  2742. dl->dl_parent_blkno, 1);
  2743. if (!dentry)
  2744. break;
  2745. spin_unlock(&dentry_attach_lock);
  2746. mlog(0, "d_delete(%.*s);\n", dentry->d_name.len,
  2747. dentry->d_name.name);
  2748. /*
  2749. * The following dcache calls may do an
  2750. * iput(). Normally we don't want that from the
  2751. * downconverting thread, but in this case it's ok
  2752. * because the requesting node already has an
  2753. * exclusive lock on the inode, so it can't be queued
  2754. * for a downconvert.
  2755. */
  2756. d_delete(dentry);
  2757. dput(dentry);
  2758. spin_lock(&dentry_attach_lock);
  2759. }
  2760. spin_unlock(&dentry_attach_lock);
  2761. /*
  2762. * If we are the last holder of this dentry lock, there is no
  2763. * reason to downconvert so skip straight to the unlock.
  2764. */
  2765. if (dl->dl_count == 1)
  2766. return UNBLOCK_STOP_POST;
  2767. return UNBLOCK_CONTINUE_POST;
  2768. }
  2769. /*
  2770. * This is the filesystem locking protocol. It provides the lock handling
  2771. * hooks for the underlying DLM. It has a maximum version number.
  2772. * The version number allows interoperability with systems running at
  2773. * the same major number and an equal or smaller minor number.
  2774. *
  2775. * Whenever the filesystem does new things with locks (adds or removes a
  2776. * lock, orders them differently, does different things underneath a lock),
  2777. * the version must be changed. The protocol is negotiated when joining
  2778. * the dlm domain. A node may join the domain if its major version is
  2779. * identical to all other nodes and its minor version is greater than
  2780. * or equal to all other nodes. When its minor version is greater than
  2781. * the other nodes, it will run at the minor version specified by the
  2782. * other nodes.
  2783. *
  2784. * If a locking change is made that will not be compatible with older
  2785. * versions, the major number must be increased and the minor version set
  2786. * to zero. If a change merely adds a behavior that can be disabled when
  2787. * speaking to older versions, the minor version must be increased. If a
  2788. * change adds a fully backwards compatible change (eg, LVB changes that
  2789. * are just ignored by older versions), the version does not need to be
  2790. * updated.
  2791. */
  2792. static struct ocfs2_locking_protocol lproto = {
  2793. .lp_max_version = {
  2794. .pv_major = OCFS2_LOCKING_PROTOCOL_MAJOR,
  2795. .pv_minor = OCFS2_LOCKING_PROTOCOL_MINOR,
  2796. },
  2797. .lp_lock_ast = ocfs2_locking_ast,
  2798. .lp_blocking_ast = ocfs2_blocking_ast,
  2799. .lp_unlock_ast = ocfs2_unlock_ast,
  2800. };
  2801. void ocfs2_set_locking_protocol(void)
  2802. {
  2803. ocfs2_stack_glue_set_locking_protocol(&lproto);
  2804. }
  2805. static void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
  2806. struct ocfs2_lock_res *lockres)
  2807. {
  2808. int status;
  2809. struct ocfs2_unblock_ctl ctl = {0, 0,};
  2810. unsigned long flags;
  2811. /* Our reference to the lockres in this function can be
  2812. * considered valid until we remove the OCFS2_LOCK_QUEUED
  2813. * flag. */
  2814. mlog_entry_void();
  2815. BUG_ON(!lockres);
  2816. BUG_ON(!lockres->l_ops);
  2817. mlog(0, "lockres %s blocked.\n", lockres->l_name);
  2818. /* Detect whether a lock has been marked as going away while
  2819. * the downconvert thread was processing other things. A lock can
  2820. * still be marked with OCFS2_LOCK_FREEING after this check,
  2821. * but short circuiting here will still save us some
  2822. * performance. */
  2823. spin_lock_irqsave(&lockres->l_lock, flags);
  2824. if (lockres->l_flags & OCFS2_LOCK_FREEING)
  2825. goto unqueue;
  2826. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2827. status = ocfs2_unblock_lock(osb, lockres, &ctl);
  2828. if (status < 0)
  2829. mlog_errno(status);
  2830. spin_lock_irqsave(&lockres->l_lock, flags);
  2831. unqueue:
  2832. if (lockres->l_flags & OCFS2_LOCK_FREEING || !ctl.requeue) {
  2833. lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
  2834. } else
  2835. ocfs2_schedule_blocked_lock(osb, lockres);
  2836. mlog(0, "lockres %s, requeue = %s.\n", lockres->l_name,
  2837. ctl.requeue ? "yes" : "no");
  2838. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2839. if (ctl.unblock_action != UNBLOCK_CONTINUE
  2840. && lockres->l_ops->post_unlock)
  2841. lockres->l_ops->post_unlock(osb, lockres);
  2842. mlog_exit_void();
  2843. }
  2844. static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
  2845. struct ocfs2_lock_res *lockres)
  2846. {
  2847. mlog_entry_void();
  2848. assert_spin_locked(&lockres->l_lock);
  2849. if (lockres->l_flags & OCFS2_LOCK_FREEING) {
  2850. /* Do not schedule a lock for downconvert when it's on
  2851. * the way to destruction - any nodes wanting access
  2852. * to the resource will get it soon. */
  2853. mlog(0, "Lockres %s won't be scheduled: flags 0x%lx\n",
  2854. lockres->l_name, lockres->l_flags);
  2855. return;
  2856. }
  2857. lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
  2858. spin_lock(&osb->dc_task_lock);
  2859. if (list_empty(&lockres->l_blocked_list)) {
  2860. list_add_tail(&lockres->l_blocked_list,
  2861. &osb->blocked_lock_list);
  2862. osb->blocked_lock_count++;
  2863. }
  2864. spin_unlock(&osb->dc_task_lock);
  2865. mlog_exit_void();
  2866. }
  2867. static void ocfs2_downconvert_thread_do_work(struct ocfs2_super *osb)
  2868. {
  2869. unsigned long processed;
  2870. struct ocfs2_lock_res *lockres;
  2871. mlog_entry_void();
  2872. spin_lock(&osb->dc_task_lock);
  2873. /* grab this early so we know to try again if a state change and
  2874. * wake happens part-way through our work */
  2875. osb->dc_work_sequence = osb->dc_wake_sequence;
  2876. processed = osb->blocked_lock_count;
  2877. while (processed) {
  2878. BUG_ON(list_empty(&osb->blocked_lock_list));
  2879. lockres = list_entry(osb->blocked_lock_list.next,
  2880. struct ocfs2_lock_res, l_blocked_list);
  2881. list_del_init(&lockres->l_blocked_list);
  2882. osb->blocked_lock_count--;
  2883. spin_unlock(&osb->dc_task_lock);
  2884. BUG_ON(!processed);
  2885. processed--;
  2886. ocfs2_process_blocked_lock(osb, lockres);
  2887. spin_lock(&osb->dc_task_lock);
  2888. }
  2889. spin_unlock(&osb->dc_task_lock);
  2890. mlog_exit_void();
  2891. }
  2892. static int ocfs2_downconvert_thread_lists_empty(struct ocfs2_super *osb)
  2893. {
  2894. int empty = 0;
  2895. spin_lock(&osb->dc_task_lock);
  2896. if (list_empty(&osb->blocked_lock_list))
  2897. empty = 1;
  2898. spin_unlock(&osb->dc_task_lock);
  2899. return empty;
  2900. }
  2901. static int ocfs2_downconvert_thread_should_wake(struct ocfs2_super *osb)
  2902. {
  2903. int should_wake = 0;
  2904. spin_lock(&osb->dc_task_lock);
  2905. if (osb->dc_work_sequence != osb->dc_wake_sequence)
  2906. should_wake = 1;
  2907. spin_unlock(&osb->dc_task_lock);
  2908. return should_wake;
  2909. }
  2910. static int ocfs2_downconvert_thread(void *arg)
  2911. {
  2912. int status = 0;
  2913. struct ocfs2_super *osb = arg;
  2914. /* only quit once we've been asked to stop and there is no more
  2915. * work available */
  2916. while (!(kthread_should_stop() &&
  2917. ocfs2_downconvert_thread_lists_empty(osb))) {
  2918. wait_event_interruptible(osb->dc_event,
  2919. ocfs2_downconvert_thread_should_wake(osb) ||
  2920. kthread_should_stop());
  2921. mlog(0, "downconvert_thread: awoken\n");
  2922. ocfs2_downconvert_thread_do_work(osb);
  2923. }
  2924. osb->dc_task = NULL;
  2925. return status;
  2926. }
  2927. void ocfs2_wake_downconvert_thread(struct ocfs2_super *osb)
  2928. {
  2929. spin_lock(&osb->dc_task_lock);
  2930. /* make sure the voting thread gets a swipe at whatever changes
  2931. * the caller may have made to the voting state */
  2932. osb->dc_wake_sequence++;
  2933. spin_unlock(&osb->dc_task_lock);
  2934. wake_up(&osb->dc_event);
  2935. }