dlmglue.c 90 KB

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