dlmglue.c 75 KB

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