dlmglue.c 89 KB

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