1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci /* 3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines Corp., 2002 4f08c3bdfSopenharmony_ci * Copyright (c) 2015 Cyril Hrubis <chrubis@suse.cz> 5f08c3bdfSopenharmony_ci * Copyright (c) 2019 FUJITSU LIMITED. All rights reserved. 6f08c3bdfSopenharmony_ci * 7f08c3bdfSopenharmony_ci * Robbie Williamson <robbiew@us.ibm.com> 8f08c3bdfSopenharmony_ci * Roy Lee <roylee@andestech.com> 9f08c3bdfSopenharmony_ci */ 10f08c3bdfSopenharmony_ci/* 11f08c3bdfSopenharmony_ci * Test Description: 12f08c3bdfSopenharmony_ci * 13f08c3bdfSopenharmony_ci * Tests truncate and mandatory record locking. 14f08c3bdfSopenharmony_ci * 15f08c3bdfSopenharmony_ci * Parent creates a file, child locks a region and sleeps. 16f08c3bdfSopenharmony_ci * 17f08c3bdfSopenharmony_ci * Parent checks that ftruncate before the locked region and inside the region 18f08c3bdfSopenharmony_ci * fails while ftruncate after the region succeds. 19f08c3bdfSopenharmony_ci * 20f08c3bdfSopenharmony_ci * Parent wakes up child, child exits, lock is unlocked. 21f08c3bdfSopenharmony_ci * 22f08c3bdfSopenharmony_ci * Parent checks that ftruncate now works in all cases. 23f08c3bdfSopenharmony_ci * 24f08c3bdfSopenharmony_ci */ 25f08c3bdfSopenharmony_ci 26f08c3bdfSopenharmony_ci#include <stdio.h> 27f08c3bdfSopenharmony_ci#include <errno.h> 28f08c3bdfSopenharmony_ci#include <sys/types.h> 29f08c3bdfSopenharmony_ci#include <sys/stat.h> 30f08c3bdfSopenharmony_ci#include <sys/mount.h> 31f08c3bdfSopenharmony_ci#include <unistd.h> 32f08c3bdfSopenharmony_ci#include <stdlib.h> 33f08c3bdfSopenharmony_ci#include <sys/statvfs.h> 34f08c3bdfSopenharmony_ci 35f08c3bdfSopenharmony_ci#include "tst_test.h" 36f08c3bdfSopenharmony_ci 37f08c3bdfSopenharmony_ci#define RECLEN 100 38f08c3bdfSopenharmony_ci#define MNTPOINT "mntpoint" 39f08c3bdfSopenharmony_ci#define TESTFILE MNTPOINT"/testfile" 40f08c3bdfSopenharmony_ci 41f08c3bdfSopenharmony_cistatic int len = 8 * 1024; 42f08c3bdfSopenharmony_cistatic int recstart, reclen; 43f08c3bdfSopenharmony_ci 44f08c3bdfSopenharmony_cistatic void ftruncate_expect_fail(int fd, off_t offset, const char *msg) 45f08c3bdfSopenharmony_ci{ 46f08c3bdfSopenharmony_ci TEST(ftruncate(fd, offset)); 47f08c3bdfSopenharmony_ci 48f08c3bdfSopenharmony_ci if (TST_RET == 0) { 49f08c3bdfSopenharmony_ci tst_res(TFAIL, "ftruncate() %s succeeded unexpectedly", msg); 50f08c3bdfSopenharmony_ci return; 51f08c3bdfSopenharmony_ci } 52f08c3bdfSopenharmony_ci 53f08c3bdfSopenharmony_ci if (TST_ERR != EAGAIN) { 54f08c3bdfSopenharmony_ci tst_res(TFAIL | TTERRNO, 55f08c3bdfSopenharmony_ci "ftruncate() %s failed unexpectedly, expected EAGAIN", 56f08c3bdfSopenharmony_ci msg); 57f08c3bdfSopenharmony_ci return; 58f08c3bdfSopenharmony_ci } 59f08c3bdfSopenharmony_ci 60f08c3bdfSopenharmony_ci tst_res(TPASS, "ftruncate() %s failed with EAGAIN", msg); 61f08c3bdfSopenharmony_ci} 62f08c3bdfSopenharmony_ci 63f08c3bdfSopenharmony_cistatic void ftruncate_expect_success(int fd, off_t offset, const char *msg) 64f08c3bdfSopenharmony_ci{ 65f08c3bdfSopenharmony_ci struct stat sb; 66f08c3bdfSopenharmony_ci 67f08c3bdfSopenharmony_ci TEST(ftruncate(fd, offset)); 68f08c3bdfSopenharmony_ci 69f08c3bdfSopenharmony_ci if (TST_RET != 0) { 70f08c3bdfSopenharmony_ci tst_res(TFAIL | TTERRNO, 71f08c3bdfSopenharmony_ci "ftruncate() %s failed unexpectedly", msg); 72f08c3bdfSopenharmony_ci return; 73f08c3bdfSopenharmony_ci } 74f08c3bdfSopenharmony_ci 75f08c3bdfSopenharmony_ci SAFE_FSTAT(fd, &sb); 76f08c3bdfSopenharmony_ci 77f08c3bdfSopenharmony_ci if (sb.st_size != offset) { 78f08c3bdfSopenharmony_ci tst_res(TFAIL, 79f08c3bdfSopenharmony_ci "ftruncate() to %li bytes succeded but fstat() reports size %li", 80f08c3bdfSopenharmony_ci (long)offset, (long)sb.st_size); 81f08c3bdfSopenharmony_ci return; 82f08c3bdfSopenharmony_ci } 83f08c3bdfSopenharmony_ci 84f08c3bdfSopenharmony_ci tst_res(TPASS, "ftruncate() %s succeded", msg); 85f08c3bdfSopenharmony_ci} 86f08c3bdfSopenharmony_ci 87f08c3bdfSopenharmony_cistatic void doparent(void) 88f08c3bdfSopenharmony_ci{ 89f08c3bdfSopenharmony_ci int fd; 90f08c3bdfSopenharmony_ci 91f08c3bdfSopenharmony_ci TST_CHECKPOINT_WAIT(0); 92f08c3bdfSopenharmony_ci 93f08c3bdfSopenharmony_ci fd = SAFE_OPEN(TESTFILE, O_RDWR | O_NONBLOCK); 94f08c3bdfSopenharmony_ci 95f08c3bdfSopenharmony_ci ftruncate_expect_fail(fd, RECLEN, "offset before lock"); 96f08c3bdfSopenharmony_ci ftruncate_expect_fail(fd, recstart + RECLEN/2, "offset in lock"); 97f08c3bdfSopenharmony_ci ftruncate_expect_success(fd, recstart + RECLEN, "offset after lock"); 98f08c3bdfSopenharmony_ci 99f08c3bdfSopenharmony_ci TST_CHECKPOINT_WAKE(0); 100f08c3bdfSopenharmony_ci SAFE_WAIT(NULL); 101f08c3bdfSopenharmony_ci 102f08c3bdfSopenharmony_ci ftruncate_expect_success(fd, recstart + RECLEN/2, "offset in lock"); 103f08c3bdfSopenharmony_ci ftruncate_expect_success(fd, recstart, "offset before lock"); 104f08c3bdfSopenharmony_ci ftruncate_expect_success(fd, recstart + RECLEN, "offset after lock"); 105f08c3bdfSopenharmony_ci 106f08c3bdfSopenharmony_ci SAFE_CLOSE(fd); 107f08c3bdfSopenharmony_ci} 108f08c3bdfSopenharmony_ci 109f08c3bdfSopenharmony_civoid dochild(void) 110f08c3bdfSopenharmony_ci{ 111f08c3bdfSopenharmony_ci int fd; 112f08c3bdfSopenharmony_ci struct flock flocks; 113f08c3bdfSopenharmony_ci 114f08c3bdfSopenharmony_ci fd = SAFE_OPEN(TESTFILE, O_RDWR); 115f08c3bdfSopenharmony_ci 116f08c3bdfSopenharmony_ci tst_res(TINFO, "Child locks file"); 117f08c3bdfSopenharmony_ci 118f08c3bdfSopenharmony_ci flocks.l_type = F_WRLCK; 119f08c3bdfSopenharmony_ci flocks.l_whence = SEEK_CUR; 120f08c3bdfSopenharmony_ci flocks.l_start = recstart; 121f08c3bdfSopenharmony_ci flocks.l_len = reclen; 122f08c3bdfSopenharmony_ci 123f08c3bdfSopenharmony_ci SAFE_FCNTL(fd, F_SETLKW, &flocks); 124f08c3bdfSopenharmony_ci 125f08c3bdfSopenharmony_ci TST_CHECKPOINT_WAKE_AND_WAIT(0); 126f08c3bdfSopenharmony_ci 127f08c3bdfSopenharmony_ci tst_res(TINFO, "Child unlocks file"); 128f08c3bdfSopenharmony_ci 129f08c3bdfSopenharmony_ci exit(0); 130f08c3bdfSopenharmony_ci} 131f08c3bdfSopenharmony_ci 132f08c3bdfSopenharmony_cistatic void verify_ftruncate(void) 133f08c3bdfSopenharmony_ci{ 134f08c3bdfSopenharmony_ci int pid; 135f08c3bdfSopenharmony_ci 136f08c3bdfSopenharmony_ci if (tst_fill_file(TESTFILE, 0, 1024, 8)) 137f08c3bdfSopenharmony_ci tst_brk(TBROK, "Failed to create test file"); 138f08c3bdfSopenharmony_ci 139f08c3bdfSopenharmony_ci SAFE_CHMOD(TESTFILE, 02666); 140f08c3bdfSopenharmony_ci 141f08c3bdfSopenharmony_ci reclen = RECLEN; 142f08c3bdfSopenharmony_ci recstart = RECLEN + rand() % (len - 3 * RECLEN); 143f08c3bdfSopenharmony_ci 144f08c3bdfSopenharmony_ci pid = SAFE_FORK(); 145f08c3bdfSopenharmony_ci 146f08c3bdfSopenharmony_ci if (pid == 0) 147f08c3bdfSopenharmony_ci dochild(); 148f08c3bdfSopenharmony_ci 149f08c3bdfSopenharmony_ci doparent(); 150f08c3bdfSopenharmony_ci} 151f08c3bdfSopenharmony_ci 152f08c3bdfSopenharmony_cistatic void setup(void) 153f08c3bdfSopenharmony_ci{ 154f08c3bdfSopenharmony_ci /* 155f08c3bdfSopenharmony_ci * Kernel returns EPERM when CONFIG_MANDATORY_FILE_LOCKING is not 156f08c3bdfSopenharmony_ci * supported - to avoid false negatives, mount the fs first without 157f08c3bdfSopenharmony_ci * flags and then remount it as MS_MANDLOCK 158f08c3bdfSopenharmony_ci */ 159f08c3bdfSopenharmony_ci if (mount(NULL, MNTPOINT, NULL, MS_REMOUNT|MS_MANDLOCK, NULL) == -1) { 160f08c3bdfSopenharmony_ci if (errno == EPERM) { 161f08c3bdfSopenharmony_ci tst_brk(TCONF, 162f08c3bdfSopenharmony_ci "Mandatory lock not supported by this system"); 163f08c3bdfSopenharmony_ci } else { 164f08c3bdfSopenharmony_ci tst_brk(TBROK | TTERRNO, 165f08c3bdfSopenharmony_ci "Remount with MS_MANDLOCK failed"); 166f08c3bdfSopenharmony_ci } 167f08c3bdfSopenharmony_ci } 168f08c3bdfSopenharmony_ci} 169f08c3bdfSopenharmony_ci 170f08c3bdfSopenharmony_cistatic struct tst_test test = { 171f08c3bdfSopenharmony_ci .needs_kconfigs = (const char *[]) { 172f08c3bdfSopenharmony_ci "CONFIG_MANDATORY_FILE_LOCKING=y", 173f08c3bdfSopenharmony_ci NULL 174f08c3bdfSopenharmony_ci }, 175f08c3bdfSopenharmony_ci .test_all = verify_ftruncate, 176f08c3bdfSopenharmony_ci .setup = setup, 177f08c3bdfSopenharmony_ci .needs_checkpoints = 1, 178f08c3bdfSopenharmony_ci .forks_child = 1, 179f08c3bdfSopenharmony_ci .mount_device = 1, 180f08c3bdfSopenharmony_ci .needs_root = 1, 181f08c3bdfSopenharmony_ci .mntpoint = MNTPOINT, 182f08c3bdfSopenharmony_ci}; 183