dlmglue.c 90 KB

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