dlmglue.c 113 KB

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