dlmglue.c 113 KB

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