xfs_log_recover.c 97 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_shared.h"
  9. #include "xfs_format.h"
  10. #include "xfs_log_format.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_bit.h"
  13. #include "xfs_sb.h"
  14. #include "xfs_mount.h"
  15. #include "xfs_defer.h"
  16. #include "xfs_inode.h"
  17. #include "xfs_trans.h"
  18. #include "xfs_log.h"
  19. #include "xfs_log_priv.h"
  20. #include "xfs_log_recover.h"
  21. #include "xfs_trans_priv.h"
  22. #include "xfs_alloc.h"
  23. #include "xfs_ialloc.h"
  24. #include "xfs_trace.h"
  25. #include "xfs_icache.h"
  26. #include "xfs_error.h"
  27. #include "xfs_buf_item.h"
  28. #include "xfs_ag.h"
  29. #include "xfs_quota.h"
  30. #include "xfs_reflink.h"
  31. #define BLK_AVG(blk1, blk2) ((blk1+blk2) >> 1)
  32. STATIC int
  33. xlog_find_zeroed(
  34. struct xlog *,
  35. xfs_daddr_t *);
  36. STATIC int
  37. xlog_clear_stale_blocks(
  38. struct xlog *,
  39. xfs_lsn_t);
  40. STATIC int
  41. xlog_do_recovery_pass(
  42. struct xlog *, xfs_daddr_t, xfs_daddr_t, int, xfs_daddr_t *);
  43. /*
  44. * Sector aligned buffer routines for buffer create/read/write/access
  45. */
  46. /*
  47. * Verify the log-relative block number and length in basic blocks are valid for
  48. * an operation involving the given XFS log buffer. Returns true if the fields
  49. * are valid, false otherwise.
  50. */
  51. static inline bool
  52. xlog_verify_bno(
  53. struct xlog *log,
  54. xfs_daddr_t blk_no,
  55. int bbcount)
  56. {
  57. if (blk_no < 0 || blk_no >= log->l_logBBsize)
  58. return false;
  59. if (bbcount <= 0 || (blk_no + bbcount) > log->l_logBBsize)
  60. return false;
  61. return true;
  62. }
  63. /*
  64. * Allocate a buffer to hold log data. The buffer needs to be able to map to
  65. * a range of nbblks basic blocks at any valid offset within the log.
  66. */
  67. static char *
  68. xlog_alloc_buffer(
  69. struct xlog *log,
  70. int nbblks)
  71. {
  72. /*
  73. * Pass log block 0 since we don't have an addr yet, buffer will be
  74. * verified on read.
  75. */
  76. if (XFS_IS_CORRUPT(log->l_mp, !xlog_verify_bno(log, 0, nbblks))) {
  77. xfs_warn(log->l_mp, "Invalid block length (0x%x) for buffer",
  78. nbblks);
  79. return NULL;
  80. }
  81. /*
  82. * We do log I/O in units of log sectors (a power-of-2 multiple of the
  83. * basic block size), so we round up the requested size to accommodate
  84. * the basic blocks required for complete log sectors.
  85. *
  86. * In addition, the buffer may be used for a non-sector-aligned block
  87. * offset, in which case an I/O of the requested size could extend
  88. * beyond the end of the buffer. If the requested size is only 1 basic
  89. * block it will never straddle a sector boundary, so this won't be an
  90. * issue. Nor will this be a problem if the log I/O is done in basic
  91. * blocks (sector size 1). But otherwise we extend the buffer by one
  92. * extra log sector to ensure there's space to accommodate this
  93. * possibility.
  94. */
  95. if (nbblks > 1 && log->l_sectBBsize > 1)
  96. nbblks += log->l_sectBBsize;
  97. nbblks = round_up(nbblks, log->l_sectBBsize);
  98. return kvzalloc(BBTOB(nbblks), GFP_KERNEL | __GFP_RETRY_MAYFAIL);
  99. }
  100. /*
  101. * Return the address of the start of the given block number's data
  102. * in a log buffer. The buffer covers a log sector-aligned region.
  103. */
  104. static inline unsigned int
  105. xlog_align(
  106. struct xlog *log,
  107. xfs_daddr_t blk_no)
  108. {
  109. return BBTOB(blk_no & ((xfs_daddr_t)log->l_sectBBsize - 1));
  110. }
  111. static int
  112. xlog_do_io(
  113. struct xlog *log,
  114. xfs_daddr_t blk_no,
  115. unsigned int nbblks,
  116. char *data,
  117. enum req_op op)
  118. {
  119. int error;
  120. if (XFS_IS_CORRUPT(log->l_mp, !xlog_verify_bno(log, blk_no, nbblks))) {
  121. xfs_warn(log->l_mp,
  122. "Invalid log block/length (0x%llx, 0x%x) for buffer",
  123. blk_no, nbblks);
  124. return -EFSCORRUPTED;
  125. }
  126. blk_no = round_down(blk_no, log->l_sectBBsize);
  127. nbblks = round_up(nbblks, log->l_sectBBsize);
  128. ASSERT(nbblks > 0);
  129. error = xfs_rw_bdev(log->l_targ->bt_bdev, log->l_logBBstart + blk_no,
  130. BBTOB(nbblks), data, op);
  131. if (error && !xlog_is_shutdown(log)) {
  132. xfs_alert(log->l_mp,
  133. "log recovery %s I/O error at daddr 0x%llx len %d error %d",
  134. op == REQ_OP_WRITE ? "write" : "read",
  135. blk_no, nbblks, error);
  136. }
  137. return error;
  138. }
  139. STATIC int
  140. xlog_bread_noalign(
  141. struct xlog *log,
  142. xfs_daddr_t blk_no,
  143. int nbblks,
  144. char *data)
  145. {
  146. return xlog_do_io(log, blk_no, nbblks, data, REQ_OP_READ);
  147. }
  148. STATIC int
  149. xlog_bread(
  150. struct xlog *log,
  151. xfs_daddr_t blk_no,
  152. int nbblks,
  153. char *data,
  154. char **offset)
  155. {
  156. int error;
  157. error = xlog_do_io(log, blk_no, nbblks, data, REQ_OP_READ);
  158. if (!error)
  159. *offset = data + xlog_align(log, blk_no);
  160. return error;
  161. }
  162. STATIC int
  163. xlog_bwrite(
  164. struct xlog *log,
  165. xfs_daddr_t blk_no,
  166. int nbblks,
  167. char *data)
  168. {
  169. return xlog_do_io(log, blk_no, nbblks, data, REQ_OP_WRITE);
  170. }
  171. #ifdef DEBUG
  172. /*
  173. * dump debug superblock and log record information
  174. */
  175. STATIC void
  176. xlog_header_check_dump(
  177. xfs_mount_t *mp,
  178. xlog_rec_header_t *head)
  179. {
  180. xfs_debug(mp, "%s: SB : uuid = %pU, fmt = %d",
  181. __func__, &mp->m_sb.sb_uuid, XLOG_FMT);
  182. xfs_debug(mp, " log : uuid = %pU, fmt = %d",
  183. &head->h_fs_uuid, be32_to_cpu(head->h_fmt));
  184. }
  185. #else
  186. #define xlog_header_check_dump(mp, head)
  187. #endif
  188. /*
  189. * check log record header for recovery
  190. */
  191. STATIC int
  192. xlog_header_check_recover(
  193. xfs_mount_t *mp,
  194. xlog_rec_header_t *head)
  195. {
  196. ASSERT(head->h_magicno == cpu_to_be32(XLOG_HEADER_MAGIC_NUM));
  197. /*
  198. * IRIX doesn't write the h_fmt field and leaves it zeroed
  199. * (XLOG_FMT_UNKNOWN). This stops us from trying to recover
  200. * a dirty log created in IRIX.
  201. */
  202. if (XFS_IS_CORRUPT(mp, head->h_fmt != cpu_to_be32(XLOG_FMT))) {
  203. xfs_warn(mp,
  204. "dirty log written in incompatible format - can't recover");
  205. xlog_header_check_dump(mp, head);
  206. return -EFSCORRUPTED;
  207. }
  208. if (XFS_IS_CORRUPT(mp, !uuid_equal(&mp->m_sb.sb_uuid,
  209. &head->h_fs_uuid))) {
  210. xfs_warn(mp,
  211. "dirty log entry has mismatched uuid - can't recover");
  212. xlog_header_check_dump(mp, head);
  213. return -EFSCORRUPTED;
  214. }
  215. return 0;
  216. }
  217. /*
  218. * read the head block of the log and check the header
  219. */
  220. STATIC int
  221. xlog_header_check_mount(
  222. xfs_mount_t *mp,
  223. xlog_rec_header_t *head)
  224. {
  225. ASSERT(head->h_magicno == cpu_to_be32(XLOG_HEADER_MAGIC_NUM));
  226. if (uuid_is_null(&head->h_fs_uuid)) {
  227. /*
  228. * IRIX doesn't write the h_fs_uuid or h_fmt fields. If
  229. * h_fs_uuid is null, we assume this log was last mounted
  230. * by IRIX and continue.
  231. */
  232. xfs_warn(mp, "null uuid in log - IRIX style log");
  233. } else if (XFS_IS_CORRUPT(mp, !uuid_equal(&mp->m_sb.sb_uuid,
  234. &head->h_fs_uuid))) {
  235. xfs_warn(mp, "log has mismatched uuid - can't recover");
  236. xlog_header_check_dump(mp, head);
  237. return -EFSCORRUPTED;
  238. }
  239. return 0;
  240. }
  241. /*
  242. * This routine finds (to an approximation) the first block in the physical
  243. * log which contains the given cycle. It uses a binary search algorithm.
  244. * Note that the algorithm can not be perfect because the disk will not
  245. * necessarily be perfect.
  246. */
  247. STATIC int
  248. xlog_find_cycle_start(
  249. struct xlog *log,
  250. char *buffer,
  251. xfs_daddr_t first_blk,
  252. xfs_daddr_t *last_blk,
  253. uint cycle)
  254. {
  255. char *offset;
  256. xfs_daddr_t mid_blk;
  257. xfs_daddr_t end_blk;
  258. uint mid_cycle;
  259. int error;
  260. end_blk = *last_blk;
  261. mid_blk = BLK_AVG(first_blk, end_blk);
  262. while (mid_blk != first_blk && mid_blk != end_blk) {
  263. error = xlog_bread(log, mid_blk, 1, buffer, &offset);
  264. if (error)
  265. return error;
  266. mid_cycle = xlog_get_cycle(offset);
  267. if (mid_cycle == cycle)
  268. end_blk = mid_blk; /* last_half_cycle == mid_cycle */
  269. else
  270. first_blk = mid_blk; /* first_half_cycle == mid_cycle */
  271. mid_blk = BLK_AVG(first_blk, end_blk);
  272. }
  273. ASSERT((mid_blk == first_blk && mid_blk+1 == end_blk) ||
  274. (mid_blk == end_blk && mid_blk-1 == first_blk));
  275. *last_blk = end_blk;
  276. return 0;
  277. }
  278. /*
  279. * Check that a range of blocks does not contain stop_on_cycle_no.
  280. * Fill in *new_blk with the block offset where such a block is
  281. * found, or with -1 (an invalid block number) if there is no such
  282. * block in the range. The scan needs to occur from front to back
  283. * and the pointer into the region must be updated since a later
  284. * routine will need to perform another test.
  285. */
  286. STATIC int
  287. xlog_find_verify_cycle(
  288. struct xlog *log,
  289. xfs_daddr_t start_blk,
  290. int nbblks,
  291. uint stop_on_cycle_no,
  292. xfs_daddr_t *new_blk)
  293. {
  294. xfs_daddr_t i, j;
  295. uint cycle;
  296. char *buffer;
  297. xfs_daddr_t bufblks;
  298. char *buf = NULL;
  299. int error = 0;
  300. /*
  301. * Greedily allocate a buffer big enough to handle the full
  302. * range of basic blocks we'll be examining. If that fails,
  303. * try a smaller size. We need to be able to read at least
  304. * a log sector, or we're out of luck.
  305. */
  306. bufblks = 1 << ffs(nbblks);
  307. while (bufblks > log->l_logBBsize)
  308. bufblks >>= 1;
  309. while (!(buffer = xlog_alloc_buffer(log, bufblks))) {
  310. bufblks >>= 1;
  311. if (bufblks < log->l_sectBBsize)
  312. return -ENOMEM;
  313. }
  314. for (i = start_blk; i < start_blk + nbblks; i += bufblks) {
  315. int bcount;
  316. bcount = min(bufblks, (start_blk + nbblks - i));
  317. error = xlog_bread(log, i, bcount, buffer, &buf);
  318. if (error)
  319. goto out;
  320. for (j = 0; j < bcount; j++) {
  321. cycle = xlog_get_cycle(buf);
  322. if (cycle == stop_on_cycle_no) {
  323. *new_blk = i+j;
  324. goto out;
  325. }
  326. buf += BBSIZE;
  327. }
  328. }
  329. *new_blk = -1;
  330. out:
  331. kmem_free(buffer);
  332. return error;
  333. }
  334. static inline int
  335. xlog_logrec_hblks(struct xlog *log, struct xlog_rec_header *rh)
  336. {
  337. if (xfs_has_logv2(log->l_mp)) {
  338. int h_size = be32_to_cpu(rh->h_size);
  339. if ((be32_to_cpu(rh->h_version) & XLOG_VERSION_2) &&
  340. h_size > XLOG_HEADER_CYCLE_SIZE)
  341. return DIV_ROUND_UP(h_size, XLOG_HEADER_CYCLE_SIZE);
  342. }
  343. return 1;
  344. }
  345. /*
  346. * Potentially backup over partial log record write.
  347. *
  348. * In the typical case, last_blk is the number of the block directly after
  349. * a good log record. Therefore, we subtract one to get the block number
  350. * of the last block in the given buffer. extra_bblks contains the number
  351. * of blocks we would have read on a previous read. This happens when the
  352. * last log record is split over the end of the physical log.
  353. *
  354. * extra_bblks is the number of blocks potentially verified on a previous
  355. * call to this routine.
  356. */
  357. STATIC int
  358. xlog_find_verify_log_record(
  359. struct xlog *log,
  360. xfs_daddr_t start_blk,
  361. xfs_daddr_t *last_blk,
  362. int extra_bblks)
  363. {
  364. xfs_daddr_t i;
  365. char *buffer;
  366. char *offset = NULL;
  367. xlog_rec_header_t *head = NULL;
  368. int error = 0;
  369. int smallmem = 0;
  370. int num_blks = *last_blk - start_blk;
  371. int xhdrs;
  372. ASSERT(start_blk != 0 || *last_blk != start_blk);
  373. buffer = xlog_alloc_buffer(log, num_blks);
  374. if (!buffer) {
  375. buffer = xlog_alloc_buffer(log, 1);
  376. if (!buffer)
  377. return -ENOMEM;
  378. smallmem = 1;
  379. } else {
  380. error = xlog_bread(log, start_blk, num_blks, buffer, &offset);
  381. if (error)
  382. goto out;
  383. offset += ((num_blks - 1) << BBSHIFT);
  384. }
  385. for (i = (*last_blk) - 1; i >= 0; i--) {
  386. if (i < start_blk) {
  387. /* valid log record not found */
  388. xfs_warn(log->l_mp,
  389. "Log inconsistent (didn't find previous header)");
  390. ASSERT(0);
  391. error = -EFSCORRUPTED;
  392. goto out;
  393. }
  394. if (smallmem) {
  395. error = xlog_bread(log, i, 1, buffer, &offset);
  396. if (error)
  397. goto out;
  398. }
  399. head = (xlog_rec_header_t *)offset;
  400. if (head->h_magicno == cpu_to_be32(XLOG_HEADER_MAGIC_NUM))
  401. break;
  402. if (!smallmem)
  403. offset -= BBSIZE;
  404. }
  405. /*
  406. * We hit the beginning of the physical log & still no header. Return
  407. * to caller. If caller can handle a return of -1, then this routine
  408. * will be called again for the end of the physical log.
  409. */
  410. if (i == -1) {
  411. error = 1;
  412. goto out;
  413. }
  414. /*
  415. * We have the final block of the good log (the first block
  416. * of the log record _before_ the head. So we check the uuid.
  417. */
  418. if ((error = xlog_header_check_mount(log->l_mp, head)))
  419. goto out;
  420. /*
  421. * We may have found a log record header before we expected one.
  422. * last_blk will be the 1st block # with a given cycle #. We may end
  423. * up reading an entire log record. In this case, we don't want to
  424. * reset last_blk. Only when last_blk points in the middle of a log
  425. * record do we update last_blk.
  426. */
  427. xhdrs = xlog_logrec_hblks(log, head);
  428. if (*last_blk - i + extra_bblks !=
  429. BTOBB(be32_to_cpu(head->h_len)) + xhdrs)
  430. *last_blk = i;
  431. out:
  432. kmem_free(buffer);
  433. return error;
  434. }
  435. /*
  436. * Head is defined to be the point of the log where the next log write
  437. * could go. This means that incomplete LR writes at the end are
  438. * eliminated when calculating the head. We aren't guaranteed that previous
  439. * LR have complete transactions. We only know that a cycle number of
  440. * current cycle number -1 won't be present in the log if we start writing
  441. * from our current block number.
  442. *
  443. * last_blk contains the block number of the first block with a given
  444. * cycle number.
  445. *
  446. * Return: zero if normal, non-zero if error.
  447. */
  448. STATIC int
  449. xlog_find_head(
  450. struct xlog *log,
  451. xfs_daddr_t *return_head_blk)
  452. {
  453. char *buffer;
  454. char *offset;
  455. xfs_daddr_t new_blk, first_blk, start_blk, last_blk, head_blk;
  456. int num_scan_bblks;
  457. uint first_half_cycle, last_half_cycle;
  458. uint stop_on_cycle;
  459. int error, log_bbnum = log->l_logBBsize;
  460. /* Is the end of the log device zeroed? */
  461. error = xlog_find_zeroed(log, &first_blk);
  462. if (error < 0) {
  463. xfs_warn(log->l_mp, "empty log check failed");
  464. return error;
  465. }
  466. if (error == 1) {
  467. *return_head_blk = first_blk;
  468. /* Is the whole lot zeroed? */
  469. if (!first_blk) {
  470. /* Linux XFS shouldn't generate totally zeroed logs -
  471. * mkfs etc write a dummy unmount record to a fresh
  472. * log so we can store the uuid in there
  473. */
  474. xfs_warn(log->l_mp, "totally zeroed log");
  475. }
  476. return 0;
  477. }
  478. first_blk = 0; /* get cycle # of 1st block */
  479. buffer = xlog_alloc_buffer(log, 1);
  480. if (!buffer)
  481. return -ENOMEM;
  482. error = xlog_bread(log, 0, 1, buffer, &offset);
  483. if (error)
  484. goto out_free_buffer;
  485. first_half_cycle = xlog_get_cycle(offset);
  486. last_blk = head_blk = log_bbnum - 1; /* get cycle # of last block */
  487. error = xlog_bread(log, last_blk, 1, buffer, &offset);
  488. if (error)
  489. goto out_free_buffer;
  490. last_half_cycle = xlog_get_cycle(offset);
  491. ASSERT(last_half_cycle != 0);
  492. /*
  493. * If the 1st half cycle number is equal to the last half cycle number,
  494. * then the entire log is stamped with the same cycle number. In this
  495. * case, head_blk can't be set to zero (which makes sense). The below
  496. * math doesn't work out properly with head_blk equal to zero. Instead,
  497. * we set it to log_bbnum which is an invalid block number, but this
  498. * value makes the math correct. If head_blk doesn't changed through
  499. * all the tests below, *head_blk is set to zero at the very end rather
  500. * than log_bbnum. In a sense, log_bbnum and zero are the same block
  501. * in a circular file.
  502. */
  503. if (first_half_cycle == last_half_cycle) {
  504. /*
  505. * In this case we believe that the entire log should have
  506. * cycle number last_half_cycle. We need to scan backwards
  507. * from the end verifying that there are no holes still
  508. * containing last_half_cycle - 1. If we find such a hole,
  509. * then the start of that hole will be the new head. The
  510. * simple case looks like
  511. * x | x ... | x - 1 | x
  512. * Another case that fits this picture would be
  513. * x | x + 1 | x ... | x
  514. * In this case the head really is somewhere at the end of the
  515. * log, as one of the latest writes at the beginning was
  516. * incomplete.
  517. * One more case is
  518. * x | x + 1 | x ... | x - 1 | x
  519. * This is really the combination of the above two cases, and
  520. * the head has to end up at the start of the x-1 hole at the
  521. * end of the log.
  522. *
  523. * In the 256k log case, we will read from the beginning to the
  524. * end of the log and search for cycle numbers equal to x-1.
  525. * We don't worry about the x+1 blocks that we encounter,
  526. * because we know that they cannot be the head since the log
  527. * started with x.
  528. */
  529. head_blk = log_bbnum;
  530. stop_on_cycle = last_half_cycle - 1;
  531. } else {
  532. /*
  533. * In this case we want to find the first block with cycle
  534. * number matching last_half_cycle. We expect the log to be
  535. * some variation on
  536. * x + 1 ... | x ... | x
  537. * The first block with cycle number x (last_half_cycle) will
  538. * be where the new head belongs. First we do a binary search
  539. * for the first occurrence of last_half_cycle. The binary
  540. * search may not be totally accurate, so then we scan back
  541. * from there looking for occurrences of last_half_cycle before
  542. * us. If that backwards scan wraps around the beginning of
  543. * the log, then we look for occurrences of last_half_cycle - 1
  544. * at the end of the log. The cases we're looking for look
  545. * like
  546. * v binary search stopped here
  547. * x + 1 ... | x | x + 1 | x ... | x
  548. * ^ but we want to locate this spot
  549. * or
  550. * <---------> less than scan distance
  551. * x + 1 ... | x ... | x - 1 | x
  552. * ^ we want to locate this spot
  553. */
  554. stop_on_cycle = last_half_cycle;
  555. error = xlog_find_cycle_start(log, buffer, first_blk, &head_blk,
  556. last_half_cycle);
  557. if (error)
  558. goto out_free_buffer;
  559. }
  560. /*
  561. * Now validate the answer. Scan back some number of maximum possible
  562. * blocks and make sure each one has the expected cycle number. The
  563. * maximum is determined by the total possible amount of buffering
  564. * in the in-core log. The following number can be made tighter if
  565. * we actually look at the block size of the filesystem.
  566. */
  567. num_scan_bblks = min_t(int, log_bbnum, XLOG_TOTAL_REC_SHIFT(log));
  568. if (head_blk >= num_scan_bblks) {
  569. /*
  570. * We are guaranteed that the entire check can be performed
  571. * in one buffer.
  572. */
  573. start_blk = head_blk - num_scan_bblks;
  574. if ((error = xlog_find_verify_cycle(log,
  575. start_blk, num_scan_bblks,
  576. stop_on_cycle, &new_blk)))
  577. goto out_free_buffer;
  578. if (new_blk != -1)
  579. head_blk = new_blk;
  580. } else { /* need to read 2 parts of log */
  581. /*
  582. * We are going to scan backwards in the log in two parts.
  583. * First we scan the physical end of the log. In this part
  584. * of the log, we are looking for blocks with cycle number
  585. * last_half_cycle - 1.
  586. * If we find one, then we know that the log starts there, as
  587. * we've found a hole that didn't get written in going around
  588. * the end of the physical log. The simple case for this is
  589. * x + 1 ... | x ... | x - 1 | x
  590. * <---------> less than scan distance
  591. * If all of the blocks at the end of the log have cycle number
  592. * last_half_cycle, then we check the blocks at the start of
  593. * the log looking for occurrences of last_half_cycle. If we
  594. * find one, then our current estimate for the location of the
  595. * first occurrence of last_half_cycle is wrong and we move
  596. * back to the hole we've found. This case looks like
  597. * x + 1 ... | x | x + 1 | x ...
  598. * ^ binary search stopped here
  599. * Another case we need to handle that only occurs in 256k
  600. * logs is
  601. * x + 1 ... | x ... | x+1 | x ...
  602. * ^ binary search stops here
  603. * In a 256k log, the scan at the end of the log will see the
  604. * x + 1 blocks. We need to skip past those since that is
  605. * certainly not the head of the log. By searching for
  606. * last_half_cycle-1 we accomplish that.
  607. */
  608. ASSERT(head_blk <= INT_MAX &&
  609. (xfs_daddr_t) num_scan_bblks >= head_blk);
  610. start_blk = log_bbnum - (num_scan_bblks - head_blk);
  611. if ((error = xlog_find_verify_cycle(log, start_blk,
  612. num_scan_bblks - (int)head_blk,
  613. (stop_on_cycle - 1), &new_blk)))
  614. goto out_free_buffer;
  615. if (new_blk != -1) {
  616. head_blk = new_blk;
  617. goto validate_head;
  618. }
  619. /*
  620. * Scan beginning of log now. The last part of the physical
  621. * log is good. This scan needs to verify that it doesn't find
  622. * the last_half_cycle.
  623. */
  624. start_blk = 0;
  625. ASSERT(head_blk <= INT_MAX);
  626. if ((error = xlog_find_verify_cycle(log,
  627. start_blk, (int)head_blk,
  628. stop_on_cycle, &new_blk)))
  629. goto out_free_buffer;
  630. if (new_blk != -1)
  631. head_blk = new_blk;
  632. }
  633. validate_head:
  634. /*
  635. * Now we need to make sure head_blk is not pointing to a block in
  636. * the middle of a log record.
  637. */
  638. num_scan_bblks = XLOG_REC_SHIFT(log);
  639. if (head_blk >= num_scan_bblks) {
  640. start_blk = head_blk - num_scan_bblks; /* don't read head_blk */
  641. /* start ptr at last block ptr before head_blk */
  642. error = xlog_find_verify_log_record(log, start_blk, &head_blk, 0);
  643. if (error == 1)
  644. error = -EIO;
  645. if (error)
  646. goto out_free_buffer;
  647. } else {
  648. start_blk = 0;
  649. ASSERT(head_blk <= INT_MAX);
  650. error = xlog_find_verify_log_record(log, start_blk, &head_blk, 0);
  651. if (error < 0)
  652. goto out_free_buffer;
  653. if (error == 1) {
  654. /* We hit the beginning of the log during our search */
  655. start_blk = log_bbnum - (num_scan_bblks - head_blk);
  656. new_blk = log_bbnum;
  657. ASSERT(start_blk <= INT_MAX &&
  658. (xfs_daddr_t) log_bbnum-start_blk >= 0);
  659. ASSERT(head_blk <= INT_MAX);
  660. error = xlog_find_verify_log_record(log, start_blk,
  661. &new_blk, (int)head_blk);
  662. if (error == 1)
  663. error = -EIO;
  664. if (error)
  665. goto out_free_buffer;
  666. if (new_blk != log_bbnum)
  667. head_blk = new_blk;
  668. } else if (error)
  669. goto out_free_buffer;
  670. }
  671. kmem_free(buffer);
  672. if (head_blk == log_bbnum)
  673. *return_head_blk = 0;
  674. else
  675. *return_head_blk = head_blk;
  676. /*
  677. * When returning here, we have a good block number. Bad block
  678. * means that during a previous crash, we didn't have a clean break
  679. * from cycle number N to cycle number N-1. In this case, we need
  680. * to find the first block with cycle number N-1.
  681. */
  682. return 0;
  683. out_free_buffer:
  684. kmem_free(buffer);
  685. if (error)
  686. xfs_warn(log->l_mp, "failed to find log head");
  687. return error;
  688. }
  689. /*
  690. * Seek backwards in the log for log record headers.
  691. *
  692. * Given a starting log block, walk backwards until we find the provided number
  693. * of records or hit the provided tail block. The return value is the number of
  694. * records encountered or a negative error code. The log block and buffer
  695. * pointer of the last record seen are returned in rblk and rhead respectively.
  696. */
  697. STATIC int
  698. xlog_rseek_logrec_hdr(
  699. struct xlog *log,
  700. xfs_daddr_t head_blk,
  701. xfs_daddr_t tail_blk,
  702. int count,
  703. char *buffer,
  704. xfs_daddr_t *rblk,
  705. struct xlog_rec_header **rhead,
  706. bool *wrapped)
  707. {
  708. int i;
  709. int error;
  710. int found = 0;
  711. char *offset = NULL;
  712. xfs_daddr_t end_blk;
  713. *wrapped = false;
  714. /*
  715. * Walk backwards from the head block until we hit the tail or the first
  716. * block in the log.
  717. */
  718. end_blk = head_blk > tail_blk ? tail_blk : 0;
  719. for (i = (int) head_blk - 1; i >= end_blk; i--) {
  720. error = xlog_bread(log, i, 1, buffer, &offset);
  721. if (error)
  722. goto out_error;
  723. if (*(__be32 *) offset == cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) {
  724. *rblk = i;
  725. *rhead = (struct xlog_rec_header *) offset;
  726. if (++found == count)
  727. break;
  728. }
  729. }
  730. /*
  731. * If we haven't hit the tail block or the log record header count,
  732. * start looking again from the end of the physical log. Note that
  733. * callers can pass head == tail if the tail is not yet known.
  734. */
  735. if (tail_blk >= head_blk && found != count) {
  736. for (i = log->l_logBBsize - 1; i >= (int) tail_blk; i--) {
  737. error = xlog_bread(log, i, 1, buffer, &offset);
  738. if (error)
  739. goto out_error;
  740. if (*(__be32 *)offset ==
  741. cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) {
  742. *wrapped = true;
  743. *rblk = i;
  744. *rhead = (struct xlog_rec_header *) offset;
  745. if (++found == count)
  746. break;
  747. }
  748. }
  749. }
  750. return found;
  751. out_error:
  752. return error;
  753. }
  754. /*
  755. * Seek forward in the log for log record headers.
  756. *
  757. * Given head and tail blocks, walk forward from the tail block until we find
  758. * the provided number of records or hit the head block. The return value is the
  759. * number of records encountered or a negative error code. The log block and
  760. * buffer pointer of the last record seen are returned in rblk and rhead
  761. * respectively.
  762. */
  763. STATIC int
  764. xlog_seek_logrec_hdr(
  765. struct xlog *log,
  766. xfs_daddr_t head_blk,
  767. xfs_daddr_t tail_blk,
  768. int count,
  769. char *buffer,
  770. xfs_daddr_t *rblk,
  771. struct xlog_rec_header **rhead,
  772. bool *wrapped)
  773. {
  774. int i;
  775. int error;
  776. int found = 0;
  777. char *offset = NULL;
  778. xfs_daddr_t end_blk;
  779. *wrapped = false;
  780. /*
  781. * Walk forward from the tail block until we hit the head or the last
  782. * block in the log.
  783. */
  784. end_blk = head_blk > tail_blk ? head_blk : log->l_logBBsize - 1;
  785. for (i = (int) tail_blk; i <= end_blk; i++) {
  786. error = xlog_bread(log, i, 1, buffer, &offset);
  787. if (error)
  788. goto out_error;
  789. if (*(__be32 *) offset == cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) {
  790. *rblk = i;
  791. *rhead = (struct xlog_rec_header *) offset;
  792. if (++found == count)
  793. break;
  794. }
  795. }
  796. /*
  797. * If we haven't hit the head block or the log record header count,
  798. * start looking again from the start of the physical log.
  799. */
  800. if (tail_blk > head_blk && found != count) {
  801. for (i = 0; i < (int) head_blk; i++) {
  802. error = xlog_bread(log, i, 1, buffer, &offset);
  803. if (error)
  804. goto out_error;
  805. if (*(__be32 *)offset ==
  806. cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) {
  807. *wrapped = true;
  808. *rblk = i;
  809. *rhead = (struct xlog_rec_header *) offset;
  810. if (++found == count)
  811. break;
  812. }
  813. }
  814. }
  815. return found;
  816. out_error:
  817. return error;
  818. }
  819. /*
  820. * Calculate distance from head to tail (i.e., unused space in the log).
  821. */
  822. static inline int
  823. xlog_tail_distance(
  824. struct xlog *log,
  825. xfs_daddr_t head_blk,
  826. xfs_daddr_t tail_blk)
  827. {
  828. if (head_blk < tail_blk)
  829. return tail_blk - head_blk;
  830. return tail_blk + (log->l_logBBsize - head_blk);
  831. }
  832. /*
  833. * Verify the log tail. This is particularly important when torn or incomplete
  834. * writes have been detected near the front of the log and the head has been
  835. * walked back accordingly.
  836. *
  837. * We also have to handle the case where the tail was pinned and the head
  838. * blocked behind the tail right before a crash. If the tail had been pushed
  839. * immediately prior to the crash and the subsequent checkpoint was only
  840. * partially written, it's possible it overwrote the last referenced tail in the
  841. * log with garbage. This is not a coherency problem because the tail must have
  842. * been pushed before it can be overwritten, but appears as log corruption to
  843. * recovery because we have no way to know the tail was updated if the
  844. * subsequent checkpoint didn't write successfully.
  845. *
  846. * Therefore, CRC check the log from tail to head. If a failure occurs and the
  847. * offending record is within max iclog bufs from the head, walk the tail
  848. * forward and retry until a valid tail is found or corruption is detected out
  849. * of the range of a possible overwrite.
  850. */
  851. STATIC int
  852. xlog_verify_tail(
  853. struct xlog *log,
  854. xfs_daddr_t head_blk,
  855. xfs_daddr_t *tail_blk,
  856. int hsize)
  857. {
  858. struct xlog_rec_header *thead;
  859. char *buffer;
  860. xfs_daddr_t first_bad;
  861. int error = 0;
  862. bool wrapped;
  863. xfs_daddr_t tmp_tail;
  864. xfs_daddr_t orig_tail = *tail_blk;
  865. buffer = xlog_alloc_buffer(log, 1);
  866. if (!buffer)
  867. return -ENOMEM;
  868. /*
  869. * Make sure the tail points to a record (returns positive count on
  870. * success).
  871. */
  872. error = xlog_seek_logrec_hdr(log, head_blk, *tail_blk, 1, buffer,
  873. &tmp_tail, &thead, &wrapped);
  874. if (error < 0)
  875. goto out;
  876. if (*tail_blk != tmp_tail)
  877. *tail_blk = tmp_tail;
  878. /*
  879. * Run a CRC check from the tail to the head. We can't just check
  880. * MAX_ICLOGS records past the tail because the tail may point to stale
  881. * blocks cleared during the search for the head/tail. These blocks are
  882. * overwritten with zero-length records and thus record count is not a
  883. * reliable indicator of the iclog state before a crash.
  884. */
  885. first_bad = 0;
  886. error = xlog_do_recovery_pass(log, head_blk, *tail_blk,
  887. XLOG_RECOVER_CRCPASS, &first_bad);
  888. while ((error == -EFSBADCRC || error == -EFSCORRUPTED) && first_bad) {
  889. int tail_distance;
  890. /*
  891. * Is corruption within range of the head? If so, retry from
  892. * the next record. Otherwise return an error.
  893. */
  894. tail_distance = xlog_tail_distance(log, head_blk, first_bad);
  895. if (tail_distance > BTOBB(XLOG_MAX_ICLOGS * hsize))
  896. break;
  897. /* skip to the next record; returns positive count on success */
  898. error = xlog_seek_logrec_hdr(log, head_blk, first_bad, 2,
  899. buffer, &tmp_tail, &thead, &wrapped);
  900. if (error < 0)
  901. goto out;
  902. *tail_blk = tmp_tail;
  903. first_bad = 0;
  904. error = xlog_do_recovery_pass(log, head_blk, *tail_blk,
  905. XLOG_RECOVER_CRCPASS, &first_bad);
  906. }
  907. if (!error && *tail_blk != orig_tail)
  908. xfs_warn(log->l_mp,
  909. "Tail block (0x%llx) overwrite detected. Updated to 0x%llx",
  910. orig_tail, *tail_blk);
  911. out:
  912. kmem_free(buffer);
  913. return error;
  914. }
  915. /*
  916. * Detect and trim torn writes from the head of the log.
  917. *
  918. * Storage without sector atomicity guarantees can result in torn writes in the
  919. * log in the event of a crash. Our only means to detect this scenario is via
  920. * CRC verification. While we can't always be certain that CRC verification
  921. * failure is due to a torn write vs. an unrelated corruption, we do know that
  922. * only a certain number (XLOG_MAX_ICLOGS) of log records can be written out at
  923. * one time. Therefore, CRC verify up to XLOG_MAX_ICLOGS records at the head of
  924. * the log and treat failures in this range as torn writes as a matter of
  925. * policy. In the event of CRC failure, the head is walked back to the last good
  926. * record in the log and the tail is updated from that record and verified.
  927. */
  928. STATIC int
  929. xlog_verify_head(
  930. struct xlog *log,
  931. xfs_daddr_t *head_blk, /* in/out: unverified head */
  932. xfs_daddr_t *tail_blk, /* out: tail block */
  933. char *buffer,
  934. xfs_daddr_t *rhead_blk, /* start blk of last record */
  935. struct xlog_rec_header **rhead, /* ptr to last record */
  936. bool *wrapped) /* last rec. wraps phys. log */
  937. {
  938. struct xlog_rec_header *tmp_rhead;
  939. char *tmp_buffer;
  940. xfs_daddr_t first_bad;
  941. xfs_daddr_t tmp_rhead_blk;
  942. int found;
  943. int error;
  944. bool tmp_wrapped;
  945. /*
  946. * Check the head of the log for torn writes. Search backwards from the
  947. * head until we hit the tail or the maximum number of log record I/Os
  948. * that could have been in flight at one time. Use a temporary buffer so
  949. * we don't trash the rhead/buffer pointers from the caller.
  950. */
  951. tmp_buffer = xlog_alloc_buffer(log, 1);
  952. if (!tmp_buffer)
  953. return -ENOMEM;
  954. error = xlog_rseek_logrec_hdr(log, *head_blk, *tail_blk,
  955. XLOG_MAX_ICLOGS, tmp_buffer,
  956. &tmp_rhead_blk, &tmp_rhead, &tmp_wrapped);
  957. kmem_free(tmp_buffer);
  958. if (error < 0)
  959. return error;
  960. /*
  961. * Now run a CRC verification pass over the records starting at the
  962. * block found above to the current head. If a CRC failure occurs, the
  963. * log block of the first bad record is saved in first_bad.
  964. */
  965. error = xlog_do_recovery_pass(log, *head_blk, tmp_rhead_blk,
  966. XLOG_RECOVER_CRCPASS, &first_bad);
  967. if ((error == -EFSBADCRC || error == -EFSCORRUPTED) && first_bad) {
  968. /*
  969. * We've hit a potential torn write. Reset the error and warn
  970. * about it.
  971. */
  972. error = 0;
  973. xfs_warn(log->l_mp,
  974. "Torn write (CRC failure) detected at log block 0x%llx. Truncating head block from 0x%llx.",
  975. first_bad, *head_blk);
  976. /*
  977. * Get the header block and buffer pointer for the last good
  978. * record before the bad record.
  979. *
  980. * Note that xlog_find_tail() clears the blocks at the new head
  981. * (i.e., the records with invalid CRC) if the cycle number
  982. * matches the current cycle.
  983. */
  984. found = xlog_rseek_logrec_hdr(log, first_bad, *tail_blk, 1,
  985. buffer, rhead_blk, rhead, wrapped);
  986. if (found < 0)
  987. return found;
  988. if (found == 0) /* XXX: right thing to do here? */
  989. return -EIO;
  990. /*
  991. * Reset the head block to the starting block of the first bad
  992. * log record and set the tail block based on the last good
  993. * record.
  994. *
  995. * Bail out if the updated head/tail match as this indicates
  996. * possible corruption outside of the acceptable
  997. * (XLOG_MAX_ICLOGS) range. This is a job for xfs_repair...
  998. */
  999. *head_blk = first_bad;
  1000. *tail_blk = BLOCK_LSN(be64_to_cpu((*rhead)->h_tail_lsn));
  1001. if (*head_blk == *tail_blk) {
  1002. ASSERT(0);
  1003. return 0;
  1004. }
  1005. }
  1006. if (error)
  1007. return error;
  1008. return xlog_verify_tail(log, *head_blk, tail_blk,
  1009. be32_to_cpu((*rhead)->h_size));
  1010. }
  1011. /*
  1012. * We need to make sure we handle log wrapping properly, so we can't use the
  1013. * calculated logbno directly. Make sure it wraps to the correct bno inside the
  1014. * log.
  1015. *
  1016. * The log is limited to 32 bit sizes, so we use the appropriate modulus
  1017. * operation here and cast it back to a 64 bit daddr on return.
  1018. */
  1019. static inline xfs_daddr_t
  1020. xlog_wrap_logbno(
  1021. struct xlog *log,
  1022. xfs_daddr_t bno)
  1023. {
  1024. int mod;
  1025. div_s64_rem(bno, log->l_logBBsize, &mod);
  1026. return mod;
  1027. }
  1028. /*
  1029. * Check whether the head of the log points to an unmount record. In other
  1030. * words, determine whether the log is clean. If so, update the in-core state
  1031. * appropriately.
  1032. */
  1033. static int
  1034. xlog_check_unmount_rec(
  1035. struct xlog *log,
  1036. xfs_daddr_t *head_blk,
  1037. xfs_daddr_t *tail_blk,
  1038. struct xlog_rec_header *rhead,
  1039. xfs_daddr_t rhead_blk,
  1040. char *buffer,
  1041. bool *clean)
  1042. {
  1043. struct xlog_op_header *op_head;
  1044. xfs_daddr_t umount_data_blk;
  1045. xfs_daddr_t after_umount_blk;
  1046. int hblks;
  1047. int error;
  1048. char *offset;
  1049. *clean = false;
  1050. /*
  1051. * Look for unmount record. If we find it, then we know there was a
  1052. * clean unmount. Since 'i' could be the last block in the physical
  1053. * log, we convert to a log block before comparing to the head_blk.
  1054. *
  1055. * Save the current tail lsn to use to pass to xlog_clear_stale_blocks()
  1056. * below. We won't want to clear the unmount record if there is one, so
  1057. * we pass the lsn of the unmount record rather than the block after it.
  1058. */
  1059. hblks = xlog_logrec_hblks(log, rhead);
  1060. after_umount_blk = xlog_wrap_logbno(log,
  1061. rhead_blk + hblks + BTOBB(be32_to_cpu(rhead->h_len)));
  1062. if (*head_blk == after_umount_blk &&
  1063. be32_to_cpu(rhead->h_num_logops) == 1) {
  1064. umount_data_blk = xlog_wrap_logbno(log, rhead_blk + hblks);
  1065. error = xlog_bread(log, umount_data_blk, 1, buffer, &offset);
  1066. if (error)
  1067. return error;
  1068. op_head = (struct xlog_op_header *)offset;
  1069. if (op_head->oh_flags & XLOG_UNMOUNT_TRANS) {
  1070. /*
  1071. * Set tail and last sync so that newly written log
  1072. * records will point recovery to after the current
  1073. * unmount record.
  1074. */
  1075. xlog_assign_atomic_lsn(&log->l_tail_lsn,
  1076. log->l_curr_cycle, after_umount_blk);
  1077. xlog_assign_atomic_lsn(&log->l_last_sync_lsn,
  1078. log->l_curr_cycle, after_umount_blk);
  1079. *tail_blk = after_umount_blk;
  1080. *clean = true;
  1081. }
  1082. }
  1083. return 0;
  1084. }
  1085. static void
  1086. xlog_set_state(
  1087. struct xlog *log,
  1088. xfs_daddr_t head_blk,
  1089. struct xlog_rec_header *rhead,
  1090. xfs_daddr_t rhead_blk,
  1091. bool bump_cycle)
  1092. {
  1093. /*
  1094. * Reset log values according to the state of the log when we
  1095. * crashed. In the case where head_blk == 0, we bump curr_cycle
  1096. * one because the next write starts a new cycle rather than
  1097. * continuing the cycle of the last good log record. At this
  1098. * point we have guaranteed that all partial log records have been
  1099. * accounted for. Therefore, we know that the last good log record
  1100. * written was complete and ended exactly on the end boundary
  1101. * of the physical log.
  1102. */
  1103. log->l_prev_block = rhead_blk;
  1104. log->l_curr_block = (int)head_blk;
  1105. log->l_curr_cycle = be32_to_cpu(rhead->h_cycle);
  1106. if (bump_cycle)
  1107. log->l_curr_cycle++;
  1108. atomic64_set(&log->l_tail_lsn, be64_to_cpu(rhead->h_tail_lsn));
  1109. atomic64_set(&log->l_last_sync_lsn, be64_to_cpu(rhead->h_lsn));
  1110. xlog_assign_grant_head(&log->l_reserve_head.grant, log->l_curr_cycle,
  1111. BBTOB(log->l_curr_block));
  1112. xlog_assign_grant_head(&log->l_write_head.grant, log->l_curr_cycle,
  1113. BBTOB(log->l_curr_block));
  1114. }
  1115. /*
  1116. * Find the sync block number or the tail of the log.
  1117. *
  1118. * This will be the block number of the last record to have its
  1119. * associated buffers synced to disk. Every log record header has
  1120. * a sync lsn embedded in it. LSNs hold block numbers, so it is easy
  1121. * to get a sync block number. The only concern is to figure out which
  1122. * log record header to believe.
  1123. *
  1124. * The following algorithm uses the log record header with the largest
  1125. * lsn. The entire log record does not need to be valid. We only care
  1126. * that the header is valid.
  1127. *
  1128. * We could speed up search by using current head_blk buffer, but it is not
  1129. * available.
  1130. */
  1131. STATIC int
  1132. xlog_find_tail(
  1133. struct xlog *log,
  1134. xfs_daddr_t *head_blk,
  1135. xfs_daddr_t *tail_blk)
  1136. {
  1137. xlog_rec_header_t *rhead;
  1138. char *offset = NULL;
  1139. char *buffer;
  1140. int error;
  1141. xfs_daddr_t rhead_blk;
  1142. xfs_lsn_t tail_lsn;
  1143. bool wrapped = false;
  1144. bool clean = false;
  1145. /*
  1146. * Find previous log record
  1147. */
  1148. if ((error = xlog_find_head(log, head_blk)))
  1149. return error;
  1150. ASSERT(*head_blk < INT_MAX);
  1151. buffer = xlog_alloc_buffer(log, 1);
  1152. if (!buffer)
  1153. return -ENOMEM;
  1154. if (*head_blk == 0) { /* special case */
  1155. error = xlog_bread(log, 0, 1, buffer, &offset);
  1156. if (error)
  1157. goto done;
  1158. if (xlog_get_cycle(offset) == 0) {
  1159. *tail_blk = 0;
  1160. /* leave all other log inited values alone */
  1161. goto done;
  1162. }
  1163. }
  1164. /*
  1165. * Search backwards through the log looking for the log record header
  1166. * block. This wraps all the way back around to the head so something is
  1167. * seriously wrong if we can't find it.
  1168. */
  1169. error = xlog_rseek_logrec_hdr(log, *head_blk, *head_blk, 1, buffer,
  1170. &rhead_blk, &rhead, &wrapped);
  1171. if (error < 0)
  1172. goto done;
  1173. if (!error) {
  1174. xfs_warn(log->l_mp, "%s: couldn't find sync record", __func__);
  1175. error = -EFSCORRUPTED;
  1176. goto done;
  1177. }
  1178. *tail_blk = BLOCK_LSN(be64_to_cpu(rhead->h_tail_lsn));
  1179. /*
  1180. * Set the log state based on the current head record.
  1181. */
  1182. xlog_set_state(log, *head_blk, rhead, rhead_blk, wrapped);
  1183. tail_lsn = atomic64_read(&log->l_tail_lsn);
  1184. /*
  1185. * Look for an unmount record at the head of the log. This sets the log
  1186. * state to determine whether recovery is necessary.
  1187. */
  1188. error = xlog_check_unmount_rec(log, head_blk, tail_blk, rhead,
  1189. rhead_blk, buffer, &clean);
  1190. if (error)
  1191. goto done;
  1192. /*
  1193. * Verify the log head if the log is not clean (e.g., we have anything
  1194. * but an unmount record at the head). This uses CRC verification to
  1195. * detect and trim torn writes. If discovered, CRC failures are
  1196. * considered torn writes and the log head is trimmed accordingly.
  1197. *
  1198. * Note that we can only run CRC verification when the log is dirty
  1199. * because there's no guarantee that the log data behind an unmount
  1200. * record is compatible with the current architecture.
  1201. */
  1202. if (!clean) {
  1203. xfs_daddr_t orig_head = *head_blk;
  1204. error = xlog_verify_head(log, head_blk, tail_blk, buffer,
  1205. &rhead_blk, &rhead, &wrapped);
  1206. if (error)
  1207. goto done;
  1208. /* update in-core state again if the head changed */
  1209. if (*head_blk != orig_head) {
  1210. xlog_set_state(log, *head_blk, rhead, rhead_blk,
  1211. wrapped);
  1212. tail_lsn = atomic64_read(&log->l_tail_lsn);
  1213. error = xlog_check_unmount_rec(log, head_blk, tail_blk,
  1214. rhead, rhead_blk, buffer,
  1215. &clean);
  1216. if (error)
  1217. goto done;
  1218. }
  1219. }
  1220. /*
  1221. * Note that the unmount was clean. If the unmount was not clean, we
  1222. * need to know this to rebuild the superblock counters from the perag
  1223. * headers if we have a filesystem using non-persistent counters.
  1224. */
  1225. if (clean)
  1226. set_bit(XFS_OPSTATE_CLEAN, &log->l_mp->m_opstate);
  1227. /*
  1228. * Make sure that there are no blocks in front of the head
  1229. * with the same cycle number as the head. This can happen
  1230. * because we allow multiple outstanding log writes concurrently,
  1231. * and the later writes might make it out before earlier ones.
  1232. *
  1233. * We use the lsn from before modifying it so that we'll never
  1234. * overwrite the unmount record after a clean unmount.
  1235. *
  1236. * Do this only if we are going to recover the filesystem
  1237. *
  1238. * NOTE: This used to say "if (!readonly)"
  1239. * However on Linux, we can & do recover a read-only filesystem.
  1240. * We only skip recovery if NORECOVERY is specified on mount,
  1241. * in which case we would not be here.
  1242. *
  1243. * But... if the -device- itself is readonly, just skip this.
  1244. * We can't recover this device anyway, so it won't matter.
  1245. */
  1246. if (!xfs_readonly_buftarg(log->l_targ))
  1247. error = xlog_clear_stale_blocks(log, tail_lsn);
  1248. done:
  1249. kmem_free(buffer);
  1250. if (error)
  1251. xfs_warn(log->l_mp, "failed to locate log tail");
  1252. return error;
  1253. }
  1254. /*
  1255. * Is the log zeroed at all?
  1256. *
  1257. * The last binary search should be changed to perform an X block read
  1258. * once X becomes small enough. You can then search linearly through
  1259. * the X blocks. This will cut down on the number of reads we need to do.
  1260. *
  1261. * If the log is partially zeroed, this routine will pass back the blkno
  1262. * of the first block with cycle number 0. It won't have a complete LR
  1263. * preceding it.
  1264. *
  1265. * Return:
  1266. * 0 => the log is completely written to
  1267. * 1 => use *blk_no as the first block of the log
  1268. * <0 => error has occurred
  1269. */
  1270. STATIC int
  1271. xlog_find_zeroed(
  1272. struct xlog *log,
  1273. xfs_daddr_t *blk_no)
  1274. {
  1275. char *buffer;
  1276. char *offset;
  1277. uint first_cycle, last_cycle;
  1278. xfs_daddr_t new_blk, last_blk, start_blk;
  1279. xfs_daddr_t num_scan_bblks;
  1280. int error, log_bbnum = log->l_logBBsize;
  1281. *blk_no = 0;
  1282. /* check totally zeroed log */
  1283. buffer = xlog_alloc_buffer(log, 1);
  1284. if (!buffer)
  1285. return -ENOMEM;
  1286. error = xlog_bread(log, 0, 1, buffer, &offset);
  1287. if (error)
  1288. goto out_free_buffer;
  1289. first_cycle = xlog_get_cycle(offset);
  1290. if (first_cycle == 0) { /* completely zeroed log */
  1291. *blk_no = 0;
  1292. kmem_free(buffer);
  1293. return 1;
  1294. }
  1295. /* check partially zeroed log */
  1296. error = xlog_bread(log, log_bbnum-1, 1, buffer, &offset);
  1297. if (error)
  1298. goto out_free_buffer;
  1299. last_cycle = xlog_get_cycle(offset);
  1300. if (last_cycle != 0) { /* log completely written to */
  1301. kmem_free(buffer);
  1302. return 0;
  1303. }
  1304. /* we have a partially zeroed log */
  1305. last_blk = log_bbnum-1;
  1306. error = xlog_find_cycle_start(log, buffer, 0, &last_blk, 0);
  1307. if (error)
  1308. goto out_free_buffer;
  1309. /*
  1310. * Validate the answer. Because there is no way to guarantee that
  1311. * the entire log is made up of log records which are the same size,
  1312. * we scan over the defined maximum blocks. At this point, the maximum
  1313. * is not chosen to mean anything special. XXXmiken
  1314. */
  1315. num_scan_bblks = XLOG_TOTAL_REC_SHIFT(log);
  1316. ASSERT(num_scan_bblks <= INT_MAX);
  1317. if (last_blk < num_scan_bblks)
  1318. num_scan_bblks = last_blk;
  1319. start_blk = last_blk - num_scan_bblks;
  1320. /*
  1321. * We search for any instances of cycle number 0 that occur before
  1322. * our current estimate of the head. What we're trying to detect is
  1323. * 1 ... | 0 | 1 | 0...
  1324. * ^ binary search ends here
  1325. */
  1326. if ((error = xlog_find_verify_cycle(log, start_blk,
  1327. (int)num_scan_bblks, 0, &new_blk)))
  1328. goto out_free_buffer;
  1329. if (new_blk != -1)
  1330. last_blk = new_blk;
  1331. /*
  1332. * Potentially backup over partial log record write. We don't need
  1333. * to search the end of the log because we know it is zero.
  1334. */
  1335. error = xlog_find_verify_log_record(log, start_blk, &last_blk, 0);
  1336. if (error == 1)
  1337. error = -EIO;
  1338. if (error)
  1339. goto out_free_buffer;
  1340. *blk_no = last_blk;
  1341. out_free_buffer:
  1342. kmem_free(buffer);
  1343. if (error)
  1344. return error;
  1345. return 1;
  1346. }
  1347. /*
  1348. * These are simple subroutines used by xlog_clear_stale_blocks() below
  1349. * to initialize a buffer full of empty log record headers and write
  1350. * them into the log.
  1351. */
  1352. STATIC void
  1353. xlog_add_record(
  1354. struct xlog *log,
  1355. char *buf,
  1356. int cycle,
  1357. int block,
  1358. int tail_cycle,
  1359. int tail_block)
  1360. {
  1361. xlog_rec_header_t *recp = (xlog_rec_header_t *)buf;
  1362. memset(buf, 0, BBSIZE);
  1363. recp->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
  1364. recp->h_cycle = cpu_to_be32(cycle);
  1365. recp->h_version = cpu_to_be32(
  1366. xfs_has_logv2(log->l_mp) ? 2 : 1);
  1367. recp->h_lsn = cpu_to_be64(xlog_assign_lsn(cycle, block));
  1368. recp->h_tail_lsn = cpu_to_be64(xlog_assign_lsn(tail_cycle, tail_block));
  1369. recp->h_fmt = cpu_to_be32(XLOG_FMT);
  1370. memcpy(&recp->h_fs_uuid, &log->l_mp->m_sb.sb_uuid, sizeof(uuid_t));
  1371. }
  1372. STATIC int
  1373. xlog_write_log_records(
  1374. struct xlog *log,
  1375. int cycle,
  1376. int start_block,
  1377. int blocks,
  1378. int tail_cycle,
  1379. int tail_block)
  1380. {
  1381. char *offset;
  1382. char *buffer;
  1383. int balign, ealign;
  1384. int sectbb = log->l_sectBBsize;
  1385. int end_block = start_block + blocks;
  1386. int bufblks;
  1387. int error = 0;
  1388. int i, j = 0;
  1389. /*
  1390. * Greedily allocate a buffer big enough to handle the full
  1391. * range of basic blocks to be written. If that fails, try
  1392. * a smaller size. We need to be able to write at least a
  1393. * log sector, or we're out of luck.
  1394. */
  1395. bufblks = 1 << ffs(blocks);
  1396. while (bufblks > log->l_logBBsize)
  1397. bufblks >>= 1;
  1398. while (!(buffer = xlog_alloc_buffer(log, bufblks))) {
  1399. bufblks >>= 1;
  1400. if (bufblks < sectbb)
  1401. return -ENOMEM;
  1402. }
  1403. /* We may need to do a read at the start to fill in part of
  1404. * the buffer in the starting sector not covered by the first
  1405. * write below.
  1406. */
  1407. balign = round_down(start_block, sectbb);
  1408. if (balign != start_block) {
  1409. error = xlog_bread_noalign(log, start_block, 1, buffer);
  1410. if (error)
  1411. goto out_free_buffer;
  1412. j = start_block - balign;
  1413. }
  1414. for (i = start_block; i < end_block; i += bufblks) {
  1415. int bcount, endcount;
  1416. bcount = min(bufblks, end_block - start_block);
  1417. endcount = bcount - j;
  1418. /* We may need to do a read at the end to fill in part of
  1419. * the buffer in the final sector not covered by the write.
  1420. * If this is the same sector as the above read, skip it.
  1421. */
  1422. ealign = round_down(end_block, sectbb);
  1423. if (j == 0 && (start_block + endcount > ealign)) {
  1424. error = xlog_bread_noalign(log, ealign, sectbb,
  1425. buffer + BBTOB(ealign - start_block));
  1426. if (error)
  1427. break;
  1428. }
  1429. offset = buffer + xlog_align(log, start_block);
  1430. for (; j < endcount; j++) {
  1431. xlog_add_record(log, offset, cycle, i+j,
  1432. tail_cycle, tail_block);
  1433. offset += BBSIZE;
  1434. }
  1435. error = xlog_bwrite(log, start_block, endcount, buffer);
  1436. if (error)
  1437. break;
  1438. start_block += endcount;
  1439. j = 0;
  1440. }
  1441. out_free_buffer:
  1442. kmem_free(buffer);
  1443. return error;
  1444. }
  1445. /*
  1446. * This routine is called to blow away any incomplete log writes out
  1447. * in front of the log head. We do this so that we won't become confused
  1448. * if we come up, write only a little bit more, and then crash again.
  1449. * If we leave the partial log records out there, this situation could
  1450. * cause us to think those partial writes are valid blocks since they
  1451. * have the current cycle number. We get rid of them by overwriting them
  1452. * with empty log records with the old cycle number rather than the
  1453. * current one.
  1454. *
  1455. * The tail lsn is passed in rather than taken from
  1456. * the log so that we will not write over the unmount record after a
  1457. * clean unmount in a 512 block log. Doing so would leave the log without
  1458. * any valid log records in it until a new one was written. If we crashed
  1459. * during that time we would not be able to recover.
  1460. */
  1461. STATIC int
  1462. xlog_clear_stale_blocks(
  1463. struct xlog *log,
  1464. xfs_lsn_t tail_lsn)
  1465. {
  1466. int tail_cycle, head_cycle;
  1467. int tail_block, head_block;
  1468. int tail_distance, max_distance;
  1469. int distance;
  1470. int error;
  1471. tail_cycle = CYCLE_LSN(tail_lsn);
  1472. tail_block = BLOCK_LSN(tail_lsn);
  1473. head_cycle = log->l_curr_cycle;
  1474. head_block = log->l_curr_block;
  1475. /*
  1476. * Figure out the distance between the new head of the log
  1477. * and the tail. We want to write over any blocks beyond the
  1478. * head that we may have written just before the crash, but
  1479. * we don't want to overwrite the tail of the log.
  1480. */
  1481. if (head_cycle == tail_cycle) {
  1482. /*
  1483. * The tail is behind the head in the physical log,
  1484. * so the distance from the head to the tail is the
  1485. * distance from the head to the end of the log plus
  1486. * the distance from the beginning of the log to the
  1487. * tail.
  1488. */
  1489. if (XFS_IS_CORRUPT(log->l_mp,
  1490. head_block < tail_block ||
  1491. head_block >= log->l_logBBsize))
  1492. return -EFSCORRUPTED;
  1493. tail_distance = tail_block + (log->l_logBBsize - head_block);
  1494. } else {
  1495. /*
  1496. * The head is behind the tail in the physical log,
  1497. * so the distance from the head to the tail is just
  1498. * the tail block minus the head block.
  1499. */
  1500. if (XFS_IS_CORRUPT(log->l_mp,
  1501. head_block >= tail_block ||
  1502. head_cycle != tail_cycle + 1))
  1503. return -EFSCORRUPTED;
  1504. tail_distance = tail_block - head_block;
  1505. }
  1506. /*
  1507. * If the head is right up against the tail, we can't clear
  1508. * anything.
  1509. */
  1510. if (tail_distance <= 0) {
  1511. ASSERT(tail_distance == 0);
  1512. return 0;
  1513. }
  1514. max_distance = XLOG_TOTAL_REC_SHIFT(log);
  1515. /*
  1516. * Take the smaller of the maximum amount of outstanding I/O
  1517. * we could have and the distance to the tail to clear out.
  1518. * We take the smaller so that we don't overwrite the tail and
  1519. * we don't waste all day writing from the head to the tail
  1520. * for no reason.
  1521. */
  1522. max_distance = min(max_distance, tail_distance);
  1523. if ((head_block + max_distance) <= log->l_logBBsize) {
  1524. /*
  1525. * We can stomp all the blocks we need to without
  1526. * wrapping around the end of the log. Just do it
  1527. * in a single write. Use the cycle number of the
  1528. * current cycle minus one so that the log will look like:
  1529. * n ... | n - 1 ...
  1530. */
  1531. error = xlog_write_log_records(log, (head_cycle - 1),
  1532. head_block, max_distance, tail_cycle,
  1533. tail_block);
  1534. if (error)
  1535. return error;
  1536. } else {
  1537. /*
  1538. * We need to wrap around the end of the physical log in
  1539. * order to clear all the blocks. Do it in two separate
  1540. * I/Os. The first write should be from the head to the
  1541. * end of the physical log, and it should use the current
  1542. * cycle number minus one just like above.
  1543. */
  1544. distance = log->l_logBBsize - head_block;
  1545. error = xlog_write_log_records(log, (head_cycle - 1),
  1546. head_block, distance, tail_cycle,
  1547. tail_block);
  1548. if (error)
  1549. return error;
  1550. /*
  1551. * Now write the blocks at the start of the physical log.
  1552. * This writes the remainder of the blocks we want to clear.
  1553. * It uses the current cycle number since we're now on the
  1554. * same cycle as the head so that we get:
  1555. * n ... n ... | n - 1 ...
  1556. * ^^^^^ blocks we're writing
  1557. */
  1558. distance = max_distance - (log->l_logBBsize - head_block);
  1559. error = xlog_write_log_records(log, head_cycle, 0, distance,
  1560. tail_cycle, tail_block);
  1561. if (error)
  1562. return error;
  1563. }
  1564. return 0;
  1565. }
  1566. /*
  1567. * Release the recovered intent item in the AIL that matches the given intent
  1568. * type and intent id.
  1569. */
  1570. void
  1571. xlog_recover_release_intent(
  1572. struct xlog *log,
  1573. unsigned short intent_type,
  1574. uint64_t intent_id)
  1575. {
  1576. struct xfs_ail_cursor cur;
  1577. struct xfs_log_item *lip;
  1578. struct xfs_ail *ailp = log->l_ailp;
  1579. spin_lock(&ailp->ail_lock);
  1580. for (lip = xfs_trans_ail_cursor_first(ailp, &cur, 0); lip != NULL;
  1581. lip = xfs_trans_ail_cursor_next(ailp, &cur)) {
  1582. if (lip->li_type != intent_type)
  1583. continue;
  1584. if (!lip->li_ops->iop_match(lip, intent_id))
  1585. continue;
  1586. spin_unlock(&ailp->ail_lock);
  1587. lip->li_ops->iop_release(lip);
  1588. spin_lock(&ailp->ail_lock);
  1589. break;
  1590. }
  1591. xfs_trans_ail_cursor_done(&cur);
  1592. spin_unlock(&ailp->ail_lock);
  1593. }
  1594. int
  1595. xlog_recover_iget(
  1596. struct xfs_mount *mp,
  1597. xfs_ino_t ino,
  1598. struct xfs_inode **ipp)
  1599. {
  1600. int error;
  1601. error = xfs_iget(mp, NULL, ino, 0, 0, ipp);
  1602. if (error)
  1603. return error;
  1604. error = xfs_qm_dqattach(*ipp);
  1605. if (error) {
  1606. xfs_irele(*ipp);
  1607. return error;
  1608. }
  1609. if (VFS_I(*ipp)->i_nlink == 0)
  1610. xfs_iflags_set(*ipp, XFS_IRECOVERY);
  1611. return 0;
  1612. }
  1613. /******************************************************************************
  1614. *
  1615. * Log recover routines
  1616. *
  1617. ******************************************************************************
  1618. */
  1619. static const struct xlog_recover_item_ops *xlog_recover_item_ops[] = {
  1620. &xlog_buf_item_ops,
  1621. &xlog_inode_item_ops,
  1622. &xlog_dquot_item_ops,
  1623. &xlog_quotaoff_item_ops,
  1624. &xlog_icreate_item_ops,
  1625. &xlog_efi_item_ops,
  1626. &xlog_efd_item_ops,
  1627. &xlog_rui_item_ops,
  1628. &xlog_rud_item_ops,
  1629. &xlog_cui_item_ops,
  1630. &xlog_cud_item_ops,
  1631. &xlog_bui_item_ops,
  1632. &xlog_bud_item_ops,
  1633. &xlog_attri_item_ops,
  1634. &xlog_attrd_item_ops,
  1635. };
  1636. static const struct xlog_recover_item_ops *
  1637. xlog_find_item_ops(
  1638. struct xlog_recover_item *item)
  1639. {
  1640. unsigned int i;
  1641. for (i = 0; i < ARRAY_SIZE(xlog_recover_item_ops); i++)
  1642. if (ITEM_TYPE(item) == xlog_recover_item_ops[i]->item_type)
  1643. return xlog_recover_item_ops[i];
  1644. return NULL;
  1645. }
  1646. /*
  1647. * Sort the log items in the transaction.
  1648. *
  1649. * The ordering constraints are defined by the inode allocation and unlink
  1650. * behaviour. The rules are:
  1651. *
  1652. * 1. Every item is only logged once in a given transaction. Hence it
  1653. * represents the last logged state of the item. Hence ordering is
  1654. * dependent on the order in which operations need to be performed so
  1655. * required initial conditions are always met.
  1656. *
  1657. * 2. Cancelled buffers are recorded in pass 1 in a separate table and
  1658. * there's nothing to replay from them so we can simply cull them
  1659. * from the transaction. However, we can't do that until after we've
  1660. * replayed all the other items because they may be dependent on the
  1661. * cancelled buffer and replaying the cancelled buffer can remove it
  1662. * form the cancelled buffer table. Hence they have tobe done last.
  1663. *
  1664. * 3. Inode allocation buffers must be replayed before inode items that
  1665. * read the buffer and replay changes into it. For filesystems using the
  1666. * ICREATE transactions, this means XFS_LI_ICREATE objects need to get
  1667. * treated the same as inode allocation buffers as they create and
  1668. * initialise the buffers directly.
  1669. *
  1670. * 4. Inode unlink buffers must be replayed after inode items are replayed.
  1671. * This ensures that inodes are completely flushed to the inode buffer
  1672. * in a "free" state before we remove the unlinked inode list pointer.
  1673. *
  1674. * Hence the ordering needs to be inode allocation buffers first, inode items
  1675. * second, inode unlink buffers third and cancelled buffers last.
  1676. *
  1677. * But there's a problem with that - we can't tell an inode allocation buffer
  1678. * apart from a regular buffer, so we can't separate them. We can, however,
  1679. * tell an inode unlink buffer from the others, and so we can separate them out
  1680. * from all the other buffers and move them to last.
  1681. *
  1682. * Hence, 4 lists, in order from head to tail:
  1683. * - buffer_list for all buffers except cancelled/inode unlink buffers
  1684. * - item_list for all non-buffer items
  1685. * - inode_buffer_list for inode unlink buffers
  1686. * - cancel_list for the cancelled buffers
  1687. *
  1688. * Note that we add objects to the tail of the lists so that first-to-last
  1689. * ordering is preserved within the lists. Adding objects to the head of the
  1690. * list means when we traverse from the head we walk them in last-to-first
  1691. * order. For cancelled buffers and inode unlink buffers this doesn't matter,
  1692. * but for all other items there may be specific ordering that we need to
  1693. * preserve.
  1694. */
  1695. STATIC int
  1696. xlog_recover_reorder_trans(
  1697. struct xlog *log,
  1698. struct xlog_recover *trans,
  1699. int pass)
  1700. {
  1701. struct xlog_recover_item *item, *n;
  1702. int error = 0;
  1703. LIST_HEAD(sort_list);
  1704. LIST_HEAD(cancel_list);
  1705. LIST_HEAD(buffer_list);
  1706. LIST_HEAD(inode_buffer_list);
  1707. LIST_HEAD(item_list);
  1708. list_splice_init(&trans->r_itemq, &sort_list);
  1709. list_for_each_entry_safe(item, n, &sort_list, ri_list) {
  1710. enum xlog_recover_reorder fate = XLOG_REORDER_ITEM_LIST;
  1711. item->ri_ops = xlog_find_item_ops(item);
  1712. if (!item->ri_ops) {
  1713. xfs_warn(log->l_mp,
  1714. "%s: unrecognized type of log operation (%d)",
  1715. __func__, ITEM_TYPE(item));
  1716. ASSERT(0);
  1717. /*
  1718. * return the remaining items back to the transaction
  1719. * item list so they can be freed in caller.
  1720. */
  1721. if (!list_empty(&sort_list))
  1722. list_splice_init(&sort_list, &trans->r_itemq);
  1723. error = -EFSCORRUPTED;
  1724. break;
  1725. }
  1726. if (item->ri_ops->reorder)
  1727. fate = item->ri_ops->reorder(item);
  1728. switch (fate) {
  1729. case XLOG_REORDER_BUFFER_LIST:
  1730. list_move_tail(&item->ri_list, &buffer_list);
  1731. break;
  1732. case XLOG_REORDER_CANCEL_LIST:
  1733. trace_xfs_log_recover_item_reorder_head(log,
  1734. trans, item, pass);
  1735. list_move(&item->ri_list, &cancel_list);
  1736. break;
  1737. case XLOG_REORDER_INODE_BUFFER_LIST:
  1738. list_move(&item->ri_list, &inode_buffer_list);
  1739. break;
  1740. case XLOG_REORDER_ITEM_LIST:
  1741. trace_xfs_log_recover_item_reorder_tail(log,
  1742. trans, item, pass);
  1743. list_move_tail(&item->ri_list, &item_list);
  1744. break;
  1745. }
  1746. }
  1747. ASSERT(list_empty(&sort_list));
  1748. if (!list_empty(&buffer_list))
  1749. list_splice(&buffer_list, &trans->r_itemq);
  1750. if (!list_empty(&item_list))
  1751. list_splice_tail(&item_list, &trans->r_itemq);
  1752. if (!list_empty(&inode_buffer_list))
  1753. list_splice_tail(&inode_buffer_list, &trans->r_itemq);
  1754. if (!list_empty(&cancel_list))
  1755. list_splice_tail(&cancel_list, &trans->r_itemq);
  1756. return error;
  1757. }
  1758. void
  1759. xlog_buf_readahead(
  1760. struct xlog *log,
  1761. xfs_daddr_t blkno,
  1762. uint len,
  1763. const struct xfs_buf_ops *ops)
  1764. {
  1765. if (!xlog_is_buffer_cancelled(log, blkno, len))
  1766. xfs_buf_readahead(log->l_mp->m_ddev_targp, blkno, len, ops);
  1767. }
  1768. STATIC int
  1769. xlog_recover_items_pass2(
  1770. struct xlog *log,
  1771. struct xlog_recover *trans,
  1772. struct list_head *buffer_list,
  1773. struct list_head *item_list)
  1774. {
  1775. struct xlog_recover_item *item;
  1776. int error = 0;
  1777. list_for_each_entry(item, item_list, ri_list) {
  1778. trace_xfs_log_recover_item_recover(log, trans, item,
  1779. XLOG_RECOVER_PASS2);
  1780. if (item->ri_ops->commit_pass2)
  1781. error = item->ri_ops->commit_pass2(log, buffer_list,
  1782. item, trans->r_lsn);
  1783. if (error)
  1784. return error;
  1785. }
  1786. return error;
  1787. }
  1788. /*
  1789. * Perform the transaction.
  1790. *
  1791. * If the transaction modifies a buffer or inode, do it now. Otherwise,
  1792. * EFIs and EFDs get queued up by adding entries into the AIL for them.
  1793. */
  1794. STATIC int
  1795. xlog_recover_commit_trans(
  1796. struct xlog *log,
  1797. struct xlog_recover *trans,
  1798. int pass,
  1799. struct list_head *buffer_list)
  1800. {
  1801. int error = 0;
  1802. int items_queued = 0;
  1803. struct xlog_recover_item *item;
  1804. struct xlog_recover_item *next;
  1805. LIST_HEAD (ra_list);
  1806. LIST_HEAD (done_list);
  1807. #define XLOG_RECOVER_COMMIT_QUEUE_MAX 100
  1808. hlist_del_init(&trans->r_list);
  1809. error = xlog_recover_reorder_trans(log, trans, pass);
  1810. if (error)
  1811. return error;
  1812. list_for_each_entry_safe(item, next, &trans->r_itemq, ri_list) {
  1813. trace_xfs_log_recover_item_recover(log, trans, item, pass);
  1814. switch (pass) {
  1815. case XLOG_RECOVER_PASS1:
  1816. if (item->ri_ops->commit_pass1)
  1817. error = item->ri_ops->commit_pass1(log, item);
  1818. break;
  1819. case XLOG_RECOVER_PASS2:
  1820. if (item->ri_ops->ra_pass2)
  1821. item->ri_ops->ra_pass2(log, item);
  1822. list_move_tail(&item->ri_list, &ra_list);
  1823. items_queued++;
  1824. if (items_queued >= XLOG_RECOVER_COMMIT_QUEUE_MAX) {
  1825. error = xlog_recover_items_pass2(log, trans,
  1826. buffer_list, &ra_list);
  1827. list_splice_tail_init(&ra_list, &done_list);
  1828. items_queued = 0;
  1829. }
  1830. break;
  1831. default:
  1832. ASSERT(0);
  1833. }
  1834. if (error)
  1835. goto out;
  1836. }
  1837. out:
  1838. if (!list_empty(&ra_list)) {
  1839. if (!error)
  1840. error = xlog_recover_items_pass2(log, trans,
  1841. buffer_list, &ra_list);
  1842. list_splice_tail_init(&ra_list, &done_list);
  1843. }
  1844. if (!list_empty(&done_list))
  1845. list_splice_init(&done_list, &trans->r_itemq);
  1846. return error;
  1847. }
  1848. STATIC void
  1849. xlog_recover_add_item(
  1850. struct list_head *head)
  1851. {
  1852. struct xlog_recover_item *item;
  1853. item = kmem_zalloc(sizeof(struct xlog_recover_item), 0);
  1854. INIT_LIST_HEAD(&item->ri_list);
  1855. list_add_tail(&item->ri_list, head);
  1856. }
  1857. STATIC int
  1858. xlog_recover_add_to_cont_trans(
  1859. struct xlog *log,
  1860. struct xlog_recover *trans,
  1861. char *dp,
  1862. int len)
  1863. {
  1864. struct xlog_recover_item *item;
  1865. char *ptr, *old_ptr;
  1866. int old_len;
  1867. /*
  1868. * If the transaction is empty, the header was split across this and the
  1869. * previous record. Copy the rest of the header.
  1870. */
  1871. if (list_empty(&trans->r_itemq)) {
  1872. ASSERT(len <= sizeof(struct xfs_trans_header));
  1873. if (len > sizeof(struct xfs_trans_header)) {
  1874. xfs_warn(log->l_mp, "%s: bad header length", __func__);
  1875. return -EFSCORRUPTED;
  1876. }
  1877. xlog_recover_add_item(&trans->r_itemq);
  1878. ptr = (char *)&trans->r_theader +
  1879. sizeof(struct xfs_trans_header) - len;
  1880. memcpy(ptr, dp, len);
  1881. return 0;
  1882. }
  1883. /* take the tail entry */
  1884. item = list_entry(trans->r_itemq.prev, struct xlog_recover_item,
  1885. ri_list);
  1886. old_ptr = item->ri_buf[item->ri_cnt-1].i_addr;
  1887. old_len = item->ri_buf[item->ri_cnt-1].i_len;
  1888. ptr = kvrealloc(old_ptr, old_len, len + old_len, GFP_KERNEL);
  1889. if (!ptr)
  1890. return -ENOMEM;
  1891. memcpy(&ptr[old_len], dp, len);
  1892. item->ri_buf[item->ri_cnt-1].i_len += len;
  1893. item->ri_buf[item->ri_cnt-1].i_addr = ptr;
  1894. trace_xfs_log_recover_item_add_cont(log, trans, item, 0);
  1895. return 0;
  1896. }
  1897. /*
  1898. * The next region to add is the start of a new region. It could be
  1899. * a whole region or it could be the first part of a new region. Because
  1900. * of this, the assumption here is that the type and size fields of all
  1901. * format structures fit into the first 32 bits of the structure.
  1902. *
  1903. * This works because all regions must be 32 bit aligned. Therefore, we
  1904. * either have both fields or we have neither field. In the case we have
  1905. * neither field, the data part of the region is zero length. We only have
  1906. * a log_op_header and can throw away the header since a new one will appear
  1907. * later. If we have at least 4 bytes, then we can determine how many regions
  1908. * will appear in the current log item.
  1909. */
  1910. STATIC int
  1911. xlog_recover_add_to_trans(
  1912. struct xlog *log,
  1913. struct xlog_recover *trans,
  1914. char *dp,
  1915. int len)
  1916. {
  1917. struct xfs_inode_log_format *in_f; /* any will do */
  1918. struct xlog_recover_item *item;
  1919. char *ptr;
  1920. if (!len)
  1921. return 0;
  1922. if (list_empty(&trans->r_itemq)) {
  1923. /* we need to catch log corruptions here */
  1924. if (*(uint *)dp != XFS_TRANS_HEADER_MAGIC) {
  1925. xfs_warn(log->l_mp, "%s: bad header magic number",
  1926. __func__);
  1927. ASSERT(0);
  1928. return -EFSCORRUPTED;
  1929. }
  1930. if (len > sizeof(struct xfs_trans_header)) {
  1931. xfs_warn(log->l_mp, "%s: bad header length", __func__);
  1932. ASSERT(0);
  1933. return -EFSCORRUPTED;
  1934. }
  1935. /*
  1936. * The transaction header can be arbitrarily split across op
  1937. * records. If we don't have the whole thing here, copy what we
  1938. * do have and handle the rest in the next record.
  1939. */
  1940. if (len == sizeof(struct xfs_trans_header))
  1941. xlog_recover_add_item(&trans->r_itemq);
  1942. memcpy(&trans->r_theader, dp, len);
  1943. return 0;
  1944. }
  1945. ptr = kmem_alloc(len, 0);
  1946. memcpy(ptr, dp, len);
  1947. in_f = (struct xfs_inode_log_format *)ptr;
  1948. /* take the tail entry */
  1949. item = list_entry(trans->r_itemq.prev, struct xlog_recover_item,
  1950. ri_list);
  1951. if (item->ri_total != 0 &&
  1952. item->ri_total == item->ri_cnt) {
  1953. /* tail item is in use, get a new one */
  1954. xlog_recover_add_item(&trans->r_itemq);
  1955. item = list_entry(trans->r_itemq.prev,
  1956. struct xlog_recover_item, ri_list);
  1957. }
  1958. if (item->ri_total == 0) { /* first region to be added */
  1959. if (in_f->ilf_size == 0 ||
  1960. in_f->ilf_size > XLOG_MAX_REGIONS_IN_ITEM) {
  1961. xfs_warn(log->l_mp,
  1962. "bad number of regions (%d) in inode log format",
  1963. in_f->ilf_size);
  1964. ASSERT(0);
  1965. kmem_free(ptr);
  1966. return -EFSCORRUPTED;
  1967. }
  1968. item->ri_total = in_f->ilf_size;
  1969. item->ri_buf =
  1970. kmem_zalloc(item->ri_total * sizeof(xfs_log_iovec_t),
  1971. 0);
  1972. }
  1973. if (item->ri_total <= item->ri_cnt) {
  1974. xfs_warn(log->l_mp,
  1975. "log item region count (%d) overflowed size (%d)",
  1976. item->ri_cnt, item->ri_total);
  1977. ASSERT(0);
  1978. kmem_free(ptr);
  1979. return -EFSCORRUPTED;
  1980. }
  1981. /* Description region is ri_buf[0] */
  1982. item->ri_buf[item->ri_cnt].i_addr = ptr;
  1983. item->ri_buf[item->ri_cnt].i_len = len;
  1984. item->ri_cnt++;
  1985. trace_xfs_log_recover_item_add(log, trans, item, 0);
  1986. return 0;
  1987. }
  1988. /*
  1989. * Free up any resources allocated by the transaction
  1990. *
  1991. * Remember that EFIs, EFDs, and IUNLINKs are handled later.
  1992. */
  1993. STATIC void
  1994. xlog_recover_free_trans(
  1995. struct xlog_recover *trans)
  1996. {
  1997. struct xlog_recover_item *item, *n;
  1998. int i;
  1999. hlist_del_init(&trans->r_list);
  2000. list_for_each_entry_safe(item, n, &trans->r_itemq, ri_list) {
  2001. /* Free the regions in the item. */
  2002. list_del(&item->ri_list);
  2003. for (i = 0; i < item->ri_cnt; i++)
  2004. kmem_free(item->ri_buf[i].i_addr);
  2005. /* Free the item itself */
  2006. kmem_free(item->ri_buf);
  2007. kmem_free(item);
  2008. }
  2009. /* Free the transaction recover structure */
  2010. kmem_free(trans);
  2011. }
  2012. /*
  2013. * On error or completion, trans is freed.
  2014. */
  2015. STATIC int
  2016. xlog_recovery_process_trans(
  2017. struct xlog *log,
  2018. struct xlog_recover *trans,
  2019. char *dp,
  2020. unsigned int len,
  2021. unsigned int flags,
  2022. int pass,
  2023. struct list_head *buffer_list)
  2024. {
  2025. int error = 0;
  2026. bool freeit = false;
  2027. /* mask off ophdr transaction container flags */
  2028. flags &= ~XLOG_END_TRANS;
  2029. if (flags & XLOG_WAS_CONT_TRANS)
  2030. flags &= ~XLOG_CONTINUE_TRANS;
  2031. /*
  2032. * Callees must not free the trans structure. We'll decide if we need to
  2033. * free it or not based on the operation being done and it's result.
  2034. */
  2035. switch (flags) {
  2036. /* expected flag values */
  2037. case 0:
  2038. case XLOG_CONTINUE_TRANS:
  2039. error = xlog_recover_add_to_trans(log, trans, dp, len);
  2040. break;
  2041. case XLOG_WAS_CONT_TRANS:
  2042. error = xlog_recover_add_to_cont_trans(log, trans, dp, len);
  2043. break;
  2044. case XLOG_COMMIT_TRANS:
  2045. error = xlog_recover_commit_trans(log, trans, pass,
  2046. buffer_list);
  2047. /* success or fail, we are now done with this transaction. */
  2048. freeit = true;
  2049. break;
  2050. /* unexpected flag values */
  2051. case XLOG_UNMOUNT_TRANS:
  2052. /* just skip trans */
  2053. xfs_warn(log->l_mp, "%s: Unmount LR", __func__);
  2054. freeit = true;
  2055. break;
  2056. case XLOG_START_TRANS:
  2057. default:
  2058. xfs_warn(log->l_mp, "%s: bad flag 0x%x", __func__, flags);
  2059. ASSERT(0);
  2060. error = -EFSCORRUPTED;
  2061. break;
  2062. }
  2063. if (error || freeit)
  2064. xlog_recover_free_trans(trans);
  2065. return error;
  2066. }
  2067. /*
  2068. * Lookup the transaction recovery structure associated with the ID in the
  2069. * current ophdr. If the transaction doesn't exist and the start flag is set in
  2070. * the ophdr, then allocate a new transaction for future ID matches to find.
  2071. * Either way, return what we found during the lookup - an existing transaction
  2072. * or nothing.
  2073. */
  2074. STATIC struct xlog_recover *
  2075. xlog_recover_ophdr_to_trans(
  2076. struct hlist_head rhash[],
  2077. struct xlog_rec_header *rhead,
  2078. struct xlog_op_header *ohead)
  2079. {
  2080. struct xlog_recover *trans;
  2081. xlog_tid_t tid;
  2082. struct hlist_head *rhp;
  2083. tid = be32_to_cpu(ohead->oh_tid);
  2084. rhp = &rhash[XLOG_RHASH(tid)];
  2085. hlist_for_each_entry(trans, rhp, r_list) {
  2086. if (trans->r_log_tid == tid)
  2087. return trans;
  2088. }
  2089. /*
  2090. * skip over non-start transaction headers - we could be
  2091. * processing slack space before the next transaction starts
  2092. */
  2093. if (!(ohead->oh_flags & XLOG_START_TRANS))
  2094. return NULL;
  2095. ASSERT(be32_to_cpu(ohead->oh_len) == 0);
  2096. /*
  2097. * This is a new transaction so allocate a new recovery container to
  2098. * hold the recovery ops that will follow.
  2099. */
  2100. trans = kmem_zalloc(sizeof(struct xlog_recover), 0);
  2101. trans->r_log_tid = tid;
  2102. trans->r_lsn = be64_to_cpu(rhead->h_lsn);
  2103. INIT_LIST_HEAD(&trans->r_itemq);
  2104. INIT_HLIST_NODE(&trans->r_list);
  2105. hlist_add_head(&trans->r_list, rhp);
  2106. /*
  2107. * Nothing more to do for this ophdr. Items to be added to this new
  2108. * transaction will be in subsequent ophdr containers.
  2109. */
  2110. return NULL;
  2111. }
  2112. STATIC int
  2113. xlog_recover_process_ophdr(
  2114. struct xlog *log,
  2115. struct hlist_head rhash[],
  2116. struct xlog_rec_header *rhead,
  2117. struct xlog_op_header *ohead,
  2118. char *dp,
  2119. char *end,
  2120. int pass,
  2121. struct list_head *buffer_list)
  2122. {
  2123. struct xlog_recover *trans;
  2124. unsigned int len;
  2125. int error;
  2126. /* Do we understand who wrote this op? */
  2127. if (ohead->oh_clientid != XFS_TRANSACTION &&
  2128. ohead->oh_clientid != XFS_LOG) {
  2129. xfs_warn(log->l_mp, "%s: bad clientid 0x%x",
  2130. __func__, ohead->oh_clientid);
  2131. ASSERT(0);
  2132. return -EFSCORRUPTED;
  2133. }
  2134. /*
  2135. * Check the ophdr contains all the data it is supposed to contain.
  2136. */
  2137. len = be32_to_cpu(ohead->oh_len);
  2138. if (dp + len > end) {
  2139. xfs_warn(log->l_mp, "%s: bad length 0x%x", __func__, len);
  2140. WARN_ON(1);
  2141. return -EFSCORRUPTED;
  2142. }
  2143. trans = xlog_recover_ophdr_to_trans(rhash, rhead, ohead);
  2144. if (!trans) {
  2145. /* nothing to do, so skip over this ophdr */
  2146. return 0;
  2147. }
  2148. /*
  2149. * The recovered buffer queue is drained only once we know that all
  2150. * recovery items for the current LSN have been processed. This is
  2151. * required because:
  2152. *
  2153. * - Buffer write submission updates the metadata LSN of the buffer.
  2154. * - Log recovery skips items with a metadata LSN >= the current LSN of
  2155. * the recovery item.
  2156. * - Separate recovery items against the same metadata buffer can share
  2157. * a current LSN. I.e., consider that the LSN of a recovery item is
  2158. * defined as the starting LSN of the first record in which its
  2159. * transaction appears, that a record can hold multiple transactions,
  2160. * and/or that a transaction can span multiple records.
  2161. *
  2162. * In other words, we are allowed to submit a buffer from log recovery
  2163. * once per current LSN. Otherwise, we may incorrectly skip recovery
  2164. * items and cause corruption.
  2165. *
  2166. * We don't know up front whether buffers are updated multiple times per
  2167. * LSN. Therefore, track the current LSN of each commit log record as it
  2168. * is processed and drain the queue when it changes. Use commit records
  2169. * because they are ordered correctly by the logging code.
  2170. */
  2171. if (log->l_recovery_lsn != trans->r_lsn &&
  2172. ohead->oh_flags & XLOG_COMMIT_TRANS) {
  2173. error = xfs_buf_delwri_submit(buffer_list);
  2174. if (error)
  2175. return error;
  2176. log->l_recovery_lsn = trans->r_lsn;
  2177. }
  2178. return xlog_recovery_process_trans(log, trans, dp, len,
  2179. ohead->oh_flags, pass, buffer_list);
  2180. }
  2181. /*
  2182. * There are two valid states of the r_state field. 0 indicates that the
  2183. * transaction structure is in a normal state. We have either seen the
  2184. * start of the transaction or the last operation we added was not a partial
  2185. * operation. If the last operation we added to the transaction was a
  2186. * partial operation, we need to mark r_state with XLOG_WAS_CONT_TRANS.
  2187. *
  2188. * NOTE: skip LRs with 0 data length.
  2189. */
  2190. STATIC int
  2191. xlog_recover_process_data(
  2192. struct xlog *log,
  2193. struct hlist_head rhash[],
  2194. struct xlog_rec_header *rhead,
  2195. char *dp,
  2196. int pass,
  2197. struct list_head *buffer_list)
  2198. {
  2199. struct xlog_op_header *ohead;
  2200. char *end;
  2201. int num_logops;
  2202. int error;
  2203. end = dp + be32_to_cpu(rhead->h_len);
  2204. num_logops = be32_to_cpu(rhead->h_num_logops);
  2205. /* check the log format matches our own - else we can't recover */
  2206. if (xlog_header_check_recover(log->l_mp, rhead))
  2207. return -EIO;
  2208. trace_xfs_log_recover_record(log, rhead, pass);
  2209. while ((dp < end) && num_logops) {
  2210. ohead = (struct xlog_op_header *)dp;
  2211. dp += sizeof(*ohead);
  2212. ASSERT(dp <= end);
  2213. /* errors will abort recovery */
  2214. error = xlog_recover_process_ophdr(log, rhash, rhead, ohead,
  2215. dp, end, pass, buffer_list);
  2216. if (error)
  2217. return error;
  2218. dp += be32_to_cpu(ohead->oh_len);
  2219. num_logops--;
  2220. }
  2221. return 0;
  2222. }
  2223. /* Take all the collected deferred ops and finish them in order. */
  2224. static int
  2225. xlog_finish_defer_ops(
  2226. struct xfs_mount *mp,
  2227. struct list_head *capture_list)
  2228. {
  2229. struct xfs_defer_capture *dfc, *next;
  2230. struct xfs_trans *tp;
  2231. int error = 0;
  2232. list_for_each_entry_safe(dfc, next, capture_list, dfc_list) {
  2233. struct xfs_trans_res resv;
  2234. struct xfs_defer_resources dres;
  2235. /*
  2236. * Create a new transaction reservation from the captured
  2237. * information. Set logcount to 1 to force the new transaction
  2238. * to regrant every roll so that we can make forward progress
  2239. * in recovery no matter how full the log might be.
  2240. */
  2241. resv.tr_logres = dfc->dfc_logres;
  2242. resv.tr_logcount = 1;
  2243. resv.tr_logflags = XFS_TRANS_PERM_LOG_RES;
  2244. error = xfs_trans_alloc(mp, &resv, dfc->dfc_blkres,
  2245. dfc->dfc_rtxres, XFS_TRANS_RESERVE, &tp);
  2246. if (error) {
  2247. xlog_force_shutdown(mp->m_log, SHUTDOWN_LOG_IO_ERROR);
  2248. return error;
  2249. }
  2250. /*
  2251. * Transfer to this new transaction all the dfops we captured
  2252. * from recovering a single intent item.
  2253. */
  2254. list_del_init(&dfc->dfc_list);
  2255. xfs_defer_ops_continue(dfc, tp, &dres);
  2256. error = xfs_trans_commit(tp);
  2257. xfs_defer_resources_rele(&dres);
  2258. if (error)
  2259. return error;
  2260. }
  2261. ASSERT(list_empty(capture_list));
  2262. return 0;
  2263. }
  2264. /* Release all the captured defer ops and capture structures in this list. */
  2265. static void
  2266. xlog_abort_defer_ops(
  2267. struct xfs_mount *mp,
  2268. struct list_head *capture_list)
  2269. {
  2270. struct xfs_defer_capture *dfc;
  2271. struct xfs_defer_capture *next;
  2272. list_for_each_entry_safe(dfc, next, capture_list, dfc_list) {
  2273. list_del_init(&dfc->dfc_list);
  2274. xfs_defer_ops_capture_free(mp, dfc);
  2275. }
  2276. }
  2277. /*
  2278. * When this is called, all of the log intent items which did not have
  2279. * corresponding log done items should be in the AIL. What we do now is update
  2280. * the data structures associated with each one.
  2281. *
  2282. * Since we process the log intent items in normal transactions, they will be
  2283. * removed at some point after the commit. This prevents us from just walking
  2284. * down the list processing each one. We'll use a flag in the intent item to
  2285. * skip those that we've already processed and use the AIL iteration mechanism's
  2286. * generation count to try to speed this up at least a bit.
  2287. *
  2288. * When we start, we know that the intents are the only things in the AIL. As we
  2289. * process them, however, other items are added to the AIL. Hence we know we
  2290. * have started recovery on all the pending intents when we find an non-intent
  2291. * item in the AIL.
  2292. */
  2293. STATIC int
  2294. xlog_recover_process_intents(
  2295. struct xlog *log)
  2296. {
  2297. LIST_HEAD(capture_list);
  2298. struct xfs_ail_cursor cur;
  2299. struct xfs_log_item *lip;
  2300. struct xfs_ail *ailp;
  2301. int error = 0;
  2302. #if defined(DEBUG) || defined(XFS_WARN)
  2303. xfs_lsn_t last_lsn;
  2304. #endif
  2305. ailp = log->l_ailp;
  2306. spin_lock(&ailp->ail_lock);
  2307. #if defined(DEBUG) || defined(XFS_WARN)
  2308. last_lsn = xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block);
  2309. #endif
  2310. for (lip = xfs_trans_ail_cursor_first(ailp, &cur, 0);
  2311. lip != NULL;
  2312. lip = xfs_trans_ail_cursor_next(ailp, &cur)) {
  2313. const struct xfs_item_ops *ops;
  2314. if (!xlog_item_is_intent(lip))
  2315. break;
  2316. /*
  2317. * We should never see a redo item with a LSN higher than
  2318. * the last transaction we found in the log at the start
  2319. * of recovery.
  2320. */
  2321. ASSERT(XFS_LSN_CMP(last_lsn, lip->li_lsn) >= 0);
  2322. /*
  2323. * NOTE: If your intent processing routine can create more
  2324. * deferred ops, you /must/ attach them to the capture list in
  2325. * the recover routine or else those subsequent intents will be
  2326. * replayed in the wrong order!
  2327. *
  2328. * The recovery function can free the log item, so we must not
  2329. * access lip after it returns.
  2330. */
  2331. spin_unlock(&ailp->ail_lock);
  2332. ops = lip->li_ops;
  2333. error = ops->iop_recover(lip, &capture_list);
  2334. spin_lock(&ailp->ail_lock);
  2335. if (error) {
  2336. trace_xlog_intent_recovery_failed(log->l_mp, error,
  2337. ops->iop_recover);
  2338. break;
  2339. }
  2340. }
  2341. xfs_trans_ail_cursor_done(&cur);
  2342. spin_unlock(&ailp->ail_lock);
  2343. if (error)
  2344. goto err;
  2345. error = xlog_finish_defer_ops(log->l_mp, &capture_list);
  2346. if (error)
  2347. goto err;
  2348. return 0;
  2349. err:
  2350. xlog_abort_defer_ops(log->l_mp, &capture_list);
  2351. return error;
  2352. }
  2353. /*
  2354. * A cancel occurs when the mount has failed and we're bailing out. Release all
  2355. * pending log intent items that we haven't started recovery on so they don't
  2356. * pin the AIL.
  2357. */
  2358. STATIC void
  2359. xlog_recover_cancel_intents(
  2360. struct xlog *log)
  2361. {
  2362. struct xfs_log_item *lip;
  2363. struct xfs_ail_cursor cur;
  2364. struct xfs_ail *ailp;
  2365. ailp = log->l_ailp;
  2366. spin_lock(&ailp->ail_lock);
  2367. lip = xfs_trans_ail_cursor_first(ailp, &cur, 0);
  2368. while (lip != NULL) {
  2369. if (!xlog_item_is_intent(lip))
  2370. break;
  2371. spin_unlock(&ailp->ail_lock);
  2372. lip->li_ops->iop_release(lip);
  2373. spin_lock(&ailp->ail_lock);
  2374. lip = xfs_trans_ail_cursor_next(ailp, &cur);
  2375. }
  2376. xfs_trans_ail_cursor_done(&cur);
  2377. spin_unlock(&ailp->ail_lock);
  2378. }
  2379. /*
  2380. * This routine performs a transaction to null out a bad inode pointer
  2381. * in an agi unlinked inode hash bucket.
  2382. */
  2383. STATIC void
  2384. xlog_recover_clear_agi_bucket(
  2385. struct xfs_perag *pag,
  2386. int bucket)
  2387. {
  2388. struct xfs_mount *mp = pag->pag_mount;
  2389. struct xfs_trans *tp;
  2390. struct xfs_agi *agi;
  2391. struct xfs_buf *agibp;
  2392. int offset;
  2393. int error;
  2394. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_clearagi, 0, 0, 0, &tp);
  2395. if (error)
  2396. goto out_error;
  2397. error = xfs_read_agi(pag, tp, &agibp);
  2398. if (error)
  2399. goto out_abort;
  2400. agi = agibp->b_addr;
  2401. agi->agi_unlinked[bucket] = cpu_to_be32(NULLAGINO);
  2402. offset = offsetof(xfs_agi_t, agi_unlinked) +
  2403. (sizeof(xfs_agino_t) * bucket);
  2404. xfs_trans_log_buf(tp, agibp, offset,
  2405. (offset + sizeof(xfs_agino_t) - 1));
  2406. error = xfs_trans_commit(tp);
  2407. if (error)
  2408. goto out_error;
  2409. return;
  2410. out_abort:
  2411. xfs_trans_cancel(tp);
  2412. out_error:
  2413. xfs_warn(mp, "%s: failed to clear agi %d. Continuing.", __func__,
  2414. pag->pag_agno);
  2415. return;
  2416. }
  2417. static int
  2418. xlog_recover_iunlink_bucket(
  2419. struct xfs_perag *pag,
  2420. struct xfs_agi *agi,
  2421. int bucket)
  2422. {
  2423. struct xfs_mount *mp = pag->pag_mount;
  2424. struct xfs_inode *prev_ip = NULL;
  2425. struct xfs_inode *ip;
  2426. xfs_agino_t prev_agino, agino;
  2427. int error = 0;
  2428. agino = be32_to_cpu(agi->agi_unlinked[bucket]);
  2429. while (agino != NULLAGINO) {
  2430. error = xfs_iget(mp, NULL,
  2431. XFS_AGINO_TO_INO(mp, pag->pag_agno, agino),
  2432. 0, 0, &ip);
  2433. if (error)
  2434. break;
  2435. ASSERT(VFS_I(ip)->i_nlink == 0);
  2436. ASSERT(VFS_I(ip)->i_mode != 0);
  2437. xfs_iflags_clear(ip, XFS_IRECOVERY);
  2438. agino = ip->i_next_unlinked;
  2439. if (prev_ip) {
  2440. ip->i_prev_unlinked = prev_agino;
  2441. xfs_irele(prev_ip);
  2442. /*
  2443. * Ensure the inode is removed from the unlinked list
  2444. * before we continue so that it won't race with
  2445. * building the in-memory list here. This could be
  2446. * serialised with the agibp lock, but that just
  2447. * serialises via lockstepping and it's much simpler
  2448. * just to flush the inodegc queue and wait for it to
  2449. * complete.
  2450. */
  2451. xfs_inodegc_flush(mp);
  2452. }
  2453. prev_agino = agino;
  2454. prev_ip = ip;
  2455. }
  2456. if (prev_ip) {
  2457. ip->i_prev_unlinked = prev_agino;
  2458. xfs_irele(prev_ip);
  2459. }
  2460. xfs_inodegc_flush(mp);
  2461. return error;
  2462. }
  2463. /*
  2464. * Recover AGI unlinked lists
  2465. *
  2466. * This is called during recovery to process any inodes which we unlinked but
  2467. * not freed when the system crashed. These inodes will be on the lists in the
  2468. * AGI blocks. What we do here is scan all the AGIs and fully truncate and free
  2469. * any inodes found on the lists. Each inode is removed from the lists when it
  2470. * has been fully truncated and is freed. The freeing of the inode and its
  2471. * removal from the list must be atomic.
  2472. *
  2473. * If everything we touch in the agi processing loop is already in memory, this
  2474. * loop can hold the cpu for a long time. It runs without lock contention,
  2475. * memory allocation contention, the need wait for IO, etc, and so will run
  2476. * until we either run out of inodes to process, run low on memory or we run out
  2477. * of log space.
  2478. *
  2479. * This behaviour is bad for latency on single CPU and non-preemptible kernels,
  2480. * and can prevent other filesystem work (such as CIL pushes) from running. This
  2481. * can lead to deadlocks if the recovery process runs out of log reservation
  2482. * space. Hence we need to yield the CPU when there is other kernel work
  2483. * scheduled on this CPU to ensure other scheduled work can run without undue
  2484. * latency.
  2485. */
  2486. static void
  2487. xlog_recover_iunlink_ag(
  2488. struct xfs_perag *pag)
  2489. {
  2490. struct xfs_agi *agi;
  2491. struct xfs_buf *agibp;
  2492. int bucket;
  2493. int error;
  2494. error = xfs_read_agi(pag, NULL, &agibp);
  2495. if (error) {
  2496. /*
  2497. * AGI is b0rked. Don't process it.
  2498. *
  2499. * We should probably mark the filesystem as corrupt after we've
  2500. * recovered all the ag's we can....
  2501. */
  2502. return;
  2503. }
  2504. /*
  2505. * Unlock the buffer so that it can be acquired in the normal course of
  2506. * the transaction to truncate and free each inode. Because we are not
  2507. * racing with anyone else here for the AGI buffer, we don't even need
  2508. * to hold it locked to read the initial unlinked bucket entries out of
  2509. * the buffer. We keep buffer reference though, so that it stays pinned
  2510. * in memory while we need the buffer.
  2511. */
  2512. agi = agibp->b_addr;
  2513. xfs_buf_unlock(agibp);
  2514. for (bucket = 0; bucket < XFS_AGI_UNLINKED_BUCKETS; bucket++) {
  2515. error = xlog_recover_iunlink_bucket(pag, agi, bucket);
  2516. if (error) {
  2517. /*
  2518. * Bucket is unrecoverable, so only a repair scan can
  2519. * free the remaining unlinked inodes. Just empty the
  2520. * bucket and remaining inodes on it unreferenced and
  2521. * unfreeable.
  2522. */
  2523. xfs_inodegc_flush(pag->pag_mount);
  2524. xlog_recover_clear_agi_bucket(pag, bucket);
  2525. }
  2526. }
  2527. xfs_buf_rele(agibp);
  2528. }
  2529. static void
  2530. xlog_recover_process_iunlinks(
  2531. struct xlog *log)
  2532. {
  2533. struct xfs_perag *pag;
  2534. xfs_agnumber_t agno;
  2535. for_each_perag(log->l_mp, agno, pag)
  2536. xlog_recover_iunlink_ag(pag);
  2537. /*
  2538. * Flush the pending unlinked inodes to ensure that the inactivations
  2539. * are fully completed on disk and the incore inodes can be reclaimed
  2540. * before we signal that recovery is complete.
  2541. */
  2542. xfs_inodegc_flush(log->l_mp);
  2543. }
  2544. STATIC void
  2545. xlog_unpack_data(
  2546. struct xlog_rec_header *rhead,
  2547. char *dp,
  2548. struct xlog *log)
  2549. {
  2550. int i, j, k;
  2551. for (i = 0; i < BTOBB(be32_to_cpu(rhead->h_len)) &&
  2552. i < (XLOG_HEADER_CYCLE_SIZE / BBSIZE); i++) {
  2553. *(__be32 *)dp = *(__be32 *)&rhead->h_cycle_data[i];
  2554. dp += BBSIZE;
  2555. }
  2556. if (xfs_has_logv2(log->l_mp)) {
  2557. xlog_in_core_2_t *xhdr = (xlog_in_core_2_t *)rhead;
  2558. for ( ; i < BTOBB(be32_to_cpu(rhead->h_len)); i++) {
  2559. j = i / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
  2560. k = i % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
  2561. *(__be32 *)dp = xhdr[j].hic_xheader.xh_cycle_data[k];
  2562. dp += BBSIZE;
  2563. }
  2564. }
  2565. }
  2566. /*
  2567. * CRC check, unpack and process a log record.
  2568. */
  2569. STATIC int
  2570. xlog_recover_process(
  2571. struct xlog *log,
  2572. struct hlist_head rhash[],
  2573. struct xlog_rec_header *rhead,
  2574. char *dp,
  2575. int pass,
  2576. struct list_head *buffer_list)
  2577. {
  2578. __le32 old_crc = rhead->h_crc;
  2579. __le32 crc;
  2580. crc = xlog_cksum(log, rhead, dp, be32_to_cpu(rhead->h_len));
  2581. /*
  2582. * Nothing else to do if this is a CRC verification pass. Just return
  2583. * if this a record with a non-zero crc. Unfortunately, mkfs always
  2584. * sets old_crc to 0 so we must consider this valid even on v5 supers.
  2585. * Otherwise, return EFSBADCRC on failure so the callers up the stack
  2586. * know precisely what failed.
  2587. */
  2588. if (pass == XLOG_RECOVER_CRCPASS) {
  2589. if (old_crc && crc != old_crc)
  2590. return -EFSBADCRC;
  2591. return 0;
  2592. }
  2593. /*
  2594. * We're in the normal recovery path. Issue a warning if and only if the
  2595. * CRC in the header is non-zero. This is an advisory warning and the
  2596. * zero CRC check prevents warnings from being emitted when upgrading
  2597. * the kernel from one that does not add CRCs by default.
  2598. */
  2599. if (crc != old_crc) {
  2600. if (old_crc || xfs_has_crc(log->l_mp)) {
  2601. xfs_alert(log->l_mp,
  2602. "log record CRC mismatch: found 0x%x, expected 0x%x.",
  2603. le32_to_cpu(old_crc),
  2604. le32_to_cpu(crc));
  2605. xfs_hex_dump(dp, 32);
  2606. }
  2607. /*
  2608. * If the filesystem is CRC enabled, this mismatch becomes a
  2609. * fatal log corruption failure.
  2610. */
  2611. if (xfs_has_crc(log->l_mp)) {
  2612. XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, log->l_mp);
  2613. return -EFSCORRUPTED;
  2614. }
  2615. }
  2616. xlog_unpack_data(rhead, dp, log);
  2617. return xlog_recover_process_data(log, rhash, rhead, dp, pass,
  2618. buffer_list);
  2619. }
  2620. STATIC int
  2621. xlog_valid_rec_header(
  2622. struct xlog *log,
  2623. struct xlog_rec_header *rhead,
  2624. xfs_daddr_t blkno,
  2625. int bufsize)
  2626. {
  2627. int hlen;
  2628. if (XFS_IS_CORRUPT(log->l_mp,
  2629. rhead->h_magicno != cpu_to_be32(XLOG_HEADER_MAGIC_NUM)))
  2630. return -EFSCORRUPTED;
  2631. if (XFS_IS_CORRUPT(log->l_mp,
  2632. (!rhead->h_version ||
  2633. (be32_to_cpu(rhead->h_version) &
  2634. (~XLOG_VERSION_OKBITS))))) {
  2635. xfs_warn(log->l_mp, "%s: unrecognised log version (%d).",
  2636. __func__, be32_to_cpu(rhead->h_version));
  2637. return -EFSCORRUPTED;
  2638. }
  2639. /*
  2640. * LR body must have data (or it wouldn't have been written)
  2641. * and h_len must not be greater than LR buffer size.
  2642. */
  2643. hlen = be32_to_cpu(rhead->h_len);
  2644. if (XFS_IS_CORRUPT(log->l_mp, hlen <= 0 || hlen > bufsize))
  2645. return -EFSCORRUPTED;
  2646. if (XFS_IS_CORRUPT(log->l_mp,
  2647. blkno > log->l_logBBsize || blkno > INT_MAX))
  2648. return -EFSCORRUPTED;
  2649. return 0;
  2650. }
  2651. /*
  2652. * Read the log from tail to head and process the log records found.
  2653. * Handle the two cases where the tail and head are in the same cycle
  2654. * and where the active portion of the log wraps around the end of
  2655. * the physical log separately. The pass parameter is passed through
  2656. * to the routines called to process the data and is not looked at
  2657. * here.
  2658. */
  2659. STATIC int
  2660. xlog_do_recovery_pass(
  2661. struct xlog *log,
  2662. xfs_daddr_t head_blk,
  2663. xfs_daddr_t tail_blk,
  2664. int pass,
  2665. xfs_daddr_t *first_bad) /* out: first bad log rec */
  2666. {
  2667. xlog_rec_header_t *rhead;
  2668. xfs_daddr_t blk_no, rblk_no;
  2669. xfs_daddr_t rhead_blk;
  2670. char *offset;
  2671. char *hbp, *dbp;
  2672. int error = 0, h_size, h_len;
  2673. int error2 = 0;
  2674. int bblks, split_bblks;
  2675. int hblks, split_hblks, wrapped_hblks;
  2676. int i;
  2677. struct hlist_head rhash[XLOG_RHASH_SIZE];
  2678. LIST_HEAD (buffer_list);
  2679. ASSERT(head_blk != tail_blk);
  2680. blk_no = rhead_blk = tail_blk;
  2681. for (i = 0; i < XLOG_RHASH_SIZE; i++)
  2682. INIT_HLIST_HEAD(&rhash[i]);
  2683. /*
  2684. * Read the header of the tail block and get the iclog buffer size from
  2685. * h_size. Use this to tell how many sectors make up the log header.
  2686. */
  2687. if (xfs_has_logv2(log->l_mp)) {
  2688. /*
  2689. * When using variable length iclogs, read first sector of
  2690. * iclog header and extract the header size from it. Get a
  2691. * new hbp that is the correct size.
  2692. */
  2693. hbp = xlog_alloc_buffer(log, 1);
  2694. if (!hbp)
  2695. return -ENOMEM;
  2696. error = xlog_bread(log, tail_blk, 1, hbp, &offset);
  2697. if (error)
  2698. goto bread_err1;
  2699. rhead = (xlog_rec_header_t *)offset;
  2700. /*
  2701. * xfsprogs has a bug where record length is based on lsunit but
  2702. * h_size (iclog size) is hardcoded to 32k. Now that we
  2703. * unconditionally CRC verify the unmount record, this means the
  2704. * log buffer can be too small for the record and cause an
  2705. * overrun.
  2706. *
  2707. * Detect this condition here. Use lsunit for the buffer size as
  2708. * long as this looks like the mkfs case. Otherwise, return an
  2709. * error to avoid a buffer overrun.
  2710. */
  2711. h_size = be32_to_cpu(rhead->h_size);
  2712. h_len = be32_to_cpu(rhead->h_len);
  2713. if (h_len > h_size && h_len <= log->l_mp->m_logbsize &&
  2714. rhead->h_num_logops == cpu_to_be32(1)) {
  2715. xfs_warn(log->l_mp,
  2716. "invalid iclog size (%d bytes), using lsunit (%d bytes)",
  2717. h_size, log->l_mp->m_logbsize);
  2718. h_size = log->l_mp->m_logbsize;
  2719. }
  2720. error = xlog_valid_rec_header(log, rhead, tail_blk, h_size);
  2721. if (error)
  2722. goto bread_err1;
  2723. hblks = xlog_logrec_hblks(log, rhead);
  2724. if (hblks != 1) {
  2725. kmem_free(hbp);
  2726. hbp = xlog_alloc_buffer(log, hblks);
  2727. }
  2728. } else {
  2729. ASSERT(log->l_sectBBsize == 1);
  2730. hblks = 1;
  2731. hbp = xlog_alloc_buffer(log, 1);
  2732. h_size = XLOG_BIG_RECORD_BSIZE;
  2733. }
  2734. if (!hbp)
  2735. return -ENOMEM;
  2736. dbp = xlog_alloc_buffer(log, BTOBB(h_size));
  2737. if (!dbp) {
  2738. kmem_free(hbp);
  2739. return -ENOMEM;
  2740. }
  2741. memset(rhash, 0, sizeof(rhash));
  2742. if (tail_blk > head_blk) {
  2743. /*
  2744. * Perform recovery around the end of the physical log.
  2745. * When the head is not on the same cycle number as the tail,
  2746. * we can't do a sequential recovery.
  2747. */
  2748. while (blk_no < log->l_logBBsize) {
  2749. /*
  2750. * Check for header wrapping around physical end-of-log
  2751. */
  2752. offset = hbp;
  2753. split_hblks = 0;
  2754. wrapped_hblks = 0;
  2755. if (blk_no + hblks <= log->l_logBBsize) {
  2756. /* Read header in one read */
  2757. error = xlog_bread(log, blk_no, hblks, hbp,
  2758. &offset);
  2759. if (error)
  2760. goto bread_err2;
  2761. } else {
  2762. /* This LR is split across physical log end */
  2763. if (blk_no != log->l_logBBsize) {
  2764. /* some data before physical log end */
  2765. ASSERT(blk_no <= INT_MAX);
  2766. split_hblks = log->l_logBBsize - (int)blk_no;
  2767. ASSERT(split_hblks > 0);
  2768. error = xlog_bread(log, blk_no,
  2769. split_hblks, hbp,
  2770. &offset);
  2771. if (error)
  2772. goto bread_err2;
  2773. }
  2774. /*
  2775. * Note: this black magic still works with
  2776. * large sector sizes (non-512) only because:
  2777. * - we increased the buffer size originally
  2778. * by 1 sector giving us enough extra space
  2779. * for the second read;
  2780. * - the log start is guaranteed to be sector
  2781. * aligned;
  2782. * - we read the log end (LR header start)
  2783. * _first_, then the log start (LR header end)
  2784. * - order is important.
  2785. */
  2786. wrapped_hblks = hblks - split_hblks;
  2787. error = xlog_bread_noalign(log, 0,
  2788. wrapped_hblks,
  2789. offset + BBTOB(split_hblks));
  2790. if (error)
  2791. goto bread_err2;
  2792. }
  2793. rhead = (xlog_rec_header_t *)offset;
  2794. error = xlog_valid_rec_header(log, rhead,
  2795. split_hblks ? blk_no : 0, h_size);
  2796. if (error)
  2797. goto bread_err2;
  2798. bblks = (int)BTOBB(be32_to_cpu(rhead->h_len));
  2799. blk_no += hblks;
  2800. /*
  2801. * Read the log record data in multiple reads if it
  2802. * wraps around the end of the log. Note that if the
  2803. * header already wrapped, blk_no could point past the
  2804. * end of the log. The record data is contiguous in
  2805. * that case.
  2806. */
  2807. if (blk_no + bblks <= log->l_logBBsize ||
  2808. blk_no >= log->l_logBBsize) {
  2809. rblk_no = xlog_wrap_logbno(log, blk_no);
  2810. error = xlog_bread(log, rblk_no, bblks, dbp,
  2811. &offset);
  2812. if (error)
  2813. goto bread_err2;
  2814. } else {
  2815. /* This log record is split across the
  2816. * physical end of log */
  2817. offset = dbp;
  2818. split_bblks = 0;
  2819. if (blk_no != log->l_logBBsize) {
  2820. /* some data is before the physical
  2821. * end of log */
  2822. ASSERT(!wrapped_hblks);
  2823. ASSERT(blk_no <= INT_MAX);
  2824. split_bblks =
  2825. log->l_logBBsize - (int)blk_no;
  2826. ASSERT(split_bblks > 0);
  2827. error = xlog_bread(log, blk_no,
  2828. split_bblks, dbp,
  2829. &offset);
  2830. if (error)
  2831. goto bread_err2;
  2832. }
  2833. /*
  2834. * Note: this black magic still works with
  2835. * large sector sizes (non-512) only because:
  2836. * - we increased the buffer size originally
  2837. * by 1 sector giving us enough extra space
  2838. * for the second read;
  2839. * - the log start is guaranteed to be sector
  2840. * aligned;
  2841. * - we read the log end (LR header start)
  2842. * _first_, then the log start (LR header end)
  2843. * - order is important.
  2844. */
  2845. error = xlog_bread_noalign(log, 0,
  2846. bblks - split_bblks,
  2847. offset + BBTOB(split_bblks));
  2848. if (error)
  2849. goto bread_err2;
  2850. }
  2851. error = xlog_recover_process(log, rhash, rhead, offset,
  2852. pass, &buffer_list);
  2853. if (error)
  2854. goto bread_err2;
  2855. blk_no += bblks;
  2856. rhead_blk = blk_no;
  2857. }
  2858. ASSERT(blk_no >= log->l_logBBsize);
  2859. blk_no -= log->l_logBBsize;
  2860. rhead_blk = blk_no;
  2861. }
  2862. /* read first part of physical log */
  2863. while (blk_no < head_blk) {
  2864. error = xlog_bread(log, blk_no, hblks, hbp, &offset);
  2865. if (error)
  2866. goto bread_err2;
  2867. rhead = (xlog_rec_header_t *)offset;
  2868. error = xlog_valid_rec_header(log, rhead, blk_no, h_size);
  2869. if (error)
  2870. goto bread_err2;
  2871. /* blocks in data section */
  2872. bblks = (int)BTOBB(be32_to_cpu(rhead->h_len));
  2873. error = xlog_bread(log, blk_no+hblks, bblks, dbp,
  2874. &offset);
  2875. if (error)
  2876. goto bread_err2;
  2877. error = xlog_recover_process(log, rhash, rhead, offset, pass,
  2878. &buffer_list);
  2879. if (error)
  2880. goto bread_err2;
  2881. blk_no += bblks + hblks;
  2882. rhead_blk = blk_no;
  2883. }
  2884. bread_err2:
  2885. kmem_free(dbp);
  2886. bread_err1:
  2887. kmem_free(hbp);
  2888. /*
  2889. * Submit buffers that have been added from the last record processed,
  2890. * regardless of error status.
  2891. */
  2892. if (!list_empty(&buffer_list))
  2893. error2 = xfs_buf_delwri_submit(&buffer_list);
  2894. if (error && first_bad)
  2895. *first_bad = rhead_blk;
  2896. /*
  2897. * Transactions are freed at commit time but transactions without commit
  2898. * records on disk are never committed. Free any that may be left in the
  2899. * hash table.
  2900. */
  2901. for (i = 0; i < XLOG_RHASH_SIZE; i++) {
  2902. struct hlist_node *tmp;
  2903. struct xlog_recover *trans;
  2904. hlist_for_each_entry_safe(trans, tmp, &rhash[i], r_list)
  2905. xlog_recover_free_trans(trans);
  2906. }
  2907. return error ? error : error2;
  2908. }
  2909. /*
  2910. * Do the recovery of the log. We actually do this in two phases.
  2911. * The two passes are necessary in order to implement the function
  2912. * of cancelling a record written into the log. The first pass
  2913. * determines those things which have been cancelled, and the
  2914. * second pass replays log items normally except for those which
  2915. * have been cancelled. The handling of the replay and cancellations
  2916. * takes place in the log item type specific routines.
  2917. *
  2918. * The table of items which have cancel records in the log is allocated
  2919. * and freed at this level, since only here do we know when all of
  2920. * the log recovery has been completed.
  2921. */
  2922. STATIC int
  2923. xlog_do_log_recovery(
  2924. struct xlog *log,
  2925. xfs_daddr_t head_blk,
  2926. xfs_daddr_t tail_blk)
  2927. {
  2928. int error;
  2929. ASSERT(head_blk != tail_blk);
  2930. /*
  2931. * First do a pass to find all of the cancelled buf log items.
  2932. * Store them in the buf_cancel_table for use in the second pass.
  2933. */
  2934. error = xlog_alloc_buf_cancel_table(log);
  2935. if (error)
  2936. return error;
  2937. error = xlog_do_recovery_pass(log, head_blk, tail_blk,
  2938. XLOG_RECOVER_PASS1, NULL);
  2939. if (error != 0)
  2940. goto out_cancel;
  2941. /*
  2942. * Then do a second pass to actually recover the items in the log.
  2943. * When it is complete free the table of buf cancel items.
  2944. */
  2945. error = xlog_do_recovery_pass(log, head_blk, tail_blk,
  2946. XLOG_RECOVER_PASS2, NULL);
  2947. if (!error)
  2948. xlog_check_buf_cancel_table(log);
  2949. out_cancel:
  2950. xlog_free_buf_cancel_table(log);
  2951. return error;
  2952. }
  2953. /*
  2954. * Do the actual recovery
  2955. */
  2956. STATIC int
  2957. xlog_do_recover(
  2958. struct xlog *log,
  2959. xfs_daddr_t head_blk,
  2960. xfs_daddr_t tail_blk)
  2961. {
  2962. struct xfs_mount *mp = log->l_mp;
  2963. struct xfs_buf *bp = mp->m_sb_bp;
  2964. struct xfs_sb *sbp = &mp->m_sb;
  2965. int error;
  2966. trace_xfs_log_recover(log, head_blk, tail_blk);
  2967. /*
  2968. * First replay the images in the log.
  2969. */
  2970. error = xlog_do_log_recovery(log, head_blk, tail_blk);
  2971. if (error)
  2972. return error;
  2973. if (xlog_is_shutdown(log))
  2974. return -EIO;
  2975. /*
  2976. * We now update the tail_lsn since much of the recovery has completed
  2977. * and there may be space available to use. If there were no extent
  2978. * or iunlinks, we can free up the entire log and set the tail_lsn to
  2979. * be the last_sync_lsn. This was set in xlog_find_tail to be the
  2980. * lsn of the last known good LR on disk. If there are extent frees
  2981. * or iunlinks they will have some entries in the AIL; so we look at
  2982. * the AIL to determine how to set the tail_lsn.
  2983. */
  2984. xlog_assign_tail_lsn(mp);
  2985. /*
  2986. * Now that we've finished replaying all buffer and inode updates,
  2987. * re-read the superblock and reverify it.
  2988. */
  2989. xfs_buf_lock(bp);
  2990. xfs_buf_hold(bp);
  2991. error = _xfs_buf_read(bp, XBF_READ);
  2992. if (error) {
  2993. if (!xlog_is_shutdown(log)) {
  2994. xfs_buf_ioerror_alert(bp, __this_address);
  2995. ASSERT(0);
  2996. }
  2997. xfs_buf_relse(bp);
  2998. return error;
  2999. }
  3000. /* Convert superblock from on-disk format */
  3001. xfs_sb_from_disk(sbp, bp->b_addr);
  3002. xfs_buf_relse(bp);
  3003. /* re-initialise in-core superblock and geometry structures */
  3004. mp->m_features |= xfs_sb_version_to_features(sbp);
  3005. xfs_reinit_percpu_counters(mp);
  3006. error = xfs_initialize_perag(mp, sbp->sb_agcount, sbp->sb_dblocks,
  3007. &mp->m_maxagi);
  3008. if (error) {
  3009. xfs_warn(mp, "Failed post-recovery per-ag init: %d", error);
  3010. return error;
  3011. }
  3012. mp->m_alloc_set_aside = xfs_alloc_set_aside(mp);
  3013. /* Normal transactions can now occur */
  3014. clear_bit(XLOG_ACTIVE_RECOVERY, &log->l_opstate);
  3015. return 0;
  3016. }
  3017. /*
  3018. * Perform recovery and re-initialize some log variables in xlog_find_tail.
  3019. *
  3020. * Return error or zero.
  3021. */
  3022. int
  3023. xlog_recover(
  3024. struct xlog *log)
  3025. {
  3026. xfs_daddr_t head_blk, tail_blk;
  3027. int error;
  3028. /* find the tail of the log */
  3029. error = xlog_find_tail(log, &head_blk, &tail_blk);
  3030. if (error)
  3031. return error;
  3032. /*
  3033. * The superblock was read before the log was available and thus the LSN
  3034. * could not be verified. Check the superblock LSN against the current
  3035. * LSN now that it's known.
  3036. */
  3037. if (xfs_has_crc(log->l_mp) &&
  3038. !xfs_log_check_lsn(log->l_mp, log->l_mp->m_sb.sb_lsn))
  3039. return -EINVAL;
  3040. if (tail_blk != head_blk) {
  3041. /* There used to be a comment here:
  3042. *
  3043. * disallow recovery on read-only mounts. note -- mount
  3044. * checks for ENOSPC and turns it into an intelligent
  3045. * error message.
  3046. * ...but this is no longer true. Now, unless you specify
  3047. * NORECOVERY (in which case this function would never be
  3048. * called), we just go ahead and recover. We do this all
  3049. * under the vfs layer, so we can get away with it unless
  3050. * the device itself is read-only, in which case we fail.
  3051. */
  3052. if ((error = xfs_dev_is_read_only(log->l_mp, "recovery"))) {
  3053. return error;
  3054. }
  3055. /*
  3056. * Version 5 superblock log feature mask validation. We know the
  3057. * log is dirty so check if there are any unknown log features
  3058. * in what we need to recover. If there are unknown features
  3059. * (e.g. unsupported transactions, then simply reject the
  3060. * attempt at recovery before touching anything.
  3061. */
  3062. if (xfs_sb_is_v5(&log->l_mp->m_sb) &&
  3063. xfs_sb_has_incompat_log_feature(&log->l_mp->m_sb,
  3064. XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN)) {
  3065. xfs_warn(log->l_mp,
  3066. "Superblock has unknown incompatible log features (0x%x) enabled.",
  3067. (log->l_mp->m_sb.sb_features_log_incompat &
  3068. XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN));
  3069. xfs_warn(log->l_mp,
  3070. "The log can not be fully and/or safely recovered by this kernel.");
  3071. xfs_warn(log->l_mp,
  3072. "Please recover the log on a kernel that supports the unknown features.");
  3073. return -EINVAL;
  3074. }
  3075. /*
  3076. * Delay log recovery if the debug hook is set. This is debug
  3077. * instrumentation to coordinate simulation of I/O failures with
  3078. * log recovery.
  3079. */
  3080. if (xfs_globals.log_recovery_delay) {
  3081. xfs_notice(log->l_mp,
  3082. "Delaying log recovery for %d seconds.",
  3083. xfs_globals.log_recovery_delay);
  3084. msleep(xfs_globals.log_recovery_delay * 1000);
  3085. }
  3086. xfs_notice(log->l_mp, "Starting recovery (logdev: %s)",
  3087. log->l_mp->m_logname ? log->l_mp->m_logname
  3088. : "internal");
  3089. error = xlog_do_recover(log, head_blk, tail_blk);
  3090. set_bit(XLOG_RECOVERY_NEEDED, &log->l_opstate);
  3091. }
  3092. return error;
  3093. }
  3094. /*
  3095. * In the first part of recovery we replay inodes and buffers and build up the
  3096. * list of intents which need to be processed. Here we process the intents and
  3097. * clean up the on disk unlinked inode lists. This is separated from the first
  3098. * part of recovery so that the root and real-time bitmap inodes can be read in
  3099. * from disk in between the two stages. This is necessary so that we can free
  3100. * space in the real-time portion of the file system.
  3101. */
  3102. int
  3103. xlog_recover_finish(
  3104. struct xlog *log)
  3105. {
  3106. int error;
  3107. error = xlog_recover_process_intents(log);
  3108. if (error) {
  3109. /*
  3110. * Cancel all the unprocessed intent items now so that we don't
  3111. * leave them pinned in the AIL. This can cause the AIL to
  3112. * livelock on the pinned item if anyone tries to push the AIL
  3113. * (inode reclaim does this) before we get around to
  3114. * xfs_log_mount_cancel.
  3115. */
  3116. xlog_recover_cancel_intents(log);
  3117. xfs_alert(log->l_mp, "Failed to recover intents");
  3118. xlog_force_shutdown(log, SHUTDOWN_LOG_IO_ERROR);
  3119. return error;
  3120. }
  3121. /*
  3122. * Sync the log to get all the intents out of the AIL. This isn't
  3123. * absolutely necessary, but it helps in case the unlink transactions
  3124. * would have problems pushing the intents out of the way.
  3125. */
  3126. xfs_log_force(log->l_mp, XFS_LOG_SYNC);
  3127. /*
  3128. * Now that we've recovered the log and all the intents, we can clear
  3129. * the log incompat feature bits in the superblock because there's no
  3130. * longer anything to protect. We rely on the AIL push to write out the
  3131. * updated superblock after everything else.
  3132. */
  3133. if (xfs_clear_incompat_log_features(log->l_mp)) {
  3134. error = xfs_sync_sb(log->l_mp, false);
  3135. if (error < 0) {
  3136. xfs_alert(log->l_mp,
  3137. "Failed to clear log incompat features on recovery");
  3138. return error;
  3139. }
  3140. }
  3141. xlog_recover_process_iunlinks(log);
  3142. /*
  3143. * Recover any CoW staging blocks that are still referenced by the
  3144. * ondisk refcount metadata. During mount there cannot be any live
  3145. * staging extents as we have not permitted any user modifications.
  3146. * Therefore, it is safe to free them all right now, even on a
  3147. * read-only mount.
  3148. */
  3149. error = xfs_reflink_recover_cow(log->l_mp);
  3150. if (error) {
  3151. xfs_alert(log->l_mp,
  3152. "Failed to recover leftover CoW staging extents, err %d.",
  3153. error);
  3154. /*
  3155. * If we get an error here, make sure the log is shut down
  3156. * but return zero so that any log items committed since the
  3157. * end of intents processing can be pushed through the CIL
  3158. * and AIL.
  3159. */
  3160. xlog_force_shutdown(log, SHUTDOWN_LOG_IO_ERROR);
  3161. }
  3162. return 0;
  3163. }
  3164. void
  3165. xlog_recover_cancel(
  3166. struct xlog *log)
  3167. {
  3168. if (xlog_recovery_needed(log))
  3169. xlog_recover_cancel_intents(log);
  3170. }