innodb-utils 5.0.0

InnoDB file analysis toolkit
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
use std::io::Write;
use std::path::Path;

use rayon::prelude::*;
use serde::Serialize;

use crate::cli::{create_progress_bar, wprintln};
use crate::innodb::checksum::validate_checksum;
use crate::innodb::corruption::classify_corruption;
use crate::innodb::page::FilHeader;
use crate::util::fs::find_tablespace_files;
use crate::IdbError;

/// Options for the `inno find` subcommand.
pub struct FindOptions {
    /// MySQL data directory path to search.
    pub datadir: String,
    /// Page number to search for across all tablespace files.
    pub page: Option<u64>,
    /// Optional checksum filter — only match pages with this stored checksum.
    pub checksum: Option<u32>,
    /// Optional space ID filter — only match pages in this tablespace.
    pub space_id: Option<u32>,
    /// Scan for pages with checksum mismatches.
    pub corrupt: bool,
    /// Stop searching after the first match.
    pub first: bool,
    /// Emit output as JSON.
    pub json: bool,
    /// Override the auto-detected page size.
    pub page_size: Option<u32>,
    /// Number of threads for parallel processing (0 = auto-detect).
    pub threads: usize,
    /// Use memory-mapped I/O for file access.
    pub mmap: bool,
    /// Maximum directory recursion depth (None = default 2, Some(0) = unlimited).
    pub depth: Option<u32>,
}

// -----------------------------------------------------------------------
// Page-number search structs
// -----------------------------------------------------------------------

#[derive(Serialize)]
struct FindResultJson {
    datadir: String,
    target_page: u64,
    matches: Vec<FindMatchJson>,
    files_searched: usize,
}

#[derive(Serialize, Clone)]
struct FindMatchJson {
    file: String,
    page_number: u64,
    checksum: u32,
    space_id: u32,
}

// -----------------------------------------------------------------------
// Corrupt search structs
// -----------------------------------------------------------------------

#[derive(Serialize)]
struct FindCorruptResultJson {
    datadir: String,
    corrupt_pages: Vec<FindCorruptMatchJson>,
    files_searched: usize,
    total_corrupt: usize,
}

#[derive(Serialize, Clone)]
struct FindCorruptMatchJson {
    file: String,
    page_number: u64,
    stored_checksum: u32,
    calculated_checksum: u32,
    algorithm: String,
    corruption_pattern: String,
}

// -----------------------------------------------------------------------
// Page-number search implementation
// -----------------------------------------------------------------------

/// Search a single tablespace file for pages matching the target page number.
#[allow(clippy::too_many_arguments)]
fn search_file(
    ibd_path: &Path,
    datadir: &Path,
    target_page: u64,
    checksum_filter: Option<u32>,
    space_id_filter: Option<u32>,
    page_size_override: Option<u32>,
    first: bool,
    use_mmap: bool,
) -> (Vec<FindMatchJson>, bool) {
    let display_path = ibd_path.strip_prefix(datadir).unwrap_or(ibd_path);

    let path_str = ibd_path.to_string_lossy();
    let ts_result = crate::cli::open_tablespace(&path_str, page_size_override, use_mmap);
    let mut ts = match ts_result {
        Ok(t) => t,
        Err(_) => return (Vec::new(), false),
    };

    let all_data = match ts.read_all_pages() {
        Ok(d) => d,
        Err(_) => return (Vec::new(), true),
    };

    let page_size = ts.page_size() as usize;
    let page_count = ts.page_count();

    let file_matches: Vec<FindMatchJson> = (0..page_count)
        .into_par_iter()
        .filter_map(|page_num| {
            let offset = page_num as usize * page_size;
            if offset + page_size > all_data.len() {
                return None;
            }
            let page_data = &all_data[offset..offset + page_size];

            let header = FilHeader::parse(page_data)?;

            if header.page_number as u64 != target_page {
                return None;
            }

            if let Some(expected_csum) = checksum_filter {
                if header.checksum != expected_csum {
                    return None;
                }
            }

            if let Some(expected_sid) = space_id_filter {
                if header.space_id != expected_sid {
                    return None;
                }
            }

            Some(FindMatchJson {
                file: display_path.display().to_string(),
                page_number: header.page_number as u64,
                checksum: header.checksum,
                space_id: header.space_id,
            })
        })
        .collect();

    let matches = if first {
        file_matches.into_iter().take(1).collect()
    } else {
        file_matches
    };
    (matches, true)
}

fn execute_find_page(opts: &FindOptions, writer: &mut dyn Write) -> Result<(), IdbError> {
    let target_page = opts.page.unwrap();
    let datadir = Path::new(&opts.datadir);

    let ibd_files = find_tablespace_files(datadir, &["ibd"], opts.depth)?;

    if ibd_files.is_empty() {
        if opts.json {
            let result = FindResultJson {
                datadir: opts.datadir.clone(),
                target_page,
                matches: Vec::new(),
                files_searched: 0,
            };
            let json = serde_json::to_string_pretty(&result)
                .map_err(|e| IdbError::Parse(format!("JSON serialization error: {}", e)))?;
            wprintln!(writer, "{}", json)?;
        } else {
            wprintln!(writer, "No .ibd files found in {}", opts.datadir)?;
        }
        return Ok(());
    }

    let pb = if !opts.json {
        Some(create_progress_bar(ibd_files.len() as u64, "files"))
    } else {
        None
    };

    let checksum_filter = opts.checksum;
    let space_id_filter = opts.space_id;
    let page_size_override = opts.page_size;
    let first = opts.first;
    let use_mmap = opts.mmap;

    let all_results: Vec<(Vec<FindMatchJson>, bool)> = ibd_files
        .par_iter()
        .map(|ibd_path| {
            let result = search_file(
                ibd_path,
                datadir,
                target_page,
                checksum_filter,
                space_id_filter,
                page_size_override,
                first,
                use_mmap,
            );
            if let Some(ref pb) = pb {
                pb.inc(1);
            }
            result
        })
        .collect();

    if let Some(ref pb) = pb {
        pb.finish_and_clear();
    }

    let mut matches: Vec<FindMatchJson> = Vec::new();
    let files_searched: usize = all_results.iter().filter(|(_, opened)| *opened).count();

    for (file_matches, _opened) in &all_results {
        for m in file_matches {
            if !opts.json {
                wprintln!(
                    writer,
                    "Found page {} in {} (checksum: {}, space_id: {})",
                    target_page,
                    m.file,
                    m.checksum,
                    m.space_id
                )?;
            }
            matches.push(m.clone());
            if opts.first {
                break;
            }
        }
        if opts.first && !matches.is_empty() {
            break;
        }
    }

    if opts.json {
        let result = FindResultJson {
            datadir: opts.datadir.clone(),
            target_page,
            matches,
            files_searched,
        };
        let json = serde_json::to_string_pretty(&result)
            .map_err(|e| IdbError::Parse(format!("JSON serialization error: {}", e)))?;
        wprintln!(writer, "{}", json)?;
    } else if matches.is_empty() {
        wprintln!(writer, "Page {} not found in any .ibd file.", target_page)?;
    } else {
        wprintln!(writer)?;
        wprintln!(
            writer,
            "Found {} match(es) in {} file(s) searched.",
            matches.len(),
            files_searched
        )?;
    }

    Ok(())
}

// -----------------------------------------------------------------------
// Corrupt search implementation
// -----------------------------------------------------------------------

/// Search a single tablespace file for pages with invalid checksums.
fn search_file_corrupt(
    ibd_path: &Path,
    datadir: &Path,
    space_id_filter: Option<u32>,
    page_size_override: Option<u32>,
    first: bool,
    use_mmap: bool,
) -> (Vec<FindCorruptMatchJson>, bool) {
    let display_path = ibd_path.strip_prefix(datadir).unwrap_or(ibd_path);

    let path_str = ibd_path.to_string_lossy();
    let ts_result = crate::cli::open_tablespace(&path_str, page_size_override, use_mmap);
    let mut ts = match ts_result {
        Ok(t) => t,
        Err(_) => return (Vec::new(), false),
    };

    let page_size = ts.page_size();
    let page_count = ts.page_count();
    let vendor_info = ts.vendor_info().clone();

    let all_data = match ts.read_all_pages() {
        Ok(d) => d,
        Err(_) => return (Vec::new(), true),
    };

    let ps = page_size as usize;
    let file_str = display_path.display().to_string();

    let file_matches: Vec<FindCorruptMatchJson> = (0..page_count)
        .into_par_iter()
        .filter_map(|page_num| {
            let offset = page_num as usize * ps;
            if offset + ps > all_data.len() {
                return None;
            }
            let page_data = &all_data[offset..offset + ps];

            // Skip empty pages
            if page_data.iter().all(|&b| b == 0) {
                return None;
            }

            // Apply space_id filter if specified
            if let Some(expected_sid) = space_id_filter {
                if let Some(header) = FilHeader::parse(page_data) {
                    if header.space_id != expected_sid {
                        return None;
                    }
                }
            }

            let csum = validate_checksum(page_data, page_size, Some(&vendor_info));
            if csum.valid {
                return None;
            }

            let pattern = classify_corruption(page_data, page_size);

            Some(FindCorruptMatchJson {
                file: file_str.clone(),
                page_number: page_num,
                stored_checksum: csum.stored_checksum,
                calculated_checksum: csum.calculated_checksum,
                algorithm: format!("{:?}", csum.algorithm),
                corruption_pattern: pattern.name().to_string(),
            })
        })
        .collect();

    let matches = if first {
        file_matches.into_iter().take(1).collect()
    } else {
        file_matches
    };
    (matches, true)
}

fn execute_find_corrupt(opts: &FindOptions, writer: &mut dyn Write) -> Result<(), IdbError> {
    let datadir = Path::new(&opts.datadir);

    let ibd_files = find_tablespace_files(datadir, &["ibd"], opts.depth)?;

    if ibd_files.is_empty() {
        if opts.json {
            let result = FindCorruptResultJson {
                datadir: opts.datadir.clone(),
                corrupt_pages: Vec::new(),
                files_searched: 0,
                total_corrupt: 0,
            };
            let json = serde_json::to_string_pretty(&result)
                .map_err(|e| IdbError::Parse(format!("JSON serialization error: {}", e)))?;
            wprintln!(writer, "{}", json)?;
        } else {
            wprintln!(writer, "No .ibd files found in {}", opts.datadir)?;
        }
        return Ok(());
    }

    let pb = if !opts.json {
        Some(create_progress_bar(ibd_files.len() as u64, "files"))
    } else {
        None
    };

    let space_id_filter = opts.space_id;
    let page_size_override = opts.page_size;
    let first = opts.first;
    let use_mmap = opts.mmap;

    let all_results: Vec<(Vec<FindCorruptMatchJson>, bool)> = ibd_files
        .par_iter()
        .map(|ibd_path| {
            let result = search_file_corrupt(
                ibd_path,
                datadir,
                space_id_filter,
                page_size_override,
                first,
                use_mmap,
            );
            if let Some(ref pb) = pb {
                pb.inc(1);
            }
            result
        })
        .collect();

    if let Some(ref pb) = pb {
        pb.finish_and_clear();
    }

    let mut corrupt_pages: Vec<FindCorruptMatchJson> = Vec::new();
    let files_searched: usize = all_results.iter().filter(|(_, opened)| *opened).count();

    for (file_matches, _opened) in &all_results {
        for m in file_matches {
            if !opts.json {
                wprintln!(
                    writer,
                    "Corrupt page {} in {} (stored: 0x{:08x}, calculated: 0x{:08x}, algo: {}, pattern: {})",
                    m.page_number,
                    m.file,
                    m.stored_checksum,
                    m.calculated_checksum,
                    m.algorithm,
                    m.corruption_pattern
                )?;
            }
            corrupt_pages.push(m.clone());
            if opts.first {
                break;
            }
        }
        if opts.first && !corrupt_pages.is_empty() {
            break;
        }
    }

    if opts.json {
        let total_corrupt = corrupt_pages.len();
        let result = FindCorruptResultJson {
            datadir: opts.datadir.clone(),
            corrupt_pages,
            files_searched,
            total_corrupt,
        };
        let json = serde_json::to_string_pretty(&result)
            .map_err(|e| IdbError::Parse(format!("JSON serialization error: {}", e)))?;
        wprintln!(writer, "{}", json)?;
    } else if corrupt_pages.is_empty() {
        wprintln!(
            writer,
            "No corrupt pages found in {} file(s) searched.",
            files_searched
        )?;
    } else {
        wprintln!(writer)?;
        wprintln!(
            writer,
            "Found {} corrupt page(s) in {} file(s) searched.",
            corrupt_pages.len(),
            files_searched
        )?;
    }

    Ok(())
}

// -----------------------------------------------------------------------
// Entry point
// -----------------------------------------------------------------------

/// Search a MySQL data directory for pages matching a given page number,
/// or scan for pages with checksum mismatches (`--corrupt`).
pub fn execute(opts: &FindOptions, writer: &mut dyn Write) -> Result<(), IdbError> {
    // Validate mutually exclusive modes
    if opts.corrupt && opts.page.is_some() {
        return Err(IdbError::Argument(
            "--corrupt and --page are mutually exclusive".to_string(),
        ));
    }
    if !opts.corrupt && opts.page.is_none() {
        return Err(IdbError::Argument(
            "Either --page or --corrupt must be specified".to_string(),
        ));
    }
    if opts.corrupt && opts.checksum.is_some() {
        return Err(IdbError::Argument(
            "--checksum is not compatible with --corrupt".to_string(),
        ));
    }

    let datadir = Path::new(&opts.datadir);
    if !datadir.is_dir() {
        return Err(IdbError::Argument(format!(
            "Data directory does not exist: {}",
            opts.datadir
        )));
    }

    if opts.corrupt {
        execute_find_corrupt(opts, writer)
    } else {
        execute_find_page(opts, writer)
    }
}