Revision 4534ff54

b/block.c
1222 1222
 * free of errors) or -errno when an internal error occurred. The results of the
1223 1223
 * check are stored in res.
1224 1224
 */
1225
int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res)
1225
int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res, BdrvCheckMode fix)
1226 1226
{
1227 1227
    if (bs->drv->bdrv_check == NULL) {
1228 1228
        return -ENOTSUP;
1229 1229
    }
1230 1230

  
1231 1231
    memset(res, 0, sizeof(*res));
1232
    return bs->drv->bdrv_check(bs, res);
1232
    return bs->drv->bdrv_check(bs, res, fix);
1233 1233
}
1234 1234

  
1235 1235
#define COMMIT_BUF_SECTORS 2048
b/block.h
190 190
    BlockFragInfo bfi;
191 191
} BdrvCheckResult;
192 192

  
193
int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res);
193
typedef enum {
194
    BDRV_FIX_LEAKS    = 1,
195
    BDRV_FIX_ERRORS   = 2,
196
} BdrvCheckMode;
197

  
198
int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res, BdrvCheckMode fix);
194 199

  
195 200
/* async block I/O */
196 201
typedef void BlockDriverDirtyHandler(BlockDriverState *bs, int64_t sector,
b/block/qcow2.c
1470 1470
}
1471 1471

  
1472 1472

  
1473
static int qcow2_check(BlockDriverState *bs, BdrvCheckResult *result)
1473
static int qcow2_check(BlockDriverState *bs, BdrvCheckResult *result,
1474
                       BdrvCheckMode fix)
1474 1475
{
1476
    if (fix) {
1477
        return -ENOTSUP;
1478
    }
1479

  
1475 1480
    return qcow2_check_refcounts(bs, result);
1476 1481
}
1477 1482

  
b/block/qed.c
1517 1517
    bdrv_qed_open(bs, bs->open_flags);
1518 1518
}
1519 1519

  
1520
static int bdrv_qed_check(BlockDriverState *bs, BdrvCheckResult *result)
1520
static int bdrv_qed_check(BlockDriverState *bs, BdrvCheckResult *result,
1521
                          BdrvCheckMode fix)
1521 1522
{
1522 1523
    BDRVQEDState *s = bs->opaque;
1523 1524

  
1524
    return qed_check(s, result, false);
1525
    return qed_check(s, result, !!fix);
1525 1526
}
1526 1527

  
1527 1528
static QEMUOptionParameter qed_create_options[] = {
b/block/vdi.c
277 277
}
278 278
#endif
279 279

  
280
static int vdi_check(BlockDriverState *bs, BdrvCheckResult *res)
280
static int vdi_check(BlockDriverState *bs, BdrvCheckResult *res,
281
                     BdrvCheckMode fix)
281 282
{
282 283
    /* TODO: additional checks possible. */
283 284
    BDRVVdiState *s = (BDRVVdiState *)bs->opaque;
......
286 287
    uint32_t *bmap;
287 288
    logout("\n");
288 289

  
290
    if (fix) {
291
        return -ENOTSUP;
292
    }
293

  
289 294
    bmap = g_malloc(s->header.blocks_in_image * sizeof(uint32_t));
290 295
    memset(bmap, 0xff, s->header.blocks_in_image * sizeof(uint32_t));
291 296

  
b/block_int.h
241 241
     * Returns 0 for completed check, -errno for internal errors.
242 242
     * The check results are stored in result.
243 243
     */
244
    int (*bdrv_check)(BlockDriverState* bs, BdrvCheckResult *result);
244
    int (*bdrv_check)(BlockDriverState* bs, BdrvCheckResult *result,
245
        BdrvCheckMode fix);
245 246

  
246 247
    void (*bdrv_debug_event)(BlockDriverState *bs, BlkDebugEvent event);
247 248

  
b/qemu-img-cmds.hx
10 10
ETEXI
11 11

  
12 12
DEF("check", img_check,
13
    "check [-f fmt] filename")
13
    "check [-f fmt] [-r [leaks | all]] filename")
14 14
STEXI
15
@item check [-f @var{fmt}] @var{filename}
15
@item check [-f @var{fmt}] [-r [leaks | all]] @var{filename}
16 16
ETEXI
17 17

  
18 18
DEF("create", img_create,
b/qemu-img.c
85 85
           "  '-S' indicates the consecutive number of bytes that must contain only zeros\n"
86 86
           "       for qemu-img to create a sparse image during conversion\n"
87 87
           "\n"
88
           "Parameters to check subcommand:\n"
89
           "  '-r' tries to repair any inconsistencies that are found during the check.\n"
90
           "       '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
91
           "       kinds of errors, with a higher risk of choosing the wrong fix or\n"
92
           "       hiding corruption that has already occured.\n"
93
           "\n"
88 94
           "Parameters to snapshot subcommand:\n"
89 95
           "  'snapshot' is the name of the snapshot to create, apply or delete\n"
90 96
           "  '-a' applies a snapshot (revert disk to saved state)\n"
......
372 378
    const char *filename, *fmt;
373 379
    BlockDriverState *bs;
374 380
    BdrvCheckResult result;
381
    int fix = 0;
382
    int flags = BDRV_O_FLAGS;
375 383

  
376 384
    fmt = NULL;
377 385
    for(;;) {
378
        c = getopt(argc, argv, "f:h");
386
        c = getopt(argc, argv, "f:hr:");
379 387
        if (c == -1) {
380 388
            break;
381 389
        }
......
387 395
        case 'f':
388 396
            fmt = optarg;
389 397
            break;
398
        case 'r':
399
            flags |= BDRV_O_RDWR;
400

  
401
            if (!strcmp(optarg, "leaks")) {
402
                fix = BDRV_FIX_LEAKS;
403
            } else if (!strcmp(optarg, "all")) {
404
                fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
405
            } else {
406
                help();
407
            }
408
            break;
390 409
        }
391 410
    }
392 411
    if (optind >= argc) {
......
394 413
    }
395 414
    filename = argv[optind++];
396 415

  
397
    bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS);
416
    bs = bdrv_new_open(filename, fmt, flags);
398 417
    if (!bs) {
399 418
        return 1;
400 419
    }
401
    ret = bdrv_check(bs, &result);
420
    ret = bdrv_check(bs, &result, fix);
402 421

  
403 422
    if (ret == -ENOTSUP) {
404 423
        error_report("This image format does not support checks");
b/qemu-img.texi
70 70
Command description:
71 71

  
72 72
@table @option
73
@item check [-f @var{fmt}] @var{filename}
73
@item check [-f @var{fmt}] [-r [leaks | all]] @var{filename}
74 74

  
75 75
Perform a consistency check on the disk image @var{filename}.
76 76

  
77
If @code{-r} is specified, qemu-img tries to repair any inconsistencies found
78
during the check. @code{-r leaks} repairs only cluster leaks, whereas
79
@code{-r all} fixes all kinds of errors, with a higher risk of choosing the
80
wrong fix or hiding corruption that has already occured.
81

  
77 82
Only the formats @code{qcow2}, @code{qed} and @code{vdi} support
78 83
consistency checks.
79 84

  

Also available in: Unified diff