dlmglue.c 113 KB

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