dlmglue.c 81 KB

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