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