1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
/* Test for copy_file_range with large sizes (bug 33245).
Copyright (C) 2025 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
/* This test exercises copy_file_range with various large file sizes
on FUSE filesystems to verify proper handling of system call return
values. No data is actually copied. */
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <support/check.h>
#include <support/fuse.h>
#include <support/support.h>
#include <support/test-driver.h>
#include <support/xthread.h>
#include <support/xunistd.h>
#include <sys/stat.h>
#include <unistd.h>
static void
fuse_thread (struct support_fuse *f, void *closure)
{
/* Node IDs for our test files. */
enum { NODE_SOURCE = 2, NODE_DEST = 3 };
/* A large size, so that the kernel does not fail the
copy_file_range attempt before performing the FUSE callback.
Only the source file size matters to the kernel, but both files
use the same size for simplicity. */
const uint64_t file_size = 1LLU << 61;
struct fuse_in_header *inh;
while ((inh = support_fuse_next (f)) != NULL)
{
if (support_fuse_handle_mountpoint (f)
|| (inh->nodeid == 1 && support_fuse_handle_directory (f)))
continue;
switch (inh->opcode)
{
case FUSE_LOOKUP:
{
char *name = support_fuse_cast (LOOKUP, inh);
int node = 0;
if (inh->nodeid == 1 && strcmp (name, "source") == 0)
node = NODE_SOURCE;
else if (inh->nodeid == 1 && strcmp (name, "dest") == 0)
node = NODE_DEST;
if (node != 0)
{
struct fuse_entry_out *out
= support_fuse_prepare_entry (f, node);
out->attr.mode = S_IFREG | 0600;
out->attr.size = file_size;
support_fuse_reply_prepared (f);
}
else
support_fuse_reply_error (f, ENOENT);
}
break;
case FUSE_OPEN:
/* File open */
{
if (inh->nodeid == NODE_SOURCE || inh->nodeid == NODE_DEST)
{
struct fuse_open_out out = { .fh = inh->nodeid };
support_fuse_reply (f, &out, sizeof (out));
}
else
support_fuse_reply_error (f, ENOENT);
}
break;
case FUSE_GETATTR:
/* Get file attributes */
if (inh->nodeid == NODE_SOURCE || inh->nodeid == NODE_DEST)
{
struct fuse_attr_out *out = support_fuse_prepare_attr (f);
out->attr.mode = S_IFREG | 0600;
out->attr.size = file_size;
support_fuse_reply_prepared (f);
}
else
support_fuse_reply_error (f, ENOENT);
break;
case FUSE_COPY_FILE_RANGE:
{
struct fuse_copy_file_range_in *p
= support_fuse_cast (COPY_FILE_RANGE, inh);
/* Verify this is a copy from source to dest, starting at
offset 0. */
TEST_COMPARE (p->fh_in, NODE_SOURCE);
TEST_COMPARE (p->nodeid_out, NODE_DEST);
TEST_COMPARE (p->off_in, 0);
TEST_COMPARE (p->off_out, 0);
TEST_VERIFY (p->len > 0);
TEST_VERIFY (p->len <= file_size);
/* Pretend the copy succeeded. */
struct fuse_write_out out = { .size = p->len };
support_fuse_reply (f, &out, sizeof (out));
}
break;
case FUSE_FLUSH:
support_fuse_reply_empty (f);
break;
default:
support_fuse_reply_error (f, EIO);
}
}
}
static void
test_size (struct support_fuse *f, off64_t size)
{
/* On 32-bit targets, not all possible return values from
copy_file_range are representable. The current (Linux 6.5.18)
kernel FUSE implementation can produce negative non-error results
from copy_file_range in the range [1UL << 31, UINT_MAX - 4095],
but this seems to be a FUSE bug. */
if (size != (ssize_t) size)
{
printf ("info:%s size 0x%llx is too large for ssize_t\n",
test_verbose ? " " : "", (unsigned long long int) size);
return;
}
verbose_printf ("info: testing copy size 0x%llx\n",
(unsigned long long int) size);
const char *mountpoint = support_fuse_mountpoint (f);
char *source_path = xasprintf ("%s/source", mountpoint);
char *dest_path = xasprintf ("%s/dest", mountpoint);
int source_fd = xopen (source_path, O_RDONLY, 0);
int dest_fd = xopen (dest_path, O_WRONLY, 0);
ssize_t copied = copy_file_range (source_fd, NULL, dest_fd, NULL, size, 0);
/* Avoid FAIL_UNSUPPORTED if it is likely bogus due to previous
copy_file_range successes. */
if (copied == -1 && errno == ENOSYS)
{
/* Unmounting avoids a test hang on exit. */
xclose (dest_fd);
xclose (source_fd);
support_fuse_unmount (f);
FAIL_UNSUPPORTED ("copy_file_range not supported");
}
TEST_COMPARE (copied, size);
xclose (dest_fd);
xclose (source_fd);
free (dest_path);
free (source_path);
}
static void
test_all_sizes (struct support_fuse *f)
{
test_size (f, 0); /* Not actually handled by the callback. */
test_size (f, 20);
test_size (f, 1 << 30);
test_size (f, INT_MAX);
for (int i = 0; i <= 5; ++i)
test_size (f, (1U << 31) + i);
for (int i = -4100; i <= -4090; ++i)
test_size (f, UINT_MAX + i);
for (int i = -100; i <= 0; ++i)
test_size (f, UINT_MAX + i);
/* We would like to test larger values than UINT_MAX here, but they
do not work because the FUSE protocol uses uint32_t for the
copy_file_range result in struct fuse_write_out. */
}
static void *
test_cancel_state_variants (void *f_ptr)
{
struct support_fuse *f = (struct support_fuse *) f_ptr;
verbose_printf ("info: testing default cancellation settings\n");
test_all_sizes (f);
verbose_printf ("info: testing with cancellation disabled\n");
TEST_COMPARE (pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, NULL), 0);
test_all_sizes (f);
verbose_printf ("info: testing with cancellation enabled\n");
TEST_COMPARE (pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL), 0);
test_all_sizes (f);
return NULL;
}
static int
do_test (void)
{
support_fuse_init ();
struct support_fuse *f = support_fuse_mount (fuse_thread, NULL);
verbose_printf ("info: testing on main thread\n");
test_cancel_state_variants (f);
verbose_printf ("info: testing on secondary thread\n");
TEST_VERIFY (xpthread_join (xpthread_create
(NULL, test_cancel_state_variants, f))
== NULL);
verbose_printf ("info: testing on separate thread\n");
support_fuse_unmount (f);
return 0;
}
#include <support/test-driver.c>
|