xfs_log_recover.c 108 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084
  1. /*
  2. * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_types.h"
  21. #include "xfs_bit.h"
  22. #include "xfs_log.h"
  23. #include "xfs_inum.h"
  24. #include "xfs_trans.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_dir2.h"
  28. #include "xfs_dmapi.h"
  29. #include "xfs_mount.h"
  30. #include "xfs_error.h"
  31. #include "xfs_bmap_btree.h"
  32. #include "xfs_alloc_btree.h"
  33. #include "xfs_ialloc_btree.h"
  34. #include "xfs_dir2_sf.h"
  35. #include "xfs_attr_sf.h"
  36. #include "xfs_dinode.h"
  37. #include "xfs_inode.h"
  38. #include "xfs_inode_item.h"
  39. #include "xfs_alloc.h"
  40. #include "xfs_ialloc.h"
  41. #include "xfs_log_priv.h"
  42. #include "xfs_buf_item.h"
  43. #include "xfs_log_recover.h"
  44. #include "xfs_extfree_item.h"
  45. #include "xfs_trans_priv.h"
  46. #include "xfs_quota.h"
  47. #include "xfs_rw.h"
  48. #include "xfs_utils.h"
  49. STATIC int xlog_find_zeroed(xlog_t *, xfs_daddr_t *);
  50. STATIC int xlog_clear_stale_blocks(xlog_t *, xfs_lsn_t);
  51. STATIC void xlog_recover_insert_item_backq(xlog_recover_item_t **q,
  52. xlog_recover_item_t *item);
  53. #if defined(DEBUG)
  54. STATIC void xlog_recover_check_summary(xlog_t *);
  55. #else
  56. #define xlog_recover_check_summary(log)
  57. #endif
  58. /*
  59. * Sector aligned buffer routines for buffer create/read/write/access
  60. */
  61. #define XLOG_SECTOR_ROUNDUP_BBCOUNT(log, bbs) \
  62. ( ((log)->l_sectbb_mask && (bbs & (log)->l_sectbb_mask)) ? \
  63. ((bbs + (log)->l_sectbb_mask + 1) & ~(log)->l_sectbb_mask) : (bbs) )
  64. #define XLOG_SECTOR_ROUNDDOWN_BLKNO(log, bno) ((bno) & ~(log)->l_sectbb_mask)
  65. xfs_buf_t *
  66. xlog_get_bp(
  67. xlog_t *log,
  68. int nbblks)
  69. {
  70. if (nbblks <= 0 || nbblks > log->l_logBBsize) {
  71. xlog_warn("XFS: Invalid block length (0x%x) given for buffer", nbblks);
  72. XFS_ERROR_REPORT("xlog_get_bp(1)",
  73. XFS_ERRLEVEL_HIGH, log->l_mp);
  74. return NULL;
  75. }
  76. if (log->l_sectbb_log) {
  77. if (nbblks > 1)
  78. nbblks += XLOG_SECTOR_ROUNDUP_BBCOUNT(log, 1);
  79. nbblks = XLOG_SECTOR_ROUNDUP_BBCOUNT(log, nbblks);
  80. }
  81. return xfs_buf_get_noaddr(BBTOB(nbblks), log->l_mp->m_logdev_targp);
  82. }
  83. void
  84. xlog_put_bp(
  85. xfs_buf_t *bp)
  86. {
  87. xfs_buf_free(bp);
  88. }
  89. STATIC xfs_caddr_t
  90. xlog_align(
  91. xlog_t *log,
  92. xfs_daddr_t blk_no,
  93. int nbblks,
  94. xfs_buf_t *bp)
  95. {
  96. xfs_caddr_t ptr;
  97. if (!log->l_sectbb_log)
  98. return XFS_BUF_PTR(bp);
  99. ptr = XFS_BUF_PTR(bp) + BBTOB((int)blk_no & log->l_sectbb_mask);
  100. ASSERT(XFS_BUF_SIZE(bp) >=
  101. BBTOB(nbblks + (blk_no & log->l_sectbb_mask)));
  102. return ptr;
  103. }
  104. /*
  105. * nbblks should be uint, but oh well. Just want to catch that 32-bit length.
  106. */
  107. STATIC int
  108. xlog_bread_noalign(
  109. xlog_t *log,
  110. xfs_daddr_t blk_no,
  111. int nbblks,
  112. xfs_buf_t *bp)
  113. {
  114. int error;
  115. if (nbblks <= 0 || nbblks > log->l_logBBsize) {
  116. xlog_warn("XFS: Invalid block length (0x%x) given for buffer", nbblks);
  117. XFS_ERROR_REPORT("xlog_bread(1)",
  118. XFS_ERRLEVEL_HIGH, log->l_mp);
  119. return EFSCORRUPTED;
  120. }
  121. if (log->l_sectbb_log) {
  122. blk_no = XLOG_SECTOR_ROUNDDOWN_BLKNO(log, blk_no);
  123. nbblks = XLOG_SECTOR_ROUNDUP_BBCOUNT(log, nbblks);
  124. }
  125. ASSERT(nbblks > 0);
  126. ASSERT(BBTOB(nbblks) <= XFS_BUF_SIZE(bp));
  127. ASSERT(bp);
  128. XFS_BUF_SET_ADDR(bp, log->l_logBBstart + blk_no);
  129. XFS_BUF_READ(bp);
  130. XFS_BUF_BUSY(bp);
  131. XFS_BUF_SET_COUNT(bp, BBTOB(nbblks));
  132. XFS_BUF_SET_TARGET(bp, log->l_mp->m_logdev_targp);
  133. xfsbdstrat(log->l_mp, bp);
  134. error = xfs_iowait(bp);
  135. if (error)
  136. xfs_ioerror_alert("xlog_bread", log->l_mp,
  137. bp, XFS_BUF_ADDR(bp));
  138. return error;
  139. }
  140. STATIC int
  141. xlog_bread(
  142. xlog_t *log,
  143. xfs_daddr_t blk_no,
  144. int nbblks,
  145. xfs_buf_t *bp,
  146. xfs_caddr_t *offset)
  147. {
  148. int error;
  149. error = xlog_bread_noalign(log, blk_no, nbblks, bp);
  150. if (error)
  151. return error;
  152. *offset = xlog_align(log, blk_no, nbblks, bp);
  153. return 0;
  154. }
  155. /*
  156. * Write out the buffer at the given block for the given number of blocks.
  157. * The buffer is kept locked across the write and is returned locked.
  158. * This can only be used for synchronous log writes.
  159. */
  160. STATIC int
  161. xlog_bwrite(
  162. xlog_t *log,
  163. xfs_daddr_t blk_no,
  164. int nbblks,
  165. xfs_buf_t *bp)
  166. {
  167. int error;
  168. if (nbblks <= 0 || nbblks > log->l_logBBsize) {
  169. xlog_warn("XFS: Invalid block length (0x%x) given for buffer", nbblks);
  170. XFS_ERROR_REPORT("xlog_bwrite(1)",
  171. XFS_ERRLEVEL_HIGH, log->l_mp);
  172. return EFSCORRUPTED;
  173. }
  174. if (log->l_sectbb_log) {
  175. blk_no = XLOG_SECTOR_ROUNDDOWN_BLKNO(log, blk_no);
  176. nbblks = XLOG_SECTOR_ROUNDUP_BBCOUNT(log, nbblks);
  177. }
  178. ASSERT(nbblks > 0);
  179. ASSERT(BBTOB(nbblks) <= XFS_BUF_SIZE(bp));
  180. XFS_BUF_SET_ADDR(bp, log->l_logBBstart + blk_no);
  181. XFS_BUF_ZEROFLAGS(bp);
  182. XFS_BUF_BUSY(bp);
  183. XFS_BUF_HOLD(bp);
  184. XFS_BUF_PSEMA(bp, PRIBIO);
  185. XFS_BUF_SET_COUNT(bp, BBTOB(nbblks));
  186. XFS_BUF_SET_TARGET(bp, log->l_mp->m_logdev_targp);
  187. if ((error = xfs_bwrite(log->l_mp, bp)))
  188. xfs_ioerror_alert("xlog_bwrite", log->l_mp,
  189. bp, XFS_BUF_ADDR(bp));
  190. return error;
  191. }
  192. #ifdef DEBUG
  193. /*
  194. * dump debug superblock and log record information
  195. */
  196. STATIC void
  197. xlog_header_check_dump(
  198. xfs_mount_t *mp,
  199. xlog_rec_header_t *head)
  200. {
  201. int b;
  202. cmn_err(CE_DEBUG, "%s: SB : uuid = ", __func__);
  203. for (b = 0; b < 16; b++)
  204. cmn_err(CE_DEBUG, "%02x", ((__uint8_t *)&mp->m_sb.sb_uuid)[b]);
  205. cmn_err(CE_DEBUG, ", fmt = %d\n", XLOG_FMT);
  206. cmn_err(CE_DEBUG, " log : uuid = ");
  207. for (b = 0; b < 16; b++)
  208. cmn_err(CE_DEBUG, "%02x", ((__uint8_t *)&head->h_fs_uuid)[b]);
  209. cmn_err(CE_DEBUG, ", fmt = %d\n", be32_to_cpu(head->h_fmt));
  210. }
  211. #else
  212. #define xlog_header_check_dump(mp, head)
  213. #endif
  214. /*
  215. * check log record header for recovery
  216. */
  217. STATIC int
  218. xlog_header_check_recover(
  219. xfs_mount_t *mp,
  220. xlog_rec_header_t *head)
  221. {
  222. ASSERT(be32_to_cpu(head->h_magicno) == XLOG_HEADER_MAGIC_NUM);
  223. /*
  224. * IRIX doesn't write the h_fmt field and leaves it zeroed
  225. * (XLOG_FMT_UNKNOWN). This stops us from trying to recover
  226. * a dirty log created in IRIX.
  227. */
  228. if (unlikely(be32_to_cpu(head->h_fmt) != XLOG_FMT)) {
  229. xlog_warn(
  230. "XFS: dirty log written in incompatible format - can't recover");
  231. xlog_header_check_dump(mp, head);
  232. XFS_ERROR_REPORT("xlog_header_check_recover(1)",
  233. XFS_ERRLEVEL_HIGH, mp);
  234. return XFS_ERROR(EFSCORRUPTED);
  235. } else if (unlikely(!uuid_equal(&mp->m_sb.sb_uuid, &head->h_fs_uuid))) {
  236. xlog_warn(
  237. "XFS: dirty log entry has mismatched uuid - can't recover");
  238. xlog_header_check_dump(mp, head);
  239. XFS_ERROR_REPORT("xlog_header_check_recover(2)",
  240. XFS_ERRLEVEL_HIGH, mp);
  241. return XFS_ERROR(EFSCORRUPTED);
  242. }
  243. return 0;
  244. }
  245. /*
  246. * read the head block of the log and check the header
  247. */
  248. STATIC int
  249. xlog_header_check_mount(
  250. xfs_mount_t *mp,
  251. xlog_rec_header_t *head)
  252. {
  253. ASSERT(be32_to_cpu(head->h_magicno) == XLOG_HEADER_MAGIC_NUM);
  254. if (uuid_is_nil(&head->h_fs_uuid)) {
  255. /*
  256. * IRIX doesn't write the h_fs_uuid or h_fmt fields. If
  257. * h_fs_uuid is nil, we assume this log was last mounted
  258. * by IRIX and continue.
  259. */
  260. xlog_warn("XFS: nil uuid in log - IRIX style log");
  261. } else if (unlikely(!uuid_equal(&mp->m_sb.sb_uuid, &head->h_fs_uuid))) {
  262. xlog_warn("XFS: log has mismatched uuid - can't recover");
  263. xlog_header_check_dump(mp, head);
  264. XFS_ERROR_REPORT("xlog_header_check_mount",
  265. XFS_ERRLEVEL_HIGH, mp);
  266. return XFS_ERROR(EFSCORRUPTED);
  267. }
  268. return 0;
  269. }
  270. STATIC void
  271. xlog_recover_iodone(
  272. struct xfs_buf *bp)
  273. {
  274. if (XFS_BUF_GETERROR(bp)) {
  275. /*
  276. * We're not going to bother about retrying
  277. * this during recovery. One strike!
  278. */
  279. xfs_ioerror_alert("xlog_recover_iodone",
  280. bp->b_mount, bp, XFS_BUF_ADDR(bp));
  281. xfs_force_shutdown(bp->b_mount, SHUTDOWN_META_IO_ERROR);
  282. }
  283. bp->b_mount = NULL;
  284. XFS_BUF_CLR_IODONE_FUNC(bp);
  285. xfs_biodone(bp);
  286. }
  287. /*
  288. * This routine finds (to an approximation) the first block in the physical
  289. * log which contains the given cycle. It uses a binary search algorithm.
  290. * Note that the algorithm can not be perfect because the disk will not
  291. * necessarily be perfect.
  292. */
  293. STATIC int
  294. xlog_find_cycle_start(
  295. xlog_t *log,
  296. xfs_buf_t *bp,
  297. xfs_daddr_t first_blk,
  298. xfs_daddr_t *last_blk,
  299. uint cycle)
  300. {
  301. xfs_caddr_t offset;
  302. xfs_daddr_t mid_blk;
  303. uint mid_cycle;
  304. int error;
  305. mid_blk = BLK_AVG(first_blk, *last_blk);
  306. while (mid_blk != first_blk && mid_blk != *last_blk) {
  307. error = xlog_bread(log, mid_blk, 1, bp, &offset);
  308. if (error)
  309. return error;
  310. mid_cycle = xlog_get_cycle(offset);
  311. if (mid_cycle == cycle) {
  312. *last_blk = mid_blk;
  313. /* last_half_cycle == mid_cycle */
  314. } else {
  315. first_blk = mid_blk;
  316. /* first_half_cycle == mid_cycle */
  317. }
  318. mid_blk = BLK_AVG(first_blk, *last_blk);
  319. }
  320. ASSERT((mid_blk == first_blk && mid_blk+1 == *last_blk) ||
  321. (mid_blk == *last_blk && mid_blk-1 == first_blk));
  322. return 0;
  323. }
  324. /*
  325. * Check that the range of blocks does not contain the cycle number
  326. * given. The scan needs to occur from front to back and the ptr into the
  327. * region must be updated since a later routine will need to perform another
  328. * test. If the region is completely good, we end up returning the same
  329. * last block number.
  330. *
  331. * Set blkno to -1 if we encounter no errors. This is an invalid block number
  332. * since we don't ever expect logs to get this large.
  333. */
  334. STATIC int
  335. xlog_find_verify_cycle(
  336. xlog_t *log,
  337. xfs_daddr_t start_blk,
  338. int nbblks,
  339. uint stop_on_cycle_no,
  340. xfs_daddr_t *new_blk)
  341. {
  342. xfs_daddr_t i, j;
  343. uint cycle;
  344. xfs_buf_t *bp;
  345. xfs_daddr_t bufblks;
  346. xfs_caddr_t buf = NULL;
  347. int error = 0;
  348. bufblks = 1 << ffs(nbblks);
  349. while (!(bp = xlog_get_bp(log, bufblks))) {
  350. /* can't get enough memory to do everything in one big buffer */
  351. bufblks >>= 1;
  352. if (bufblks <= log->l_sectbb_log)
  353. return ENOMEM;
  354. }
  355. for (i = start_blk; i < start_blk + nbblks; i += bufblks) {
  356. int bcount;
  357. bcount = min(bufblks, (start_blk + nbblks - i));
  358. error = xlog_bread(log, i, bcount, bp, &buf);
  359. if (error)
  360. goto out;
  361. for (j = 0; j < bcount; j++) {
  362. cycle = xlog_get_cycle(buf);
  363. if (cycle == stop_on_cycle_no) {
  364. *new_blk = i+j;
  365. goto out;
  366. }
  367. buf += BBSIZE;
  368. }
  369. }
  370. *new_blk = -1;
  371. out:
  372. xlog_put_bp(bp);
  373. return error;
  374. }
  375. /*
  376. * Potentially backup over partial log record write.
  377. *
  378. * In the typical case, last_blk is the number of the block directly after
  379. * a good log record. Therefore, we subtract one to get the block number
  380. * of the last block in the given buffer. extra_bblks contains the number
  381. * of blocks we would have read on a previous read. This happens when the
  382. * last log record is split over the end of the physical log.
  383. *
  384. * extra_bblks is the number of blocks potentially verified on a previous
  385. * call to this routine.
  386. */
  387. STATIC int
  388. xlog_find_verify_log_record(
  389. xlog_t *log,
  390. xfs_daddr_t start_blk,
  391. xfs_daddr_t *last_blk,
  392. int extra_bblks)
  393. {
  394. xfs_daddr_t i;
  395. xfs_buf_t *bp;
  396. xfs_caddr_t offset = NULL;
  397. xlog_rec_header_t *head = NULL;
  398. int error = 0;
  399. int smallmem = 0;
  400. int num_blks = *last_blk - start_blk;
  401. int xhdrs;
  402. ASSERT(start_blk != 0 || *last_blk != start_blk);
  403. if (!(bp = xlog_get_bp(log, num_blks))) {
  404. if (!(bp = xlog_get_bp(log, 1)))
  405. return ENOMEM;
  406. smallmem = 1;
  407. } else {
  408. error = xlog_bread(log, start_blk, num_blks, bp, &offset);
  409. if (error)
  410. goto out;
  411. offset += ((num_blks - 1) << BBSHIFT);
  412. }
  413. for (i = (*last_blk) - 1; i >= 0; i--) {
  414. if (i < start_blk) {
  415. /* valid log record not found */
  416. xlog_warn(
  417. "XFS: Log inconsistent (didn't find previous header)");
  418. ASSERT(0);
  419. error = XFS_ERROR(EIO);
  420. goto out;
  421. }
  422. if (smallmem) {
  423. error = xlog_bread(log, i, 1, bp, &offset);
  424. if (error)
  425. goto out;
  426. }
  427. head = (xlog_rec_header_t *)offset;
  428. if (XLOG_HEADER_MAGIC_NUM == be32_to_cpu(head->h_magicno))
  429. break;
  430. if (!smallmem)
  431. offset -= BBSIZE;
  432. }
  433. /*
  434. * We hit the beginning of the physical log & still no header. Return
  435. * to caller. If caller can handle a return of -1, then this routine
  436. * will be called again for the end of the physical log.
  437. */
  438. if (i == -1) {
  439. error = -1;
  440. goto out;
  441. }
  442. /*
  443. * We have the final block of the good log (the first block
  444. * of the log record _before_ the head. So we check the uuid.
  445. */
  446. if ((error = xlog_header_check_mount(log->l_mp, head)))
  447. goto out;
  448. /*
  449. * We may have found a log record header before we expected one.
  450. * last_blk will be the 1st block # with a given cycle #. We may end
  451. * up reading an entire log record. In this case, we don't want to
  452. * reset last_blk. Only when last_blk points in the middle of a log
  453. * record do we update last_blk.
  454. */
  455. if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
  456. uint h_size = be32_to_cpu(head->h_size);
  457. xhdrs = h_size / XLOG_HEADER_CYCLE_SIZE;
  458. if (h_size % XLOG_HEADER_CYCLE_SIZE)
  459. xhdrs++;
  460. } else {
  461. xhdrs = 1;
  462. }
  463. if (*last_blk - i + extra_bblks !=
  464. BTOBB(be32_to_cpu(head->h_len)) + xhdrs)
  465. *last_blk = i;
  466. out:
  467. xlog_put_bp(bp);
  468. return error;
  469. }
  470. /*
  471. * Head is defined to be the point of the log where the next log write
  472. * write could go. This means that incomplete LR writes at the end are
  473. * eliminated when calculating the head. We aren't guaranteed that previous
  474. * LR have complete transactions. We only know that a cycle number of
  475. * current cycle number -1 won't be present in the log if we start writing
  476. * from our current block number.
  477. *
  478. * last_blk contains the block number of the first block with a given
  479. * cycle number.
  480. *
  481. * Return: zero if normal, non-zero if error.
  482. */
  483. STATIC int
  484. xlog_find_head(
  485. xlog_t *log,
  486. xfs_daddr_t *return_head_blk)
  487. {
  488. xfs_buf_t *bp;
  489. xfs_caddr_t offset;
  490. xfs_daddr_t new_blk, first_blk, start_blk, last_blk, head_blk;
  491. int num_scan_bblks;
  492. uint first_half_cycle, last_half_cycle;
  493. uint stop_on_cycle;
  494. int error, log_bbnum = log->l_logBBsize;
  495. /* Is the end of the log device zeroed? */
  496. if ((error = xlog_find_zeroed(log, &first_blk)) == -1) {
  497. *return_head_blk = first_blk;
  498. /* Is the whole lot zeroed? */
  499. if (!first_blk) {
  500. /* Linux XFS shouldn't generate totally zeroed logs -
  501. * mkfs etc write a dummy unmount record to a fresh
  502. * log so we can store the uuid in there
  503. */
  504. xlog_warn("XFS: totally zeroed log");
  505. }
  506. return 0;
  507. } else if (error) {
  508. xlog_warn("XFS: empty log check failed");
  509. return error;
  510. }
  511. first_blk = 0; /* get cycle # of 1st block */
  512. bp = xlog_get_bp(log, 1);
  513. if (!bp)
  514. return ENOMEM;
  515. error = xlog_bread(log, 0, 1, bp, &offset);
  516. if (error)
  517. goto bp_err;
  518. first_half_cycle = xlog_get_cycle(offset);
  519. last_blk = head_blk = log_bbnum - 1; /* get cycle # of last block */
  520. error = xlog_bread(log, last_blk, 1, bp, &offset);
  521. if (error)
  522. goto bp_err;
  523. last_half_cycle = xlog_get_cycle(offset);
  524. ASSERT(last_half_cycle != 0);
  525. /*
  526. * If the 1st half cycle number is equal to the last half cycle number,
  527. * then the entire log is stamped with the same cycle number. In this
  528. * case, head_blk can't be set to zero (which makes sense). The below
  529. * math doesn't work out properly with head_blk equal to zero. Instead,
  530. * we set it to log_bbnum which is an invalid block number, but this
  531. * value makes the math correct. If head_blk doesn't changed through
  532. * all the tests below, *head_blk is set to zero at the very end rather
  533. * than log_bbnum. In a sense, log_bbnum and zero are the same block
  534. * in a circular file.
  535. */
  536. if (first_half_cycle == last_half_cycle) {
  537. /*
  538. * In this case we believe that the entire log should have
  539. * cycle number last_half_cycle. We need to scan backwards
  540. * from the end verifying that there are no holes still
  541. * containing last_half_cycle - 1. If we find such a hole,
  542. * then the start of that hole will be the new head. The
  543. * simple case looks like
  544. * x | x ... | x - 1 | x
  545. * Another case that fits this picture would be
  546. * x | x + 1 | x ... | x
  547. * In this case the head really is somewhere at the end of the
  548. * log, as one of the latest writes at the beginning was
  549. * incomplete.
  550. * One more case is
  551. * x | x + 1 | x ... | x - 1 | x
  552. * This is really the combination of the above two cases, and
  553. * the head has to end up at the start of the x-1 hole at the
  554. * end of the log.
  555. *
  556. * In the 256k log case, we will read from the beginning to the
  557. * end of the log and search for cycle numbers equal to x-1.
  558. * We don't worry about the x+1 blocks that we encounter,
  559. * because we know that they cannot be the head since the log
  560. * started with x.
  561. */
  562. head_blk = log_bbnum;
  563. stop_on_cycle = last_half_cycle - 1;
  564. } else {
  565. /*
  566. * In this case we want to find the first block with cycle
  567. * number matching last_half_cycle. We expect the log to be
  568. * some variation on
  569. * x + 1 ... | x ...
  570. * The first block with cycle number x (last_half_cycle) will
  571. * be where the new head belongs. First we do a binary search
  572. * for the first occurrence of last_half_cycle. The binary
  573. * search may not be totally accurate, so then we scan back
  574. * from there looking for occurrences of last_half_cycle before
  575. * us. If that backwards scan wraps around the beginning of
  576. * the log, then we look for occurrences of last_half_cycle - 1
  577. * at the end of the log. The cases we're looking for look
  578. * like
  579. * x + 1 ... | x | x + 1 | x ...
  580. * ^ binary search stopped here
  581. * or
  582. * x + 1 ... | x ... | x - 1 | x
  583. * <---------> less than scan distance
  584. */
  585. stop_on_cycle = last_half_cycle;
  586. if ((error = xlog_find_cycle_start(log, bp, first_blk,
  587. &head_blk, last_half_cycle)))
  588. goto bp_err;
  589. }
  590. /*
  591. * Now validate the answer. Scan back some number of maximum possible
  592. * blocks and make sure each one has the expected cycle number. The
  593. * maximum is determined by the total possible amount of buffering
  594. * in the in-core log. The following number can be made tighter if
  595. * we actually look at the block size of the filesystem.
  596. */
  597. num_scan_bblks = XLOG_TOTAL_REC_SHIFT(log);
  598. if (head_blk >= num_scan_bblks) {
  599. /*
  600. * We are guaranteed that the entire check can be performed
  601. * in one buffer.
  602. */
  603. start_blk = head_blk - num_scan_bblks;
  604. if ((error = xlog_find_verify_cycle(log,
  605. start_blk, num_scan_bblks,
  606. stop_on_cycle, &new_blk)))
  607. goto bp_err;
  608. if (new_blk != -1)
  609. head_blk = new_blk;
  610. } else { /* need to read 2 parts of log */
  611. /*
  612. * We are going to scan backwards in the log in two parts.
  613. * First we scan the physical end of the log. In this part
  614. * of the log, we are looking for blocks with cycle number
  615. * last_half_cycle - 1.
  616. * If we find one, then we know that the log starts there, as
  617. * we've found a hole that didn't get written in going around
  618. * the end of the physical log. The simple case for this is
  619. * x + 1 ... | x ... | x - 1 | x
  620. * <---------> less than scan distance
  621. * If all of the blocks at the end of the log have cycle number
  622. * last_half_cycle, then we check the blocks at the start of
  623. * the log looking for occurrences of last_half_cycle. If we
  624. * find one, then our current estimate for the location of the
  625. * first occurrence of last_half_cycle is wrong and we move
  626. * back to the hole we've found. This case looks like
  627. * x + 1 ... | x | x + 1 | x ...
  628. * ^ binary search stopped here
  629. * Another case we need to handle that only occurs in 256k
  630. * logs is
  631. * x + 1 ... | x ... | x+1 | x ...
  632. * ^ binary search stops here
  633. * In a 256k log, the scan at the end of the log will see the
  634. * x + 1 blocks. We need to skip past those since that is
  635. * certainly not the head of the log. By searching for
  636. * last_half_cycle-1 we accomplish that.
  637. */
  638. start_blk = log_bbnum - num_scan_bblks + head_blk;
  639. ASSERT(head_blk <= INT_MAX &&
  640. (xfs_daddr_t) num_scan_bblks - head_blk >= 0);
  641. if ((error = xlog_find_verify_cycle(log, start_blk,
  642. num_scan_bblks - (int)head_blk,
  643. (stop_on_cycle - 1), &new_blk)))
  644. goto bp_err;
  645. if (new_blk != -1) {
  646. head_blk = new_blk;
  647. goto bad_blk;
  648. }
  649. /*
  650. * Scan beginning of log now. The last part of the physical
  651. * log is good. This scan needs to verify that it doesn't find
  652. * the last_half_cycle.
  653. */
  654. start_blk = 0;
  655. ASSERT(head_blk <= INT_MAX);
  656. if ((error = xlog_find_verify_cycle(log,
  657. start_blk, (int)head_blk,
  658. stop_on_cycle, &new_blk)))
  659. goto bp_err;
  660. if (new_blk != -1)
  661. head_blk = new_blk;
  662. }
  663. bad_blk:
  664. /*
  665. * Now we need to make sure head_blk is not pointing to a block in
  666. * the middle of a log record.
  667. */
  668. num_scan_bblks = XLOG_REC_SHIFT(log);
  669. if (head_blk >= num_scan_bblks) {
  670. start_blk = head_blk - num_scan_bblks; /* don't read head_blk */
  671. /* start ptr at last block ptr before head_blk */
  672. if ((error = xlog_find_verify_log_record(log, start_blk,
  673. &head_blk, 0)) == -1) {
  674. error = XFS_ERROR(EIO);
  675. goto bp_err;
  676. } else if (error)
  677. goto bp_err;
  678. } else {
  679. start_blk = 0;
  680. ASSERT(head_blk <= INT_MAX);
  681. if ((error = xlog_find_verify_log_record(log, start_blk,
  682. &head_blk, 0)) == -1) {
  683. /* We hit the beginning of the log during our search */
  684. start_blk = log_bbnum - num_scan_bblks + head_blk;
  685. new_blk = log_bbnum;
  686. ASSERT(start_blk <= INT_MAX &&
  687. (xfs_daddr_t) log_bbnum-start_blk >= 0);
  688. ASSERT(head_blk <= INT_MAX);
  689. if ((error = xlog_find_verify_log_record(log,
  690. start_blk, &new_blk,
  691. (int)head_blk)) == -1) {
  692. error = XFS_ERROR(EIO);
  693. goto bp_err;
  694. } else if (error)
  695. goto bp_err;
  696. if (new_blk != log_bbnum)
  697. head_blk = new_blk;
  698. } else if (error)
  699. goto bp_err;
  700. }
  701. xlog_put_bp(bp);
  702. if (head_blk == log_bbnum)
  703. *return_head_blk = 0;
  704. else
  705. *return_head_blk = head_blk;
  706. /*
  707. * When returning here, we have a good block number. Bad block
  708. * means that during a previous crash, we didn't have a clean break
  709. * from cycle number N to cycle number N-1. In this case, we need
  710. * to find the first block with cycle number N-1.
  711. */
  712. return 0;
  713. bp_err:
  714. xlog_put_bp(bp);
  715. if (error)
  716. xlog_warn("XFS: failed to find log head");
  717. return error;
  718. }
  719. /*
  720. * Find the sync block number or the tail of the log.
  721. *
  722. * This will be the block number of the last record to have its
  723. * associated buffers synced to disk. Every log record header has
  724. * a sync lsn embedded in it. LSNs hold block numbers, so it is easy
  725. * to get a sync block number. The only concern is to figure out which
  726. * log record header to believe.
  727. *
  728. * The following algorithm uses the log record header with the largest
  729. * lsn. The entire log record does not need to be valid. We only care
  730. * that the header is valid.
  731. *
  732. * We could speed up search by using current head_blk buffer, but it is not
  733. * available.
  734. */
  735. int
  736. xlog_find_tail(
  737. xlog_t *log,
  738. xfs_daddr_t *head_blk,
  739. xfs_daddr_t *tail_blk)
  740. {
  741. xlog_rec_header_t *rhead;
  742. xlog_op_header_t *op_head;
  743. xfs_caddr_t offset = NULL;
  744. xfs_buf_t *bp;
  745. int error, i, found;
  746. xfs_daddr_t umount_data_blk;
  747. xfs_daddr_t after_umount_blk;
  748. xfs_lsn_t tail_lsn;
  749. int hblks;
  750. found = 0;
  751. /*
  752. * Find previous log record
  753. */
  754. if ((error = xlog_find_head(log, head_blk)))
  755. return error;
  756. bp = xlog_get_bp(log, 1);
  757. if (!bp)
  758. return ENOMEM;
  759. if (*head_blk == 0) { /* special case */
  760. error = xlog_bread(log, 0, 1, bp, &offset);
  761. if (error)
  762. goto bread_err;
  763. if (xlog_get_cycle(offset) == 0) {
  764. *tail_blk = 0;
  765. /* leave all other log inited values alone */
  766. goto exit;
  767. }
  768. }
  769. /*
  770. * Search backwards looking for log record header block
  771. */
  772. ASSERT(*head_blk < INT_MAX);
  773. for (i = (int)(*head_blk) - 1; i >= 0; i--) {
  774. error = xlog_bread(log, i, 1, bp, &offset);
  775. if (error)
  776. goto bread_err;
  777. if (XLOG_HEADER_MAGIC_NUM == be32_to_cpu(*(__be32 *)offset)) {
  778. found = 1;
  779. break;
  780. }
  781. }
  782. /*
  783. * If we haven't found the log record header block, start looking
  784. * again from the end of the physical log. XXXmiken: There should be
  785. * a check here to make sure we didn't search more than N blocks in
  786. * the previous code.
  787. */
  788. if (!found) {
  789. for (i = log->l_logBBsize - 1; i >= (int)(*head_blk); i--) {
  790. error = xlog_bread(log, i, 1, bp, &offset);
  791. if (error)
  792. goto bread_err;
  793. if (XLOG_HEADER_MAGIC_NUM ==
  794. be32_to_cpu(*(__be32 *)offset)) {
  795. found = 2;
  796. break;
  797. }
  798. }
  799. }
  800. if (!found) {
  801. xlog_warn("XFS: xlog_find_tail: couldn't find sync record");
  802. ASSERT(0);
  803. return XFS_ERROR(EIO);
  804. }
  805. /* find blk_no of tail of log */
  806. rhead = (xlog_rec_header_t *)offset;
  807. *tail_blk = BLOCK_LSN(be64_to_cpu(rhead->h_tail_lsn));
  808. /*
  809. * Reset log values according to the state of the log when we
  810. * crashed. In the case where head_blk == 0, we bump curr_cycle
  811. * one because the next write starts a new cycle rather than
  812. * continuing the cycle of the last good log record. At this
  813. * point we have guaranteed that all partial log records have been
  814. * accounted for. Therefore, we know that the last good log record
  815. * written was complete and ended exactly on the end boundary
  816. * of the physical log.
  817. */
  818. log->l_prev_block = i;
  819. log->l_curr_block = (int)*head_blk;
  820. log->l_curr_cycle = be32_to_cpu(rhead->h_cycle);
  821. if (found == 2)
  822. log->l_curr_cycle++;
  823. log->l_tail_lsn = be64_to_cpu(rhead->h_tail_lsn);
  824. log->l_last_sync_lsn = be64_to_cpu(rhead->h_lsn);
  825. log->l_grant_reserve_cycle = log->l_curr_cycle;
  826. log->l_grant_reserve_bytes = BBTOB(log->l_curr_block);
  827. log->l_grant_write_cycle = log->l_curr_cycle;
  828. log->l_grant_write_bytes = BBTOB(log->l_curr_block);
  829. /*
  830. * Look for unmount record. If we find it, then we know there
  831. * was a clean unmount. Since 'i' could be the last block in
  832. * the physical log, we convert to a log block before comparing
  833. * to the head_blk.
  834. *
  835. * Save the current tail lsn to use to pass to
  836. * xlog_clear_stale_blocks() below. We won't want to clear the
  837. * unmount record if there is one, so we pass the lsn of the
  838. * unmount record rather than the block after it.
  839. */
  840. if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
  841. int h_size = be32_to_cpu(rhead->h_size);
  842. int h_version = be32_to_cpu(rhead->h_version);
  843. if ((h_version & XLOG_VERSION_2) &&
  844. (h_size > XLOG_HEADER_CYCLE_SIZE)) {
  845. hblks = h_size / XLOG_HEADER_CYCLE_SIZE;
  846. if (h_size % XLOG_HEADER_CYCLE_SIZE)
  847. hblks++;
  848. } else {
  849. hblks = 1;
  850. }
  851. } else {
  852. hblks = 1;
  853. }
  854. after_umount_blk = (i + hblks + (int)
  855. BTOBB(be32_to_cpu(rhead->h_len))) % log->l_logBBsize;
  856. tail_lsn = log->l_tail_lsn;
  857. if (*head_blk == after_umount_blk &&
  858. be32_to_cpu(rhead->h_num_logops) == 1) {
  859. umount_data_blk = (i + hblks) % log->l_logBBsize;
  860. error = xlog_bread(log, umount_data_blk, 1, bp, &offset);
  861. if (error)
  862. goto bread_err;
  863. op_head = (xlog_op_header_t *)offset;
  864. if (op_head->oh_flags & XLOG_UNMOUNT_TRANS) {
  865. /*
  866. * Set tail and last sync so that newly written
  867. * log records will point recovery to after the
  868. * current unmount record.
  869. */
  870. log->l_tail_lsn =
  871. xlog_assign_lsn(log->l_curr_cycle,
  872. after_umount_blk);
  873. log->l_last_sync_lsn =
  874. xlog_assign_lsn(log->l_curr_cycle,
  875. after_umount_blk);
  876. *tail_blk = after_umount_blk;
  877. /*
  878. * Note that the unmount was clean. If the unmount
  879. * was not clean, we need to know this to rebuild the
  880. * superblock counters from the perag headers if we
  881. * have a filesystem using non-persistent counters.
  882. */
  883. log->l_mp->m_flags |= XFS_MOUNT_WAS_CLEAN;
  884. }
  885. }
  886. /*
  887. * Make sure that there are no blocks in front of the head
  888. * with the same cycle number as the head. This can happen
  889. * because we allow multiple outstanding log writes concurrently,
  890. * and the later writes might make it out before earlier ones.
  891. *
  892. * We use the lsn from before modifying it so that we'll never
  893. * overwrite the unmount record after a clean unmount.
  894. *
  895. * Do this only if we are going to recover the filesystem
  896. *
  897. * NOTE: This used to say "if (!readonly)"
  898. * However on Linux, we can & do recover a read-only filesystem.
  899. * We only skip recovery if NORECOVERY is specified on mount,
  900. * in which case we would not be here.
  901. *
  902. * But... if the -device- itself is readonly, just skip this.
  903. * We can't recover this device anyway, so it won't matter.
  904. */
  905. if (!xfs_readonly_buftarg(log->l_mp->m_logdev_targp)) {
  906. error = xlog_clear_stale_blocks(log, tail_lsn);
  907. }
  908. bread_err:
  909. exit:
  910. xlog_put_bp(bp);
  911. if (error)
  912. xlog_warn("XFS: failed to locate log tail");
  913. return error;
  914. }
  915. /*
  916. * Is the log zeroed at all?
  917. *
  918. * The last binary search should be changed to perform an X block read
  919. * once X becomes small enough. You can then search linearly through
  920. * the X blocks. This will cut down on the number of reads we need to do.
  921. *
  922. * If the log is partially zeroed, this routine will pass back the blkno
  923. * of the first block with cycle number 0. It won't have a complete LR
  924. * preceding it.
  925. *
  926. * Return:
  927. * 0 => the log is completely written to
  928. * -1 => use *blk_no as the first block of the log
  929. * >0 => error has occurred
  930. */
  931. STATIC int
  932. xlog_find_zeroed(
  933. xlog_t *log,
  934. xfs_daddr_t *blk_no)
  935. {
  936. xfs_buf_t *bp;
  937. xfs_caddr_t offset;
  938. uint first_cycle, last_cycle;
  939. xfs_daddr_t new_blk, last_blk, start_blk;
  940. xfs_daddr_t num_scan_bblks;
  941. int error, log_bbnum = log->l_logBBsize;
  942. *blk_no = 0;
  943. /* check totally zeroed log */
  944. bp = xlog_get_bp(log, 1);
  945. if (!bp)
  946. return ENOMEM;
  947. error = xlog_bread(log, 0, 1, bp, &offset);
  948. if (error)
  949. goto bp_err;
  950. first_cycle = xlog_get_cycle(offset);
  951. if (first_cycle == 0) { /* completely zeroed log */
  952. *blk_no = 0;
  953. xlog_put_bp(bp);
  954. return -1;
  955. }
  956. /* check partially zeroed log */
  957. error = xlog_bread(log, log_bbnum-1, 1, bp, &offset);
  958. if (error)
  959. goto bp_err;
  960. last_cycle = xlog_get_cycle(offset);
  961. if (last_cycle != 0) { /* log completely written to */
  962. xlog_put_bp(bp);
  963. return 0;
  964. } else if (first_cycle != 1) {
  965. /*
  966. * If the cycle of the last block is zero, the cycle of
  967. * the first block must be 1. If it's not, maybe we're
  968. * not looking at a log... Bail out.
  969. */
  970. xlog_warn("XFS: Log inconsistent or not a log (last==0, first!=1)");
  971. return XFS_ERROR(EINVAL);
  972. }
  973. /* we have a partially zeroed log */
  974. last_blk = log_bbnum-1;
  975. if ((error = xlog_find_cycle_start(log, bp, 0, &last_blk, 0)))
  976. goto bp_err;
  977. /*
  978. * Validate the answer. Because there is no way to guarantee that
  979. * the entire log is made up of log records which are the same size,
  980. * we scan over the defined maximum blocks. At this point, the maximum
  981. * is not chosen to mean anything special. XXXmiken
  982. */
  983. num_scan_bblks = XLOG_TOTAL_REC_SHIFT(log);
  984. ASSERT(num_scan_bblks <= INT_MAX);
  985. if (last_blk < num_scan_bblks)
  986. num_scan_bblks = last_blk;
  987. start_blk = last_blk - num_scan_bblks;
  988. /*
  989. * We search for any instances of cycle number 0 that occur before
  990. * our current estimate of the head. What we're trying to detect is
  991. * 1 ... | 0 | 1 | 0...
  992. * ^ binary search ends here
  993. */
  994. if ((error = xlog_find_verify_cycle(log, start_blk,
  995. (int)num_scan_bblks, 0, &new_blk)))
  996. goto bp_err;
  997. if (new_blk != -1)
  998. last_blk = new_blk;
  999. /*
  1000. * Potentially backup over partial log record write. We don't need
  1001. * to search the end of the log because we know it is zero.
  1002. */
  1003. if ((error = xlog_find_verify_log_record(log, start_blk,
  1004. &last_blk, 0)) == -1) {
  1005. error = XFS_ERROR(EIO);
  1006. goto bp_err;
  1007. } else if (error)
  1008. goto bp_err;
  1009. *blk_no = last_blk;
  1010. bp_err:
  1011. xlog_put_bp(bp);
  1012. if (error)
  1013. return error;
  1014. return -1;
  1015. }
  1016. /*
  1017. * These are simple subroutines used by xlog_clear_stale_blocks() below
  1018. * to initialize a buffer full of empty log record headers and write
  1019. * them into the log.
  1020. */
  1021. STATIC void
  1022. xlog_add_record(
  1023. xlog_t *log,
  1024. xfs_caddr_t buf,
  1025. int cycle,
  1026. int block,
  1027. int tail_cycle,
  1028. int tail_block)
  1029. {
  1030. xlog_rec_header_t *recp = (xlog_rec_header_t *)buf;
  1031. memset(buf, 0, BBSIZE);
  1032. recp->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
  1033. recp->h_cycle = cpu_to_be32(cycle);
  1034. recp->h_version = cpu_to_be32(
  1035. xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1);
  1036. recp->h_lsn = cpu_to_be64(xlog_assign_lsn(cycle, block));
  1037. recp->h_tail_lsn = cpu_to_be64(xlog_assign_lsn(tail_cycle, tail_block));
  1038. recp->h_fmt = cpu_to_be32(XLOG_FMT);
  1039. memcpy(&recp->h_fs_uuid, &log->l_mp->m_sb.sb_uuid, sizeof(uuid_t));
  1040. }
  1041. STATIC int
  1042. xlog_write_log_records(
  1043. xlog_t *log,
  1044. int cycle,
  1045. int start_block,
  1046. int blocks,
  1047. int tail_cycle,
  1048. int tail_block)
  1049. {
  1050. xfs_caddr_t offset;
  1051. xfs_buf_t *bp;
  1052. int balign, ealign;
  1053. int sectbb = XLOG_SECTOR_ROUNDUP_BBCOUNT(log, 1);
  1054. int end_block = start_block + blocks;
  1055. int bufblks;
  1056. int error = 0;
  1057. int i, j = 0;
  1058. bufblks = 1 << ffs(blocks);
  1059. while (!(bp = xlog_get_bp(log, bufblks))) {
  1060. bufblks >>= 1;
  1061. if (bufblks <= log->l_sectbb_log)
  1062. return ENOMEM;
  1063. }
  1064. /* We may need to do a read at the start to fill in part of
  1065. * the buffer in the starting sector not covered by the first
  1066. * write below.
  1067. */
  1068. balign = XLOG_SECTOR_ROUNDDOWN_BLKNO(log, start_block);
  1069. if (balign != start_block) {
  1070. error = xlog_bread_noalign(log, start_block, 1, bp);
  1071. if (error)
  1072. goto out_put_bp;
  1073. j = start_block - balign;
  1074. }
  1075. for (i = start_block; i < end_block; i += bufblks) {
  1076. int bcount, endcount;
  1077. bcount = min(bufblks, end_block - start_block);
  1078. endcount = bcount - j;
  1079. /* We may need to do a read at the end to fill in part of
  1080. * the buffer in the final sector not covered by the write.
  1081. * If this is the same sector as the above read, skip it.
  1082. */
  1083. ealign = XLOG_SECTOR_ROUNDDOWN_BLKNO(log, end_block);
  1084. if (j == 0 && (start_block + endcount > ealign)) {
  1085. offset = XFS_BUF_PTR(bp);
  1086. balign = BBTOB(ealign - start_block);
  1087. error = XFS_BUF_SET_PTR(bp, offset + balign,
  1088. BBTOB(sectbb));
  1089. if (error)
  1090. break;
  1091. error = xlog_bread_noalign(log, ealign, sectbb, bp);
  1092. if (error)
  1093. break;
  1094. error = XFS_BUF_SET_PTR(bp, offset, bufblks);
  1095. if (error)
  1096. break;
  1097. }
  1098. offset = xlog_align(log, start_block, endcount, bp);
  1099. for (; j < endcount; j++) {
  1100. xlog_add_record(log, offset, cycle, i+j,
  1101. tail_cycle, tail_block);
  1102. offset += BBSIZE;
  1103. }
  1104. error = xlog_bwrite(log, start_block, endcount, bp);
  1105. if (error)
  1106. break;
  1107. start_block += endcount;
  1108. j = 0;
  1109. }
  1110. out_put_bp:
  1111. xlog_put_bp(bp);
  1112. return error;
  1113. }
  1114. /*
  1115. * This routine is called to blow away any incomplete log writes out
  1116. * in front of the log head. We do this so that we won't become confused
  1117. * if we come up, write only a little bit more, and then crash again.
  1118. * If we leave the partial log records out there, this situation could
  1119. * cause us to think those partial writes are valid blocks since they
  1120. * have the current cycle number. We get rid of them by overwriting them
  1121. * with empty log records with the old cycle number rather than the
  1122. * current one.
  1123. *
  1124. * The tail lsn is passed in rather than taken from
  1125. * the log so that we will not write over the unmount record after a
  1126. * clean unmount in a 512 block log. Doing so would leave the log without
  1127. * any valid log records in it until a new one was written. If we crashed
  1128. * during that time we would not be able to recover.
  1129. */
  1130. STATIC int
  1131. xlog_clear_stale_blocks(
  1132. xlog_t *log,
  1133. xfs_lsn_t tail_lsn)
  1134. {
  1135. int tail_cycle, head_cycle;
  1136. int tail_block, head_block;
  1137. int tail_distance, max_distance;
  1138. int distance;
  1139. int error;
  1140. tail_cycle = CYCLE_LSN(tail_lsn);
  1141. tail_block = BLOCK_LSN(tail_lsn);
  1142. head_cycle = log->l_curr_cycle;
  1143. head_block = log->l_curr_block;
  1144. /*
  1145. * Figure out the distance between the new head of the log
  1146. * and the tail. We want to write over any blocks beyond the
  1147. * head that we may have written just before the crash, but
  1148. * we don't want to overwrite the tail of the log.
  1149. */
  1150. if (head_cycle == tail_cycle) {
  1151. /*
  1152. * The tail is behind the head in the physical log,
  1153. * so the distance from the head to the tail is the
  1154. * distance from the head to the end of the log plus
  1155. * the distance from the beginning of the log to the
  1156. * tail.
  1157. */
  1158. if (unlikely(head_block < tail_block || head_block >= log->l_logBBsize)) {
  1159. XFS_ERROR_REPORT("xlog_clear_stale_blocks(1)",
  1160. XFS_ERRLEVEL_LOW, log->l_mp);
  1161. return XFS_ERROR(EFSCORRUPTED);
  1162. }
  1163. tail_distance = tail_block + (log->l_logBBsize - head_block);
  1164. } else {
  1165. /*
  1166. * The head is behind the tail in the physical log,
  1167. * so the distance from the head to the tail is just
  1168. * the tail block minus the head block.
  1169. */
  1170. if (unlikely(head_block >= tail_block || head_cycle != (tail_cycle + 1))){
  1171. XFS_ERROR_REPORT("xlog_clear_stale_blocks(2)",
  1172. XFS_ERRLEVEL_LOW, log->l_mp);
  1173. return XFS_ERROR(EFSCORRUPTED);
  1174. }
  1175. tail_distance = tail_block - head_block;
  1176. }
  1177. /*
  1178. * If the head is right up against the tail, we can't clear
  1179. * anything.
  1180. */
  1181. if (tail_distance <= 0) {
  1182. ASSERT(tail_distance == 0);
  1183. return 0;
  1184. }
  1185. max_distance = XLOG_TOTAL_REC_SHIFT(log);
  1186. /*
  1187. * Take the smaller of the maximum amount of outstanding I/O
  1188. * we could have and the distance to the tail to clear out.
  1189. * We take the smaller so that we don't overwrite the tail and
  1190. * we don't waste all day writing from the head to the tail
  1191. * for no reason.
  1192. */
  1193. max_distance = MIN(max_distance, tail_distance);
  1194. if ((head_block + max_distance) <= log->l_logBBsize) {
  1195. /*
  1196. * We can stomp all the blocks we need to without
  1197. * wrapping around the end of the log. Just do it
  1198. * in a single write. Use the cycle number of the
  1199. * current cycle minus one so that the log will look like:
  1200. * n ... | n - 1 ...
  1201. */
  1202. error = xlog_write_log_records(log, (head_cycle - 1),
  1203. head_block, max_distance, tail_cycle,
  1204. tail_block);
  1205. if (error)
  1206. return error;
  1207. } else {
  1208. /*
  1209. * We need to wrap around the end of the physical log in
  1210. * order to clear all the blocks. Do it in two separate
  1211. * I/Os. The first write should be from the head to the
  1212. * end of the physical log, and it should use the current
  1213. * cycle number minus one just like above.
  1214. */
  1215. distance = log->l_logBBsize - head_block;
  1216. error = xlog_write_log_records(log, (head_cycle - 1),
  1217. head_block, distance, tail_cycle,
  1218. tail_block);
  1219. if (error)
  1220. return error;
  1221. /*
  1222. * Now write the blocks at the start of the physical log.
  1223. * This writes the remainder of the blocks we want to clear.
  1224. * It uses the current cycle number since we're now on the
  1225. * same cycle as the head so that we get:
  1226. * n ... n ... | n - 1 ...
  1227. * ^^^^^ blocks we're writing
  1228. */
  1229. distance = max_distance - (log->l_logBBsize - head_block);
  1230. error = xlog_write_log_records(log, head_cycle, 0, distance,
  1231. tail_cycle, tail_block);
  1232. if (error)
  1233. return error;
  1234. }
  1235. return 0;
  1236. }
  1237. /******************************************************************************
  1238. *
  1239. * Log recover routines
  1240. *
  1241. ******************************************************************************
  1242. */
  1243. STATIC xlog_recover_t *
  1244. xlog_recover_find_tid(
  1245. xlog_recover_t *q,
  1246. xlog_tid_t tid)
  1247. {
  1248. xlog_recover_t *p = q;
  1249. while (p != NULL) {
  1250. if (p->r_log_tid == tid)
  1251. break;
  1252. p = p->r_next;
  1253. }
  1254. return p;
  1255. }
  1256. STATIC void
  1257. xlog_recover_put_hashq(
  1258. xlog_recover_t **q,
  1259. xlog_recover_t *trans)
  1260. {
  1261. trans->r_next = *q;
  1262. *q = trans;
  1263. }
  1264. STATIC void
  1265. xlog_recover_add_item(
  1266. xlog_recover_item_t **itemq)
  1267. {
  1268. xlog_recover_item_t *item;
  1269. item = kmem_zalloc(sizeof(xlog_recover_item_t), KM_SLEEP);
  1270. xlog_recover_insert_item_backq(itemq, item);
  1271. }
  1272. STATIC int
  1273. xlog_recover_add_to_cont_trans(
  1274. xlog_recover_t *trans,
  1275. xfs_caddr_t dp,
  1276. int len)
  1277. {
  1278. xlog_recover_item_t *item;
  1279. xfs_caddr_t ptr, old_ptr;
  1280. int old_len;
  1281. item = trans->r_itemq;
  1282. if (item == NULL) {
  1283. /* finish copying rest of trans header */
  1284. xlog_recover_add_item(&trans->r_itemq);
  1285. ptr = (xfs_caddr_t) &trans->r_theader +
  1286. sizeof(xfs_trans_header_t) - len;
  1287. memcpy(ptr, dp, len); /* d, s, l */
  1288. return 0;
  1289. }
  1290. item = item->ri_prev;
  1291. old_ptr = item->ri_buf[item->ri_cnt-1].i_addr;
  1292. old_len = item->ri_buf[item->ri_cnt-1].i_len;
  1293. ptr = kmem_realloc(old_ptr, len+old_len, old_len, 0u);
  1294. memcpy(&ptr[old_len], dp, len); /* d, s, l */
  1295. item->ri_buf[item->ri_cnt-1].i_len += len;
  1296. item->ri_buf[item->ri_cnt-1].i_addr = ptr;
  1297. return 0;
  1298. }
  1299. /*
  1300. * The next region to add is the start of a new region. It could be
  1301. * a whole region or it could be the first part of a new region. Because
  1302. * of this, the assumption here is that the type and size fields of all
  1303. * format structures fit into the first 32 bits of the structure.
  1304. *
  1305. * This works because all regions must be 32 bit aligned. Therefore, we
  1306. * either have both fields or we have neither field. In the case we have
  1307. * neither field, the data part of the region is zero length. We only have
  1308. * a log_op_header and can throw away the header since a new one will appear
  1309. * later. If we have at least 4 bytes, then we can determine how many regions
  1310. * will appear in the current log item.
  1311. */
  1312. STATIC int
  1313. xlog_recover_add_to_trans(
  1314. xlog_recover_t *trans,
  1315. xfs_caddr_t dp,
  1316. int len)
  1317. {
  1318. xfs_inode_log_format_t *in_f; /* any will do */
  1319. xlog_recover_item_t *item;
  1320. xfs_caddr_t ptr;
  1321. if (!len)
  1322. return 0;
  1323. item = trans->r_itemq;
  1324. if (item == NULL) {
  1325. /* we need to catch log corruptions here */
  1326. if (*(uint *)dp != XFS_TRANS_HEADER_MAGIC) {
  1327. xlog_warn("XFS: xlog_recover_add_to_trans: "
  1328. "bad header magic number");
  1329. ASSERT(0);
  1330. return XFS_ERROR(EIO);
  1331. }
  1332. if (len == sizeof(xfs_trans_header_t))
  1333. xlog_recover_add_item(&trans->r_itemq);
  1334. memcpy(&trans->r_theader, dp, len); /* d, s, l */
  1335. return 0;
  1336. }
  1337. ptr = kmem_alloc(len, KM_SLEEP);
  1338. memcpy(ptr, dp, len);
  1339. in_f = (xfs_inode_log_format_t *)ptr;
  1340. if (item->ri_prev->ri_total != 0 &&
  1341. item->ri_prev->ri_total == item->ri_prev->ri_cnt) {
  1342. xlog_recover_add_item(&trans->r_itemq);
  1343. }
  1344. item = trans->r_itemq;
  1345. item = item->ri_prev;
  1346. if (item->ri_total == 0) { /* first region to be added */
  1347. if (in_f->ilf_size == 0 ||
  1348. in_f->ilf_size > XLOG_MAX_REGIONS_IN_ITEM) {
  1349. xlog_warn(
  1350. "XFS: bad number of regions (%d) in inode log format",
  1351. in_f->ilf_size);
  1352. ASSERT(0);
  1353. return XFS_ERROR(EIO);
  1354. }
  1355. item->ri_total = in_f->ilf_size;
  1356. item->ri_buf =
  1357. kmem_zalloc(item->ri_total * sizeof(xfs_log_iovec_t),
  1358. KM_SLEEP);
  1359. }
  1360. ASSERT(item->ri_total > item->ri_cnt);
  1361. /* Description region is ri_buf[0] */
  1362. item->ri_buf[item->ri_cnt].i_addr = ptr;
  1363. item->ri_buf[item->ri_cnt].i_len = len;
  1364. item->ri_cnt++;
  1365. return 0;
  1366. }
  1367. STATIC void
  1368. xlog_recover_new_tid(
  1369. xlog_recover_t **q,
  1370. xlog_tid_t tid,
  1371. xfs_lsn_t lsn)
  1372. {
  1373. xlog_recover_t *trans;
  1374. trans = kmem_zalloc(sizeof(xlog_recover_t), KM_SLEEP);
  1375. trans->r_log_tid = tid;
  1376. trans->r_lsn = lsn;
  1377. xlog_recover_put_hashq(q, trans);
  1378. }
  1379. STATIC int
  1380. xlog_recover_unlink_tid(
  1381. xlog_recover_t **q,
  1382. xlog_recover_t *trans)
  1383. {
  1384. xlog_recover_t *tp;
  1385. int found = 0;
  1386. ASSERT(trans != NULL);
  1387. if (trans == *q) {
  1388. *q = (*q)->r_next;
  1389. } else {
  1390. tp = *q;
  1391. while (tp) {
  1392. if (tp->r_next == trans) {
  1393. found = 1;
  1394. break;
  1395. }
  1396. tp = tp->r_next;
  1397. }
  1398. if (!found) {
  1399. xlog_warn(
  1400. "XFS: xlog_recover_unlink_tid: trans not found");
  1401. ASSERT(0);
  1402. return XFS_ERROR(EIO);
  1403. }
  1404. tp->r_next = tp->r_next->r_next;
  1405. }
  1406. return 0;
  1407. }
  1408. STATIC void
  1409. xlog_recover_insert_item_backq(
  1410. xlog_recover_item_t **q,
  1411. xlog_recover_item_t *item)
  1412. {
  1413. if (*q == NULL) {
  1414. item->ri_prev = item->ri_next = item;
  1415. *q = item;
  1416. } else {
  1417. item->ri_next = *q;
  1418. item->ri_prev = (*q)->ri_prev;
  1419. (*q)->ri_prev = item;
  1420. item->ri_prev->ri_next = item;
  1421. }
  1422. }
  1423. STATIC void
  1424. xlog_recover_insert_item_frontq(
  1425. xlog_recover_item_t **q,
  1426. xlog_recover_item_t *item)
  1427. {
  1428. xlog_recover_insert_item_backq(q, item);
  1429. *q = item;
  1430. }
  1431. STATIC int
  1432. xlog_recover_reorder_trans(
  1433. xlog_recover_t *trans)
  1434. {
  1435. xlog_recover_item_t *first_item, *itemq, *itemq_next;
  1436. xfs_buf_log_format_t *buf_f;
  1437. ushort flags = 0;
  1438. first_item = itemq = trans->r_itemq;
  1439. trans->r_itemq = NULL;
  1440. do {
  1441. itemq_next = itemq->ri_next;
  1442. buf_f = (xfs_buf_log_format_t *)itemq->ri_buf[0].i_addr;
  1443. switch (ITEM_TYPE(itemq)) {
  1444. case XFS_LI_BUF:
  1445. flags = buf_f->blf_flags;
  1446. if (!(flags & XFS_BLI_CANCEL)) {
  1447. xlog_recover_insert_item_frontq(&trans->r_itemq,
  1448. itemq);
  1449. break;
  1450. }
  1451. case XFS_LI_INODE:
  1452. case XFS_LI_DQUOT:
  1453. case XFS_LI_QUOTAOFF:
  1454. case XFS_LI_EFD:
  1455. case XFS_LI_EFI:
  1456. xlog_recover_insert_item_backq(&trans->r_itemq, itemq);
  1457. break;
  1458. default:
  1459. xlog_warn(
  1460. "XFS: xlog_recover_reorder_trans: unrecognized type of log operation");
  1461. ASSERT(0);
  1462. return XFS_ERROR(EIO);
  1463. }
  1464. itemq = itemq_next;
  1465. } while (first_item != itemq);
  1466. return 0;
  1467. }
  1468. /*
  1469. * Build up the table of buf cancel records so that we don't replay
  1470. * cancelled data in the second pass. For buffer records that are
  1471. * not cancel records, there is nothing to do here so we just return.
  1472. *
  1473. * If we get a cancel record which is already in the table, this indicates
  1474. * that the buffer was cancelled multiple times. In order to ensure
  1475. * that during pass 2 we keep the record in the table until we reach its
  1476. * last occurrence in the log, we keep a reference count in the cancel
  1477. * record in the table to tell us how many times we expect to see this
  1478. * record during the second pass.
  1479. */
  1480. STATIC void
  1481. xlog_recover_do_buffer_pass1(
  1482. xlog_t *log,
  1483. xfs_buf_log_format_t *buf_f)
  1484. {
  1485. xfs_buf_cancel_t *bcp;
  1486. xfs_buf_cancel_t *nextp;
  1487. xfs_buf_cancel_t *prevp;
  1488. xfs_buf_cancel_t **bucket;
  1489. xfs_daddr_t blkno = 0;
  1490. uint len = 0;
  1491. ushort flags = 0;
  1492. switch (buf_f->blf_type) {
  1493. case XFS_LI_BUF:
  1494. blkno = buf_f->blf_blkno;
  1495. len = buf_f->blf_len;
  1496. flags = buf_f->blf_flags;
  1497. break;
  1498. }
  1499. /*
  1500. * If this isn't a cancel buffer item, then just return.
  1501. */
  1502. if (!(flags & XFS_BLI_CANCEL))
  1503. return;
  1504. /*
  1505. * Insert an xfs_buf_cancel record into the hash table of
  1506. * them. If there is already an identical record, bump
  1507. * its reference count.
  1508. */
  1509. bucket = &log->l_buf_cancel_table[(__uint64_t)blkno %
  1510. XLOG_BC_TABLE_SIZE];
  1511. /*
  1512. * If the hash bucket is empty then just insert a new record into
  1513. * the bucket.
  1514. */
  1515. if (*bucket == NULL) {
  1516. bcp = (xfs_buf_cancel_t *)kmem_alloc(sizeof(xfs_buf_cancel_t),
  1517. KM_SLEEP);
  1518. bcp->bc_blkno = blkno;
  1519. bcp->bc_len = len;
  1520. bcp->bc_refcount = 1;
  1521. bcp->bc_next = NULL;
  1522. *bucket = bcp;
  1523. return;
  1524. }
  1525. /*
  1526. * The hash bucket is not empty, so search for duplicates of our
  1527. * record. If we find one them just bump its refcount. If not
  1528. * then add us at the end of the list.
  1529. */
  1530. prevp = NULL;
  1531. nextp = *bucket;
  1532. while (nextp != NULL) {
  1533. if (nextp->bc_blkno == blkno && nextp->bc_len == len) {
  1534. nextp->bc_refcount++;
  1535. return;
  1536. }
  1537. prevp = nextp;
  1538. nextp = nextp->bc_next;
  1539. }
  1540. ASSERT(prevp != NULL);
  1541. bcp = (xfs_buf_cancel_t *)kmem_alloc(sizeof(xfs_buf_cancel_t),
  1542. KM_SLEEP);
  1543. bcp->bc_blkno = blkno;
  1544. bcp->bc_len = len;
  1545. bcp->bc_refcount = 1;
  1546. bcp->bc_next = NULL;
  1547. prevp->bc_next = bcp;
  1548. }
  1549. /*
  1550. * Check to see whether the buffer being recovered has a corresponding
  1551. * entry in the buffer cancel record table. If it does then return 1
  1552. * so that it will be cancelled, otherwise return 0. If the buffer is
  1553. * actually a buffer cancel item (XFS_BLI_CANCEL is set), then decrement
  1554. * the refcount on the entry in the table and remove it from the table
  1555. * if this is the last reference.
  1556. *
  1557. * We remove the cancel record from the table when we encounter its
  1558. * last occurrence in the log so that if the same buffer is re-used
  1559. * again after its last cancellation we actually replay the changes
  1560. * made at that point.
  1561. */
  1562. STATIC int
  1563. xlog_check_buffer_cancelled(
  1564. xlog_t *log,
  1565. xfs_daddr_t blkno,
  1566. uint len,
  1567. ushort flags)
  1568. {
  1569. xfs_buf_cancel_t *bcp;
  1570. xfs_buf_cancel_t *prevp;
  1571. xfs_buf_cancel_t **bucket;
  1572. if (log->l_buf_cancel_table == NULL) {
  1573. /*
  1574. * There is nothing in the table built in pass one,
  1575. * so this buffer must not be cancelled.
  1576. */
  1577. ASSERT(!(flags & XFS_BLI_CANCEL));
  1578. return 0;
  1579. }
  1580. bucket = &log->l_buf_cancel_table[(__uint64_t)blkno %
  1581. XLOG_BC_TABLE_SIZE];
  1582. bcp = *bucket;
  1583. if (bcp == NULL) {
  1584. /*
  1585. * There is no corresponding entry in the table built
  1586. * in pass one, so this buffer has not been cancelled.
  1587. */
  1588. ASSERT(!(flags & XFS_BLI_CANCEL));
  1589. return 0;
  1590. }
  1591. /*
  1592. * Search for an entry in the buffer cancel table that
  1593. * matches our buffer.
  1594. */
  1595. prevp = NULL;
  1596. while (bcp != NULL) {
  1597. if (bcp->bc_blkno == blkno && bcp->bc_len == len) {
  1598. /*
  1599. * We've go a match, so return 1 so that the
  1600. * recovery of this buffer is cancelled.
  1601. * If this buffer is actually a buffer cancel
  1602. * log item, then decrement the refcount on the
  1603. * one in the table and remove it if this is the
  1604. * last reference.
  1605. */
  1606. if (flags & XFS_BLI_CANCEL) {
  1607. bcp->bc_refcount--;
  1608. if (bcp->bc_refcount == 0) {
  1609. if (prevp == NULL) {
  1610. *bucket = bcp->bc_next;
  1611. } else {
  1612. prevp->bc_next = bcp->bc_next;
  1613. }
  1614. kmem_free(bcp);
  1615. }
  1616. }
  1617. return 1;
  1618. }
  1619. prevp = bcp;
  1620. bcp = bcp->bc_next;
  1621. }
  1622. /*
  1623. * We didn't find a corresponding entry in the table, so
  1624. * return 0 so that the buffer is NOT cancelled.
  1625. */
  1626. ASSERT(!(flags & XFS_BLI_CANCEL));
  1627. return 0;
  1628. }
  1629. STATIC int
  1630. xlog_recover_do_buffer_pass2(
  1631. xlog_t *log,
  1632. xfs_buf_log_format_t *buf_f)
  1633. {
  1634. xfs_daddr_t blkno = 0;
  1635. ushort flags = 0;
  1636. uint len = 0;
  1637. switch (buf_f->blf_type) {
  1638. case XFS_LI_BUF:
  1639. blkno = buf_f->blf_blkno;
  1640. flags = buf_f->blf_flags;
  1641. len = buf_f->blf_len;
  1642. break;
  1643. }
  1644. return xlog_check_buffer_cancelled(log, blkno, len, flags);
  1645. }
  1646. /*
  1647. * Perform recovery for a buffer full of inodes. In these buffers,
  1648. * the only data which should be recovered is that which corresponds
  1649. * to the di_next_unlinked pointers in the on disk inode structures.
  1650. * The rest of the data for the inodes is always logged through the
  1651. * inodes themselves rather than the inode buffer and is recovered
  1652. * in xlog_recover_do_inode_trans().
  1653. *
  1654. * The only time when buffers full of inodes are fully recovered is
  1655. * when the buffer is full of newly allocated inodes. In this case
  1656. * the buffer will not be marked as an inode buffer and so will be
  1657. * sent to xlog_recover_do_reg_buffer() below during recovery.
  1658. */
  1659. STATIC int
  1660. xlog_recover_do_inode_buffer(
  1661. xfs_mount_t *mp,
  1662. xlog_recover_item_t *item,
  1663. xfs_buf_t *bp,
  1664. xfs_buf_log_format_t *buf_f)
  1665. {
  1666. int i;
  1667. int item_index;
  1668. int bit;
  1669. int nbits;
  1670. int reg_buf_offset;
  1671. int reg_buf_bytes;
  1672. int next_unlinked_offset;
  1673. int inodes_per_buf;
  1674. xfs_agino_t *logged_nextp;
  1675. xfs_agino_t *buffer_nextp;
  1676. unsigned int *data_map = NULL;
  1677. unsigned int map_size = 0;
  1678. switch (buf_f->blf_type) {
  1679. case XFS_LI_BUF:
  1680. data_map = buf_f->blf_data_map;
  1681. map_size = buf_f->blf_map_size;
  1682. break;
  1683. }
  1684. /*
  1685. * Set the variables corresponding to the current region to
  1686. * 0 so that we'll initialize them on the first pass through
  1687. * the loop.
  1688. */
  1689. reg_buf_offset = 0;
  1690. reg_buf_bytes = 0;
  1691. bit = 0;
  1692. nbits = 0;
  1693. item_index = 0;
  1694. inodes_per_buf = XFS_BUF_COUNT(bp) >> mp->m_sb.sb_inodelog;
  1695. for (i = 0; i < inodes_per_buf; i++) {
  1696. next_unlinked_offset = (i * mp->m_sb.sb_inodesize) +
  1697. offsetof(xfs_dinode_t, di_next_unlinked);
  1698. while (next_unlinked_offset >=
  1699. (reg_buf_offset + reg_buf_bytes)) {
  1700. /*
  1701. * The next di_next_unlinked field is beyond
  1702. * the current logged region. Find the next
  1703. * logged region that contains or is beyond
  1704. * the current di_next_unlinked field.
  1705. */
  1706. bit += nbits;
  1707. bit = xfs_next_bit(data_map, map_size, bit);
  1708. /*
  1709. * If there are no more logged regions in the
  1710. * buffer, then we're done.
  1711. */
  1712. if (bit == -1) {
  1713. return 0;
  1714. }
  1715. nbits = xfs_contig_bits(data_map, map_size,
  1716. bit);
  1717. ASSERT(nbits > 0);
  1718. reg_buf_offset = bit << XFS_BLI_SHIFT;
  1719. reg_buf_bytes = nbits << XFS_BLI_SHIFT;
  1720. item_index++;
  1721. }
  1722. /*
  1723. * If the current logged region starts after the current
  1724. * di_next_unlinked field, then move on to the next
  1725. * di_next_unlinked field.
  1726. */
  1727. if (next_unlinked_offset < reg_buf_offset) {
  1728. continue;
  1729. }
  1730. ASSERT(item->ri_buf[item_index].i_addr != NULL);
  1731. ASSERT((item->ri_buf[item_index].i_len % XFS_BLI_CHUNK) == 0);
  1732. ASSERT((reg_buf_offset + reg_buf_bytes) <= XFS_BUF_COUNT(bp));
  1733. /*
  1734. * The current logged region contains a copy of the
  1735. * current di_next_unlinked field. Extract its value
  1736. * and copy it to the buffer copy.
  1737. */
  1738. logged_nextp = (xfs_agino_t *)
  1739. ((char *)(item->ri_buf[item_index].i_addr) +
  1740. (next_unlinked_offset - reg_buf_offset));
  1741. if (unlikely(*logged_nextp == 0)) {
  1742. xfs_fs_cmn_err(CE_ALERT, mp,
  1743. "bad inode buffer log record (ptr = 0x%p, bp = 0x%p). XFS trying to replay bad (0) inode di_next_unlinked field",
  1744. item, bp);
  1745. XFS_ERROR_REPORT("xlog_recover_do_inode_buf",
  1746. XFS_ERRLEVEL_LOW, mp);
  1747. return XFS_ERROR(EFSCORRUPTED);
  1748. }
  1749. buffer_nextp = (xfs_agino_t *)xfs_buf_offset(bp,
  1750. next_unlinked_offset);
  1751. *buffer_nextp = *logged_nextp;
  1752. }
  1753. return 0;
  1754. }
  1755. /*
  1756. * Perform a 'normal' buffer recovery. Each logged region of the
  1757. * buffer should be copied over the corresponding region in the
  1758. * given buffer. The bitmap in the buf log format structure indicates
  1759. * where to place the logged data.
  1760. */
  1761. /*ARGSUSED*/
  1762. STATIC void
  1763. xlog_recover_do_reg_buffer(
  1764. xlog_recover_item_t *item,
  1765. xfs_buf_t *bp,
  1766. xfs_buf_log_format_t *buf_f)
  1767. {
  1768. int i;
  1769. int bit;
  1770. int nbits;
  1771. unsigned int *data_map = NULL;
  1772. unsigned int map_size = 0;
  1773. int error;
  1774. switch (buf_f->blf_type) {
  1775. case XFS_LI_BUF:
  1776. data_map = buf_f->blf_data_map;
  1777. map_size = buf_f->blf_map_size;
  1778. break;
  1779. }
  1780. bit = 0;
  1781. i = 1; /* 0 is the buf format structure */
  1782. while (1) {
  1783. bit = xfs_next_bit(data_map, map_size, bit);
  1784. if (bit == -1)
  1785. break;
  1786. nbits = xfs_contig_bits(data_map, map_size, bit);
  1787. ASSERT(nbits > 0);
  1788. ASSERT(item->ri_buf[i].i_addr != NULL);
  1789. ASSERT(item->ri_buf[i].i_len % XFS_BLI_CHUNK == 0);
  1790. ASSERT(XFS_BUF_COUNT(bp) >=
  1791. ((uint)bit << XFS_BLI_SHIFT)+(nbits<<XFS_BLI_SHIFT));
  1792. /*
  1793. * Do a sanity check if this is a dquot buffer. Just checking
  1794. * the first dquot in the buffer should do. XXXThis is
  1795. * probably a good thing to do for other buf types also.
  1796. */
  1797. error = 0;
  1798. if (buf_f->blf_flags &
  1799. (XFS_BLI_UDQUOT_BUF|XFS_BLI_PDQUOT_BUF|XFS_BLI_GDQUOT_BUF)) {
  1800. if (item->ri_buf[i].i_addr == NULL) {
  1801. cmn_err(CE_ALERT,
  1802. "XFS: NULL dquot in %s.", __func__);
  1803. goto next;
  1804. }
  1805. if (item->ri_buf[i].i_len < sizeof(xfs_disk_dquot_t)) {
  1806. cmn_err(CE_ALERT,
  1807. "XFS: dquot too small (%d) in %s.",
  1808. item->ri_buf[i].i_len, __func__);
  1809. goto next;
  1810. }
  1811. error = xfs_qm_dqcheck((xfs_disk_dquot_t *)
  1812. item->ri_buf[i].i_addr,
  1813. -1, 0, XFS_QMOPT_DOWARN,
  1814. "dquot_buf_recover");
  1815. if (error)
  1816. goto next;
  1817. }
  1818. memcpy(xfs_buf_offset(bp,
  1819. (uint)bit << XFS_BLI_SHIFT), /* dest */
  1820. item->ri_buf[i].i_addr, /* source */
  1821. nbits<<XFS_BLI_SHIFT); /* length */
  1822. next:
  1823. i++;
  1824. bit += nbits;
  1825. }
  1826. /* Shouldn't be any more regions */
  1827. ASSERT(i == item->ri_total);
  1828. }
  1829. /*
  1830. * Do some primitive error checking on ondisk dquot data structures.
  1831. */
  1832. int
  1833. xfs_qm_dqcheck(
  1834. xfs_disk_dquot_t *ddq,
  1835. xfs_dqid_t id,
  1836. uint type, /* used only when IO_dorepair is true */
  1837. uint flags,
  1838. char *str)
  1839. {
  1840. xfs_dqblk_t *d = (xfs_dqblk_t *)ddq;
  1841. int errs = 0;
  1842. /*
  1843. * We can encounter an uninitialized dquot buffer for 2 reasons:
  1844. * 1. If we crash while deleting the quotainode(s), and those blks got
  1845. * used for user data. This is because we take the path of regular
  1846. * file deletion; however, the size field of quotainodes is never
  1847. * updated, so all the tricks that we play in itruncate_finish
  1848. * don't quite matter.
  1849. *
  1850. * 2. We don't play the quota buffers when there's a quotaoff logitem.
  1851. * But the allocation will be replayed so we'll end up with an
  1852. * uninitialized quota block.
  1853. *
  1854. * This is all fine; things are still consistent, and we haven't lost
  1855. * any quota information. Just don't complain about bad dquot blks.
  1856. */
  1857. if (be16_to_cpu(ddq->d_magic) != XFS_DQUOT_MAGIC) {
  1858. if (flags & XFS_QMOPT_DOWARN)
  1859. cmn_err(CE_ALERT,
  1860. "%s : XFS dquot ID 0x%x, magic 0x%x != 0x%x",
  1861. str, id, be16_to_cpu(ddq->d_magic), XFS_DQUOT_MAGIC);
  1862. errs++;
  1863. }
  1864. if (ddq->d_version != XFS_DQUOT_VERSION) {
  1865. if (flags & XFS_QMOPT_DOWARN)
  1866. cmn_err(CE_ALERT,
  1867. "%s : XFS dquot ID 0x%x, version 0x%x != 0x%x",
  1868. str, id, ddq->d_version, XFS_DQUOT_VERSION);
  1869. errs++;
  1870. }
  1871. if (ddq->d_flags != XFS_DQ_USER &&
  1872. ddq->d_flags != XFS_DQ_PROJ &&
  1873. ddq->d_flags != XFS_DQ_GROUP) {
  1874. if (flags & XFS_QMOPT_DOWARN)
  1875. cmn_err(CE_ALERT,
  1876. "%s : XFS dquot ID 0x%x, unknown flags 0x%x",
  1877. str, id, ddq->d_flags);
  1878. errs++;
  1879. }
  1880. if (id != -1 && id != be32_to_cpu(ddq->d_id)) {
  1881. if (flags & XFS_QMOPT_DOWARN)
  1882. cmn_err(CE_ALERT,
  1883. "%s : ondisk-dquot 0x%p, ID mismatch: "
  1884. "0x%x expected, found id 0x%x",
  1885. str, ddq, id, be32_to_cpu(ddq->d_id));
  1886. errs++;
  1887. }
  1888. if (!errs && ddq->d_id) {
  1889. if (ddq->d_blk_softlimit &&
  1890. be64_to_cpu(ddq->d_bcount) >=
  1891. be64_to_cpu(ddq->d_blk_softlimit)) {
  1892. if (!ddq->d_btimer) {
  1893. if (flags & XFS_QMOPT_DOWARN)
  1894. cmn_err(CE_ALERT,
  1895. "%s : Dquot ID 0x%x (0x%p) "
  1896. "BLK TIMER NOT STARTED",
  1897. str, (int)be32_to_cpu(ddq->d_id), ddq);
  1898. errs++;
  1899. }
  1900. }
  1901. if (ddq->d_ino_softlimit &&
  1902. be64_to_cpu(ddq->d_icount) >=
  1903. be64_to_cpu(ddq->d_ino_softlimit)) {
  1904. if (!ddq->d_itimer) {
  1905. if (flags & XFS_QMOPT_DOWARN)
  1906. cmn_err(CE_ALERT,
  1907. "%s : Dquot ID 0x%x (0x%p) "
  1908. "INODE TIMER NOT STARTED",
  1909. str, (int)be32_to_cpu(ddq->d_id), ddq);
  1910. errs++;
  1911. }
  1912. }
  1913. if (ddq->d_rtb_softlimit &&
  1914. be64_to_cpu(ddq->d_rtbcount) >=
  1915. be64_to_cpu(ddq->d_rtb_softlimit)) {
  1916. if (!ddq->d_rtbtimer) {
  1917. if (flags & XFS_QMOPT_DOWARN)
  1918. cmn_err(CE_ALERT,
  1919. "%s : Dquot ID 0x%x (0x%p) "
  1920. "RTBLK TIMER NOT STARTED",
  1921. str, (int)be32_to_cpu(ddq->d_id), ddq);
  1922. errs++;
  1923. }
  1924. }
  1925. }
  1926. if (!errs || !(flags & XFS_QMOPT_DQREPAIR))
  1927. return errs;
  1928. if (flags & XFS_QMOPT_DOWARN)
  1929. cmn_err(CE_NOTE, "Re-initializing dquot ID 0x%x", id);
  1930. /*
  1931. * Typically, a repair is only requested by quotacheck.
  1932. */
  1933. ASSERT(id != -1);
  1934. ASSERT(flags & XFS_QMOPT_DQREPAIR);
  1935. memset(d, 0, sizeof(xfs_dqblk_t));
  1936. d->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
  1937. d->dd_diskdq.d_version = XFS_DQUOT_VERSION;
  1938. d->dd_diskdq.d_flags = type;
  1939. d->dd_diskdq.d_id = cpu_to_be32(id);
  1940. return errs;
  1941. }
  1942. /*
  1943. * Perform a dquot buffer recovery.
  1944. * Simple algorithm: if we have found a QUOTAOFF logitem of the same type
  1945. * (ie. USR or GRP), then just toss this buffer away; don't recover it.
  1946. * Else, treat it as a regular buffer and do recovery.
  1947. */
  1948. STATIC void
  1949. xlog_recover_do_dquot_buffer(
  1950. xfs_mount_t *mp,
  1951. xlog_t *log,
  1952. xlog_recover_item_t *item,
  1953. xfs_buf_t *bp,
  1954. xfs_buf_log_format_t *buf_f)
  1955. {
  1956. uint type;
  1957. /*
  1958. * Filesystems are required to send in quota flags at mount time.
  1959. */
  1960. if (mp->m_qflags == 0) {
  1961. return;
  1962. }
  1963. type = 0;
  1964. if (buf_f->blf_flags & XFS_BLI_UDQUOT_BUF)
  1965. type |= XFS_DQ_USER;
  1966. if (buf_f->blf_flags & XFS_BLI_PDQUOT_BUF)
  1967. type |= XFS_DQ_PROJ;
  1968. if (buf_f->blf_flags & XFS_BLI_GDQUOT_BUF)
  1969. type |= XFS_DQ_GROUP;
  1970. /*
  1971. * This type of quotas was turned off, so ignore this buffer
  1972. */
  1973. if (log->l_quotaoffs_flag & type)
  1974. return;
  1975. xlog_recover_do_reg_buffer(item, bp, buf_f);
  1976. }
  1977. /*
  1978. * This routine replays a modification made to a buffer at runtime.
  1979. * There are actually two types of buffer, regular and inode, which
  1980. * are handled differently. Inode buffers are handled differently
  1981. * in that we only recover a specific set of data from them, namely
  1982. * the inode di_next_unlinked fields. This is because all other inode
  1983. * data is actually logged via inode records and any data we replay
  1984. * here which overlaps that may be stale.
  1985. *
  1986. * When meta-data buffers are freed at run time we log a buffer item
  1987. * with the XFS_BLI_CANCEL bit set to indicate that previous copies
  1988. * of the buffer in the log should not be replayed at recovery time.
  1989. * This is so that if the blocks covered by the buffer are reused for
  1990. * file data before we crash we don't end up replaying old, freed
  1991. * meta-data into a user's file.
  1992. *
  1993. * To handle the cancellation of buffer log items, we make two passes
  1994. * over the log during recovery. During the first we build a table of
  1995. * those buffers which have been cancelled, and during the second we
  1996. * only replay those buffers which do not have corresponding cancel
  1997. * records in the table. See xlog_recover_do_buffer_pass[1,2] above
  1998. * for more details on the implementation of the table of cancel records.
  1999. */
  2000. STATIC int
  2001. xlog_recover_do_buffer_trans(
  2002. xlog_t *log,
  2003. xlog_recover_item_t *item,
  2004. int pass)
  2005. {
  2006. xfs_buf_log_format_t *buf_f;
  2007. xfs_mount_t *mp;
  2008. xfs_buf_t *bp;
  2009. int error;
  2010. int cancel;
  2011. xfs_daddr_t blkno;
  2012. int len;
  2013. ushort flags;
  2014. uint buf_flags;
  2015. buf_f = (xfs_buf_log_format_t *)item->ri_buf[0].i_addr;
  2016. if (pass == XLOG_RECOVER_PASS1) {
  2017. /*
  2018. * In this pass we're only looking for buf items
  2019. * with the XFS_BLI_CANCEL bit set.
  2020. */
  2021. xlog_recover_do_buffer_pass1(log, buf_f);
  2022. return 0;
  2023. } else {
  2024. /*
  2025. * In this pass we want to recover all the buffers
  2026. * which have not been cancelled and are not
  2027. * cancellation buffers themselves. The routine
  2028. * we call here will tell us whether or not to
  2029. * continue with the replay of this buffer.
  2030. */
  2031. cancel = xlog_recover_do_buffer_pass2(log, buf_f);
  2032. if (cancel) {
  2033. return 0;
  2034. }
  2035. }
  2036. switch (buf_f->blf_type) {
  2037. case XFS_LI_BUF:
  2038. blkno = buf_f->blf_blkno;
  2039. len = buf_f->blf_len;
  2040. flags = buf_f->blf_flags;
  2041. break;
  2042. default:
  2043. xfs_fs_cmn_err(CE_ALERT, log->l_mp,
  2044. "xfs_log_recover: unknown buffer type 0x%x, logdev %s",
  2045. buf_f->blf_type, log->l_mp->m_logname ?
  2046. log->l_mp->m_logname : "internal");
  2047. XFS_ERROR_REPORT("xlog_recover_do_buffer_trans",
  2048. XFS_ERRLEVEL_LOW, log->l_mp);
  2049. return XFS_ERROR(EFSCORRUPTED);
  2050. }
  2051. mp = log->l_mp;
  2052. buf_flags = XFS_BUF_LOCK;
  2053. if (!(flags & XFS_BLI_INODE_BUF))
  2054. buf_flags |= XFS_BUF_MAPPED;
  2055. bp = xfs_buf_read(mp->m_ddev_targp, blkno, len, buf_flags);
  2056. if (XFS_BUF_ISERROR(bp)) {
  2057. xfs_ioerror_alert("xlog_recover_do..(read#1)", log->l_mp,
  2058. bp, blkno);
  2059. error = XFS_BUF_GETERROR(bp);
  2060. xfs_buf_relse(bp);
  2061. return error;
  2062. }
  2063. error = 0;
  2064. if (flags & XFS_BLI_INODE_BUF) {
  2065. error = xlog_recover_do_inode_buffer(mp, item, bp, buf_f);
  2066. } else if (flags &
  2067. (XFS_BLI_UDQUOT_BUF|XFS_BLI_PDQUOT_BUF|XFS_BLI_GDQUOT_BUF)) {
  2068. xlog_recover_do_dquot_buffer(mp, log, item, bp, buf_f);
  2069. } else {
  2070. xlog_recover_do_reg_buffer(item, bp, buf_f);
  2071. }
  2072. if (error)
  2073. return XFS_ERROR(error);
  2074. /*
  2075. * Perform delayed write on the buffer. Asynchronous writes will be
  2076. * slower when taking into account all the buffers to be flushed.
  2077. *
  2078. * Also make sure that only inode buffers with good sizes stay in
  2079. * the buffer cache. The kernel moves inodes in buffers of 1 block
  2080. * or XFS_INODE_CLUSTER_SIZE bytes, whichever is bigger. The inode
  2081. * buffers in the log can be a different size if the log was generated
  2082. * by an older kernel using unclustered inode buffers or a newer kernel
  2083. * running with a different inode cluster size. Regardless, if the
  2084. * the inode buffer size isn't MAX(blocksize, XFS_INODE_CLUSTER_SIZE)
  2085. * for *our* value of XFS_INODE_CLUSTER_SIZE, then we need to keep
  2086. * the buffer out of the buffer cache so that the buffer won't
  2087. * overlap with future reads of those inodes.
  2088. */
  2089. if (XFS_DINODE_MAGIC ==
  2090. be16_to_cpu(*((__be16 *)xfs_buf_offset(bp, 0))) &&
  2091. (XFS_BUF_COUNT(bp) != MAX(log->l_mp->m_sb.sb_blocksize,
  2092. (__uint32_t)XFS_INODE_CLUSTER_SIZE(log->l_mp)))) {
  2093. XFS_BUF_STALE(bp);
  2094. error = xfs_bwrite(mp, bp);
  2095. } else {
  2096. ASSERT(bp->b_mount == NULL || bp->b_mount == mp);
  2097. bp->b_mount = mp;
  2098. XFS_BUF_SET_IODONE_FUNC(bp, xlog_recover_iodone);
  2099. xfs_bdwrite(mp, bp);
  2100. }
  2101. return (error);
  2102. }
  2103. STATIC int
  2104. xlog_recover_do_inode_trans(
  2105. xlog_t *log,
  2106. xlog_recover_item_t *item,
  2107. int pass)
  2108. {
  2109. xfs_inode_log_format_t *in_f;
  2110. xfs_mount_t *mp;
  2111. xfs_buf_t *bp;
  2112. xfs_dinode_t *dip;
  2113. xfs_ino_t ino;
  2114. int len;
  2115. xfs_caddr_t src;
  2116. xfs_caddr_t dest;
  2117. int error;
  2118. int attr_index;
  2119. uint fields;
  2120. xfs_icdinode_t *dicp;
  2121. int need_free = 0;
  2122. if (pass == XLOG_RECOVER_PASS1) {
  2123. return 0;
  2124. }
  2125. if (item->ri_buf[0].i_len == sizeof(xfs_inode_log_format_t)) {
  2126. in_f = (xfs_inode_log_format_t *)item->ri_buf[0].i_addr;
  2127. } else {
  2128. in_f = (xfs_inode_log_format_t *)kmem_alloc(
  2129. sizeof(xfs_inode_log_format_t), KM_SLEEP);
  2130. need_free = 1;
  2131. error = xfs_inode_item_format_convert(&item->ri_buf[0], in_f);
  2132. if (error)
  2133. goto error;
  2134. }
  2135. ino = in_f->ilf_ino;
  2136. mp = log->l_mp;
  2137. /*
  2138. * Inode buffers can be freed, look out for it,
  2139. * and do not replay the inode.
  2140. */
  2141. if (xlog_check_buffer_cancelled(log, in_f->ilf_blkno,
  2142. in_f->ilf_len, 0)) {
  2143. error = 0;
  2144. goto error;
  2145. }
  2146. bp = xfs_buf_read(mp->m_ddev_targp, in_f->ilf_blkno, in_f->ilf_len,
  2147. XFS_BUF_LOCK);
  2148. if (XFS_BUF_ISERROR(bp)) {
  2149. xfs_ioerror_alert("xlog_recover_do..(read#2)", mp,
  2150. bp, in_f->ilf_blkno);
  2151. error = XFS_BUF_GETERROR(bp);
  2152. xfs_buf_relse(bp);
  2153. goto error;
  2154. }
  2155. error = 0;
  2156. ASSERT(in_f->ilf_fields & XFS_ILOG_CORE);
  2157. dip = (xfs_dinode_t *)xfs_buf_offset(bp, in_f->ilf_boffset);
  2158. /*
  2159. * Make sure the place we're flushing out to really looks
  2160. * like an inode!
  2161. */
  2162. if (unlikely(be16_to_cpu(dip->di_magic) != XFS_DINODE_MAGIC)) {
  2163. xfs_buf_relse(bp);
  2164. xfs_fs_cmn_err(CE_ALERT, mp,
  2165. "xfs_inode_recover: Bad inode magic number, dino ptr = 0x%p, dino bp = 0x%p, ino = %Ld",
  2166. dip, bp, ino);
  2167. XFS_ERROR_REPORT("xlog_recover_do_inode_trans(1)",
  2168. XFS_ERRLEVEL_LOW, mp);
  2169. error = EFSCORRUPTED;
  2170. goto error;
  2171. }
  2172. dicp = (xfs_icdinode_t *)(item->ri_buf[1].i_addr);
  2173. if (unlikely(dicp->di_magic != XFS_DINODE_MAGIC)) {
  2174. xfs_buf_relse(bp);
  2175. xfs_fs_cmn_err(CE_ALERT, mp,
  2176. "xfs_inode_recover: Bad inode log record, rec ptr 0x%p, ino %Ld",
  2177. item, ino);
  2178. XFS_ERROR_REPORT("xlog_recover_do_inode_trans(2)",
  2179. XFS_ERRLEVEL_LOW, mp);
  2180. error = EFSCORRUPTED;
  2181. goto error;
  2182. }
  2183. /* Skip replay when the on disk inode is newer than the log one */
  2184. if (dicp->di_flushiter < be16_to_cpu(dip->di_flushiter)) {
  2185. /*
  2186. * Deal with the wrap case, DI_MAX_FLUSH is less
  2187. * than smaller numbers
  2188. */
  2189. if (be16_to_cpu(dip->di_flushiter) == DI_MAX_FLUSH &&
  2190. dicp->di_flushiter < (DI_MAX_FLUSH >> 1)) {
  2191. /* do nothing */
  2192. } else {
  2193. xfs_buf_relse(bp);
  2194. error = 0;
  2195. goto error;
  2196. }
  2197. }
  2198. /* Take the opportunity to reset the flush iteration count */
  2199. dicp->di_flushiter = 0;
  2200. if (unlikely((dicp->di_mode & S_IFMT) == S_IFREG)) {
  2201. if ((dicp->di_format != XFS_DINODE_FMT_EXTENTS) &&
  2202. (dicp->di_format != XFS_DINODE_FMT_BTREE)) {
  2203. XFS_CORRUPTION_ERROR("xlog_recover_do_inode_trans(3)",
  2204. XFS_ERRLEVEL_LOW, mp, dicp);
  2205. xfs_buf_relse(bp);
  2206. xfs_fs_cmn_err(CE_ALERT, mp,
  2207. "xfs_inode_recover: Bad regular inode log record, rec ptr 0x%p, ino ptr = 0x%p, ino bp = 0x%p, ino %Ld",
  2208. item, dip, bp, ino);
  2209. error = EFSCORRUPTED;
  2210. goto error;
  2211. }
  2212. } else if (unlikely((dicp->di_mode & S_IFMT) == S_IFDIR)) {
  2213. if ((dicp->di_format != XFS_DINODE_FMT_EXTENTS) &&
  2214. (dicp->di_format != XFS_DINODE_FMT_BTREE) &&
  2215. (dicp->di_format != XFS_DINODE_FMT_LOCAL)) {
  2216. XFS_CORRUPTION_ERROR("xlog_recover_do_inode_trans(4)",
  2217. XFS_ERRLEVEL_LOW, mp, dicp);
  2218. xfs_buf_relse(bp);
  2219. xfs_fs_cmn_err(CE_ALERT, mp,
  2220. "xfs_inode_recover: Bad dir inode log record, rec ptr 0x%p, ino ptr = 0x%p, ino bp = 0x%p, ino %Ld",
  2221. item, dip, bp, ino);
  2222. error = EFSCORRUPTED;
  2223. goto error;
  2224. }
  2225. }
  2226. if (unlikely(dicp->di_nextents + dicp->di_anextents > dicp->di_nblocks)){
  2227. XFS_CORRUPTION_ERROR("xlog_recover_do_inode_trans(5)",
  2228. XFS_ERRLEVEL_LOW, mp, dicp);
  2229. xfs_buf_relse(bp);
  2230. xfs_fs_cmn_err(CE_ALERT, mp,
  2231. "xfs_inode_recover: Bad inode log record, rec ptr 0x%p, dino ptr 0x%p, dino bp 0x%p, ino %Ld, total extents = %d, nblocks = %Ld",
  2232. item, dip, bp, ino,
  2233. dicp->di_nextents + dicp->di_anextents,
  2234. dicp->di_nblocks);
  2235. error = EFSCORRUPTED;
  2236. goto error;
  2237. }
  2238. if (unlikely(dicp->di_forkoff > mp->m_sb.sb_inodesize)) {
  2239. XFS_CORRUPTION_ERROR("xlog_recover_do_inode_trans(6)",
  2240. XFS_ERRLEVEL_LOW, mp, dicp);
  2241. xfs_buf_relse(bp);
  2242. xfs_fs_cmn_err(CE_ALERT, mp,
  2243. "xfs_inode_recover: Bad inode log rec ptr 0x%p, dino ptr 0x%p, dino bp 0x%p, ino %Ld, forkoff 0x%x",
  2244. item, dip, bp, ino, dicp->di_forkoff);
  2245. error = EFSCORRUPTED;
  2246. goto error;
  2247. }
  2248. if (unlikely(item->ri_buf[1].i_len > sizeof(struct xfs_icdinode))) {
  2249. XFS_CORRUPTION_ERROR("xlog_recover_do_inode_trans(7)",
  2250. XFS_ERRLEVEL_LOW, mp, dicp);
  2251. xfs_buf_relse(bp);
  2252. xfs_fs_cmn_err(CE_ALERT, mp,
  2253. "xfs_inode_recover: Bad inode log record length %d, rec ptr 0x%p",
  2254. item->ri_buf[1].i_len, item);
  2255. error = EFSCORRUPTED;
  2256. goto error;
  2257. }
  2258. /* The core is in in-core format */
  2259. xfs_dinode_to_disk(dip, (xfs_icdinode_t *)item->ri_buf[1].i_addr);
  2260. /* the rest is in on-disk format */
  2261. if (item->ri_buf[1].i_len > sizeof(struct xfs_icdinode)) {
  2262. memcpy((xfs_caddr_t) dip + sizeof(struct xfs_icdinode),
  2263. item->ri_buf[1].i_addr + sizeof(struct xfs_icdinode),
  2264. item->ri_buf[1].i_len - sizeof(struct xfs_icdinode));
  2265. }
  2266. fields = in_f->ilf_fields;
  2267. switch (fields & (XFS_ILOG_DEV | XFS_ILOG_UUID)) {
  2268. case XFS_ILOG_DEV:
  2269. xfs_dinode_put_rdev(dip, in_f->ilf_u.ilfu_rdev);
  2270. break;
  2271. case XFS_ILOG_UUID:
  2272. memcpy(XFS_DFORK_DPTR(dip),
  2273. &in_f->ilf_u.ilfu_uuid,
  2274. sizeof(uuid_t));
  2275. break;
  2276. }
  2277. if (in_f->ilf_size == 2)
  2278. goto write_inode_buffer;
  2279. len = item->ri_buf[2].i_len;
  2280. src = item->ri_buf[2].i_addr;
  2281. ASSERT(in_f->ilf_size <= 4);
  2282. ASSERT((in_f->ilf_size == 3) || (fields & XFS_ILOG_AFORK));
  2283. ASSERT(!(fields & XFS_ILOG_DFORK) ||
  2284. (len == in_f->ilf_dsize));
  2285. switch (fields & XFS_ILOG_DFORK) {
  2286. case XFS_ILOG_DDATA:
  2287. case XFS_ILOG_DEXT:
  2288. memcpy(XFS_DFORK_DPTR(dip), src, len);
  2289. break;
  2290. case XFS_ILOG_DBROOT:
  2291. xfs_bmbt_to_bmdr(mp, (struct xfs_btree_block *)src, len,
  2292. (xfs_bmdr_block_t *)XFS_DFORK_DPTR(dip),
  2293. XFS_DFORK_DSIZE(dip, mp));
  2294. break;
  2295. default:
  2296. /*
  2297. * There are no data fork flags set.
  2298. */
  2299. ASSERT((fields & XFS_ILOG_DFORK) == 0);
  2300. break;
  2301. }
  2302. /*
  2303. * If we logged any attribute data, recover it. There may or
  2304. * may not have been any other non-core data logged in this
  2305. * transaction.
  2306. */
  2307. if (in_f->ilf_fields & XFS_ILOG_AFORK) {
  2308. if (in_f->ilf_fields & XFS_ILOG_DFORK) {
  2309. attr_index = 3;
  2310. } else {
  2311. attr_index = 2;
  2312. }
  2313. len = item->ri_buf[attr_index].i_len;
  2314. src = item->ri_buf[attr_index].i_addr;
  2315. ASSERT(len == in_f->ilf_asize);
  2316. switch (in_f->ilf_fields & XFS_ILOG_AFORK) {
  2317. case XFS_ILOG_ADATA:
  2318. case XFS_ILOG_AEXT:
  2319. dest = XFS_DFORK_APTR(dip);
  2320. ASSERT(len <= XFS_DFORK_ASIZE(dip, mp));
  2321. memcpy(dest, src, len);
  2322. break;
  2323. case XFS_ILOG_ABROOT:
  2324. dest = XFS_DFORK_APTR(dip);
  2325. xfs_bmbt_to_bmdr(mp, (struct xfs_btree_block *)src,
  2326. len, (xfs_bmdr_block_t*)dest,
  2327. XFS_DFORK_ASIZE(dip, mp));
  2328. break;
  2329. default:
  2330. xlog_warn("XFS: xlog_recover_do_inode_trans: Invalid flag");
  2331. ASSERT(0);
  2332. xfs_buf_relse(bp);
  2333. error = EIO;
  2334. goto error;
  2335. }
  2336. }
  2337. write_inode_buffer:
  2338. ASSERT(bp->b_mount == NULL || bp->b_mount == mp);
  2339. bp->b_mount = mp;
  2340. XFS_BUF_SET_IODONE_FUNC(bp, xlog_recover_iodone);
  2341. xfs_bdwrite(mp, bp);
  2342. error:
  2343. if (need_free)
  2344. kmem_free(in_f);
  2345. return XFS_ERROR(error);
  2346. }
  2347. /*
  2348. * Recover QUOTAOFF records. We simply make a note of it in the xlog_t
  2349. * structure, so that we know not to do any dquot item or dquot buffer recovery,
  2350. * of that type.
  2351. */
  2352. STATIC int
  2353. xlog_recover_do_quotaoff_trans(
  2354. xlog_t *log,
  2355. xlog_recover_item_t *item,
  2356. int pass)
  2357. {
  2358. xfs_qoff_logformat_t *qoff_f;
  2359. if (pass == XLOG_RECOVER_PASS2) {
  2360. return (0);
  2361. }
  2362. qoff_f = (xfs_qoff_logformat_t *)item->ri_buf[0].i_addr;
  2363. ASSERT(qoff_f);
  2364. /*
  2365. * The logitem format's flag tells us if this was user quotaoff,
  2366. * group/project quotaoff or both.
  2367. */
  2368. if (qoff_f->qf_flags & XFS_UQUOTA_ACCT)
  2369. log->l_quotaoffs_flag |= XFS_DQ_USER;
  2370. if (qoff_f->qf_flags & XFS_PQUOTA_ACCT)
  2371. log->l_quotaoffs_flag |= XFS_DQ_PROJ;
  2372. if (qoff_f->qf_flags & XFS_GQUOTA_ACCT)
  2373. log->l_quotaoffs_flag |= XFS_DQ_GROUP;
  2374. return (0);
  2375. }
  2376. /*
  2377. * Recover a dquot record
  2378. */
  2379. STATIC int
  2380. xlog_recover_do_dquot_trans(
  2381. xlog_t *log,
  2382. xlog_recover_item_t *item,
  2383. int pass)
  2384. {
  2385. xfs_mount_t *mp;
  2386. xfs_buf_t *bp;
  2387. struct xfs_disk_dquot *ddq, *recddq;
  2388. int error;
  2389. xfs_dq_logformat_t *dq_f;
  2390. uint type;
  2391. if (pass == XLOG_RECOVER_PASS1) {
  2392. return 0;
  2393. }
  2394. mp = log->l_mp;
  2395. /*
  2396. * Filesystems are required to send in quota flags at mount time.
  2397. */
  2398. if (mp->m_qflags == 0)
  2399. return (0);
  2400. recddq = (xfs_disk_dquot_t *)item->ri_buf[1].i_addr;
  2401. if (item->ri_buf[1].i_addr == NULL) {
  2402. cmn_err(CE_ALERT,
  2403. "XFS: NULL dquot in %s.", __func__);
  2404. return XFS_ERROR(EIO);
  2405. }
  2406. if (item->ri_buf[1].i_len < sizeof(xfs_disk_dquot_t)) {
  2407. cmn_err(CE_ALERT,
  2408. "XFS: dquot too small (%d) in %s.",
  2409. item->ri_buf[1].i_len, __func__);
  2410. return XFS_ERROR(EIO);
  2411. }
  2412. /*
  2413. * This type of quotas was turned off, so ignore this record.
  2414. */
  2415. type = recddq->d_flags & (XFS_DQ_USER | XFS_DQ_PROJ | XFS_DQ_GROUP);
  2416. ASSERT(type);
  2417. if (log->l_quotaoffs_flag & type)
  2418. return (0);
  2419. /*
  2420. * At this point we know that quota was _not_ turned off.
  2421. * Since the mount flags are not indicating to us otherwise, this
  2422. * must mean that quota is on, and the dquot needs to be replayed.
  2423. * Remember that we may not have fully recovered the superblock yet,
  2424. * so we can't do the usual trick of looking at the SB quota bits.
  2425. *
  2426. * The other possibility, of course, is that the quota subsystem was
  2427. * removed since the last mount - ENOSYS.
  2428. */
  2429. dq_f = (xfs_dq_logformat_t *)item->ri_buf[0].i_addr;
  2430. ASSERT(dq_f);
  2431. if ((error = xfs_qm_dqcheck(recddq,
  2432. dq_f->qlf_id,
  2433. 0, XFS_QMOPT_DOWARN,
  2434. "xlog_recover_do_dquot_trans (log copy)"))) {
  2435. return XFS_ERROR(EIO);
  2436. }
  2437. ASSERT(dq_f->qlf_len == 1);
  2438. error = xfs_read_buf(mp, mp->m_ddev_targp,
  2439. dq_f->qlf_blkno,
  2440. XFS_FSB_TO_BB(mp, dq_f->qlf_len),
  2441. 0, &bp);
  2442. if (error) {
  2443. xfs_ioerror_alert("xlog_recover_do..(read#3)", mp,
  2444. bp, dq_f->qlf_blkno);
  2445. return error;
  2446. }
  2447. ASSERT(bp);
  2448. ddq = (xfs_disk_dquot_t *)xfs_buf_offset(bp, dq_f->qlf_boffset);
  2449. /*
  2450. * At least the magic num portion should be on disk because this
  2451. * was among a chunk of dquots created earlier, and we did some
  2452. * minimal initialization then.
  2453. */
  2454. if (xfs_qm_dqcheck(ddq, dq_f->qlf_id, 0, XFS_QMOPT_DOWARN,
  2455. "xlog_recover_do_dquot_trans")) {
  2456. xfs_buf_relse(bp);
  2457. return XFS_ERROR(EIO);
  2458. }
  2459. memcpy(ddq, recddq, item->ri_buf[1].i_len);
  2460. ASSERT(dq_f->qlf_size == 2);
  2461. ASSERT(bp->b_mount == NULL || bp->b_mount == mp);
  2462. bp->b_mount = mp;
  2463. XFS_BUF_SET_IODONE_FUNC(bp, xlog_recover_iodone);
  2464. xfs_bdwrite(mp, bp);
  2465. return (0);
  2466. }
  2467. /*
  2468. * This routine is called to create an in-core extent free intent
  2469. * item from the efi format structure which was logged on disk.
  2470. * It allocates an in-core efi, copies the extents from the format
  2471. * structure into it, and adds the efi to the AIL with the given
  2472. * LSN.
  2473. */
  2474. STATIC int
  2475. xlog_recover_do_efi_trans(
  2476. xlog_t *log,
  2477. xlog_recover_item_t *item,
  2478. xfs_lsn_t lsn,
  2479. int pass)
  2480. {
  2481. int error;
  2482. xfs_mount_t *mp;
  2483. xfs_efi_log_item_t *efip;
  2484. xfs_efi_log_format_t *efi_formatp;
  2485. if (pass == XLOG_RECOVER_PASS1) {
  2486. return 0;
  2487. }
  2488. efi_formatp = (xfs_efi_log_format_t *)item->ri_buf[0].i_addr;
  2489. mp = log->l_mp;
  2490. efip = xfs_efi_init(mp, efi_formatp->efi_nextents);
  2491. if ((error = xfs_efi_copy_format(&(item->ri_buf[0]),
  2492. &(efip->efi_format)))) {
  2493. xfs_efi_item_free(efip);
  2494. return error;
  2495. }
  2496. efip->efi_next_extent = efi_formatp->efi_nextents;
  2497. efip->efi_flags |= XFS_EFI_COMMITTED;
  2498. spin_lock(&log->l_ailp->xa_lock);
  2499. /*
  2500. * xfs_trans_ail_update() drops the AIL lock.
  2501. */
  2502. xfs_trans_ail_update(log->l_ailp, (xfs_log_item_t *)efip, lsn);
  2503. return 0;
  2504. }
  2505. /*
  2506. * This routine is called when an efd format structure is found in
  2507. * a committed transaction in the log. It's purpose is to cancel
  2508. * the corresponding efi if it was still in the log. To do this
  2509. * it searches the AIL for the efi with an id equal to that in the
  2510. * efd format structure. If we find it, we remove the efi from the
  2511. * AIL and free it.
  2512. */
  2513. STATIC void
  2514. xlog_recover_do_efd_trans(
  2515. xlog_t *log,
  2516. xlog_recover_item_t *item,
  2517. int pass)
  2518. {
  2519. xfs_efd_log_format_t *efd_formatp;
  2520. xfs_efi_log_item_t *efip = NULL;
  2521. xfs_log_item_t *lip;
  2522. __uint64_t efi_id;
  2523. struct xfs_ail_cursor cur;
  2524. struct xfs_ail *ailp = log->l_ailp;
  2525. if (pass == XLOG_RECOVER_PASS1) {
  2526. return;
  2527. }
  2528. efd_formatp = (xfs_efd_log_format_t *)item->ri_buf[0].i_addr;
  2529. ASSERT((item->ri_buf[0].i_len == (sizeof(xfs_efd_log_format_32_t) +
  2530. ((efd_formatp->efd_nextents - 1) * sizeof(xfs_extent_32_t)))) ||
  2531. (item->ri_buf[0].i_len == (sizeof(xfs_efd_log_format_64_t) +
  2532. ((efd_formatp->efd_nextents - 1) * sizeof(xfs_extent_64_t)))));
  2533. efi_id = efd_formatp->efd_efi_id;
  2534. /*
  2535. * Search for the efi with the id in the efd format structure
  2536. * in the AIL.
  2537. */
  2538. spin_lock(&ailp->xa_lock);
  2539. lip = xfs_trans_ail_cursor_first(ailp, &cur, 0);
  2540. while (lip != NULL) {
  2541. if (lip->li_type == XFS_LI_EFI) {
  2542. efip = (xfs_efi_log_item_t *)lip;
  2543. if (efip->efi_format.efi_id == efi_id) {
  2544. /*
  2545. * xfs_trans_ail_delete() drops the
  2546. * AIL lock.
  2547. */
  2548. xfs_trans_ail_delete(ailp, lip);
  2549. xfs_efi_item_free(efip);
  2550. spin_lock(&ailp->xa_lock);
  2551. break;
  2552. }
  2553. }
  2554. lip = xfs_trans_ail_cursor_next(ailp, &cur);
  2555. }
  2556. xfs_trans_ail_cursor_done(ailp, &cur);
  2557. spin_unlock(&ailp->xa_lock);
  2558. }
  2559. /*
  2560. * Perform the transaction
  2561. *
  2562. * If the transaction modifies a buffer or inode, do it now. Otherwise,
  2563. * EFIs and EFDs get queued up by adding entries into the AIL for them.
  2564. */
  2565. STATIC int
  2566. xlog_recover_do_trans(
  2567. xlog_t *log,
  2568. xlog_recover_t *trans,
  2569. int pass)
  2570. {
  2571. int error = 0;
  2572. xlog_recover_item_t *item, *first_item;
  2573. error = xlog_recover_reorder_trans(trans);
  2574. if (error)
  2575. return error;
  2576. first_item = item = trans->r_itemq;
  2577. do {
  2578. switch (ITEM_TYPE(item)) {
  2579. case XFS_LI_BUF:
  2580. error = xlog_recover_do_buffer_trans(log, item, pass);
  2581. break;
  2582. case XFS_LI_INODE:
  2583. error = xlog_recover_do_inode_trans(log, item, pass);
  2584. break;
  2585. case XFS_LI_EFI:
  2586. error = xlog_recover_do_efi_trans(log, item,
  2587. trans->r_lsn, pass);
  2588. break;
  2589. case XFS_LI_EFD:
  2590. xlog_recover_do_efd_trans(log, item, pass);
  2591. error = 0;
  2592. break;
  2593. case XFS_LI_DQUOT:
  2594. error = xlog_recover_do_dquot_trans(log, item, pass);
  2595. break;
  2596. case XFS_LI_QUOTAOFF:
  2597. error = xlog_recover_do_quotaoff_trans(log, item,
  2598. pass);
  2599. break;
  2600. default:
  2601. xlog_warn(
  2602. "XFS: invalid item type (%d) xlog_recover_do_trans", ITEM_TYPE(item));
  2603. ASSERT(0);
  2604. error = XFS_ERROR(EIO);
  2605. break;
  2606. }
  2607. if (error)
  2608. return error;
  2609. item = item->ri_next;
  2610. } while (first_item != item);
  2611. return 0;
  2612. }
  2613. /*
  2614. * Free up any resources allocated by the transaction
  2615. *
  2616. * Remember that EFIs, EFDs, and IUNLINKs are handled later.
  2617. */
  2618. STATIC void
  2619. xlog_recover_free_trans(
  2620. xlog_recover_t *trans)
  2621. {
  2622. xlog_recover_item_t *first_item, *item, *free_item;
  2623. int i;
  2624. item = first_item = trans->r_itemq;
  2625. do {
  2626. free_item = item;
  2627. item = item->ri_next;
  2628. /* Free the regions in the item. */
  2629. for (i = 0; i < free_item->ri_cnt; i++) {
  2630. kmem_free(free_item->ri_buf[i].i_addr);
  2631. }
  2632. /* Free the item itself */
  2633. kmem_free(free_item->ri_buf);
  2634. kmem_free(free_item);
  2635. } while (first_item != item);
  2636. /* Free the transaction recover structure */
  2637. kmem_free(trans);
  2638. }
  2639. STATIC int
  2640. xlog_recover_commit_trans(
  2641. xlog_t *log,
  2642. xlog_recover_t **q,
  2643. xlog_recover_t *trans,
  2644. int pass)
  2645. {
  2646. int error;
  2647. if ((error = xlog_recover_unlink_tid(q, trans)))
  2648. return error;
  2649. if ((error = xlog_recover_do_trans(log, trans, pass)))
  2650. return error;
  2651. xlog_recover_free_trans(trans); /* no error */
  2652. return 0;
  2653. }
  2654. STATIC int
  2655. xlog_recover_unmount_trans(
  2656. xlog_recover_t *trans)
  2657. {
  2658. /* Do nothing now */
  2659. xlog_warn("XFS: xlog_recover_unmount_trans: Unmount LR");
  2660. return 0;
  2661. }
  2662. /*
  2663. * There are two valid states of the r_state field. 0 indicates that the
  2664. * transaction structure is in a normal state. We have either seen the
  2665. * start of the transaction or the last operation we added was not a partial
  2666. * operation. If the last operation we added to the transaction was a
  2667. * partial operation, we need to mark r_state with XLOG_WAS_CONT_TRANS.
  2668. *
  2669. * NOTE: skip LRs with 0 data length.
  2670. */
  2671. STATIC int
  2672. xlog_recover_process_data(
  2673. xlog_t *log,
  2674. xlog_recover_t *rhash[],
  2675. xlog_rec_header_t *rhead,
  2676. xfs_caddr_t dp,
  2677. int pass)
  2678. {
  2679. xfs_caddr_t lp;
  2680. int num_logops;
  2681. xlog_op_header_t *ohead;
  2682. xlog_recover_t *trans;
  2683. xlog_tid_t tid;
  2684. int error;
  2685. unsigned long hash;
  2686. uint flags;
  2687. lp = dp + be32_to_cpu(rhead->h_len);
  2688. num_logops = be32_to_cpu(rhead->h_num_logops);
  2689. /* check the log format matches our own - else we can't recover */
  2690. if (xlog_header_check_recover(log->l_mp, rhead))
  2691. return (XFS_ERROR(EIO));
  2692. while ((dp < lp) && num_logops) {
  2693. ASSERT(dp + sizeof(xlog_op_header_t) <= lp);
  2694. ohead = (xlog_op_header_t *)dp;
  2695. dp += sizeof(xlog_op_header_t);
  2696. if (ohead->oh_clientid != XFS_TRANSACTION &&
  2697. ohead->oh_clientid != XFS_LOG) {
  2698. xlog_warn(
  2699. "XFS: xlog_recover_process_data: bad clientid");
  2700. ASSERT(0);
  2701. return (XFS_ERROR(EIO));
  2702. }
  2703. tid = be32_to_cpu(ohead->oh_tid);
  2704. hash = XLOG_RHASH(tid);
  2705. trans = xlog_recover_find_tid(rhash[hash], tid);
  2706. if (trans == NULL) { /* not found; add new tid */
  2707. if (ohead->oh_flags & XLOG_START_TRANS)
  2708. xlog_recover_new_tid(&rhash[hash], tid,
  2709. be64_to_cpu(rhead->h_lsn));
  2710. } else {
  2711. if (dp + be32_to_cpu(ohead->oh_len) > lp) {
  2712. xlog_warn(
  2713. "XFS: xlog_recover_process_data: bad length");
  2714. WARN_ON(1);
  2715. return (XFS_ERROR(EIO));
  2716. }
  2717. flags = ohead->oh_flags & ~XLOG_END_TRANS;
  2718. if (flags & XLOG_WAS_CONT_TRANS)
  2719. flags &= ~XLOG_CONTINUE_TRANS;
  2720. switch (flags) {
  2721. case XLOG_COMMIT_TRANS:
  2722. error = xlog_recover_commit_trans(log,
  2723. &rhash[hash], trans, pass);
  2724. break;
  2725. case XLOG_UNMOUNT_TRANS:
  2726. error = xlog_recover_unmount_trans(trans);
  2727. break;
  2728. case XLOG_WAS_CONT_TRANS:
  2729. error = xlog_recover_add_to_cont_trans(trans,
  2730. dp, be32_to_cpu(ohead->oh_len));
  2731. break;
  2732. case XLOG_START_TRANS:
  2733. xlog_warn(
  2734. "XFS: xlog_recover_process_data: bad transaction");
  2735. ASSERT(0);
  2736. error = XFS_ERROR(EIO);
  2737. break;
  2738. case 0:
  2739. case XLOG_CONTINUE_TRANS:
  2740. error = xlog_recover_add_to_trans(trans,
  2741. dp, be32_to_cpu(ohead->oh_len));
  2742. break;
  2743. default:
  2744. xlog_warn(
  2745. "XFS: xlog_recover_process_data: bad flag");
  2746. ASSERT(0);
  2747. error = XFS_ERROR(EIO);
  2748. break;
  2749. }
  2750. if (error)
  2751. return error;
  2752. }
  2753. dp += be32_to_cpu(ohead->oh_len);
  2754. num_logops--;
  2755. }
  2756. return 0;
  2757. }
  2758. /*
  2759. * Process an extent free intent item that was recovered from
  2760. * the log. We need to free the extents that it describes.
  2761. */
  2762. STATIC int
  2763. xlog_recover_process_efi(
  2764. xfs_mount_t *mp,
  2765. xfs_efi_log_item_t *efip)
  2766. {
  2767. xfs_efd_log_item_t *efdp;
  2768. xfs_trans_t *tp;
  2769. int i;
  2770. int error = 0;
  2771. xfs_extent_t *extp;
  2772. xfs_fsblock_t startblock_fsb;
  2773. ASSERT(!(efip->efi_flags & XFS_EFI_RECOVERED));
  2774. /*
  2775. * First check the validity of the extents described by the
  2776. * EFI. If any are bad, then assume that all are bad and
  2777. * just toss the EFI.
  2778. */
  2779. for (i = 0; i < efip->efi_format.efi_nextents; i++) {
  2780. extp = &(efip->efi_format.efi_extents[i]);
  2781. startblock_fsb = XFS_BB_TO_FSB(mp,
  2782. XFS_FSB_TO_DADDR(mp, extp->ext_start));
  2783. if ((startblock_fsb == 0) ||
  2784. (extp->ext_len == 0) ||
  2785. (startblock_fsb >= mp->m_sb.sb_dblocks) ||
  2786. (extp->ext_len >= mp->m_sb.sb_agblocks)) {
  2787. /*
  2788. * This will pull the EFI from the AIL and
  2789. * free the memory associated with it.
  2790. */
  2791. xfs_efi_release(efip, efip->efi_format.efi_nextents);
  2792. return XFS_ERROR(EIO);
  2793. }
  2794. }
  2795. tp = xfs_trans_alloc(mp, 0);
  2796. error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0, 0, 0);
  2797. if (error)
  2798. goto abort_error;
  2799. efdp = xfs_trans_get_efd(tp, efip, efip->efi_format.efi_nextents);
  2800. for (i = 0; i < efip->efi_format.efi_nextents; i++) {
  2801. extp = &(efip->efi_format.efi_extents[i]);
  2802. error = xfs_free_extent(tp, extp->ext_start, extp->ext_len);
  2803. if (error)
  2804. goto abort_error;
  2805. xfs_trans_log_efd_extent(tp, efdp, extp->ext_start,
  2806. extp->ext_len);
  2807. }
  2808. efip->efi_flags |= XFS_EFI_RECOVERED;
  2809. error = xfs_trans_commit(tp, 0);
  2810. return error;
  2811. abort_error:
  2812. xfs_trans_cancel(tp, XFS_TRANS_ABORT);
  2813. return error;
  2814. }
  2815. /*
  2816. * When this is called, all of the EFIs which did not have
  2817. * corresponding EFDs should be in the AIL. What we do now
  2818. * is free the extents associated with each one.
  2819. *
  2820. * Since we process the EFIs in normal transactions, they
  2821. * will be removed at some point after the commit. This prevents
  2822. * us from just walking down the list processing each one.
  2823. * We'll use a flag in the EFI to skip those that we've already
  2824. * processed and use the AIL iteration mechanism's generation
  2825. * count to try to speed this up at least a bit.
  2826. *
  2827. * When we start, we know that the EFIs are the only things in
  2828. * the AIL. As we process them, however, other items are added
  2829. * to the AIL. Since everything added to the AIL must come after
  2830. * everything already in the AIL, we stop processing as soon as
  2831. * we see something other than an EFI in the AIL.
  2832. */
  2833. STATIC int
  2834. xlog_recover_process_efis(
  2835. xlog_t *log)
  2836. {
  2837. xfs_log_item_t *lip;
  2838. xfs_efi_log_item_t *efip;
  2839. int error = 0;
  2840. struct xfs_ail_cursor cur;
  2841. struct xfs_ail *ailp;
  2842. ailp = log->l_ailp;
  2843. spin_lock(&ailp->xa_lock);
  2844. lip = xfs_trans_ail_cursor_first(ailp, &cur, 0);
  2845. while (lip != NULL) {
  2846. /*
  2847. * We're done when we see something other than an EFI.
  2848. * There should be no EFIs left in the AIL now.
  2849. */
  2850. if (lip->li_type != XFS_LI_EFI) {
  2851. #ifdef DEBUG
  2852. for (; lip; lip = xfs_trans_ail_cursor_next(ailp, &cur))
  2853. ASSERT(lip->li_type != XFS_LI_EFI);
  2854. #endif
  2855. break;
  2856. }
  2857. /*
  2858. * Skip EFIs that we've already processed.
  2859. */
  2860. efip = (xfs_efi_log_item_t *)lip;
  2861. if (efip->efi_flags & XFS_EFI_RECOVERED) {
  2862. lip = xfs_trans_ail_cursor_next(ailp, &cur);
  2863. continue;
  2864. }
  2865. spin_unlock(&ailp->xa_lock);
  2866. error = xlog_recover_process_efi(log->l_mp, efip);
  2867. spin_lock(&ailp->xa_lock);
  2868. if (error)
  2869. goto out;
  2870. lip = xfs_trans_ail_cursor_next(ailp, &cur);
  2871. }
  2872. out:
  2873. xfs_trans_ail_cursor_done(ailp, &cur);
  2874. spin_unlock(&ailp->xa_lock);
  2875. return error;
  2876. }
  2877. /*
  2878. * This routine performs a transaction to null out a bad inode pointer
  2879. * in an agi unlinked inode hash bucket.
  2880. */
  2881. STATIC void
  2882. xlog_recover_clear_agi_bucket(
  2883. xfs_mount_t *mp,
  2884. xfs_agnumber_t agno,
  2885. int bucket)
  2886. {
  2887. xfs_trans_t *tp;
  2888. xfs_agi_t *agi;
  2889. xfs_buf_t *agibp;
  2890. int offset;
  2891. int error;
  2892. tp = xfs_trans_alloc(mp, XFS_TRANS_CLEAR_AGI_BUCKET);
  2893. error = xfs_trans_reserve(tp, 0, XFS_CLEAR_AGI_BUCKET_LOG_RES(mp),
  2894. 0, 0, 0);
  2895. if (error)
  2896. goto out_abort;
  2897. error = xfs_read_agi(mp, tp, agno, &agibp);
  2898. if (error)
  2899. goto out_abort;
  2900. agi = XFS_BUF_TO_AGI(agibp);
  2901. agi->agi_unlinked[bucket] = cpu_to_be32(NULLAGINO);
  2902. offset = offsetof(xfs_agi_t, agi_unlinked) +
  2903. (sizeof(xfs_agino_t) * bucket);
  2904. xfs_trans_log_buf(tp, agibp, offset,
  2905. (offset + sizeof(xfs_agino_t) - 1));
  2906. error = xfs_trans_commit(tp, 0);
  2907. if (error)
  2908. goto out_error;
  2909. return;
  2910. out_abort:
  2911. xfs_trans_cancel(tp, XFS_TRANS_ABORT);
  2912. out_error:
  2913. xfs_fs_cmn_err(CE_WARN, mp, "xlog_recover_clear_agi_bucket: "
  2914. "failed to clear agi %d. Continuing.", agno);
  2915. return;
  2916. }
  2917. STATIC xfs_agino_t
  2918. xlog_recover_process_one_iunlink(
  2919. struct xfs_mount *mp,
  2920. xfs_agnumber_t agno,
  2921. xfs_agino_t agino,
  2922. int bucket)
  2923. {
  2924. struct xfs_buf *ibp;
  2925. struct xfs_dinode *dip;
  2926. struct xfs_inode *ip;
  2927. xfs_ino_t ino;
  2928. int error;
  2929. ino = XFS_AGINO_TO_INO(mp, agno, agino);
  2930. error = xfs_iget(mp, NULL, ino, 0, 0, &ip, 0);
  2931. if (error)
  2932. goto fail;
  2933. /*
  2934. * Get the on disk inode to find the next inode in the bucket.
  2935. */
  2936. error = xfs_itobp(mp, NULL, ip, &dip, &ibp, XFS_BUF_LOCK);
  2937. if (error)
  2938. goto fail_iput;
  2939. ASSERT(ip->i_d.di_nlink == 0);
  2940. ASSERT(ip->i_d.di_mode != 0);
  2941. /* setup for the next pass */
  2942. agino = be32_to_cpu(dip->di_next_unlinked);
  2943. xfs_buf_relse(ibp);
  2944. /*
  2945. * Prevent any DMAPI event from being sent when the reference on
  2946. * the inode is dropped.
  2947. */
  2948. ip->i_d.di_dmevmask = 0;
  2949. IRELE(ip);
  2950. return agino;
  2951. fail_iput:
  2952. IRELE(ip);
  2953. fail:
  2954. /*
  2955. * We can't read in the inode this bucket points to, or this inode
  2956. * is messed up. Just ditch this bucket of inodes. We will lose
  2957. * some inodes and space, but at least we won't hang.
  2958. *
  2959. * Call xlog_recover_clear_agi_bucket() to perform a transaction to
  2960. * clear the inode pointer in the bucket.
  2961. */
  2962. xlog_recover_clear_agi_bucket(mp, agno, bucket);
  2963. return NULLAGINO;
  2964. }
  2965. /*
  2966. * xlog_iunlink_recover
  2967. *
  2968. * This is called during recovery to process any inodes which
  2969. * we unlinked but not freed when the system crashed. These
  2970. * inodes will be on the lists in the AGI blocks. What we do
  2971. * here is scan all the AGIs and fully truncate and free any
  2972. * inodes found on the lists. Each inode is removed from the
  2973. * lists when it has been fully truncated and is freed. The
  2974. * freeing of the inode and its removal from the list must be
  2975. * atomic.
  2976. */
  2977. STATIC void
  2978. xlog_recover_process_iunlinks(
  2979. xlog_t *log)
  2980. {
  2981. xfs_mount_t *mp;
  2982. xfs_agnumber_t agno;
  2983. xfs_agi_t *agi;
  2984. xfs_buf_t *agibp;
  2985. xfs_agino_t agino;
  2986. int bucket;
  2987. int error;
  2988. uint mp_dmevmask;
  2989. mp = log->l_mp;
  2990. /*
  2991. * Prevent any DMAPI event from being sent while in this function.
  2992. */
  2993. mp_dmevmask = mp->m_dmevmask;
  2994. mp->m_dmevmask = 0;
  2995. for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
  2996. /*
  2997. * Find the agi for this ag.
  2998. */
  2999. error = xfs_read_agi(mp, NULL, agno, &agibp);
  3000. if (error) {
  3001. /*
  3002. * AGI is b0rked. Don't process it.
  3003. *
  3004. * We should probably mark the filesystem as corrupt
  3005. * after we've recovered all the ag's we can....
  3006. */
  3007. continue;
  3008. }
  3009. agi = XFS_BUF_TO_AGI(agibp);
  3010. for (bucket = 0; bucket < XFS_AGI_UNLINKED_BUCKETS; bucket++) {
  3011. agino = be32_to_cpu(agi->agi_unlinked[bucket]);
  3012. while (agino != NULLAGINO) {
  3013. /*
  3014. * Release the agi buffer so that it can
  3015. * be acquired in the normal course of the
  3016. * transaction to truncate and free the inode.
  3017. */
  3018. xfs_buf_relse(agibp);
  3019. agino = xlog_recover_process_one_iunlink(mp,
  3020. agno, agino, bucket);
  3021. /*
  3022. * Reacquire the agibuffer and continue around
  3023. * the loop. This should never fail as we know
  3024. * the buffer was good earlier on.
  3025. */
  3026. error = xfs_read_agi(mp, NULL, agno, &agibp);
  3027. ASSERT(error == 0);
  3028. agi = XFS_BUF_TO_AGI(agibp);
  3029. }
  3030. }
  3031. /*
  3032. * Release the buffer for the current agi so we can
  3033. * go on to the next one.
  3034. */
  3035. xfs_buf_relse(agibp);
  3036. }
  3037. mp->m_dmevmask = mp_dmevmask;
  3038. }
  3039. #ifdef DEBUG
  3040. STATIC void
  3041. xlog_pack_data_checksum(
  3042. xlog_t *log,
  3043. xlog_in_core_t *iclog,
  3044. int size)
  3045. {
  3046. int i;
  3047. __be32 *up;
  3048. uint chksum = 0;
  3049. up = (__be32 *)iclog->ic_datap;
  3050. /* divide length by 4 to get # words */
  3051. for (i = 0; i < (size >> 2); i++) {
  3052. chksum ^= be32_to_cpu(*up);
  3053. up++;
  3054. }
  3055. iclog->ic_header.h_chksum = cpu_to_be32(chksum);
  3056. }
  3057. #else
  3058. #define xlog_pack_data_checksum(log, iclog, size)
  3059. #endif
  3060. /*
  3061. * Stamp cycle number in every block
  3062. */
  3063. void
  3064. xlog_pack_data(
  3065. xlog_t *log,
  3066. xlog_in_core_t *iclog,
  3067. int roundoff)
  3068. {
  3069. int i, j, k;
  3070. int size = iclog->ic_offset + roundoff;
  3071. __be32 cycle_lsn;
  3072. xfs_caddr_t dp;
  3073. xlog_pack_data_checksum(log, iclog, size);
  3074. cycle_lsn = CYCLE_LSN_DISK(iclog->ic_header.h_lsn);
  3075. dp = iclog->ic_datap;
  3076. for (i = 0; i < BTOBB(size) &&
  3077. i < (XLOG_HEADER_CYCLE_SIZE / BBSIZE); i++) {
  3078. iclog->ic_header.h_cycle_data[i] = *(__be32 *)dp;
  3079. *(__be32 *)dp = cycle_lsn;
  3080. dp += BBSIZE;
  3081. }
  3082. if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
  3083. xlog_in_core_2_t *xhdr = iclog->ic_data;
  3084. for ( ; i < BTOBB(size); i++) {
  3085. j = i / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
  3086. k = i % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
  3087. xhdr[j].hic_xheader.xh_cycle_data[k] = *(__be32 *)dp;
  3088. *(__be32 *)dp = cycle_lsn;
  3089. dp += BBSIZE;
  3090. }
  3091. for (i = 1; i < log->l_iclog_heads; i++) {
  3092. xhdr[i].hic_xheader.xh_cycle = cycle_lsn;
  3093. }
  3094. }
  3095. }
  3096. #if defined(DEBUG) && defined(XFS_LOUD_RECOVERY)
  3097. STATIC void
  3098. xlog_unpack_data_checksum(
  3099. xlog_rec_header_t *rhead,
  3100. xfs_caddr_t dp,
  3101. xlog_t *log)
  3102. {
  3103. __be32 *up = (__be32 *)dp;
  3104. uint chksum = 0;
  3105. int i;
  3106. /* divide length by 4 to get # words */
  3107. for (i=0; i < be32_to_cpu(rhead->h_len) >> 2; i++) {
  3108. chksum ^= be32_to_cpu(*up);
  3109. up++;
  3110. }
  3111. if (chksum != be32_to_cpu(rhead->h_chksum)) {
  3112. if (rhead->h_chksum ||
  3113. ((log->l_flags & XLOG_CHKSUM_MISMATCH) == 0)) {
  3114. cmn_err(CE_DEBUG,
  3115. "XFS: LogR chksum mismatch: was (0x%x) is (0x%x)\n",
  3116. be32_to_cpu(rhead->h_chksum), chksum);
  3117. cmn_err(CE_DEBUG,
  3118. "XFS: Disregard message if filesystem was created with non-DEBUG kernel");
  3119. if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
  3120. cmn_err(CE_DEBUG,
  3121. "XFS: LogR this is a LogV2 filesystem\n");
  3122. }
  3123. log->l_flags |= XLOG_CHKSUM_MISMATCH;
  3124. }
  3125. }
  3126. }
  3127. #else
  3128. #define xlog_unpack_data_checksum(rhead, dp, log)
  3129. #endif
  3130. STATIC void
  3131. xlog_unpack_data(
  3132. xlog_rec_header_t *rhead,
  3133. xfs_caddr_t dp,
  3134. xlog_t *log)
  3135. {
  3136. int i, j, k;
  3137. for (i = 0; i < BTOBB(be32_to_cpu(rhead->h_len)) &&
  3138. i < (XLOG_HEADER_CYCLE_SIZE / BBSIZE); i++) {
  3139. *(__be32 *)dp = *(__be32 *)&rhead->h_cycle_data[i];
  3140. dp += BBSIZE;
  3141. }
  3142. if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
  3143. xlog_in_core_2_t *xhdr = (xlog_in_core_2_t *)rhead;
  3144. for ( ; i < BTOBB(be32_to_cpu(rhead->h_len)); i++) {
  3145. j = i / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
  3146. k = i % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
  3147. *(__be32 *)dp = xhdr[j].hic_xheader.xh_cycle_data[k];
  3148. dp += BBSIZE;
  3149. }
  3150. }
  3151. xlog_unpack_data_checksum(rhead, dp, log);
  3152. }
  3153. STATIC int
  3154. xlog_valid_rec_header(
  3155. xlog_t *log,
  3156. xlog_rec_header_t *rhead,
  3157. xfs_daddr_t blkno)
  3158. {
  3159. int hlen;
  3160. if (unlikely(be32_to_cpu(rhead->h_magicno) != XLOG_HEADER_MAGIC_NUM)) {
  3161. XFS_ERROR_REPORT("xlog_valid_rec_header(1)",
  3162. XFS_ERRLEVEL_LOW, log->l_mp);
  3163. return XFS_ERROR(EFSCORRUPTED);
  3164. }
  3165. if (unlikely(
  3166. (!rhead->h_version ||
  3167. (be32_to_cpu(rhead->h_version) & (~XLOG_VERSION_OKBITS))))) {
  3168. xlog_warn("XFS: %s: unrecognised log version (%d).",
  3169. __func__, be32_to_cpu(rhead->h_version));
  3170. return XFS_ERROR(EIO);
  3171. }
  3172. /* LR body must have data or it wouldn't have been written */
  3173. hlen = be32_to_cpu(rhead->h_len);
  3174. if (unlikely( hlen <= 0 || hlen > INT_MAX )) {
  3175. XFS_ERROR_REPORT("xlog_valid_rec_header(2)",
  3176. XFS_ERRLEVEL_LOW, log->l_mp);
  3177. return XFS_ERROR(EFSCORRUPTED);
  3178. }
  3179. if (unlikely( blkno > log->l_logBBsize || blkno > INT_MAX )) {
  3180. XFS_ERROR_REPORT("xlog_valid_rec_header(3)",
  3181. XFS_ERRLEVEL_LOW, log->l_mp);
  3182. return XFS_ERROR(EFSCORRUPTED);
  3183. }
  3184. return 0;
  3185. }
  3186. /*
  3187. * Read the log from tail to head and process the log records found.
  3188. * Handle the two cases where the tail and head are in the same cycle
  3189. * and where the active portion of the log wraps around the end of
  3190. * the physical log separately. The pass parameter is passed through
  3191. * to the routines called to process the data and is not looked at
  3192. * here.
  3193. */
  3194. STATIC int
  3195. xlog_do_recovery_pass(
  3196. xlog_t *log,
  3197. xfs_daddr_t head_blk,
  3198. xfs_daddr_t tail_blk,
  3199. int pass)
  3200. {
  3201. xlog_rec_header_t *rhead;
  3202. xfs_daddr_t blk_no;
  3203. xfs_caddr_t offset;
  3204. xfs_buf_t *hbp, *dbp;
  3205. int error = 0, h_size;
  3206. int bblks, split_bblks;
  3207. int hblks, split_hblks, wrapped_hblks;
  3208. xlog_recover_t *rhash[XLOG_RHASH_SIZE];
  3209. ASSERT(head_blk != tail_blk);
  3210. /*
  3211. * Read the header of the tail block and get the iclog buffer size from
  3212. * h_size. Use this to tell how many sectors make up the log header.
  3213. */
  3214. if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
  3215. /*
  3216. * When using variable length iclogs, read first sector of
  3217. * iclog header and extract the header size from it. Get a
  3218. * new hbp that is the correct size.
  3219. */
  3220. hbp = xlog_get_bp(log, 1);
  3221. if (!hbp)
  3222. return ENOMEM;
  3223. error = xlog_bread(log, tail_blk, 1, hbp, &offset);
  3224. if (error)
  3225. goto bread_err1;
  3226. rhead = (xlog_rec_header_t *)offset;
  3227. error = xlog_valid_rec_header(log, rhead, tail_blk);
  3228. if (error)
  3229. goto bread_err1;
  3230. h_size = be32_to_cpu(rhead->h_size);
  3231. if ((be32_to_cpu(rhead->h_version) & XLOG_VERSION_2) &&
  3232. (h_size > XLOG_HEADER_CYCLE_SIZE)) {
  3233. hblks = h_size / XLOG_HEADER_CYCLE_SIZE;
  3234. if (h_size % XLOG_HEADER_CYCLE_SIZE)
  3235. hblks++;
  3236. xlog_put_bp(hbp);
  3237. hbp = xlog_get_bp(log, hblks);
  3238. } else {
  3239. hblks = 1;
  3240. }
  3241. } else {
  3242. ASSERT(log->l_sectbb_log == 0);
  3243. hblks = 1;
  3244. hbp = xlog_get_bp(log, 1);
  3245. h_size = XLOG_BIG_RECORD_BSIZE;
  3246. }
  3247. if (!hbp)
  3248. return ENOMEM;
  3249. dbp = xlog_get_bp(log, BTOBB(h_size));
  3250. if (!dbp) {
  3251. xlog_put_bp(hbp);
  3252. return ENOMEM;
  3253. }
  3254. memset(rhash, 0, sizeof(rhash));
  3255. if (tail_blk <= head_blk) {
  3256. for (blk_no = tail_blk; blk_no < head_blk; ) {
  3257. error = xlog_bread(log, blk_no, hblks, hbp, &offset);
  3258. if (error)
  3259. goto bread_err2;
  3260. rhead = (xlog_rec_header_t *)offset;
  3261. error = xlog_valid_rec_header(log, rhead, blk_no);
  3262. if (error)
  3263. goto bread_err2;
  3264. /* blocks in data section */
  3265. bblks = (int)BTOBB(be32_to_cpu(rhead->h_len));
  3266. error = xlog_bread(log, blk_no + hblks, bblks, dbp,
  3267. &offset);
  3268. if (error)
  3269. goto bread_err2;
  3270. xlog_unpack_data(rhead, offset, log);
  3271. if ((error = xlog_recover_process_data(log,
  3272. rhash, rhead, offset, pass)))
  3273. goto bread_err2;
  3274. blk_no += bblks + hblks;
  3275. }
  3276. } else {
  3277. /*
  3278. * Perform recovery around the end of the physical log.
  3279. * When the head is not on the same cycle number as the tail,
  3280. * we can't do a sequential recovery as above.
  3281. */
  3282. blk_no = tail_blk;
  3283. while (blk_no < log->l_logBBsize) {
  3284. /*
  3285. * Check for header wrapping around physical end-of-log
  3286. */
  3287. offset = XFS_BUF_PTR(hbp);
  3288. split_hblks = 0;
  3289. wrapped_hblks = 0;
  3290. if (blk_no + hblks <= log->l_logBBsize) {
  3291. /* Read header in one read */
  3292. error = xlog_bread(log, blk_no, hblks, hbp,
  3293. &offset);
  3294. if (error)
  3295. goto bread_err2;
  3296. } else {
  3297. /* This LR is split across physical log end */
  3298. if (blk_no != log->l_logBBsize) {
  3299. /* some data before physical log end */
  3300. ASSERT(blk_no <= INT_MAX);
  3301. split_hblks = log->l_logBBsize - (int)blk_no;
  3302. ASSERT(split_hblks > 0);
  3303. error = xlog_bread(log, blk_no,
  3304. split_hblks, hbp,
  3305. &offset);
  3306. if (error)
  3307. goto bread_err2;
  3308. }
  3309. /*
  3310. * Note: this black magic still works with
  3311. * large sector sizes (non-512) only because:
  3312. * - we increased the buffer size originally
  3313. * by 1 sector giving us enough extra space
  3314. * for the second read;
  3315. * - the log start is guaranteed to be sector
  3316. * aligned;
  3317. * - we read the log end (LR header start)
  3318. * _first_, then the log start (LR header end)
  3319. * - order is important.
  3320. */
  3321. wrapped_hblks = hblks - split_hblks;
  3322. error = XFS_BUF_SET_PTR(hbp,
  3323. offset + BBTOB(split_hblks),
  3324. BBTOB(hblks - split_hblks));
  3325. if (error)
  3326. goto bread_err2;
  3327. error = xlog_bread_noalign(log, 0,
  3328. wrapped_hblks, hbp);
  3329. if (error)
  3330. goto bread_err2;
  3331. error = XFS_BUF_SET_PTR(hbp, offset,
  3332. BBTOB(hblks));
  3333. if (error)
  3334. goto bread_err2;
  3335. }
  3336. rhead = (xlog_rec_header_t *)offset;
  3337. error = xlog_valid_rec_header(log, rhead,
  3338. split_hblks ? blk_no : 0);
  3339. if (error)
  3340. goto bread_err2;
  3341. bblks = (int)BTOBB(be32_to_cpu(rhead->h_len));
  3342. blk_no += hblks;
  3343. /* Read in data for log record */
  3344. if (blk_no + bblks <= log->l_logBBsize) {
  3345. error = xlog_bread(log, blk_no, bblks, dbp,
  3346. &offset);
  3347. if (error)
  3348. goto bread_err2;
  3349. } else {
  3350. /* This log record is split across the
  3351. * physical end of log */
  3352. offset = XFS_BUF_PTR(dbp);
  3353. split_bblks = 0;
  3354. if (blk_no != log->l_logBBsize) {
  3355. /* some data is before the physical
  3356. * end of log */
  3357. ASSERT(!wrapped_hblks);
  3358. ASSERT(blk_no <= INT_MAX);
  3359. split_bblks =
  3360. log->l_logBBsize - (int)blk_no;
  3361. ASSERT(split_bblks > 0);
  3362. error = xlog_bread(log, blk_no,
  3363. split_bblks, dbp,
  3364. &offset);
  3365. if (error)
  3366. goto bread_err2;
  3367. }
  3368. /*
  3369. * Note: this black magic still works with
  3370. * large sector sizes (non-512) only because:
  3371. * - we increased the buffer size originally
  3372. * by 1 sector giving us enough extra space
  3373. * for the second read;
  3374. * - the log start is guaranteed to be sector
  3375. * aligned;
  3376. * - we read the log end (LR header start)
  3377. * _first_, then the log start (LR header end)
  3378. * - order is important.
  3379. */
  3380. error = XFS_BUF_SET_PTR(dbp,
  3381. offset + BBTOB(split_bblks),
  3382. BBTOB(bblks - split_bblks));
  3383. if (error)
  3384. goto bread_err2;
  3385. error = xlog_bread_noalign(log, wrapped_hblks,
  3386. bblks - split_bblks,
  3387. dbp);
  3388. if (error)
  3389. goto bread_err2;
  3390. error = XFS_BUF_SET_PTR(dbp, offset, h_size);
  3391. if (error)
  3392. goto bread_err2;
  3393. }
  3394. xlog_unpack_data(rhead, offset, log);
  3395. if ((error = xlog_recover_process_data(log, rhash,
  3396. rhead, offset, pass)))
  3397. goto bread_err2;
  3398. blk_no += bblks;
  3399. }
  3400. ASSERT(blk_no >= log->l_logBBsize);
  3401. blk_no -= log->l_logBBsize;
  3402. /* read first part of physical log */
  3403. while (blk_no < head_blk) {
  3404. error = xlog_bread(log, blk_no, hblks, hbp, &offset);
  3405. if (error)
  3406. goto bread_err2;
  3407. rhead = (xlog_rec_header_t *)offset;
  3408. error = xlog_valid_rec_header(log, rhead, blk_no);
  3409. if (error)
  3410. goto bread_err2;
  3411. bblks = (int)BTOBB(be32_to_cpu(rhead->h_len));
  3412. error = xlog_bread(log, blk_no+hblks, bblks, dbp,
  3413. &offset);
  3414. if (error)
  3415. goto bread_err2;
  3416. xlog_unpack_data(rhead, offset, log);
  3417. if ((error = xlog_recover_process_data(log, rhash,
  3418. rhead, offset, pass)))
  3419. goto bread_err2;
  3420. blk_no += bblks + hblks;
  3421. }
  3422. }
  3423. bread_err2:
  3424. xlog_put_bp(dbp);
  3425. bread_err1:
  3426. xlog_put_bp(hbp);
  3427. return error;
  3428. }
  3429. /*
  3430. * Do the recovery of the log. We actually do this in two phases.
  3431. * The two passes are necessary in order to implement the function
  3432. * of cancelling a record written into the log. The first pass
  3433. * determines those things which have been cancelled, and the
  3434. * second pass replays log items normally except for those which
  3435. * have been cancelled. The handling of the replay and cancellations
  3436. * takes place in the log item type specific routines.
  3437. *
  3438. * The table of items which have cancel records in the log is allocated
  3439. * and freed at this level, since only here do we know when all of
  3440. * the log recovery has been completed.
  3441. */
  3442. STATIC int
  3443. xlog_do_log_recovery(
  3444. xlog_t *log,
  3445. xfs_daddr_t head_blk,
  3446. xfs_daddr_t tail_blk)
  3447. {
  3448. int error;
  3449. ASSERT(head_blk != tail_blk);
  3450. /*
  3451. * First do a pass to find all of the cancelled buf log items.
  3452. * Store them in the buf_cancel_table for use in the second pass.
  3453. */
  3454. log->l_buf_cancel_table =
  3455. (xfs_buf_cancel_t **)kmem_zalloc(XLOG_BC_TABLE_SIZE *
  3456. sizeof(xfs_buf_cancel_t*),
  3457. KM_SLEEP);
  3458. error = xlog_do_recovery_pass(log, head_blk, tail_blk,
  3459. XLOG_RECOVER_PASS1);
  3460. if (error != 0) {
  3461. kmem_free(log->l_buf_cancel_table);
  3462. log->l_buf_cancel_table = NULL;
  3463. return error;
  3464. }
  3465. /*
  3466. * Then do a second pass to actually recover the items in the log.
  3467. * When it is complete free the table of buf cancel items.
  3468. */
  3469. error = xlog_do_recovery_pass(log, head_blk, tail_blk,
  3470. XLOG_RECOVER_PASS2);
  3471. #ifdef DEBUG
  3472. if (!error) {
  3473. int i;
  3474. for (i = 0; i < XLOG_BC_TABLE_SIZE; i++)
  3475. ASSERT(log->l_buf_cancel_table[i] == NULL);
  3476. }
  3477. #endif /* DEBUG */
  3478. kmem_free(log->l_buf_cancel_table);
  3479. log->l_buf_cancel_table = NULL;
  3480. return error;
  3481. }
  3482. /*
  3483. * Do the actual recovery
  3484. */
  3485. STATIC int
  3486. xlog_do_recover(
  3487. xlog_t *log,
  3488. xfs_daddr_t head_blk,
  3489. xfs_daddr_t tail_blk)
  3490. {
  3491. int error;
  3492. xfs_buf_t *bp;
  3493. xfs_sb_t *sbp;
  3494. /*
  3495. * First replay the images in the log.
  3496. */
  3497. error = xlog_do_log_recovery(log, head_blk, tail_blk);
  3498. if (error) {
  3499. return error;
  3500. }
  3501. XFS_bflush(log->l_mp->m_ddev_targp);
  3502. /*
  3503. * If IO errors happened during recovery, bail out.
  3504. */
  3505. if (XFS_FORCED_SHUTDOWN(log->l_mp)) {
  3506. return (EIO);
  3507. }
  3508. /*
  3509. * We now update the tail_lsn since much of the recovery has completed
  3510. * and there may be space available to use. If there were no extent
  3511. * or iunlinks, we can free up the entire log and set the tail_lsn to
  3512. * be the last_sync_lsn. This was set in xlog_find_tail to be the
  3513. * lsn of the last known good LR on disk. If there are extent frees
  3514. * or iunlinks they will have some entries in the AIL; so we look at
  3515. * the AIL to determine how to set the tail_lsn.
  3516. */
  3517. xlog_assign_tail_lsn(log->l_mp);
  3518. /*
  3519. * Now that we've finished replaying all buffer and inode
  3520. * updates, re-read in the superblock.
  3521. */
  3522. bp = xfs_getsb(log->l_mp, 0);
  3523. XFS_BUF_UNDONE(bp);
  3524. ASSERT(!(XFS_BUF_ISWRITE(bp)));
  3525. ASSERT(!(XFS_BUF_ISDELAYWRITE(bp)));
  3526. XFS_BUF_READ(bp);
  3527. XFS_BUF_UNASYNC(bp);
  3528. xfsbdstrat(log->l_mp, bp);
  3529. error = xfs_iowait(bp);
  3530. if (error) {
  3531. xfs_ioerror_alert("xlog_do_recover",
  3532. log->l_mp, bp, XFS_BUF_ADDR(bp));
  3533. ASSERT(0);
  3534. xfs_buf_relse(bp);
  3535. return error;
  3536. }
  3537. /* Convert superblock from on-disk format */
  3538. sbp = &log->l_mp->m_sb;
  3539. xfs_sb_from_disk(sbp, XFS_BUF_TO_SBP(bp));
  3540. ASSERT(sbp->sb_magicnum == XFS_SB_MAGIC);
  3541. ASSERT(xfs_sb_good_version(sbp));
  3542. xfs_buf_relse(bp);
  3543. /* We've re-read the superblock so re-initialize per-cpu counters */
  3544. xfs_icsb_reinit_counters(log->l_mp);
  3545. xlog_recover_check_summary(log);
  3546. /* Normal transactions can now occur */
  3547. log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
  3548. return 0;
  3549. }
  3550. /*
  3551. * Perform recovery and re-initialize some log variables in xlog_find_tail.
  3552. *
  3553. * Return error or zero.
  3554. */
  3555. int
  3556. xlog_recover(
  3557. xlog_t *log)
  3558. {
  3559. xfs_daddr_t head_blk, tail_blk;
  3560. int error;
  3561. /* find the tail of the log */
  3562. if ((error = xlog_find_tail(log, &head_blk, &tail_blk)))
  3563. return error;
  3564. if (tail_blk != head_blk) {
  3565. /* There used to be a comment here:
  3566. *
  3567. * disallow recovery on read-only mounts. note -- mount
  3568. * checks for ENOSPC and turns it into an intelligent
  3569. * error message.
  3570. * ...but this is no longer true. Now, unless you specify
  3571. * NORECOVERY (in which case this function would never be
  3572. * called), we just go ahead and recover. We do this all
  3573. * under the vfs layer, so we can get away with it unless
  3574. * the device itself is read-only, in which case we fail.
  3575. */
  3576. if ((error = xfs_dev_is_read_only(log->l_mp, "recovery"))) {
  3577. return error;
  3578. }
  3579. cmn_err(CE_NOTE,
  3580. "Starting XFS recovery on filesystem: %s (logdev: %s)",
  3581. log->l_mp->m_fsname, log->l_mp->m_logname ?
  3582. log->l_mp->m_logname : "internal");
  3583. error = xlog_do_recover(log, head_blk, tail_blk);
  3584. log->l_flags |= XLOG_RECOVERY_NEEDED;
  3585. }
  3586. return error;
  3587. }
  3588. /*
  3589. * In the first part of recovery we replay inodes and buffers and build
  3590. * up the list of extent free items which need to be processed. Here
  3591. * we process the extent free items and clean up the on disk unlinked
  3592. * inode lists. This is separated from the first part of recovery so
  3593. * that the root and real-time bitmap inodes can be read in from disk in
  3594. * between the two stages. This is necessary so that we can free space
  3595. * in the real-time portion of the file system.
  3596. */
  3597. int
  3598. xlog_recover_finish(
  3599. xlog_t *log)
  3600. {
  3601. /*
  3602. * Now we're ready to do the transactions needed for the
  3603. * rest of recovery. Start with completing all the extent
  3604. * free intent records and then process the unlinked inode
  3605. * lists. At this point, we essentially run in normal mode
  3606. * except that we're still performing recovery actions
  3607. * rather than accepting new requests.
  3608. */
  3609. if (log->l_flags & XLOG_RECOVERY_NEEDED) {
  3610. int error;
  3611. error = xlog_recover_process_efis(log);
  3612. if (error) {
  3613. cmn_err(CE_ALERT,
  3614. "Failed to recover EFIs on filesystem: %s",
  3615. log->l_mp->m_fsname);
  3616. return error;
  3617. }
  3618. /*
  3619. * Sync the log to get all the EFIs out of the AIL.
  3620. * This isn't absolutely necessary, but it helps in
  3621. * case the unlink transactions would have problems
  3622. * pushing the EFIs out of the way.
  3623. */
  3624. xfs_log_force(log->l_mp, (xfs_lsn_t)0,
  3625. (XFS_LOG_FORCE | XFS_LOG_SYNC));
  3626. xlog_recover_process_iunlinks(log);
  3627. xlog_recover_check_summary(log);
  3628. cmn_err(CE_NOTE,
  3629. "Ending XFS recovery on filesystem: %s (logdev: %s)",
  3630. log->l_mp->m_fsname, log->l_mp->m_logname ?
  3631. log->l_mp->m_logname : "internal");
  3632. log->l_flags &= ~XLOG_RECOVERY_NEEDED;
  3633. } else {
  3634. cmn_err(CE_DEBUG,
  3635. "!Ending clean XFS mount for filesystem: %s\n",
  3636. log->l_mp->m_fsname);
  3637. }
  3638. return 0;
  3639. }
  3640. #if defined(DEBUG)
  3641. /*
  3642. * Read all of the agf and agi counters and check that they
  3643. * are consistent with the superblock counters.
  3644. */
  3645. void
  3646. xlog_recover_check_summary(
  3647. xlog_t *log)
  3648. {
  3649. xfs_mount_t *mp;
  3650. xfs_agf_t *agfp;
  3651. xfs_buf_t *agfbp;
  3652. xfs_buf_t *agibp;
  3653. xfs_buf_t *sbbp;
  3654. #ifdef XFS_LOUD_RECOVERY
  3655. xfs_sb_t *sbp;
  3656. #endif
  3657. xfs_agnumber_t agno;
  3658. __uint64_t freeblks;
  3659. __uint64_t itotal;
  3660. __uint64_t ifree;
  3661. int error;
  3662. mp = log->l_mp;
  3663. freeblks = 0LL;
  3664. itotal = 0LL;
  3665. ifree = 0LL;
  3666. for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
  3667. error = xfs_read_agf(mp, NULL, agno, 0, &agfbp);
  3668. if (error) {
  3669. xfs_fs_cmn_err(CE_ALERT, mp,
  3670. "xlog_recover_check_summary(agf)"
  3671. "agf read failed agno %d error %d",
  3672. agno, error);
  3673. } else {
  3674. agfp = XFS_BUF_TO_AGF(agfbp);
  3675. freeblks += be32_to_cpu(agfp->agf_freeblks) +
  3676. be32_to_cpu(agfp->agf_flcount);
  3677. xfs_buf_relse(agfbp);
  3678. }
  3679. error = xfs_read_agi(mp, NULL, agno, &agibp);
  3680. if (!error) {
  3681. struct xfs_agi *agi = XFS_BUF_TO_AGI(agibp);
  3682. itotal += be32_to_cpu(agi->agi_count);
  3683. ifree += be32_to_cpu(agi->agi_freecount);
  3684. xfs_buf_relse(agibp);
  3685. }
  3686. }
  3687. sbbp = xfs_getsb(mp, 0);
  3688. #ifdef XFS_LOUD_RECOVERY
  3689. sbp = &mp->m_sb;
  3690. xfs_sb_from_disk(sbp, XFS_BUF_TO_SBP(sbbp));
  3691. cmn_err(CE_NOTE,
  3692. "xlog_recover_check_summary: sb_icount %Lu itotal %Lu",
  3693. sbp->sb_icount, itotal);
  3694. cmn_err(CE_NOTE,
  3695. "xlog_recover_check_summary: sb_ifree %Lu itotal %Lu",
  3696. sbp->sb_ifree, ifree);
  3697. cmn_err(CE_NOTE,
  3698. "xlog_recover_check_summary: sb_fdblocks %Lu freeblks %Lu",
  3699. sbp->sb_fdblocks, freeblks);
  3700. #if 0
  3701. /*
  3702. * This is turned off until I account for the allocation
  3703. * btree blocks which live in free space.
  3704. */
  3705. ASSERT(sbp->sb_icount == itotal);
  3706. ASSERT(sbp->sb_ifree == ifree);
  3707. ASSERT(sbp->sb_fdblocks == freeblks);
  3708. #endif
  3709. #endif
  3710. xfs_buf_relse(sbbp);
  3711. }
  3712. #endif /* DEBUG */