Whamcloud - gitweb
new upstream libsysio snapshot (20041101)
[fs/lustre-release.git] / libsysio / src / lseek.c
1 /*
2  *    This Cplant(TM) source code is the property of Sandia National
3  *    Laboratories.
4  *
5  *    This Cplant(TM) source code is copyrighted by Sandia National
6  *    Laboratories.
7  *
8  *    The redistribution of this Cplant(TM) source code is subject to the
9  *    terms of the GNU Lesser General Public License
10  *    (see cit/LGPL or http://www.gnu.org/licenses/lgpl.html)
11  *
12  *    Cplant(TM) Copyright 1998-2003 Sandia Corporation. 
13  *    Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
14  *    license for use of this work by or on behalf of the US Government.
15  *    Export of this program may require a license from the United States
16  *    Government.
17  */
18
19 /*
20  * This library is free software; you can redistribute it and/or
21  * modify it under the terms of the GNU Lesser General Public
22  * License as published by the Free Software Foundation; either
23  * version 2.1 of the License, or (at your option) any later version.
24  * 
25  * This library is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28  * Lesser General Public License for more details.
29  * 
30  * You should have received a copy of the GNU Lesser General Public
31  * License along with this library; if not, write to the Free Software
32  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33  *
34  * Questions or comments about this library should be sent to:
35  *
36  * Lee Ward
37  * Sandia National Laboratories, New Mexico
38  * P.O. Box 5800
39  * Albuquerque, NM 87185-1110
40  *
41  * lee@sandia.gov
42  */
43
44 #include <errno.h>
45 #include <unistd.h>
46 #include <assert.h>
47 #include <sys/types.h>
48 #include <sys/stat.h>
49 #include <sys/queue.h>
50
51 #include "sysio.h"
52 #include "inode.h"
53 #include "file.h"
54
55 #include "sysio-symbols.h"
56
57 static _SYSIO_OFF_T
58 _sysio_lseek(int fd, _SYSIO_OFF_T offset, int whence)
59 {
60         struct file *fil;
61         _SYSIO_OFF_T off, pos;
62         struct intnl_stat stbuf;
63
64         fil = _sysio_fd_find(fd);
65         if (!fil)
66                 return -EBADF;
67
68         off = -1;
69         switch (whence) {
70         
71         case SEEK_SET:
72                 off = 0;
73                 break;
74         case SEEK_CUR:
75                 off = fil->f_pos;
76                 break;
77         case SEEK_END:
78                 {
79                         int     err;
80
81                         /*
82                          * Don't blindly trust the attributes
83                          * in the inode record for this. Give the
84                          * driver a chance to refresh them.
85                          */
86                         err =
87                             (*fil->f_ino->i_ops.inop_getattr)(NULL,
88                                                               fil->f_ino,
89                                                               &stbuf);
90                         if (err)
91                                 return err;
92         
93                 }
94                 off = stbuf.st_size;
95                 break;
96         default:
97                 return -EINVAL;
98         }
99         pos = off + offset;
100         if ((offset < 0 && -offset > off) || (offset > 0 && pos <= off))
101                 return -EINVAL;
102
103 #ifdef O_LARGEFILE
104         if (pos >= ((fil->f_flags & O_LARGEFILE) ? _SYSIO_OFF_T_MAX : LONG_MAX))
105                 return -EOVERFLOW;
106 #else
107         if (pos >= _SYSIO_OFF_T_MAX)
108                 return -EOVERFLOW;
109 #endif
110         pos = (fil->f_ino->i_ops.inop_pos)(fil->f_ino, pos);
111         if (pos < 0)
112                 return pos;
113         fil->f_pos = pos;
114         return pos;
115 }
116
117 #if _LARGEFILE64_SOURCE
118 #undef lseek64
119
120 extern off64_t
121 SYSIO_INTERFACE_NAME(lseek64)(int fd, off64_t offset, int whence)
122 {
123         _SYSIO_OFF_T off;
124         off_t   rtn;
125         SYSIO_INTERFACE_DISPLAY_BLOCK;
126
127         SYSIO_INTERFACE_ENTER;
128         off = _sysio_lseek(fd, offset, whence);
129         if (off < 0)
130                 SYSIO_INTERFACE_RETURN((off_t )-1, (int )off);
131         rtn = (off64_t )off;
132         assert(rtn == off);
133         SYSIO_INTERFACE_RETURN(rtn, 0);
134 }
135 #ifdef __GLIBC__
136 #undef __lseek64
137 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(lseek64),
138                      PREPEND(__, SYSIO_INTERFACE_NAME(lseek64)))
139 #endif
140 #ifdef REDSTORM
141 #undef __libc_lseek64
142 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(lseek64),
143                      PREPEND(__, SYSIO_INTERFACE_NAME(libc_lseek64)))
144 #endif
145 #endif
146
147 #undef lseek
148
149 extern off_t
150 SYSIO_INTERFACE_NAME(lseek)(int fd, off_t offset, int whence)
151 {
152         _SYSIO_OFF_T off;
153         off_t   rtn;
154         SYSIO_INTERFACE_DISPLAY_BLOCK;
155
156         SYSIO_INTERFACE_ENTER;
157         off = _sysio_lseek(fd, offset, whence);
158         if (off < 0)
159                 SYSIO_INTERFACE_RETURN((off_t )-1, (int )off);
160         rtn = (off_t )off;
161         assert(rtn == off);
162         SYSIO_INTERFACE_RETURN(rtn, 0);
163 }
164
165 #ifdef __GLIBC__
166 #undef __lseek
167 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(lseek),
168                      PREPEND(__, SYSIO_INTERFACE_NAME(lseek)))
169 #endif
170
171 #ifdef __linux__
172 #undef llseek
173 int
174 SYSIO_INTERFACE_NAME(llseek)(unsigned int fd __IS_UNUSED,
175        unsigned long offset_high __IS_UNUSED,
176        unsigned long offset_low __IS_UNUSED,
177        loff_t *result __IS_UNUSED,
178        unsigned int whence __IS_UNUSED)
179 {
180         loff_t  off;
181         SYSIO_INTERFACE_DISPLAY_BLOCK;
182
183         /*
184          * This is just plain goofy.
185          */
186         SYSIO_INTERFACE_ENTER;
187 #if !_LARGEFILE64_SOURCE
188         if (offset_high) {
189                 /*
190                  * We are using 32-bit internals. This just isn't
191                  * going to work.
192                  */
193                 SYSIO_INTERFACE_RETURN(-1, -EOVERFLOW);
194         }
195 #else
196         off = offset_high;
197         off <<= 32;
198         off |= offset_low;
199 #endif
200         off = _sysio_lseek(fd, off, whence);
201         if (off < 0)
202                 SYSIO_INTERFACE_RETURN((off_t )-1, (int )off);
203         *result = off;
204         SYSIO_INTERFACE_RETURN(0, 0);
205 }
206
207 #undef __llseek
208 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(llseek), 
209                      PREPEND(__, SYSIO_INTERFACE_NAME(llseek)))
210 #endif