dlmglue.c 90 KB

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