dlmglue.c 82 KB

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