dlmglue.c 106 KB

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