dlmglue.c 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105
  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 = ocfs2_inode_sector_count(inode);
  1228. inode->i_uid = be32_to_cpu(lvb->lvb_iuid);
  1229. inode->i_gid = be32_to_cpu(lvb->lvb_igid);
  1230. inode->i_mode = be16_to_cpu(lvb->lvb_imode);
  1231. inode->i_nlink = be16_to_cpu(lvb->lvb_inlink);
  1232. ocfs2_unpack_timespec(&inode->i_atime,
  1233. be64_to_cpu(lvb->lvb_iatime_packed));
  1234. ocfs2_unpack_timespec(&inode->i_mtime,
  1235. be64_to_cpu(lvb->lvb_imtime_packed));
  1236. ocfs2_unpack_timespec(&inode->i_ctime,
  1237. be64_to_cpu(lvb->lvb_ictime_packed));
  1238. spin_unlock(&oi->ip_lock);
  1239. mlog_exit_void();
  1240. }
  1241. static inline int ocfs2_meta_lvb_is_trustable(struct inode *inode,
  1242. struct ocfs2_lock_res *lockres)
  1243. {
  1244. struct ocfs2_meta_lvb *lvb = (struct ocfs2_meta_lvb *) lockres->l_lksb.lvb;
  1245. if (lvb->lvb_version == OCFS2_LVB_VERSION
  1246. && be32_to_cpu(lvb->lvb_igeneration) == inode->i_generation)
  1247. return 1;
  1248. return 0;
  1249. }
  1250. /* Determine whether a lock resource needs to be refreshed, and
  1251. * arbitrate who gets to refresh it.
  1252. *
  1253. * 0 means no refresh needed.
  1254. *
  1255. * > 0 means you need to refresh this and you MUST call
  1256. * ocfs2_complete_lock_res_refresh afterwards. */
  1257. static int ocfs2_should_refresh_lock_res(struct ocfs2_lock_res *lockres)
  1258. {
  1259. unsigned long flags;
  1260. int status = 0;
  1261. mlog_entry_void();
  1262. refresh_check:
  1263. spin_lock_irqsave(&lockres->l_lock, flags);
  1264. if (!(lockres->l_flags & OCFS2_LOCK_NEEDS_REFRESH)) {
  1265. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1266. goto bail;
  1267. }
  1268. if (lockres->l_flags & OCFS2_LOCK_REFRESHING) {
  1269. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1270. ocfs2_wait_on_refreshing_lock(lockres);
  1271. goto refresh_check;
  1272. }
  1273. /* Ok, I'll be the one to refresh this lock. */
  1274. lockres_or_flags(lockres, OCFS2_LOCK_REFRESHING);
  1275. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1276. status = 1;
  1277. bail:
  1278. mlog_exit(status);
  1279. return status;
  1280. }
  1281. /* If status is non zero, I'll mark it as not being in refresh
  1282. * anymroe, but i won't clear the needs refresh flag. */
  1283. static inline void ocfs2_complete_lock_res_refresh(struct ocfs2_lock_res *lockres,
  1284. int status)
  1285. {
  1286. unsigned long flags;
  1287. mlog_entry_void();
  1288. spin_lock_irqsave(&lockres->l_lock, flags);
  1289. lockres_clear_flags(lockres, OCFS2_LOCK_REFRESHING);
  1290. if (!status)
  1291. lockres_clear_flags(lockres, OCFS2_LOCK_NEEDS_REFRESH);
  1292. spin_unlock_irqrestore(&lockres->l_lock, flags);
  1293. wake_up(&lockres->l_event);
  1294. mlog_exit_void();
  1295. }
  1296. /* may or may not return a bh if it went to disk. */
  1297. static int ocfs2_meta_lock_update(struct inode *inode,
  1298. struct buffer_head **bh)
  1299. {
  1300. int status = 0;
  1301. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1302. struct ocfs2_lock_res *lockres = &oi->ip_meta_lockres;
  1303. struct ocfs2_dinode *fe;
  1304. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1305. mlog_entry_void();
  1306. if (ocfs2_mount_local(osb))
  1307. goto bail;
  1308. spin_lock(&oi->ip_lock);
  1309. if (oi->ip_flags & OCFS2_INODE_DELETED) {
  1310. mlog(0, "Orphaned inode %llu was deleted while we "
  1311. "were waiting on a lock. ip_flags = 0x%x\n",
  1312. (unsigned long long)oi->ip_blkno, oi->ip_flags);
  1313. spin_unlock(&oi->ip_lock);
  1314. status = -ENOENT;
  1315. goto bail;
  1316. }
  1317. spin_unlock(&oi->ip_lock);
  1318. if (!ocfs2_should_refresh_lock_res(lockres))
  1319. goto bail;
  1320. /* This will discard any caching information we might have had
  1321. * for the inode metadata. */
  1322. ocfs2_metadata_cache_purge(inode);
  1323. ocfs2_extent_map_trunc(inode, 0);
  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. }