dlmglue.c 81 KB

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