xfs_log_recover.c 111 KB

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