Whamcloud - gitweb
build fixes for posix 2008 enviroment
[fs/lustre-release.git] / libsysio / src / fcntl.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-2005 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 <string.h>
45 #include <stdlib.h>
46 #include <errno.h>
47 #include <assert.h>
48 #include <sys/types.h>
49 #include <sys/stat.h>
50 #include <unistd.h>
51 #include <fcntl.h>
52 #include <sys/queue.h>
53
54 #include "sysio.h"
55 #include "inode.h"
56 #include "file.h"
57
58 #include "sysio-symbols.h"
59
60 #ifdef HAVE_LUSTRE_HACK
61 #include <sys/syscall.h>
62 #include <native.h>
63 #endif
64
65 #ifdef HAVE_LUSTRE_HACK
66 static int
67 _sysio_lustre_fcntl(int fd, int cmd, va_list ap, int *rtn)
68 {
69         long arg = va_arg(ap, long);
70
71         *rtn = syscall(SYSIO_SYS_fcntl, fd, cmd, arg);
72         return *rtn == -1 ? -errno : 0;
73 }
74 #endif
75
76 static int
77 _sysio_fcntl_raw_call(struct inode *ino, int *r, int cmd, ...)
78 {
79         va_list ap;
80         int     err;
81
82         va_start(ap, cmd);
83         err = ino->i_ops.inop_fcntl(ino, cmd, ap, r);
84         va_end(ap);
85         return err;
86 }
87
88 /*
89  * Convert offsets to absolute, when appropriate, and call appropriate driver
90  * to complete the fcntl lock function. If successful, convert
91  * returned values back to appropriate form.
92  */
93 static int
94 _sysio_fcntl_lock(struct file *fil, int cmd, struct _SYSIO_FLOCK *fl)
95 {
96         struct _SYSIO_FLOCK flock;
97         _SYSIO_OFF_T pos;
98         int     err;
99         int     rtn;
100
101         /*
102          * The drivers will not have a clue as to the
103          * current position of the file pointer. We need to
104          * convert relative whence values to absolute
105          * file adresses for them, then.
106          */
107         flock = *fl;
108         switch (flock.l_whence) {
109         case SEEK_SET:
110                 /*
111                  * At least parameter check this one, too.
112                  */
113         case SEEK_CUR:
114         case SEEK_END:
115                 pos =
116                     _sysio_lseek_prepare(fil,
117                                          flock.l_start,
118                                          flock.l_whence,
119                                          _SEEK_MAX(fil));
120                 if (pos < 0)
121                         return (int )pos;
122                 flock.l_start = pos;
123                 flock.l_whence = SEEK_SET;
124                 break;
125         default:
126                 return -EINVAL;
127         }
128         err =
129             _sysio_fcntl_raw_call(fil->f_ino, &rtn, cmd, &flock);
130         if (err)
131                 return err;
132         /*
133          * Ugh, convert back to relative form.
134          */
135         switch (fl->l_whence) {
136         case SEEK_SET:
137                 break;
138         case SEEK_CUR:
139                 fl->l_start = flock.l_start;
140                 fl->l_start -= fil->f_pos;
141                 break;
142         case SEEK_END:
143                 fl->l_start = flock.l_start;
144                 fl->l_start -=
145                     fil->f_ino->i_stbuf.st_size;
146                 break;
147         default:
148                 abort();
149         }
150         /*
151          * Return success.
152          */
153         return 0;
154 }
155
156 static int
157 _sysio_vfcntl(int fd, int cmd, va_list ap)
158 {
159         int     err;
160         int     rtn;
161         struct file *fil;
162         SYSIO_INTERFACE_DISPLAY_BLOCK;
163
164         SYSIO_INTERFACE_ENTER;
165         err = 0;
166         fil = _sysio_fd_find(fd);
167         if (!fil) {
168 #ifdef HAVE_LUSTRE_HACK
169                 err = _sysio_lustre_fcntl(fd, cmd, ap, &rtn);
170                 goto out;
171 #else
172                 rtn = -1;
173                 err = -EBADF;
174                 goto out;
175 #endif
176         }
177
178         switch (cmd) {
179
180             case F_DUPFD:
181                 {
182                         long    newfd;
183
184                         newfd = va_arg(ap, long);
185                         if (newfd != (int )newfd || newfd < 0) {
186                                 rtn = -1;
187                                 err = -EBADF;
188                                 goto out;
189                         }
190                         rtn = _sysio_fd_dup(fd, (int )newfd, 0);
191                         if (rtn < 0) {
192                                 err = rtn;
193                                 rtn = -1;
194                         }
195                 }
196                 break;
197 #if !(defined(_LARGEFILE64_SOURCE) || F_GETLK64 == F_GETLK)
198             case F_GETLK:
199             case F_SETLK:
200             case F_SETLKW:
201                 {
202                         struct intnl_stat buf;
203                         struct flock *fl;
204 #ifdef _LARGEFILE64_SOURCE
205                         struct _SYSIO_FLOCK flock64;
206 #endif
207
208                         /*
209                          * Refresh the cached attributes.
210                          */
211                         err =
212                             fil->f_ino->i_ops.inop_getattr(NULL,
213                                                            fil->f_ino,
214                                                            &buf);
215                         if (err) {
216                                 rtn = -1;
217                                 break;
218                         }
219                         /*
220                          * Copy args to a temp and normalize.
221                          */
222                         fl = va_arg(ap, struct flock *);
223 #ifdef _LARGEFILE64_SOURCE
224                         flock64.l_type = fl->l_type;
225                         flock64.l_whence = fl->l_whence;
226                         flock64.l_start = fl->l_start;
227                         flock64.l_len = fl->l_len;
228                         flock64.l_pid = fl->l_pid;
229                         err = _sysio_fcntl_lock(fil, cmd, &flock64);
230 #else
231                         err = _sysio_fcntl_lock(fil, cmd, fl);
232 #endif
233                         if (err < 0) {
234                                 rtn = -1;
235                                 break;
236                         }
237 #ifdef _LARGEFILE64_SOURCE
238                         /*
239                          * Copy back. Note that the fcntl_lock call
240                          * should have ensured that no overflow was possible.
241                          */
242                         fl->l_type = flock64.l_type;
243                         fl->l_whence = flock64.l_whence;
244                         fl->l_start = flock64.l_start;
245                         assert(fl->l_start == flock64.l_start);
246                         fl->l_len = flock64.l_len;
247                         assert(fl->l_len == flock64.l_len);
248                         fl->l_pid = flock64.l_pid;
249 #endif
250                         rtn = 0;
251                 }
252                 break;
253 #endif /* !(_LARGEFILE64_SOURCE || F_GETLK64 == F_GETLK) */
254 #ifdef _LARGEFILE64_SOURCE
255             case F_GETLK64:
256             case F_SETLK64:
257             case F_SETLKW64:
258                         {
259                                 struct flock64 *fl64;
260
261                                 fl64 = va_arg(ap, struct flock64 *);
262                                 err = _sysio_fcntl_lock(fil, cmd, fl64);
263                                 rtn = err ? -1 : 0;
264                         }
265                 break;
266 #endif
267             default:
268                 err = fil->f_ino->i_ops.inop_fcntl(fil->f_ino, cmd, ap, &rtn);
269                 break;
270         }
271
272 out:
273         SYSIO_INTERFACE_RETURN(rtn, err);
274 }
275
276 int
277 SYSIO_INTERFACE_NAME(fcntl)(int fd, int cmd, ...)
278 {
279         va_list ap;
280         int     err;
281
282         va_start(ap, cmd);
283         err = _sysio_vfcntl(fd, cmd, ap);
284         va_end(ap);
285         return err;
286 }
287
288 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(fcntl),
289                      SYSIO_INTERFACE_NAME(fcntl64))
290
291 #ifdef __GLIBC__
292 #undef __fcntl
293 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(fcntl), 
294                      PREPEND(__, SYSIO_INTERFACE_NAME(fcntl)))
295 #endif
296
297 #ifdef BSD
298 #undef _fcntl
299 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(fcntl), 
300                      PREPEND(_, SYSIO_INTERFACE_NAME(fcntl)))
301 #endif