Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 1 | /* |
| 2 | * llseek.c -- stub calling the llseek system call |
| 3 | * |
Theodore Ts'o | 1c27cac | 1997-08-14 17:20:42 +0000 | [diff] [blame] | 4 | * Copyright (C) 1994, 1995, 1996, 1997 Theodore Ts'o. |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 5 | * |
| 6 | * %Begin-Header% |
| 7 | * This file may be redistributed under the terms of the GNU Public |
| 8 | * License. |
| 9 | * %End-Header% |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
Theodore Ts'o | dc5f68c | 2000-05-25 23:31:54 +0000 | [diff] [blame] | 12 | #define _LARGEFILE_SOURCE |
| 13 | #define _LARGEFILE64_SOURCE |
| 14 | |
Theodore Ts'o | 1d2ff46 | 1997-10-19 23:00:21 +0000 | [diff] [blame] | 15 | #if HAVE_SYS_TYPES_H |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 16 | #include <sys/types.h> |
Theodore Ts'o | 1d2ff46 | 1997-10-19 23:00:21 +0000 | [diff] [blame] | 17 | #endif |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 18 | |
Theodore Ts'o | c555aeb | 1997-10-25 04:16:53 +0000 | [diff] [blame] | 19 | #if HAVE_ERRNO_H |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 20 | #include <errno.h> |
Theodore Ts'o | c555aeb | 1997-10-25 04:16:53 +0000 | [diff] [blame] | 21 | #endif |
Theodore Ts'o | 4cbe8af | 1997-08-10 23:07:40 +0000 | [diff] [blame] | 22 | #if HAVE_UNISTD_H |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 23 | #include <unistd.h> |
Theodore Ts'o | 4cbe8af | 1997-08-10 23:07:40 +0000 | [diff] [blame] | 24 | #endif |
Theodore Ts'o | 3cb6c50 | 1997-08-11 20:29:22 +0000 | [diff] [blame] | 25 | #ifdef __MSDOS__ |
| 26 | #include <io.h> |
| 27 | #endif |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 28 | #include "et/com_err.h" |
Theodore Ts'o | d40259f | 1997-10-20 00:44:26 +0000 | [diff] [blame] | 29 | #include "ext2fs/ext2_io.h" |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 30 | |
| 31 | #ifdef __linux__ |
| 32 | |
Theodore Ts'o | dc5f68c | 2000-05-25 23:31:54 +0000 | [diff] [blame] | 33 | #if defined(HAVE_LSEEK64) && defined(HAVE_LSEEK64_PROTOTYPE) |
| 34 | |
| 35 | #define my_llseek lseek64 |
| 36 | |
Theodore Ts'o | 48e6e81 | 2003-07-06 00:36:48 -0400 | [diff] [blame] | 37 | #else |
| 38 | #if defined(HAVE_LLSEEK) |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 39 | #include <syscall.h> |
| 40 | |
Theodore Ts'o | b0b9c4d | 1997-10-20 23:36:30 +0000 | [diff] [blame] | 41 | #ifndef HAVE_LLSEEK_PROTOTYPE |
| 42 | extern long long llseek (int fd, long long offset, int origin); |
Theodore Ts'o | 1c27cac | 1997-08-14 17:20:42 +0000 | [diff] [blame] | 43 | #endif |
| 44 | |
| 45 | #define my_llseek llseek |
| 46 | |
Theodore Ts'o | dc5f68c | 2000-05-25 23:31:54 +0000 | [diff] [blame] | 47 | #else /* ! HAVE_LLSEEK */ |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 48 | |
Theodore Ts'o | c2dbc18 | 2008-01-27 00:35:32 -0500 | [diff] [blame] | 49 | #if SIZEOF_LONG == SIZEOF_LONG_LONG |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 50 | |
| 51 | #define llseek lseek |
| 52 | |
Theodore Ts'o | c2dbc18 | 2008-01-27 00:35:32 -0500 | [diff] [blame] | 53 | #else /* SIZEOF_LONG != SIZEOF_LONG_LONG */ |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 54 | |
| 55 | #include <linux/unistd.h> |
| 56 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 57 | #ifndef __NR__llseek |
| 58 | #define __NR__llseek 140 |
| 59 | #endif |
| 60 | |
Theodore Ts'o | 1c27cac | 1997-08-14 17:20:42 +0000 | [diff] [blame] | 61 | #ifndef __i386__ |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 62 | static int _llseek (unsigned int, unsigned long, |
| 63 | unsigned long, ext2_loff_t *, unsigned int); |
| 64 | |
| 65 | static _syscall5(int,_llseek,unsigned int,fd,unsigned long,offset_high, |
| 66 | unsigned long, offset_low,ext2_loff_t *,result, |
| 67 | unsigned int, origin) |
Theodore Ts'o | 1c27cac | 1997-08-14 17:20:42 +0000 | [diff] [blame] | 68 | #endif |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 69 | |
Theodore Ts'o | 1c27cac | 1997-08-14 17:20:42 +0000 | [diff] [blame] | 70 | static ext2_loff_t my_llseek (int fd, ext2_loff_t offset, int origin) |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 71 | { |
| 72 | ext2_loff_t result; |
| 73 | int retval; |
| 74 | |
Theodore Ts'o | 1c27cac | 1997-08-14 17:20:42 +0000 | [diff] [blame] | 75 | #ifndef __i386__ |
| 76 | retval = _llseek(fd, ((unsigned long long) offset) >> 32, |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame^] | 77 | #else |
Theodore Ts'o | 1c27cac | 1997-08-14 17:20:42 +0000 | [diff] [blame] | 78 | retval = syscall(__NR__llseek, fd, (unsigned long long) (offset >> 32), |
| 79 | #endif |
| 80 | ((unsigned long long) offset) & 0xffffffff, |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 81 | &result, origin); |
| 82 | return (retval == -1 ? (ext2_loff_t) retval : result); |
| 83 | } |
| 84 | |
Theodore Ts'o | 4381966 | 2000-10-25 02:06:09 +0000 | [diff] [blame] | 85 | #endif /* __alpha__ || __ia64__ */ |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 86 | |
Theodore Ts'o | dc5f68c | 2000-05-25 23:31:54 +0000 | [diff] [blame] | 87 | #endif /* HAVE_LLSEEK */ |
Theodore Ts'o | 48e6e81 | 2003-07-06 00:36:48 -0400 | [diff] [blame] | 88 | #endif /* defined(HAVE_LSEEK64) && defined(HAVE_LSEEK64_PROTOTYPE) */ |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 89 | |
Theodore Ts'o | 1c27cac | 1997-08-14 17:20:42 +0000 | [diff] [blame] | 90 | ext2_loff_t ext2fs_llseek (int fd, ext2_loff_t offset, int origin) |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 91 | { |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 92 | ext2_loff_t result; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 93 | static int do_compat = 0; |
| 94 | |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 95 | if ((sizeof(off_t) >= sizeof(ext2_loff_t)) || |
| 96 | (offset < ((ext2_loff_t) 1 << ((sizeof(off_t)*8) -1)))) |
| 97 | return lseek(fd, (off_t) offset, origin); |
| 98 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 99 | if (do_compat) { |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 100 | errno = EINVAL; |
| 101 | return -1; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 102 | } |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame^] | 103 | |
Theodore Ts'o | 1c27cac | 1997-08-14 17:20:42 +0000 | [diff] [blame] | 104 | result = my_llseek (fd, offset, origin); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 105 | if (result == -1 && errno == ENOSYS) { |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 106 | /* |
| 107 | * Just in case this code runs on top of an old kernel |
| 108 | * which does not support the llseek system call |
| 109 | */ |
| 110 | do_compat++; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 111 | errno = EINVAL; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 112 | } |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 113 | return result; |
| 114 | } |
| 115 | |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 116 | #else /* !linux */ |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 117 | |
Theodore Ts'o | 73f17cf | 1999-01-04 07:35:45 +0000 | [diff] [blame] | 118 | #ifndef EINVAL |
| 119 | #define EINVAL EXT2_ET_INVALID_ARGUMENT |
| 120 | #endif |
| 121 | |
Theodore Ts'o | d163b09 | 1997-10-03 17:42:28 +0000 | [diff] [blame] | 122 | ext2_loff_t ext2fs_llseek (int fd, ext2_loff_t offset, int origin) |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 123 | { |
Theodore Ts'o | 488f3c2 | 2004-09-17 17:47:12 -0400 | [diff] [blame] | 124 | #if defined(HAVE_LSEEK64) && defined(HAVE_LSEEK64_PROTOTYPE) |
| 125 | return lseek64 (fd, offset, origin); |
| 126 | #else |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 127 | if ((sizeof(off_t) < sizeof(ext2_loff_t)) && |
| 128 | (offset >= ((ext2_loff_t) 1 << ((sizeof(off_t)*8) -1)))) { |
Theodore Ts'o | 73f17cf | 1999-01-04 07:35:45 +0000 | [diff] [blame] | 129 | errno = EINVAL; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 130 | return -1; |
| 131 | } |
| 132 | return lseek (fd, (off_t) offset, origin); |
Theodore Ts'o | 488f3c2 | 2004-09-17 17:47:12 -0400 | [diff] [blame] | 133 | #endif |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 136 | #endif /* linux */ |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 137 | |
| 138 | |