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