Whamcloud - gitweb
Introduce .gitignore files.
[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-2006 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);
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 = (*ino->i_ops.inop_open)(pno, flags, mode);
107                 }
108         } else if ((flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
109                 err = -EEXIST;
110         else if (!ino)
111                 err = _sysio_p_validate(pno, NULL, NULL);
112 #ifdef O_NOFOLLOW
113         else if (flags & O_NOFOLLOW && S_ISLNK(ino->i_stbuf.st_mode))
114                 err = -ELOOP;
115 #endif
116         else {
117                 /*
118                  * Simple open of pre-existing file.
119                  */
120                 err = (*ino->i_ops.inop_open)(pno, flags, mode);
121         }
122
123         return err;
124 }
125
126 #undef open
127
128 int
129 SYSIO_INTERFACE_NAME(open)(const char *path, int flags, ...)
130 {
131         mode_t  mode;
132         unsigned ndflags;
133         struct intent intent;
134         int     rtn;
135         struct pnode *pno;
136         struct file *fil;
137         SYSIO_INTERFACE_DISPLAY_BLOCK;
138
139         SYSIO_INTERFACE_ENTER;
140         /*
141          * Get mode argument and determine parameters for namei
142          */
143         mode = 0;
144         ndflags = 0;
145         intent.int_opmask = INT_OPEN;
146         if (flags & O_CREAT) {
147                 va_list ap;
148
149                 /*
150                  * Set ndflags to indicate return of negative alias is OK.
151                  */
152                 ndflags |= ND_NEGOK;
153
154                 /*
155                  * Will need mode too.
156                  */
157                 va_start(ap, flags);
158                 mode =
159 #ifndef REDSTORM
160                     va_arg(ap, mode_t);
161 #else
162                     va_arg(ap, int);
163 #endif
164                 va_end(ap);
165                 mode &= ~(_sysio_umask & 0777) | 07000; /* apply umask */
166                 intent.int_opmask |= INT_CREAT;
167         }
168 #ifdef O_NOFOLLOW
169         if (flags & O_NOFOLLOW)
170                 ndflags |= ND_NOFOLLOW;
171 #endif
172
173         /*
174          * Find the file.
175          */
176         fil = NULL;
177         INTENT_INIT(&intent, intent.int_opmask, &mode, &flags);
178         pno = NULL;
179         rtn = _sysio_namei(_sysio_cwd, path, ndflags, &intent, &pno);
180         if (rtn)
181                 goto error;
182         /*
183          * Ask for the open/creat.
184          */
185         rtn = _sysio_open(pno, flags, mode);
186         if (rtn)
187                 goto error;
188         /*
189          * Get a file descriptor.
190          */
191         fil = _sysio_fnew(pno->p_base->pb_ino, flags);
192         if (!fil) {
193                 rtn = -ENOMEM;
194                 goto error;
195         }
196         rtn = _sysio_fd_set(fil, -1, 0);
197         if (rtn < 0)
198                 goto error;
199
200         P_RELE(pno);
201
202         SYSIO_INTERFACE_RETURN(rtn, 0);
203
204 error:
205         if (fil)
206                 F_RELE(fil);
207         if (pno)
208                 P_RELE(pno);
209         SYSIO_INTERFACE_RETURN(-1, rtn);
210 }
211
212 #ifdef __GLIBC__
213 #undef __open
214 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(open),
215                      PREPEND(__, SYSIO_INTERFACE_NAME(open)))
216 #undef open64
217 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(open), SYSIO_INTERFACE_NAME(open64))
218 #undef __open64
219 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(open),
220                      PREPEND(__, SYSIO_INTERFACE_NAME(open64)))
221 #endif
222
223 #ifdef REDSTORM
224 #undef __libc_open64
225 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(open),
226                      PREPEND(__, SYSIO_INTERFACE_NAME(libc_open64)))
227 #endif
228
229 #ifdef BSD
230 #undef _open
231 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(open),
232                      PREPEND(_, SYSIO_INTERFACE_NAME(open)))
233 #endif
234
235 int
236 SYSIO_INTERFACE_NAME(close)(int fd)
237 {
238         int     err;
239         SYSIO_INTERFACE_DISPLAY_BLOCK;
240
241         SYSIO_INTERFACE_ENTER;
242         err = _sysio_fd_close(fd);
243         SYSIO_INTERFACE_RETURN(err ? -1 : 0, err);
244 }
245
246 #ifdef __GLIBC__
247 #undef __close
248 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(close),
249                      PREPEND(__, SYSIO_INTERFACE_NAME(close)))
250 #endif
251
252 #ifdef BSD
253 #undef _close
254 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(close),
255                      PREPEND(_, SYSIO_INTERFACE_NAME(close)))
256 #endif
257
258 int
259 SYSIO_INTERFACE_NAME(creat)(const char *path, mode_t mode)
260 {
261
262         return SYSIO_INTERFACE_NAME(open)(path, O_CREAT|O_WRONLY|O_TRUNC, mode);
263 }
264
265 #ifdef __GLIBC__
266 #undef __creat
267 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(creat),
268                      PREPEND(__, SYSIO_INTERFACE_NAME(creat)))
269 #undef creat64
270 #ifndef HAVE_LUSTRE_HACK
271 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(creat), SYSIO_INTERFACE_NAME(creat64))
272 #else
273 /* XXX workaround SuSE SLES 8, glibc-2.2.5 */
274 sysio_sym_strong_alias(SYSIO_INTERFACE_NAME(creat),
275                        SYSIO_INTERFACE_NAME(creat64))
276 #endif
277 #undef __creat64
278 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(creat),
279                      PREPEND(__, SYSIO_INTERFACE_NAME(creat64)))
280 #endif
281
282 #ifdef REDSTORM
283 #undef __libc_creat
284 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(creat),
285                      PREPEND(__, SYSIO_INTERFACE_NAME(libc_creat)))
286 #endif
287
288 #ifdef BSD
289 #undef _creat
290 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(creat),
291                      PREPEND(_, SYSIO_INTERFACE_NAME(creat)))
292 #endif
293
294 mode_t
295 SYSIO_INTERFACE_NAME(umask)(mode_t mask)
296 {
297         mode_t  omask;
298
299         omask = _sysio_umask;
300         _sysio_umask = mask & 0777;
301         return omask;
302 }
303
304 #ifdef REDSTORM
305 #undef __umask
306 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(umask),
307                      PREPEND(__, SYSIO_INTERFACE_NAME(umask)))
308 #endif