|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | +/* |
| 3 | + * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved. |
| 4 | + * Author: Yang Xu <[email protected]> |
| 5 | + * |
| 6 | + * This is a basic ioctl test about loopdevice. |
| 7 | + * |
| 8 | + * It is designed to test LOOP_SET_CAPACITY can update a live |
| 9 | + * loop device size when we change the size of the underlying |
| 10 | + * backing file. Also check sys value. |
| 11 | + */ |
| 12 | +#include <stdio.h> |
| 13 | +#include <unistd.h> |
| 14 | +#include <string.h> |
| 15 | +#include <stdlib.h> |
| 16 | +#include "lapi/loop.h" |
| 17 | +#include "tst_test.h" |
| 18 | + |
| 19 | +#define OLD_SIZE 10240 |
| 20 | +#define NEW_SIZE 5120 |
| 21 | + |
| 22 | +static char dev_path[1024], sys_loop_sizepath[1024]; |
| 23 | +static char *wrbuf; |
| 24 | +static int dev_num, dev_fd, file_fd, attach_flag; |
| 25 | + |
| 26 | +static void verify_ioctl_loop(void) |
| 27 | +{ |
| 28 | + struct loop_info loopinfoget; |
| 29 | + |
| 30 | + memset(&loopinfoget, 0, sizeof(loopinfoget)); |
| 31 | + tst_fill_file("test.img", 0, 1024, OLD_SIZE/1024); |
| 32 | + tst_attach_device(dev_path, "test.img"); |
| 33 | + attach_flag = 1; |
| 34 | + |
| 35 | + TST_ASSERT_INT(sys_loop_sizepath, OLD_SIZE/512); |
| 36 | + dev_fd = SAFE_OPEN(dev_path, O_RDWR); |
| 37 | + file_fd = SAFE_OPEN("test.img", O_RDWR); |
| 38 | + SAFE_IOCTL(dev_fd, LOOP_GET_STATUS, &loopinfoget); |
| 39 | + |
| 40 | + if (loopinfoget.lo_flags & LO_FLAGS_READ_ONLY) |
| 41 | + tst_brk(TCONF, "Current environment has unexpected LO_FLAGS_READ_ONLY flag"); |
| 42 | + |
| 43 | + SAFE_TRUNCATE("test.img", NEW_SIZE); |
| 44 | + SAFE_IOCTL(dev_fd, LOOP_SET_CAPACITY); |
| 45 | + |
| 46 | + /*check that we can't write data beyond 5K into loop device*/ |
| 47 | + TEST(write(dev_fd, wrbuf, OLD_SIZE)); |
| 48 | + if (TST_RET == NEW_SIZE) { |
| 49 | + tst_res(TPASS, "LOOP_SET_CAPACITY set loop size to %d", NEW_SIZE); |
| 50 | + } else { |
| 51 | + tst_res(TFAIL, "LOOP_SET_CAPACITY didn't set loop size to %d, its size is %ld", |
| 52 | + NEW_SIZE, TST_RET); |
| 53 | + } |
| 54 | + |
| 55 | + TST_ASSERT_INT(sys_loop_sizepath, NEW_SIZE/512); |
| 56 | + |
| 57 | + SAFE_CLOSE(file_fd); |
| 58 | + SAFE_CLOSE(dev_fd); |
| 59 | + tst_detach_device(dev_path); |
| 60 | + unlink("test.img"); |
| 61 | + attach_flag = 0; |
| 62 | +} |
| 63 | + |
| 64 | +static void setup(void) |
| 65 | +{ |
| 66 | + dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path)); |
| 67 | + if (dev_num < 0) |
| 68 | + tst_brk(TBROK, "Failed to find free loop device"); |
| 69 | + |
| 70 | + wrbuf = SAFE_MALLOC(OLD_SIZE); |
| 71 | + memset(wrbuf, 'x', OLD_SIZE); |
| 72 | + sprintf(sys_loop_sizepath, "/sys/block/loop%d/size", dev_num); |
| 73 | +} |
| 74 | + |
| 75 | +static void cleanup(void) |
| 76 | +{ |
| 77 | + if (dev_fd > 0) |
| 78 | + SAFE_CLOSE(dev_fd); |
| 79 | + if (file_fd > 0) |
| 80 | + SAFE_CLOSE(file_fd); |
| 81 | + if (wrbuf) |
| 82 | + free(wrbuf); |
| 83 | + if (attach_flag) |
| 84 | + tst_detach_device(dev_path); |
| 85 | +} |
| 86 | + |
| 87 | +static struct tst_test test = { |
| 88 | + .setup = setup, |
| 89 | + .cleanup = cleanup, |
| 90 | + .test_all = verify_ioctl_loop, |
| 91 | + .needs_root = 1, |
| 92 | + .needs_tmpdir = 1, |
| 93 | + .needs_drivers = (const char *const []) { |
| 94 | + "loop", |
| 95 | + NULL |
| 96 | + } |
| 97 | +}; |
0 commit comments