dlmglue.c 79 KB

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