Whamcloud - gitweb
a7c31276de452e4cd14f4ae12167056ab19f948e
[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 #include "sysio-symbols.h"
66
67 /*
68  * Open file support.
69  */
70
71 mode_t  _sysio_umask = 0;                               /* process umask. */
72
73 /*
74  * Internal form of open.
75  */
76 int
77 _sysio_open(struct pnode *pno, int flags, mode_t mode)
78 {
79         int     ro;
80         int     w;
81         int     err;
82         struct inode *ino;
83
84         ro = IS_RDONLY(pno, pno->p_base->pb_ino);
85         w = flags & (O_WRONLY|O_RDWR);
86         if (w == (O_WRONLY|O_RDWR)) {
87                 /*
88                  * Huh?
89                  */
90                 return -EINVAL;
91         }
92         if (w && ro)
93                 return -EROFS;
94         ino = pno->p_base->pb_ino;
95         if ((flags & O_CREAT) && !ino) {
96                 struct pnode *parent;
97
98                 /*
99                  * Must create it.
100                  */
101                 if (ro)
102                         return -EROFS;
103                 parent = pno->p_parent;
104                 err = _sysio_p_validate(parent, NULL, NULL);
105                 if (!err) {
106                         ino = parent->p_base->pb_ino;
107                         assert(ino);
108                         err =
109                             !IS_RDONLY(parent, ino)
110                               ? (*ino->i_ops.inop_open)(pno, flags, mode)
111                               : -EROFS;
112                 }
113         } else if ((flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
114                 err = -EEXIST;
115         else if (!ino)
116                 err = _sysio_p_validate(pno, NULL, NULL);
117         else {
118                 /*
119                  * Simple open of pre-existing file.
120                  */
121                 err = (*ino->i_ops.inop_open)(pno, flags, mode);
122         }
123
124         return err;
125 }
126
127 #undef open
128
129 int
130 SYSIO_INTERFACE_NAME(open)(const char *path, int flags, ...)
131 {
132         mode_t  mode;
133         unsigned ndflags;
134         struct intent intent;
135         int     rtn;
136         struct pnode *pno;
137         struct file *fil;
138         SYSIO_INTERFACE_DISPLAY_BLOCK;
139
140         SYSIO_INTERFACE_ENTER;
141         /*
142          * Get mode argument and determine parameters for namei
143          */
144         mode = 0;
145         ndflags = 0;
146         intent.int_opmask = INT_OPEN;
147         if (flags & O_CREAT) {
148                 va_list ap;
149
150                 /*
151                  * Set ndflags to indicate return of negative alias is OK.
152                  */
153                 ndflags |= ND_NEGOK;
154
155                 /*
156                  * Will need mode too.
157                  */
158                 va_start(ap, flags);
159                 mode =
160 #ifndef REDSTORM
161                     va_arg(ap, mode_t);
162 #else
163                     va_arg(ap, int);
164 #endif
165                 va_end(ap);
166                 mode &= ~(_sysio_umask & 0777) | 07000; /* apply umask */
167
168                 if (flags & O_EXCL) {
169                         /*
170                          * Tell others we intend to create this file.
171                          */
172                         intent.int_opmask |= INT_CREAT;
173                 }
174         }
175 #ifdef O_NOFOLLOW
176         if (flags & O_NOFOLLOW)
177                 ndflags |= ND_NOFOLLOW;
178 #endif
179
180         /*
181          * Find the file.
182          */
183         fil = NULL;
184         INTENT_INIT(&intent, intent.int_opmask, &mode, &flags);
185         pno = NULL;
186         rtn = _sysio_namei(_sysio_cwd, path, ndflags, &intent, &pno);
187         if (rtn)
188                 goto error;
189         /*
190          * Ask for the open/creat.
191          */
192         rtn = _sysio_open(pno, flags, mode);
193         if (rtn)
194                 goto error;
195         /*
196          * Get a file descriptor.
197          */
198         fil = _sysio_fnew(pno->p_base->pb_ino, flags);
199         if (!fil) {
200                 rtn = -ENOMEM;
201                 goto error;
202         }
203         rtn = _sysio_fd_set(fil, -1);
204         if (rtn < 0)
205                 goto error;
206
207         P_RELE(pno);
208
209         SYSIO_INTERFACE_RETURN(rtn, 0);
210
211 error:
212         if (fil)
213                 F_RELE(fil);
214         if (pno)
215                 P_RELE(pno);
216         SYSIO_INTERFACE_RETURN(-1, rtn);
217 }
218
219 #ifdef __GLIBC__
220 #undef __open
221 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(open), __open)
222 #undef open64
223 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(open), SYSIO_INTERFACE_NAME(open64))
224 #undef __open64
225 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(open), __open64)
226 #endif
227
228 #ifdef REDSTORM
229 #undef __libc_open64
230 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(open), __libc_open64)
231 #endif
232
233 #ifdef BSD
234 #undef _open
235 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(open), _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), __close)
252 #endif
253
254 #ifdef BSD
255 #undef _close
256 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(close), _close)
257 #endif
258
259 int
260 SYSIO_INTERFACE_NAME(creat)(const char *path, mode_t mode)
261 {
262
263         return open(path, O_CREAT|O_WRONLY|O_TRUNC, mode);
264 }
265
266 #ifdef __GLIBC__
267 #undef __creat
268 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(creat), __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), SYSIO_INTERFACE_NAME(creat64))
275 #endif
276 #undef __creat64
277 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(creat), __creat64)
278 #endif
279
280 #ifdef REDSTORM
281 #undef __libc_creat
282 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(creat), __libc_creat)
283 #endif
284
285 #ifdef BSD
286 #undef _creat
287 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(creat), _creat)
288 #endif
289
290 mode_t
291 SYSIO_INTERFACE_NAME(umask)(mode_t mask)
292 {
293         mode_t  omask;
294
295         omask = _sysio_umask;
296         _sysio_umask = mask & 0777;
297         return omask;
298 }
299
300 #ifdef REDSTORM
301 #undef __umask
302 sysio_sym_weak_alias(SYSIO_INTERFACE_NAME(umask), __umask)
303 #endif