Elliott Hughes | 06040fd | 2013-07-09 13:25:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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/statvfs.h> |
| 20 | |
| 21 | #include <sys/types.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <fcntl.h> |
| 24 | |
Elliott Hughes | 3d305f1 | 2013-10-21 19:27:19 -0700 | [diff] [blame] | 25 | #include <string> |
| 26 | |
Elliott Hughes | db1ea34 | 2014-01-17 18:42:49 -0800 | [diff] [blame] | 27 | template <typename StatVfsT> void Check(StatVfsT& sb) { |
Vilas Bhat | 4c87f0d | 2024-08-16 00:03:31 +0000 | [diff] [blame^] | 28 | #if defined(__x86_64__) |
| 29 | // On x86_64 based 16kb page size targets, the page size in userspace is simulated to 16kb but |
| 30 | // the underlying filesystem block size would remain unchanged, i.e., 4kb. |
| 31 | // For more info: |
| 32 | // https://source.android.com/docs/core/architecture/16kb-page-size/getting-started-cf-x86-64-pgagnostic |
| 33 | EXPECT_EQ(4096, static_cast<int>(sb.f_bsize)); |
| 34 | #else |
Vilas Bhat | 297e7dd | 2024-05-01 01:11:30 +0000 | [diff] [blame] | 35 | EXPECT_EQ(getpagesize(), static_cast<int>(sb.f_bsize)); |
Vilas Bhat | 4c87f0d | 2024-08-16 00:03:31 +0000 | [diff] [blame^] | 36 | #endif |
Elliott Hughes | 3d305f1 | 2013-10-21 19:27:19 -0700 | [diff] [blame] | 37 | EXPECT_EQ(0U, sb.f_bfree); |
| 38 | EXPECT_EQ(0U, sb.f_ffree); |
Elliott Hughes | 3d305f1 | 2013-10-21 19:27:19 -0700 | [diff] [blame] | 39 | EXPECT_EQ(255U, sb.f_namemax); |
Elliott Hughes | fa495d5 | 2015-03-18 15:46:48 -0700 | [diff] [blame] | 40 | |
Elliott Hughes | 7506c37 | 2024-01-11 20:46:23 +0000 | [diff] [blame] | 41 | // Linux 6.7 requires that all filesystems have a non-zero fsid. |
| 42 | if (sb.f_fsid != 0U) { |
| 43 | // fs/libfs.c reuses the filesystem's device number. |
| 44 | struct stat proc_sb; |
| 45 | ASSERT_EQ(0, stat("/proc", &proc_sb)); |
| 46 | EXPECT_EQ(proc_sb.st_dev, sb.f_fsid); |
| 47 | } else { |
| 48 | // Prior to that, the fsid for /proc was just 0. |
| 49 | EXPECT_EQ(0U, sb.f_fsid); |
| 50 | } |
| 51 | |
Elliott Hughes | fa495d5 | 2015-03-18 15:46:48 -0700 | [diff] [blame] | 52 | // The kernel sets a private bit to indicate that f_flags is valid. |
| 53 | // This flag is not supposed to be exposed to libc clients. |
| 54 | static const uint32_t ST_VALID = 0x0020; |
| 55 | EXPECT_TRUE((sb.f_flag & ST_VALID) == 0) << sb.f_flag; |
Elliott Hughes | 06040fd | 2013-07-09 13:25:03 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Elliott Hughes | db1ea34 | 2014-01-17 18:42:49 -0800 | [diff] [blame] | 58 | TEST(sys_statvfs, statvfs) { |
Elliott Hughes | 06040fd | 2013-07-09 13:25:03 -0700 | [diff] [blame] | 59 | struct statvfs sb; |
Elliott Hughes | db1ea34 | 2014-01-17 18:42:49 -0800 | [diff] [blame] | 60 | ASSERT_EQ(0, statvfs("/proc", &sb)); |
| 61 | Check(sb); |
| 62 | } |
Elliott Hughes | 06040fd | 2013-07-09 13:25:03 -0700 | [diff] [blame] | 63 | |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 64 | TEST(sys_statvfs, statvfs64_smoke) { |
Elliott Hughes | db1ea34 | 2014-01-17 18:42:49 -0800 | [diff] [blame] | 65 | struct statvfs64 sb; |
| 66 | ASSERT_EQ(0, statvfs64("/proc", &sb)); |
| 67 | Check(sb); |
| 68 | } |
| 69 | |
| 70 | TEST(sys_statvfs, fstatvfs) { |
| 71 | struct statvfs sb; |
Elliott Hughes | 3d305f1 | 2013-10-21 19:27:19 -0700 | [diff] [blame] | 72 | int fd = open("/proc", O_RDONLY); |
Elliott Hughes | 06040fd | 2013-07-09 13:25:03 -0700 | [diff] [blame] | 73 | ASSERT_EQ(0, fstatvfs(fd, &sb)); |
| 74 | close(fd); |
Elliott Hughes | db1ea34 | 2014-01-17 18:42:49 -0800 | [diff] [blame] | 75 | Check(sb); |
| 76 | } |
Elliott Hughes | fa495d5 | 2015-03-18 15:46:48 -0700 | [diff] [blame] | 77 | |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 78 | TEST(sys_statvfs, fstatvfs64_smoke) { |
Elliott Hughes | db1ea34 | 2014-01-17 18:42:49 -0800 | [diff] [blame] | 79 | struct statvfs64 sb; |
| 80 | int fd = open("/proc", O_RDONLY); |
| 81 | ASSERT_EQ(0, fstatvfs64(fd, &sb)); |
| 82 | close(fd); |
| 83 | Check(sb); |
Elliott Hughes | 06040fd | 2013-07-09 13:25:03 -0700 | [diff] [blame] | 84 | } |