xfs_log_recover.c 128 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651
  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_shared.h"
  21. #include "xfs_format.h"
  22. #include "xfs_log_format.h"
  23. #include "xfs_trans_resv.h"
  24. #include "xfs_bit.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_mount.h"
  27. #include "xfs_da_format.h"
  28. #include "xfs_da_btree.h"
  29. #include "xfs_inode.h"
  30. #include "xfs_trans.h"
  31. #include "xfs_log.h"
  32. #include "xfs_log_priv.h"
  33. #include "xfs_log_recover.h"
  34. #include "xfs_inode_item.h"
  35. #include "xfs_extfree_item.h"
  36. #include "xfs_trans_priv.h"
  37. #include "xfs_alloc.h"
  38. #include "xfs_ialloc.h"
  39. #include "xfs_quota.h"
  40. #include "xfs_cksum.h"
  41. #include "xfs_trace.h"
  42. #include "xfs_icache.h"
  43. #include "xfs_bmap_btree.h"
  44. #include "xfs_error.h"
  45. #include "xfs_dir2.h"
  46. #define BLK_AVG(blk1, blk2) ((blk1+blk2) >> 1)
  47. STATIC int
  48. xlog_find_zeroed(
  49. struct xlog *,
  50. xfs_daddr_t *);
  51. STATIC int
  52. xlog_clear_stale_blocks(
  53. struct xlog *,
  54. xfs_lsn_t);
  55. #if defined(DEBUG)
  56. STATIC void
  57. xlog_recover_check_summary(
  58. struct xlog *);
  59. #else
  60. #define xlog_recover_check_summary(log)
  61. #endif
  62. /*
  63. * This structure is used during recovery to record the buf log items which
  64. * have been canceled and should not be replayed.
  65. */
  66. struct xfs_buf_cancel {
  67. xfs_daddr_t bc_blkno;
  68. uint bc_len;
  69. int bc_refcount;
  70. struct list_head bc_list;
  71. };
  72. /*
  73. * Sector aligned buffer routines for buffer create/read/write/access
  74. */
  75. /*
  76. * Verify the given count of basic blocks is valid number of blocks
  77. * to specify for an operation involving the given XFS log buffer.
  78. * Returns nonzero if the count is valid, 0 otherwise.
  79. */
  80. static inline int
  81. xlog_buf_bbcount_valid(
  82. struct xlog *log,
  83. int bbcount)
  84. {
  85. return bbcount > 0 && bbcount <= log->l_logBBsize;
  86. }
  87. /*
  88. * Allocate a buffer to hold log data. The buffer needs to be able
  89. * to map to a range of nbblks basic blocks at any valid (basic
  90. * block) offset within the log.
  91. */
  92. STATIC xfs_buf_t *
  93. xlog_get_bp(
  94. struct xlog *log,
  95. int nbblks)
  96. {
  97. struct xfs_buf *bp;
  98. if (!xlog_buf_bbcount_valid(log, nbblks)) {
  99. xfs_warn(log->l_mp, "Invalid block length (0x%x) for buffer",
  100. nbblks);
  101. XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_HIGH, log->l_mp);
  102. return NULL;
  103. }
  104. /*
  105. * We do log I/O in units of log sectors (a power-of-2
  106. * multiple of the basic block size), so we round up the
  107. * requested size to accommodate the basic blocks required
  108. * for complete log sectors.
  109. *
  110. * In addition, the buffer may be used for a non-sector-
  111. * aligned block offset, in which case an I/O of the
  112. * requested size could extend beyond the end of the
  113. * buffer. If the requested size is only 1 basic block it
  114. * will never straddle a sector boundary, so this won't be
  115. * an issue. Nor will this be a problem if the log I/O is
  116. * done in basic blocks (sector size 1). But otherwise we
  117. * extend the buffer by one extra log sector to ensure
  118. * there's space to accommodate this possibility.
  119. */
  120. if (nbblks > 1 && log->l_sectBBsize > 1)
  121. nbblks += log->l_sectBBsize;
  122. nbblks = round_up(nbblks, log->l_sectBBsize);
  123. bp = xfs_buf_get_uncached(log->l_mp->m_logdev_targp, nbblks, 0);
  124. if (bp)
  125. xfs_buf_unlock(bp);
  126. return bp;
  127. }
  128. STATIC void
  129. xlog_put_bp(
  130. xfs_buf_t *bp)
  131. {
  132. xfs_buf_free(bp);
  133. }
  134. /*
  135. * Return the address of the start of the given block number's data
  136. * in a log buffer. The buffer covers a log sector-aligned region.
  137. */
  138. STATIC xfs_caddr_t
  139. xlog_align(
  140. struct xlog *log,
  141. xfs_daddr_t blk_no,
  142. int nbblks,
  143. struct xfs_buf *bp)
  144. {
  145. xfs_daddr_t offset = blk_no & ((xfs_daddr_t)log->l_sectBBsize - 1);
  146. ASSERT(offset + nbblks <= bp->b_length);
  147. return bp->b_addr + BBTOB(offset);
  148. }
  149. /*
  150. * nbblks should be uint, but oh well. Just want to catch that 32-bit length.
  151. */
  152. STATIC int
  153. xlog_bread_noalign(
  154. struct xlog *log,
  155. xfs_daddr_t blk_no,
  156. int nbblks,
  157. struct xfs_buf *bp)
  158. {
  159. int error;
  160. if (!xlog_buf_bbcount_valid(log, nbblks)) {
  161. xfs_warn(log->l_mp, "Invalid block length (0x%x) for buffer",
  162. nbblks);
  163. XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_HIGH, log->l_mp);
  164. return -EFSCORRUPTED;
  165. }
  166. blk_no = round_down(blk_no, log->l_sectBBsize);
  167. nbblks = round_up(nbblks, log->l_sectBBsize);
  168. ASSERT(nbblks > 0);
  169. ASSERT(nbblks <= bp->b_length);
  170. XFS_BUF_SET_ADDR(bp, log->l_logBBstart + blk_no);
  171. XFS_BUF_READ(bp);
  172. bp->b_io_length = nbblks;
  173. bp->b_error = 0;
  174. error = xfs_buf_submit_wait(bp);
  175. if (error && !XFS_FORCED_SHUTDOWN(log->l_mp))
  176. xfs_buf_ioerror_alert(bp, __func__);
  177. return error;
  178. }
  179. STATIC int
  180. xlog_bread(
  181. struct xlog *log,
  182. xfs_daddr_t blk_no,
  183. int nbblks,
  184. struct xfs_buf *bp,
  185. xfs_caddr_t *offset)
  186. {
  187. int error;
  188. error = xlog_bread_noalign(log, blk_no, nbblks, bp);
  189. if (error)
  190. return error;
  191. *offset = xlog_align(log, blk_no, nbblks, bp);
  192. return 0;
  193. }
  194. /*
  195. * Read at an offset into the buffer. Returns with the buffer in it's original
  196. * state regardless of the result of the read.
  197. */
  198. STATIC int
  199. xlog_bread_offset(
  200. struct xlog *log,
  201. xfs_daddr_t blk_no, /* block to read from */
  202. int nbblks, /* blocks to read */
  203. struct xfs_buf *bp,
  204. xfs_caddr_t offset)
  205. {
  206. xfs_caddr_t orig_offset = bp->b_addr;
  207. int orig_len = BBTOB(bp->b_length);
  208. int error, error2;
  209. error = xfs_buf_associate_memory(bp, offset, BBTOB(nbblks));
  210. if (error)
  211. return error;
  212. error = xlog_bread_noalign(log, blk_no, nbblks, bp);
  213. /* must reset buffer pointer even on error */
  214. error2 = xfs_buf_associate_memory(bp, orig_offset, orig_len);
  215. if (error)
  216. return error;
  217. return error2;
  218. }
  219. /*
  220. * Write out the buffer at the given block for the given number of blocks.
  221. * The buffer is kept locked across the write and is returned locked.
  222. * This can only be used for synchronous log writes.
  223. */
  224. STATIC int
  225. xlog_bwrite(
  226. struct xlog *log,
  227. xfs_daddr_t blk_no,
  228. int nbblks,
  229. struct xfs_buf *bp)
  230. {
  231. int error;
  232. if (!xlog_buf_bbcount_valid(log, nbblks)) {
  233. xfs_warn(log->l_mp, "Invalid block length (0x%x) for buffer",
  234. nbblks);
  235. XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_HIGH, log->l_mp);
  236. return -EFSCORRUPTED;
  237. }
  238. blk_no = round_down(blk_no, log->l_sectBBsize);
  239. nbblks = round_up(nbblks, log->l_sectBBsize);
  240. ASSERT(nbblks > 0);
  241. ASSERT(nbblks <= bp->b_length);
  242. XFS_BUF_SET_ADDR(bp, log->l_logBBstart + blk_no);
  243. XFS_BUF_ZEROFLAGS(bp);
  244. xfs_buf_hold(bp);
  245. xfs_buf_lock(bp);
  246. bp->b_io_length = nbblks;
  247. bp->b_error = 0;
  248. error = xfs_bwrite(bp);
  249. if (error)
  250. xfs_buf_ioerror_alert(bp, __func__);
  251. xfs_buf_relse(bp);
  252. return error;
  253. }
  254. #ifdef DEBUG
  255. /*
  256. * dump debug superblock and log record information
  257. */
  258. STATIC void
  259. xlog_header_check_dump(
  260. xfs_mount_t *mp,
  261. xlog_rec_header_t *head)
  262. {
  263. xfs_debug(mp, "%s: SB : uuid = %pU, fmt = %d",
  264. __func__, &mp->m_sb.sb_uuid, XLOG_FMT);
  265. xfs_debug(mp, " log : uuid = %pU, fmt = %d",
  266. &head->h_fs_uuid, be32_to_cpu(head->h_fmt));
  267. }
  268. #else
  269. #define xlog_header_check_dump(mp, head)
  270. #endif
  271. /*
  272. * check log record header for recovery
  273. */
  274. STATIC int
  275. xlog_header_check_recover(
  276. xfs_mount_t *mp,
  277. xlog_rec_header_t *head)
  278. {
  279. ASSERT(head->h_magicno == cpu_to_be32(XLOG_HEADER_MAGIC_NUM));
  280. /*
  281. * IRIX doesn't write the h_fmt field and leaves it zeroed
  282. * (XLOG_FMT_UNKNOWN). This stops us from trying to recover
  283. * a dirty log created in IRIX.
  284. */
  285. if (unlikely(head->h_fmt != cpu_to_be32(XLOG_FMT))) {
  286. xfs_warn(mp,
  287. "dirty log written in incompatible format - can't recover");
  288. xlog_header_check_dump(mp, head);
  289. XFS_ERROR_REPORT("xlog_header_check_recover(1)",
  290. XFS_ERRLEVEL_HIGH, mp);
  291. return -EFSCORRUPTED;
  292. } else if (unlikely(!uuid_equal(&mp->m_sb.sb_uuid, &head->h_fs_uuid))) {
  293. xfs_warn(mp,
  294. "dirty log entry has mismatched uuid - can't recover");
  295. xlog_header_check_dump(mp, head);
  296. XFS_ERROR_REPORT("xlog_header_check_recover(2)",
  297. XFS_ERRLEVEL_HIGH, mp);
  298. return -EFSCORRUPTED;
  299. }
  300. return 0;
  301. }
  302. /*
  303. * read the head block of the log and check the header
  304. */
  305. STATIC int
  306. xlog_header_check_mount(
  307. xfs_mount_t *mp,
  308. xlog_rec_header_t *head)
  309. {
  310. ASSERT(head->h_magicno == cpu_to_be32(XLOG_HEADER_MAGIC_NUM));
  311. if (uuid_is_nil(&head->h_fs_uuid)) {
  312. /*
  313. * IRIX doesn't write the h_fs_uuid or h_fmt fields. If
  314. * h_fs_uuid is nil, we assume this log was last mounted
  315. * by IRIX and continue.
  316. */
  317. xfs_warn(mp, "nil uuid in log - IRIX style log");
  318. } else if (unlikely(!uuid_equal(&mp->m_sb.sb_uuid, &head->h_fs_uuid))) {
  319. xfs_warn(mp, "log has mismatched uuid - can't recover");
  320. xlog_header_check_dump(mp, head);
  321. XFS_ERROR_REPORT("xlog_header_check_mount",
  322. XFS_ERRLEVEL_HIGH, mp);
  323. return -EFSCORRUPTED;
  324. }
  325. return 0;
  326. }
  327. STATIC void
  328. xlog_recover_iodone(
  329. struct xfs_buf *bp)
  330. {
  331. if (bp->b_error) {
  332. /*
  333. * We're not going to bother about retrying
  334. * this during recovery. One strike!
  335. */
  336. if (!XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) {
  337. xfs_buf_ioerror_alert(bp, __func__);
  338. xfs_force_shutdown(bp->b_target->bt_mount,
  339. SHUTDOWN_META_IO_ERROR);
  340. }
  341. }
  342. bp->b_iodone = NULL;
  343. xfs_buf_ioend(bp);
  344. }
  345. /*
  346. * This routine finds (to an approximation) the first block in the physical
  347. * log which contains the given cycle. It uses a binary search algorithm.
  348. * Note that the algorithm can not be perfect because the disk will not
  349. * necessarily be perfect.
  350. */
  351. STATIC int
  352. xlog_find_cycle_start(
  353. struct xlog *log,
  354. struct xfs_buf *bp,
  355. xfs_daddr_t first_blk,
  356. xfs_daddr_t *last_blk,
  357. uint cycle)
  358. {
  359. xfs_caddr_t offset;
  360. xfs_daddr_t mid_blk;
  361. xfs_daddr_t end_blk;
  362. uint mid_cycle;
  363. int error;
  364. end_blk = *last_blk;
  365. mid_blk = BLK_AVG(first_blk, end_blk);
  366. while (mid_blk != first_blk && mid_blk != end_blk) {
  367. error = xlog_bread(log, mid_blk, 1, bp, &offset);
  368. if (error)
  369. return error;
  370. mid_cycle = xlog_get_cycle(offset);
  371. if (mid_cycle == cycle)
  372. end_blk = mid_blk; /* last_half_cycle == mid_cycle */
  373. else
  374. first_blk = mid_blk; /* first_half_cycle == mid_cycle */
  375. mid_blk = BLK_AVG(first_blk, end_blk);
  376. }
  377. ASSERT((mid_blk == first_blk && mid_blk+1 == end_blk) ||
  378. (mid_blk == end_blk && mid_blk-1 == first_blk));
  379. *last_blk = end_blk;
  380. return 0;
  381. }
  382. /*
  383. * Check that a range of blocks does not contain stop_on_cycle_no.
  384. * Fill in *new_blk with the block offset where such a block is
  385. * found, or with -1 (an invalid block number) if there is no such
  386. * block in the range. The scan needs to occur from front to back
  387. * and the pointer into the region must be updated since a later
  388. * routine will need to perform another test.
  389. */
  390. STATIC int
  391. xlog_find_verify_cycle(
  392. struct xlog *log,
  393. xfs_daddr_t start_blk,
  394. int nbblks,
  395. uint stop_on_cycle_no,
  396. xfs_daddr_t *new_blk)
  397. {
  398. xfs_daddr_t i, j;
  399. uint cycle;
  400. xfs_buf_t *bp;
  401. xfs_daddr_t bufblks;
  402. xfs_caddr_t buf = NULL;
  403. int error = 0;
  404. /*
  405. * Greedily allocate a buffer big enough to handle the full
  406. * range of basic blocks we'll be examining. If that fails,
  407. * try a smaller size. We need to be able to read at least
  408. * a log sector, or we're out of luck.
  409. */
  410. bufblks = 1 << ffs(nbblks);
  411. while (bufblks > log->l_logBBsize)
  412. bufblks >>= 1;
  413. while (!(bp = xlog_get_bp(log, bufblks))) {
  414. bufblks >>= 1;
  415. if (bufblks < log->l_sectBBsize)
  416. return -ENOMEM;
  417. }
  418. for (i = start_blk; i < start_blk + nbblks; i += bufblks) {
  419. int bcount;
  420. bcount = min(bufblks, (start_blk + nbblks - i));
  421. error = xlog_bread(log, i, bcount, bp, &buf);
  422. if (error)
  423. goto out;
  424. for (j = 0; j < bcount; j++) {
  425. cycle = xlog_get_cycle(buf);
  426. if (cycle == stop_on_cycle_no) {
  427. *new_blk = i+j;
  428. goto out;
  429. }
  430. buf += BBSIZE;
  431. }
  432. }
  433. *new_blk = -1;
  434. out:
  435. xlog_put_bp(bp);
  436. return error;
  437. }
  438. /*
  439. * Potentially backup over partial log record write.
  440. *
  441. * In the typical case, last_blk is the number of the block directly after
  442. * a good log record. Therefore, we subtract one to get the block number
  443. * of the last block in the given buffer. extra_bblks contains the number
  444. * of blocks we would have read on a previous read. This happens when the
  445. * last log record is split over the end of the physical log.
  446. *
  447. * extra_bblks is the number of blocks potentially verified on a previous
  448. * call to this routine.
  449. */
  450. STATIC int
  451. xlog_find_verify_log_record(
  452. struct xlog *log,
  453. xfs_daddr_t start_blk,
  454. xfs_daddr_t *last_blk,
  455. int extra_bblks)
  456. {
  457. xfs_daddr_t i;
  458. xfs_buf_t *bp;
  459. xfs_caddr_t offset = NULL;
  460. xlog_rec_header_t *head = NULL;
  461. int error = 0;
  462. int smallmem = 0;
  463. int num_blks = *last_blk - start_blk;
  464. int xhdrs;
  465. ASSERT(start_blk != 0 || *last_blk != start_blk);
  466. if (!(bp = xlog_get_bp(log, num_blks))) {
  467. if (!(bp = xlog_get_bp(log, 1)))
  468. return -ENOMEM;
  469. smallmem = 1;
  470. } else {
  471. error = xlog_bread(log, start_blk, num_blks, bp, &offset);
  472. if (error)
  473. goto out;
  474. offset += ((num_blks - 1) << BBSHIFT);
  475. }
  476. for (i = (*last_blk) - 1; i >= 0; i--) {
  477. if (i < start_blk) {
  478. /* valid log record not found */
  479. xfs_warn(log->l_mp,
  480. "Log inconsistent (didn't find previous header)");
  481. ASSERT(0);
  482. error = -EIO;
  483. goto out;
  484. }
  485. if (smallmem) {
  486. error = xlog_bread(log, i, 1, bp, &offset);
  487. if (error)
  488. goto out;
  489. }
  490. head = (xlog_rec_header_t *)offset;
  491. if (head->h_magicno == cpu_to_be32(XLOG_HEADER_MAGIC_NUM))
  492. break;
  493. if (!smallmem)
  494. offset -= BBSIZE;
  495. }
  496. /*
  497. * We hit the beginning of the physical log & still no header. Return
  498. * to caller. If caller can handle a return of -1, then this routine
  499. * will be called again for the end of the physical log.
  500. */
  501. if (i == -1) {
  502. error = 1;
  503. goto out;
  504. }
  505. /*
  506. * We have the final block of the good log (the first block
  507. * of the log record _before_ the head. So we check the uuid.
  508. */
  509. if ((error = xlog_header_check_mount(log->l_mp, head)))
  510. goto out;
  511. /*
  512. * We may have found a log record header before we expected one.
  513. * last_blk will be the 1st block # with a given cycle #. We may end
  514. * up reading an entire log record. In this case, we don't want to
  515. * reset last_blk. Only when last_blk points in the middle of a log
  516. * record do we update last_blk.
  517. */
  518. if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
  519. uint h_size = be32_to_cpu(head->h_size);
  520. xhdrs = h_size / XLOG_HEADER_CYCLE_SIZE;
  521. if (h_size % XLOG_HEADER_CYCLE_SIZE)
  522. xhdrs++;
  523. } else {
  524. xhdrs = 1;
  525. }
  526. if (*last_blk - i + extra_bblks !=
  527. BTOBB(be32_to_cpu(head->h_len)) + xhdrs)
  528. *last_blk = i;
  529. out:
  530. xlog_put_bp(bp);
  531. return error;
  532. }
  533. /*
  534. * Head is defined to be the point of the log where the next log write
  535. * could go. This means that incomplete LR writes at the end are
  536. * eliminated when calculating the head. We aren't guaranteed that previous
  537. * LR have complete transactions. We only know that a cycle number of
  538. * current cycle number -1 won't be present in the log if we start writing
  539. * from our current block number.
  540. *
  541. * last_blk contains the block number of the first block with a given
  542. * cycle number.
  543. *
  544. * Return: zero if normal, non-zero if error.
  545. */
  546. STATIC int
  547. xlog_find_head(
  548. struct xlog *log,
  549. xfs_daddr_t *return_head_blk)
  550. {
  551. xfs_buf_t *bp;
  552. xfs_caddr_t offset;
  553. xfs_daddr_t new_blk, first_blk, start_blk, last_blk, head_blk;
  554. int num_scan_bblks;
  555. uint first_half_cycle, last_half_cycle;
  556. uint stop_on_cycle;
  557. int error, log_bbnum = log->l_logBBsize;
  558. /* Is the end of the log device zeroed? */
  559. error = xlog_find_zeroed(log, &first_blk);
  560. if (error < 0) {
  561. xfs_warn(log->l_mp, "empty log check failed");
  562. return error;
  563. }
  564. if (error == 1) {
  565. *return_head_blk = first_blk;
  566. /* Is the whole lot zeroed? */
  567. if (!first_blk) {
  568. /* Linux XFS shouldn't generate totally zeroed logs -
  569. * mkfs etc write a dummy unmount record to a fresh
  570. * log so we can store the uuid in there
  571. */
  572. xfs_warn(log->l_mp, "totally zeroed log");
  573. }
  574. return 0;
  575. }
  576. first_blk = 0; /* get cycle # of 1st block */
  577. bp = xlog_get_bp(log, 1);
  578. if (!bp)
  579. return -ENOMEM;
  580. error = xlog_bread(log, 0, 1, bp, &offset);
  581. if (error)
  582. goto bp_err;
  583. first_half_cycle = xlog_get_cycle(offset);
  584. last_blk = head_blk = log_bbnum - 1; /* get cycle # of last block */
  585. error = xlog_bread(log, last_blk, 1, bp, &offset);
  586. if (error)
  587. goto bp_err;
  588. last_half_cycle = xlog_get_cycle(offset);
  589. ASSERT(last_half_cycle != 0);
  590. /*
  591. * If the 1st half cycle number is equal to the last half cycle number,
  592. * then the entire log is stamped with the same cycle number. In this
  593. * case, head_blk can't be set to zero (which makes sense). The below
  594. * math doesn't work out properly with head_blk equal to zero. Instead,
  595. * we set it to log_bbnum which is an invalid block number, but this
  596. * value makes the math correct. If head_blk doesn't changed through
  597. * all the tests below, *head_blk is set to zero at the very end rather
  598. * than log_bbnum. In a sense, log_bbnum and zero are the same block
  599. * in a circular file.
  600. */
  601. if (first_half_cycle == last_half_cycle) {
  602. /*
  603. * In this case we believe that the entire log should have
  604. * cycle number last_half_cycle. We need to scan backwards
  605. * from the end verifying that there are no holes still
  606. * containing last_half_cycle - 1. If we find such a hole,
  607. * then the start of that hole will be the new head. The
  608. * simple case looks like
  609. * x | x ... | x - 1 | x
  610. * Another case that fits this picture would be
  611. * x | x + 1 | x ... | x
  612. * In this case the head really is somewhere at the end of the
  613. * log, as one of the latest writes at the beginning was
  614. * incomplete.
  615. * One more case is
  616. * x | x + 1 | x ... | x - 1 | x
  617. * This is really the combination of the above two cases, and
  618. * the head has to end up at the start of the x-1 hole at the
  619. * end of the log.
  620. *
  621. * In the 256k log case, we will read from the beginning to the
  622. * end of the log and search for cycle numbers equal to x-1.
  623. * We don't worry about the x+1 blocks that we encounter,
  624. * because we know that they cannot be the head since the log
  625. * started with x.
  626. */
  627. head_blk = log_bbnum;
  628. stop_on_cycle = last_half_cycle - 1;
  629. } else {
  630. /*
  631. * In this case we want to find the first block with cycle
  632. * number matching last_half_cycle. We expect the log to be
  633. * some variation on
  634. * x + 1 ... | x ... | x
  635. * The first block with cycle number x (last_half_cycle) will
  636. * be where the new head belongs. First we do a binary search
  637. * for the first occurrence of last_half_cycle. The binary
  638. * search may not be totally accurate, so then we scan back
  639. * from there looking for occurrences of last_half_cycle before
  640. * us. If that backwards scan wraps around the beginning of
  641. * the log, then we look for occurrences of last_half_cycle - 1
  642. * at the end of the log. The cases we're looking for look
  643. * like
  644. * v binary search stopped here
  645. * x + 1 ... | x | x + 1 | x ... | x
  646. * ^ but we want to locate this spot
  647. * or
  648. * <---------> less than scan distance
  649. * x + 1 ... | x ... | x - 1 | x
  650. * ^ we want to locate this spot
  651. */
  652. stop_on_cycle = last_half_cycle;
  653. if ((error = xlog_find_cycle_start(log, bp, first_blk,
  654. &head_blk, last_half_cycle)))
  655. goto bp_err;
  656. }
  657. /*
  658. * Now validate the answer. Scan back some number of maximum possible
  659. * blocks and make sure each one has the expected cycle number. The
  660. * maximum is determined by the total possible amount of buffering
  661. * in the in-core log. The following number can be made tighter if
  662. * we actually look at the block size of the filesystem.
  663. */
  664. num_scan_bblks = XLOG_TOTAL_REC_SHIFT(log);
  665. if (head_blk >= num_scan_bblks) {
  666. /*
  667. * We are guaranteed that the entire check can be performed
  668. * in one buffer.
  669. */
  670. start_blk = head_blk - num_scan_bblks;
  671. if ((error = xlog_find_verify_cycle(log,
  672. start_blk, num_scan_bblks,
  673. stop_on_cycle, &new_blk)))
  674. goto bp_err;
  675. if (new_blk != -1)
  676. head_blk = new_blk;
  677. } else { /* need to read 2 parts of log */
  678. /*
  679. * We are going to scan backwards in the log in two parts.
  680. * First we scan the physical end of the log. In this part
  681. * of the log, we are looking for blocks with cycle number
  682. * last_half_cycle - 1.
  683. * If we find one, then we know that the log starts there, as
  684. * we've found a hole that didn't get written in going around
  685. * the end of the physical log. The simple case for this is
  686. * x + 1 ... | x ... | x - 1 | x
  687. * <---------> less than scan distance
  688. * If all of the blocks at the end of the log have cycle number
  689. * last_half_cycle, then we check the blocks at the start of
  690. * the log looking for occurrences of last_half_cycle. If we
  691. * find one, then our current estimate for the location of the
  692. * first occurrence of last_half_cycle is wrong and we move
  693. * back to the hole we've found. This case looks like
  694. * x + 1 ... | x | x + 1 | x ...
  695. * ^ binary search stopped here
  696. * Another case we need to handle that only occurs in 256k
  697. * logs is
  698. * x + 1 ... | x ... | x+1 | x ...
  699. * ^ binary search stops here
  700. * In a 256k log, the scan at the end of the log will see the
  701. * x + 1 blocks. We need to skip past those since that is
  702. * certainly not the head of the log. By searching for
  703. * last_half_cycle-1 we accomplish that.
  704. */
  705. ASSERT(head_blk <= INT_MAX &&
  706. (xfs_daddr_t) num_scan_bblks >= head_blk);
  707. start_blk = log_bbnum - (num_scan_bblks - head_blk);
  708. if ((error = xlog_find_verify_cycle(log, start_blk,
  709. num_scan_bblks - (int)head_blk,
  710. (stop_on_cycle - 1), &new_blk)))
  711. goto bp_err;
  712. if (new_blk != -1) {
  713. head_blk = new_blk;
  714. goto validate_head;
  715. }
  716. /*
  717. * Scan beginning of log now. The last part of the physical
  718. * log is good. This scan needs to verify that it doesn't find
  719. * the last_half_cycle.
  720. */
  721. start_blk = 0;
  722. ASSERT(head_blk <= INT_MAX);
  723. if ((error = xlog_find_verify_cycle(log,
  724. start_blk, (int)head_blk,
  725. stop_on_cycle, &new_blk)))
  726. goto bp_err;
  727. if (new_blk != -1)
  728. head_blk = new_blk;
  729. }
  730. validate_head:
  731. /*
  732. * Now we need to make sure head_blk is not pointing to a block in
  733. * the middle of a log record.
  734. */
  735. num_scan_bblks = XLOG_REC_SHIFT(log);
  736. if (head_blk >= num_scan_bblks) {
  737. start_blk = head_blk - num_scan_bblks; /* don't read head_blk */
  738. /* start ptr at last block ptr before head_blk */
  739. error = xlog_find_verify_log_record(log, start_blk, &head_blk, 0);
  740. if (error == 1)
  741. error = -EIO;
  742. if (error)
  743. goto bp_err;
  744. } else {
  745. start_blk = 0;
  746. ASSERT(head_blk <= INT_MAX);
  747. error = xlog_find_verify_log_record(log, start_blk, &head_blk, 0);
  748. if (error < 0)
  749. goto bp_err;
  750. if (error == 1) {
  751. /* We hit the beginning of the log during our search */
  752. start_blk = log_bbnum - (num_scan_bblks - head_blk);
  753. new_blk = log_bbnum;
  754. ASSERT(start_blk <= INT_MAX &&
  755. (xfs_daddr_t) log_bbnum-start_blk >= 0);
  756. ASSERT(head_blk <= INT_MAX);
  757. error = xlog_find_verify_log_record(log, start_blk,
  758. &new_blk, (int)head_blk);
  759. if (error == 1)
  760. error = -EIO;
  761. if (error)
  762. goto bp_err;
  763. if (new_blk != log_bbnum)
  764. head_blk = new_blk;
  765. } else if (error)
  766. goto bp_err;
  767. }
  768. xlog_put_bp(bp);
  769. if (head_blk == log_bbnum)
  770. *return_head_blk = 0;
  771. else
  772. *return_head_blk = head_blk;
  773. /*
  774. * When returning here, we have a good block number. Bad block
  775. * means that during a previous crash, we didn't have a clean break
  776. * from cycle number N to cycle number N-1. In this case, we need
  777. * to find the first block with cycle number N-1.
  778. */
  779. return 0;
  780. bp_err:
  781. xlog_put_bp(bp);
  782. if (error)
  783. xfs_warn(log->l_mp, "failed to find log head");
  784. return error;
  785. }
  786. /*
  787. * Find the sync block number or the tail of the log.
  788. *
  789. * This will be the block number of the last record to have its
  790. * associated buffers synced to disk. Every log record header has
  791. * a sync lsn embedded in it. LSNs hold block numbers, so it is easy
  792. * to get a sync block number. The only concern is to figure out which
  793. * log record header to believe.
  794. *
  795. * The following algorithm uses the log record header with the largest
  796. * lsn. The entire log record does not need to be valid. We only care
  797. * that the header is valid.
  798. *
  799. * We could speed up search by using current head_blk buffer, but it is not
  800. * available.
  801. */
  802. STATIC int
  803. xlog_find_tail(
  804. struct xlog *log,
  805. xfs_daddr_t *head_blk,
  806. xfs_daddr_t *tail_blk)
  807. {
  808. xlog_rec_header_t *rhead;
  809. xlog_op_header_t *op_head;
  810. xfs_caddr_t offset = NULL;
  811. xfs_buf_t *bp;
  812. int error, i, found;
  813. xfs_daddr_t umount_data_blk;
  814. xfs_daddr_t after_umount_blk;
  815. xfs_lsn_t tail_lsn;
  816. int hblks;
  817. found = 0;
  818. /*
  819. * Find previous log record
  820. */
  821. if ((error = xlog_find_head(log, head_blk)))
  822. return error;
  823. bp = xlog_get_bp(log, 1);
  824. if (!bp)
  825. return -ENOMEM;
  826. if (*head_blk == 0) { /* special case */
  827. error = xlog_bread(log, 0, 1, bp, &offset);
  828. if (error)
  829. goto done;
  830. if (xlog_get_cycle(offset) == 0) {
  831. *tail_blk = 0;
  832. /* leave all other log inited values alone */
  833. goto done;
  834. }
  835. }
  836. /*
  837. * Search backwards looking for log record header block
  838. */
  839. ASSERT(*head_blk < INT_MAX);
  840. for (i = (int)(*head_blk) - 1; i >= 0; i--) {
  841. error = xlog_bread(log, i, 1, bp, &offset);
  842. if (error)
  843. goto done;
  844. if (*(__be32 *)offset == cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) {
  845. found = 1;
  846. break;
  847. }
  848. }
  849. /*
  850. * If we haven't found the log record header block, start looking
  851. * again from the end of the physical log. XXXmiken: There should be
  852. * a check here to make sure we didn't search more than N blocks in
  853. * the previous code.
  854. */
  855. if (!found) {
  856. for (i = log->l_logBBsize - 1; i >= (int)(*head_blk); i--) {
  857. error = xlog_bread(log, i, 1, bp, &offset);
  858. if (error)
  859. goto done;
  860. if (*(__be32 *)offset ==
  861. cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) {
  862. found = 2;
  863. break;
  864. }
  865. }
  866. }
  867. if (!found) {
  868. xfs_warn(log->l_mp, "%s: couldn't find sync record", __func__);
  869. xlog_put_bp(bp);
  870. ASSERT(0);
  871. return -EIO;
  872. }
  873. /* find blk_no of tail of log */
  874. rhead = (xlog_rec_header_t *)offset;
  875. *tail_blk = BLOCK_LSN(be64_to_cpu(rhead->h_tail_lsn));
  876. /*
  877. * Reset log values according to the state of the log when we
  878. * crashed. In the case where head_blk == 0, we bump curr_cycle
  879. * one because the next write starts a new cycle rather than
  880. * continuing the cycle of the last good log record. At this
  881. * point we have guaranteed that all partial log records have been
  882. * accounted for. Therefore, we know that the last good log record
  883. * written was complete and ended exactly on the end boundary
  884. * of the physical log.
  885. */
  886. log->l_prev_block = i;
  887. log->l_curr_block = (int)*head_blk;
  888. log->l_curr_cycle = be32_to_cpu(rhead->h_cycle);
  889. if (found == 2)
  890. log->l_curr_cycle++;
  891. atomic64_set(&log->l_tail_lsn, be64_to_cpu(rhead->h_tail_lsn));
  892. atomic64_set(&log->l_last_sync_lsn, be64_to_cpu(rhead->h_lsn));
  893. xlog_assign_grant_head(&log->l_reserve_head.grant, log->l_curr_cycle,
  894. BBTOB(log->l_curr_block));
  895. xlog_assign_grant_head(&log->l_write_head.grant, log->l_curr_cycle,
  896. BBTOB(log->l_curr_block));
  897. /*
  898. * Look for unmount record. If we find it, then we know there
  899. * was a clean unmount. Since 'i' could be the last block in
  900. * the physical log, we convert to a log block before comparing
  901. * to the head_blk.
  902. *
  903. * Save the current tail lsn to use to pass to
  904. * xlog_clear_stale_blocks() below. We won't want to clear the
  905. * unmount record if there is one, so we pass the lsn of the
  906. * unmount record rather than the block after it.
  907. */
  908. if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
  909. int h_size = be32_to_cpu(rhead->h_size);
  910. int h_version = be32_to_cpu(rhead->h_version);
  911. if ((h_version & XLOG_VERSION_2) &&
  912. (h_size > XLOG_HEADER_CYCLE_SIZE)) {
  913. hblks = h_size / XLOG_HEADER_CYCLE_SIZE;
  914. if (h_size % XLOG_HEADER_CYCLE_SIZE)
  915. hblks++;
  916. } else {
  917. hblks = 1;
  918. }
  919. } else {
  920. hblks = 1;
  921. }
  922. after_umount_blk = (i + hblks + (int)
  923. BTOBB(be32_to_cpu(rhead->h_len))) % log->l_logBBsize;
  924. tail_lsn = atomic64_read(&log->l_tail_lsn);
  925. if (*head_blk == after_umount_blk &&
  926. be32_to_cpu(rhead->h_num_logops) == 1) {
  927. umount_data_blk = (i + hblks) % log->l_logBBsize;
  928. error = xlog_bread(log, umount_data_blk, 1, bp, &offset);
  929. if (error)
  930. goto done;
  931. op_head = (xlog_op_header_t *)offset;
  932. if (op_head->oh_flags & XLOG_UNMOUNT_TRANS) {
  933. /*
  934. * Set tail and last sync so that newly written
  935. * log records will point recovery to after the
  936. * current unmount record.
  937. */
  938. xlog_assign_atomic_lsn(&log->l_tail_lsn,
  939. log->l_curr_cycle, after_umount_blk);
  940. xlog_assign_atomic_lsn(&log->l_last_sync_lsn,
  941. log->l_curr_cycle, after_umount_blk);
  942. *tail_blk = after_umount_blk;
  943. /*
  944. * Note that the unmount was clean. If the unmount
  945. * was not clean, we need to know this to rebuild the
  946. * superblock counters from the perag headers if we
  947. * have a filesystem using non-persistent counters.
  948. */
  949. log->l_mp->m_flags |= XFS_MOUNT_WAS_CLEAN;
  950. }
  951. }
  952. /*
  953. * Make sure that there are no blocks in front of the head
  954. * with the same cycle number as the head. This can happen
  955. * because we allow multiple outstanding log writes concurrently,
  956. * and the later writes might make it out before earlier ones.
  957. *
  958. * We use the lsn from before modifying it so that we'll never
  959. * overwrite the unmount record after a clean unmount.
  960. *
  961. * Do this only if we are going to recover the filesystem
  962. *
  963. * NOTE: This used to say "if (!readonly)"
  964. * However on Linux, we can & do recover a read-only filesystem.
  965. * We only skip recovery if NORECOVERY is specified on mount,
  966. * in which case we would not be here.
  967. *
  968. * But... if the -device- itself is readonly, just skip this.
  969. * We can't recover this device anyway, so it won't matter.
  970. */
  971. if (!xfs_readonly_buftarg(log->l_mp->m_logdev_targp))
  972. error = xlog_clear_stale_blocks(log, tail_lsn);
  973. done:
  974. xlog_put_bp(bp);
  975. if (error)
  976. xfs_warn(log->l_mp, "failed to locate log tail");
  977. return error;
  978. }
  979. /*
  980. * Is the log zeroed at all?
  981. *
  982. * The last binary search should be changed to perform an X block read
  983. * once X becomes small enough. You can then search linearly through
  984. * the X blocks. This will cut down on the number of reads we need to do.
  985. *
  986. * If the log is partially zeroed, this routine will pass back the blkno
  987. * of the first block with cycle number 0. It won't have a complete LR
  988. * preceding it.
  989. *
  990. * Return:
  991. * 0 => the log is completely written to
  992. * 1 => use *blk_no as the first block of the log
  993. * <0 => error has occurred
  994. */
  995. STATIC int
  996. xlog_find_zeroed(
  997. struct xlog *log,
  998. xfs_daddr_t *blk_no)
  999. {
  1000. xfs_buf_t *bp;
  1001. xfs_caddr_t offset;
  1002. uint first_cycle, last_cycle;
  1003. xfs_daddr_t new_blk, last_blk, start_blk;
  1004. xfs_daddr_t num_scan_bblks;
  1005. int error, log_bbnum = log->l_logBBsize;
  1006. *blk_no = 0;
  1007. /* check totally zeroed log */
  1008. bp = xlog_get_bp(log, 1);
  1009. if (!bp)
  1010. return -ENOMEM;
  1011. error = xlog_bread(log, 0, 1, bp, &offset);
  1012. if (error)
  1013. goto bp_err;
  1014. first_cycle = xlog_get_cycle(offset);
  1015. if (first_cycle == 0) { /* completely zeroed log */
  1016. *blk_no = 0;
  1017. xlog_put_bp(bp);
  1018. return 1;
  1019. }
  1020. /* check partially zeroed log */
  1021. error = xlog_bread(log, log_bbnum-1, 1, bp, &offset);
  1022. if (error)
  1023. goto bp_err;
  1024. last_cycle = xlog_get_cycle(offset);
  1025. if (last_cycle != 0) { /* log completely written to */
  1026. xlog_put_bp(bp);
  1027. return 0;
  1028. } else if (first_cycle != 1) {
  1029. /*
  1030. * If the cycle of the last block is zero, the cycle of
  1031. * the first block must be 1. If it's not, maybe we're
  1032. * not looking at a log... Bail out.
  1033. */
  1034. xfs_warn(log->l_mp,
  1035. "Log inconsistent or not a log (last==0, first!=1)");
  1036. error = -EINVAL;
  1037. goto bp_err;
  1038. }
  1039. /* we have a partially zeroed log */
  1040. last_blk = log_bbnum-1;
  1041. if ((error = xlog_find_cycle_start(log, bp, 0, &last_blk, 0)))
  1042. goto bp_err;
  1043. /*
  1044. * Validate the answer. Because there is no way to guarantee that
  1045. * the entire log is made up of log records which are the same size,
  1046. * we scan over the defined maximum blocks. At this point, the maximum
  1047. * is not chosen to mean anything special. XXXmiken
  1048. */
  1049. num_scan_bblks = XLOG_TOTAL_REC_SHIFT(log);
  1050. ASSERT(num_scan_bblks <= INT_MAX);
  1051. if (last_blk < num_scan_bblks)
  1052. num_scan_bblks = last_blk;
  1053. start_blk = last_blk - num_scan_bblks;
  1054. /*
  1055. * We search for any instances of cycle number 0 that occur before
  1056. * our current estimate of the head. What we're trying to detect is
  1057. * 1 ... | 0 | 1 | 0...
  1058. * ^ binary search ends here
  1059. */
  1060. if ((error = xlog_find_verify_cycle(log, start_blk,
  1061. (int)num_scan_bblks, 0, &new_blk)))
  1062. goto bp_err;
  1063. if (new_blk != -1)
  1064. last_blk = new_blk;
  1065. /*
  1066. * Potentially backup over partial log record write. We don't need
  1067. * to search the end of the log because we know it is zero.
  1068. */
  1069. error = xlog_find_verify_log_record(log, start_blk, &last_blk, 0);
  1070. if (error == 1)
  1071. error = -EIO;
  1072. if (error)
  1073. goto bp_err;
  1074. *blk_no = last_blk;
  1075. bp_err:
  1076. xlog_put_bp(bp);
  1077. if (error)
  1078. return error;
  1079. return 1;
  1080. }
  1081. /*
  1082. * These are simple subroutines used by xlog_clear_stale_blocks() below
  1083. * to initialize a buffer full of empty log record headers and write
  1084. * them into the log.
  1085. */
  1086. STATIC void
  1087. xlog_add_record(
  1088. struct xlog *log,
  1089. xfs_caddr_t buf,
  1090. int cycle,
  1091. int block,
  1092. int tail_cycle,
  1093. int tail_block)
  1094. {
  1095. xlog_rec_header_t *recp = (xlog_rec_header_t *)buf;
  1096. memset(buf, 0, BBSIZE);
  1097. recp->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
  1098. recp->h_cycle = cpu_to_be32(cycle);
  1099. recp->h_version = cpu_to_be32(
  1100. xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1);
  1101. recp->h_lsn = cpu_to_be64(xlog_assign_lsn(cycle, block));
  1102. recp->h_tail_lsn = cpu_to_be64(xlog_assign_lsn(tail_cycle, tail_block));
  1103. recp->h_fmt = cpu_to_be32(XLOG_FMT);
  1104. memcpy(&recp->h_fs_uuid, &log->l_mp->m_sb.sb_uuid, sizeof(uuid_t));
  1105. }
  1106. STATIC int
  1107. xlog_write_log_records(
  1108. struct xlog *log,
  1109. int cycle,
  1110. int start_block,
  1111. int blocks,
  1112. int tail_cycle,
  1113. int tail_block)
  1114. {
  1115. xfs_caddr_t offset;
  1116. xfs_buf_t *bp;
  1117. int balign, ealign;
  1118. int sectbb = log->l_sectBBsize;
  1119. int end_block = start_block + blocks;
  1120. int bufblks;
  1121. int error = 0;
  1122. int i, j = 0;
  1123. /*
  1124. * Greedily allocate a buffer big enough to handle the full
  1125. * range of basic blocks to be written. If that fails, try
  1126. * a smaller size. We need to be able to write at least a
  1127. * log sector, or we're out of luck.
  1128. */
  1129. bufblks = 1 << ffs(blocks);
  1130. while (bufblks > log->l_logBBsize)
  1131. bufblks >>= 1;
  1132. while (!(bp = xlog_get_bp(log, bufblks))) {
  1133. bufblks >>= 1;
  1134. if (bufblks < sectbb)
  1135. return -ENOMEM;
  1136. }
  1137. /* We may need to do a read at the start to fill in part of
  1138. * the buffer in the starting sector not covered by the first
  1139. * write below.
  1140. */
  1141. balign = round_down(start_block, sectbb);
  1142. if (balign != start_block) {
  1143. error = xlog_bread_noalign(log, start_block, 1, bp);
  1144. if (error)
  1145. goto out_put_bp;
  1146. j = start_block - balign;
  1147. }
  1148. for (i = start_block; i < end_block; i += bufblks) {
  1149. int bcount, endcount;
  1150. bcount = min(bufblks, end_block - start_block);
  1151. endcount = bcount - j;
  1152. /* We may need to do a read at the end to fill in part of
  1153. * the buffer in the final sector not covered by the write.
  1154. * If this is the same sector as the above read, skip it.
  1155. */
  1156. ealign = round_down(end_block, sectbb);
  1157. if (j == 0 && (start_block + endcount > ealign)) {
  1158. offset = bp->b_addr + BBTOB(ealign - start_block);
  1159. error = xlog_bread_offset(log, ealign, sectbb,
  1160. bp, offset);
  1161. if (error)
  1162. break;
  1163. }
  1164. offset = xlog_align(log, start_block, endcount, bp);
  1165. for (; j < endcount; j++) {
  1166. xlog_add_record(log, offset, cycle, i+j,
  1167. tail_cycle, tail_block);
  1168. offset += BBSIZE;
  1169. }
  1170. error = xlog_bwrite(log, start_block, endcount, bp);
  1171. if (error)
  1172. break;
  1173. start_block += endcount;
  1174. j = 0;
  1175. }
  1176. out_put_bp:
  1177. xlog_put_bp(bp);
  1178. return error;
  1179. }
  1180. /*
  1181. * This routine is called to blow away any incomplete log writes out
  1182. * in front of the log head. We do this so that we won't become confused
  1183. * if we come up, write only a little bit more, and then crash again.
  1184. * If we leave the partial log records out there, this situation could
  1185. * cause us to think those partial writes are valid blocks since they
  1186. * have the current cycle number. We get rid of them by overwriting them
  1187. * with empty log records with the old cycle number rather than the
  1188. * current one.
  1189. *
  1190. * The tail lsn is passed in rather than taken from
  1191. * the log so that we will not write over the unmount record after a
  1192. * clean unmount in a 512 block log. Doing so would leave the log without
  1193. * any valid log records in it until a new one was written. If we crashed
  1194. * during that time we would not be able to recover.
  1195. */
  1196. STATIC int
  1197. xlog_clear_stale_blocks(
  1198. struct xlog *log,
  1199. xfs_lsn_t tail_lsn)
  1200. {
  1201. int tail_cycle, head_cycle;
  1202. int tail_block, head_block;
  1203. int tail_distance, max_distance;
  1204. int distance;
  1205. int error;
  1206. tail_cycle = CYCLE_LSN(tail_lsn);
  1207. tail_block = BLOCK_LSN(tail_lsn);
  1208. head_cycle = log->l_curr_cycle;
  1209. head_block = log->l_curr_block;
  1210. /*
  1211. * Figure out the distance between the new head of the log
  1212. * and the tail. We want to write over any blocks beyond the
  1213. * head that we may have written just before the crash, but
  1214. * we don't want to overwrite the tail of the log.
  1215. */
  1216. if (head_cycle == tail_cycle) {
  1217. /*
  1218. * The tail is behind the head in the physical log,
  1219. * so the distance from the head to the tail is the
  1220. * distance from the head to the end of the log plus
  1221. * the distance from the beginning of the log to the
  1222. * tail.
  1223. */
  1224. if (unlikely(head_block < tail_block || head_block >= log->l_logBBsize)) {
  1225. XFS_ERROR_REPORT("xlog_clear_stale_blocks(1)",
  1226. XFS_ERRLEVEL_LOW, log->l_mp);
  1227. return -EFSCORRUPTED;
  1228. }
  1229. tail_distance = tail_block + (log->l_logBBsize - head_block);
  1230. } else {
  1231. /*
  1232. * The head is behind the tail in the physical log,
  1233. * so the distance from the head to the tail is just
  1234. * the tail block minus the head block.
  1235. */
  1236. if (unlikely(head_block >= tail_block || head_cycle != (tail_cycle + 1))){
  1237. XFS_ERROR_REPORT("xlog_clear_stale_blocks(2)",
  1238. XFS_ERRLEVEL_LOW, log->l_mp);
  1239. return -EFSCORRUPTED;
  1240. }
  1241. tail_distance = tail_block - head_block;
  1242. }
  1243. /*
  1244. * If the head is right up against the tail, we can't clear
  1245. * anything.
  1246. */
  1247. if (tail_distance <= 0) {
  1248. ASSERT(tail_distance == 0);
  1249. return 0;
  1250. }
  1251. max_distance = XLOG_TOTAL_REC_SHIFT(log);
  1252. /*
  1253. * Take the smaller of the maximum amount of outstanding I/O
  1254. * we could have and the distance to the tail to clear out.
  1255. * We take the smaller so that we don't overwrite the tail and
  1256. * we don't waste all day writing from the head to the tail
  1257. * for no reason.
  1258. */
  1259. max_distance = MIN(max_distance, tail_distance);
  1260. if ((head_block + max_distance) <= log->l_logBBsize) {
  1261. /*
  1262. * We can stomp all the blocks we need to without
  1263. * wrapping around the end of the log. Just do it
  1264. * in a single write. Use the cycle number of the
  1265. * current cycle minus one so that the log will look like:
  1266. * n ... | n - 1 ...
  1267. */
  1268. error = xlog_write_log_records(log, (head_cycle - 1),
  1269. head_block, max_distance, tail_cycle,
  1270. tail_block);
  1271. if (error)
  1272. return error;
  1273. } else {
  1274. /*
  1275. * We need to wrap around the end of the physical log in
  1276. * order to clear all the blocks. Do it in two separate
  1277. * I/Os. The first write should be from the head to the
  1278. * end of the physical log, and it should use the current
  1279. * cycle number minus one just like above.
  1280. */
  1281. distance = log->l_logBBsize - head_block;
  1282. error = xlog_write_log_records(log, (head_cycle - 1),
  1283. head_block, distance, tail_cycle,
  1284. tail_block);
  1285. if (error)
  1286. return error;
  1287. /*
  1288. * Now write the blocks at the start of the physical log.
  1289. * This writes the remainder of the blocks we want to clear.
  1290. * It uses the current cycle number since we're now on the
  1291. * same cycle as the head so that we get:
  1292. * n ... n ... | n - 1 ...
  1293. * ^^^^^ blocks we're writing
  1294. */
  1295. distance = max_distance - (log->l_logBBsize - head_block);
  1296. error = xlog_write_log_records(log, head_cycle, 0, distance,
  1297. tail_cycle, tail_block);
  1298. if (error)
  1299. return error;
  1300. }
  1301. return 0;
  1302. }
  1303. /******************************************************************************
  1304. *
  1305. * Log recover routines
  1306. *
  1307. ******************************************************************************
  1308. */
  1309. /*
  1310. * Sort the log items in the transaction.
  1311. *
  1312. * The ordering constraints are defined by the inode allocation and unlink
  1313. * behaviour. The rules are:
  1314. *
  1315. * 1. Every item is only logged once in a given transaction. Hence it
  1316. * represents the last logged state of the item. Hence ordering is
  1317. * dependent on the order in which operations need to be performed so
  1318. * required initial conditions are always met.
  1319. *
  1320. * 2. Cancelled buffers are recorded in pass 1 in a separate table and
  1321. * there's nothing to replay from them so we can simply cull them
  1322. * from the transaction. However, we can't do that until after we've
  1323. * replayed all the other items because they may be dependent on the
  1324. * cancelled buffer and replaying the cancelled buffer can remove it
  1325. * form the cancelled buffer table. Hence they have tobe done last.
  1326. *
  1327. * 3. Inode allocation buffers must be replayed before inode items that
  1328. * read the buffer and replay changes into it. For filesystems using the
  1329. * ICREATE transactions, this means XFS_LI_ICREATE objects need to get
  1330. * treated the same as inode allocation buffers as they create and
  1331. * initialise the buffers directly.
  1332. *
  1333. * 4. Inode unlink buffers must be replayed after inode items are replayed.
  1334. * This ensures that inodes are completely flushed to the inode buffer
  1335. * in a "free" state before we remove the unlinked inode list pointer.
  1336. *
  1337. * Hence the ordering needs to be inode allocation buffers first, inode items
  1338. * second, inode unlink buffers third and cancelled buffers last.
  1339. *
  1340. * But there's a problem with that - we can't tell an inode allocation buffer
  1341. * apart from a regular buffer, so we can't separate them. We can, however,
  1342. * tell an inode unlink buffer from the others, and so we can separate them out
  1343. * from all the other buffers and move them to last.
  1344. *
  1345. * Hence, 4 lists, in order from head to tail:
  1346. * - buffer_list for all buffers except cancelled/inode unlink buffers
  1347. * - item_list for all non-buffer items
  1348. * - inode_buffer_list for inode unlink buffers
  1349. * - cancel_list for the cancelled buffers
  1350. *
  1351. * Note that we add objects to the tail of the lists so that first-to-last
  1352. * ordering is preserved within the lists. Adding objects to the head of the
  1353. * list means when we traverse from the head we walk them in last-to-first
  1354. * order. For cancelled buffers and inode unlink buffers this doesn't matter,
  1355. * but for all other items there may be specific ordering that we need to
  1356. * preserve.
  1357. */
  1358. STATIC int
  1359. xlog_recover_reorder_trans(
  1360. struct xlog *log,
  1361. struct xlog_recover *trans,
  1362. int pass)
  1363. {
  1364. xlog_recover_item_t *item, *n;
  1365. int error = 0;
  1366. LIST_HEAD(sort_list);
  1367. LIST_HEAD(cancel_list);
  1368. LIST_HEAD(buffer_list);
  1369. LIST_HEAD(inode_buffer_list);
  1370. LIST_HEAD(inode_list);
  1371. list_splice_init(&trans->r_itemq, &sort_list);
  1372. list_for_each_entry_safe(item, n, &sort_list, ri_list) {
  1373. xfs_buf_log_format_t *buf_f = item->ri_buf[0].i_addr;
  1374. switch (ITEM_TYPE(item)) {
  1375. case XFS_LI_ICREATE:
  1376. list_move_tail(&item->ri_list, &buffer_list);
  1377. break;
  1378. case XFS_LI_BUF:
  1379. if (buf_f->blf_flags & XFS_BLF_CANCEL) {
  1380. trace_xfs_log_recover_item_reorder_head(log,
  1381. trans, item, pass);
  1382. list_move(&item->ri_list, &cancel_list);
  1383. break;
  1384. }
  1385. if (buf_f->blf_flags & XFS_BLF_INODE_BUF) {
  1386. list_move(&item->ri_list, &inode_buffer_list);
  1387. break;
  1388. }
  1389. list_move_tail(&item->ri_list, &buffer_list);
  1390. break;
  1391. case XFS_LI_INODE:
  1392. case XFS_LI_DQUOT:
  1393. case XFS_LI_QUOTAOFF:
  1394. case XFS_LI_EFD:
  1395. case XFS_LI_EFI:
  1396. trace_xfs_log_recover_item_reorder_tail(log,
  1397. trans, item, pass);
  1398. list_move_tail(&item->ri_list, &inode_list);
  1399. break;
  1400. default:
  1401. xfs_warn(log->l_mp,
  1402. "%s: unrecognized type of log operation",
  1403. __func__);
  1404. ASSERT(0);
  1405. /*
  1406. * return the remaining items back to the transaction
  1407. * item list so they can be freed in caller.
  1408. */
  1409. if (!list_empty(&sort_list))
  1410. list_splice_init(&sort_list, &trans->r_itemq);
  1411. error = -EIO;
  1412. goto out;
  1413. }
  1414. }
  1415. out:
  1416. ASSERT(list_empty(&sort_list));
  1417. if (!list_empty(&buffer_list))
  1418. list_splice(&buffer_list, &trans->r_itemq);
  1419. if (!list_empty(&inode_list))
  1420. list_splice_tail(&inode_list, &trans->r_itemq);
  1421. if (!list_empty(&inode_buffer_list))
  1422. list_splice_tail(&inode_buffer_list, &trans->r_itemq);
  1423. if (!list_empty(&cancel_list))
  1424. list_splice_tail(&cancel_list, &trans->r_itemq);
  1425. return error;
  1426. }
  1427. /*
  1428. * Build up the table of buf cancel records so that we don't replay
  1429. * cancelled data in the second pass. For buffer records that are
  1430. * not cancel records, there is nothing to do here so we just return.
  1431. *
  1432. * If we get a cancel record which is already in the table, this indicates
  1433. * that the buffer was cancelled multiple times. In order to ensure
  1434. * that during pass 2 we keep the record in the table until we reach its
  1435. * last occurrence in the log, we keep a reference count in the cancel
  1436. * record in the table to tell us how many times we expect to see this
  1437. * record during the second pass.
  1438. */
  1439. STATIC int
  1440. xlog_recover_buffer_pass1(
  1441. struct xlog *log,
  1442. struct xlog_recover_item *item)
  1443. {
  1444. xfs_buf_log_format_t *buf_f = item->ri_buf[0].i_addr;
  1445. struct list_head *bucket;
  1446. struct xfs_buf_cancel *bcp;
  1447. /*
  1448. * If this isn't a cancel buffer item, then just return.
  1449. */
  1450. if (!(buf_f->blf_flags & XFS_BLF_CANCEL)) {
  1451. trace_xfs_log_recover_buf_not_cancel(log, buf_f);
  1452. return 0;
  1453. }
  1454. /*
  1455. * Insert an xfs_buf_cancel record into the hash table of them.
  1456. * If there is already an identical record, bump its reference count.
  1457. */
  1458. bucket = XLOG_BUF_CANCEL_BUCKET(log, buf_f->blf_blkno);
  1459. list_for_each_entry(bcp, bucket, bc_list) {
  1460. if (bcp->bc_blkno == buf_f->blf_blkno &&
  1461. bcp->bc_len == buf_f->blf_len) {
  1462. bcp->bc_refcount++;
  1463. trace_xfs_log_recover_buf_cancel_ref_inc(log, buf_f);
  1464. return 0;
  1465. }
  1466. }
  1467. bcp = kmem_alloc(sizeof(struct xfs_buf_cancel), KM_SLEEP);
  1468. bcp->bc_blkno = buf_f->blf_blkno;
  1469. bcp->bc_len = buf_f->blf_len;
  1470. bcp->bc_refcount = 1;
  1471. list_add_tail(&bcp->bc_list, bucket);
  1472. trace_xfs_log_recover_buf_cancel_add(log, buf_f);
  1473. return 0;
  1474. }
  1475. /*
  1476. * Check to see whether the buffer being recovered has a corresponding
  1477. * entry in the buffer cancel record table. If it is, return the cancel
  1478. * buffer structure to the caller.
  1479. */
  1480. STATIC struct xfs_buf_cancel *
  1481. xlog_peek_buffer_cancelled(
  1482. struct xlog *log,
  1483. xfs_daddr_t blkno,
  1484. uint len,
  1485. ushort flags)
  1486. {
  1487. struct list_head *bucket;
  1488. struct xfs_buf_cancel *bcp;
  1489. if (!log->l_buf_cancel_table) {
  1490. /* empty table means no cancelled buffers in the log */
  1491. ASSERT(!(flags & XFS_BLF_CANCEL));
  1492. return NULL;
  1493. }
  1494. bucket = XLOG_BUF_CANCEL_BUCKET(log, blkno);
  1495. list_for_each_entry(bcp, bucket, bc_list) {
  1496. if (bcp->bc_blkno == blkno && bcp->bc_len == len)
  1497. return bcp;
  1498. }
  1499. /*
  1500. * We didn't find a corresponding entry in the table, so return 0 so
  1501. * that the buffer is NOT cancelled.
  1502. */
  1503. ASSERT(!(flags & XFS_BLF_CANCEL));
  1504. return NULL;
  1505. }
  1506. /*
  1507. * If the buffer is being cancelled then return 1 so that it will be cancelled,
  1508. * otherwise return 0. If the buffer is actually a buffer cancel item
  1509. * (XFS_BLF_CANCEL is set), then decrement the refcount on the entry in the
  1510. * table and remove it from the table if this is the last reference.
  1511. *
  1512. * We remove the cancel record from the table when we encounter its last
  1513. * occurrence in the log so that if the same buffer is re-used again after its
  1514. * last cancellation we actually replay the changes made at that point.
  1515. */
  1516. STATIC int
  1517. xlog_check_buffer_cancelled(
  1518. struct xlog *log,
  1519. xfs_daddr_t blkno,
  1520. uint len,
  1521. ushort flags)
  1522. {
  1523. struct xfs_buf_cancel *bcp;
  1524. bcp = xlog_peek_buffer_cancelled(log, blkno, len, flags);
  1525. if (!bcp)
  1526. return 0;
  1527. /*
  1528. * We've go a match, so return 1 so that the recovery of this buffer
  1529. * is cancelled. If this buffer is actually a buffer cancel log
  1530. * item, then decrement the refcount on the one in the table and
  1531. * remove it if this is the last reference.
  1532. */
  1533. if (flags & XFS_BLF_CANCEL) {
  1534. if (--bcp->bc_refcount == 0) {
  1535. list_del(&bcp->bc_list);
  1536. kmem_free(bcp);
  1537. }
  1538. }
  1539. return 1;
  1540. }
  1541. /*
  1542. * Perform recovery for a buffer full of inodes. In these buffers, the only
  1543. * data which should be recovered is that which corresponds to the
  1544. * di_next_unlinked pointers in the on disk inode structures. The rest of the
  1545. * data for the inodes is always logged through the inodes themselves rather
  1546. * than the inode buffer and is recovered in xlog_recover_inode_pass2().
  1547. *
  1548. * The only time when buffers full of inodes are fully recovered is when the
  1549. * buffer is full of newly allocated inodes. In this case the buffer will
  1550. * not be marked as an inode buffer and so will be sent to
  1551. * xlog_recover_do_reg_buffer() below during recovery.
  1552. */
  1553. STATIC int
  1554. xlog_recover_do_inode_buffer(
  1555. struct xfs_mount *mp,
  1556. xlog_recover_item_t *item,
  1557. struct xfs_buf *bp,
  1558. xfs_buf_log_format_t *buf_f)
  1559. {
  1560. int i;
  1561. int item_index = 0;
  1562. int bit = 0;
  1563. int nbits = 0;
  1564. int reg_buf_offset = 0;
  1565. int reg_buf_bytes = 0;
  1566. int next_unlinked_offset;
  1567. int inodes_per_buf;
  1568. xfs_agino_t *logged_nextp;
  1569. xfs_agino_t *buffer_nextp;
  1570. trace_xfs_log_recover_buf_inode_buf(mp->m_log, buf_f);
  1571. /*
  1572. * Post recovery validation only works properly on CRC enabled
  1573. * filesystems.
  1574. */
  1575. if (xfs_sb_version_hascrc(&mp->m_sb))
  1576. bp->b_ops = &xfs_inode_buf_ops;
  1577. inodes_per_buf = BBTOB(bp->b_io_length) >> mp->m_sb.sb_inodelog;
  1578. for (i = 0; i < inodes_per_buf; i++) {
  1579. next_unlinked_offset = (i * mp->m_sb.sb_inodesize) +
  1580. offsetof(xfs_dinode_t, di_next_unlinked);
  1581. while (next_unlinked_offset >=
  1582. (reg_buf_offset + reg_buf_bytes)) {
  1583. /*
  1584. * The next di_next_unlinked field is beyond
  1585. * the current logged region. Find the next
  1586. * logged region that contains or is beyond
  1587. * the current di_next_unlinked field.
  1588. */
  1589. bit += nbits;
  1590. bit = xfs_next_bit(buf_f->blf_data_map,
  1591. buf_f->blf_map_size, bit);
  1592. /*
  1593. * If there are no more logged regions in the
  1594. * buffer, then we're done.
  1595. */
  1596. if (bit == -1)
  1597. return 0;
  1598. nbits = xfs_contig_bits(buf_f->blf_data_map,
  1599. buf_f->blf_map_size, bit);
  1600. ASSERT(nbits > 0);
  1601. reg_buf_offset = bit << XFS_BLF_SHIFT;
  1602. reg_buf_bytes = nbits << XFS_BLF_SHIFT;
  1603. item_index++;
  1604. }
  1605. /*
  1606. * If the current logged region starts after the current
  1607. * di_next_unlinked field, then move on to the next
  1608. * di_next_unlinked field.
  1609. */
  1610. if (next_unlinked_offset < reg_buf_offset)
  1611. continue;
  1612. ASSERT(item->ri_buf[item_index].i_addr != NULL);
  1613. ASSERT((item->ri_buf[item_index].i_len % XFS_BLF_CHUNK) == 0);
  1614. ASSERT((reg_buf_offset + reg_buf_bytes) <=
  1615. BBTOB(bp->b_io_length));
  1616. /*
  1617. * The current logged region contains a copy of the
  1618. * current di_next_unlinked field. Extract its value
  1619. * and copy it to the buffer copy.
  1620. */
  1621. logged_nextp = item->ri_buf[item_index].i_addr +
  1622. next_unlinked_offset - reg_buf_offset;
  1623. if (unlikely(*logged_nextp == 0)) {
  1624. xfs_alert(mp,
  1625. "Bad inode buffer log record (ptr = 0x%p, bp = 0x%p). "
  1626. "Trying to replay bad (0) inode di_next_unlinked field.",
  1627. item, bp);
  1628. XFS_ERROR_REPORT("xlog_recover_do_inode_buf",
  1629. XFS_ERRLEVEL_LOW, mp);
  1630. return -EFSCORRUPTED;
  1631. }
  1632. buffer_nextp = (xfs_agino_t *)xfs_buf_offset(bp,
  1633. next_unlinked_offset);
  1634. *buffer_nextp = *logged_nextp;
  1635. /*
  1636. * If necessary, recalculate the CRC in the on-disk inode. We
  1637. * have to leave the inode in a consistent state for whoever
  1638. * reads it next....
  1639. */
  1640. xfs_dinode_calc_crc(mp, (struct xfs_dinode *)
  1641. xfs_buf_offset(bp, i * mp->m_sb.sb_inodesize));
  1642. }
  1643. return 0;
  1644. }
  1645. /*
  1646. * V5 filesystems know the age of the buffer on disk being recovered. We can
  1647. * have newer objects on disk than we are replaying, and so for these cases we
  1648. * don't want to replay the current change as that will make the buffer contents
  1649. * temporarily invalid on disk.
  1650. *
  1651. * The magic number might not match the buffer type we are going to recover
  1652. * (e.g. reallocated blocks), so we ignore the xfs_buf_log_format flags. Hence
  1653. * extract the LSN of the existing object in the buffer based on it's current
  1654. * magic number. If we don't recognise the magic number in the buffer, then
  1655. * return a LSN of -1 so that the caller knows it was an unrecognised block and
  1656. * so can recover the buffer.
  1657. *
  1658. * Note: we cannot rely solely on magic number matches to determine that the
  1659. * buffer has a valid LSN - we also need to verify that it belongs to this
  1660. * filesystem, so we need to extract the object's LSN and compare it to that
  1661. * which we read from the superblock. If the UUIDs don't match, then we've got a
  1662. * stale metadata block from an old filesystem instance that we need to recover
  1663. * over the top of.
  1664. */
  1665. static xfs_lsn_t
  1666. xlog_recover_get_buf_lsn(
  1667. struct xfs_mount *mp,
  1668. struct xfs_buf *bp)
  1669. {
  1670. __uint32_t magic32;
  1671. __uint16_t magic16;
  1672. __uint16_t magicda;
  1673. void *blk = bp->b_addr;
  1674. uuid_t *uuid;
  1675. xfs_lsn_t lsn = -1;
  1676. /* v4 filesystems always recover immediately */
  1677. if (!xfs_sb_version_hascrc(&mp->m_sb))
  1678. goto recover_immediately;
  1679. magic32 = be32_to_cpu(*(__be32 *)blk);
  1680. switch (magic32) {
  1681. case XFS_ABTB_CRC_MAGIC:
  1682. case XFS_ABTC_CRC_MAGIC:
  1683. case XFS_ABTB_MAGIC:
  1684. case XFS_ABTC_MAGIC:
  1685. case XFS_IBT_CRC_MAGIC:
  1686. case XFS_IBT_MAGIC: {
  1687. struct xfs_btree_block *btb = blk;
  1688. lsn = be64_to_cpu(btb->bb_u.s.bb_lsn);
  1689. uuid = &btb->bb_u.s.bb_uuid;
  1690. break;
  1691. }
  1692. case XFS_BMAP_CRC_MAGIC:
  1693. case XFS_BMAP_MAGIC: {
  1694. struct xfs_btree_block *btb = blk;
  1695. lsn = be64_to_cpu(btb->bb_u.l.bb_lsn);
  1696. uuid = &btb->bb_u.l.bb_uuid;
  1697. break;
  1698. }
  1699. case XFS_AGF_MAGIC:
  1700. lsn = be64_to_cpu(((struct xfs_agf *)blk)->agf_lsn);
  1701. uuid = &((struct xfs_agf *)blk)->agf_uuid;
  1702. break;
  1703. case XFS_AGFL_MAGIC:
  1704. lsn = be64_to_cpu(((struct xfs_agfl *)blk)->agfl_lsn);
  1705. uuid = &((struct xfs_agfl *)blk)->agfl_uuid;
  1706. break;
  1707. case XFS_AGI_MAGIC:
  1708. lsn = be64_to_cpu(((struct xfs_agi *)blk)->agi_lsn);
  1709. uuid = &((struct xfs_agi *)blk)->agi_uuid;
  1710. break;
  1711. case XFS_SYMLINK_MAGIC:
  1712. lsn = be64_to_cpu(((struct xfs_dsymlink_hdr *)blk)->sl_lsn);
  1713. uuid = &((struct xfs_dsymlink_hdr *)blk)->sl_uuid;
  1714. break;
  1715. case XFS_DIR3_BLOCK_MAGIC:
  1716. case XFS_DIR3_DATA_MAGIC:
  1717. case XFS_DIR3_FREE_MAGIC:
  1718. lsn = be64_to_cpu(((struct xfs_dir3_blk_hdr *)blk)->lsn);
  1719. uuid = &((struct xfs_dir3_blk_hdr *)blk)->uuid;
  1720. break;
  1721. case XFS_ATTR3_RMT_MAGIC:
  1722. lsn = be64_to_cpu(((struct xfs_attr3_rmt_hdr *)blk)->rm_lsn);
  1723. uuid = &((struct xfs_attr3_rmt_hdr *)blk)->rm_uuid;
  1724. break;
  1725. case XFS_SB_MAGIC:
  1726. lsn = be64_to_cpu(((struct xfs_dsb *)blk)->sb_lsn);
  1727. uuid = &((struct xfs_dsb *)blk)->sb_uuid;
  1728. break;
  1729. default:
  1730. break;
  1731. }
  1732. if (lsn != (xfs_lsn_t)-1) {
  1733. if (!uuid_equal(&mp->m_sb.sb_uuid, uuid))
  1734. goto recover_immediately;
  1735. return lsn;
  1736. }
  1737. magicda = be16_to_cpu(((struct xfs_da_blkinfo *)blk)->magic);
  1738. switch (magicda) {
  1739. case XFS_DIR3_LEAF1_MAGIC:
  1740. case XFS_DIR3_LEAFN_MAGIC:
  1741. case XFS_DA3_NODE_MAGIC:
  1742. lsn = be64_to_cpu(((struct xfs_da3_blkinfo *)blk)->lsn);
  1743. uuid = &((struct xfs_da3_blkinfo *)blk)->uuid;
  1744. break;
  1745. default:
  1746. break;
  1747. }
  1748. if (lsn != (xfs_lsn_t)-1) {
  1749. if (!uuid_equal(&mp->m_sb.sb_uuid, uuid))
  1750. goto recover_immediately;
  1751. return lsn;
  1752. }
  1753. /*
  1754. * We do individual object checks on dquot and inode buffers as they
  1755. * have their own individual LSN records. Also, we could have a stale
  1756. * buffer here, so we have to at least recognise these buffer types.
  1757. *
  1758. * A notd complexity here is inode unlinked list processing - it logs
  1759. * the inode directly in the buffer, but we don't know which inodes have
  1760. * been modified, and there is no global buffer LSN. Hence we need to
  1761. * recover all inode buffer types immediately. This problem will be
  1762. * fixed by logical logging of the unlinked list modifications.
  1763. */
  1764. magic16 = be16_to_cpu(*(__be16 *)blk);
  1765. switch (magic16) {
  1766. case XFS_DQUOT_MAGIC:
  1767. case XFS_DINODE_MAGIC:
  1768. goto recover_immediately;
  1769. default:
  1770. break;
  1771. }
  1772. /* unknown buffer contents, recover immediately */
  1773. recover_immediately:
  1774. return (xfs_lsn_t)-1;
  1775. }
  1776. /*
  1777. * Validate the recovered buffer is of the correct type and attach the
  1778. * appropriate buffer operations to them for writeback. Magic numbers are in a
  1779. * few places:
  1780. * the first 16 bits of the buffer (inode buffer, dquot buffer),
  1781. * the first 32 bits of the buffer (most blocks),
  1782. * inside a struct xfs_da_blkinfo at the start of the buffer.
  1783. */
  1784. static void
  1785. xlog_recover_validate_buf_type(
  1786. struct xfs_mount *mp,
  1787. struct xfs_buf *bp,
  1788. xfs_buf_log_format_t *buf_f)
  1789. {
  1790. struct xfs_da_blkinfo *info = bp->b_addr;
  1791. __uint32_t magic32;
  1792. __uint16_t magic16;
  1793. __uint16_t magicda;
  1794. /*
  1795. * We can only do post recovery validation on items on CRC enabled
  1796. * fielsystems as we need to know when the buffer was written to be able
  1797. * to determine if we should have replayed the item. If we replay old
  1798. * metadata over a newer buffer, then it will enter a temporarily
  1799. * inconsistent state resulting in verification failures. Hence for now
  1800. * just avoid the verification stage for non-crc filesystems
  1801. */
  1802. if (!xfs_sb_version_hascrc(&mp->m_sb))
  1803. return;
  1804. magic32 = be32_to_cpu(*(__be32 *)bp->b_addr);
  1805. magic16 = be16_to_cpu(*(__be16*)bp->b_addr);
  1806. magicda = be16_to_cpu(info->magic);
  1807. switch (xfs_blft_from_flags(buf_f)) {
  1808. case XFS_BLFT_BTREE_BUF:
  1809. switch (magic32) {
  1810. case XFS_ABTB_CRC_MAGIC:
  1811. case XFS_ABTC_CRC_MAGIC:
  1812. case XFS_ABTB_MAGIC:
  1813. case XFS_ABTC_MAGIC:
  1814. bp->b_ops = &xfs_allocbt_buf_ops;
  1815. break;
  1816. case XFS_IBT_CRC_MAGIC:
  1817. case XFS_FIBT_CRC_MAGIC:
  1818. case XFS_IBT_MAGIC:
  1819. case XFS_FIBT_MAGIC:
  1820. bp->b_ops = &xfs_inobt_buf_ops;
  1821. break;
  1822. case XFS_BMAP_CRC_MAGIC:
  1823. case XFS_BMAP_MAGIC:
  1824. bp->b_ops = &xfs_bmbt_buf_ops;
  1825. break;
  1826. default:
  1827. xfs_warn(mp, "Bad btree block magic!");
  1828. ASSERT(0);
  1829. break;
  1830. }
  1831. break;
  1832. case XFS_BLFT_AGF_BUF:
  1833. if (magic32 != XFS_AGF_MAGIC) {
  1834. xfs_warn(mp, "Bad AGF block magic!");
  1835. ASSERT(0);
  1836. break;
  1837. }
  1838. bp->b_ops = &xfs_agf_buf_ops;
  1839. break;
  1840. case XFS_BLFT_AGFL_BUF:
  1841. if (magic32 != XFS_AGFL_MAGIC) {
  1842. xfs_warn(mp, "Bad AGFL block magic!");
  1843. ASSERT(0);
  1844. break;
  1845. }
  1846. bp->b_ops = &xfs_agfl_buf_ops;
  1847. break;
  1848. case XFS_BLFT_AGI_BUF:
  1849. if (magic32 != XFS_AGI_MAGIC) {
  1850. xfs_warn(mp, "Bad AGI block magic!");
  1851. ASSERT(0);
  1852. break;
  1853. }
  1854. bp->b_ops = &xfs_agi_buf_ops;
  1855. break;
  1856. case XFS_BLFT_UDQUOT_BUF:
  1857. case XFS_BLFT_PDQUOT_BUF:
  1858. case XFS_BLFT_GDQUOT_BUF:
  1859. #ifdef CONFIG_XFS_QUOTA
  1860. if (magic16 != XFS_DQUOT_MAGIC) {
  1861. xfs_warn(mp, "Bad DQUOT block magic!");
  1862. ASSERT(0);
  1863. break;
  1864. }
  1865. bp->b_ops = &xfs_dquot_buf_ops;
  1866. #else
  1867. xfs_alert(mp,
  1868. "Trying to recover dquots without QUOTA support built in!");
  1869. ASSERT(0);
  1870. #endif
  1871. break;
  1872. case XFS_BLFT_DINO_BUF:
  1873. if (magic16 != XFS_DINODE_MAGIC) {
  1874. xfs_warn(mp, "Bad INODE block magic!");
  1875. ASSERT(0);
  1876. break;
  1877. }
  1878. bp->b_ops = &xfs_inode_buf_ops;
  1879. break;
  1880. case XFS_BLFT_SYMLINK_BUF:
  1881. if (magic32 != XFS_SYMLINK_MAGIC) {
  1882. xfs_warn(mp, "Bad symlink block magic!");
  1883. ASSERT(0);
  1884. break;
  1885. }
  1886. bp->b_ops = &xfs_symlink_buf_ops;
  1887. break;
  1888. case XFS_BLFT_DIR_BLOCK_BUF:
  1889. if (magic32 != XFS_DIR2_BLOCK_MAGIC &&
  1890. magic32 != XFS_DIR3_BLOCK_MAGIC) {
  1891. xfs_warn(mp, "Bad dir block magic!");
  1892. ASSERT(0);
  1893. break;
  1894. }
  1895. bp->b_ops = &xfs_dir3_block_buf_ops;
  1896. break;
  1897. case XFS_BLFT_DIR_DATA_BUF:
  1898. if (magic32 != XFS_DIR2_DATA_MAGIC &&
  1899. magic32 != XFS_DIR3_DATA_MAGIC) {
  1900. xfs_warn(mp, "Bad dir data magic!");
  1901. ASSERT(0);
  1902. break;
  1903. }
  1904. bp->b_ops = &xfs_dir3_data_buf_ops;
  1905. break;
  1906. case XFS_BLFT_DIR_FREE_BUF:
  1907. if (magic32 != XFS_DIR2_FREE_MAGIC &&
  1908. magic32 != XFS_DIR3_FREE_MAGIC) {
  1909. xfs_warn(mp, "Bad dir3 free magic!");
  1910. ASSERT(0);
  1911. break;
  1912. }
  1913. bp->b_ops = &xfs_dir3_free_buf_ops;
  1914. break;
  1915. case XFS_BLFT_DIR_LEAF1_BUF:
  1916. if (magicda != XFS_DIR2_LEAF1_MAGIC &&
  1917. magicda != XFS_DIR3_LEAF1_MAGIC) {
  1918. xfs_warn(mp, "Bad dir leaf1 magic!");
  1919. ASSERT(0);
  1920. break;
  1921. }
  1922. bp->b_ops = &xfs_dir3_leaf1_buf_ops;
  1923. break;
  1924. case XFS_BLFT_DIR_LEAFN_BUF:
  1925. if (magicda != XFS_DIR2_LEAFN_MAGIC &&
  1926. magicda != XFS_DIR3_LEAFN_MAGIC) {
  1927. xfs_warn(mp, "Bad dir leafn magic!");
  1928. ASSERT(0);
  1929. break;
  1930. }
  1931. bp->b_ops = &xfs_dir3_leafn_buf_ops;
  1932. break;
  1933. case XFS_BLFT_DA_NODE_BUF:
  1934. if (magicda != XFS_DA_NODE_MAGIC &&
  1935. magicda != XFS_DA3_NODE_MAGIC) {
  1936. xfs_warn(mp, "Bad da node magic!");
  1937. ASSERT(0);
  1938. break;
  1939. }
  1940. bp->b_ops = &xfs_da3_node_buf_ops;
  1941. break;
  1942. case XFS_BLFT_ATTR_LEAF_BUF:
  1943. if (magicda != XFS_ATTR_LEAF_MAGIC &&
  1944. magicda != XFS_ATTR3_LEAF_MAGIC) {
  1945. xfs_warn(mp, "Bad attr leaf magic!");
  1946. ASSERT(0);
  1947. break;
  1948. }
  1949. bp->b_ops = &xfs_attr3_leaf_buf_ops;
  1950. break;
  1951. case XFS_BLFT_ATTR_RMT_BUF:
  1952. if (magic32 != XFS_ATTR3_RMT_MAGIC) {
  1953. xfs_warn(mp, "Bad attr remote magic!");
  1954. ASSERT(0);
  1955. break;
  1956. }
  1957. bp->b_ops = &xfs_attr3_rmt_buf_ops;
  1958. break;
  1959. case XFS_BLFT_SB_BUF:
  1960. if (magic32 != XFS_SB_MAGIC) {
  1961. xfs_warn(mp, "Bad SB block magic!");
  1962. ASSERT(0);
  1963. break;
  1964. }
  1965. bp->b_ops = &xfs_sb_buf_ops;
  1966. break;
  1967. default:
  1968. xfs_warn(mp, "Unknown buffer type %d!",
  1969. xfs_blft_from_flags(buf_f));
  1970. break;
  1971. }
  1972. }
  1973. /*
  1974. * Perform a 'normal' buffer recovery. Each logged region of the
  1975. * buffer should be copied over the corresponding region in the
  1976. * given buffer. The bitmap in the buf log format structure indicates
  1977. * where to place the logged data.
  1978. */
  1979. STATIC void
  1980. xlog_recover_do_reg_buffer(
  1981. struct xfs_mount *mp,
  1982. xlog_recover_item_t *item,
  1983. struct xfs_buf *bp,
  1984. xfs_buf_log_format_t *buf_f)
  1985. {
  1986. int i;
  1987. int bit;
  1988. int nbits;
  1989. int error;
  1990. trace_xfs_log_recover_buf_reg_buf(mp->m_log, buf_f);
  1991. bit = 0;
  1992. i = 1; /* 0 is the buf format structure */
  1993. while (1) {
  1994. bit = xfs_next_bit(buf_f->blf_data_map,
  1995. buf_f->blf_map_size, bit);
  1996. if (bit == -1)
  1997. break;
  1998. nbits = xfs_contig_bits(buf_f->blf_data_map,
  1999. buf_f->blf_map_size, bit);
  2000. ASSERT(nbits > 0);
  2001. ASSERT(item->ri_buf[i].i_addr != NULL);
  2002. ASSERT(item->ri_buf[i].i_len % XFS_BLF_CHUNK == 0);
  2003. ASSERT(BBTOB(bp->b_io_length) >=
  2004. ((uint)bit << XFS_BLF_SHIFT) + (nbits << XFS_BLF_SHIFT));
  2005. /*
  2006. * The dirty regions logged in the buffer, even though
  2007. * contiguous, may span multiple chunks. This is because the
  2008. * dirty region may span a physical page boundary in a buffer
  2009. * and hence be split into two separate vectors for writing into
  2010. * the log. Hence we need to trim nbits back to the length of
  2011. * the current region being copied out of the log.
  2012. */
  2013. if (item->ri_buf[i].i_len < (nbits << XFS_BLF_SHIFT))
  2014. nbits = item->ri_buf[i].i_len >> XFS_BLF_SHIFT;
  2015. /*
  2016. * Do a sanity check if this is a dquot buffer. Just checking
  2017. * the first dquot in the buffer should do. XXXThis is
  2018. * probably a good thing to do for other buf types also.
  2019. */
  2020. error = 0;
  2021. if (buf_f->blf_flags &
  2022. (XFS_BLF_UDQUOT_BUF|XFS_BLF_PDQUOT_BUF|XFS_BLF_GDQUOT_BUF)) {
  2023. if (item->ri_buf[i].i_addr == NULL) {
  2024. xfs_alert(mp,
  2025. "XFS: NULL dquot in %s.", __func__);
  2026. goto next;
  2027. }
  2028. if (item->ri_buf[i].i_len < sizeof(xfs_disk_dquot_t)) {
  2029. xfs_alert(mp,
  2030. "XFS: dquot too small (%d) in %s.",
  2031. item->ri_buf[i].i_len, __func__);
  2032. goto next;
  2033. }
  2034. error = xfs_dqcheck(mp, item->ri_buf[i].i_addr,
  2035. -1, 0, XFS_QMOPT_DOWARN,
  2036. "dquot_buf_recover");
  2037. if (error)
  2038. goto next;
  2039. }
  2040. memcpy(xfs_buf_offset(bp,
  2041. (uint)bit << XFS_BLF_SHIFT), /* dest */
  2042. item->ri_buf[i].i_addr, /* source */
  2043. nbits<<XFS_BLF_SHIFT); /* length */
  2044. next:
  2045. i++;
  2046. bit += nbits;
  2047. }
  2048. /* Shouldn't be any more regions */
  2049. ASSERT(i == item->ri_total);
  2050. xlog_recover_validate_buf_type(mp, bp, buf_f);
  2051. }
  2052. /*
  2053. * Perform a dquot buffer recovery.
  2054. * Simple algorithm: if we have found a QUOTAOFF log item of the same type
  2055. * (ie. USR or GRP), then just toss this buffer away; don't recover it.
  2056. * Else, treat it as a regular buffer and do recovery.
  2057. *
  2058. * Return false if the buffer was tossed and true if we recovered the buffer to
  2059. * indicate to the caller if the buffer needs writing.
  2060. */
  2061. STATIC bool
  2062. xlog_recover_do_dquot_buffer(
  2063. struct xfs_mount *mp,
  2064. struct xlog *log,
  2065. struct xlog_recover_item *item,
  2066. struct xfs_buf *bp,
  2067. struct xfs_buf_log_format *buf_f)
  2068. {
  2069. uint type;
  2070. trace_xfs_log_recover_buf_dquot_buf(log, buf_f);
  2071. /*
  2072. * Filesystems are required to send in quota flags at mount time.
  2073. */
  2074. if (!mp->m_qflags)
  2075. return false;
  2076. type = 0;
  2077. if (buf_f->blf_flags & XFS_BLF_UDQUOT_BUF)
  2078. type |= XFS_DQ_USER;
  2079. if (buf_f->blf_flags & XFS_BLF_PDQUOT_BUF)
  2080. type |= XFS_DQ_PROJ;
  2081. if (buf_f->blf_flags & XFS_BLF_GDQUOT_BUF)
  2082. type |= XFS_DQ_GROUP;
  2083. /*
  2084. * This type of quotas was turned off, so ignore this buffer
  2085. */
  2086. if (log->l_quotaoffs_flag & type)
  2087. return false;
  2088. xlog_recover_do_reg_buffer(mp, item, bp, buf_f);
  2089. return true;
  2090. }
  2091. /*
  2092. * This routine replays a modification made to a buffer at runtime.
  2093. * There are actually two types of buffer, regular and inode, which
  2094. * are handled differently. Inode buffers are handled differently
  2095. * in that we only recover a specific set of data from them, namely
  2096. * the inode di_next_unlinked fields. This is because all other inode
  2097. * data is actually logged via inode records and any data we replay
  2098. * here which overlaps that may be stale.
  2099. *
  2100. * When meta-data buffers are freed at run time we log a buffer item
  2101. * with the XFS_BLF_CANCEL bit set to indicate that previous copies
  2102. * of the buffer in the log should not be replayed at recovery time.
  2103. * This is so that if the blocks covered by the buffer are reused for
  2104. * file data before we crash we don't end up replaying old, freed
  2105. * meta-data into a user's file.
  2106. *
  2107. * To handle the cancellation of buffer log items, we make two passes
  2108. * over the log during recovery. During the first we build a table of
  2109. * those buffers which have been cancelled, and during the second we
  2110. * only replay those buffers which do not have corresponding cancel
  2111. * records in the table. See xlog_recover_buffer_pass[1,2] above
  2112. * for more details on the implementation of the table of cancel records.
  2113. */
  2114. STATIC int
  2115. xlog_recover_buffer_pass2(
  2116. struct xlog *log,
  2117. struct list_head *buffer_list,
  2118. struct xlog_recover_item *item,
  2119. xfs_lsn_t current_lsn)
  2120. {
  2121. xfs_buf_log_format_t *buf_f = item->ri_buf[0].i_addr;
  2122. xfs_mount_t *mp = log->l_mp;
  2123. xfs_buf_t *bp;
  2124. int error;
  2125. uint buf_flags;
  2126. xfs_lsn_t lsn;
  2127. /*
  2128. * In this pass we only want to recover all the buffers which have
  2129. * not been cancelled and are not cancellation buffers themselves.
  2130. */
  2131. if (xlog_check_buffer_cancelled(log, buf_f->blf_blkno,
  2132. buf_f->blf_len, buf_f->blf_flags)) {
  2133. trace_xfs_log_recover_buf_cancel(log, buf_f);
  2134. return 0;
  2135. }
  2136. trace_xfs_log_recover_buf_recover(log, buf_f);
  2137. buf_flags = 0;
  2138. if (buf_f->blf_flags & XFS_BLF_INODE_BUF)
  2139. buf_flags |= XBF_UNMAPPED;
  2140. bp = xfs_buf_read(mp->m_ddev_targp, buf_f->blf_blkno, buf_f->blf_len,
  2141. buf_flags, NULL);
  2142. if (!bp)
  2143. return -ENOMEM;
  2144. error = bp->b_error;
  2145. if (error) {
  2146. xfs_buf_ioerror_alert(bp, "xlog_recover_do..(read#1)");
  2147. goto out_release;
  2148. }
  2149. /*
  2150. * Recover the buffer only if we get an LSN from it and it's less than
  2151. * the lsn of the transaction we are replaying.
  2152. *
  2153. * Note that we have to be extremely careful of readahead here.
  2154. * Readahead does not attach verfiers to the buffers so if we don't
  2155. * actually do any replay after readahead because of the LSN we found
  2156. * in the buffer if more recent than that current transaction then we
  2157. * need to attach the verifier directly. Failure to do so can lead to
  2158. * future recovery actions (e.g. EFI and unlinked list recovery) can
  2159. * operate on the buffers and they won't get the verifier attached. This
  2160. * can lead to blocks on disk having the correct content but a stale
  2161. * CRC.
  2162. *
  2163. * It is safe to assume these clean buffers are currently up to date.
  2164. * If the buffer is dirtied by a later transaction being replayed, then
  2165. * the verifier will be reset to match whatever recover turns that
  2166. * buffer into.
  2167. */
  2168. lsn = xlog_recover_get_buf_lsn(mp, bp);
  2169. if (lsn && lsn != -1 && XFS_LSN_CMP(lsn, current_lsn) >= 0) {
  2170. xlog_recover_validate_buf_type(mp, bp, buf_f);
  2171. goto out_release;
  2172. }
  2173. if (buf_f->blf_flags & XFS_BLF_INODE_BUF) {
  2174. error = xlog_recover_do_inode_buffer(mp, item, bp, buf_f);
  2175. if (error)
  2176. goto out_release;
  2177. } else if (buf_f->blf_flags &
  2178. (XFS_BLF_UDQUOT_BUF|XFS_BLF_PDQUOT_BUF|XFS_BLF_GDQUOT_BUF)) {
  2179. bool dirty;
  2180. dirty = xlog_recover_do_dquot_buffer(mp, log, item, bp, buf_f);
  2181. if (!dirty)
  2182. goto out_release;
  2183. } else {
  2184. xlog_recover_do_reg_buffer(mp, item, bp, buf_f);
  2185. }
  2186. /*
  2187. * Perform delayed write on the buffer. Asynchronous writes will be
  2188. * slower when taking into account all the buffers to be flushed.
  2189. *
  2190. * Also make sure that only inode buffers with good sizes stay in
  2191. * the buffer cache. The kernel moves inodes in buffers of 1 block
  2192. * or mp->m_inode_cluster_size bytes, whichever is bigger. The inode
  2193. * buffers in the log can be a different size if the log was generated
  2194. * by an older kernel using unclustered inode buffers or a newer kernel
  2195. * running with a different inode cluster size. Regardless, if the
  2196. * the inode buffer size isn't MAX(blocksize, mp->m_inode_cluster_size)
  2197. * for *our* value of mp->m_inode_cluster_size, then we need to keep
  2198. * the buffer out of the buffer cache so that the buffer won't
  2199. * overlap with future reads of those inodes.
  2200. */
  2201. if (XFS_DINODE_MAGIC ==
  2202. be16_to_cpu(*((__be16 *)xfs_buf_offset(bp, 0))) &&
  2203. (BBTOB(bp->b_io_length) != MAX(log->l_mp->m_sb.sb_blocksize,
  2204. (__uint32_t)log->l_mp->m_inode_cluster_size))) {
  2205. xfs_buf_stale(bp);
  2206. error = xfs_bwrite(bp);
  2207. } else {
  2208. ASSERT(bp->b_target->bt_mount == mp);
  2209. bp->b_iodone = xlog_recover_iodone;
  2210. xfs_buf_delwri_queue(bp, buffer_list);
  2211. }
  2212. out_release:
  2213. xfs_buf_relse(bp);
  2214. return error;
  2215. }
  2216. /*
  2217. * Inode fork owner changes
  2218. *
  2219. * If we have been told that we have to reparent the inode fork, it's because an
  2220. * extent swap operation on a CRC enabled filesystem has been done and we are
  2221. * replaying it. We need to walk the BMBT of the appropriate fork and change the
  2222. * owners of it.
  2223. *
  2224. * The complexity here is that we don't have an inode context to work with, so
  2225. * after we've replayed the inode we need to instantiate one. This is where the
  2226. * fun begins.
  2227. *
  2228. * We are in the middle of log recovery, so we can't run transactions. That
  2229. * means we cannot use cache coherent inode instantiation via xfs_iget(), as
  2230. * that will result in the corresponding iput() running the inode through
  2231. * xfs_inactive(). If we've just replayed an inode core that changes the link
  2232. * count to zero (i.e. it's been unlinked), then xfs_inactive() will run
  2233. * transactions (bad!).
  2234. *
  2235. * So, to avoid this, we instantiate an inode directly from the inode core we've
  2236. * just recovered. We have the buffer still locked, and all we really need to
  2237. * instantiate is the inode core and the forks being modified. We can do this
  2238. * manually, then run the inode btree owner change, and then tear down the
  2239. * xfs_inode without having to run any transactions at all.
  2240. *
  2241. * Also, because we don't have a transaction context available here but need to
  2242. * gather all the buffers we modify for writeback so we pass the buffer_list
  2243. * instead for the operation to use.
  2244. */
  2245. STATIC int
  2246. xfs_recover_inode_owner_change(
  2247. struct xfs_mount *mp,
  2248. struct xfs_dinode *dip,
  2249. struct xfs_inode_log_format *in_f,
  2250. struct list_head *buffer_list)
  2251. {
  2252. struct xfs_inode *ip;
  2253. int error;
  2254. ASSERT(in_f->ilf_fields & (XFS_ILOG_DOWNER|XFS_ILOG_AOWNER));
  2255. ip = xfs_inode_alloc(mp, in_f->ilf_ino);
  2256. if (!ip)
  2257. return -ENOMEM;
  2258. /* instantiate the inode */
  2259. xfs_dinode_from_disk(&ip->i_d, dip);
  2260. ASSERT(ip->i_d.di_version >= 3);
  2261. error = xfs_iformat_fork(ip, dip);
  2262. if (error)
  2263. goto out_free_ip;
  2264. if (in_f->ilf_fields & XFS_ILOG_DOWNER) {
  2265. ASSERT(in_f->ilf_fields & XFS_ILOG_DBROOT);
  2266. error = xfs_bmbt_change_owner(NULL, ip, XFS_DATA_FORK,
  2267. ip->i_ino, buffer_list);
  2268. if (error)
  2269. goto out_free_ip;
  2270. }
  2271. if (in_f->ilf_fields & XFS_ILOG_AOWNER) {
  2272. ASSERT(in_f->ilf_fields & XFS_ILOG_ABROOT);
  2273. error = xfs_bmbt_change_owner(NULL, ip, XFS_ATTR_FORK,
  2274. ip->i_ino, buffer_list);
  2275. if (error)
  2276. goto out_free_ip;
  2277. }
  2278. out_free_ip:
  2279. xfs_inode_free(ip);
  2280. return error;
  2281. }
  2282. STATIC int
  2283. xlog_recover_inode_pass2(
  2284. struct xlog *log,
  2285. struct list_head *buffer_list,
  2286. struct xlog_recover_item *item,
  2287. xfs_lsn_t current_lsn)
  2288. {
  2289. xfs_inode_log_format_t *in_f;
  2290. xfs_mount_t *mp = log->l_mp;
  2291. xfs_buf_t *bp;
  2292. xfs_dinode_t *dip;
  2293. int len;
  2294. xfs_caddr_t src;
  2295. xfs_caddr_t dest;
  2296. int error;
  2297. int attr_index;
  2298. uint fields;
  2299. xfs_icdinode_t *dicp;
  2300. uint isize;
  2301. int need_free = 0;
  2302. if (item->ri_buf[0].i_len == sizeof(xfs_inode_log_format_t)) {
  2303. in_f = item->ri_buf[0].i_addr;
  2304. } else {
  2305. in_f = kmem_alloc(sizeof(xfs_inode_log_format_t), KM_SLEEP);
  2306. need_free = 1;
  2307. error = xfs_inode_item_format_convert(&item->ri_buf[0], in_f);
  2308. if (error)
  2309. goto error;
  2310. }
  2311. /*
  2312. * Inode buffers can be freed, look out for it,
  2313. * and do not replay the inode.
  2314. */
  2315. if (xlog_check_buffer_cancelled(log, in_f->ilf_blkno,
  2316. in_f->ilf_len, 0)) {
  2317. error = 0;
  2318. trace_xfs_log_recover_inode_cancel(log, in_f);
  2319. goto error;
  2320. }
  2321. trace_xfs_log_recover_inode_recover(log, in_f);
  2322. bp = xfs_buf_read(mp->m_ddev_targp, in_f->ilf_blkno, in_f->ilf_len, 0,
  2323. &xfs_inode_buf_ops);
  2324. if (!bp) {
  2325. error = -ENOMEM;
  2326. goto error;
  2327. }
  2328. error = bp->b_error;
  2329. if (error) {
  2330. xfs_buf_ioerror_alert(bp, "xlog_recover_do..(read#2)");
  2331. goto out_release;
  2332. }
  2333. ASSERT(in_f->ilf_fields & XFS_ILOG_CORE);
  2334. dip = (xfs_dinode_t *)xfs_buf_offset(bp, in_f->ilf_boffset);
  2335. /*
  2336. * Make sure the place we're flushing out to really looks
  2337. * like an inode!
  2338. */
  2339. if (unlikely(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC))) {
  2340. xfs_alert(mp,
  2341. "%s: Bad inode magic number, dip = 0x%p, dino bp = 0x%p, ino = %Ld",
  2342. __func__, dip, bp, in_f->ilf_ino);
  2343. XFS_ERROR_REPORT("xlog_recover_inode_pass2(1)",
  2344. XFS_ERRLEVEL_LOW, mp);
  2345. error = -EFSCORRUPTED;
  2346. goto out_release;
  2347. }
  2348. dicp = item->ri_buf[1].i_addr;
  2349. if (unlikely(dicp->di_magic != XFS_DINODE_MAGIC)) {
  2350. xfs_alert(mp,
  2351. "%s: Bad inode log record, rec ptr 0x%p, ino %Ld",
  2352. __func__, item, in_f->ilf_ino);
  2353. XFS_ERROR_REPORT("xlog_recover_inode_pass2(2)",
  2354. XFS_ERRLEVEL_LOW, mp);
  2355. error = -EFSCORRUPTED;
  2356. goto out_release;
  2357. }
  2358. /*
  2359. * If the inode has an LSN in it, recover the inode only if it's less
  2360. * than the lsn of the transaction we are replaying. Note: we still
  2361. * need to replay an owner change even though the inode is more recent
  2362. * than the transaction as there is no guarantee that all the btree
  2363. * blocks are more recent than this transaction, too.
  2364. */
  2365. if (dip->di_version >= 3) {
  2366. xfs_lsn_t lsn = be64_to_cpu(dip->di_lsn);
  2367. if (lsn && lsn != -1 && XFS_LSN_CMP(lsn, current_lsn) >= 0) {
  2368. trace_xfs_log_recover_inode_skip(log, in_f);
  2369. error = 0;
  2370. goto out_owner_change;
  2371. }
  2372. }
  2373. /*
  2374. * di_flushiter is only valid for v1/2 inodes. All changes for v3 inodes
  2375. * are transactional and if ordering is necessary we can determine that
  2376. * more accurately by the LSN field in the V3 inode core. Don't trust
  2377. * the inode versions we might be changing them here - use the
  2378. * superblock flag to determine whether we need to look at di_flushiter
  2379. * to skip replay when the on disk inode is newer than the log one
  2380. */
  2381. if (!xfs_sb_version_hascrc(&mp->m_sb) &&
  2382. dicp->di_flushiter < be16_to_cpu(dip->di_flushiter)) {
  2383. /*
  2384. * Deal with the wrap case, DI_MAX_FLUSH is less
  2385. * than smaller numbers
  2386. */
  2387. if (be16_to_cpu(dip->di_flushiter) == DI_MAX_FLUSH &&
  2388. dicp->di_flushiter < (DI_MAX_FLUSH >> 1)) {
  2389. /* do nothing */
  2390. } else {
  2391. trace_xfs_log_recover_inode_skip(log, in_f);
  2392. error = 0;
  2393. goto out_release;
  2394. }
  2395. }
  2396. /* Take the opportunity to reset the flush iteration count */
  2397. dicp->di_flushiter = 0;
  2398. if (unlikely(S_ISREG(dicp->di_mode))) {
  2399. if ((dicp->di_format != XFS_DINODE_FMT_EXTENTS) &&
  2400. (dicp->di_format != XFS_DINODE_FMT_BTREE)) {
  2401. XFS_CORRUPTION_ERROR("xlog_recover_inode_pass2(3)",
  2402. XFS_ERRLEVEL_LOW, mp, dicp);
  2403. xfs_alert(mp,
  2404. "%s: Bad regular inode log record, rec ptr 0x%p, "
  2405. "ino ptr = 0x%p, ino bp = 0x%p, ino %Ld",
  2406. __func__, item, dip, bp, in_f->ilf_ino);
  2407. error = -EFSCORRUPTED;
  2408. goto out_release;
  2409. }
  2410. } else if (unlikely(S_ISDIR(dicp->di_mode))) {
  2411. if ((dicp->di_format != XFS_DINODE_FMT_EXTENTS) &&
  2412. (dicp->di_format != XFS_DINODE_FMT_BTREE) &&
  2413. (dicp->di_format != XFS_DINODE_FMT_LOCAL)) {
  2414. XFS_CORRUPTION_ERROR("xlog_recover_inode_pass2(4)",
  2415. XFS_ERRLEVEL_LOW, mp, dicp);
  2416. xfs_alert(mp,
  2417. "%s: Bad dir inode log record, rec ptr 0x%p, "
  2418. "ino ptr = 0x%p, ino bp = 0x%p, ino %Ld",
  2419. __func__, item, dip, bp, in_f->ilf_ino);
  2420. error = -EFSCORRUPTED;
  2421. goto out_release;
  2422. }
  2423. }
  2424. if (unlikely(dicp->di_nextents + dicp->di_anextents > dicp->di_nblocks)){
  2425. XFS_CORRUPTION_ERROR("xlog_recover_inode_pass2(5)",
  2426. XFS_ERRLEVEL_LOW, mp, dicp);
  2427. xfs_alert(mp,
  2428. "%s: Bad inode log record, rec ptr 0x%p, dino ptr 0x%p, "
  2429. "dino bp 0x%p, ino %Ld, total extents = %d, nblocks = %Ld",
  2430. __func__, item, dip, bp, in_f->ilf_ino,
  2431. dicp->di_nextents + dicp->di_anextents,
  2432. dicp->di_nblocks);
  2433. error = -EFSCORRUPTED;
  2434. goto out_release;
  2435. }
  2436. if (unlikely(dicp->di_forkoff > mp->m_sb.sb_inodesize)) {
  2437. XFS_CORRUPTION_ERROR("xlog_recover_inode_pass2(6)",
  2438. XFS_ERRLEVEL_LOW, mp, dicp);
  2439. xfs_alert(mp,
  2440. "%s: Bad inode log record, rec ptr 0x%p, dino ptr 0x%p, "
  2441. "dino bp 0x%p, ino %Ld, forkoff 0x%x", __func__,
  2442. item, dip, bp, in_f->ilf_ino, dicp->di_forkoff);
  2443. error = -EFSCORRUPTED;
  2444. goto out_release;
  2445. }
  2446. isize = xfs_icdinode_size(dicp->di_version);
  2447. if (unlikely(item->ri_buf[1].i_len > isize)) {
  2448. XFS_CORRUPTION_ERROR("xlog_recover_inode_pass2(7)",
  2449. XFS_ERRLEVEL_LOW, mp, dicp);
  2450. xfs_alert(mp,
  2451. "%s: Bad inode log record length %d, rec ptr 0x%p",
  2452. __func__, item->ri_buf[1].i_len, item);
  2453. error = -EFSCORRUPTED;
  2454. goto out_release;
  2455. }
  2456. /* The core is in in-core format */
  2457. xfs_dinode_to_disk(dip, dicp);
  2458. /* the rest is in on-disk format */
  2459. if (item->ri_buf[1].i_len > isize) {
  2460. memcpy((char *)dip + isize,
  2461. item->ri_buf[1].i_addr + isize,
  2462. item->ri_buf[1].i_len - isize);
  2463. }
  2464. fields = in_f->ilf_fields;
  2465. switch (fields & (XFS_ILOG_DEV | XFS_ILOG_UUID)) {
  2466. case XFS_ILOG_DEV:
  2467. xfs_dinode_put_rdev(dip, in_f->ilf_u.ilfu_rdev);
  2468. break;
  2469. case XFS_ILOG_UUID:
  2470. memcpy(XFS_DFORK_DPTR(dip),
  2471. &in_f->ilf_u.ilfu_uuid,
  2472. sizeof(uuid_t));
  2473. break;
  2474. }
  2475. if (in_f->ilf_size == 2)
  2476. goto out_owner_change;
  2477. len = item->ri_buf[2].i_len;
  2478. src = item->ri_buf[2].i_addr;
  2479. ASSERT(in_f->ilf_size <= 4);
  2480. ASSERT((in_f->ilf_size == 3) || (fields & XFS_ILOG_AFORK));
  2481. ASSERT(!(fields & XFS_ILOG_DFORK) ||
  2482. (len == in_f->ilf_dsize));
  2483. switch (fields & XFS_ILOG_DFORK) {
  2484. case XFS_ILOG_DDATA:
  2485. case XFS_ILOG_DEXT:
  2486. memcpy(XFS_DFORK_DPTR(dip), src, len);
  2487. break;
  2488. case XFS_ILOG_DBROOT:
  2489. xfs_bmbt_to_bmdr(mp, (struct xfs_btree_block *)src, len,
  2490. (xfs_bmdr_block_t *)XFS_DFORK_DPTR(dip),
  2491. XFS_DFORK_DSIZE(dip, mp));
  2492. break;
  2493. default:
  2494. /*
  2495. * There are no data fork flags set.
  2496. */
  2497. ASSERT((fields & XFS_ILOG_DFORK) == 0);
  2498. break;
  2499. }
  2500. /*
  2501. * If we logged any attribute data, recover it. There may or
  2502. * may not have been any other non-core data logged in this
  2503. * transaction.
  2504. */
  2505. if (in_f->ilf_fields & XFS_ILOG_AFORK) {
  2506. if (in_f->ilf_fields & XFS_ILOG_DFORK) {
  2507. attr_index = 3;
  2508. } else {
  2509. attr_index = 2;
  2510. }
  2511. len = item->ri_buf[attr_index].i_len;
  2512. src = item->ri_buf[attr_index].i_addr;
  2513. ASSERT(len == in_f->ilf_asize);
  2514. switch (in_f->ilf_fields & XFS_ILOG_AFORK) {
  2515. case XFS_ILOG_ADATA:
  2516. case XFS_ILOG_AEXT:
  2517. dest = XFS_DFORK_APTR(dip);
  2518. ASSERT(len <= XFS_DFORK_ASIZE(dip, mp));
  2519. memcpy(dest, src, len);
  2520. break;
  2521. case XFS_ILOG_ABROOT:
  2522. dest = XFS_DFORK_APTR(dip);
  2523. xfs_bmbt_to_bmdr(mp, (struct xfs_btree_block *)src,
  2524. len, (xfs_bmdr_block_t*)dest,
  2525. XFS_DFORK_ASIZE(dip, mp));
  2526. break;
  2527. default:
  2528. xfs_warn(log->l_mp, "%s: Invalid flag", __func__);
  2529. ASSERT(0);
  2530. error = -EIO;
  2531. goto out_release;
  2532. }
  2533. }
  2534. out_owner_change:
  2535. if (in_f->ilf_fields & (XFS_ILOG_DOWNER|XFS_ILOG_AOWNER))
  2536. error = xfs_recover_inode_owner_change(mp, dip, in_f,
  2537. buffer_list);
  2538. /* re-generate the checksum. */
  2539. xfs_dinode_calc_crc(log->l_mp, dip);
  2540. ASSERT(bp->b_target->bt_mount == mp);
  2541. bp->b_iodone = xlog_recover_iodone;
  2542. xfs_buf_delwri_queue(bp, buffer_list);
  2543. out_release:
  2544. xfs_buf_relse(bp);
  2545. error:
  2546. if (need_free)
  2547. kmem_free(in_f);
  2548. return error;
  2549. }
  2550. /*
  2551. * Recover QUOTAOFF records. We simply make a note of it in the xlog
  2552. * structure, so that we know not to do any dquot item or dquot buffer recovery,
  2553. * of that type.
  2554. */
  2555. STATIC int
  2556. xlog_recover_quotaoff_pass1(
  2557. struct xlog *log,
  2558. struct xlog_recover_item *item)
  2559. {
  2560. xfs_qoff_logformat_t *qoff_f = item->ri_buf[0].i_addr;
  2561. ASSERT(qoff_f);
  2562. /*
  2563. * The logitem format's flag tells us if this was user quotaoff,
  2564. * group/project quotaoff or both.
  2565. */
  2566. if (qoff_f->qf_flags & XFS_UQUOTA_ACCT)
  2567. log->l_quotaoffs_flag |= XFS_DQ_USER;
  2568. if (qoff_f->qf_flags & XFS_PQUOTA_ACCT)
  2569. log->l_quotaoffs_flag |= XFS_DQ_PROJ;
  2570. if (qoff_f->qf_flags & XFS_GQUOTA_ACCT)
  2571. log->l_quotaoffs_flag |= XFS_DQ_GROUP;
  2572. return 0;
  2573. }
  2574. /*
  2575. * Recover a dquot record
  2576. */
  2577. STATIC int
  2578. xlog_recover_dquot_pass2(
  2579. struct xlog *log,
  2580. struct list_head *buffer_list,
  2581. struct xlog_recover_item *item,
  2582. xfs_lsn_t current_lsn)
  2583. {
  2584. xfs_mount_t *mp = log->l_mp;
  2585. xfs_buf_t *bp;
  2586. struct xfs_disk_dquot *ddq, *recddq;
  2587. int error;
  2588. xfs_dq_logformat_t *dq_f;
  2589. uint type;
  2590. /*
  2591. * Filesystems are required to send in quota flags at mount time.
  2592. */
  2593. if (mp->m_qflags == 0)
  2594. return 0;
  2595. recddq = item->ri_buf[1].i_addr;
  2596. if (recddq == NULL) {
  2597. xfs_alert(log->l_mp, "NULL dquot in %s.", __func__);
  2598. return -EIO;
  2599. }
  2600. if (item->ri_buf[1].i_len < sizeof(xfs_disk_dquot_t)) {
  2601. xfs_alert(log->l_mp, "dquot too small (%d) in %s.",
  2602. item->ri_buf[1].i_len, __func__);
  2603. return -EIO;
  2604. }
  2605. /*
  2606. * This type of quotas was turned off, so ignore this record.
  2607. */
  2608. type = recddq->d_flags & (XFS_DQ_USER | XFS_DQ_PROJ | XFS_DQ_GROUP);
  2609. ASSERT(type);
  2610. if (log->l_quotaoffs_flag & type)
  2611. return 0;
  2612. /*
  2613. * At this point we know that quota was _not_ turned off.
  2614. * Since the mount flags are not indicating to us otherwise, this
  2615. * must mean that quota is on, and the dquot needs to be replayed.
  2616. * Remember that we may not have fully recovered the superblock yet,
  2617. * so we can't do the usual trick of looking at the SB quota bits.
  2618. *
  2619. * The other possibility, of course, is that the quota subsystem was
  2620. * removed since the last mount - ENOSYS.
  2621. */
  2622. dq_f = item->ri_buf[0].i_addr;
  2623. ASSERT(dq_f);
  2624. error = xfs_dqcheck(mp, recddq, dq_f->qlf_id, 0, XFS_QMOPT_DOWARN,
  2625. "xlog_recover_dquot_pass2 (log copy)");
  2626. if (error)
  2627. return -EIO;
  2628. ASSERT(dq_f->qlf_len == 1);
  2629. /*
  2630. * At this point we are assuming that the dquots have been allocated
  2631. * and hence the buffer has valid dquots stamped in it. It should,
  2632. * therefore, pass verifier validation. If the dquot is bad, then the
  2633. * we'll return an error here, so we don't need to specifically check
  2634. * the dquot in the buffer after the verifier has run.
  2635. */
  2636. error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dq_f->qlf_blkno,
  2637. XFS_FSB_TO_BB(mp, dq_f->qlf_len), 0, &bp,
  2638. &xfs_dquot_buf_ops);
  2639. if (error)
  2640. return error;
  2641. ASSERT(bp);
  2642. ddq = (xfs_disk_dquot_t *)xfs_buf_offset(bp, dq_f->qlf_boffset);
  2643. /*
  2644. * If the dquot has an LSN in it, recover the dquot only if it's less
  2645. * than the lsn of the transaction we are replaying.
  2646. */
  2647. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  2648. struct xfs_dqblk *dqb = (struct xfs_dqblk *)ddq;
  2649. xfs_lsn_t lsn = be64_to_cpu(dqb->dd_lsn);
  2650. if (lsn && lsn != -1 && XFS_LSN_CMP(lsn, current_lsn) >= 0) {
  2651. goto out_release;
  2652. }
  2653. }
  2654. memcpy(ddq, recddq, item->ri_buf[1].i_len);
  2655. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  2656. xfs_update_cksum((char *)ddq, sizeof(struct xfs_dqblk),
  2657. XFS_DQUOT_CRC_OFF);
  2658. }
  2659. ASSERT(dq_f->qlf_size == 2);
  2660. ASSERT(bp->b_target->bt_mount == mp);
  2661. bp->b_iodone = xlog_recover_iodone;
  2662. xfs_buf_delwri_queue(bp, buffer_list);
  2663. out_release:
  2664. xfs_buf_relse(bp);
  2665. return 0;
  2666. }
  2667. /*
  2668. * This routine is called to create an in-core extent free intent
  2669. * item from the efi format structure which was logged on disk.
  2670. * It allocates an in-core efi, copies the extents from the format
  2671. * structure into it, and adds the efi to the AIL with the given
  2672. * LSN.
  2673. */
  2674. STATIC int
  2675. xlog_recover_efi_pass2(
  2676. struct xlog *log,
  2677. struct xlog_recover_item *item,
  2678. xfs_lsn_t lsn)
  2679. {
  2680. int error;
  2681. xfs_mount_t *mp = log->l_mp;
  2682. xfs_efi_log_item_t *efip;
  2683. xfs_efi_log_format_t *efi_formatp;
  2684. efi_formatp = item->ri_buf[0].i_addr;
  2685. efip = xfs_efi_init(mp, efi_formatp->efi_nextents);
  2686. if ((error = xfs_efi_copy_format(&(item->ri_buf[0]),
  2687. &(efip->efi_format)))) {
  2688. xfs_efi_item_free(efip);
  2689. return error;
  2690. }
  2691. atomic_set(&efip->efi_next_extent, efi_formatp->efi_nextents);
  2692. spin_lock(&log->l_ailp->xa_lock);
  2693. /*
  2694. * xfs_trans_ail_update() drops the AIL lock.
  2695. */
  2696. xfs_trans_ail_update(log->l_ailp, &efip->efi_item, lsn);
  2697. return 0;
  2698. }
  2699. /*
  2700. * This routine is called when an efd format structure is found in
  2701. * a committed transaction in the log. It's purpose is to cancel
  2702. * the corresponding efi if it was still in the log. To do this
  2703. * it searches the AIL for the efi with an id equal to that in the
  2704. * efd format structure. If we find it, we remove the efi from the
  2705. * AIL and free it.
  2706. */
  2707. STATIC int
  2708. xlog_recover_efd_pass2(
  2709. struct xlog *log,
  2710. struct xlog_recover_item *item)
  2711. {
  2712. xfs_efd_log_format_t *efd_formatp;
  2713. xfs_efi_log_item_t *efip = NULL;
  2714. xfs_log_item_t *lip;
  2715. __uint64_t efi_id;
  2716. struct xfs_ail_cursor cur;
  2717. struct xfs_ail *ailp = log->l_ailp;
  2718. efd_formatp = item->ri_buf[0].i_addr;
  2719. ASSERT((item->ri_buf[0].i_len == (sizeof(xfs_efd_log_format_32_t) +
  2720. ((efd_formatp->efd_nextents - 1) * sizeof(xfs_extent_32_t)))) ||
  2721. (item->ri_buf[0].i_len == (sizeof(xfs_efd_log_format_64_t) +
  2722. ((efd_formatp->efd_nextents - 1) * sizeof(xfs_extent_64_t)))));
  2723. efi_id = efd_formatp->efd_efi_id;
  2724. /*
  2725. * Search for the efi with the id in the efd format structure
  2726. * in the AIL.
  2727. */
  2728. spin_lock(&ailp->xa_lock);
  2729. lip = xfs_trans_ail_cursor_first(ailp, &cur, 0);
  2730. while (lip != NULL) {
  2731. if (lip->li_type == XFS_LI_EFI) {
  2732. efip = (xfs_efi_log_item_t *)lip;
  2733. if (efip->efi_format.efi_id == efi_id) {
  2734. /*
  2735. * xfs_trans_ail_delete() drops the
  2736. * AIL lock.
  2737. */
  2738. xfs_trans_ail_delete(ailp, lip,
  2739. SHUTDOWN_CORRUPT_INCORE);
  2740. xfs_efi_item_free(efip);
  2741. spin_lock(&ailp->xa_lock);
  2742. break;
  2743. }
  2744. }
  2745. lip = xfs_trans_ail_cursor_next(ailp, &cur);
  2746. }
  2747. xfs_trans_ail_cursor_done(&cur);
  2748. spin_unlock(&ailp->xa_lock);
  2749. return 0;
  2750. }
  2751. /*
  2752. * This routine is called when an inode create format structure is found in a
  2753. * committed transaction in the log. It's purpose is to initialise the inodes
  2754. * being allocated on disk. This requires us to get inode cluster buffers that
  2755. * match the range to be intialised, stamped with inode templates and written
  2756. * by delayed write so that subsequent modifications will hit the cached buffer
  2757. * and only need writing out at the end of recovery.
  2758. */
  2759. STATIC int
  2760. xlog_recover_do_icreate_pass2(
  2761. struct xlog *log,
  2762. struct list_head *buffer_list,
  2763. xlog_recover_item_t *item)
  2764. {
  2765. struct xfs_mount *mp = log->l_mp;
  2766. struct xfs_icreate_log *icl;
  2767. xfs_agnumber_t agno;
  2768. xfs_agblock_t agbno;
  2769. unsigned int count;
  2770. unsigned int isize;
  2771. xfs_agblock_t length;
  2772. icl = (struct xfs_icreate_log *)item->ri_buf[0].i_addr;
  2773. if (icl->icl_type != XFS_LI_ICREATE) {
  2774. xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad type");
  2775. return -EINVAL;
  2776. }
  2777. if (icl->icl_size != 1) {
  2778. xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad icl size");
  2779. return -EINVAL;
  2780. }
  2781. agno = be32_to_cpu(icl->icl_ag);
  2782. if (agno >= mp->m_sb.sb_agcount) {
  2783. xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad agno");
  2784. return -EINVAL;
  2785. }
  2786. agbno = be32_to_cpu(icl->icl_agbno);
  2787. if (!agbno || agbno == NULLAGBLOCK || agbno >= mp->m_sb.sb_agblocks) {
  2788. xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad agbno");
  2789. return -EINVAL;
  2790. }
  2791. isize = be32_to_cpu(icl->icl_isize);
  2792. if (isize != mp->m_sb.sb_inodesize) {
  2793. xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad isize");
  2794. return -EINVAL;
  2795. }
  2796. count = be32_to_cpu(icl->icl_count);
  2797. if (!count) {
  2798. xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad count");
  2799. return -EINVAL;
  2800. }
  2801. length = be32_to_cpu(icl->icl_length);
  2802. if (!length || length >= mp->m_sb.sb_agblocks) {
  2803. xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad length");
  2804. return -EINVAL;
  2805. }
  2806. /* existing allocation is fixed value */
  2807. ASSERT(count == mp->m_ialloc_inos);
  2808. ASSERT(length == mp->m_ialloc_blks);
  2809. if (count != mp->m_ialloc_inos ||
  2810. length != mp->m_ialloc_blks) {
  2811. xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad count 2");
  2812. return -EINVAL;
  2813. }
  2814. /*
  2815. * Inode buffers can be freed. Do not replay the inode initialisation as
  2816. * we could be overwriting something written after this inode buffer was
  2817. * cancelled.
  2818. *
  2819. * XXX: we need to iterate all buffers and only init those that are not
  2820. * cancelled. I think that a more fine grained factoring of
  2821. * xfs_ialloc_inode_init may be appropriate here to enable this to be
  2822. * done easily.
  2823. */
  2824. if (xlog_check_buffer_cancelled(log,
  2825. XFS_AGB_TO_DADDR(mp, agno, agbno), length, 0))
  2826. return 0;
  2827. xfs_ialloc_inode_init(mp, NULL, buffer_list, agno, agbno, length,
  2828. be32_to_cpu(icl->icl_gen));
  2829. return 0;
  2830. }
  2831. STATIC void
  2832. xlog_recover_buffer_ra_pass2(
  2833. struct xlog *log,
  2834. struct xlog_recover_item *item)
  2835. {
  2836. struct xfs_buf_log_format *buf_f = item->ri_buf[0].i_addr;
  2837. struct xfs_mount *mp = log->l_mp;
  2838. if (xlog_peek_buffer_cancelled(log, buf_f->blf_blkno,
  2839. buf_f->blf_len, buf_f->blf_flags)) {
  2840. return;
  2841. }
  2842. xfs_buf_readahead(mp->m_ddev_targp, buf_f->blf_blkno,
  2843. buf_f->blf_len, NULL);
  2844. }
  2845. STATIC void
  2846. xlog_recover_inode_ra_pass2(
  2847. struct xlog *log,
  2848. struct xlog_recover_item *item)
  2849. {
  2850. struct xfs_inode_log_format ilf_buf;
  2851. struct xfs_inode_log_format *ilfp;
  2852. struct xfs_mount *mp = log->l_mp;
  2853. int error;
  2854. if (item->ri_buf[0].i_len == sizeof(struct xfs_inode_log_format)) {
  2855. ilfp = item->ri_buf[0].i_addr;
  2856. } else {
  2857. ilfp = &ilf_buf;
  2858. memset(ilfp, 0, sizeof(*ilfp));
  2859. error = xfs_inode_item_format_convert(&item->ri_buf[0], ilfp);
  2860. if (error)
  2861. return;
  2862. }
  2863. if (xlog_peek_buffer_cancelled(log, ilfp->ilf_blkno, ilfp->ilf_len, 0))
  2864. return;
  2865. xfs_buf_readahead(mp->m_ddev_targp, ilfp->ilf_blkno,
  2866. ilfp->ilf_len, &xfs_inode_buf_ra_ops);
  2867. }
  2868. STATIC void
  2869. xlog_recover_dquot_ra_pass2(
  2870. struct xlog *log,
  2871. struct xlog_recover_item *item)
  2872. {
  2873. struct xfs_mount *mp = log->l_mp;
  2874. struct xfs_disk_dquot *recddq;
  2875. struct xfs_dq_logformat *dq_f;
  2876. uint type;
  2877. if (mp->m_qflags == 0)
  2878. return;
  2879. recddq = item->ri_buf[1].i_addr;
  2880. if (recddq == NULL)
  2881. return;
  2882. if (item->ri_buf[1].i_len < sizeof(struct xfs_disk_dquot))
  2883. return;
  2884. type = recddq->d_flags & (XFS_DQ_USER | XFS_DQ_PROJ | XFS_DQ_GROUP);
  2885. ASSERT(type);
  2886. if (log->l_quotaoffs_flag & type)
  2887. return;
  2888. dq_f = item->ri_buf[0].i_addr;
  2889. ASSERT(dq_f);
  2890. ASSERT(dq_f->qlf_len == 1);
  2891. xfs_buf_readahead(mp->m_ddev_targp, dq_f->qlf_blkno,
  2892. XFS_FSB_TO_BB(mp, dq_f->qlf_len), NULL);
  2893. }
  2894. STATIC void
  2895. xlog_recover_ra_pass2(
  2896. struct xlog *log,
  2897. struct xlog_recover_item *item)
  2898. {
  2899. switch (ITEM_TYPE(item)) {
  2900. case XFS_LI_BUF:
  2901. xlog_recover_buffer_ra_pass2(log, item);
  2902. break;
  2903. case XFS_LI_INODE:
  2904. xlog_recover_inode_ra_pass2(log, item);
  2905. break;
  2906. case XFS_LI_DQUOT:
  2907. xlog_recover_dquot_ra_pass2(log, item);
  2908. break;
  2909. case XFS_LI_EFI:
  2910. case XFS_LI_EFD:
  2911. case XFS_LI_QUOTAOFF:
  2912. default:
  2913. break;
  2914. }
  2915. }
  2916. STATIC int
  2917. xlog_recover_commit_pass1(
  2918. struct xlog *log,
  2919. struct xlog_recover *trans,
  2920. struct xlog_recover_item *item)
  2921. {
  2922. trace_xfs_log_recover_item_recover(log, trans, item, XLOG_RECOVER_PASS1);
  2923. switch (ITEM_TYPE(item)) {
  2924. case XFS_LI_BUF:
  2925. return xlog_recover_buffer_pass1(log, item);
  2926. case XFS_LI_QUOTAOFF:
  2927. return xlog_recover_quotaoff_pass1(log, item);
  2928. case XFS_LI_INODE:
  2929. case XFS_LI_EFI:
  2930. case XFS_LI_EFD:
  2931. case XFS_LI_DQUOT:
  2932. case XFS_LI_ICREATE:
  2933. /* nothing to do in pass 1 */
  2934. return 0;
  2935. default:
  2936. xfs_warn(log->l_mp, "%s: invalid item type (%d)",
  2937. __func__, ITEM_TYPE(item));
  2938. ASSERT(0);
  2939. return -EIO;
  2940. }
  2941. }
  2942. STATIC int
  2943. xlog_recover_commit_pass2(
  2944. struct xlog *log,
  2945. struct xlog_recover *trans,
  2946. struct list_head *buffer_list,
  2947. struct xlog_recover_item *item)
  2948. {
  2949. trace_xfs_log_recover_item_recover(log, trans, item, XLOG_RECOVER_PASS2);
  2950. switch (ITEM_TYPE(item)) {
  2951. case XFS_LI_BUF:
  2952. return xlog_recover_buffer_pass2(log, buffer_list, item,
  2953. trans->r_lsn);
  2954. case XFS_LI_INODE:
  2955. return xlog_recover_inode_pass2(log, buffer_list, item,
  2956. trans->r_lsn);
  2957. case XFS_LI_EFI:
  2958. return xlog_recover_efi_pass2(log, item, trans->r_lsn);
  2959. case XFS_LI_EFD:
  2960. return xlog_recover_efd_pass2(log, item);
  2961. case XFS_LI_DQUOT:
  2962. return xlog_recover_dquot_pass2(log, buffer_list, item,
  2963. trans->r_lsn);
  2964. case XFS_LI_ICREATE:
  2965. return xlog_recover_do_icreate_pass2(log, buffer_list, item);
  2966. case XFS_LI_QUOTAOFF:
  2967. /* nothing to do in pass2 */
  2968. return 0;
  2969. default:
  2970. xfs_warn(log->l_mp, "%s: invalid item type (%d)",
  2971. __func__, ITEM_TYPE(item));
  2972. ASSERT(0);
  2973. return -EIO;
  2974. }
  2975. }
  2976. STATIC int
  2977. xlog_recover_items_pass2(
  2978. struct xlog *log,
  2979. struct xlog_recover *trans,
  2980. struct list_head *buffer_list,
  2981. struct list_head *item_list)
  2982. {
  2983. struct xlog_recover_item *item;
  2984. int error = 0;
  2985. list_for_each_entry(item, item_list, ri_list) {
  2986. error = xlog_recover_commit_pass2(log, trans,
  2987. buffer_list, item);
  2988. if (error)
  2989. return error;
  2990. }
  2991. return error;
  2992. }
  2993. /*
  2994. * Perform the transaction.
  2995. *
  2996. * If the transaction modifies a buffer or inode, do it now. Otherwise,
  2997. * EFIs and EFDs get queued up by adding entries into the AIL for them.
  2998. */
  2999. STATIC int
  3000. xlog_recover_commit_trans(
  3001. struct xlog *log,
  3002. struct xlog_recover *trans,
  3003. int pass)
  3004. {
  3005. int error = 0;
  3006. int error2;
  3007. int items_queued = 0;
  3008. struct xlog_recover_item *item;
  3009. struct xlog_recover_item *next;
  3010. LIST_HEAD (buffer_list);
  3011. LIST_HEAD (ra_list);
  3012. LIST_HEAD (done_list);
  3013. #define XLOG_RECOVER_COMMIT_QUEUE_MAX 100
  3014. hlist_del(&trans->r_list);
  3015. error = xlog_recover_reorder_trans(log, trans, pass);
  3016. if (error)
  3017. return error;
  3018. list_for_each_entry_safe(item, next, &trans->r_itemq, ri_list) {
  3019. switch (pass) {
  3020. case XLOG_RECOVER_PASS1:
  3021. error = xlog_recover_commit_pass1(log, trans, item);
  3022. break;
  3023. case XLOG_RECOVER_PASS2:
  3024. xlog_recover_ra_pass2(log, item);
  3025. list_move_tail(&item->ri_list, &ra_list);
  3026. items_queued++;
  3027. if (items_queued >= XLOG_RECOVER_COMMIT_QUEUE_MAX) {
  3028. error = xlog_recover_items_pass2(log, trans,
  3029. &buffer_list, &ra_list);
  3030. list_splice_tail_init(&ra_list, &done_list);
  3031. items_queued = 0;
  3032. }
  3033. break;
  3034. default:
  3035. ASSERT(0);
  3036. }
  3037. if (error)
  3038. goto out;
  3039. }
  3040. out:
  3041. if (!list_empty(&ra_list)) {
  3042. if (!error)
  3043. error = xlog_recover_items_pass2(log, trans,
  3044. &buffer_list, &ra_list);
  3045. list_splice_tail_init(&ra_list, &done_list);
  3046. }
  3047. if (!list_empty(&done_list))
  3048. list_splice_init(&done_list, &trans->r_itemq);
  3049. error2 = xfs_buf_delwri_submit(&buffer_list);
  3050. return error ? error : error2;
  3051. }
  3052. STATIC void
  3053. xlog_recover_add_item(
  3054. struct list_head *head)
  3055. {
  3056. xlog_recover_item_t *item;
  3057. item = kmem_zalloc(sizeof(xlog_recover_item_t), KM_SLEEP);
  3058. INIT_LIST_HEAD(&item->ri_list);
  3059. list_add_tail(&item->ri_list, head);
  3060. }
  3061. STATIC int
  3062. xlog_recover_add_to_cont_trans(
  3063. struct xlog *log,
  3064. struct xlog_recover *trans,
  3065. xfs_caddr_t dp,
  3066. int len)
  3067. {
  3068. xlog_recover_item_t *item;
  3069. xfs_caddr_t ptr, old_ptr;
  3070. int old_len;
  3071. if (list_empty(&trans->r_itemq)) {
  3072. /* finish copying rest of trans header */
  3073. xlog_recover_add_item(&trans->r_itemq);
  3074. ptr = (xfs_caddr_t) &trans->r_theader +
  3075. sizeof(xfs_trans_header_t) - len;
  3076. memcpy(ptr, dp, len);
  3077. return 0;
  3078. }
  3079. /* take the tail entry */
  3080. item = list_entry(trans->r_itemq.prev, xlog_recover_item_t, ri_list);
  3081. old_ptr = item->ri_buf[item->ri_cnt-1].i_addr;
  3082. old_len = item->ri_buf[item->ri_cnt-1].i_len;
  3083. ptr = kmem_realloc(old_ptr, len+old_len, old_len, KM_SLEEP);
  3084. memcpy(&ptr[old_len], dp, len);
  3085. item->ri_buf[item->ri_cnt-1].i_len += len;
  3086. item->ri_buf[item->ri_cnt-1].i_addr = ptr;
  3087. trace_xfs_log_recover_item_add_cont(log, trans, item, 0);
  3088. return 0;
  3089. }
  3090. /*
  3091. * The next region to add is the start of a new region. It could be
  3092. * a whole region or it could be the first part of a new region. Because
  3093. * of this, the assumption here is that the type and size fields of all
  3094. * format structures fit into the first 32 bits of the structure.
  3095. *
  3096. * This works because all regions must be 32 bit aligned. Therefore, we
  3097. * either have both fields or we have neither field. In the case we have
  3098. * neither field, the data part of the region is zero length. We only have
  3099. * a log_op_header and can throw away the header since a new one will appear
  3100. * later. If we have at least 4 bytes, then we can determine how many regions
  3101. * will appear in the current log item.
  3102. */
  3103. STATIC int
  3104. xlog_recover_add_to_trans(
  3105. struct xlog *log,
  3106. struct xlog_recover *trans,
  3107. xfs_caddr_t dp,
  3108. int len)
  3109. {
  3110. xfs_inode_log_format_t *in_f; /* any will do */
  3111. xlog_recover_item_t *item;
  3112. xfs_caddr_t ptr;
  3113. if (!len)
  3114. return 0;
  3115. if (list_empty(&trans->r_itemq)) {
  3116. /* we need to catch log corruptions here */
  3117. if (*(uint *)dp != XFS_TRANS_HEADER_MAGIC) {
  3118. xfs_warn(log->l_mp, "%s: bad header magic number",
  3119. __func__);
  3120. ASSERT(0);
  3121. return -EIO;
  3122. }
  3123. if (len == sizeof(xfs_trans_header_t))
  3124. xlog_recover_add_item(&trans->r_itemq);
  3125. memcpy(&trans->r_theader, dp, len);
  3126. return 0;
  3127. }
  3128. ptr = kmem_alloc(len, KM_SLEEP);
  3129. memcpy(ptr, dp, len);
  3130. in_f = (xfs_inode_log_format_t *)ptr;
  3131. /* take the tail entry */
  3132. item = list_entry(trans->r_itemq.prev, xlog_recover_item_t, ri_list);
  3133. if (item->ri_total != 0 &&
  3134. item->ri_total == item->ri_cnt) {
  3135. /* tail item is in use, get a new one */
  3136. xlog_recover_add_item(&trans->r_itemq);
  3137. item = list_entry(trans->r_itemq.prev,
  3138. xlog_recover_item_t, ri_list);
  3139. }
  3140. if (item->ri_total == 0) { /* first region to be added */
  3141. if (in_f->ilf_size == 0 ||
  3142. in_f->ilf_size > XLOG_MAX_REGIONS_IN_ITEM) {
  3143. xfs_warn(log->l_mp,
  3144. "bad number of regions (%d) in inode log format",
  3145. in_f->ilf_size);
  3146. ASSERT(0);
  3147. kmem_free(ptr);
  3148. return -EIO;
  3149. }
  3150. item->ri_total = in_f->ilf_size;
  3151. item->ri_buf =
  3152. kmem_zalloc(item->ri_total * sizeof(xfs_log_iovec_t),
  3153. KM_SLEEP);
  3154. }
  3155. ASSERT(item->ri_total > item->ri_cnt);
  3156. /* Description region is ri_buf[0] */
  3157. item->ri_buf[item->ri_cnt].i_addr = ptr;
  3158. item->ri_buf[item->ri_cnt].i_len = len;
  3159. item->ri_cnt++;
  3160. trace_xfs_log_recover_item_add(log, trans, item, 0);
  3161. return 0;
  3162. }
  3163. /*
  3164. * Free up any resources allocated by the transaction
  3165. *
  3166. * Remember that EFIs, EFDs, and IUNLINKs are handled later.
  3167. */
  3168. STATIC void
  3169. xlog_recover_free_trans(
  3170. struct xlog_recover *trans)
  3171. {
  3172. xlog_recover_item_t *item, *n;
  3173. int i;
  3174. list_for_each_entry_safe(item, n, &trans->r_itemq, ri_list) {
  3175. /* Free the regions in the item. */
  3176. list_del(&item->ri_list);
  3177. for (i = 0; i < item->ri_cnt; i++)
  3178. kmem_free(item->ri_buf[i].i_addr);
  3179. /* Free the item itself */
  3180. kmem_free(item->ri_buf);
  3181. kmem_free(item);
  3182. }
  3183. /* Free the transaction recover structure */
  3184. kmem_free(trans);
  3185. }
  3186. /*
  3187. * On error or completion, trans is freed.
  3188. */
  3189. STATIC int
  3190. xlog_recovery_process_trans(
  3191. struct xlog *log,
  3192. struct xlog_recover *trans,
  3193. xfs_caddr_t dp,
  3194. unsigned int len,
  3195. unsigned int flags,
  3196. int pass)
  3197. {
  3198. int error = 0;
  3199. bool freeit = false;
  3200. /* mask off ophdr transaction container flags */
  3201. flags &= ~XLOG_END_TRANS;
  3202. if (flags & XLOG_WAS_CONT_TRANS)
  3203. flags &= ~XLOG_CONTINUE_TRANS;
  3204. /*
  3205. * Callees must not free the trans structure. We'll decide if we need to
  3206. * free it or not based on the operation being done and it's result.
  3207. */
  3208. switch (flags) {
  3209. /* expected flag values */
  3210. case 0:
  3211. case XLOG_CONTINUE_TRANS:
  3212. error = xlog_recover_add_to_trans(log, trans, dp, len);
  3213. break;
  3214. case XLOG_WAS_CONT_TRANS:
  3215. error = xlog_recover_add_to_cont_trans(log, trans, dp, len);
  3216. break;
  3217. case XLOG_COMMIT_TRANS:
  3218. error = xlog_recover_commit_trans(log, trans, pass);
  3219. /* success or fail, we are now done with this transaction. */
  3220. freeit = true;
  3221. break;
  3222. /* unexpected flag values */
  3223. case XLOG_UNMOUNT_TRANS:
  3224. /* just skip trans */
  3225. xfs_warn(log->l_mp, "%s: Unmount LR", __func__);
  3226. freeit = true;
  3227. break;
  3228. case XLOG_START_TRANS:
  3229. default:
  3230. xfs_warn(log->l_mp, "%s: bad flag 0x%x", __func__, flags);
  3231. ASSERT(0);
  3232. error = -EIO;
  3233. break;
  3234. }
  3235. if (error || freeit)
  3236. xlog_recover_free_trans(trans);
  3237. return error;
  3238. }
  3239. /*
  3240. * Lookup the transaction recovery structure associated with the ID in the
  3241. * current ophdr. If the transaction doesn't exist and the start flag is set in
  3242. * the ophdr, then allocate a new transaction for future ID matches to find.
  3243. * Either way, return what we found during the lookup - an existing transaction
  3244. * or nothing.
  3245. */
  3246. STATIC struct xlog_recover *
  3247. xlog_recover_ophdr_to_trans(
  3248. struct hlist_head rhash[],
  3249. struct xlog_rec_header *rhead,
  3250. struct xlog_op_header *ohead)
  3251. {
  3252. struct xlog_recover *trans;
  3253. xlog_tid_t tid;
  3254. struct hlist_head *rhp;
  3255. tid = be32_to_cpu(ohead->oh_tid);
  3256. rhp = &rhash[XLOG_RHASH(tid)];
  3257. hlist_for_each_entry(trans, rhp, r_list) {
  3258. if (trans->r_log_tid == tid)
  3259. return trans;
  3260. }
  3261. /*
  3262. * skip over non-start transaction headers - we could be
  3263. * processing slack space before the next transaction starts
  3264. */
  3265. if (!(ohead->oh_flags & XLOG_START_TRANS))
  3266. return NULL;
  3267. ASSERT(be32_to_cpu(ohead->oh_len) == 0);
  3268. /*
  3269. * This is a new transaction so allocate a new recovery container to
  3270. * hold the recovery ops that will follow.
  3271. */
  3272. trans = kmem_zalloc(sizeof(struct xlog_recover), KM_SLEEP);
  3273. trans->r_log_tid = tid;
  3274. trans->r_lsn = be64_to_cpu(rhead->h_lsn);
  3275. INIT_LIST_HEAD(&trans->r_itemq);
  3276. INIT_HLIST_NODE(&trans->r_list);
  3277. hlist_add_head(&trans->r_list, rhp);
  3278. /*
  3279. * Nothing more to do for this ophdr. Items to be added to this new
  3280. * transaction will be in subsequent ophdr containers.
  3281. */
  3282. return NULL;
  3283. }
  3284. STATIC int
  3285. xlog_recover_process_ophdr(
  3286. struct xlog *log,
  3287. struct hlist_head rhash[],
  3288. struct xlog_rec_header *rhead,
  3289. struct xlog_op_header *ohead,
  3290. xfs_caddr_t dp,
  3291. xfs_caddr_t end,
  3292. int pass)
  3293. {
  3294. struct xlog_recover *trans;
  3295. unsigned int len;
  3296. /* Do we understand who wrote this op? */
  3297. if (ohead->oh_clientid != XFS_TRANSACTION &&
  3298. ohead->oh_clientid != XFS_LOG) {
  3299. xfs_warn(log->l_mp, "%s: bad clientid 0x%x",
  3300. __func__, ohead->oh_clientid);
  3301. ASSERT(0);
  3302. return -EIO;
  3303. }
  3304. /*
  3305. * Check the ophdr contains all the data it is supposed to contain.
  3306. */
  3307. len = be32_to_cpu(ohead->oh_len);
  3308. if (dp + len > end) {
  3309. xfs_warn(log->l_mp, "%s: bad length 0x%x", __func__, len);
  3310. WARN_ON(1);
  3311. return -EIO;
  3312. }
  3313. trans = xlog_recover_ophdr_to_trans(rhash, rhead, ohead);
  3314. if (!trans) {
  3315. /* nothing to do, so skip over this ophdr */
  3316. return 0;
  3317. }
  3318. return xlog_recovery_process_trans(log, trans, dp, len,
  3319. ohead->oh_flags, pass);
  3320. }
  3321. /*
  3322. * There are two valid states of the r_state field. 0 indicates that the
  3323. * transaction structure is in a normal state. We have either seen the
  3324. * start of the transaction or the last operation we added was not a partial
  3325. * operation. If the last operation we added to the transaction was a
  3326. * partial operation, we need to mark r_state with XLOG_WAS_CONT_TRANS.
  3327. *
  3328. * NOTE: skip LRs with 0 data length.
  3329. */
  3330. STATIC int
  3331. xlog_recover_process_data(
  3332. struct xlog *log,
  3333. struct hlist_head rhash[],
  3334. struct xlog_rec_header *rhead,
  3335. xfs_caddr_t dp,
  3336. int pass)
  3337. {
  3338. struct xlog_op_header *ohead;
  3339. xfs_caddr_t end;
  3340. int num_logops;
  3341. int error;
  3342. end = dp + be32_to_cpu(rhead->h_len);
  3343. num_logops = be32_to_cpu(rhead->h_num_logops);
  3344. /* check the log format matches our own - else we can't recover */
  3345. if (xlog_header_check_recover(log->l_mp, rhead))
  3346. return -EIO;
  3347. while ((dp < end) && num_logops) {
  3348. ohead = (struct xlog_op_header *)dp;
  3349. dp += sizeof(*ohead);
  3350. ASSERT(dp <= end);
  3351. /* errors will abort recovery */
  3352. error = xlog_recover_process_ophdr(log, rhash, rhead, ohead,
  3353. dp, end, pass);
  3354. if (error)
  3355. return error;
  3356. dp += be32_to_cpu(ohead->oh_len);
  3357. num_logops--;
  3358. }
  3359. return 0;
  3360. }
  3361. /*
  3362. * Process an extent free intent item that was recovered from
  3363. * the log. We need to free the extents that it describes.
  3364. */
  3365. STATIC int
  3366. xlog_recover_process_efi(
  3367. xfs_mount_t *mp,
  3368. xfs_efi_log_item_t *efip)
  3369. {
  3370. xfs_efd_log_item_t *efdp;
  3371. xfs_trans_t *tp;
  3372. int i;
  3373. int error = 0;
  3374. xfs_extent_t *extp;
  3375. xfs_fsblock_t startblock_fsb;
  3376. ASSERT(!test_bit(XFS_EFI_RECOVERED, &efip->efi_flags));
  3377. /*
  3378. * First check the validity of the extents described by the
  3379. * EFI. If any are bad, then assume that all are bad and
  3380. * just toss the EFI.
  3381. */
  3382. for (i = 0; i < efip->efi_format.efi_nextents; i++) {
  3383. extp = &(efip->efi_format.efi_extents[i]);
  3384. startblock_fsb = XFS_BB_TO_FSB(mp,
  3385. XFS_FSB_TO_DADDR(mp, extp->ext_start));
  3386. if ((startblock_fsb == 0) ||
  3387. (extp->ext_len == 0) ||
  3388. (startblock_fsb >= mp->m_sb.sb_dblocks) ||
  3389. (extp->ext_len >= mp->m_sb.sb_agblocks)) {
  3390. /*
  3391. * This will pull the EFI from the AIL and
  3392. * free the memory associated with it.
  3393. */
  3394. set_bit(XFS_EFI_RECOVERED, &efip->efi_flags);
  3395. xfs_efi_release(efip, efip->efi_format.efi_nextents);
  3396. return -EIO;
  3397. }
  3398. }
  3399. tp = xfs_trans_alloc(mp, 0);
  3400. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_itruncate, 0, 0);
  3401. if (error)
  3402. goto abort_error;
  3403. efdp = xfs_trans_get_efd(tp, efip, efip->efi_format.efi_nextents);
  3404. for (i = 0; i < efip->efi_format.efi_nextents; i++) {
  3405. extp = &(efip->efi_format.efi_extents[i]);
  3406. error = xfs_free_extent(tp, extp->ext_start, extp->ext_len);
  3407. if (error)
  3408. goto abort_error;
  3409. xfs_trans_log_efd_extent(tp, efdp, extp->ext_start,
  3410. extp->ext_len);
  3411. }
  3412. set_bit(XFS_EFI_RECOVERED, &efip->efi_flags);
  3413. error = xfs_trans_commit(tp, 0);
  3414. return error;
  3415. abort_error:
  3416. xfs_trans_cancel(tp, XFS_TRANS_ABORT);
  3417. return error;
  3418. }
  3419. /*
  3420. * When this is called, all of the EFIs which did not have
  3421. * corresponding EFDs should be in the AIL. What we do now
  3422. * is free the extents associated with each one.
  3423. *
  3424. * Since we process the EFIs in normal transactions, they
  3425. * will be removed at some point after the commit. This prevents
  3426. * us from just walking down the list processing each one.
  3427. * We'll use a flag in the EFI to skip those that we've already
  3428. * processed and use the AIL iteration mechanism's generation
  3429. * count to try to speed this up at least a bit.
  3430. *
  3431. * When we start, we know that the EFIs are the only things in
  3432. * the AIL. As we process them, however, other items are added
  3433. * to the AIL. Since everything added to the AIL must come after
  3434. * everything already in the AIL, we stop processing as soon as
  3435. * we see something other than an EFI in the AIL.
  3436. */
  3437. STATIC int
  3438. xlog_recover_process_efis(
  3439. struct xlog *log)
  3440. {
  3441. xfs_log_item_t *lip;
  3442. xfs_efi_log_item_t *efip;
  3443. int error = 0;
  3444. struct xfs_ail_cursor cur;
  3445. struct xfs_ail *ailp;
  3446. ailp = log->l_ailp;
  3447. spin_lock(&ailp->xa_lock);
  3448. lip = xfs_trans_ail_cursor_first(ailp, &cur, 0);
  3449. while (lip != NULL) {
  3450. /*
  3451. * We're done when we see something other than an EFI.
  3452. * There should be no EFIs left in the AIL now.
  3453. */
  3454. if (lip->li_type != XFS_LI_EFI) {
  3455. #ifdef DEBUG
  3456. for (; lip; lip = xfs_trans_ail_cursor_next(ailp, &cur))
  3457. ASSERT(lip->li_type != XFS_LI_EFI);
  3458. #endif
  3459. break;
  3460. }
  3461. /*
  3462. * Skip EFIs that we've already processed.
  3463. */
  3464. efip = (xfs_efi_log_item_t *)lip;
  3465. if (test_bit(XFS_EFI_RECOVERED, &efip->efi_flags)) {
  3466. lip = xfs_trans_ail_cursor_next(ailp, &cur);
  3467. continue;
  3468. }
  3469. spin_unlock(&ailp->xa_lock);
  3470. error = xlog_recover_process_efi(log->l_mp, efip);
  3471. spin_lock(&ailp->xa_lock);
  3472. if (error)
  3473. goto out;
  3474. lip = xfs_trans_ail_cursor_next(ailp, &cur);
  3475. }
  3476. out:
  3477. xfs_trans_ail_cursor_done(&cur);
  3478. spin_unlock(&ailp->xa_lock);
  3479. return error;
  3480. }
  3481. /*
  3482. * This routine performs a transaction to null out a bad inode pointer
  3483. * in an agi unlinked inode hash bucket.
  3484. */
  3485. STATIC void
  3486. xlog_recover_clear_agi_bucket(
  3487. xfs_mount_t *mp,
  3488. xfs_agnumber_t agno,
  3489. int bucket)
  3490. {
  3491. xfs_trans_t *tp;
  3492. xfs_agi_t *agi;
  3493. xfs_buf_t *agibp;
  3494. int offset;
  3495. int error;
  3496. tp = xfs_trans_alloc(mp, XFS_TRANS_CLEAR_AGI_BUCKET);
  3497. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_clearagi, 0, 0);
  3498. if (error)
  3499. goto out_abort;
  3500. error = xfs_read_agi(mp, tp, agno, &agibp);
  3501. if (error)
  3502. goto out_abort;
  3503. agi = XFS_BUF_TO_AGI(agibp);
  3504. agi->agi_unlinked[bucket] = cpu_to_be32(NULLAGINO);
  3505. offset = offsetof(xfs_agi_t, agi_unlinked) +
  3506. (sizeof(xfs_agino_t) * bucket);
  3507. xfs_trans_log_buf(tp, agibp, offset,
  3508. (offset + sizeof(xfs_agino_t) - 1));
  3509. error = xfs_trans_commit(tp, 0);
  3510. if (error)
  3511. goto out_error;
  3512. return;
  3513. out_abort:
  3514. xfs_trans_cancel(tp, XFS_TRANS_ABORT);
  3515. out_error:
  3516. xfs_warn(mp, "%s: failed to clear agi %d. Continuing.", __func__, agno);
  3517. return;
  3518. }
  3519. STATIC xfs_agino_t
  3520. xlog_recover_process_one_iunlink(
  3521. struct xfs_mount *mp,
  3522. xfs_agnumber_t agno,
  3523. xfs_agino_t agino,
  3524. int bucket)
  3525. {
  3526. struct xfs_buf *ibp;
  3527. struct xfs_dinode *dip;
  3528. struct xfs_inode *ip;
  3529. xfs_ino_t ino;
  3530. int error;
  3531. ino = XFS_AGINO_TO_INO(mp, agno, agino);
  3532. error = xfs_iget(mp, NULL, ino, 0, 0, &ip);
  3533. if (error)
  3534. goto fail;
  3535. /*
  3536. * Get the on disk inode to find the next inode in the bucket.
  3537. */
  3538. error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &ibp, 0, 0);
  3539. if (error)
  3540. goto fail_iput;
  3541. ASSERT(ip->i_d.di_nlink == 0);
  3542. ASSERT(ip->i_d.di_mode != 0);
  3543. /* setup for the next pass */
  3544. agino = be32_to_cpu(dip->di_next_unlinked);
  3545. xfs_buf_relse(ibp);
  3546. /*
  3547. * Prevent any DMAPI event from being sent when the reference on
  3548. * the inode is dropped.
  3549. */
  3550. ip->i_d.di_dmevmask = 0;
  3551. IRELE(ip);
  3552. return agino;
  3553. fail_iput:
  3554. IRELE(ip);
  3555. fail:
  3556. /*
  3557. * We can't read in the inode this bucket points to, or this inode
  3558. * is messed up. Just ditch this bucket of inodes. We will lose
  3559. * some inodes and space, but at least we won't hang.
  3560. *
  3561. * Call xlog_recover_clear_agi_bucket() to perform a transaction to
  3562. * clear the inode pointer in the bucket.
  3563. */
  3564. xlog_recover_clear_agi_bucket(mp, agno, bucket);
  3565. return NULLAGINO;
  3566. }
  3567. /*
  3568. * xlog_iunlink_recover
  3569. *
  3570. * This is called during recovery to process any inodes which
  3571. * we unlinked but not freed when the system crashed. These
  3572. * inodes will be on the lists in the AGI blocks. What we do
  3573. * here is scan all the AGIs and fully truncate and free any
  3574. * inodes found on the lists. Each inode is removed from the
  3575. * lists when it has been fully truncated and is freed. The
  3576. * freeing of the inode and its removal from the list must be
  3577. * atomic.
  3578. */
  3579. STATIC void
  3580. xlog_recover_process_iunlinks(
  3581. struct xlog *log)
  3582. {
  3583. xfs_mount_t *mp;
  3584. xfs_agnumber_t agno;
  3585. xfs_agi_t *agi;
  3586. xfs_buf_t *agibp;
  3587. xfs_agino_t agino;
  3588. int bucket;
  3589. int error;
  3590. uint mp_dmevmask;
  3591. mp = log->l_mp;
  3592. /*
  3593. * Prevent any DMAPI event from being sent while in this function.
  3594. */
  3595. mp_dmevmask = mp->m_dmevmask;
  3596. mp->m_dmevmask = 0;
  3597. for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
  3598. /*
  3599. * Find the agi for this ag.
  3600. */
  3601. error = xfs_read_agi(mp, NULL, agno, &agibp);
  3602. if (error) {
  3603. /*
  3604. * AGI is b0rked. Don't process it.
  3605. *
  3606. * We should probably mark the filesystem as corrupt
  3607. * after we've recovered all the ag's we can....
  3608. */
  3609. continue;
  3610. }
  3611. /*
  3612. * Unlock the buffer so that it can be acquired in the normal
  3613. * course of the transaction to truncate and free each inode.
  3614. * Because we are not racing with anyone else here for the AGI
  3615. * buffer, we don't even need to hold it locked to read the
  3616. * initial unlinked bucket entries out of the buffer. We keep
  3617. * buffer reference though, so that it stays pinned in memory
  3618. * while we need the buffer.
  3619. */
  3620. agi = XFS_BUF_TO_AGI(agibp);
  3621. xfs_buf_unlock(agibp);
  3622. for (bucket = 0; bucket < XFS_AGI_UNLINKED_BUCKETS; bucket++) {
  3623. agino = be32_to_cpu(agi->agi_unlinked[bucket]);
  3624. while (agino != NULLAGINO) {
  3625. agino = xlog_recover_process_one_iunlink(mp,
  3626. agno, agino, bucket);
  3627. }
  3628. }
  3629. xfs_buf_rele(agibp);
  3630. }
  3631. mp->m_dmevmask = mp_dmevmask;
  3632. }
  3633. /*
  3634. * Upack the log buffer data and crc check it. If the check fails, issue a
  3635. * warning if and only if the CRC in the header is non-zero. This makes the
  3636. * check an advisory warning, and the zero CRC check will prevent failure
  3637. * warnings from being emitted when upgrading the kernel from one that does not
  3638. * add CRCs by default.
  3639. *
  3640. * When filesystems are CRC enabled, this CRC mismatch becomes a fatal log
  3641. * corruption failure
  3642. */
  3643. STATIC int
  3644. xlog_unpack_data_crc(
  3645. struct xlog_rec_header *rhead,
  3646. xfs_caddr_t dp,
  3647. struct xlog *log)
  3648. {
  3649. __le32 crc;
  3650. crc = xlog_cksum(log, rhead, dp, be32_to_cpu(rhead->h_len));
  3651. if (crc != rhead->h_crc) {
  3652. if (rhead->h_crc || xfs_sb_version_hascrc(&log->l_mp->m_sb)) {
  3653. xfs_alert(log->l_mp,
  3654. "log record CRC mismatch: found 0x%x, expected 0x%x.",
  3655. le32_to_cpu(rhead->h_crc),
  3656. le32_to_cpu(crc));
  3657. xfs_hex_dump(dp, 32);
  3658. }
  3659. /*
  3660. * If we've detected a log record corruption, then we can't
  3661. * recover past this point. Abort recovery if we are enforcing
  3662. * CRC protection by punting an error back up the stack.
  3663. */
  3664. if (xfs_sb_version_hascrc(&log->l_mp->m_sb))
  3665. return -EFSCORRUPTED;
  3666. }
  3667. return 0;
  3668. }
  3669. STATIC int
  3670. xlog_unpack_data(
  3671. struct xlog_rec_header *rhead,
  3672. xfs_caddr_t dp,
  3673. struct xlog *log)
  3674. {
  3675. int i, j, k;
  3676. int error;
  3677. error = xlog_unpack_data_crc(rhead, dp, log);
  3678. if (error)
  3679. return error;
  3680. for (i = 0; i < BTOBB(be32_to_cpu(rhead->h_len)) &&
  3681. i < (XLOG_HEADER_CYCLE_SIZE / BBSIZE); i++) {
  3682. *(__be32 *)dp = *(__be32 *)&rhead->h_cycle_data[i];
  3683. dp += BBSIZE;
  3684. }
  3685. if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
  3686. xlog_in_core_2_t *xhdr = (xlog_in_core_2_t *)rhead;
  3687. for ( ; i < BTOBB(be32_to_cpu(rhead->h_len)); i++) {
  3688. j = i / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
  3689. k = i % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
  3690. *(__be32 *)dp = xhdr[j].hic_xheader.xh_cycle_data[k];
  3691. dp += BBSIZE;
  3692. }
  3693. }
  3694. return 0;
  3695. }
  3696. STATIC int
  3697. xlog_valid_rec_header(
  3698. struct xlog *log,
  3699. struct xlog_rec_header *rhead,
  3700. xfs_daddr_t blkno)
  3701. {
  3702. int hlen;
  3703. if (unlikely(rhead->h_magicno != cpu_to_be32(XLOG_HEADER_MAGIC_NUM))) {
  3704. XFS_ERROR_REPORT("xlog_valid_rec_header(1)",
  3705. XFS_ERRLEVEL_LOW, log->l_mp);
  3706. return -EFSCORRUPTED;
  3707. }
  3708. if (unlikely(
  3709. (!rhead->h_version ||
  3710. (be32_to_cpu(rhead->h_version) & (~XLOG_VERSION_OKBITS))))) {
  3711. xfs_warn(log->l_mp, "%s: unrecognised log version (%d).",
  3712. __func__, be32_to_cpu(rhead->h_version));
  3713. return -EIO;
  3714. }
  3715. /* LR body must have data or it wouldn't have been written */
  3716. hlen = be32_to_cpu(rhead->h_len);
  3717. if (unlikely( hlen <= 0 || hlen > INT_MAX )) {
  3718. XFS_ERROR_REPORT("xlog_valid_rec_header(2)",
  3719. XFS_ERRLEVEL_LOW, log->l_mp);
  3720. return -EFSCORRUPTED;
  3721. }
  3722. if (unlikely( blkno > log->l_logBBsize || blkno > INT_MAX )) {
  3723. XFS_ERROR_REPORT("xlog_valid_rec_header(3)",
  3724. XFS_ERRLEVEL_LOW, log->l_mp);
  3725. return -EFSCORRUPTED;
  3726. }
  3727. return 0;
  3728. }
  3729. /*
  3730. * Read the log from tail to head and process the log records found.
  3731. * Handle the two cases where the tail and head are in the same cycle
  3732. * and where the active portion of the log wraps around the end of
  3733. * the physical log separately. The pass parameter is passed through
  3734. * to the routines called to process the data and is not looked at
  3735. * here.
  3736. */
  3737. STATIC int
  3738. xlog_do_recovery_pass(
  3739. struct xlog *log,
  3740. xfs_daddr_t head_blk,
  3741. xfs_daddr_t tail_blk,
  3742. int pass)
  3743. {
  3744. xlog_rec_header_t *rhead;
  3745. xfs_daddr_t blk_no;
  3746. xfs_caddr_t offset;
  3747. xfs_buf_t *hbp, *dbp;
  3748. int error = 0, h_size;
  3749. int bblks, split_bblks;
  3750. int hblks, split_hblks, wrapped_hblks;
  3751. struct hlist_head rhash[XLOG_RHASH_SIZE];
  3752. ASSERT(head_blk != tail_blk);
  3753. /*
  3754. * Read the header of the tail block and get the iclog buffer size from
  3755. * h_size. Use this to tell how many sectors make up the log header.
  3756. */
  3757. if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
  3758. /*
  3759. * When using variable length iclogs, read first sector of
  3760. * iclog header and extract the header size from it. Get a
  3761. * new hbp that is the correct size.
  3762. */
  3763. hbp = xlog_get_bp(log, 1);
  3764. if (!hbp)
  3765. return -ENOMEM;
  3766. error = xlog_bread(log, tail_blk, 1, hbp, &offset);
  3767. if (error)
  3768. goto bread_err1;
  3769. rhead = (xlog_rec_header_t *)offset;
  3770. error = xlog_valid_rec_header(log, rhead, tail_blk);
  3771. if (error)
  3772. goto bread_err1;
  3773. h_size = be32_to_cpu(rhead->h_size);
  3774. if ((be32_to_cpu(rhead->h_version) & XLOG_VERSION_2) &&
  3775. (h_size > XLOG_HEADER_CYCLE_SIZE)) {
  3776. hblks = h_size / XLOG_HEADER_CYCLE_SIZE;
  3777. if (h_size % XLOG_HEADER_CYCLE_SIZE)
  3778. hblks++;
  3779. xlog_put_bp(hbp);
  3780. hbp = xlog_get_bp(log, hblks);
  3781. } else {
  3782. hblks = 1;
  3783. }
  3784. } else {
  3785. ASSERT(log->l_sectBBsize == 1);
  3786. hblks = 1;
  3787. hbp = xlog_get_bp(log, 1);
  3788. h_size = XLOG_BIG_RECORD_BSIZE;
  3789. }
  3790. if (!hbp)
  3791. return -ENOMEM;
  3792. dbp = xlog_get_bp(log, BTOBB(h_size));
  3793. if (!dbp) {
  3794. xlog_put_bp(hbp);
  3795. return -ENOMEM;
  3796. }
  3797. memset(rhash, 0, sizeof(rhash));
  3798. blk_no = tail_blk;
  3799. if (tail_blk > head_blk) {
  3800. /*
  3801. * Perform recovery around the end of the physical log.
  3802. * When the head is not on the same cycle number as the tail,
  3803. * we can't do a sequential recovery.
  3804. */
  3805. while (blk_no < log->l_logBBsize) {
  3806. /*
  3807. * Check for header wrapping around physical end-of-log
  3808. */
  3809. offset = hbp->b_addr;
  3810. split_hblks = 0;
  3811. wrapped_hblks = 0;
  3812. if (blk_no + hblks <= log->l_logBBsize) {
  3813. /* Read header in one read */
  3814. error = xlog_bread(log, blk_no, hblks, hbp,
  3815. &offset);
  3816. if (error)
  3817. goto bread_err2;
  3818. } else {
  3819. /* This LR is split across physical log end */
  3820. if (blk_no != log->l_logBBsize) {
  3821. /* some data before physical log end */
  3822. ASSERT(blk_no <= INT_MAX);
  3823. split_hblks = log->l_logBBsize - (int)blk_no;
  3824. ASSERT(split_hblks > 0);
  3825. error = xlog_bread(log, blk_no,
  3826. split_hblks, hbp,
  3827. &offset);
  3828. if (error)
  3829. goto bread_err2;
  3830. }
  3831. /*
  3832. * Note: this black magic still works with
  3833. * large sector sizes (non-512) only because:
  3834. * - we increased the buffer size originally
  3835. * by 1 sector giving us enough extra space
  3836. * for the second read;
  3837. * - the log start is guaranteed to be sector
  3838. * aligned;
  3839. * - we read the log end (LR header start)
  3840. * _first_, then the log start (LR header end)
  3841. * - order is important.
  3842. */
  3843. wrapped_hblks = hblks - split_hblks;
  3844. error = xlog_bread_offset(log, 0,
  3845. wrapped_hblks, hbp,
  3846. offset + BBTOB(split_hblks));
  3847. if (error)
  3848. goto bread_err2;
  3849. }
  3850. rhead = (xlog_rec_header_t *)offset;
  3851. error = xlog_valid_rec_header(log, rhead,
  3852. split_hblks ? blk_no : 0);
  3853. if (error)
  3854. goto bread_err2;
  3855. bblks = (int)BTOBB(be32_to_cpu(rhead->h_len));
  3856. blk_no += hblks;
  3857. /* Read in data for log record */
  3858. if (blk_no + bblks <= log->l_logBBsize) {
  3859. error = xlog_bread(log, blk_no, bblks, dbp,
  3860. &offset);
  3861. if (error)
  3862. goto bread_err2;
  3863. } else {
  3864. /* This log record is split across the
  3865. * physical end of log */
  3866. offset = dbp->b_addr;
  3867. split_bblks = 0;
  3868. if (blk_no != log->l_logBBsize) {
  3869. /* some data is before the physical
  3870. * end of log */
  3871. ASSERT(!wrapped_hblks);
  3872. ASSERT(blk_no <= INT_MAX);
  3873. split_bblks =
  3874. log->l_logBBsize - (int)blk_no;
  3875. ASSERT(split_bblks > 0);
  3876. error = xlog_bread(log, blk_no,
  3877. split_bblks, dbp,
  3878. &offset);
  3879. if (error)
  3880. goto bread_err2;
  3881. }
  3882. /*
  3883. * Note: this black magic still works with
  3884. * large sector sizes (non-512) only because:
  3885. * - we increased the buffer size originally
  3886. * by 1 sector giving us enough extra space
  3887. * for the second read;
  3888. * - the log start is guaranteed to be sector
  3889. * aligned;
  3890. * - we read the log end (LR header start)
  3891. * _first_, then the log start (LR header end)
  3892. * - order is important.
  3893. */
  3894. error = xlog_bread_offset(log, 0,
  3895. bblks - split_bblks, dbp,
  3896. offset + BBTOB(split_bblks));
  3897. if (error)
  3898. goto bread_err2;
  3899. }
  3900. error = xlog_unpack_data(rhead, offset, log);
  3901. if (error)
  3902. goto bread_err2;
  3903. error = xlog_recover_process_data(log, rhash,
  3904. rhead, offset, pass);
  3905. if (error)
  3906. goto bread_err2;
  3907. blk_no += bblks;
  3908. }
  3909. ASSERT(blk_no >= log->l_logBBsize);
  3910. blk_no -= log->l_logBBsize;
  3911. }
  3912. /* read first part of physical log */
  3913. while (blk_no < head_blk) {
  3914. error = xlog_bread(log, blk_no, hblks, hbp, &offset);
  3915. if (error)
  3916. goto bread_err2;
  3917. rhead = (xlog_rec_header_t *)offset;
  3918. error = xlog_valid_rec_header(log, rhead, blk_no);
  3919. if (error)
  3920. goto bread_err2;
  3921. /* blocks in data section */
  3922. bblks = (int)BTOBB(be32_to_cpu(rhead->h_len));
  3923. error = xlog_bread(log, blk_no+hblks, bblks, dbp,
  3924. &offset);
  3925. if (error)
  3926. goto bread_err2;
  3927. error = xlog_unpack_data(rhead, offset, log);
  3928. if (error)
  3929. goto bread_err2;
  3930. error = xlog_recover_process_data(log, rhash,
  3931. rhead, offset, pass);
  3932. if (error)
  3933. goto bread_err2;
  3934. blk_no += bblks + hblks;
  3935. }
  3936. bread_err2:
  3937. xlog_put_bp(dbp);
  3938. bread_err1:
  3939. xlog_put_bp(hbp);
  3940. return error;
  3941. }
  3942. /*
  3943. * Do the recovery of the log. We actually do this in two phases.
  3944. * The two passes are necessary in order to implement the function
  3945. * of cancelling a record written into the log. The first pass
  3946. * determines those things which have been cancelled, and the
  3947. * second pass replays log items normally except for those which
  3948. * have been cancelled. The handling of the replay and cancellations
  3949. * takes place in the log item type specific routines.
  3950. *
  3951. * The table of items which have cancel records in the log is allocated
  3952. * and freed at this level, since only here do we know when all of
  3953. * the log recovery has been completed.
  3954. */
  3955. STATIC int
  3956. xlog_do_log_recovery(
  3957. struct xlog *log,
  3958. xfs_daddr_t head_blk,
  3959. xfs_daddr_t tail_blk)
  3960. {
  3961. int error, i;
  3962. ASSERT(head_blk != tail_blk);
  3963. /*
  3964. * First do a pass to find all of the cancelled buf log items.
  3965. * Store them in the buf_cancel_table for use in the second pass.
  3966. */
  3967. log->l_buf_cancel_table = kmem_zalloc(XLOG_BC_TABLE_SIZE *
  3968. sizeof(struct list_head),
  3969. KM_SLEEP);
  3970. for (i = 0; i < XLOG_BC_TABLE_SIZE; i++)
  3971. INIT_LIST_HEAD(&log->l_buf_cancel_table[i]);
  3972. error = xlog_do_recovery_pass(log, head_blk, tail_blk,
  3973. XLOG_RECOVER_PASS1);
  3974. if (error != 0) {
  3975. kmem_free(log->l_buf_cancel_table);
  3976. log->l_buf_cancel_table = NULL;
  3977. return error;
  3978. }
  3979. /*
  3980. * Then do a second pass to actually recover the items in the log.
  3981. * When it is complete free the table of buf cancel items.
  3982. */
  3983. error = xlog_do_recovery_pass(log, head_blk, tail_blk,
  3984. XLOG_RECOVER_PASS2);
  3985. #ifdef DEBUG
  3986. if (!error) {
  3987. int i;
  3988. for (i = 0; i < XLOG_BC_TABLE_SIZE; i++)
  3989. ASSERT(list_empty(&log->l_buf_cancel_table[i]));
  3990. }
  3991. #endif /* DEBUG */
  3992. kmem_free(log->l_buf_cancel_table);
  3993. log->l_buf_cancel_table = NULL;
  3994. return error;
  3995. }
  3996. /*
  3997. * Do the actual recovery
  3998. */
  3999. STATIC int
  4000. xlog_do_recover(
  4001. struct xlog *log,
  4002. xfs_daddr_t head_blk,
  4003. xfs_daddr_t tail_blk)
  4004. {
  4005. int error;
  4006. xfs_buf_t *bp;
  4007. xfs_sb_t *sbp;
  4008. /*
  4009. * First replay the images in the log.
  4010. */
  4011. error = xlog_do_log_recovery(log, head_blk, tail_blk);
  4012. if (error)
  4013. return error;
  4014. /*
  4015. * If IO errors happened during recovery, bail out.
  4016. */
  4017. if (XFS_FORCED_SHUTDOWN(log->l_mp)) {
  4018. return -EIO;
  4019. }
  4020. /*
  4021. * We now update the tail_lsn since much of the recovery has completed
  4022. * and there may be space available to use. If there were no extent
  4023. * or iunlinks, we can free up the entire log and set the tail_lsn to
  4024. * be the last_sync_lsn. This was set in xlog_find_tail to be the
  4025. * lsn of the last known good LR on disk. If there are extent frees
  4026. * or iunlinks they will have some entries in the AIL; so we look at
  4027. * the AIL to determine how to set the tail_lsn.
  4028. */
  4029. xlog_assign_tail_lsn(log->l_mp);
  4030. /*
  4031. * Now that we've finished replaying all buffer and inode
  4032. * updates, re-read in the superblock and reverify it.
  4033. */
  4034. bp = xfs_getsb(log->l_mp, 0);
  4035. XFS_BUF_UNDONE(bp);
  4036. ASSERT(!(XFS_BUF_ISWRITE(bp)));
  4037. XFS_BUF_READ(bp);
  4038. XFS_BUF_UNASYNC(bp);
  4039. bp->b_ops = &xfs_sb_buf_ops;
  4040. error = xfs_buf_submit_wait(bp);
  4041. if (error) {
  4042. if (!XFS_FORCED_SHUTDOWN(log->l_mp)) {
  4043. xfs_buf_ioerror_alert(bp, __func__);
  4044. ASSERT(0);
  4045. }
  4046. xfs_buf_relse(bp);
  4047. return error;
  4048. }
  4049. /* Convert superblock from on-disk format */
  4050. sbp = &log->l_mp->m_sb;
  4051. xfs_sb_from_disk(sbp, XFS_BUF_TO_SBP(bp));
  4052. ASSERT(sbp->sb_magicnum == XFS_SB_MAGIC);
  4053. ASSERT(xfs_sb_good_version(sbp));
  4054. xfs_buf_relse(bp);
  4055. /* We've re-read the superblock so re-initialize per-cpu counters */
  4056. xfs_icsb_reinit_counters(log->l_mp);
  4057. xlog_recover_check_summary(log);
  4058. /* Normal transactions can now occur */
  4059. log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
  4060. return 0;
  4061. }
  4062. /*
  4063. * Perform recovery and re-initialize some log variables in xlog_find_tail.
  4064. *
  4065. * Return error or zero.
  4066. */
  4067. int
  4068. xlog_recover(
  4069. struct xlog *log)
  4070. {
  4071. xfs_daddr_t head_blk, tail_blk;
  4072. int error;
  4073. /* find the tail of the log */
  4074. if ((error = xlog_find_tail(log, &head_blk, &tail_blk)))
  4075. return error;
  4076. if (tail_blk != head_blk) {
  4077. /* There used to be a comment here:
  4078. *
  4079. * disallow recovery on read-only mounts. note -- mount
  4080. * checks for ENOSPC and turns it into an intelligent
  4081. * error message.
  4082. * ...but this is no longer true. Now, unless you specify
  4083. * NORECOVERY (in which case this function would never be
  4084. * called), we just go ahead and recover. We do this all
  4085. * under the vfs layer, so we can get away with it unless
  4086. * the device itself is read-only, in which case we fail.
  4087. */
  4088. if ((error = xfs_dev_is_read_only(log->l_mp, "recovery"))) {
  4089. return error;
  4090. }
  4091. /*
  4092. * Version 5 superblock log feature mask validation. We know the
  4093. * log is dirty so check if there are any unknown log features
  4094. * in what we need to recover. If there are unknown features
  4095. * (e.g. unsupported transactions, then simply reject the
  4096. * attempt at recovery before touching anything.
  4097. */
  4098. if (XFS_SB_VERSION_NUM(&log->l_mp->m_sb) == XFS_SB_VERSION_5 &&
  4099. xfs_sb_has_incompat_log_feature(&log->l_mp->m_sb,
  4100. XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN)) {
  4101. xfs_warn(log->l_mp,
  4102. "Superblock has unknown incompatible log features (0x%x) enabled.\n"
  4103. "The log can not be fully and/or safely recovered by this kernel.\n"
  4104. "Please recover the log on a kernel that supports the unknown features.",
  4105. (log->l_mp->m_sb.sb_features_log_incompat &
  4106. XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN));
  4107. return -EINVAL;
  4108. }
  4109. /*
  4110. * Delay log recovery if the debug hook is set. This is debug
  4111. * instrumention to coordinate simulation of I/O failures with
  4112. * log recovery.
  4113. */
  4114. if (xfs_globals.log_recovery_delay) {
  4115. xfs_notice(log->l_mp,
  4116. "Delaying log recovery for %d seconds.",
  4117. xfs_globals.log_recovery_delay);
  4118. msleep(xfs_globals.log_recovery_delay * 1000);
  4119. }
  4120. xfs_notice(log->l_mp, "Starting recovery (logdev: %s)",
  4121. log->l_mp->m_logname ? log->l_mp->m_logname
  4122. : "internal");
  4123. error = xlog_do_recover(log, head_blk, tail_blk);
  4124. log->l_flags |= XLOG_RECOVERY_NEEDED;
  4125. }
  4126. return error;
  4127. }
  4128. /*
  4129. * In the first part of recovery we replay inodes and buffers and build
  4130. * up the list of extent free items which need to be processed. Here
  4131. * we process the extent free items and clean up the on disk unlinked
  4132. * inode lists. This is separated from the first part of recovery so
  4133. * that the root and real-time bitmap inodes can be read in from disk in
  4134. * between the two stages. This is necessary so that we can free space
  4135. * in the real-time portion of the file system.
  4136. */
  4137. int
  4138. xlog_recover_finish(
  4139. struct xlog *log)
  4140. {
  4141. /*
  4142. * Now we're ready to do the transactions needed for the
  4143. * rest of recovery. Start with completing all the extent
  4144. * free intent records and then process the unlinked inode
  4145. * lists. At this point, we essentially run in normal mode
  4146. * except that we're still performing recovery actions
  4147. * rather than accepting new requests.
  4148. */
  4149. if (log->l_flags & XLOG_RECOVERY_NEEDED) {
  4150. int error;
  4151. error = xlog_recover_process_efis(log);
  4152. if (error) {
  4153. xfs_alert(log->l_mp, "Failed to recover EFIs");
  4154. return error;
  4155. }
  4156. /*
  4157. * Sync the log to get all the EFIs out of the AIL.
  4158. * This isn't absolutely necessary, but it helps in
  4159. * case the unlink transactions would have problems
  4160. * pushing the EFIs out of the way.
  4161. */
  4162. xfs_log_force(log->l_mp, XFS_LOG_SYNC);
  4163. xlog_recover_process_iunlinks(log);
  4164. xlog_recover_check_summary(log);
  4165. xfs_notice(log->l_mp, "Ending recovery (logdev: %s)",
  4166. log->l_mp->m_logname ? log->l_mp->m_logname
  4167. : "internal");
  4168. log->l_flags &= ~XLOG_RECOVERY_NEEDED;
  4169. } else {
  4170. xfs_info(log->l_mp, "Ending clean mount");
  4171. }
  4172. return 0;
  4173. }
  4174. #if defined(DEBUG)
  4175. /*
  4176. * Read all of the agf and agi counters and check that they
  4177. * are consistent with the superblock counters.
  4178. */
  4179. void
  4180. xlog_recover_check_summary(
  4181. struct xlog *log)
  4182. {
  4183. xfs_mount_t *mp;
  4184. xfs_agf_t *agfp;
  4185. xfs_buf_t *agfbp;
  4186. xfs_buf_t *agibp;
  4187. xfs_agnumber_t agno;
  4188. __uint64_t freeblks;
  4189. __uint64_t itotal;
  4190. __uint64_t ifree;
  4191. int error;
  4192. mp = log->l_mp;
  4193. freeblks = 0LL;
  4194. itotal = 0LL;
  4195. ifree = 0LL;
  4196. for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
  4197. error = xfs_read_agf(mp, NULL, agno, 0, &agfbp);
  4198. if (error) {
  4199. xfs_alert(mp, "%s agf read failed agno %d error %d",
  4200. __func__, agno, error);
  4201. } else {
  4202. agfp = XFS_BUF_TO_AGF(agfbp);
  4203. freeblks += be32_to_cpu(agfp->agf_freeblks) +
  4204. be32_to_cpu(agfp->agf_flcount);
  4205. xfs_buf_relse(agfbp);
  4206. }
  4207. error = xfs_read_agi(mp, NULL, agno, &agibp);
  4208. if (error) {
  4209. xfs_alert(mp, "%s agi read failed agno %d error %d",
  4210. __func__, agno, error);
  4211. } else {
  4212. struct xfs_agi *agi = XFS_BUF_TO_AGI(agibp);
  4213. itotal += be32_to_cpu(agi->agi_count);
  4214. ifree += be32_to_cpu(agi->agi_freecount);
  4215. xfs_buf_relse(agibp);
  4216. }
  4217. }
  4218. }
  4219. #endif /* DEBUG */