1From 71f14902256e3c3529710b713e1ea43100bf4c40 Mon Sep 17 00:00:00 2001
2From: Khem Raj <raj.khem@gmail.com>
3Date: Sat, 17 Dec 2022 08:37:46 -0800
4Subject: [PATCH 2/2] linux-user: Replace use of lfs64 related functions and
5 macros
6
7Builds defines -D_FILE_OFFSET_BITS=64 which makes the original functions
8anf macros behave same as their 64 suffixed counterparts. This also
9helps in compiling with latest musl C library, where these macros and
10functions are no more available under _GNU_SOURCE feature macro
11
12Upstream-Status: Submitted [https://lists.gnu.org/archive/html/qemu-devel/2022-12/msg02841.html]
13Signed-off-by: Khem Raj <raj.khem@gmail.com>
14Cc: Laurent Vivier <laurent@vivier.eu>
15---
16 linux-user/syscall.c | 153 +++++++++++--------------------------------
17 1 file changed, 39 insertions(+), 114 deletions(-)
18
19Index: qemu-8.0.0/linux-user/syscall.c
20===================================================================
21--- qemu-8.0.0.orig/linux-user/syscall.c
22+++ qemu-8.0.0/linux-user/syscall.c
23@@ -761,8 +761,8 @@ safe_syscall6(ssize_t, copy_file_range,
24  */
25 #define safe_ioctl(...) safe_syscall(__NR_ioctl, __VA_ARGS__)
26 /* Similarly for fcntl. Note that callers must always:
27- *  pass the F_GETLK64 etc constants rather than the unsuffixed F_GETLK
28- *  use the flock64 struct rather than unsuffixed flock
29+ *  pass the F_GETLK etc constants rather than the unsuffixed F_GETLK
30+ *  use the flock struct rather than unsuffixed flock
31  * This will then work and use a 64-bit offset for both 32-bit and 64-bit hosts.
32  */
33 #ifdef __NR_fcntl64
34@@ -6813,13 +6813,13 @@ static int target_to_host_fcntl_cmd(int
35         ret = cmd;
36         break;
37     case TARGET_F_GETLK:
38-        ret = F_GETLK64;
39+        ret = F_GETLK;
40         break;
41     case TARGET_F_SETLK:
42-        ret = F_SETLK64;
43+        ret = F_SETLK;
44         break;
45     case TARGET_F_SETLKW:
46-        ret = F_SETLKW64;
47+        ret = F_SETLKW;
48         break;
49     case TARGET_F_GETOWN:
50         ret = F_GETOWN;
51@@ -6833,17 +6833,6 @@ static int target_to_host_fcntl_cmd(int
52     case TARGET_F_SETSIG:
53         ret = F_SETSIG;
54         break;
55-#if TARGET_ABI_BITS == 32
56-    case TARGET_F_GETLK64:
57-        ret = F_GETLK64;
58-        break;
59-    case TARGET_F_SETLK64:
60-        ret = F_SETLK64;
61-        break;
62-    case TARGET_F_SETLKW64:
63-        ret = F_SETLKW64;
64-        break;
65-#endif
66     case TARGET_F_SETLEASE:
67         ret = F_SETLEASE;
68         break;
69@@ -6895,8 +6884,8 @@ static int target_to_host_fcntl_cmd(int
70      * them to 5, 6 and 7 before making the syscall(). Since we make the
71      * syscall directly, adjust to what is supported by the kernel.
72      */
73-    if (ret >= F_GETLK64 && ret <= F_SETLKW64) {
74-        ret -= F_GETLK64 - 5;
75+    if (ret >= F_GETLK && ret <= F_SETLKW) {
76+        ret -= F_GETLK - 5;
77     }
78 #endif
79
80@@ -6929,55 +6918,11 @@ static int host_to_target_flock(int type
81     return type;
82 }
83
84-static inline abi_long copy_from_user_flock(struct flock64 *fl,
85-                                            abi_ulong target_flock_addr)
86-{
87-    struct target_flock *target_fl;
88-    int l_type;
89-
90-    if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
91-        return -TARGET_EFAULT;
92-    }
93-
94-    __get_user(l_type, &target_fl->l_type);
95-    l_type = target_to_host_flock(l_type);
96-    if (l_type < 0) {
97-        return l_type;
98-    }
99-    fl->l_type = l_type;
100-    __get_user(fl->l_whence, &target_fl->l_whence);
101-    __get_user(fl->l_start, &target_fl->l_start);
102-    __get_user(fl->l_len, &target_fl->l_len);
103-    __get_user(fl->l_pid, &target_fl->l_pid);
104-    unlock_user_struct(target_fl, target_flock_addr, 0);
105-    return 0;
106-}
107-
108-static inline abi_long copy_to_user_flock(abi_ulong target_flock_addr,
109-                                          const struct flock64 *fl)
110-{
111-    struct target_flock *target_fl;
112-    short l_type;
113-
114-    if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
115-        return -TARGET_EFAULT;
116-    }
117-
118-    l_type = host_to_target_flock(fl->l_type);
119-    __put_user(l_type, &target_fl->l_type);
120-    __put_user(fl->l_whence, &target_fl->l_whence);
121-    __put_user(fl->l_start, &target_fl->l_start);
122-    __put_user(fl->l_len, &target_fl->l_len);
123-    __put_user(fl->l_pid, &target_fl->l_pid);
124-    unlock_user_struct(target_fl, target_flock_addr, 1);
125-    return 0;
126-}
127-
128-typedef abi_long from_flock64_fn(struct flock64 *fl, abi_ulong target_addr);
129-typedef abi_long to_flock64_fn(abi_ulong target_addr, const struct flock64 *fl);
130+typedef abi_long from_flock_fn(struct flock *fl, abi_ulong target_addr);
131+typedef abi_long to_flock_fn(abi_ulong target_addr, const struct flock *fl);
132
133 #if defined(TARGET_ARM) && TARGET_ABI_BITS == 32
134-struct target_oabi_flock64 {
135+struct target_oabi_flock {
136     abi_short l_type;
137     abi_short l_whence;
138     abi_llong l_start;
139@@ -6985,10 +6930,10 @@ struct target_oabi_flock64 {
140     abi_int   l_pid;
141 } QEMU_PACKED;
142
143-static inline abi_long copy_from_user_oabi_flock64(struct flock64 *fl,
144+static inline abi_long copy_from_user_oabi_flock(struct flock *fl,
145                                                    abi_ulong target_flock_addr)
146 {
147-    struct target_oabi_flock64 *target_fl;
148+    struct target_oabi_flock *target_fl;
149     int l_type;
150
151     if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
152@@ -7009,10 +6954,10 @@ static inline abi_long copy_from_user_oa
153     return 0;
154 }
155
156-static inline abi_long copy_to_user_oabi_flock64(abi_ulong target_flock_addr,
157-                                                 const struct flock64 *fl)
158+static inline abi_long copy_to_user_oabi_flock(abi_ulong target_flock_addr,
159+                                                 const struct flock *fl)
160 {
161-    struct target_oabi_flock64 *target_fl;
162+    struct target_oabi_flock *target_fl;
163     short l_type;
164
165     if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
166@@ -7030,10 +6975,10 @@ static inline abi_long copy_to_user_oabi
167 }
168 #endif
169
170-static inline abi_long copy_from_user_flock64(struct flock64 *fl,
171+static inline abi_long copy_from_user_flock(struct flock *fl,
172                                               abi_ulong target_flock_addr)
173 {
174-    struct target_flock64 *target_fl;
175+    struct target_flock *target_fl;
176     int l_type;
177
178     if (!lock_user_struct(VERIFY_READ, target_fl, target_flock_addr, 1)) {
179@@ -7054,10 +6999,10 @@ static inline abi_long copy_from_user_fl
180     return 0;
181 }
182
183-static inline abi_long copy_to_user_flock64(abi_ulong target_flock_addr,
184-                                            const struct flock64 *fl)
185+static inline abi_long copy_to_user_flock(abi_ulong target_flock_addr,
186+                                            const struct flock *fl)
187 {
188-    struct target_flock64 *target_fl;
189+    struct target_flock *target_fl;
190     short l_type;
191
192     if (!lock_user_struct(VERIFY_WRITE, target_fl, target_flock_addr, 0)) {
193@@ -7076,7 +7021,7 @@ static inline abi_long copy_to_user_floc
194
195 static abi_long do_fcntl(int fd, int cmd, abi_ulong arg)
196 {
197-    struct flock64 fl64;
198+    struct flock fl64;
199 #ifdef F_GETOWN_EX
200     struct f_owner_ex fox;
201     struct target_f_owner_ex *target_fox;
202@@ -7089,6 +7034,7 @@ static abi_long do_fcntl(int fd, int cmd
203
204     switch(cmd) {
205     case TARGET_F_GETLK:
206+    case TARGET_F_OFD_GETLK:
207         ret = copy_from_user_flock(&fl64, arg);
208         if (ret) {
209             return ret;
210@@ -7098,32 +7044,11 @@ static abi_long do_fcntl(int fd, int cmd
211             ret = copy_to_user_flock(arg, &fl64);
212         }
213         break;
214-
215     case TARGET_F_SETLK:
216     case TARGET_F_SETLKW:
217-        ret = copy_from_user_flock(&fl64, arg);
218-        if (ret) {
219-            return ret;
220-        }
221-        ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
222-        break;
223-
224-    case TARGET_F_GETLK64:
225-    case TARGET_F_OFD_GETLK:
226-        ret = copy_from_user_flock64(&fl64, arg);
227-        if (ret) {
228-            return ret;
229-        }
230-        ret = get_errno(safe_fcntl(fd, host_cmd, &fl64));
231-        if (ret == 0) {
232-            ret = copy_to_user_flock64(arg, &fl64);
233-        }
234-        break;
235-    case TARGET_F_SETLK64:
236-    case TARGET_F_SETLKW64:
237     case TARGET_F_OFD_SETLK:
238     case TARGET_F_OFD_SETLKW:
239-        ret = copy_from_user_flock64(&fl64, arg);
240+        ret = copy_from_user_flock(&fl64, arg);
241         if (ret) {
242             return ret;
243         }
244@@ -7348,7 +7273,7 @@ static inline abi_long target_truncate64
245         arg2 = arg3;
246         arg3 = arg4;
247     }
248-    return get_errno(truncate64(arg1, target_offset64(arg2, arg3)));
249+    return get_errno(truncate(arg1, target_offset64(arg2, arg3)));
250 }
251 #endif
252
253@@ -7362,7 +7287,7 @@ static inline abi_long target_ftruncate6
254         arg2 = arg3;
255         arg3 = arg4;
256     }
257-    return get_errno(ftruncate64(arg1, target_offset64(arg2, arg3)));
258+    return get_errno(ftruncate(arg1, target_offset64(arg2, arg3)));
259 }
260 #endif
261
262@@ -8598,7 +8523,7 @@ static int do_getdents(abi_long dirfd, a
263     void *tdirp;
264     int hlen, hoff, toff;
265     int hreclen, treclen;
266-    off64_t prev_diroff = 0;
267+    off_t prev_diroff = 0;
268
269     hdirp = g_try_malloc(count);
270     if (!hdirp) {
271@@ -8651,7 +8576,7 @@ static int do_getdents(abi_long dirfd, a
272              * Return what we have, resetting the file pointer to the
273              * location of the first record not returned.
274              */
275-            lseek64(dirfd, prev_diroff, SEEK_SET);
276+            lseek(dirfd, prev_diroff, SEEK_SET);
277             break;
278         }
279
280@@ -8685,7 +8610,7 @@ static int do_getdents64(abi_long dirfd,
281     void *tdirp;
282     int hlen, hoff, toff;
283     int hreclen, treclen;
284-    off64_t prev_diroff = 0;
285+    off_t prev_diroff = 0;
286
287     hdirp = g_try_malloc(count);
288     if (!hdirp) {
289@@ -8727,7 +8652,7 @@ static int do_getdents64(abi_long dirfd,
290              * Return what we have, resetting the file pointer to the
291              * location of the first record not returned.
292              */
293-            lseek64(dirfd, prev_diroff, SEEK_SET);
294+            lseek(dirfd, prev_diroff, SEEK_SET);
295             break;
296         }
297
298@@ -11158,7 +11083,7 @@ static abi_long do_syscall1(CPUArchState
299                 return -TARGET_EFAULT;
300             }
301         }
302-        ret = get_errno(pread64(arg1, p, arg3, target_offset64(arg4, arg5)));
303+        ret = get_errno(pread(arg1, p, arg3, target_offset64(arg4, arg5)));
304         unlock_user(p, arg2, ret);
305         return ret;
306     case TARGET_NR_pwrite64:
307@@ -11175,7 +11100,7 @@ static abi_long do_syscall1(CPUArchState
308                 return -TARGET_EFAULT;
309             }
310         }
311-        ret = get_errno(pwrite64(arg1, p, arg3, target_offset64(arg4, arg5)));
312+        ret = get_errno(pwrite(arg1, p, arg3, target_offset64(arg4, arg5)));
313         unlock_user(p, arg2, 0);
314         return ret;
315 #endif
316@@ -11998,14 +11923,14 @@ static abi_long do_syscall1(CPUArchState
317     case TARGET_NR_fcntl64:
318     {
319         int cmd;
320-        struct flock64 fl;
321-        from_flock64_fn *copyfrom = copy_from_user_flock64;
322-        to_flock64_fn *copyto = copy_to_user_flock64;
323+        struct flock fl;
324+        from_flock_fn *copyfrom = copy_from_user_flock;
325+        to_flock_fn *copyto = copy_to_user_flock;
326
327 #ifdef TARGET_ARM
328         if (!cpu_env->eabi) {
329-            copyfrom = copy_from_user_oabi_flock64;
330-            copyto = copy_to_user_oabi_flock64;
331+            copyfrom = copy_from_user_oabi_flock;
332+            copyto = copy_to_user_oabi_flock;
333         }
334 #endif
335
336@@ -12015,7 +11940,7 @@ static abi_long do_syscall1(CPUArchState
337         }
338
339         switch(arg2) {
340-        case TARGET_F_GETLK64:
341+        case TARGET_F_GETLK:
342             ret = copyfrom(&fl, arg3);
343             if (ret) {
344                 break;
345@@ -12026,8 +11951,8 @@ static abi_long do_syscall1(CPUArchState
346             }
347 	    break;
348
349-        case TARGET_F_SETLK64:
350-        case TARGET_F_SETLKW64:
351+        case TARGET_F_SETLK:
352+        case TARGET_F_SETLKW:
353             ret = copyfrom(&fl, arg3);
354             if (ret) {
355                 break;
356