dlmglue.c 95 KB

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