dlmglue.c 85 KB

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