xfs_log_recover.c 132 KB

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