dlmglue.c 113 KB

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