dlmglue.c 81 KB

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