xfs_log_recover.c 143 KB

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