xfs_log_recover.c 144 KB

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