dlmglue.c 103 KB

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