dlmglue.c 83 KB

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