dlmglue.c 116 KB

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