dlmglue.c 115 KB

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