dlmglue.c 119 KB

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