Whamcloud - gitweb
b=10329
[fs/lustre-release.git] / libsysio / src / open.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 /*
45  * Incorporate the GNU flags for open if we can.
46  */
47 #define _GNU_SOURCE
48
49 #include <stdlib.h>
50 #include <string.h>
51 #include <errno.h>
52 #include <assert.h>
53 #include <sys/types.h>
54 #include <sys/stat.h>
55 #include <fcntl.h>
56 #include <sys/queue.h>
57
58 #include "sysio.h"
59 #include "inode.h"
60 #include "file.h"
61 #include "fs.h"
62 #include "mount.h"
63 #include "sysio-symbols.h"
64
65 /*
66  * Open file support.
67  */
68
69 mode_t  _sysio_umask = 0;                               /* process umask. */
70
71 /*
72  * Internal form of open.
73  */
74 int
75 _sysio_open(struct pnode *pno, int flags, mode_t mode)
76 {
77         int     ro;
78         int     w;
79         int     err;
80         struct inode *ino;
81
82         ro = IS_RDONLY(pno, pno->p_base->pb_ino);
83         w = flags & (O_WRONLY|O_RDWR);
84         if (w == (O_WRONLY|O_RDWR)) {
85                 /*
86                  * Huh?
87                  */
88                 return -EINVAL;
89         }
90         if (w && ro)
91                 return -EROFS;
92         ino = pno->p_base->pb_ino;
93         if ((flags & O_CREAT) && !ino) {
94                 struct pnode *parent;
95
96                 /*
97                  * Must create it.
98                  */
99                 if (ro)
100                         return -EROFS;
101                 parent = pno->p_parent;
102                 err = _sysio_p_validate(parent, NULL, NULL);
103                 if (!err) {
104                         ino = parent->p_base->pb_ino;
105                         assert(ino);
106                         err =
107                             !IS_RDONLY(parent, ino)
108                               ? (*ino->i_ops.inop_open)(pno, flags, mode)
109                               : -EROFS;
110                 }
111         } else if ((flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
112                 err = -EEXIST;
113         else if (!ino)
114                 err = _sysio_p_validate(pno, NULL, NULL);
115 #ifdef O_NOFOLLOW
116         else if (flags & O_NOFOLLOW && S_ISLNK(ino->i_stbuf.st_mode))
117                 err = -ELOOP;
118 #endif
119         else {
120                 /*
121                  * Simple open of pre-existing file.
122                  */
123                 err = (*ino->i_ops.inop_open)(pno, flags, mode);
124         }
125
126         return err;
127 }
128
129 #undef open
130
131 int
132 SYSIO_INTERFACE_NAME(open)(const char *path, int flags, ...)
133 {
134         mode_t  mode;
135         unsigned ndflags;
136         struct intent intent;
137         int     rtn;
138         struct pnode *pno;
139         struct file *fil;
140         SYSIO_INTERFACE_DISPLAY_BLOCK;
141
142         SYSIO_INTERFACE_ENTER;
143         /*
144          * Get mode argument and determine parameters for namei
145          */
146         mode = 0;
147         ndflags = 0;
148         intent.int_opmask = INT_OPEN;
149         if (flags & O_CREAT) {
150                 va_list ap;
151
152                 /*
153                  * Set ndflags to indicate return of negative alias is OK.
154                  */
155                 ndflags |= ND_NEGOK;
156
157                 /*
158                  * Will need mode too.
159                  */
160                 va_start(ap, flags);
161                 mode =
162 #ifndef REDSTORM
163                     va_arg(ap, mode_t);
164 #else
165                     va_arg(ap, int);
166 #endif
167                 va_end(ap);
168                 mode &= ~(_sysio_umask & 0777) | 07000; /* apply umask */
169                 intent.int_opmask |= INT_CREAT;
170         }
171 #ifdef O_NOFOLLOW
172         if (flags & O_NOFOLLOW)
173                 ndflags |= ND_NOFOLLOW;
174 #endif
175
176         /*
177          * Find the file.
178          */
179         fil = NULL;
180         INTENT_INIT(&intent, intent.int_opmask, &mode, &flags);
181         pno = NULL;
182         rtn = _sysio_namei(_sysio_cwd, path, ndflags, &intent, &pno);
183         if (rtn)
184                 goto error;
185         /*
186          * Ask for the open/creat.
187          */
188         rtn = _sysio_open(pno, flags, mode);
189         if (rtn)
190                 goto error;
191         /*
192          * Get a file descriptor.
193          */
194         fil = _sysio_fnew(pno->p_base->pb_ino, flags);
195         if (!fil) {
196                 rtn = -ENOMEM;
197                 goto error;
198         }
199         rtn = _sysio_fd_set(fil, -1, 0);
200         if (rtn < 0)
201                 goto error;
202
203         P_RELE(pno);
204
205         SYSIO_INTERFACE_RETURN(rtn, 0);
206
207 error:
208         if (fil)
209                 F_RELE(fil);
210         if (pno)
211                 P_RELE(pno);
212         SYSIO_INTERFACE_RETURN(-1, rtn);
213 }
214
215 #ifdef __GLIBC__
216 #undef __open
217 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(open),
218                      PREPEND(__, SYSIO_INTERFACE_NAME(open)))
219 #undef open64
220 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(open), SYSIO_INTERFACE_NAME(open64))
221 #undef __open64
222 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(open),
223                      PREPEND(__, SYSIO_INTERFACE_NAME(open64)))
224 #endif
225
226 #ifdef REDSTORM
227 #undef __libc_open64
228 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(open),
229                      PREPEND(__, SYSIO_INTERFACE_NAME(libc_open64)))
230 #endif
231
232 #ifdef BSD
233 #undef _open
234 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(open),
235                      PREPEND(_, SYSIO_INTERFACE_NAME(open)))
236 #endif
237
238 int
239 SYSIO_INTERFACE_NAME(close)(int fd)
240 {
241         int     err;
242         SYSIO_INTERFACE_DISPLAY_BLOCK;
243
244         SYSIO_INTERFACE_ENTER;
245         err = _sysio_fd_close(fd);
246         SYSIO_INTERFACE_RETURN(err ? -1 : 0, err);
247 }
248
249 #ifdef __GLIBC__
250 #undef __close
251 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(close),
252                      PREPEND(__, SYSIO_INTERFACE_NAME(close)))
253 #endif
254
255 #ifdef BSD
256 #undef _close
257 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(close),
258                      PREPEND(_, SYSIO_INTERFACE_NAME(close)))
259 #endif
260
261 int
262 SYSIO_INTERFACE_NAME(creat)(const char *path, mode_t mode)
263 {
264
265         return SYSIO_INTERFACE_NAME(open)(path, O_CREAT|O_WRONLY|O_TRUNC, mode);
266 }
267
268 #ifdef __GLIBC__
269 #undef __creat
270 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(creat),
271                      PREPEND(__, SYSIO_INTERFACE_NAME(creat)))
272 #undef creat64
273 #ifndef HAVE_LUSTRE_HACK
274 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(creat), SYSIO_INTERFACE_NAME(creat64))
275 #else
276 /* XXX workaround SuSE SLES 8, glibc-2.2.5 */
277 sysio_sym_strong_alias(SYSIO_INTERFACE_NAME(creat),
278                        SYSIO_INTERFACE_NAME(creat64))
279 #endif
280 #undef __creat64
281 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(creat),
282                      PREPEND(__, SYSIO_INTERFACE_NAME(creat64)))
283 #endif
284
285 #ifdef REDSTORM
286 #undef __libc_creat
287 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(creat),
288                      PREPEND(__, SYSIO_INTERFACE_NAME(libc_creat)))
289 #endif
290
291 #ifdef BSD
292 #undef _creat
293 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(creat),
294                      PREPEND(_, SYSIO_INTERFACE_NAME(creat)))
295 #endif
296
297 mode_t
298 SYSIO_INTERFACE_NAME(umask)(mode_t mask)
299 {
300         mode_t  omask;
301
302         omask = _sysio_umask;
303         _sysio_umask = mask & 0777;
304         return omask;
305 }
306
307 #ifdef REDSTORM
308 #undef __umask
309 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(umask),
310                      PREPEND(__, SYSIO_INTERFACE_NAME(umask)))
311 #endif