storage.c 29.2 KB
Newer Older
L
LiFeng 已提交
1 2
/******************************************************************************
 * Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved.
L
lifeng68 已提交
3 4 5 6
 * iSulad licensed under the Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *     http://license.coscl.org.cn/MulanPSL2
L
LiFeng 已提交
7 8 9
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
 * PURPOSE.
L
lifeng68 已提交
10
 * See the Mulan PSL v2 for more details.
L
LiFeng 已提交
11 12 13 14 15 16 17 18
 * Author: lifeng
 * Create: 2020-04-01
 * Description: provide storage functions
 ******************************************************************************/
#include "storage.h"

#include <stdlib.h>
#include <string.h>
L
LiFeng 已提交
19
#include <unistd.h>
20 21 22 23 24
#include <fcntl.h>
#include <isula_libutils/imagetool_fs_info.h>
#include <isula_libutils/imagetool_images_list.h>
#include <isula_libutils/storage_rootfs.h>
#include <pthread.h>
L
LiFeng 已提交
25

L
lifeng68 已提交
26
#include "io_wrapper.h"
L
LiFeng 已提交
27
#include "utils.h"
L
lifeng68 已提交
28
#include "utils_images.h"
L
lifeng68 已提交
29
#include "isula_libutils/log.h"
L
LiFeng 已提交
30 31
#include "layer_store.h"
#include "image_store.h"
32
#include "rootfs_store.h"
L
lifeng68 已提交
33
#include "err_msg.h"
34 35 36 37 38
#include "constants.h"
#include "utils_array.h"
#include "utils_file.h"
#include "utils_string.h"
#include "utils_verify.h"
L
LiFeng 已提交
39

L
LiFeng 已提交
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
static pthread_rwlock_t g_storage_rwlock;

static inline bool storage_lock(pthread_rwlock_t *store_lock, bool writable)
{
    int nret = 0;

    if (writable) {
        nret = pthread_rwlock_wrlock(store_lock);
    } else {
        nret = pthread_rwlock_rdlock(store_lock);
    }
    if (nret != 0) {
        ERROR("Lock memory store failed: %s", strerror(nret));
        return false;
    }

    return true;
}

static inline void storage_unlock(pthread_rwlock_t *store_lock)
{
    int nret = 0;

    nret = pthread_rwlock_unlock(store_lock);
    if (nret != 0) {
        FATAL("Unlock memory store failed: %s", strerror(nret));
    }
}

L
LiFeng 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
static ssize_t layer_archive_io_read(void *context, void *buf, size_t buf_len)
{
    int *read_fd = (int *)context;

    return util_read_nointr(*read_fd, buf, buf_len);
}

static int layer_archive_io_close(void *context, char **err)
{
    int *read_fd = (int *)context;

    close(*read_fd);

    free(read_fd);

    return 0;
}

87
static int fill_read_wrapper(const char *layer_data_path, struct io_read_wrapper **reader)
L
LiFeng 已提交
88 89 90
{
    int ret = 0;
    int *fd_ptr = NULL;
91 92 93 94 95 96 97 98 99 100 101 102
    struct io_read_wrapper *reader_tmp = NULL;

    if (layer_data_path == NULL) {
        return 0;
    }

    reader_tmp = util_common_calloc_s(sizeof(struct io_read_wrapper));
    if (reader_tmp == NULL) {
        ERROR("Memory out");
        ret = -1;
        goto out;
    }
L
LiFeng 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117

    fd_ptr = util_common_calloc_s(sizeof(int));
    if (fd_ptr == NULL) {
        ERROR("Memory out");
        ret = -1;
        goto out;
    }

    *fd_ptr = util_open(layer_data_path, O_RDONLY, 0);
    if (*fd_ptr == -1) {
        ERROR("Failed to open layer data %s", layer_data_path);
        ret = -1;
        goto err_out;
    }

118 119 120 121
    reader_tmp->context = fd_ptr;
    reader_tmp->read = layer_archive_io_read;
    reader_tmp->close = layer_archive_io_close;
    *reader = reader_tmp;
L
LiFeng 已提交
122 123 124 125 126

    goto out;

err_out:
    free(fd_ptr);
127
    free(reader_tmp);
L
LiFeng 已提交
128 129 130 131
out:
    return ret;
}

132
static struct layer_opts *fill_create_layer_opts(storage_layer_create_opts_t *copts, const char *mount_label)
L
LiFeng 已提交
133 134 135 136 137 138 139 140 141
{
    struct layer_opts *opts = NULL;

    opts = util_common_calloc_s(sizeof(struct layer_opts));
    if (opts == NULL) {
        ERROR("Memory out");
        goto out;
    }

H
haozi007 已提交
142 143 144
    opts->parent = util_strdup_s(copts->parent);
    opts->uncompressed_digest = util_strdup_s(copts->uncompress_digest);
    opts->compressed_digest = util_strdup_s(copts->compressed_digest);
145
    opts->writable = copts->writable;
L
LiFeng 已提交
146

147 148 149 150 151 152 153 154
    opts->opts = util_common_calloc_s(sizeof(struct layer_store_mount_opts));
    if (opts->opts == NULL) {
        ERROR("Memory out");
        goto err_out;
    }

    opts->opts->mount_label = util_strdup_s(mount_label);

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
    if (copts->storage_opts != NULL) {
        opts->opts->mount_opts = util_common_calloc_s(sizeof(json_map_string_string));
        if (opts->opts->mount_opts == NULL) {
            ERROR("Memory out");
            goto err_out;
        }
        if (dup_json_map_string_string(copts->storage_opts, opts->opts->mount_opts) != 0) {
            ERROR("Failed to dup storage opts");
            goto err_out;
        }
    }

    goto out;

err_out:
    free_layer_opts(opts);
    opts = NULL;

L
LiFeng 已提交
173 174 175 176
out:
    return opts;
}

H
haozi007 已提交
177
int storage_layer_create(const char *layer_id, storage_layer_create_opts_t *copts)
L
LiFeng 已提交
178 179
{
    int ret = 0;
180
    struct io_read_wrapper *reader = NULL;
L
LiFeng 已提交
181 182
    struct layer_opts *opts = NULL;

H
haozi007 已提交
183 184 185 186 187
    if (copts == NULL) {
        ERROR("Create opts is null");
        return -1;
    }

188
    if (!copts->writable && copts->layer_data_path == NULL) {
L
LiFeng 已提交
189 190 191 192 193
        ERROR("Invalid arguments for put ro layer");
        ret = -1;
        goto out;
    }

H
haozi007 已提交
194
    if (fill_read_wrapper(copts->layer_data_path, &reader) != 0) {
L
LiFeng 已提交
195 196 197 198 199
        ERROR("Failed to fill layer read wrapper");
        ret = -1;
        goto out;
    }

200
    opts = fill_create_layer_opts(copts, NULL);
L
LiFeng 已提交
201 202 203 204 205 206
    if (opts == NULL) {
        ERROR("Failed to fill create ro layer options");
        ret = -1;
        goto out;
    }

L
LiFeng 已提交
207 208 209 210 211 212
    if (!storage_lock(&g_storage_rwlock, true)) {
        ERROR("Failed to lock image store, not allowed to create new layer");
        ret = -1;
        goto out;
    }

213
    ret = layer_store_create(layer_id, opts, reader, NULL);
L
LiFeng 已提交
214 215 216
    if (ret != 0) {
        ERROR("Failed to call layer store create");
        ret = -1;
L
LiFeng 已提交
217
        goto unlock_out;
L
LiFeng 已提交
218 219
    }

L
LiFeng 已提交
220 221 222
unlock_out:
    storage_unlock(&g_storage_rwlock);

L
LiFeng 已提交
223
out:
224 225 226 227 228
    if (reader != NULL) {
        if (reader->close != NULL) {
            reader->close(reader->context, NULL);
        }
        free(reader);
L
LiFeng 已提交
229 230 231 232 233
    }
    free_layer_opts(opts);
    return ret;
}

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
struct layer_list *storage_layers_get_by_uncompress_digest(const char *digest)
{
    int ret = 0;
    struct layer_list *layers = NULL;

    layers = util_common_calloc_s(sizeof(struct layer_list));
    if (layers == NULL) {
        ERROR("Out of memory");
        return NULL;
    }

    ret = layer_store_by_compress_digest(digest, layers);
    if (ret != 0) {
        ERROR("get layers by compressed digest failed");
        goto out;
    }

out:

    if (ret != 0) {
        free(layers);
        layers = NULL;
    }

    return layers;
}

L
LiFeng 已提交
261
struct layer *storage_layer_get(const char *layer_id)
L
LiFeng 已提交
262
{
L
LiFeng 已提交
263
    return layer_store_lookup(layer_id);
L
LiFeng 已提交
264 265
}

L
LiFeng 已提交
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
void free_layer(struct layer *ptr)
{
    if (ptr == NULL) {
        return;
    }
    free(ptr->id);
    ptr->id = NULL;
    free(ptr->parent);
    ptr->parent = NULL;
    free(ptr->mount_point);
    ptr->mount_point = NULL;
    free(ptr->compressed_digest);
    ptr->compressed_digest = NULL;
    free(ptr->uncompressed_digest);
    ptr->uncompressed_digest = NULL;
    free(ptr);
}

L
LiFeng 已提交
284
int storage_layer_try_repair_lowers(const char *layer_id, const char *last_layer_id)
L
LiFeng 已提交
285
{
L
LiFeng 已提交
286
    return layer_store_try_repair_lowers(layer_id);
L
LiFeng 已提交
287 288 289 290 291 292
}

int storage_img_create(const char *id, const char *parent_id, const char *metadata,
                       struct storage_img_create_options *opts)
{
    int ret = 0;
W
wujing 已提交
293
    char *image_id = NULL;
L
LiFeng 已提交
294

L
LiFeng 已提交
295
    if (id == NULL || opts == NULL) {
L
LiFeng 已提交
296 297 298 299 300
        ERROR("Invalid arguments for image create");
        ret = -1;
        goto out;
    }

L
LiFeng 已提交
301 302 303 304 305 306
    if (!storage_lock(&g_storage_rwlock, true)) {
        ERROR("Failed to lock storage, not allowed to create new images");
        ret = -1;
        goto out;
    }

W
wujing 已提交
307 308
    image_id = image_store_create(id, NULL, 0, parent_id, metadata, opts->create_time, opts->digest);
    if (image_id == NULL) {
L
LiFeng 已提交
309 310
        ERROR("Failed to create img");
        ret = -1;
L
LiFeng 已提交
311
        goto unlock_out;
L
LiFeng 已提交
312 313
    }

L
LiFeng 已提交
314 315
unlock_out:
    storage_unlock(&g_storage_rwlock);
L
LiFeng 已提交
316
out:
W
wujing 已提交
317
    free(image_id);
L
LiFeng 已提交
318 319 320
    return ret;
}

321
imagetool_image *storage_img_get(const char *img_id)
L
LiFeng 已提交
322
{
323 324 325
    char *normalized_name = NULL;
    imagetool_image *image_info = NULL;

L
LiFeng 已提交
326 327 328 329 330
    if (img_id == NULL) {
        ERROR("Invalid arguments for image get");
        return NULL;
    }

331 332 333 334 335 336 337 338 339
    if (util_valid_short_sha256_id(img_id) && image_store_exists(img_id)) {
        image_info = image_store_get_image(img_id);
    } else {
        normalized_name = oci_normalize_image_name(img_id);
        image_info = image_store_get_image(normalized_name);
    }

    free(normalized_name);
    return image_info;
L
LiFeng 已提交
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
}

int storage_img_set_big_data(const char *img_id, const char *key, const char *val)
{
    int ret = 0;

    if (img_id == NULL || key == NULL || val == NULL) {
        ERROR("Invalid arguments");
        ret = -1;
        goto out;
    }

    if (image_store_set_big_data(img_id, key, val) != 0) {
        ERROR("Failed to set img %s big data %s=%s", img_id, key, val);
        ret = -1;
        goto out;
    }

out:
    return ret;
}

L
lifeng68 已提交
362
int storage_img_get_names(const char *img_id, char ***names, size_t *names_len)
J
jikui 已提交
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
{
    int ret = 0;

    if (img_id == NULL || names == NULL || names_len == NULL) {
        ERROR("Invalid arguments");
        ret = -1;
        goto out;
    }

    if (image_store_get_names(img_id, names, names_len) != 0) {
        ERROR("Failed to get img %s names", img_id);
        ret = -1;
        goto out;
    }

out:
    return ret;
}

L
LiFeng 已提交
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
int storage_img_set_names(const char *img_id, const char **names, size_t names_len)
{
    int ret = 0;
    char **unique_names = NULL;
    size_t unique_names_len = 0;

    if (img_id == NULL || names == NULL || names_len == 0) {
        ERROR("Invalid arguments");
        ret = -1;
        goto out;
    }

    if (util_string_array_unique(names, names_len, &unique_names, &unique_names_len) != 0) {
        ERROR("Failed to unique names");
        ret = -1;
        goto out;
    }

    if (image_store_set_names(img_id, (const char **)unique_names, unique_names_len) != 0) {
        ERROR("Failed to set img %s names", img_id);
        ret = -1;
        goto out;
    }

out:
J
jikui 已提交
407
    util_free_array_by_len(unique_names, unique_names_len);
L
LiFeng 已提交
408 409 410
    return ret;
}

L
LiFeng 已提交
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
int storage_img_add_name(const char *img_id, const char *img_name)
{
    int ret = 0;

    if (img_id == NULL || img_name == NULL) {
        ERROR("Invalid arguments");
        ret = -1;
        goto out;
    }

    if (image_store_add_name(img_id, img_name) != 0) {
        ERROR("Failed to add img %s name %s", img_id, img_name);
        ret = -1;
        goto out;
    }

out:
    return ret;
}

L
lifeng68 已提交
431 432 433 434 435 436 437 438 439 440
char *storage_img_get_image_id(const char *img_name)
{
    if (img_name == NULL) {
        ERROR("Invalid arguments");
        return NULL;
    }

    return image_store_lookup(img_name);
}

L
LiFeng 已提交
441 442 443 444 445
bool is_top_layer_of_other_image(const char *img_id, const imagetool_images_list *all_images, const char *layer_id)
{
    size_t i = 0;

    for (i = 0; i < all_images->images_len; i++) {
L
lifeng68 已提交
446 447
        if (strcmp(all_images->images[i]->top_layer, layer_id) == 0 &&
            strcmp(all_images->images[i]->id, layer_id) != 0) {
L
LiFeng 已提交
448 449 450 451 452 453 454
            return true;
        }
    }

    return false;
}

455 456
bool is_parent_layer_of_other_layer(const char *layer_id, const char *last_deleted_layer_id,
                                    const struct layer_list *all_layers)
L
LiFeng 已提交
457 458 459 460 461 462 463 464 465 466 467 468 469 470
{
    size_t i = 0;

    for (i = 0; i < all_layers->layers_len; i++) {
        if (all_layers->layers[i]->parent != NULL && strcmp(all_layers->layers[i]->parent, layer_id) == 0) {
            if (last_deleted_layer_id == NULL || strcmp(all_layers->layers[i]->id, last_deleted_layer_id) != 0) {
                return true;
            }
        }
    }

    return false;
}

471 472
static int do_delete_related_layers(const char *img_id, const char *img_top_layer_id,
                                    const imagetool_images_list *all_images, const struct layer_list *all_layers)
L
LiFeng 已提交
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 501 502
{
    int ret = 0;
    char *layer_id = NULL;
    char *last_deleted_layer_id = NULL;
    struct layer *layer_info = NULL;

    layer_id = util_strdup_s(img_top_layer_id);
    if (layer_id == NULL) {
        ERROR("Memory out %s", img_id);
        ret = -1;
        goto out;
    }

    while (layer_id != NULL) {
        // if the layer is the top layer of other image, then break
        if (is_top_layer_of_other_image(img_id, all_images, layer_id)) {
            break;
        }

        if (is_parent_layer_of_other_layer(layer_id, last_deleted_layer_id, all_layers)) {
            break;
        }

        layer_info = layer_store_lookup(layer_id);
        if (layer_info == NULL) {
            ERROR("Failed to get layer info for layer %s", layer_id);
            ret = -1;
            goto out;
        }

L
LiFeng 已提交
503
        if (layer_store_delete(layer_id) != 0) {
L
LiFeng 已提交
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
            ERROR("Failed to remove layer %s", layer_id);
            ret = -1;
            goto out;
        }

        free(last_deleted_layer_id);
        last_deleted_layer_id = util_strdup_s(layer_id);
        free(layer_id);
        layer_id = util_strdup_s(layer_info->parent);
        free_layer(layer_info);
        layer_info = NULL;
    }
out:
    free(last_deleted_layer_id);
    free(layer_id);
    free_layer(layer_info);
    return ret;
}

static int delete_img_related_layers(const char *img_id, const char *img_top_layer_id)
{
    int ret = 0;
    imagetool_images_list *all_images = NULL;
    struct layer_list *all_layers = NULL;

    all_images = util_common_calloc_s(sizeof(imagetool_images_list));
    if (all_images == NULL) {
        ERROR("Memory out");
        ret = -1;
        goto out;
    }

    all_layers = util_common_calloc_s(sizeof(struct layer_list));
    if (all_layers == NULL) {
        ERROR("Memory out");
        ret = -1;
        goto out;
    }

    if (image_store_get_all_images(all_images) != 0) {
        ERROR("Failed to get all images info");
        ret = -1;
        goto out;
    }

    if (layer_store_list(all_layers) != 0) {
        ERROR("Failed to get all images info");
        ret = -1;
        goto out;
    }

    ret = do_delete_related_layers(img_id, img_top_layer_id, all_images, all_layers);

out:
    free_imagetool_images_list(all_images);
    free_layer_list(all_layers);

    return ret;
}

W
wujing 已提交
564 565 566 567
static void free_rootfs_list(struct rootfs_list *list)
{
    size_t i;

W
wujing 已提交
568 569 570 571
    if (list == NULL) {
        return;
    }

W
wujing 已提交
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
    for (i = 0; i < list->rootfs_len; i++) {
        free_storage_rootfs(list->rootfs[i]);
        list->rootfs[i] = NULL;
    }

    free(list->rootfs);
    list->rootfs = NULL;
    list->rootfs_len = 0;

    free(list);
}

static int check_image_occupancy_status(const char *img_id, bool *in_using)
{
    bool ret = 0;
    size_t i;
    struct rootfs_list *all_rootfs = NULL;
    char *img_long_id = NULL;

    img_long_id = image_store_lookup(img_id);
    if (img_long_id == NULL) {
        ERROR("Image not known");
        return -1;
    }

    all_rootfs = util_common_calloc_s(sizeof(struct rootfs_list));
    if (all_rootfs == NULL) {
        ERROR("Out of memory");
        ret = -1;
        goto out;
    }

    if (rootfs_store_get_all_rootfs(all_rootfs) != 0) {
        ERROR("Failed to get all container rootfs information");
        ret = -1;
        goto out;
    }

    for (i = 0; i < all_rootfs->rootfs_len; i++) {
        if (strcmp(all_rootfs->rootfs[i]->image, img_long_id) == 0) {
            isulad_set_error_message("Image used by %s", all_rootfs->rootfs[i]->id);
            ERROR("Image used by %s", all_rootfs->rootfs[i]->id);
            *in_using = true;
            goto out;
        }
    }

out:
    free(img_long_id);
    free_rootfs_list(all_rootfs);
    return ret;
}

L
LiFeng 已提交
625 626 627
int storage_img_delete(const char *img_id, bool commit)
{
    int ret = 0;
W
wujing 已提交
628
    bool in_using = false;
L
LiFeng 已提交
629
    imagetool_image *image_info = NULL;
L
LiFeng 已提交
630

L
LiFeng 已提交
631 632 633 634 635 636 637 638
    if (img_id == NULL) {
        ERROR("Invalid input arguments");
        ret = -1;
        goto out;
    }

    if (!storage_lock(&g_storage_rwlock, true)) {
        ERROR("Failed to lock storage, not allowed to delete image");
L
LiFeng 已提交
639 640 641 642
        ret = -1;
        goto out;
    }

L
LiFeng 已提交
643
    if (!image_store_exists(img_id)) {
L
lifeng68 已提交
644
        WARN("Image %s not exists", img_id);
L
LiFeng 已提交
645 646 647 648 649 650 651 652 653 654 655
        ret = 0;
        goto unlock_out;
    }

    image_info = image_store_get_image(img_id);
    if (image_info == NULL) {
        ERROR("Failed to get image %s info", img_id);
        ret = -1;
        goto unlock_out;
    }

W
wujing 已提交
656 657 658 659 660 661 662 663 664 665 666
    if (check_image_occupancy_status(img_id, &in_using) != 0) {
        ERROR("Failed to check image occupancy status");
        ret = -1;
        goto unlock_out;
    }

    if (in_using) {
        ERROR("Image is in use by a container");
        ret = -1;
        goto unlock_out;
    }
L
LiFeng 已提交
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681

    if (image_store_delete(image_info->id) != 0) {
        ERROR("Failed to delete img %s", img_id);
        ret = -1;
        goto unlock_out;
    }

    if (delete_img_related_layers(image_info->id, image_info->top_layer) != 0) {
        ERROR("Failed to delete img related layer %s", img_id);
        ret = -1;
        goto unlock_out;
    }

unlock_out:
    storage_unlock(&g_storage_rwlock);
L
LiFeng 已提交
682
out:
L
LiFeng 已提交
683
    free_imagetool_image(image_info);
L
LiFeng 已提交
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
    return ret;
}

int storage_img_set_loaded_time(const char *img_id, types_timestamp_t *loaded_time)
{
    int ret = 0;

    if (img_id == NULL || loaded_time == NULL) {
        ERROR("Invalid arguments");
        ret = -1;
        goto out;
    }

    if (image_store_set_load_time(img_id, loaded_time) != 0) {
        ERROR("Failed to set img %s loaded time", img_id);
        ret = -1;
        goto out;
    }

out:
    return ret;
}

L
LiFeng 已提交
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
static int64_t storage_img_cal_image_size(const char *image_id)
{
    size_t i = 0;
    int64_t total_size = -1;
    char *layer_id = NULL;
    char **big_data_names = NULL;
    size_t big_data_len = 0;
    struct layer *layer_info = NULL;

    if (image_id == NULL) {
        ERROR("Invalid arguments");
        total_size = -1;
        goto out;
    }

    if (image_store_big_data_names(image_id, &big_data_names, &big_data_len) != 0) {
        ERROR("Failed to read image %s big datas", image_id);
        total_size = -1;
        goto out;
    }

    for (i = 0; i < big_data_len; i++) {
        int64_t tmp = image_store_big_data_size(image_id, big_data_names[i]);
        if (tmp == -1) {
            ERROR("Failed to read big data %s for image %s", big_data_names[i], image_id);
            total_size = -1;
            goto out;
        }
        total_size += tmp;
    }

    layer_id = image_store_top_layer(image_id);
    if (layer_id == NULL) {
        ERROR("Failed to get top layer of image %s", image_id);
        total_size = -1;
        goto out;
    }

    while (layer_id != NULL) {
        layer_info = layer_store_lookup(layer_id);
        if (layer_info == NULL) {
            ERROR("Failed to get layer info for layer %s", layer_id);
            total_size = -1;
            goto out;
        }

        if (layer_info->uncompress_size < 0 || layer_info->uncompressed_digest == NULL) {
            ERROR("size for layer %s unknown", layer_id);
            total_size = -1;
            goto out;
        }

        total_size += layer_info->uncompress_size;

        free(layer_id);
        layer_id = util_strdup_s(layer_info->parent);
        free_layer(layer_info);
        layer_info = NULL;
    }

out:
    free(layer_id);
    free_layer(layer_info);
    util_free_array_by_len(big_data_names, big_data_len);
    return total_size;
}

int storage_img_set_image_size(const char *image_id)
{
    int ret = 0;
    int64_t image_size = 0;

    image_size = storage_img_cal_image_size(image_id);
    if (image_size < 0) {
        ERROR("Failed to get image %s size", image_id);
        ret = -1;
        goto out;
    }

    if (image_store_set_image_size(image_id, (uint64_t)image_size) != 0) {
L
lifeng68 已提交
787
        ERROR("Failed to set image %s size %lu", image_id, (uint64_t)image_size);
L
LiFeng 已提交
788 789 790 791 792 793 794 795
        ret = -1;
        goto out;
    }

out:
    return ret;
}

L
LiFeng 已提交
796 797 798 799
char *storage_get_img_top_layer(const char *id)
{
    return image_store_top_layer(id);
}
L
LiFeng 已提交
800

L
LiFeng 已提交
801
int storage_get_all_images(imagetool_images_list *images)
802 803 804
{
    int ret = 0;

L
LiFeng 已提交
805 806
    if (images == NULL) {
        ERROR("Invalid input arguments");
807 808 809 810
        ret = -1;
        goto out;
    }

811
    ret = image_store_get_all_images(images);
812 813 814 815 816

out:
    return ret;
}

L
LiFeng 已提交
817 818 819 820 821
int storage_get_images_fs_usage(imagetool_fs_info *fs_info)
{
    return image_store_get_fs_info(fs_info);
}

L
LiFeng 已提交
822 823 824 825 826
bool storage_image_exist(const char *image_or_id)
{
    return image_store_exists(image_or_id);
}

827 828 829 830 831
size_t storage_get_img_count()
{
    return image_store_get_images_number();
}

L
LiFeng 已提交
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
static int check_module_init_opt(struct storage_module_init_options *opts)
{
    if (opts == NULL || opts->driver_name == NULL || opts->storage_root == NULL || opts->storage_run_root == NULL) {
        ERROR("Invalid input arguments");
        return -1;
    }

    return 0;
}

static int make_storage_directory(struct storage_module_init_options *opts)
{
    int ret = 0;

    if (util_mkdir_p(opts->storage_root, IMAGE_STORE_PATH_MODE) != 0) {
        SYSERROR("Failed to make %s", opts->storage_root);
        ret = -1;
        goto out;
    }

    if (util_mkdir_p(opts->storage_run_root, IMAGE_STORE_PATH_MODE) != 0) {
        SYSERROR("Failed to make %s", opts->storage_run_root);
        ret = -1;
        goto out;
    }

out:
    return ret;
}

862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
// recal size of images which do not have valid size
static int restore_images_size()
{
    int ret = 0;
    size_t i = 0;
    imagetool_images_list *images = NULL;

    images = util_common_calloc_s(sizeof(imagetool_images_list));
    if (images == NULL) {
        ERROR("Memory out");
        ret = -1;
        goto out;
    }

    if (image_store_get_all_images(images) != 0) {
        ERROR("Failed to list all images");
        ret = -1;
        goto out;
    }

    for (i = 0; i < images->images_len; i++) {
        if (images->images[i]->size == 0) {
            (void)storage_img_set_image_size(images->images[i]->id);
        }
    }

out:
    free_imagetool_images_list(images);
    return ret;
}

L
LiFeng 已提交
893 894 895 896
int storage_module_init(struct storage_module_init_options *opts)
{
    int ret = 0;

G
gaohuatao 已提交
897 898 899 900 901 902
    ret = util_recursive_rmdir(OCI_LOAD_TMP_WORK_DIR, 0);
    if (ret != 0) {
        ERROR("failed to remove dir %s", OCI_LOAD_TMP_WORK_DIR);
        goto out;
    }

L
LiFeng 已提交
903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924
    if (check_module_init_opt(opts) != 0) {
        ret = -1;
        goto out;
    }

    if (make_storage_directory(opts) != 0) {
        ret = -1;
        goto out;
    }

    if (layer_store_init(opts) != 0) {
        ERROR("Failed to init layer store");
        ret = -1;
        goto out;
    }

    if (image_store_init(opts) != 0) {
        ERROR("Failed to init image store");
        ret = -1;
        goto out;
    }

925 926 927 928 929 930
    if (restore_images_size() != 0) {
        ERROR("Failed to recal image size");
        ret = -1;
        goto out;
    }

L
lifeng68 已提交
931 932 933 934 935 936
    if (rootfs_store_init(opts) != 0) {
        ERROR("Failed to init rootfs store");
        ret = -1;
        goto out;
    }

L
LiFeng 已提交
937 938 939 940 941 942
    if (pthread_rwlock_init(&g_storage_rwlock, NULL) != 0) {
        ERROR("Failed to init storage rwlock");
        ret = -1;
        goto out;
    }

L
LiFeng 已提交
943 944 945 946
out:
    return ret;
}

L
LiFeng 已提交
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966
void free_storage_module_init_options(struct storage_module_init_options *opts)
{
    if (opts == NULL) {
        return;
    }

    free(opts->driver_name);
    opts->driver_name = NULL;

    free(opts->storage_root);
    opts->storage_root = NULL;

    free(opts->storage_run_root);
    opts->storage_run_root = NULL;

    util_free_array_by_len(opts->driver_opts, opts->driver_opts_len);
    opts->driver_opts = NULL;
    opts->driver_opts_len = 0;

    free(opts);
W
wujing 已提交
967
}
H
haozi007 已提交
968

969 970 971 972 973
void storage_module_exit()
{
    layer_store_exit();
}

L
LiFeng 已提交
974
void free_layer_list(struct layer_list *ptr)
H
haozi007 已提交
975 976 977 978 979 980 981 982 983 984 985 986 987 988 989
{
    size_t i = 0;
    if (ptr == NULL) {
        return;
    }

    for (; i < ptr->layers_len; i++) {
        free_layer(ptr->layers[i]);
        ptr->layers[i] = NULL;
    }
    free(ptr->layers);
    ptr->layers = NULL;
    free(ptr);
}

990
static int do_create_container_rw_layer(const char *container_id, const char *image_top_layer, const char *mount_label,
991 992 993 994 995 996 997
                                        json_map_string_string *storage_opts)
{
    int ret = 0;
    struct layer_opts *opts = NULL;

    storage_layer_create_opts_t copts = {
        .parent = image_top_layer,
998
        .writable = true,
999 1000 1001
        .storage_opts = storage_opts,
    };

1002
    opts = fill_create_layer_opts(&copts, mount_label);
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
    if (opts == NULL) {
        ERROR("Failed to fill create opts");
        ret = -1;
        goto out;
    }

    if (layer_store_create(container_id, opts, NULL, NULL) != 0) {
        ERROR("Failed to create container rootfs layer");
        ret = -1;
        goto out;
    }

out:
    free_layer_opts(opts);
    return ret;
}

1020 1021
int storage_rootfs_create(const char *container_id, const char *image, const char *mount_label,
                          json_map_string_string *storage_opts,
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
                          char **mountpoint)
{
    int ret = 0;
    char *rootfs_id = NULL;
    imagetool_image *image_info = NULL;
    struct layer *layer_info = NULL;

    if (container_id == NULL || image == NULL) {
        ERROR("Invalid arguments for rootfs create");
        ret = -1;
        goto out;
    }

    if (!storage_lock(&g_storage_rwlock, true)) {
        ERROR("Failed to lock storage, not allowed to create new rootfs");
        ret = -1;
        goto out;
    }

    image_info = storage_img_get(image);
    if (image_info == NULL) {
        ERROR("No such image:%s", image);
        ret = -1;
        goto unlock_out;
    }

    // note: we use container id as the layer id of the container
1049
    if (do_create_container_rw_layer(container_id, image_info->top_layer, mount_label, storage_opts) != 0) {
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
        ERROR("Failed to do create rootfs layer");
        ret = -1;
        goto unlock_out;
    }

    rootfs_id = rootfs_store_create(container_id, NULL, 0, image_info->id, container_id, NULL, NULL);
    if (rootfs_id == NULL) {
        ERROR("Failed to create rootfs");
        ret = -1;
        goto remove_layer;
    }

    layer_info = layer_store_lookup(container_id);
    if (layer_info == NULL) {
        ERROR("Failed to get created rootfs layer info");
        ret = -1;
        goto remove_layer;
    }

    if (mountpoint != NULL) {
        *mountpoint = util_strdup_s(layer_info->mount_point);
    }

    goto unlock_out;

remove_layer:
    if (layer_store_delete(container_id) != 0) {
        ERROR("Failed to delete layer %s due rootfs create fail", container_id);
    }

unlock_out:
    storage_unlock(&g_storage_rwlock);
out:
    free(rootfs_id);
    free_imagetool_image(image_info);
    free_layer(layer_info);
    return ret;
}

int storage_rootfs_delete(const char *container_id)
{
    int ret = 0;
    storage_rootfs *rootfs_info = NULL;

    if (container_id == NULL) {
        ERROR("Invalid input arguments");
        ret = -1;
        goto out;
    }

    if (!storage_lock(&g_storage_rwlock, true)) {
        ERROR("Failed to lock storage, not allowed to delete image");
        ret = -1;
        goto out;
    }

    if (!rootfs_store_exists(container_id)) {
        WARN("Container rootfs %s not exists", container_id);
        ret = 0;
        goto unlock_out;
    }

    rootfs_info = rootfs_store_get_rootfs(container_id);
    if (rootfs_info == NULL) {
        ERROR("Failed to get rootfs %s info", container_id);
        ret = -1;
        goto unlock_out;
    }

    if (layer_store_delete(rootfs_info->layer) != 0) {
        ERROR("Failed to remove layer %s", rootfs_info->layer);
        ret = -1;
        goto unlock_out;
    }

    if (rootfs_store_delete(container_id) != 0) {
        ERROR("Failed to remove rootfs %s", container_id);
        ret = -1;
        goto unlock_out;
    }

unlock_out:
    storage_unlock(&g_storage_rwlock);
out:
    free_storage_rootfs(rootfs_info);
    return ret;
}
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165

int storage_rootfs_fs_usgae(const char *container_id, imagetool_fs_info *fs_info)
{
    int ret = 0;
    storage_rootfs *rootfs_info = NULL;

    if (container_id == NULL || fs_info == NULL) {
        ERROR("Invalid input arguments");
        ret = -1;
        goto out;
    }

    rootfs_info = rootfs_store_get_rootfs(container_id);
    if (rootfs_info == NULL) {
        ERROR("Failed to get rootfs %s info", container_id);
        ret = -1;
        goto out;
    }

    if (layer_store_get_layer_fs_info(rootfs_info->layer, fs_info) != 0) {
        ERROR("Failed to get layer %s fs usgae info", rootfs_info->layer);
        ret = -1;
        goto out;
    }

out:
    free_storage_rootfs(rootfs_info);
    return ret;
}
L
lifeng68 已提交
1166

1167
char *storage_rootfs_mount(const char *container_id)
L
lifeng68 已提交
1168
{
1169
    char *mount_point = NULL;
L
lifeng68 已提交
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
    storage_rootfs *rootfs_info = NULL;

    if (container_id == NULL) {
        ERROR("Invalid input arguments");
        goto out;
    }

    rootfs_info = rootfs_store_get_rootfs(container_id);
    if (rootfs_info == NULL) {
        ERROR("Failed to get rootfs %s info", container_id);
        goto out;
    }

1183 1184
    mount_point = layer_store_mount(rootfs_info->layer, NULL);
    if (mount_point == NULL) {
L
lifeng68 已提交
1185 1186 1187 1188 1189 1190
        ERROR("Failed to mount %s", rootfs_info->layer);
        goto out;
    }

out:
    free_storage_rootfs(rootfs_info);
1191
    return mount_point;
L
lifeng68 已提交
1192 1193
}

L
lifeng68 已提交
1194
int storage_rootfs_umount(const char *container_id, bool force)
L
lifeng68 已提交
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
{
    int ret = 0;
    storage_rootfs *rootfs_info = NULL;

    if (container_id == NULL) {
        ERROR("Invalid input arguments");
        ret = -1;
        goto out;
    }

    rootfs_info = rootfs_store_get_rootfs(container_id);
    if (rootfs_info == NULL) {
        ERROR("Failed to get rootfs %s info", container_id);
        ret = -1;
        goto out;
    }

L
lifeng68 已提交
1212
    if (layer_store_umount(rootfs_info->layer, force) != 0) {
L
lifeng68 已提交
1213 1214 1215 1216 1217 1218 1219 1220
        ERROR("Failed to umount layer %s", rootfs_info->layer);
        ret = -1;
        goto out;
    }

out:
    free_storage_rootfs(rootfs_info);
    return ret;
W
wujing 已提交
1221
}