xfs_log_recover.c 111 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117
  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. * The dirty regions logged in the buffer, even though
  1920. * contiguous, may span multiple chunks. This is because the
  1921. * dirty region may span a physical page boundary in a buffer
  1922. * and hence be split into two separate vectors for writing into
  1923. * the log. Hence we need to trim nbits back to the length of
  1924. * the current region being copied out of the log.
  1925. */
  1926. if (item->ri_buf[i].i_len < (nbits << XFS_BLF_SHIFT))
  1927. nbits = item->ri_buf[i].i_len >> XFS_BLF_SHIFT;
  1928. /*
  1929. * Do a sanity check if this is a dquot buffer. Just checking
  1930. * the first dquot in the buffer should do. XXXThis is
  1931. * probably a good thing to do for other buf types also.
  1932. */
  1933. error = 0;
  1934. if (buf_f->blf_flags &
  1935. (XFS_BLF_UDQUOT_BUF|XFS_BLF_PDQUOT_BUF|XFS_BLF_GDQUOT_BUF)) {
  1936. if (item->ri_buf[i].i_addr == NULL) {
  1937. xfs_alert(mp,
  1938. "XFS: NULL dquot in %s.", __func__);
  1939. goto next;
  1940. }
  1941. if (item->ri_buf[i].i_len < sizeof(xfs_disk_dquot_t)) {
  1942. xfs_alert(mp,
  1943. "XFS: dquot too small (%d) in %s.",
  1944. item->ri_buf[i].i_len, __func__);
  1945. goto next;
  1946. }
  1947. error = xfs_qm_dqcheck(mp, item->ri_buf[i].i_addr,
  1948. -1, 0, XFS_QMOPT_DOWARN,
  1949. "dquot_buf_recover");
  1950. if (error)
  1951. goto next;
  1952. }
  1953. memcpy(xfs_buf_offset(bp,
  1954. (uint)bit << XFS_BLF_SHIFT), /* dest */
  1955. item->ri_buf[i].i_addr, /* source */
  1956. nbits<<XFS_BLF_SHIFT); /* length */
  1957. next:
  1958. i++;
  1959. bit += nbits;
  1960. }
  1961. /* Shouldn't be any more regions */
  1962. ASSERT(i == item->ri_total);
  1963. xlog_recovery_validate_buf_type(mp, bp, buf_f);
  1964. }
  1965. /*
  1966. * Do some primitive error checking on ondisk dquot data structures.
  1967. */
  1968. int
  1969. xfs_qm_dqcheck(
  1970. struct xfs_mount *mp,
  1971. xfs_disk_dquot_t *ddq,
  1972. xfs_dqid_t id,
  1973. uint type, /* used only when IO_dorepair is true */
  1974. uint flags,
  1975. char *str)
  1976. {
  1977. xfs_dqblk_t *d = (xfs_dqblk_t *)ddq;
  1978. int errs = 0;
  1979. /*
  1980. * We can encounter an uninitialized dquot buffer for 2 reasons:
  1981. * 1. If we crash while deleting the quotainode(s), and those blks got
  1982. * used for user data. This is because we take the path of regular
  1983. * file deletion; however, the size field of quotainodes is never
  1984. * updated, so all the tricks that we play in itruncate_finish
  1985. * don't quite matter.
  1986. *
  1987. * 2. We don't play the quota buffers when there's a quotaoff logitem.
  1988. * But the allocation will be replayed so we'll end up with an
  1989. * uninitialized quota block.
  1990. *
  1991. * This is all fine; things are still consistent, and we haven't lost
  1992. * any quota information. Just don't complain about bad dquot blks.
  1993. */
  1994. if (ddq->d_magic != cpu_to_be16(XFS_DQUOT_MAGIC)) {
  1995. if (flags & XFS_QMOPT_DOWARN)
  1996. xfs_alert(mp,
  1997. "%s : XFS dquot ID 0x%x, magic 0x%x != 0x%x",
  1998. str, id, be16_to_cpu(ddq->d_magic), XFS_DQUOT_MAGIC);
  1999. errs++;
  2000. }
  2001. if (ddq->d_version != XFS_DQUOT_VERSION) {
  2002. if (flags & XFS_QMOPT_DOWARN)
  2003. xfs_alert(mp,
  2004. "%s : XFS dquot ID 0x%x, version 0x%x != 0x%x",
  2005. str, id, ddq->d_version, XFS_DQUOT_VERSION);
  2006. errs++;
  2007. }
  2008. if (ddq->d_flags != XFS_DQ_USER &&
  2009. ddq->d_flags != XFS_DQ_PROJ &&
  2010. ddq->d_flags != XFS_DQ_GROUP) {
  2011. if (flags & XFS_QMOPT_DOWARN)
  2012. xfs_alert(mp,
  2013. "%s : XFS dquot ID 0x%x, unknown flags 0x%x",
  2014. str, id, ddq->d_flags);
  2015. errs++;
  2016. }
  2017. if (id != -1 && id != be32_to_cpu(ddq->d_id)) {
  2018. if (flags & XFS_QMOPT_DOWARN)
  2019. xfs_alert(mp,
  2020. "%s : ondisk-dquot 0x%p, ID mismatch: "
  2021. "0x%x expected, found id 0x%x",
  2022. str, ddq, id, be32_to_cpu(ddq->d_id));
  2023. errs++;
  2024. }
  2025. if (!errs && ddq->d_id) {
  2026. if (ddq->d_blk_softlimit &&
  2027. be64_to_cpu(ddq->d_bcount) >
  2028. be64_to_cpu(ddq->d_blk_softlimit)) {
  2029. if (!ddq->d_btimer) {
  2030. if (flags & XFS_QMOPT_DOWARN)
  2031. xfs_alert(mp,
  2032. "%s : Dquot ID 0x%x (0x%p) BLK TIMER NOT STARTED",
  2033. str, (int)be32_to_cpu(ddq->d_id), ddq);
  2034. errs++;
  2035. }
  2036. }
  2037. if (ddq->d_ino_softlimit &&
  2038. be64_to_cpu(ddq->d_icount) >
  2039. be64_to_cpu(ddq->d_ino_softlimit)) {
  2040. if (!ddq->d_itimer) {
  2041. if (flags & XFS_QMOPT_DOWARN)
  2042. xfs_alert(mp,
  2043. "%s : Dquot ID 0x%x (0x%p) INODE TIMER NOT STARTED",
  2044. str, (int)be32_to_cpu(ddq->d_id), ddq);
  2045. errs++;
  2046. }
  2047. }
  2048. if (ddq->d_rtb_softlimit &&
  2049. be64_to_cpu(ddq->d_rtbcount) >
  2050. be64_to_cpu(ddq->d_rtb_softlimit)) {
  2051. if (!ddq->d_rtbtimer) {
  2052. if (flags & XFS_QMOPT_DOWARN)
  2053. xfs_alert(mp,
  2054. "%s : Dquot ID 0x%x (0x%p) RTBLK TIMER NOT STARTED",
  2055. str, (int)be32_to_cpu(ddq->d_id), ddq);
  2056. errs++;
  2057. }
  2058. }
  2059. }
  2060. if (!errs || !(flags & XFS_QMOPT_DQREPAIR))
  2061. return errs;
  2062. if (flags & XFS_QMOPT_DOWARN)
  2063. xfs_notice(mp, "Re-initializing dquot ID 0x%x", id);
  2064. /*
  2065. * Typically, a repair is only requested by quotacheck.
  2066. */
  2067. ASSERT(id != -1);
  2068. ASSERT(flags & XFS_QMOPT_DQREPAIR);
  2069. memset(d, 0, sizeof(xfs_dqblk_t));
  2070. d->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
  2071. d->dd_diskdq.d_version = XFS_DQUOT_VERSION;
  2072. d->dd_diskdq.d_flags = type;
  2073. d->dd_diskdq.d_id = cpu_to_be32(id);
  2074. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  2075. uuid_copy(&d->dd_uuid, &mp->m_sb.sb_uuid);
  2076. xfs_update_cksum((char *)d, sizeof(struct xfs_dqblk),
  2077. XFS_DQUOT_CRC_OFF);
  2078. }
  2079. return errs;
  2080. }
  2081. /*
  2082. * Perform a dquot buffer recovery.
  2083. * Simple algorithm: if we have found a QUOTAOFF logitem of the same type
  2084. * (ie. USR or GRP), then just toss this buffer away; don't recover it.
  2085. * Else, treat it as a regular buffer and do recovery.
  2086. */
  2087. STATIC void
  2088. xlog_recover_do_dquot_buffer(
  2089. struct xfs_mount *mp,
  2090. struct xlog *log,
  2091. struct xlog_recover_item *item,
  2092. struct xfs_buf *bp,
  2093. struct xfs_buf_log_format *buf_f)
  2094. {
  2095. uint type;
  2096. trace_xfs_log_recover_buf_dquot_buf(log, buf_f);
  2097. /*
  2098. * Filesystems are required to send in quota flags at mount time.
  2099. */
  2100. if (mp->m_qflags == 0) {
  2101. return;
  2102. }
  2103. type = 0;
  2104. if (buf_f->blf_flags & XFS_BLF_UDQUOT_BUF)
  2105. type |= XFS_DQ_USER;
  2106. if (buf_f->blf_flags & XFS_BLF_PDQUOT_BUF)
  2107. type |= XFS_DQ_PROJ;
  2108. if (buf_f->blf_flags & XFS_BLF_GDQUOT_BUF)
  2109. type |= XFS_DQ_GROUP;
  2110. /*
  2111. * This type of quotas was turned off, so ignore this buffer
  2112. */
  2113. if (log->l_quotaoffs_flag & type)
  2114. return;
  2115. xlog_recover_do_reg_buffer(mp, item, bp, buf_f);
  2116. }
  2117. /*
  2118. * This routine replays a modification made to a buffer at runtime.
  2119. * There are actually two types of buffer, regular and inode, which
  2120. * are handled differently. Inode buffers are handled differently
  2121. * in that we only recover a specific set of data from them, namely
  2122. * the inode di_next_unlinked fields. This is because all other inode
  2123. * data is actually logged via inode records and any data we replay
  2124. * here which overlaps that may be stale.
  2125. *
  2126. * When meta-data buffers are freed at run time we log a buffer item
  2127. * with the XFS_BLF_CANCEL bit set to indicate that previous copies
  2128. * of the buffer in the log should not be replayed at recovery time.
  2129. * This is so that if the blocks covered by the buffer are reused for
  2130. * file data before we crash we don't end up replaying old, freed
  2131. * meta-data into a user's file.
  2132. *
  2133. * To handle the cancellation of buffer log items, we make two passes
  2134. * over the log during recovery. During the first we build a table of
  2135. * those buffers which have been cancelled, and during the second we
  2136. * only replay those buffers which do not have corresponding cancel
  2137. * records in the table. See xlog_recover_do_buffer_pass[1,2] above
  2138. * for more details on the implementation of the table of cancel records.
  2139. */
  2140. STATIC int
  2141. xlog_recover_buffer_pass2(
  2142. struct xlog *log,
  2143. struct list_head *buffer_list,
  2144. struct xlog_recover_item *item)
  2145. {
  2146. xfs_buf_log_format_t *buf_f = item->ri_buf[0].i_addr;
  2147. xfs_mount_t *mp = log->l_mp;
  2148. xfs_buf_t *bp;
  2149. int error;
  2150. uint buf_flags;
  2151. /*
  2152. * In this pass we only want to recover all the buffers which have
  2153. * not been cancelled and are not cancellation buffers themselves.
  2154. */
  2155. if (xlog_check_buffer_cancelled(log, buf_f->blf_blkno,
  2156. buf_f->blf_len, buf_f->blf_flags)) {
  2157. trace_xfs_log_recover_buf_cancel(log, buf_f);
  2158. return 0;
  2159. }
  2160. trace_xfs_log_recover_buf_recover(log, buf_f);
  2161. buf_flags = 0;
  2162. if (buf_f->blf_flags & XFS_BLF_INODE_BUF)
  2163. buf_flags |= XBF_UNMAPPED;
  2164. bp = xfs_buf_read(mp->m_ddev_targp, buf_f->blf_blkno, buf_f->blf_len,
  2165. buf_flags, NULL);
  2166. if (!bp)
  2167. return XFS_ERROR(ENOMEM);
  2168. error = bp->b_error;
  2169. if (error) {
  2170. xfs_buf_ioerror_alert(bp, "xlog_recover_do..(read#1)");
  2171. xfs_buf_relse(bp);
  2172. return error;
  2173. }
  2174. if (buf_f->blf_flags & XFS_BLF_INODE_BUF) {
  2175. error = xlog_recover_do_inode_buffer(mp, item, bp, buf_f);
  2176. } else if (buf_f->blf_flags &
  2177. (XFS_BLF_UDQUOT_BUF|XFS_BLF_PDQUOT_BUF|XFS_BLF_GDQUOT_BUF)) {
  2178. xlog_recover_do_dquot_buffer(mp, log, item, bp, buf_f);
  2179. } else {
  2180. xlog_recover_do_reg_buffer(mp, item, bp, buf_f);
  2181. }
  2182. if (error)
  2183. return XFS_ERROR(error);
  2184. /*
  2185. * Perform delayed write on the buffer. Asynchronous writes will be
  2186. * slower when taking into account all the buffers to be flushed.
  2187. *
  2188. * Also make sure that only inode buffers with good sizes stay in
  2189. * the buffer cache. The kernel moves inodes in buffers of 1 block
  2190. * or XFS_INODE_CLUSTER_SIZE bytes, whichever is bigger. The inode
  2191. * buffers in the log can be a different size if the log was generated
  2192. * by an older kernel using unclustered inode buffers or a newer kernel
  2193. * running with a different inode cluster size. Regardless, if the
  2194. * the inode buffer size isn't MAX(blocksize, XFS_INODE_CLUSTER_SIZE)
  2195. * for *our* value of XFS_INODE_CLUSTER_SIZE, then we need to keep
  2196. * the buffer out of the buffer cache so that the buffer won't
  2197. * overlap with future reads of those inodes.
  2198. */
  2199. if (XFS_DINODE_MAGIC ==
  2200. be16_to_cpu(*((__be16 *)xfs_buf_offset(bp, 0))) &&
  2201. (BBTOB(bp->b_io_length) != MAX(log->l_mp->m_sb.sb_blocksize,
  2202. (__uint32_t)XFS_INODE_CLUSTER_SIZE(log->l_mp)))) {
  2203. xfs_buf_stale(bp);
  2204. error = xfs_bwrite(bp);
  2205. } else {
  2206. ASSERT(bp->b_target->bt_mount == mp);
  2207. bp->b_iodone = xlog_recover_iodone;
  2208. xfs_buf_delwri_queue(bp, buffer_list);
  2209. }
  2210. xfs_buf_relse(bp);
  2211. return error;
  2212. }
  2213. STATIC int
  2214. xlog_recover_inode_pass2(
  2215. struct xlog *log,
  2216. struct list_head *buffer_list,
  2217. struct xlog_recover_item *item)
  2218. {
  2219. xfs_inode_log_format_t *in_f;
  2220. xfs_mount_t *mp = log->l_mp;
  2221. xfs_buf_t *bp;
  2222. xfs_dinode_t *dip;
  2223. int len;
  2224. xfs_caddr_t src;
  2225. xfs_caddr_t dest;
  2226. int error;
  2227. int attr_index;
  2228. uint fields;
  2229. xfs_icdinode_t *dicp;
  2230. uint isize;
  2231. int need_free = 0;
  2232. if (item->ri_buf[0].i_len == sizeof(xfs_inode_log_format_t)) {
  2233. in_f = item->ri_buf[0].i_addr;
  2234. } else {
  2235. in_f = kmem_alloc(sizeof(xfs_inode_log_format_t), KM_SLEEP);
  2236. need_free = 1;
  2237. error = xfs_inode_item_format_convert(&item->ri_buf[0], in_f);
  2238. if (error)
  2239. goto error;
  2240. }
  2241. /*
  2242. * Inode buffers can be freed, look out for it,
  2243. * and do not replay the inode.
  2244. */
  2245. if (xlog_check_buffer_cancelled(log, in_f->ilf_blkno,
  2246. in_f->ilf_len, 0)) {
  2247. error = 0;
  2248. trace_xfs_log_recover_inode_cancel(log, in_f);
  2249. goto error;
  2250. }
  2251. trace_xfs_log_recover_inode_recover(log, in_f);
  2252. bp = xfs_buf_read(mp->m_ddev_targp, in_f->ilf_blkno, in_f->ilf_len, 0,
  2253. &xfs_inode_buf_ops);
  2254. if (!bp) {
  2255. error = ENOMEM;
  2256. goto error;
  2257. }
  2258. error = bp->b_error;
  2259. if (error) {
  2260. xfs_buf_ioerror_alert(bp, "xlog_recover_do..(read#2)");
  2261. xfs_buf_relse(bp);
  2262. goto error;
  2263. }
  2264. ASSERT(in_f->ilf_fields & XFS_ILOG_CORE);
  2265. dip = (xfs_dinode_t *)xfs_buf_offset(bp, in_f->ilf_boffset);
  2266. /*
  2267. * Make sure the place we're flushing out to really looks
  2268. * like an inode!
  2269. */
  2270. if (unlikely(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC))) {
  2271. xfs_buf_relse(bp);
  2272. xfs_alert(mp,
  2273. "%s: Bad inode magic number, dip = 0x%p, dino bp = 0x%p, ino = %Ld",
  2274. __func__, dip, bp, in_f->ilf_ino);
  2275. XFS_ERROR_REPORT("xlog_recover_inode_pass2(1)",
  2276. XFS_ERRLEVEL_LOW, mp);
  2277. error = EFSCORRUPTED;
  2278. goto error;
  2279. }
  2280. dicp = item->ri_buf[1].i_addr;
  2281. if (unlikely(dicp->di_magic != XFS_DINODE_MAGIC)) {
  2282. xfs_buf_relse(bp);
  2283. xfs_alert(mp,
  2284. "%s: Bad inode log record, rec ptr 0x%p, ino %Ld",
  2285. __func__, item, in_f->ilf_ino);
  2286. XFS_ERROR_REPORT("xlog_recover_inode_pass2(2)",
  2287. XFS_ERRLEVEL_LOW, mp);
  2288. error = EFSCORRUPTED;
  2289. goto error;
  2290. }
  2291. /* Skip replay when the on disk inode is newer than the log one */
  2292. if (dicp->di_flushiter < be16_to_cpu(dip->di_flushiter)) {
  2293. /*
  2294. * Deal with the wrap case, DI_MAX_FLUSH is less
  2295. * than smaller numbers
  2296. */
  2297. if (be16_to_cpu(dip->di_flushiter) == DI_MAX_FLUSH &&
  2298. dicp->di_flushiter < (DI_MAX_FLUSH >> 1)) {
  2299. /* do nothing */
  2300. } else {
  2301. xfs_buf_relse(bp);
  2302. trace_xfs_log_recover_inode_skip(log, in_f);
  2303. error = 0;
  2304. goto error;
  2305. }
  2306. }
  2307. /* Take the opportunity to reset the flush iteration count */
  2308. dicp->di_flushiter = 0;
  2309. if (unlikely(S_ISREG(dicp->di_mode))) {
  2310. if ((dicp->di_format != XFS_DINODE_FMT_EXTENTS) &&
  2311. (dicp->di_format != XFS_DINODE_FMT_BTREE)) {
  2312. XFS_CORRUPTION_ERROR("xlog_recover_inode_pass2(3)",
  2313. XFS_ERRLEVEL_LOW, mp, dicp);
  2314. xfs_buf_relse(bp);
  2315. xfs_alert(mp,
  2316. "%s: Bad regular inode log record, rec ptr 0x%p, "
  2317. "ino ptr = 0x%p, ino bp = 0x%p, ino %Ld",
  2318. __func__, item, dip, bp, in_f->ilf_ino);
  2319. error = EFSCORRUPTED;
  2320. goto error;
  2321. }
  2322. } else if (unlikely(S_ISDIR(dicp->di_mode))) {
  2323. if ((dicp->di_format != XFS_DINODE_FMT_EXTENTS) &&
  2324. (dicp->di_format != XFS_DINODE_FMT_BTREE) &&
  2325. (dicp->di_format != XFS_DINODE_FMT_LOCAL)) {
  2326. XFS_CORRUPTION_ERROR("xlog_recover_inode_pass2(4)",
  2327. XFS_ERRLEVEL_LOW, mp, dicp);
  2328. xfs_buf_relse(bp);
  2329. xfs_alert(mp,
  2330. "%s: Bad dir inode log record, rec ptr 0x%p, "
  2331. "ino ptr = 0x%p, ino bp = 0x%p, ino %Ld",
  2332. __func__, item, dip, bp, in_f->ilf_ino);
  2333. error = EFSCORRUPTED;
  2334. goto error;
  2335. }
  2336. }
  2337. if (unlikely(dicp->di_nextents + dicp->di_anextents > dicp->di_nblocks)){
  2338. XFS_CORRUPTION_ERROR("xlog_recover_inode_pass2(5)",
  2339. XFS_ERRLEVEL_LOW, mp, dicp);
  2340. xfs_buf_relse(bp);
  2341. xfs_alert(mp,
  2342. "%s: Bad inode log record, rec ptr 0x%p, dino ptr 0x%p, "
  2343. "dino bp 0x%p, ino %Ld, total extents = %d, nblocks = %Ld",
  2344. __func__, item, dip, bp, in_f->ilf_ino,
  2345. dicp->di_nextents + dicp->di_anextents,
  2346. dicp->di_nblocks);
  2347. error = EFSCORRUPTED;
  2348. goto error;
  2349. }
  2350. if (unlikely(dicp->di_forkoff > mp->m_sb.sb_inodesize)) {
  2351. XFS_CORRUPTION_ERROR("xlog_recover_inode_pass2(6)",
  2352. XFS_ERRLEVEL_LOW, mp, dicp);
  2353. xfs_buf_relse(bp);
  2354. xfs_alert(mp,
  2355. "%s: Bad inode log record, rec ptr 0x%p, dino ptr 0x%p, "
  2356. "dino bp 0x%p, ino %Ld, forkoff 0x%x", __func__,
  2357. item, dip, bp, in_f->ilf_ino, dicp->di_forkoff);
  2358. error = EFSCORRUPTED;
  2359. goto error;
  2360. }
  2361. isize = xfs_icdinode_size(dicp->di_version);
  2362. if (unlikely(item->ri_buf[1].i_len > isize)) {
  2363. XFS_CORRUPTION_ERROR("xlog_recover_inode_pass2(7)",
  2364. XFS_ERRLEVEL_LOW, mp, dicp);
  2365. xfs_buf_relse(bp);
  2366. xfs_alert(mp,
  2367. "%s: Bad inode log record length %d, rec ptr 0x%p",
  2368. __func__, item->ri_buf[1].i_len, item);
  2369. error = EFSCORRUPTED;
  2370. goto error;
  2371. }
  2372. /* The core is in in-core format */
  2373. xfs_dinode_to_disk(dip, dicp);
  2374. /* the rest is in on-disk format */
  2375. if (item->ri_buf[1].i_len > isize) {
  2376. memcpy((char *)dip + isize,
  2377. item->ri_buf[1].i_addr + isize,
  2378. item->ri_buf[1].i_len - isize);
  2379. }
  2380. fields = in_f->ilf_fields;
  2381. switch (fields & (XFS_ILOG_DEV | XFS_ILOG_UUID)) {
  2382. case XFS_ILOG_DEV:
  2383. xfs_dinode_put_rdev(dip, in_f->ilf_u.ilfu_rdev);
  2384. break;
  2385. case XFS_ILOG_UUID:
  2386. memcpy(XFS_DFORK_DPTR(dip),
  2387. &in_f->ilf_u.ilfu_uuid,
  2388. sizeof(uuid_t));
  2389. break;
  2390. }
  2391. if (in_f->ilf_size == 2)
  2392. goto write_inode_buffer;
  2393. len = item->ri_buf[2].i_len;
  2394. src = item->ri_buf[2].i_addr;
  2395. ASSERT(in_f->ilf_size <= 4);
  2396. ASSERT((in_f->ilf_size == 3) || (fields & XFS_ILOG_AFORK));
  2397. ASSERT(!(fields & XFS_ILOG_DFORK) ||
  2398. (len == in_f->ilf_dsize));
  2399. switch (fields & XFS_ILOG_DFORK) {
  2400. case XFS_ILOG_DDATA:
  2401. case XFS_ILOG_DEXT:
  2402. memcpy(XFS_DFORK_DPTR(dip), src, len);
  2403. break;
  2404. case XFS_ILOG_DBROOT:
  2405. xfs_bmbt_to_bmdr(mp, (struct xfs_btree_block *)src, len,
  2406. (xfs_bmdr_block_t *)XFS_DFORK_DPTR(dip),
  2407. XFS_DFORK_DSIZE(dip, mp));
  2408. break;
  2409. default:
  2410. /*
  2411. * There are no data fork flags set.
  2412. */
  2413. ASSERT((fields & XFS_ILOG_DFORK) == 0);
  2414. break;
  2415. }
  2416. /*
  2417. * If we logged any attribute data, recover it. There may or
  2418. * may not have been any other non-core data logged in this
  2419. * transaction.
  2420. */
  2421. if (in_f->ilf_fields & XFS_ILOG_AFORK) {
  2422. if (in_f->ilf_fields & XFS_ILOG_DFORK) {
  2423. attr_index = 3;
  2424. } else {
  2425. attr_index = 2;
  2426. }
  2427. len = item->ri_buf[attr_index].i_len;
  2428. src = item->ri_buf[attr_index].i_addr;
  2429. ASSERT(len == in_f->ilf_asize);
  2430. switch (in_f->ilf_fields & XFS_ILOG_AFORK) {
  2431. case XFS_ILOG_ADATA:
  2432. case XFS_ILOG_AEXT:
  2433. dest = XFS_DFORK_APTR(dip);
  2434. ASSERT(len <= XFS_DFORK_ASIZE(dip, mp));
  2435. memcpy(dest, src, len);
  2436. break;
  2437. case XFS_ILOG_ABROOT:
  2438. dest = XFS_DFORK_APTR(dip);
  2439. xfs_bmbt_to_bmdr(mp, (struct xfs_btree_block *)src,
  2440. len, (xfs_bmdr_block_t*)dest,
  2441. XFS_DFORK_ASIZE(dip, mp));
  2442. break;
  2443. default:
  2444. xfs_warn(log->l_mp, "%s: Invalid flag", __func__);
  2445. ASSERT(0);
  2446. xfs_buf_relse(bp);
  2447. error = EIO;
  2448. goto error;
  2449. }
  2450. }
  2451. write_inode_buffer:
  2452. /* re-generate the checksum. */
  2453. xfs_dinode_calc_crc(log->l_mp, dip);
  2454. ASSERT(bp->b_target->bt_mount == mp);
  2455. bp->b_iodone = xlog_recover_iodone;
  2456. xfs_buf_delwri_queue(bp, buffer_list);
  2457. xfs_buf_relse(bp);
  2458. error:
  2459. if (need_free)
  2460. kmem_free(in_f);
  2461. return XFS_ERROR(error);
  2462. }
  2463. /*
  2464. * Recover QUOTAOFF records. We simply make a note of it in the xlog
  2465. * structure, so that we know not to do any dquot item or dquot buffer recovery,
  2466. * of that type.
  2467. */
  2468. STATIC int
  2469. xlog_recover_quotaoff_pass1(
  2470. struct xlog *log,
  2471. struct xlog_recover_item *item)
  2472. {
  2473. xfs_qoff_logformat_t *qoff_f = item->ri_buf[0].i_addr;
  2474. ASSERT(qoff_f);
  2475. /*
  2476. * The logitem format's flag tells us if this was user quotaoff,
  2477. * group/project quotaoff or both.
  2478. */
  2479. if (qoff_f->qf_flags & XFS_UQUOTA_ACCT)
  2480. log->l_quotaoffs_flag |= XFS_DQ_USER;
  2481. if (qoff_f->qf_flags & XFS_PQUOTA_ACCT)
  2482. log->l_quotaoffs_flag |= XFS_DQ_PROJ;
  2483. if (qoff_f->qf_flags & XFS_GQUOTA_ACCT)
  2484. log->l_quotaoffs_flag |= XFS_DQ_GROUP;
  2485. return (0);
  2486. }
  2487. /*
  2488. * Recover a dquot record
  2489. */
  2490. STATIC int
  2491. xlog_recover_dquot_pass2(
  2492. struct xlog *log,
  2493. struct list_head *buffer_list,
  2494. struct xlog_recover_item *item)
  2495. {
  2496. xfs_mount_t *mp = log->l_mp;
  2497. xfs_buf_t *bp;
  2498. struct xfs_disk_dquot *ddq, *recddq;
  2499. int error;
  2500. xfs_dq_logformat_t *dq_f;
  2501. uint type;
  2502. /*
  2503. * Filesystems are required to send in quota flags at mount time.
  2504. */
  2505. if (mp->m_qflags == 0)
  2506. return (0);
  2507. recddq = item->ri_buf[1].i_addr;
  2508. if (recddq == NULL) {
  2509. xfs_alert(log->l_mp, "NULL dquot in %s.", __func__);
  2510. return XFS_ERROR(EIO);
  2511. }
  2512. if (item->ri_buf[1].i_len < sizeof(xfs_disk_dquot_t)) {
  2513. xfs_alert(log->l_mp, "dquot too small (%d) in %s.",
  2514. item->ri_buf[1].i_len, __func__);
  2515. return XFS_ERROR(EIO);
  2516. }
  2517. /*
  2518. * This type of quotas was turned off, so ignore this record.
  2519. */
  2520. type = recddq->d_flags & (XFS_DQ_USER | XFS_DQ_PROJ | XFS_DQ_GROUP);
  2521. ASSERT(type);
  2522. if (log->l_quotaoffs_flag & type)
  2523. return (0);
  2524. /*
  2525. * At this point we know that quota was _not_ turned off.
  2526. * Since the mount flags are not indicating to us otherwise, this
  2527. * must mean that quota is on, and the dquot needs to be replayed.
  2528. * Remember that we may not have fully recovered the superblock yet,
  2529. * so we can't do the usual trick of looking at the SB quota bits.
  2530. *
  2531. * The other possibility, of course, is that the quota subsystem was
  2532. * removed since the last mount - ENOSYS.
  2533. */
  2534. dq_f = item->ri_buf[0].i_addr;
  2535. ASSERT(dq_f);
  2536. error = xfs_qm_dqcheck(mp, recddq, dq_f->qlf_id, 0, XFS_QMOPT_DOWARN,
  2537. "xlog_recover_dquot_pass2 (log copy)");
  2538. if (error)
  2539. return XFS_ERROR(EIO);
  2540. ASSERT(dq_f->qlf_len == 1);
  2541. error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dq_f->qlf_blkno,
  2542. XFS_FSB_TO_BB(mp, dq_f->qlf_len), 0, &bp,
  2543. NULL);
  2544. if (error)
  2545. return error;
  2546. ASSERT(bp);
  2547. ddq = (xfs_disk_dquot_t *)xfs_buf_offset(bp, dq_f->qlf_boffset);
  2548. /*
  2549. * At least the magic num portion should be on disk because this
  2550. * was among a chunk of dquots created earlier, and we did some
  2551. * minimal initialization then.
  2552. */
  2553. error = xfs_qm_dqcheck(mp, ddq, dq_f->qlf_id, 0, XFS_QMOPT_DOWARN,
  2554. "xlog_recover_dquot_pass2");
  2555. if (error) {
  2556. xfs_buf_relse(bp);
  2557. return XFS_ERROR(EIO);
  2558. }
  2559. memcpy(ddq, recddq, item->ri_buf[1].i_len);
  2560. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  2561. xfs_update_cksum((char *)ddq, sizeof(struct xfs_dqblk),
  2562. XFS_DQUOT_CRC_OFF);
  2563. }
  2564. ASSERT(dq_f->qlf_size == 2);
  2565. ASSERT(bp->b_target->bt_mount == mp);
  2566. bp->b_iodone = xlog_recover_iodone;
  2567. xfs_buf_delwri_queue(bp, buffer_list);
  2568. xfs_buf_relse(bp);
  2569. return (0);
  2570. }
  2571. /*
  2572. * This routine is called to create an in-core extent free intent
  2573. * item from the efi format structure which was logged on disk.
  2574. * It allocates an in-core efi, copies the extents from the format
  2575. * structure into it, and adds the efi to the AIL with the given
  2576. * LSN.
  2577. */
  2578. STATIC int
  2579. xlog_recover_efi_pass2(
  2580. struct xlog *log,
  2581. struct xlog_recover_item *item,
  2582. xfs_lsn_t lsn)
  2583. {
  2584. int error;
  2585. xfs_mount_t *mp = log->l_mp;
  2586. xfs_efi_log_item_t *efip;
  2587. xfs_efi_log_format_t *efi_formatp;
  2588. efi_formatp = item->ri_buf[0].i_addr;
  2589. efip = xfs_efi_init(mp, efi_formatp->efi_nextents);
  2590. if ((error = xfs_efi_copy_format(&(item->ri_buf[0]),
  2591. &(efip->efi_format)))) {
  2592. xfs_efi_item_free(efip);
  2593. return error;
  2594. }
  2595. atomic_set(&efip->efi_next_extent, efi_formatp->efi_nextents);
  2596. spin_lock(&log->l_ailp->xa_lock);
  2597. /*
  2598. * xfs_trans_ail_update() drops the AIL lock.
  2599. */
  2600. xfs_trans_ail_update(log->l_ailp, &efip->efi_item, lsn);
  2601. return 0;
  2602. }
  2603. /*
  2604. * This routine is called when an efd format structure is found in
  2605. * a committed transaction in the log. It's purpose is to cancel
  2606. * the corresponding efi if it was still in the log. To do this
  2607. * it searches the AIL for the efi with an id equal to that in the
  2608. * efd format structure. If we find it, we remove the efi from the
  2609. * AIL and free it.
  2610. */
  2611. STATIC int
  2612. xlog_recover_efd_pass2(
  2613. struct xlog *log,
  2614. struct xlog_recover_item *item)
  2615. {
  2616. xfs_efd_log_format_t *efd_formatp;
  2617. xfs_efi_log_item_t *efip = NULL;
  2618. xfs_log_item_t *lip;
  2619. __uint64_t efi_id;
  2620. struct xfs_ail_cursor cur;
  2621. struct xfs_ail *ailp = log->l_ailp;
  2622. efd_formatp = item->ri_buf[0].i_addr;
  2623. ASSERT((item->ri_buf[0].i_len == (sizeof(xfs_efd_log_format_32_t) +
  2624. ((efd_formatp->efd_nextents - 1) * sizeof(xfs_extent_32_t)))) ||
  2625. (item->ri_buf[0].i_len == (sizeof(xfs_efd_log_format_64_t) +
  2626. ((efd_formatp->efd_nextents - 1) * sizeof(xfs_extent_64_t)))));
  2627. efi_id = efd_formatp->efd_efi_id;
  2628. /*
  2629. * Search for the efi with the id in the efd format structure
  2630. * in the AIL.
  2631. */
  2632. spin_lock(&ailp->xa_lock);
  2633. lip = xfs_trans_ail_cursor_first(ailp, &cur, 0);
  2634. while (lip != NULL) {
  2635. if (lip->li_type == XFS_LI_EFI) {
  2636. efip = (xfs_efi_log_item_t *)lip;
  2637. if (efip->efi_format.efi_id == efi_id) {
  2638. /*
  2639. * xfs_trans_ail_delete() drops the
  2640. * AIL lock.
  2641. */
  2642. xfs_trans_ail_delete(ailp, lip,
  2643. SHUTDOWN_CORRUPT_INCORE);
  2644. xfs_efi_item_free(efip);
  2645. spin_lock(&ailp->xa_lock);
  2646. break;
  2647. }
  2648. }
  2649. lip = xfs_trans_ail_cursor_next(ailp, &cur);
  2650. }
  2651. xfs_trans_ail_cursor_done(ailp, &cur);
  2652. spin_unlock(&ailp->xa_lock);
  2653. return 0;
  2654. }
  2655. /*
  2656. * Free up any resources allocated by the transaction
  2657. *
  2658. * Remember that EFIs, EFDs, and IUNLINKs are handled later.
  2659. */
  2660. STATIC void
  2661. xlog_recover_free_trans(
  2662. struct xlog_recover *trans)
  2663. {
  2664. xlog_recover_item_t *item, *n;
  2665. int i;
  2666. list_for_each_entry_safe(item, n, &trans->r_itemq, ri_list) {
  2667. /* Free the regions in the item. */
  2668. list_del(&item->ri_list);
  2669. for (i = 0; i < item->ri_cnt; i++)
  2670. kmem_free(item->ri_buf[i].i_addr);
  2671. /* Free the item itself */
  2672. kmem_free(item->ri_buf);
  2673. kmem_free(item);
  2674. }
  2675. /* Free the transaction recover structure */
  2676. kmem_free(trans);
  2677. }
  2678. STATIC int
  2679. xlog_recover_commit_pass1(
  2680. struct xlog *log,
  2681. struct xlog_recover *trans,
  2682. struct xlog_recover_item *item)
  2683. {
  2684. trace_xfs_log_recover_item_recover(log, trans, item, XLOG_RECOVER_PASS1);
  2685. switch (ITEM_TYPE(item)) {
  2686. case XFS_LI_BUF:
  2687. return xlog_recover_buffer_pass1(log, item);
  2688. case XFS_LI_QUOTAOFF:
  2689. return xlog_recover_quotaoff_pass1(log, item);
  2690. case XFS_LI_INODE:
  2691. case XFS_LI_EFI:
  2692. case XFS_LI_EFD:
  2693. case XFS_LI_DQUOT:
  2694. /* nothing to do in pass 1 */
  2695. return 0;
  2696. default:
  2697. xfs_warn(log->l_mp, "%s: invalid item type (%d)",
  2698. __func__, ITEM_TYPE(item));
  2699. ASSERT(0);
  2700. return XFS_ERROR(EIO);
  2701. }
  2702. }
  2703. STATIC int
  2704. xlog_recover_commit_pass2(
  2705. struct xlog *log,
  2706. struct xlog_recover *trans,
  2707. struct list_head *buffer_list,
  2708. struct xlog_recover_item *item)
  2709. {
  2710. trace_xfs_log_recover_item_recover(log, trans, item, XLOG_RECOVER_PASS2);
  2711. switch (ITEM_TYPE(item)) {
  2712. case XFS_LI_BUF:
  2713. return xlog_recover_buffer_pass2(log, buffer_list, item);
  2714. case XFS_LI_INODE:
  2715. return xlog_recover_inode_pass2(log, buffer_list, item);
  2716. case XFS_LI_EFI:
  2717. return xlog_recover_efi_pass2(log, item, trans->r_lsn);
  2718. case XFS_LI_EFD:
  2719. return xlog_recover_efd_pass2(log, item);
  2720. case XFS_LI_DQUOT:
  2721. return xlog_recover_dquot_pass2(log, buffer_list, item);
  2722. case XFS_LI_QUOTAOFF:
  2723. /* nothing to do in pass2 */
  2724. return 0;
  2725. default:
  2726. xfs_warn(log->l_mp, "%s: invalid item type (%d)",
  2727. __func__, ITEM_TYPE(item));
  2728. ASSERT(0);
  2729. return XFS_ERROR(EIO);
  2730. }
  2731. }
  2732. /*
  2733. * Perform the transaction.
  2734. *
  2735. * If the transaction modifies a buffer or inode, do it now. Otherwise,
  2736. * EFIs and EFDs get queued up by adding entries into the AIL for them.
  2737. */
  2738. STATIC int
  2739. xlog_recover_commit_trans(
  2740. struct xlog *log,
  2741. struct xlog_recover *trans,
  2742. int pass)
  2743. {
  2744. int error = 0, error2;
  2745. xlog_recover_item_t *item;
  2746. LIST_HEAD (buffer_list);
  2747. hlist_del(&trans->r_list);
  2748. error = xlog_recover_reorder_trans(log, trans, pass);
  2749. if (error)
  2750. return error;
  2751. list_for_each_entry(item, &trans->r_itemq, ri_list) {
  2752. switch (pass) {
  2753. case XLOG_RECOVER_PASS1:
  2754. error = xlog_recover_commit_pass1(log, trans, item);
  2755. break;
  2756. case XLOG_RECOVER_PASS2:
  2757. error = xlog_recover_commit_pass2(log, trans,
  2758. &buffer_list, item);
  2759. break;
  2760. default:
  2761. ASSERT(0);
  2762. }
  2763. if (error)
  2764. goto out;
  2765. }
  2766. xlog_recover_free_trans(trans);
  2767. out:
  2768. error2 = xfs_buf_delwri_submit(&buffer_list);
  2769. return error ? error : error2;
  2770. }
  2771. STATIC int
  2772. xlog_recover_unmount_trans(
  2773. struct xlog *log,
  2774. struct xlog_recover *trans)
  2775. {
  2776. /* Do nothing now */
  2777. xfs_warn(log->l_mp, "%s: Unmount LR", __func__);
  2778. return 0;
  2779. }
  2780. /*
  2781. * There are two valid states of the r_state field. 0 indicates that the
  2782. * transaction structure is in a normal state. We have either seen the
  2783. * start of the transaction or the last operation we added was not a partial
  2784. * operation. If the last operation we added to the transaction was a
  2785. * partial operation, we need to mark r_state with XLOG_WAS_CONT_TRANS.
  2786. *
  2787. * NOTE: skip LRs with 0 data length.
  2788. */
  2789. STATIC int
  2790. xlog_recover_process_data(
  2791. struct xlog *log,
  2792. struct hlist_head rhash[],
  2793. struct xlog_rec_header *rhead,
  2794. xfs_caddr_t dp,
  2795. int pass)
  2796. {
  2797. xfs_caddr_t lp;
  2798. int num_logops;
  2799. xlog_op_header_t *ohead;
  2800. xlog_recover_t *trans;
  2801. xlog_tid_t tid;
  2802. int error;
  2803. unsigned long hash;
  2804. uint flags;
  2805. lp = dp + be32_to_cpu(rhead->h_len);
  2806. num_logops = be32_to_cpu(rhead->h_num_logops);
  2807. /* check the log format matches our own - else we can't recover */
  2808. if (xlog_header_check_recover(log->l_mp, rhead))
  2809. return (XFS_ERROR(EIO));
  2810. while ((dp < lp) && num_logops) {
  2811. ASSERT(dp + sizeof(xlog_op_header_t) <= lp);
  2812. ohead = (xlog_op_header_t *)dp;
  2813. dp += sizeof(xlog_op_header_t);
  2814. if (ohead->oh_clientid != XFS_TRANSACTION &&
  2815. ohead->oh_clientid != XFS_LOG) {
  2816. xfs_warn(log->l_mp, "%s: bad clientid 0x%x",
  2817. __func__, ohead->oh_clientid);
  2818. ASSERT(0);
  2819. return (XFS_ERROR(EIO));
  2820. }
  2821. tid = be32_to_cpu(ohead->oh_tid);
  2822. hash = XLOG_RHASH(tid);
  2823. trans = xlog_recover_find_tid(&rhash[hash], tid);
  2824. if (trans == NULL) { /* not found; add new tid */
  2825. if (ohead->oh_flags & XLOG_START_TRANS)
  2826. xlog_recover_new_tid(&rhash[hash], tid,
  2827. be64_to_cpu(rhead->h_lsn));
  2828. } else {
  2829. if (dp + be32_to_cpu(ohead->oh_len) > lp) {
  2830. xfs_warn(log->l_mp, "%s: bad length 0x%x",
  2831. __func__, be32_to_cpu(ohead->oh_len));
  2832. WARN_ON(1);
  2833. return (XFS_ERROR(EIO));
  2834. }
  2835. flags = ohead->oh_flags & ~XLOG_END_TRANS;
  2836. if (flags & XLOG_WAS_CONT_TRANS)
  2837. flags &= ~XLOG_CONTINUE_TRANS;
  2838. switch (flags) {
  2839. case XLOG_COMMIT_TRANS:
  2840. error = xlog_recover_commit_trans(log,
  2841. trans, pass);
  2842. break;
  2843. case XLOG_UNMOUNT_TRANS:
  2844. error = xlog_recover_unmount_trans(log, trans);
  2845. break;
  2846. case XLOG_WAS_CONT_TRANS:
  2847. error = xlog_recover_add_to_cont_trans(log,
  2848. trans, dp,
  2849. be32_to_cpu(ohead->oh_len));
  2850. break;
  2851. case XLOG_START_TRANS:
  2852. xfs_warn(log->l_mp, "%s: bad transaction",
  2853. __func__);
  2854. ASSERT(0);
  2855. error = XFS_ERROR(EIO);
  2856. break;
  2857. case 0:
  2858. case XLOG_CONTINUE_TRANS:
  2859. error = xlog_recover_add_to_trans(log, trans,
  2860. dp, be32_to_cpu(ohead->oh_len));
  2861. break;
  2862. default:
  2863. xfs_warn(log->l_mp, "%s: bad flag 0x%x",
  2864. __func__, flags);
  2865. ASSERT(0);
  2866. error = XFS_ERROR(EIO);
  2867. break;
  2868. }
  2869. if (error)
  2870. return error;
  2871. }
  2872. dp += be32_to_cpu(ohead->oh_len);
  2873. num_logops--;
  2874. }
  2875. return 0;
  2876. }
  2877. /*
  2878. * Process an extent free intent item that was recovered from
  2879. * the log. We need to free the extents that it describes.
  2880. */
  2881. STATIC int
  2882. xlog_recover_process_efi(
  2883. xfs_mount_t *mp,
  2884. xfs_efi_log_item_t *efip)
  2885. {
  2886. xfs_efd_log_item_t *efdp;
  2887. xfs_trans_t *tp;
  2888. int i;
  2889. int error = 0;
  2890. xfs_extent_t *extp;
  2891. xfs_fsblock_t startblock_fsb;
  2892. ASSERT(!test_bit(XFS_EFI_RECOVERED, &efip->efi_flags));
  2893. /*
  2894. * First check the validity of the extents described by the
  2895. * EFI. If any are bad, then assume that all are bad and
  2896. * just toss the EFI.
  2897. */
  2898. for (i = 0; i < efip->efi_format.efi_nextents; i++) {
  2899. extp = &(efip->efi_format.efi_extents[i]);
  2900. startblock_fsb = XFS_BB_TO_FSB(mp,
  2901. XFS_FSB_TO_DADDR(mp, extp->ext_start));
  2902. if ((startblock_fsb == 0) ||
  2903. (extp->ext_len == 0) ||
  2904. (startblock_fsb >= mp->m_sb.sb_dblocks) ||
  2905. (extp->ext_len >= mp->m_sb.sb_agblocks)) {
  2906. /*
  2907. * This will pull the EFI from the AIL and
  2908. * free the memory associated with it.
  2909. */
  2910. set_bit(XFS_EFI_RECOVERED, &efip->efi_flags);
  2911. xfs_efi_release(efip, efip->efi_format.efi_nextents);
  2912. return XFS_ERROR(EIO);
  2913. }
  2914. }
  2915. tp = xfs_trans_alloc(mp, 0);
  2916. error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0, 0, 0);
  2917. if (error)
  2918. goto abort_error;
  2919. efdp = xfs_trans_get_efd(tp, efip, efip->efi_format.efi_nextents);
  2920. for (i = 0; i < efip->efi_format.efi_nextents; i++) {
  2921. extp = &(efip->efi_format.efi_extents[i]);
  2922. error = xfs_free_extent(tp, extp->ext_start, extp->ext_len);
  2923. if (error)
  2924. goto abort_error;
  2925. xfs_trans_log_efd_extent(tp, efdp, extp->ext_start,
  2926. extp->ext_len);
  2927. }
  2928. set_bit(XFS_EFI_RECOVERED, &efip->efi_flags);
  2929. error = xfs_trans_commit(tp, 0);
  2930. return error;
  2931. abort_error:
  2932. xfs_trans_cancel(tp, XFS_TRANS_ABORT);
  2933. return error;
  2934. }
  2935. /*
  2936. * When this is called, all of the EFIs which did not have
  2937. * corresponding EFDs should be in the AIL. What we do now
  2938. * is free the extents associated with each one.
  2939. *
  2940. * Since we process the EFIs in normal transactions, they
  2941. * will be removed at some point after the commit. This prevents
  2942. * us from just walking down the list processing each one.
  2943. * We'll use a flag in the EFI to skip those that we've already
  2944. * processed and use the AIL iteration mechanism's generation
  2945. * count to try to speed this up at least a bit.
  2946. *
  2947. * When we start, we know that the EFIs are the only things in
  2948. * the AIL. As we process them, however, other items are added
  2949. * to the AIL. Since everything added to the AIL must come after
  2950. * everything already in the AIL, we stop processing as soon as
  2951. * we see something other than an EFI in the AIL.
  2952. */
  2953. STATIC int
  2954. xlog_recover_process_efis(
  2955. struct xlog *log)
  2956. {
  2957. xfs_log_item_t *lip;
  2958. xfs_efi_log_item_t *efip;
  2959. int error = 0;
  2960. struct xfs_ail_cursor cur;
  2961. struct xfs_ail *ailp;
  2962. ailp = log->l_ailp;
  2963. spin_lock(&ailp->xa_lock);
  2964. lip = xfs_trans_ail_cursor_first(ailp, &cur, 0);
  2965. while (lip != NULL) {
  2966. /*
  2967. * We're done when we see something other than an EFI.
  2968. * There should be no EFIs left in the AIL now.
  2969. */
  2970. if (lip->li_type != XFS_LI_EFI) {
  2971. #ifdef DEBUG
  2972. for (; lip; lip = xfs_trans_ail_cursor_next(ailp, &cur))
  2973. ASSERT(lip->li_type != XFS_LI_EFI);
  2974. #endif
  2975. break;
  2976. }
  2977. /*
  2978. * Skip EFIs that we've already processed.
  2979. */
  2980. efip = (xfs_efi_log_item_t *)lip;
  2981. if (test_bit(XFS_EFI_RECOVERED, &efip->efi_flags)) {
  2982. lip = xfs_trans_ail_cursor_next(ailp, &cur);
  2983. continue;
  2984. }
  2985. spin_unlock(&ailp->xa_lock);
  2986. error = xlog_recover_process_efi(log->l_mp, efip);
  2987. spin_lock(&ailp->xa_lock);
  2988. if (error)
  2989. goto out;
  2990. lip = xfs_trans_ail_cursor_next(ailp, &cur);
  2991. }
  2992. out:
  2993. xfs_trans_ail_cursor_done(ailp, &cur);
  2994. spin_unlock(&ailp->xa_lock);
  2995. return error;
  2996. }
  2997. /*
  2998. * This routine performs a transaction to null out a bad inode pointer
  2999. * in an agi unlinked inode hash bucket.
  3000. */
  3001. STATIC void
  3002. xlog_recover_clear_agi_bucket(
  3003. xfs_mount_t *mp,
  3004. xfs_agnumber_t agno,
  3005. int bucket)
  3006. {
  3007. xfs_trans_t *tp;
  3008. xfs_agi_t *agi;
  3009. xfs_buf_t *agibp;
  3010. int offset;
  3011. int error;
  3012. tp = xfs_trans_alloc(mp, XFS_TRANS_CLEAR_AGI_BUCKET);
  3013. error = xfs_trans_reserve(tp, 0, XFS_CLEAR_AGI_BUCKET_LOG_RES(mp),
  3014. 0, 0, 0);
  3015. if (error)
  3016. goto out_abort;
  3017. error = xfs_read_agi(mp, tp, agno, &agibp);
  3018. if (error)
  3019. goto out_abort;
  3020. agi = XFS_BUF_TO_AGI(agibp);
  3021. agi->agi_unlinked[bucket] = cpu_to_be32(NULLAGINO);
  3022. offset = offsetof(xfs_agi_t, agi_unlinked) +
  3023. (sizeof(xfs_agino_t) * bucket);
  3024. xfs_trans_log_buf(tp, agibp, offset,
  3025. (offset + sizeof(xfs_agino_t) - 1));
  3026. error = xfs_trans_commit(tp, 0);
  3027. if (error)
  3028. goto out_error;
  3029. return;
  3030. out_abort:
  3031. xfs_trans_cancel(tp, XFS_TRANS_ABORT);
  3032. out_error:
  3033. xfs_warn(mp, "%s: failed to clear agi %d. Continuing.", __func__, agno);
  3034. return;
  3035. }
  3036. STATIC xfs_agino_t
  3037. xlog_recover_process_one_iunlink(
  3038. struct xfs_mount *mp,
  3039. xfs_agnumber_t agno,
  3040. xfs_agino_t agino,
  3041. int bucket)
  3042. {
  3043. struct xfs_buf *ibp;
  3044. struct xfs_dinode *dip;
  3045. struct xfs_inode *ip;
  3046. xfs_ino_t ino;
  3047. int error;
  3048. ino = XFS_AGINO_TO_INO(mp, agno, agino);
  3049. error = xfs_iget(mp, NULL, ino, 0, 0, &ip);
  3050. if (error)
  3051. goto fail;
  3052. /*
  3053. * Get the on disk inode to find the next inode in the bucket.
  3054. */
  3055. error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &ibp, 0, 0);
  3056. if (error)
  3057. goto fail_iput;
  3058. ASSERT(ip->i_d.di_nlink == 0);
  3059. ASSERT(ip->i_d.di_mode != 0);
  3060. /* setup for the next pass */
  3061. agino = be32_to_cpu(dip->di_next_unlinked);
  3062. xfs_buf_relse(ibp);
  3063. /*
  3064. * Prevent any DMAPI event from being sent when the reference on
  3065. * the inode is dropped.
  3066. */
  3067. ip->i_d.di_dmevmask = 0;
  3068. IRELE(ip);
  3069. return agino;
  3070. fail_iput:
  3071. IRELE(ip);
  3072. fail:
  3073. /*
  3074. * We can't read in the inode this bucket points to, or this inode
  3075. * is messed up. Just ditch this bucket of inodes. We will lose
  3076. * some inodes and space, but at least we won't hang.
  3077. *
  3078. * Call xlog_recover_clear_agi_bucket() to perform a transaction to
  3079. * clear the inode pointer in the bucket.
  3080. */
  3081. xlog_recover_clear_agi_bucket(mp, agno, bucket);
  3082. return NULLAGINO;
  3083. }
  3084. /*
  3085. * xlog_iunlink_recover
  3086. *
  3087. * This is called during recovery to process any inodes which
  3088. * we unlinked but not freed when the system crashed. These
  3089. * inodes will be on the lists in the AGI blocks. What we do
  3090. * here is scan all the AGIs and fully truncate and free any
  3091. * inodes found on the lists. Each inode is removed from the
  3092. * lists when it has been fully truncated and is freed. The
  3093. * freeing of the inode and its removal from the list must be
  3094. * atomic.
  3095. */
  3096. STATIC void
  3097. xlog_recover_process_iunlinks(
  3098. struct xlog *log)
  3099. {
  3100. xfs_mount_t *mp;
  3101. xfs_agnumber_t agno;
  3102. xfs_agi_t *agi;
  3103. xfs_buf_t *agibp;
  3104. xfs_agino_t agino;
  3105. int bucket;
  3106. int error;
  3107. uint mp_dmevmask;
  3108. mp = log->l_mp;
  3109. /*
  3110. * Prevent any DMAPI event from being sent while in this function.
  3111. */
  3112. mp_dmevmask = mp->m_dmevmask;
  3113. mp->m_dmevmask = 0;
  3114. for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
  3115. /*
  3116. * Find the agi for this ag.
  3117. */
  3118. error = xfs_read_agi(mp, NULL, agno, &agibp);
  3119. if (error) {
  3120. /*
  3121. * AGI is b0rked. Don't process it.
  3122. *
  3123. * We should probably mark the filesystem as corrupt
  3124. * after we've recovered all the ag's we can....
  3125. */
  3126. continue;
  3127. }
  3128. /*
  3129. * Unlock the buffer so that it can be acquired in the normal
  3130. * course of the transaction to truncate and free each inode.
  3131. * Because we are not racing with anyone else here for the AGI
  3132. * buffer, we don't even need to hold it locked to read the
  3133. * initial unlinked bucket entries out of the buffer. We keep
  3134. * buffer reference though, so that it stays pinned in memory
  3135. * while we need the buffer.
  3136. */
  3137. agi = XFS_BUF_TO_AGI(agibp);
  3138. xfs_buf_unlock(agibp);
  3139. for (bucket = 0; bucket < XFS_AGI_UNLINKED_BUCKETS; bucket++) {
  3140. agino = be32_to_cpu(agi->agi_unlinked[bucket]);
  3141. while (agino != NULLAGINO) {
  3142. agino = xlog_recover_process_one_iunlink(mp,
  3143. agno, agino, bucket);
  3144. }
  3145. }
  3146. xfs_buf_rele(agibp);
  3147. }
  3148. mp->m_dmevmask = mp_dmevmask;
  3149. }
  3150. /*
  3151. * Upack the log buffer data and crc check it. If the check fails, issue a
  3152. * warning if and only if the CRC in the header is non-zero. This makes the
  3153. * check an advisory warning, and the zero CRC check will prevent failure
  3154. * warnings from being emitted when upgrading the kernel from one that does not
  3155. * add CRCs by default.
  3156. *
  3157. * When filesystems are CRC enabled, this CRC mismatch becomes a fatal log
  3158. * corruption failure
  3159. */
  3160. STATIC int
  3161. xlog_unpack_data_crc(
  3162. struct xlog_rec_header *rhead,
  3163. xfs_caddr_t dp,
  3164. struct xlog *log)
  3165. {
  3166. __le32 crc;
  3167. crc = xlog_cksum(log, rhead, dp, be32_to_cpu(rhead->h_len));
  3168. if (crc != rhead->h_crc) {
  3169. if (rhead->h_crc || xfs_sb_version_hascrc(&log->l_mp->m_sb)) {
  3170. xfs_alert(log->l_mp,
  3171. "log record CRC mismatch: found 0x%x, expected 0x%x.\n",
  3172. le32_to_cpu(rhead->h_crc),
  3173. le32_to_cpu(crc));
  3174. xfs_hex_dump(dp, 32);
  3175. }
  3176. /*
  3177. * If we've detected a log record corruption, then we can't
  3178. * recover past this point. Abort recovery if we are enforcing
  3179. * CRC protection by punting an error back up the stack.
  3180. */
  3181. if (xfs_sb_version_hascrc(&log->l_mp->m_sb))
  3182. return EFSCORRUPTED;
  3183. }
  3184. return 0;
  3185. }
  3186. STATIC int
  3187. xlog_unpack_data(
  3188. struct xlog_rec_header *rhead,
  3189. xfs_caddr_t dp,
  3190. struct xlog *log)
  3191. {
  3192. int i, j, k;
  3193. int error;
  3194. error = xlog_unpack_data_crc(rhead, dp, log);
  3195. if (error)
  3196. return error;
  3197. for (i = 0; i < BTOBB(be32_to_cpu(rhead->h_len)) &&
  3198. i < (XLOG_HEADER_CYCLE_SIZE / BBSIZE); i++) {
  3199. *(__be32 *)dp = *(__be32 *)&rhead->h_cycle_data[i];
  3200. dp += BBSIZE;
  3201. }
  3202. if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
  3203. xlog_in_core_2_t *xhdr = (xlog_in_core_2_t *)rhead;
  3204. for ( ; i < BTOBB(be32_to_cpu(rhead->h_len)); i++) {
  3205. j = i / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
  3206. k = i % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
  3207. *(__be32 *)dp = xhdr[j].hic_xheader.xh_cycle_data[k];
  3208. dp += BBSIZE;
  3209. }
  3210. }
  3211. return 0;
  3212. }
  3213. STATIC int
  3214. xlog_valid_rec_header(
  3215. struct xlog *log,
  3216. struct xlog_rec_header *rhead,
  3217. xfs_daddr_t blkno)
  3218. {
  3219. int hlen;
  3220. if (unlikely(rhead->h_magicno != cpu_to_be32(XLOG_HEADER_MAGIC_NUM))) {
  3221. XFS_ERROR_REPORT("xlog_valid_rec_header(1)",
  3222. XFS_ERRLEVEL_LOW, log->l_mp);
  3223. return XFS_ERROR(EFSCORRUPTED);
  3224. }
  3225. if (unlikely(
  3226. (!rhead->h_version ||
  3227. (be32_to_cpu(rhead->h_version) & (~XLOG_VERSION_OKBITS))))) {
  3228. xfs_warn(log->l_mp, "%s: unrecognised log version (%d).",
  3229. __func__, be32_to_cpu(rhead->h_version));
  3230. return XFS_ERROR(EIO);
  3231. }
  3232. /* LR body must have data or it wouldn't have been written */
  3233. hlen = be32_to_cpu(rhead->h_len);
  3234. if (unlikely( hlen <= 0 || hlen > INT_MAX )) {
  3235. XFS_ERROR_REPORT("xlog_valid_rec_header(2)",
  3236. XFS_ERRLEVEL_LOW, log->l_mp);
  3237. return XFS_ERROR(EFSCORRUPTED);
  3238. }
  3239. if (unlikely( blkno > log->l_logBBsize || blkno > INT_MAX )) {
  3240. XFS_ERROR_REPORT("xlog_valid_rec_header(3)",
  3241. XFS_ERRLEVEL_LOW, log->l_mp);
  3242. return XFS_ERROR(EFSCORRUPTED);
  3243. }
  3244. return 0;
  3245. }
  3246. /*
  3247. * Read the log from tail to head and process the log records found.
  3248. * Handle the two cases where the tail and head are in the same cycle
  3249. * and where the active portion of the log wraps around the end of
  3250. * the physical log separately. The pass parameter is passed through
  3251. * to the routines called to process the data and is not looked at
  3252. * here.
  3253. */
  3254. STATIC int
  3255. xlog_do_recovery_pass(
  3256. struct xlog *log,
  3257. xfs_daddr_t head_blk,
  3258. xfs_daddr_t tail_blk,
  3259. int pass)
  3260. {
  3261. xlog_rec_header_t *rhead;
  3262. xfs_daddr_t blk_no;
  3263. xfs_caddr_t offset;
  3264. xfs_buf_t *hbp, *dbp;
  3265. int error = 0, h_size;
  3266. int bblks, split_bblks;
  3267. int hblks, split_hblks, wrapped_hblks;
  3268. struct hlist_head rhash[XLOG_RHASH_SIZE];
  3269. ASSERT(head_blk != tail_blk);
  3270. /*
  3271. * Read the header of the tail block and get the iclog buffer size from
  3272. * h_size. Use this to tell how many sectors make up the log header.
  3273. */
  3274. if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
  3275. /*
  3276. * When using variable length iclogs, read first sector of
  3277. * iclog header and extract the header size from it. Get a
  3278. * new hbp that is the correct size.
  3279. */
  3280. hbp = xlog_get_bp(log, 1);
  3281. if (!hbp)
  3282. return ENOMEM;
  3283. error = xlog_bread(log, tail_blk, 1, hbp, &offset);
  3284. if (error)
  3285. goto bread_err1;
  3286. rhead = (xlog_rec_header_t *)offset;
  3287. error = xlog_valid_rec_header(log, rhead, tail_blk);
  3288. if (error)
  3289. goto bread_err1;
  3290. h_size = be32_to_cpu(rhead->h_size);
  3291. if ((be32_to_cpu(rhead->h_version) & XLOG_VERSION_2) &&
  3292. (h_size > XLOG_HEADER_CYCLE_SIZE)) {
  3293. hblks = h_size / XLOG_HEADER_CYCLE_SIZE;
  3294. if (h_size % XLOG_HEADER_CYCLE_SIZE)
  3295. hblks++;
  3296. xlog_put_bp(hbp);
  3297. hbp = xlog_get_bp(log, hblks);
  3298. } else {
  3299. hblks = 1;
  3300. }
  3301. } else {
  3302. ASSERT(log->l_sectBBsize == 1);
  3303. hblks = 1;
  3304. hbp = xlog_get_bp(log, 1);
  3305. h_size = XLOG_BIG_RECORD_BSIZE;
  3306. }
  3307. if (!hbp)
  3308. return ENOMEM;
  3309. dbp = xlog_get_bp(log, BTOBB(h_size));
  3310. if (!dbp) {
  3311. xlog_put_bp(hbp);
  3312. return ENOMEM;
  3313. }
  3314. memset(rhash, 0, sizeof(rhash));
  3315. if (tail_blk <= head_blk) {
  3316. for (blk_no = tail_blk; blk_no < head_blk; ) {
  3317. error = xlog_bread(log, blk_no, hblks, hbp, &offset);
  3318. if (error)
  3319. goto bread_err2;
  3320. rhead = (xlog_rec_header_t *)offset;
  3321. error = xlog_valid_rec_header(log, rhead, blk_no);
  3322. if (error)
  3323. goto bread_err2;
  3324. /* blocks in data section */
  3325. bblks = (int)BTOBB(be32_to_cpu(rhead->h_len));
  3326. error = xlog_bread(log, blk_no + hblks, bblks, dbp,
  3327. &offset);
  3328. if (error)
  3329. goto bread_err2;
  3330. error = xlog_unpack_data(rhead, offset, log);
  3331. if (error)
  3332. goto bread_err2;
  3333. error = xlog_recover_process_data(log,
  3334. rhash, rhead, offset, pass);
  3335. if (error)
  3336. goto bread_err2;
  3337. blk_no += bblks + hblks;
  3338. }
  3339. } else {
  3340. /*
  3341. * Perform recovery around the end of the physical log.
  3342. * When the head is not on the same cycle number as the tail,
  3343. * we can't do a sequential recovery as above.
  3344. */
  3345. blk_no = tail_blk;
  3346. while (blk_no < log->l_logBBsize) {
  3347. /*
  3348. * Check for header wrapping around physical end-of-log
  3349. */
  3350. offset = hbp->b_addr;
  3351. split_hblks = 0;
  3352. wrapped_hblks = 0;
  3353. if (blk_no + hblks <= log->l_logBBsize) {
  3354. /* Read header in one read */
  3355. error = xlog_bread(log, blk_no, hblks, hbp,
  3356. &offset);
  3357. if (error)
  3358. goto bread_err2;
  3359. } else {
  3360. /* This LR is split across physical log end */
  3361. if (blk_no != log->l_logBBsize) {
  3362. /* some data before physical log end */
  3363. ASSERT(blk_no <= INT_MAX);
  3364. split_hblks = log->l_logBBsize - (int)blk_no;
  3365. ASSERT(split_hblks > 0);
  3366. error = xlog_bread(log, blk_no,
  3367. split_hblks, hbp,
  3368. &offset);
  3369. if (error)
  3370. goto bread_err2;
  3371. }
  3372. /*
  3373. * Note: this black magic still works with
  3374. * large sector sizes (non-512) only because:
  3375. * - we increased the buffer size originally
  3376. * by 1 sector giving us enough extra space
  3377. * for the second read;
  3378. * - the log start is guaranteed to be sector
  3379. * aligned;
  3380. * - we read the log end (LR header start)
  3381. * _first_, then the log start (LR header end)
  3382. * - order is important.
  3383. */
  3384. wrapped_hblks = hblks - split_hblks;
  3385. error = xlog_bread_offset(log, 0,
  3386. wrapped_hblks, hbp,
  3387. offset + BBTOB(split_hblks));
  3388. if (error)
  3389. goto bread_err2;
  3390. }
  3391. rhead = (xlog_rec_header_t *)offset;
  3392. error = xlog_valid_rec_header(log, rhead,
  3393. split_hblks ? blk_no : 0);
  3394. if (error)
  3395. goto bread_err2;
  3396. bblks = (int)BTOBB(be32_to_cpu(rhead->h_len));
  3397. blk_no += hblks;
  3398. /* Read in data for log record */
  3399. if (blk_no + bblks <= log->l_logBBsize) {
  3400. error = xlog_bread(log, blk_no, bblks, dbp,
  3401. &offset);
  3402. if (error)
  3403. goto bread_err2;
  3404. } else {
  3405. /* This log record is split across the
  3406. * physical end of log */
  3407. offset = dbp->b_addr;
  3408. split_bblks = 0;
  3409. if (blk_no != log->l_logBBsize) {
  3410. /* some data is before the physical
  3411. * end of log */
  3412. ASSERT(!wrapped_hblks);
  3413. ASSERT(blk_no <= INT_MAX);
  3414. split_bblks =
  3415. log->l_logBBsize - (int)blk_no;
  3416. ASSERT(split_bblks > 0);
  3417. error = xlog_bread(log, blk_no,
  3418. split_bblks, dbp,
  3419. &offset);
  3420. if (error)
  3421. goto bread_err2;
  3422. }
  3423. /*
  3424. * Note: this black magic still works with
  3425. * large sector sizes (non-512) only because:
  3426. * - we increased the buffer size originally
  3427. * by 1 sector giving us enough extra space
  3428. * for the second read;
  3429. * - the log start is guaranteed to be sector
  3430. * aligned;
  3431. * - we read the log end (LR header start)
  3432. * _first_, then the log start (LR header end)
  3433. * - order is important.
  3434. */
  3435. error = xlog_bread_offset(log, 0,
  3436. bblks - split_bblks, dbp,
  3437. offset + BBTOB(split_bblks));
  3438. if (error)
  3439. goto bread_err2;
  3440. }
  3441. error = xlog_unpack_data(rhead, offset, log);
  3442. if (error)
  3443. goto bread_err2;
  3444. error = xlog_recover_process_data(log, rhash,
  3445. rhead, offset, pass);
  3446. if (error)
  3447. goto bread_err2;
  3448. blk_no += bblks;
  3449. }
  3450. ASSERT(blk_no >= log->l_logBBsize);
  3451. blk_no -= log->l_logBBsize;
  3452. /* read first part of physical log */
  3453. while (blk_no < head_blk) {
  3454. error = xlog_bread(log, blk_no, hblks, hbp, &offset);
  3455. if (error)
  3456. goto bread_err2;
  3457. rhead = (xlog_rec_header_t *)offset;
  3458. error = xlog_valid_rec_header(log, rhead, blk_no);
  3459. if (error)
  3460. goto bread_err2;
  3461. bblks = (int)BTOBB(be32_to_cpu(rhead->h_len));
  3462. error = xlog_bread(log, blk_no+hblks, bblks, dbp,
  3463. &offset);
  3464. if (error)
  3465. goto bread_err2;
  3466. error = xlog_unpack_data(rhead, offset, log);
  3467. if (error)
  3468. goto bread_err2;
  3469. error = xlog_recover_process_data(log, rhash,
  3470. rhead, offset, pass);
  3471. if (error)
  3472. goto bread_err2;
  3473. blk_no += bblks + hblks;
  3474. }
  3475. }
  3476. bread_err2:
  3477. xlog_put_bp(dbp);
  3478. bread_err1:
  3479. xlog_put_bp(hbp);
  3480. return error;
  3481. }
  3482. /*
  3483. * Do the recovery of the log. We actually do this in two phases.
  3484. * The two passes are necessary in order to implement the function
  3485. * of cancelling a record written into the log. The first pass
  3486. * determines those things which have been cancelled, and the
  3487. * second pass replays log items normally except for those which
  3488. * have been cancelled. The handling of the replay and cancellations
  3489. * takes place in the log item type specific routines.
  3490. *
  3491. * The table of items which have cancel records in the log is allocated
  3492. * and freed at this level, since only here do we know when all of
  3493. * the log recovery has been completed.
  3494. */
  3495. STATIC int
  3496. xlog_do_log_recovery(
  3497. struct xlog *log,
  3498. xfs_daddr_t head_blk,
  3499. xfs_daddr_t tail_blk)
  3500. {
  3501. int error, i;
  3502. ASSERT(head_blk != tail_blk);
  3503. /*
  3504. * First do a pass to find all of the cancelled buf log items.
  3505. * Store them in the buf_cancel_table for use in the second pass.
  3506. */
  3507. log->l_buf_cancel_table = kmem_zalloc(XLOG_BC_TABLE_SIZE *
  3508. sizeof(struct list_head),
  3509. KM_SLEEP);
  3510. for (i = 0; i < XLOG_BC_TABLE_SIZE; i++)
  3511. INIT_LIST_HEAD(&log->l_buf_cancel_table[i]);
  3512. error = xlog_do_recovery_pass(log, head_blk, tail_blk,
  3513. XLOG_RECOVER_PASS1);
  3514. if (error != 0) {
  3515. kmem_free(log->l_buf_cancel_table);
  3516. log->l_buf_cancel_table = NULL;
  3517. return error;
  3518. }
  3519. /*
  3520. * Then do a second pass to actually recover the items in the log.
  3521. * When it is complete free the table of buf cancel items.
  3522. */
  3523. error = xlog_do_recovery_pass(log, head_blk, tail_blk,
  3524. XLOG_RECOVER_PASS2);
  3525. #ifdef DEBUG
  3526. if (!error) {
  3527. int i;
  3528. for (i = 0; i < XLOG_BC_TABLE_SIZE; i++)
  3529. ASSERT(list_empty(&log->l_buf_cancel_table[i]));
  3530. }
  3531. #endif /* DEBUG */
  3532. kmem_free(log->l_buf_cancel_table);
  3533. log->l_buf_cancel_table = NULL;
  3534. return error;
  3535. }
  3536. /*
  3537. * Do the actual recovery
  3538. */
  3539. STATIC int
  3540. xlog_do_recover(
  3541. struct xlog *log,
  3542. xfs_daddr_t head_blk,
  3543. xfs_daddr_t tail_blk)
  3544. {
  3545. int error;
  3546. xfs_buf_t *bp;
  3547. xfs_sb_t *sbp;
  3548. /*
  3549. * First replay the images in the log.
  3550. */
  3551. error = xlog_do_log_recovery(log, head_blk, tail_blk);
  3552. if (error)
  3553. return error;
  3554. /*
  3555. * If IO errors happened during recovery, bail out.
  3556. */
  3557. if (XFS_FORCED_SHUTDOWN(log->l_mp)) {
  3558. return (EIO);
  3559. }
  3560. /*
  3561. * We now update the tail_lsn since much of the recovery has completed
  3562. * and there may be space available to use. If there were no extent
  3563. * or iunlinks, we can free up the entire log and set the tail_lsn to
  3564. * be the last_sync_lsn. This was set in xlog_find_tail to be the
  3565. * lsn of the last known good LR on disk. If there are extent frees
  3566. * or iunlinks they will have some entries in the AIL; so we look at
  3567. * the AIL to determine how to set the tail_lsn.
  3568. */
  3569. xlog_assign_tail_lsn(log->l_mp);
  3570. /*
  3571. * Now that we've finished replaying all buffer and inode
  3572. * updates, re-read in the superblock and reverify it.
  3573. */
  3574. bp = xfs_getsb(log->l_mp, 0);
  3575. XFS_BUF_UNDONE(bp);
  3576. ASSERT(!(XFS_BUF_ISWRITE(bp)));
  3577. XFS_BUF_READ(bp);
  3578. XFS_BUF_UNASYNC(bp);
  3579. bp->b_ops = &xfs_sb_buf_ops;
  3580. xfsbdstrat(log->l_mp, bp);
  3581. error = xfs_buf_iowait(bp);
  3582. if (error) {
  3583. xfs_buf_ioerror_alert(bp, __func__);
  3584. ASSERT(0);
  3585. xfs_buf_relse(bp);
  3586. return error;
  3587. }
  3588. /* Convert superblock from on-disk format */
  3589. sbp = &log->l_mp->m_sb;
  3590. xfs_sb_from_disk(sbp, XFS_BUF_TO_SBP(bp));
  3591. ASSERT(sbp->sb_magicnum == XFS_SB_MAGIC);
  3592. ASSERT(xfs_sb_good_version(sbp));
  3593. xfs_buf_relse(bp);
  3594. /* We've re-read the superblock so re-initialize per-cpu counters */
  3595. xfs_icsb_reinit_counters(log->l_mp);
  3596. xlog_recover_check_summary(log);
  3597. /* Normal transactions can now occur */
  3598. log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
  3599. return 0;
  3600. }
  3601. /*
  3602. * Perform recovery and re-initialize some log variables in xlog_find_tail.
  3603. *
  3604. * Return error or zero.
  3605. */
  3606. int
  3607. xlog_recover(
  3608. struct xlog *log)
  3609. {
  3610. xfs_daddr_t head_blk, tail_blk;
  3611. int error;
  3612. /* find the tail of the log */
  3613. if ((error = xlog_find_tail(log, &head_blk, &tail_blk)))
  3614. return error;
  3615. if (tail_blk != head_blk) {
  3616. /* There used to be a comment here:
  3617. *
  3618. * disallow recovery on read-only mounts. note -- mount
  3619. * checks for ENOSPC and turns it into an intelligent
  3620. * error message.
  3621. * ...but this is no longer true. Now, unless you specify
  3622. * NORECOVERY (in which case this function would never be
  3623. * called), we just go ahead and recover. We do this all
  3624. * under the vfs layer, so we can get away with it unless
  3625. * the device itself is read-only, in which case we fail.
  3626. */
  3627. if ((error = xfs_dev_is_read_only(log->l_mp, "recovery"))) {
  3628. return error;
  3629. }
  3630. /*
  3631. * Version 5 superblock log feature mask validation. We know the
  3632. * log is dirty so check if there are any unknown log features
  3633. * in what we need to recover. If there are unknown features
  3634. * (e.g. unsupported transactions, then simply reject the
  3635. * attempt at recovery before touching anything.
  3636. */
  3637. if (XFS_SB_VERSION_NUM(&log->l_mp->m_sb) == XFS_SB_VERSION_5 &&
  3638. xfs_sb_has_incompat_log_feature(&log->l_mp->m_sb,
  3639. XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN)) {
  3640. xfs_warn(log->l_mp,
  3641. "Superblock has unknown incompatible log features (0x%x) enabled.\n"
  3642. "The log can not be fully and/or safely recovered by this kernel.\n"
  3643. "Please recover the log on a kernel that supports the unknown features.",
  3644. (log->l_mp->m_sb.sb_features_log_incompat &
  3645. XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN));
  3646. return EINVAL;
  3647. }
  3648. xfs_notice(log->l_mp, "Starting recovery (logdev: %s)",
  3649. log->l_mp->m_logname ? log->l_mp->m_logname
  3650. : "internal");
  3651. error = xlog_do_recover(log, head_blk, tail_blk);
  3652. log->l_flags |= XLOG_RECOVERY_NEEDED;
  3653. }
  3654. return error;
  3655. }
  3656. /*
  3657. * In the first part of recovery we replay inodes and buffers and build
  3658. * up the list of extent free items which need to be processed. Here
  3659. * we process the extent free items and clean up the on disk unlinked
  3660. * inode lists. This is separated from the first part of recovery so
  3661. * that the root and real-time bitmap inodes can be read in from disk in
  3662. * between the two stages. This is necessary so that we can free space
  3663. * in the real-time portion of the file system.
  3664. */
  3665. int
  3666. xlog_recover_finish(
  3667. struct xlog *log)
  3668. {
  3669. /*
  3670. * Now we're ready to do the transactions needed for the
  3671. * rest of recovery. Start with completing all the extent
  3672. * free intent records and then process the unlinked inode
  3673. * lists. At this point, we essentially run in normal mode
  3674. * except that we're still performing recovery actions
  3675. * rather than accepting new requests.
  3676. */
  3677. if (log->l_flags & XLOG_RECOVERY_NEEDED) {
  3678. int error;
  3679. error = xlog_recover_process_efis(log);
  3680. if (error) {
  3681. xfs_alert(log->l_mp, "Failed to recover EFIs");
  3682. return error;
  3683. }
  3684. /*
  3685. * Sync the log to get all the EFIs out of the AIL.
  3686. * This isn't absolutely necessary, but it helps in
  3687. * case the unlink transactions would have problems
  3688. * pushing the EFIs out of the way.
  3689. */
  3690. xfs_log_force(log->l_mp, XFS_LOG_SYNC);
  3691. xlog_recover_process_iunlinks(log);
  3692. xlog_recover_check_summary(log);
  3693. xfs_notice(log->l_mp, "Ending recovery (logdev: %s)",
  3694. log->l_mp->m_logname ? log->l_mp->m_logname
  3695. : "internal");
  3696. log->l_flags &= ~XLOG_RECOVERY_NEEDED;
  3697. } else {
  3698. xfs_info(log->l_mp, "Ending clean mount");
  3699. }
  3700. return 0;
  3701. }
  3702. #if defined(DEBUG)
  3703. /*
  3704. * Read all of the agf and agi counters and check that they
  3705. * are consistent with the superblock counters.
  3706. */
  3707. void
  3708. xlog_recover_check_summary(
  3709. struct xlog *log)
  3710. {
  3711. xfs_mount_t *mp;
  3712. xfs_agf_t *agfp;
  3713. xfs_buf_t *agfbp;
  3714. xfs_buf_t *agibp;
  3715. xfs_agnumber_t agno;
  3716. __uint64_t freeblks;
  3717. __uint64_t itotal;
  3718. __uint64_t ifree;
  3719. int error;
  3720. mp = log->l_mp;
  3721. freeblks = 0LL;
  3722. itotal = 0LL;
  3723. ifree = 0LL;
  3724. for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
  3725. error = xfs_read_agf(mp, NULL, agno, 0, &agfbp);
  3726. if (error) {
  3727. xfs_alert(mp, "%s agf read failed agno %d error %d",
  3728. __func__, agno, error);
  3729. } else {
  3730. agfp = XFS_BUF_TO_AGF(agfbp);
  3731. freeblks += be32_to_cpu(agfp->agf_freeblks) +
  3732. be32_to_cpu(agfp->agf_flcount);
  3733. xfs_buf_relse(agfbp);
  3734. }
  3735. error = xfs_read_agi(mp, NULL, agno, &agibp);
  3736. if (error) {
  3737. xfs_alert(mp, "%s agi read failed agno %d error %d",
  3738. __func__, agno, error);
  3739. } else {
  3740. struct xfs_agi *agi = XFS_BUF_TO_AGI(agibp);
  3741. itotal += be32_to_cpu(agi->agi_count);
  3742. ifree += be32_to_cpu(agi->agi_freecount);
  3743. xfs_buf_relse(agibp);
  3744. }
  3745. }
  3746. }
  3747. #endif /* DEBUG */