xfs_log_recover.c 107 KB

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