dlmglue.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996
  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 = NULL;
  1221. struct ocfs2_dinode *fe;
  1222. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1223. mlog_entry_void();
  1224. spin_lock(&oi->ip_lock);
  1225. if (oi->ip_flags & OCFS2_INODE_DELETED) {
  1226. mlog(0, "Orphaned inode %llu was deleted while we "
  1227. "were waiting on a lock. ip_flags = 0x%x\n",
  1228. (unsigned long long)oi->ip_blkno, oi->ip_flags);
  1229. spin_unlock(&oi->ip_lock);
  1230. status = -ENOENT;
  1231. goto bail;
  1232. }
  1233. spin_unlock(&oi->ip_lock);
  1234. if (!ocfs2_mount_local(osb)) {
  1235. lockres = &oi->ip_meta_lockres;
  1236. if (!ocfs2_should_refresh_lock_res(lockres))
  1237. goto bail;
  1238. }
  1239. /* This will discard any caching information we might have had
  1240. * for the inode metadata. */
  1241. ocfs2_metadata_cache_purge(inode);
  1242. /* will do nothing for inode types that don't use the extent
  1243. * map (directories, bitmap files, etc) */
  1244. ocfs2_extent_map_trunc(inode, 0);
  1245. if (lockres && ocfs2_meta_lvb_is_trustable(inode, lockres)) {
  1246. mlog(0, "Trusting LVB on inode %llu\n",
  1247. (unsigned long long)oi->ip_blkno);
  1248. ocfs2_refresh_inode_from_lvb(inode);
  1249. } else {
  1250. /* Boo, we have to go to disk. */
  1251. /* read bh, cast, ocfs2_refresh_inode */
  1252. status = ocfs2_read_block(OCFS2_SB(inode->i_sb), oi->ip_blkno,
  1253. bh, OCFS2_BH_CACHED, inode);
  1254. if (status < 0) {
  1255. mlog_errno(status);
  1256. goto bail_refresh;
  1257. }
  1258. fe = (struct ocfs2_dinode *) (*bh)->b_data;
  1259. /* This is a good chance to make sure we're not
  1260. * locking an invalid object.
  1261. *
  1262. * We bug on a stale inode here because we checked
  1263. * above whether it was wiped from disk. The wiping
  1264. * node provides a guarantee that we receive that
  1265. * message and can mark the inode before dropping any
  1266. * locks associated with it. */
  1267. if (!OCFS2_IS_VALID_DINODE(fe)) {
  1268. OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
  1269. status = -EIO;
  1270. goto bail_refresh;
  1271. }
  1272. mlog_bug_on_msg(inode->i_generation !=
  1273. le32_to_cpu(fe->i_generation),
  1274. "Invalid dinode %llu disk generation: %u "
  1275. "inode->i_generation: %u\n",
  1276. (unsigned long long)oi->ip_blkno,
  1277. le32_to_cpu(fe->i_generation),
  1278. inode->i_generation);
  1279. mlog_bug_on_msg(le64_to_cpu(fe->i_dtime) ||
  1280. !(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL)),
  1281. "Stale dinode %llu dtime: %llu flags: 0x%x\n",
  1282. (unsigned long long)oi->ip_blkno,
  1283. (unsigned long long)le64_to_cpu(fe->i_dtime),
  1284. le32_to_cpu(fe->i_flags));
  1285. ocfs2_refresh_inode(inode, fe);
  1286. }
  1287. status = 0;
  1288. bail_refresh:
  1289. if (lockres)
  1290. ocfs2_complete_lock_res_refresh(lockres, status);
  1291. bail:
  1292. mlog_exit(status);
  1293. return status;
  1294. }
  1295. static int ocfs2_assign_bh(struct inode *inode,
  1296. struct buffer_head **ret_bh,
  1297. struct buffer_head *passed_bh)
  1298. {
  1299. int status;
  1300. if (passed_bh) {
  1301. /* Ok, the update went to disk for us, use the
  1302. * returned bh. */
  1303. *ret_bh = passed_bh;
  1304. get_bh(*ret_bh);
  1305. return 0;
  1306. }
  1307. status = ocfs2_read_block(OCFS2_SB(inode->i_sb),
  1308. OCFS2_I(inode)->ip_blkno,
  1309. ret_bh,
  1310. OCFS2_BH_CACHED,
  1311. inode);
  1312. if (status < 0)
  1313. mlog_errno(status);
  1314. return status;
  1315. }
  1316. /*
  1317. * returns < 0 error if the callback will never be called, otherwise
  1318. * the result of the lock will be communicated via the callback.
  1319. */
  1320. int ocfs2_meta_lock_full(struct inode *inode,
  1321. struct buffer_head **ret_bh,
  1322. int ex,
  1323. int arg_flags)
  1324. {
  1325. int status, level, dlm_flags, acquired;
  1326. struct ocfs2_lock_res *lockres = NULL;
  1327. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1328. struct buffer_head *local_bh = NULL;
  1329. BUG_ON(!inode);
  1330. mlog_entry_void();
  1331. mlog(0, "inode %llu, take %s META lock\n",
  1332. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1333. ex ? "EXMODE" : "PRMODE");
  1334. status = 0;
  1335. acquired = 0;
  1336. /* We'll allow faking a readonly metadata lock for
  1337. * rodevices. */
  1338. if (ocfs2_is_hard_readonly(osb)) {
  1339. if (ex)
  1340. status = -EROFS;
  1341. goto bail;
  1342. }
  1343. if (ocfs2_mount_local(osb))
  1344. goto local;
  1345. if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
  1346. wait_event(osb->recovery_event,
  1347. ocfs2_node_map_is_empty(osb, &osb->recovery_map));
  1348. acquired = 0;
  1349. lockres = &OCFS2_I(inode)->ip_meta_lockres;
  1350. level = ex ? LKM_EXMODE : LKM_PRMODE;
  1351. dlm_flags = 0;
  1352. if (arg_flags & OCFS2_META_LOCK_NOQUEUE)
  1353. dlm_flags |= LKM_NOQUEUE;
  1354. status = ocfs2_cluster_lock(osb, lockres, level, dlm_flags, arg_flags);
  1355. if (status < 0) {
  1356. if (status != -EAGAIN && status != -EIOCBRETRY)
  1357. mlog_errno(status);
  1358. goto bail;
  1359. }
  1360. /* Notify the error cleanup path to drop the cluster lock. */
  1361. acquired = 1;
  1362. /* We wait twice because a node may have died while we were in
  1363. * the lower dlm layers. The second time though, we've
  1364. * committed to owning this lock so we don't allow signals to
  1365. * abort the operation. */
  1366. if (!(arg_flags & OCFS2_META_LOCK_RECOVERY))
  1367. wait_event(osb->recovery_event,
  1368. ocfs2_node_map_is_empty(osb, &osb->recovery_map));
  1369. local:
  1370. /*
  1371. * We only see this flag if we're being called from
  1372. * ocfs2_read_locked_inode(). It means we're locking an inode
  1373. * which hasn't been populated yet, so clear the refresh flag
  1374. * and let the caller handle it.
  1375. */
  1376. if (inode->i_state & I_NEW) {
  1377. status = 0;
  1378. if (lockres)
  1379. ocfs2_complete_lock_res_refresh(lockres, 0);
  1380. goto bail;
  1381. }
  1382. /* This is fun. The caller may want a bh back, or it may
  1383. * not. ocfs2_meta_lock_update definitely wants one in, but
  1384. * may or may not read one, depending on what's in the
  1385. * LVB. The result of all of this is that we've *only* gone to
  1386. * disk if we have to, so the complexity is worthwhile. */
  1387. status = ocfs2_meta_lock_update(inode, &local_bh);
  1388. if (status < 0) {
  1389. if (status != -ENOENT)
  1390. mlog_errno(status);
  1391. goto bail;
  1392. }
  1393. if (ret_bh) {
  1394. status = ocfs2_assign_bh(inode, ret_bh, local_bh);
  1395. if (status < 0) {
  1396. mlog_errno(status);
  1397. goto bail;
  1398. }
  1399. }
  1400. bail:
  1401. if (status < 0) {
  1402. if (ret_bh && (*ret_bh)) {
  1403. brelse(*ret_bh);
  1404. *ret_bh = NULL;
  1405. }
  1406. if (acquired)
  1407. ocfs2_meta_unlock(inode, ex);
  1408. }
  1409. if (local_bh)
  1410. brelse(local_bh);
  1411. mlog_exit(status);
  1412. return status;
  1413. }
  1414. /*
  1415. * This is working around a lock inversion between tasks acquiring DLM locks
  1416. * while holding a page lock and the vote thread which blocks dlm lock acquiry
  1417. * while acquiring page locks.
  1418. *
  1419. * ** These _with_page variantes are only intended to be called from aop
  1420. * methods that hold page locks and return a very specific *positive* error
  1421. * code that aop methods pass up to the VFS -- test for errors with != 0. **
  1422. *
  1423. * The DLM is called such that it returns -EAGAIN if it would have blocked
  1424. * waiting for the vote thread. In that case we unlock our page so the vote
  1425. * thread can make progress. Once we've done this we have to return
  1426. * AOP_TRUNCATED_PAGE so the aop method that called us can bubble that back up
  1427. * into the VFS who will then immediately retry the aop call.
  1428. *
  1429. * We do a blocking lock and immediate unlock before returning, though, so that
  1430. * the lock has a great chance of being cached on this node by the time the VFS
  1431. * calls back to retry the aop. This has a potential to livelock as nodes
  1432. * ping locks back and forth, but that's a risk we're willing to take to avoid
  1433. * the lock inversion simply.
  1434. */
  1435. int ocfs2_meta_lock_with_page(struct inode *inode,
  1436. struct buffer_head **ret_bh,
  1437. int ex,
  1438. struct page *page)
  1439. {
  1440. int ret;
  1441. ret = ocfs2_meta_lock_full(inode, ret_bh, ex, OCFS2_LOCK_NONBLOCK);
  1442. if (ret == -EAGAIN) {
  1443. unlock_page(page);
  1444. if (ocfs2_meta_lock(inode, ret_bh, ex) == 0)
  1445. ocfs2_meta_unlock(inode, ex);
  1446. ret = AOP_TRUNCATED_PAGE;
  1447. }
  1448. return ret;
  1449. }
  1450. int ocfs2_meta_lock_atime(struct inode *inode,
  1451. struct vfsmount *vfsmnt,
  1452. int *level)
  1453. {
  1454. int ret;
  1455. mlog_entry_void();
  1456. ret = ocfs2_meta_lock(inode, NULL, 0);
  1457. if (ret < 0) {
  1458. mlog_errno(ret);
  1459. return ret;
  1460. }
  1461. /*
  1462. * If we should update atime, we will get EX lock,
  1463. * otherwise we just get PR lock.
  1464. */
  1465. if (ocfs2_should_update_atime(inode, vfsmnt)) {
  1466. struct buffer_head *bh = NULL;
  1467. ocfs2_meta_unlock(inode, 0);
  1468. ret = ocfs2_meta_lock(inode, &bh, 1);
  1469. if (ret < 0) {
  1470. mlog_errno(ret);
  1471. return ret;
  1472. }
  1473. *level = 1;
  1474. if (ocfs2_should_update_atime(inode, vfsmnt))
  1475. ocfs2_update_inode_atime(inode, bh);
  1476. if (bh)
  1477. brelse(bh);
  1478. } else
  1479. *level = 0;
  1480. mlog_exit(ret);
  1481. return ret;
  1482. }
  1483. void ocfs2_meta_unlock(struct inode *inode,
  1484. int ex)
  1485. {
  1486. int level = ex ? LKM_EXMODE : LKM_PRMODE;
  1487. struct ocfs2_lock_res *lockres = &OCFS2_I(inode)->ip_meta_lockres;
  1488. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1489. mlog_entry_void();
  1490. mlog(0, "inode %llu drop %s META lock\n",
  1491. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1492. ex ? "EXMODE" : "PRMODE");
  1493. if (!ocfs2_is_hard_readonly(OCFS2_SB(inode->i_sb)) &&
  1494. !ocfs2_mount_local(osb))
  1495. ocfs2_cluster_unlock(OCFS2_SB(inode->i_sb), lockres, level);
  1496. mlog_exit_void();
  1497. }
  1498. int ocfs2_super_lock(struct ocfs2_super *osb,
  1499. int ex)
  1500. {
  1501. int status = 0;
  1502. int level = ex ? LKM_EXMODE : LKM_PRMODE;
  1503. struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
  1504. struct buffer_head *bh;
  1505. struct ocfs2_slot_info *si = osb->slot_info;
  1506. mlog_entry_void();
  1507. if (ocfs2_is_hard_readonly(osb))
  1508. return -EROFS;
  1509. if (ocfs2_mount_local(osb))
  1510. goto bail;
  1511. status = ocfs2_cluster_lock(osb, lockres, level, 0, 0);
  1512. if (status < 0) {
  1513. mlog_errno(status);
  1514. goto bail;
  1515. }
  1516. /* The super block lock path is really in the best position to
  1517. * know when resources covered by the lock need to be
  1518. * refreshed, so we do it here. Of course, making sense of
  1519. * everything is up to the caller :) */
  1520. status = ocfs2_should_refresh_lock_res(lockres);
  1521. if (status < 0) {
  1522. mlog_errno(status);
  1523. goto bail;
  1524. }
  1525. if (status) {
  1526. bh = si->si_bh;
  1527. status = ocfs2_read_block(osb, bh->b_blocknr, &bh, 0,
  1528. si->si_inode);
  1529. if (status == 0)
  1530. ocfs2_update_slot_info(si);
  1531. ocfs2_complete_lock_res_refresh(lockres, status);
  1532. if (status < 0)
  1533. mlog_errno(status);
  1534. }
  1535. bail:
  1536. mlog_exit(status);
  1537. return status;
  1538. }
  1539. void ocfs2_super_unlock(struct ocfs2_super *osb,
  1540. int ex)
  1541. {
  1542. int level = ex ? LKM_EXMODE : LKM_PRMODE;
  1543. struct ocfs2_lock_res *lockres = &osb->osb_super_lockres;
  1544. if (!ocfs2_mount_local(osb))
  1545. ocfs2_cluster_unlock(osb, lockres, level);
  1546. }
  1547. int ocfs2_rename_lock(struct ocfs2_super *osb)
  1548. {
  1549. int status;
  1550. struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
  1551. if (ocfs2_is_hard_readonly(osb))
  1552. return -EROFS;
  1553. if (ocfs2_mount_local(osb))
  1554. return 0;
  1555. status = ocfs2_cluster_lock(osb, lockres, LKM_EXMODE, 0, 0);
  1556. if (status < 0)
  1557. mlog_errno(status);
  1558. return status;
  1559. }
  1560. void ocfs2_rename_unlock(struct ocfs2_super *osb)
  1561. {
  1562. struct ocfs2_lock_res *lockres = &osb->osb_rename_lockres;
  1563. if (!ocfs2_mount_local(osb))
  1564. ocfs2_cluster_unlock(osb, lockres, LKM_EXMODE);
  1565. }
  1566. int ocfs2_dentry_lock(struct dentry *dentry, int ex)
  1567. {
  1568. int ret;
  1569. int level = ex ? LKM_EXMODE : LKM_PRMODE;
  1570. struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
  1571. struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
  1572. BUG_ON(!dl);
  1573. if (ocfs2_is_hard_readonly(osb))
  1574. return -EROFS;
  1575. if (ocfs2_mount_local(osb))
  1576. return 0;
  1577. ret = ocfs2_cluster_lock(osb, &dl->dl_lockres, level, 0, 0);
  1578. if (ret < 0)
  1579. mlog_errno(ret);
  1580. return ret;
  1581. }
  1582. void ocfs2_dentry_unlock(struct dentry *dentry, int ex)
  1583. {
  1584. int level = ex ? LKM_EXMODE : LKM_PRMODE;
  1585. struct ocfs2_dentry_lock *dl = dentry->d_fsdata;
  1586. struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
  1587. if (!ocfs2_mount_local(osb))
  1588. ocfs2_cluster_unlock(osb, &dl->dl_lockres, level);
  1589. }
  1590. /* Reference counting of the dlm debug structure. We want this because
  1591. * open references on the debug inodes can live on after a mount, so
  1592. * we can't rely on the ocfs2_super to always exist. */
  1593. static void ocfs2_dlm_debug_free(struct kref *kref)
  1594. {
  1595. struct ocfs2_dlm_debug *dlm_debug;
  1596. dlm_debug = container_of(kref, struct ocfs2_dlm_debug, d_refcnt);
  1597. kfree(dlm_debug);
  1598. }
  1599. void ocfs2_put_dlm_debug(struct ocfs2_dlm_debug *dlm_debug)
  1600. {
  1601. if (dlm_debug)
  1602. kref_put(&dlm_debug->d_refcnt, ocfs2_dlm_debug_free);
  1603. }
  1604. static void ocfs2_get_dlm_debug(struct ocfs2_dlm_debug *debug)
  1605. {
  1606. kref_get(&debug->d_refcnt);
  1607. }
  1608. struct ocfs2_dlm_debug *ocfs2_new_dlm_debug(void)
  1609. {
  1610. struct ocfs2_dlm_debug *dlm_debug;
  1611. dlm_debug = kmalloc(sizeof(struct ocfs2_dlm_debug), GFP_KERNEL);
  1612. if (!dlm_debug) {
  1613. mlog_errno(-ENOMEM);
  1614. goto out;
  1615. }
  1616. kref_init(&dlm_debug->d_refcnt);
  1617. INIT_LIST_HEAD(&dlm_debug->d_lockres_tracking);
  1618. dlm_debug->d_locking_state = NULL;
  1619. out:
  1620. return dlm_debug;
  1621. }
  1622. /* Access to this is arbitrated for us via seq_file->sem. */
  1623. struct ocfs2_dlm_seq_priv {
  1624. struct ocfs2_dlm_debug *p_dlm_debug;
  1625. struct ocfs2_lock_res p_iter_res;
  1626. struct ocfs2_lock_res p_tmp_res;
  1627. };
  1628. static struct ocfs2_lock_res *ocfs2_dlm_next_res(struct ocfs2_lock_res *start,
  1629. struct ocfs2_dlm_seq_priv *priv)
  1630. {
  1631. struct ocfs2_lock_res *iter, *ret = NULL;
  1632. struct ocfs2_dlm_debug *dlm_debug = priv->p_dlm_debug;
  1633. assert_spin_locked(&ocfs2_dlm_tracking_lock);
  1634. list_for_each_entry(iter, &start->l_debug_list, l_debug_list) {
  1635. /* discover the head of the list */
  1636. if (&iter->l_debug_list == &dlm_debug->d_lockres_tracking) {
  1637. mlog(0, "End of list found, %p\n", ret);
  1638. break;
  1639. }
  1640. /* We track our "dummy" iteration lockres' by a NULL
  1641. * l_ops field. */
  1642. if (iter->l_ops != NULL) {
  1643. ret = iter;
  1644. break;
  1645. }
  1646. }
  1647. return ret;
  1648. }
  1649. static void *ocfs2_dlm_seq_start(struct seq_file *m, loff_t *pos)
  1650. {
  1651. struct ocfs2_dlm_seq_priv *priv = m->private;
  1652. struct ocfs2_lock_res *iter;
  1653. spin_lock(&ocfs2_dlm_tracking_lock);
  1654. iter = ocfs2_dlm_next_res(&priv->p_iter_res, priv);
  1655. if (iter) {
  1656. /* Since lockres' have the lifetime of their container
  1657. * (which can be inodes, ocfs2_supers, etc) we want to
  1658. * copy this out to a temporary lockres while still
  1659. * under the spinlock. Obviously after this we can't
  1660. * trust any pointers on the copy returned, but that's
  1661. * ok as the information we want isn't typically held
  1662. * in them. */
  1663. priv->p_tmp_res = *iter;
  1664. iter = &priv->p_tmp_res;
  1665. }
  1666. spin_unlock(&ocfs2_dlm_tracking_lock);
  1667. return iter;
  1668. }
  1669. static void ocfs2_dlm_seq_stop(struct seq_file *m, void *v)
  1670. {
  1671. }
  1672. static void *ocfs2_dlm_seq_next(struct seq_file *m, void *v, loff_t *pos)
  1673. {
  1674. struct ocfs2_dlm_seq_priv *priv = m->private;
  1675. struct ocfs2_lock_res *iter = v;
  1676. struct ocfs2_lock_res *dummy = &priv->p_iter_res;
  1677. spin_lock(&ocfs2_dlm_tracking_lock);
  1678. iter = ocfs2_dlm_next_res(iter, priv);
  1679. list_del_init(&dummy->l_debug_list);
  1680. if (iter) {
  1681. list_add(&dummy->l_debug_list, &iter->l_debug_list);
  1682. priv->p_tmp_res = *iter;
  1683. iter = &priv->p_tmp_res;
  1684. }
  1685. spin_unlock(&ocfs2_dlm_tracking_lock);
  1686. return iter;
  1687. }
  1688. /* So that debugfs.ocfs2 can determine which format is being used */
  1689. #define OCFS2_DLM_DEBUG_STR_VERSION 1
  1690. static int ocfs2_dlm_seq_show(struct seq_file *m, void *v)
  1691. {
  1692. int i;
  1693. char *lvb;
  1694. struct ocfs2_lock_res *lockres = v;
  1695. if (!lockres)
  1696. return -EINVAL;
  1697. seq_printf(m, "0x%x\t", OCFS2_DLM_DEBUG_STR_VERSION);
  1698. if (lockres->l_type == OCFS2_LOCK_TYPE_DENTRY)
  1699. seq_printf(m, "%.*s%08x\t", OCFS2_DENTRY_LOCK_INO_START - 1,
  1700. lockres->l_name,
  1701. (unsigned int)ocfs2_get_dentry_lock_ino(lockres));
  1702. else
  1703. seq_printf(m, "%.*s\t", OCFS2_LOCK_ID_MAX_LEN, lockres->l_name);
  1704. seq_printf(m, "%d\t"
  1705. "0x%lx\t"
  1706. "0x%x\t"
  1707. "0x%x\t"
  1708. "%u\t"
  1709. "%u\t"
  1710. "%d\t"
  1711. "%d\t",
  1712. lockres->l_level,
  1713. lockres->l_flags,
  1714. lockres->l_action,
  1715. lockres->l_unlock_action,
  1716. lockres->l_ro_holders,
  1717. lockres->l_ex_holders,
  1718. lockres->l_requested,
  1719. lockres->l_blocking);
  1720. /* Dump the raw LVB */
  1721. lvb = lockres->l_lksb.lvb;
  1722. for(i = 0; i < DLM_LVB_LEN; i++)
  1723. seq_printf(m, "0x%x\t", lvb[i]);
  1724. /* End the line */
  1725. seq_printf(m, "\n");
  1726. return 0;
  1727. }
  1728. static struct seq_operations ocfs2_dlm_seq_ops = {
  1729. .start = ocfs2_dlm_seq_start,
  1730. .stop = ocfs2_dlm_seq_stop,
  1731. .next = ocfs2_dlm_seq_next,
  1732. .show = ocfs2_dlm_seq_show,
  1733. };
  1734. static int ocfs2_dlm_debug_release(struct inode *inode, struct file *file)
  1735. {
  1736. struct seq_file *seq = (struct seq_file *) file->private_data;
  1737. struct ocfs2_dlm_seq_priv *priv = seq->private;
  1738. struct ocfs2_lock_res *res = &priv->p_iter_res;
  1739. ocfs2_remove_lockres_tracking(res);
  1740. ocfs2_put_dlm_debug(priv->p_dlm_debug);
  1741. return seq_release_private(inode, file);
  1742. }
  1743. static int ocfs2_dlm_debug_open(struct inode *inode, struct file *file)
  1744. {
  1745. int ret;
  1746. struct ocfs2_dlm_seq_priv *priv;
  1747. struct seq_file *seq;
  1748. struct ocfs2_super *osb;
  1749. priv = kzalloc(sizeof(struct ocfs2_dlm_seq_priv), GFP_KERNEL);
  1750. if (!priv) {
  1751. ret = -ENOMEM;
  1752. mlog_errno(ret);
  1753. goto out;
  1754. }
  1755. osb = inode->i_private;
  1756. ocfs2_get_dlm_debug(osb->osb_dlm_debug);
  1757. priv->p_dlm_debug = osb->osb_dlm_debug;
  1758. INIT_LIST_HEAD(&priv->p_iter_res.l_debug_list);
  1759. ret = seq_open(file, &ocfs2_dlm_seq_ops);
  1760. if (ret) {
  1761. kfree(priv);
  1762. mlog_errno(ret);
  1763. goto out;
  1764. }
  1765. seq = (struct seq_file *) file->private_data;
  1766. seq->private = priv;
  1767. ocfs2_add_lockres_tracking(&priv->p_iter_res,
  1768. priv->p_dlm_debug);
  1769. out:
  1770. return ret;
  1771. }
  1772. static const struct file_operations ocfs2_dlm_debug_fops = {
  1773. .open = ocfs2_dlm_debug_open,
  1774. .release = ocfs2_dlm_debug_release,
  1775. .read = seq_read,
  1776. .llseek = seq_lseek,
  1777. };
  1778. static int ocfs2_dlm_init_debug(struct ocfs2_super *osb)
  1779. {
  1780. int ret = 0;
  1781. struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
  1782. dlm_debug->d_locking_state = debugfs_create_file("locking_state",
  1783. S_IFREG|S_IRUSR,
  1784. osb->osb_debug_root,
  1785. osb,
  1786. &ocfs2_dlm_debug_fops);
  1787. if (!dlm_debug->d_locking_state) {
  1788. ret = -EINVAL;
  1789. mlog(ML_ERROR,
  1790. "Unable to create locking state debugfs file.\n");
  1791. goto out;
  1792. }
  1793. ocfs2_get_dlm_debug(dlm_debug);
  1794. out:
  1795. return ret;
  1796. }
  1797. static void ocfs2_dlm_shutdown_debug(struct ocfs2_super *osb)
  1798. {
  1799. struct ocfs2_dlm_debug *dlm_debug = osb->osb_dlm_debug;
  1800. if (dlm_debug) {
  1801. debugfs_remove(dlm_debug->d_locking_state);
  1802. ocfs2_put_dlm_debug(dlm_debug);
  1803. }
  1804. }
  1805. int ocfs2_dlm_init(struct ocfs2_super *osb)
  1806. {
  1807. int status = 0;
  1808. u32 dlm_key;
  1809. struct dlm_ctxt *dlm = NULL;
  1810. mlog_entry_void();
  1811. if (ocfs2_mount_local(osb))
  1812. goto local;
  1813. status = ocfs2_dlm_init_debug(osb);
  1814. if (status < 0) {
  1815. mlog_errno(status);
  1816. goto bail;
  1817. }
  1818. /* launch vote thread */
  1819. osb->vote_task = kthread_run(ocfs2_vote_thread, osb, "ocfs2vote");
  1820. if (IS_ERR(osb->vote_task)) {
  1821. status = PTR_ERR(osb->vote_task);
  1822. osb->vote_task = NULL;
  1823. mlog_errno(status);
  1824. goto bail;
  1825. }
  1826. /* used by the dlm code to make message headers unique, each
  1827. * node in this domain must agree on this. */
  1828. dlm_key = crc32_le(0, osb->uuid_str, strlen(osb->uuid_str));
  1829. /* for now, uuid == domain */
  1830. dlm = dlm_register_domain(osb->uuid_str, dlm_key);
  1831. if (IS_ERR(dlm)) {
  1832. status = PTR_ERR(dlm);
  1833. mlog_errno(status);
  1834. goto bail;
  1835. }
  1836. dlm_register_eviction_cb(dlm, &osb->osb_eviction_cb);
  1837. local:
  1838. ocfs2_super_lock_res_init(&osb->osb_super_lockres, osb);
  1839. ocfs2_rename_lock_res_init(&osb->osb_rename_lockres, osb);
  1840. osb->dlm = dlm;
  1841. status = 0;
  1842. bail:
  1843. if (status < 0) {
  1844. ocfs2_dlm_shutdown_debug(osb);
  1845. if (osb->vote_task)
  1846. kthread_stop(osb->vote_task);
  1847. }
  1848. mlog_exit(status);
  1849. return status;
  1850. }
  1851. void ocfs2_dlm_shutdown(struct ocfs2_super *osb)
  1852. {
  1853. mlog_entry_void();
  1854. dlm_unregister_eviction_cb(&osb->osb_eviction_cb);
  1855. ocfs2_drop_osb_locks(osb);
  1856. if (osb->vote_task) {
  1857. kthread_stop(osb->vote_task);
  1858. osb->vote_task = NULL;
  1859. }
  1860. ocfs2_lock_res_free(&osb->osb_super_lockres);
  1861. ocfs2_lock_res_free(&osb->osb_rename_lockres);
  1862. dlm_unregister_domain(osb->dlm);
  1863. osb->dlm = NULL;
  1864. ocfs2_dlm_shutdown_debug(osb);
  1865. mlog_exit_void();
  1866. }
  1867. static void ocfs2_unlock_ast(void *opaque, enum dlm_status status)
  1868. {
  1869. struct ocfs2_lock_res *lockres = opaque;
  1870. unsigned long flags;
  1871. mlog_entry_void();
  1872. mlog(0, "UNLOCK AST called on lock %s, action = %d\n", lockres->l_name,
  1873. lockres->l_unlock_action);
  1874. spin_lock_irqsave(&lockres->l_lock, flags);
  1875. /* We tried to cancel a convert request, but it was already
  1876. * granted. All we want to do here is clear our unlock
  1877. * state. The wake_up call done at the bottom is redundant
  1878. * (ocfs2_prepare_cancel_convert doesn't sleep on this) but doesn't
  1879. * hurt anything anyway */
  1880. if (status == DLM_CANCELGRANT &&
  1881. lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
  1882. mlog(0, "Got cancelgrant for %s\n", lockres->l_name);
  1883. /* We don't clear the busy flag in this case as it
  1884. * should have been cleared by the ast which the dlm
  1885. * has called. */
  1886. goto complete_unlock;
  1887. }
  1888. if (status != DLM_NORMAL) {
  1889. mlog(ML_ERROR, "Dlm passes status %d for lock %s, "
  1890. "unlock_action %d\n", status, lockres->l_name,
  1891. lockres->l_unlock_action);
  1892. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1893. return;
  1894. }
  1895. switch(lockres->l_unlock_action) {
  1896. case OCFS2_UNLOCK_CANCEL_CONVERT:
  1897. mlog(0, "Cancel convert success for %s\n", lockres->l_name);
  1898. lockres->l_action = OCFS2_AST_INVALID;
  1899. break;
  1900. case OCFS2_UNLOCK_DROP_LOCK:
  1901. lockres->l_level = LKM_IVMODE;
  1902. break;
  1903. default:
  1904. BUG();
  1905. }
  1906. lockres_clear_flags(lockres, OCFS2_LOCK_BUSY);
  1907. complete_unlock:
  1908. lockres->l_unlock_action = OCFS2_UNLOCK_INVALID;
  1909. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1910. wake_up(&lockres->l_event);
  1911. mlog_exit_void();
  1912. }
  1913. static int ocfs2_drop_lock(struct ocfs2_super *osb,
  1914. struct ocfs2_lock_res *lockres)
  1915. {
  1916. enum dlm_status status;
  1917. unsigned long flags;
  1918. int lkm_flags = 0;
  1919. /* We didn't get anywhere near actually using this lockres. */
  1920. if (!(lockres->l_flags & OCFS2_LOCK_INITIALIZED))
  1921. goto out;
  1922. if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB)
  1923. lkm_flags |= LKM_VALBLK;
  1924. spin_lock_irqsave(&lockres->l_lock, flags);
  1925. mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_FREEING),
  1926. "lockres %s, flags 0x%lx\n",
  1927. lockres->l_name, lockres->l_flags);
  1928. while (lockres->l_flags & OCFS2_LOCK_BUSY) {
  1929. mlog(0, "waiting on busy lock \"%s\": flags = %lx, action = "
  1930. "%u, unlock_action = %u\n",
  1931. lockres->l_name, lockres->l_flags, lockres->l_action,
  1932. lockres->l_unlock_action);
  1933. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1934. /* XXX: Today we just wait on any busy
  1935. * locks... Perhaps we need to cancel converts in the
  1936. * future? */
  1937. ocfs2_wait_on_busy_lock(lockres);
  1938. spin_lock_irqsave(&lockres->l_lock, flags);
  1939. }
  1940. if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
  1941. if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
  1942. lockres->l_level == LKM_EXMODE &&
  1943. !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
  1944. lockres->l_ops->set_lvb(lockres);
  1945. }
  1946. if (lockres->l_flags & OCFS2_LOCK_BUSY)
  1947. mlog(ML_ERROR, "destroying busy lock: \"%s\"\n",
  1948. lockres->l_name);
  1949. if (lockres->l_flags & OCFS2_LOCK_BLOCKED)
  1950. mlog(0, "destroying blocked lock: \"%s\"\n", lockres->l_name);
  1951. if (!(lockres->l_flags & OCFS2_LOCK_ATTACHED)) {
  1952. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1953. goto out;
  1954. }
  1955. lockres_clear_flags(lockres, OCFS2_LOCK_ATTACHED);
  1956. /* make sure we never get here while waiting for an ast to
  1957. * fire. */
  1958. BUG_ON(lockres->l_action != OCFS2_AST_INVALID);
  1959. /* is this necessary? */
  1960. lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
  1961. lockres->l_unlock_action = OCFS2_UNLOCK_DROP_LOCK;
  1962. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1963. mlog(0, "lock %s\n", lockres->l_name);
  1964. status = dlmunlock(osb->dlm, &lockres->l_lksb, lkm_flags,
  1965. ocfs2_unlock_ast, lockres);
  1966. if (status != DLM_NORMAL) {
  1967. ocfs2_log_dlm_error("dlmunlock", status, lockres);
  1968. mlog(ML_ERROR, "lockres flags: %lu\n", lockres->l_flags);
  1969. dlm_print_one_lock(lockres->l_lksb.lockid);
  1970. BUG();
  1971. }
  1972. mlog(0, "lock %s, successfull return from dlmunlock\n",
  1973. lockres->l_name);
  1974. ocfs2_wait_on_busy_lock(lockres);
  1975. out:
  1976. mlog_exit(0);
  1977. return 0;
  1978. }
  1979. /* Mark the lockres as being dropped. It will no longer be
  1980. * queued if blocking, but we still may have to wait on it
  1981. * being dequeued from the vote thread before we can consider
  1982. * it safe to drop.
  1983. *
  1984. * You can *not* attempt to call cluster_lock on this lockres anymore. */
  1985. void ocfs2_mark_lockres_freeing(struct ocfs2_lock_res *lockres)
  1986. {
  1987. int status;
  1988. struct ocfs2_mask_waiter mw;
  1989. unsigned long flags;
  1990. ocfs2_init_mask_waiter(&mw);
  1991. spin_lock_irqsave(&lockres->l_lock, flags);
  1992. lockres->l_flags |= OCFS2_LOCK_FREEING;
  1993. while (lockres->l_flags & OCFS2_LOCK_QUEUED) {
  1994. lockres_add_mask_waiter(lockres, &mw, OCFS2_LOCK_QUEUED, 0);
  1995. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1996. mlog(0, "Waiting on lockres %s\n", lockres->l_name);
  1997. status = ocfs2_wait_for_mask(&mw);
  1998. if (status)
  1999. mlog_errno(status);
  2000. spin_lock_irqsave(&lockres->l_lock, flags);
  2001. }
  2002. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2003. }
  2004. void ocfs2_simple_drop_lockres(struct ocfs2_super *osb,
  2005. struct ocfs2_lock_res *lockres)
  2006. {
  2007. int ret;
  2008. ocfs2_mark_lockres_freeing(lockres);
  2009. ret = ocfs2_drop_lock(osb, lockres);
  2010. if (ret)
  2011. mlog_errno(ret);
  2012. }
  2013. static void ocfs2_drop_osb_locks(struct ocfs2_super *osb)
  2014. {
  2015. ocfs2_simple_drop_lockres(osb, &osb->osb_super_lockres);
  2016. ocfs2_simple_drop_lockres(osb, &osb->osb_rename_lockres);
  2017. }
  2018. int ocfs2_drop_inode_locks(struct inode *inode)
  2019. {
  2020. int status, err;
  2021. mlog_entry_void();
  2022. /* No need to call ocfs2_mark_lockres_freeing here -
  2023. * ocfs2_clear_inode has done it for us. */
  2024. err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
  2025. &OCFS2_I(inode)->ip_data_lockres);
  2026. if (err < 0)
  2027. mlog_errno(err);
  2028. status = err;
  2029. err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
  2030. &OCFS2_I(inode)->ip_meta_lockres);
  2031. if (err < 0)
  2032. mlog_errno(err);
  2033. if (err < 0 && !status)
  2034. status = err;
  2035. err = ocfs2_drop_lock(OCFS2_SB(inode->i_sb),
  2036. &OCFS2_I(inode)->ip_rw_lockres);
  2037. if (err < 0)
  2038. mlog_errno(err);
  2039. if (err < 0 && !status)
  2040. status = err;
  2041. mlog_exit(status);
  2042. return status;
  2043. }
  2044. static void ocfs2_prepare_downconvert(struct ocfs2_lock_res *lockres,
  2045. int new_level)
  2046. {
  2047. assert_spin_locked(&lockres->l_lock);
  2048. BUG_ON(lockres->l_blocking <= LKM_NLMODE);
  2049. if (lockres->l_level <= new_level) {
  2050. mlog(ML_ERROR, "lockres->l_level (%u) <= new_level (%u)\n",
  2051. lockres->l_level, new_level);
  2052. BUG();
  2053. }
  2054. mlog(0, "lock %s, new_level = %d, l_blocking = %d\n",
  2055. lockres->l_name, new_level, lockres->l_blocking);
  2056. lockres->l_action = OCFS2_AST_DOWNCONVERT;
  2057. lockres->l_requested = new_level;
  2058. lockres_or_flags(lockres, OCFS2_LOCK_BUSY);
  2059. }
  2060. static int ocfs2_downconvert_lock(struct ocfs2_super *osb,
  2061. struct ocfs2_lock_res *lockres,
  2062. int new_level,
  2063. int lvb)
  2064. {
  2065. int ret, dlm_flags = LKM_CONVERT;
  2066. enum dlm_status status;
  2067. mlog_entry_void();
  2068. if (lvb)
  2069. dlm_flags |= LKM_VALBLK;
  2070. status = dlmlock(osb->dlm,
  2071. new_level,
  2072. &lockres->l_lksb,
  2073. dlm_flags,
  2074. lockres->l_name,
  2075. OCFS2_LOCK_ID_MAX_LEN - 1,
  2076. ocfs2_locking_ast,
  2077. lockres,
  2078. ocfs2_blocking_ast);
  2079. if (status != DLM_NORMAL) {
  2080. ocfs2_log_dlm_error("dlmlock", status, lockres);
  2081. ret = -EINVAL;
  2082. ocfs2_recover_from_dlm_error(lockres, 1);
  2083. goto bail;
  2084. }
  2085. ret = 0;
  2086. bail:
  2087. mlog_exit(ret);
  2088. return ret;
  2089. }
  2090. /* returns 1 when the caller should unlock and call dlmunlock */
  2091. static int ocfs2_prepare_cancel_convert(struct ocfs2_super *osb,
  2092. struct ocfs2_lock_res *lockres)
  2093. {
  2094. assert_spin_locked(&lockres->l_lock);
  2095. mlog_entry_void();
  2096. mlog(0, "lock %s\n", lockres->l_name);
  2097. if (lockres->l_unlock_action == OCFS2_UNLOCK_CANCEL_CONVERT) {
  2098. /* If we're already trying to cancel a lock conversion
  2099. * then just drop the spinlock and allow the caller to
  2100. * requeue this lock. */
  2101. mlog(0, "Lockres %s, skip convert\n", lockres->l_name);
  2102. return 0;
  2103. }
  2104. /* were we in a convert when we got the bast fire? */
  2105. BUG_ON(lockres->l_action != OCFS2_AST_CONVERT &&
  2106. lockres->l_action != OCFS2_AST_DOWNCONVERT);
  2107. /* set things up for the unlockast to know to just
  2108. * clear out the ast_action and unset busy, etc. */
  2109. lockres->l_unlock_action = OCFS2_UNLOCK_CANCEL_CONVERT;
  2110. mlog_bug_on_msg(!(lockres->l_flags & OCFS2_LOCK_BUSY),
  2111. "lock %s, invalid flags: 0x%lx\n",
  2112. lockres->l_name, lockres->l_flags);
  2113. return 1;
  2114. }
  2115. static int ocfs2_cancel_convert(struct ocfs2_super *osb,
  2116. struct ocfs2_lock_res *lockres)
  2117. {
  2118. int ret;
  2119. enum dlm_status status;
  2120. mlog_entry_void();
  2121. mlog(0, "lock %s\n", lockres->l_name);
  2122. ret = 0;
  2123. status = dlmunlock(osb->dlm,
  2124. &lockres->l_lksb,
  2125. LKM_CANCEL,
  2126. ocfs2_unlock_ast,
  2127. lockres);
  2128. if (status != DLM_NORMAL) {
  2129. ocfs2_log_dlm_error("dlmunlock", status, lockres);
  2130. ret = -EINVAL;
  2131. ocfs2_recover_from_dlm_error(lockres, 0);
  2132. }
  2133. mlog(0, "lock %s return from dlmunlock\n", lockres->l_name);
  2134. mlog_exit(ret);
  2135. return ret;
  2136. }
  2137. static int ocfs2_unblock_lock(struct ocfs2_super *osb,
  2138. struct ocfs2_lock_res *lockres,
  2139. struct ocfs2_unblock_ctl *ctl)
  2140. {
  2141. unsigned long flags;
  2142. int blocking;
  2143. int new_level;
  2144. int ret = 0;
  2145. int set_lvb = 0;
  2146. mlog_entry_void();
  2147. spin_lock_irqsave(&lockres->l_lock, flags);
  2148. BUG_ON(!(lockres->l_flags & OCFS2_LOCK_BLOCKED));
  2149. recheck:
  2150. if (lockres->l_flags & OCFS2_LOCK_BUSY) {
  2151. ctl->requeue = 1;
  2152. ret = ocfs2_prepare_cancel_convert(osb, lockres);
  2153. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2154. if (ret) {
  2155. ret = ocfs2_cancel_convert(osb, lockres);
  2156. if (ret < 0)
  2157. mlog_errno(ret);
  2158. }
  2159. goto leave;
  2160. }
  2161. /* if we're blocking an exclusive and we have *any* holders,
  2162. * then requeue. */
  2163. if ((lockres->l_blocking == LKM_EXMODE)
  2164. && (lockres->l_ex_holders || lockres->l_ro_holders))
  2165. goto leave_requeue;
  2166. /* If it's a PR we're blocking, then only
  2167. * requeue if we've got any EX holders */
  2168. if (lockres->l_blocking == LKM_PRMODE &&
  2169. lockres->l_ex_holders)
  2170. goto leave_requeue;
  2171. /*
  2172. * Can we get a lock in this state if the holder counts are
  2173. * zero? The meta data unblock code used to check this.
  2174. */
  2175. if ((lockres->l_ops->flags & LOCK_TYPE_REQUIRES_REFRESH)
  2176. && (lockres->l_flags & OCFS2_LOCK_REFRESHING))
  2177. goto leave_requeue;
  2178. new_level = ocfs2_highest_compat_lock_level(lockres->l_blocking);
  2179. if (lockres->l_ops->check_downconvert
  2180. && !lockres->l_ops->check_downconvert(lockres, new_level))
  2181. goto leave_requeue;
  2182. /* If we get here, then we know that there are no more
  2183. * incompatible holders (and anyone asking for an incompatible
  2184. * lock is blocked). We can now downconvert the lock */
  2185. if (!lockres->l_ops->downconvert_worker)
  2186. goto downconvert;
  2187. /* Some lockres types want to do a bit of work before
  2188. * downconverting a lock. Allow that here. The worker function
  2189. * may sleep, so we save off a copy of what we're blocking as
  2190. * it may change while we're not holding the spin lock. */
  2191. blocking = lockres->l_blocking;
  2192. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2193. ctl->unblock_action = lockres->l_ops->downconvert_worker(lockres, blocking);
  2194. if (ctl->unblock_action == UNBLOCK_STOP_POST)
  2195. goto leave;
  2196. spin_lock_irqsave(&lockres->l_lock, flags);
  2197. if (blocking != lockres->l_blocking) {
  2198. /* If this changed underneath us, then we can't drop
  2199. * it just yet. */
  2200. goto recheck;
  2201. }
  2202. downconvert:
  2203. ctl->requeue = 0;
  2204. if (lockres->l_ops->flags & LOCK_TYPE_USES_LVB) {
  2205. if (lockres->l_level == LKM_EXMODE)
  2206. set_lvb = 1;
  2207. /*
  2208. * We only set the lvb if the lock has been fully
  2209. * refreshed - otherwise we risk setting stale
  2210. * data. Otherwise, there's no need to actually clear
  2211. * out the lvb here as it's value is still valid.
  2212. */
  2213. if (set_lvb && !(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH))
  2214. lockres->l_ops->set_lvb(lockres);
  2215. }
  2216. ocfs2_prepare_downconvert(lockres, new_level);
  2217. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2218. ret = ocfs2_downconvert_lock(osb, lockres, new_level, set_lvb);
  2219. leave:
  2220. mlog_exit(ret);
  2221. return ret;
  2222. leave_requeue:
  2223. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2224. ctl->requeue = 1;
  2225. mlog_exit(0);
  2226. return 0;
  2227. }
  2228. static int ocfs2_data_convert_worker(struct ocfs2_lock_res *lockres,
  2229. int blocking)
  2230. {
  2231. struct inode *inode;
  2232. struct address_space *mapping;
  2233. inode = ocfs2_lock_res_inode(lockres);
  2234. mapping = inode->i_mapping;
  2235. /*
  2236. * We need this before the filemap_fdatawrite() so that it can
  2237. * transfer the dirty bit from the PTE to the
  2238. * page. Unfortunately this means that even for EX->PR
  2239. * downconverts, we'll lose our mappings and have to build
  2240. * them up again.
  2241. */
  2242. unmap_mapping_range(mapping, 0, 0, 0);
  2243. if (filemap_fdatawrite(mapping)) {
  2244. mlog(ML_ERROR, "Could not sync inode %llu for downconvert!",
  2245. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  2246. }
  2247. sync_mapping_buffers(mapping);
  2248. if (blocking == LKM_EXMODE) {
  2249. truncate_inode_pages(mapping, 0);
  2250. } else {
  2251. /* We only need to wait on the I/O if we're not also
  2252. * truncating pages because truncate_inode_pages waits
  2253. * for us above. We don't truncate pages if we're
  2254. * blocking anything < EXMODE because we want to keep
  2255. * them around in that case. */
  2256. filemap_fdatawait(mapping);
  2257. }
  2258. return UNBLOCK_CONTINUE;
  2259. }
  2260. static int ocfs2_check_meta_downconvert(struct ocfs2_lock_res *lockres,
  2261. int new_level)
  2262. {
  2263. struct inode *inode = ocfs2_lock_res_inode(lockres);
  2264. int checkpointed = ocfs2_inode_fully_checkpointed(inode);
  2265. BUG_ON(new_level != LKM_NLMODE && new_level != LKM_PRMODE);
  2266. BUG_ON(lockres->l_level != LKM_EXMODE && !checkpointed);
  2267. if (checkpointed)
  2268. return 1;
  2269. ocfs2_start_checkpoint(OCFS2_SB(inode->i_sb));
  2270. return 0;
  2271. }
  2272. static void ocfs2_set_meta_lvb(struct ocfs2_lock_res *lockres)
  2273. {
  2274. struct inode *inode = ocfs2_lock_res_inode(lockres);
  2275. __ocfs2_stuff_meta_lvb(inode);
  2276. }
  2277. /*
  2278. * Does the final reference drop on our dentry lock. Right now this
  2279. * happens in the vote thread, but we could choose to simplify the
  2280. * dlmglue API and push these off to the ocfs2_wq in the future.
  2281. */
  2282. static void ocfs2_dentry_post_unlock(struct ocfs2_super *osb,
  2283. struct ocfs2_lock_res *lockres)
  2284. {
  2285. struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
  2286. ocfs2_dentry_lock_put(osb, dl);
  2287. }
  2288. /*
  2289. * d_delete() matching dentries before the lock downconvert.
  2290. *
  2291. * At this point, any process waiting to destroy the
  2292. * dentry_lock due to last ref count is stopped by the
  2293. * OCFS2_LOCK_QUEUED flag.
  2294. *
  2295. * We have two potential problems
  2296. *
  2297. * 1) If we do the last reference drop on our dentry_lock (via dput)
  2298. * we'll wind up in ocfs2_release_dentry_lock(), waiting on
  2299. * the downconvert to finish. Instead we take an elevated
  2300. * reference and push the drop until after we've completed our
  2301. * unblock processing.
  2302. *
  2303. * 2) There might be another process with a final reference,
  2304. * waiting on us to finish processing. If this is the case, we
  2305. * detect it and exit out - there's no more dentries anyway.
  2306. */
  2307. static int ocfs2_dentry_convert_worker(struct ocfs2_lock_res *lockres,
  2308. int blocking)
  2309. {
  2310. struct ocfs2_dentry_lock *dl = ocfs2_lock_res_dl(lockres);
  2311. struct ocfs2_inode_info *oi = OCFS2_I(dl->dl_inode);
  2312. struct dentry *dentry;
  2313. unsigned long flags;
  2314. int extra_ref = 0;
  2315. /*
  2316. * This node is blocking another node from getting a read
  2317. * lock. This happens when we've renamed within a
  2318. * directory. We've forced the other nodes to d_delete(), but
  2319. * we never actually dropped our lock because it's still
  2320. * valid. The downconvert code will retain a PR for this node,
  2321. * so there's no further work to do.
  2322. */
  2323. if (blocking == LKM_PRMODE)
  2324. return UNBLOCK_CONTINUE;
  2325. /*
  2326. * Mark this inode as potentially orphaned. The code in
  2327. * ocfs2_delete_inode() will figure out whether it actually
  2328. * needs to be freed or not.
  2329. */
  2330. spin_lock(&oi->ip_lock);
  2331. oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
  2332. spin_unlock(&oi->ip_lock);
  2333. /*
  2334. * Yuck. We need to make sure however that the check of
  2335. * OCFS2_LOCK_FREEING and the extra reference are atomic with
  2336. * respect to a reference decrement or the setting of that
  2337. * flag.
  2338. */
  2339. spin_lock_irqsave(&lockres->l_lock, flags);
  2340. spin_lock(&dentry_attach_lock);
  2341. if (!(lockres->l_flags & OCFS2_LOCK_FREEING)
  2342. && dl->dl_count) {
  2343. dl->dl_count++;
  2344. extra_ref = 1;
  2345. }
  2346. spin_unlock(&dentry_attach_lock);
  2347. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2348. mlog(0, "extra_ref = %d\n", extra_ref);
  2349. /*
  2350. * We have a process waiting on us in ocfs2_dentry_iput(),
  2351. * which means we can't have any more outstanding
  2352. * aliases. There's no need to do any more work.
  2353. */
  2354. if (!extra_ref)
  2355. return UNBLOCK_CONTINUE;
  2356. spin_lock(&dentry_attach_lock);
  2357. while (1) {
  2358. dentry = ocfs2_find_local_alias(dl->dl_inode,
  2359. dl->dl_parent_blkno, 1);
  2360. if (!dentry)
  2361. break;
  2362. spin_unlock(&dentry_attach_lock);
  2363. mlog(0, "d_delete(%.*s);\n", dentry->d_name.len,
  2364. dentry->d_name.name);
  2365. /*
  2366. * The following dcache calls may do an
  2367. * iput(). Normally we don't want that from the
  2368. * downconverting thread, but in this case it's ok
  2369. * because the requesting node already has an
  2370. * exclusive lock on the inode, so it can't be queued
  2371. * for a downconvert.
  2372. */
  2373. d_delete(dentry);
  2374. dput(dentry);
  2375. spin_lock(&dentry_attach_lock);
  2376. }
  2377. spin_unlock(&dentry_attach_lock);
  2378. /*
  2379. * If we are the last holder of this dentry lock, there is no
  2380. * reason to downconvert so skip straight to the unlock.
  2381. */
  2382. if (dl->dl_count == 1)
  2383. return UNBLOCK_STOP_POST;
  2384. return UNBLOCK_CONTINUE_POST;
  2385. }
  2386. void ocfs2_process_blocked_lock(struct ocfs2_super *osb,
  2387. struct ocfs2_lock_res *lockres)
  2388. {
  2389. int status;
  2390. struct ocfs2_unblock_ctl ctl = {0, 0,};
  2391. unsigned long flags;
  2392. /* Our reference to the lockres in this function can be
  2393. * considered valid until we remove the OCFS2_LOCK_QUEUED
  2394. * flag. */
  2395. mlog_entry_void();
  2396. BUG_ON(!lockres);
  2397. BUG_ON(!lockres->l_ops);
  2398. mlog(0, "lockres %s blocked.\n", lockres->l_name);
  2399. /* Detect whether a lock has been marked as going away while
  2400. * the vote thread was processing other things. A lock can
  2401. * still be marked with OCFS2_LOCK_FREEING after this check,
  2402. * but short circuiting here will still save us some
  2403. * performance. */
  2404. spin_lock_irqsave(&lockres->l_lock, flags);
  2405. if (lockres->l_flags & OCFS2_LOCK_FREEING)
  2406. goto unqueue;
  2407. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2408. status = ocfs2_unblock_lock(osb, lockres, &ctl);
  2409. if (status < 0)
  2410. mlog_errno(status);
  2411. spin_lock_irqsave(&lockres->l_lock, flags);
  2412. unqueue:
  2413. if (lockres->l_flags & OCFS2_LOCK_FREEING || !ctl.requeue) {
  2414. lockres_clear_flags(lockres, OCFS2_LOCK_QUEUED);
  2415. } else
  2416. ocfs2_schedule_blocked_lock(osb, lockres);
  2417. mlog(0, "lockres %s, requeue = %s.\n", lockres->l_name,
  2418. ctl.requeue ? "yes" : "no");
  2419. spin_unlock_irqrestore(&lockres->l_lock, flags);
  2420. if (ctl.unblock_action != UNBLOCK_CONTINUE
  2421. && lockres->l_ops->post_unlock)
  2422. lockres->l_ops->post_unlock(osb, lockres);
  2423. mlog_exit_void();
  2424. }
  2425. static void ocfs2_schedule_blocked_lock(struct ocfs2_super *osb,
  2426. struct ocfs2_lock_res *lockres)
  2427. {
  2428. mlog_entry_void();
  2429. assert_spin_locked(&lockres->l_lock);
  2430. if (lockres->l_flags & OCFS2_LOCK_FREEING) {
  2431. /* Do not schedule a lock for downconvert when it's on
  2432. * the way to destruction - any nodes wanting access
  2433. * to the resource will get it soon. */
  2434. mlog(0, "Lockres %s won't be scheduled: flags 0x%lx\n",
  2435. lockres->l_name, lockres->l_flags);
  2436. return;
  2437. }
  2438. lockres_or_flags(lockres, OCFS2_LOCK_QUEUED);
  2439. spin_lock(&osb->vote_task_lock);
  2440. if (list_empty(&lockres->l_blocked_list)) {
  2441. list_add_tail(&lockres->l_blocked_list,
  2442. &osb->blocked_lock_list);
  2443. osb->blocked_lock_count++;
  2444. }
  2445. spin_unlock(&osb->vote_task_lock);
  2446. mlog_exit_void();
  2447. }
  2448. /* This aids in debugging situations where a bad LVB might be involved. */
  2449. void ocfs2_dump_meta_lvb_info(u64 level,
  2450. const char *function,
  2451. unsigned int line,
  2452. struct ocfs2_lock_res *lockres)
  2453. {
  2454. struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
  2455. mlog(level, "LVB information for %s (called from %s:%u):\n",
  2456. lockres->l_name, function, line);
  2457. mlog(level, "version: %u, clusters: %u, generation: 0x%x\n",
  2458. lvb->lvb_version, be32_to_cpu(lvb->lvb_iclusters),
  2459. be32_to_cpu(lvb->lvb_igeneration));
  2460. mlog(level, "size: %llu, uid %u, gid %u, mode 0x%x\n",
  2461. (unsigned long long)be64_to_cpu(lvb->lvb_isize),
  2462. be32_to_cpu(lvb->lvb_iuid), be32_to_cpu(lvb->lvb_igid),
  2463. be16_to_cpu(lvb->lvb_imode));
  2464. mlog(level, "nlink %u, atime_packed 0x%llx, ctime_packed 0x%llx, "
  2465. "mtime_packed 0x%llx iattr 0x%x\n", be16_to_cpu(lvb->lvb_inlink),
  2466. (long long)be64_to_cpu(lvb->lvb_iatime_packed),
  2467. (long long)be64_to_cpu(lvb->lvb_ictime_packed),
  2468. (long long)be64_to_cpu(lvb->lvb_imtime_packed),
  2469. be32_to_cpu(lvb->lvb_iattr));
  2470. }