Elliott Hughes | 1d18e9c | 2014-02-18 15:48:56 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <gtest/gtest.h> |
| 18 | |
| 19 | #include <sys/vfs.h> |
| 20 | |
| 21 | #include <sys/types.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <fcntl.h> |
| 24 | |
| 25 | #include <string> |
| 26 | |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 27 | #include "utils.h" |
| 28 | |
Elliott Hughes | 1d18e9c | 2014-02-18 15:48:56 -0800 | [diff] [blame] | 29 | template <typename StatFsT> void Check(StatFsT& sb) { |
Vilas Bhat | 4c87f0d | 2024-08-16 00:03:31 +0000 | [diff] [blame] | 30 | #if defined(__x86_64__) |
| 31 | // On x86_64 based 16kb page size targets, the page size in userspace is simulated to 16kb but |
| 32 | // the underlying filesystem block size would remain unchanged, i.e., 4kb. |
| 33 | // For more info: |
| 34 | // https://source.android.com/docs/core/architecture/16kb-page-size/getting-started-cf-x86-64-pgagnostic |
| 35 | EXPECT_EQ(4096, static_cast<int>(sb.f_bsize)); |
| 36 | #else |
Vilas Bhat | 297e7dd | 2024-05-01 01:11:30 +0000 | [diff] [blame] | 37 | EXPECT_EQ(getpagesize(), static_cast<int>(sb.f_bsize)); |
Vilas Bhat | 4c87f0d | 2024-08-16 00:03:31 +0000 | [diff] [blame] | 38 | #endif |
Elliott Hughes | 1d18e9c | 2014-02-18 15:48:56 -0800 | [diff] [blame] | 39 | EXPECT_EQ(0U, sb.f_bfree); |
| 40 | EXPECT_EQ(0U, sb.f_ffree); |
Elliott Hughes | 1d18e9c | 2014-02-18 15:48:56 -0800 | [diff] [blame] | 41 | EXPECT_EQ(255, static_cast<int>(sb.f_namelen)); |
Elliott Hughes | fa495d5 | 2015-03-18 15:46:48 -0700 | [diff] [blame] | 42 | |
Elliott Hughes | 1b48afb | 2024-01-25 10:12:37 -0800 | [diff] [blame] | 43 | // Linux 6.7 requires that all filesystems have a non-zero fsid. |
| 44 | if (sb.f_fsid.__val[0] != 0U) { |
| 45 | // fs/libfs.c reuses the filesystem's device number. |
| 46 | struct stat proc_sb; |
| 47 | ASSERT_EQ(0, stat("/proc", &proc_sb)); |
| 48 | EXPECT_EQ(static_cast<int>(proc_sb.st_dev), sb.f_fsid.__val[0]); |
| 49 | EXPECT_EQ(0, sb.f_fsid.__val[1]); |
| 50 | } else { |
| 51 | // Prior to that, the fsid for /proc was just 0. |
| 52 | EXPECT_EQ(0, sb.f_fsid.__val[0]); |
| 53 | EXPECT_EQ(0, sb.f_fsid.__val[1]); |
| 54 | } |
| 55 | |
Elliott Hughes | fa495d5 | 2015-03-18 15:46:48 -0700 | [diff] [blame] | 56 | // The kernel sets a private bit to indicate that f_flags is valid. |
| 57 | // This flag is not supposed to be exposed to libc clients. |
| 58 | static const uint32_t ST_VALID = 0x0020; |
| 59 | EXPECT_TRUE((sb.f_flags & ST_VALID) == 0) << sb.f_flags; |
Elliott Hughes | 1d18e9c | 2014-02-18 15:48:56 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | TEST(sys_vfs, statfs) { |
| 63 | struct statfs sb; |
| 64 | ASSERT_EQ(0, statfs("/proc", &sb)); |
| 65 | Check(sb); |
| 66 | } |
| 67 | |
Elliott Hughes | 7cebf83 | 2020-08-12 14:25:41 -0700 | [diff] [blame] | 68 | TEST(sys_vfs, statfs_failure) { |
| 69 | struct statfs sb; |
| 70 | errno = 0; |
| 71 | ASSERT_EQ(-1, statfs("/does-not-exist", &sb)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 72 | ASSERT_ERRNO(ENOENT); |
Elliott Hughes | 7cebf83 | 2020-08-12 14:25:41 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 75 | TEST(sys_vfs, statfs64_smoke) { |
Elliott Hughes | 1d18e9c | 2014-02-18 15:48:56 -0800 | [diff] [blame] | 76 | struct statfs64 sb; |
| 77 | ASSERT_EQ(0, statfs64("/proc", &sb)); |
| 78 | Check(sb); |
| 79 | } |
| 80 | |
Elliott Hughes | 7cebf83 | 2020-08-12 14:25:41 -0700 | [diff] [blame] | 81 | TEST(sys_vfs, statfs64_failure) { |
| 82 | struct statfs64 sb; |
| 83 | errno = 0; |
| 84 | ASSERT_EQ(-1, statfs64("/does-not-exist", &sb)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 85 | ASSERT_ERRNO(ENOENT); |
Elliott Hughes | 7cebf83 | 2020-08-12 14:25:41 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Elliott Hughes | 1d18e9c | 2014-02-18 15:48:56 -0800 | [diff] [blame] | 88 | TEST(sys_vfs, fstatfs) { |
| 89 | struct statfs sb; |
| 90 | int fd = open("/proc", O_RDONLY); |
| 91 | ASSERT_EQ(0, fstatfs(fd, &sb)); |
| 92 | close(fd); |
| 93 | Check(sb); |
| 94 | } |
Elliott Hughes | fa495d5 | 2015-03-18 15:46:48 -0700 | [diff] [blame] | 95 | |
Elliott Hughes | 7cebf83 | 2020-08-12 14:25:41 -0700 | [diff] [blame] | 96 | TEST(sys_vfs, fstatfs_failure) { |
| 97 | struct statfs sb; |
| 98 | errno = 0; |
| 99 | ASSERT_EQ(-1, fstatfs(-1, &sb)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 100 | ASSERT_ERRNO(EBADF); |
Elliott Hughes | 7cebf83 | 2020-08-12 14:25:41 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 103 | TEST(sys_vfs, fstatfs64_smoke) { |
Elliott Hughes | 1d18e9c | 2014-02-18 15:48:56 -0800 | [diff] [blame] | 104 | struct statfs64 sb; |
| 105 | int fd = open("/proc", O_RDONLY); |
| 106 | ASSERT_EQ(0, fstatfs64(fd, &sb)); |
| 107 | close(fd); |
| 108 | Check(sb); |
| 109 | } |
Elliott Hughes | 7cebf83 | 2020-08-12 14:25:41 -0700 | [diff] [blame] | 110 | |
| 111 | TEST(sys_vfs, fstatfs64_failure) { |
| 112 | struct statfs sb; |
| 113 | errno = 0; |
| 114 | ASSERT_EQ(-1, fstatfs(-1, &sb)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 115 | ASSERT_ERRNO(EBADF); |
Elliott Hughes | 7cebf83 | 2020-08-12 14:25:41 -0700 | [diff] [blame] | 116 | } |