Whamcloud - gitweb
Branch b1_6
[fs/lustre-release.git] / libsysio / src / stat.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 #include <errno.h>
45 #include <assert.h>
46 #include <sys/types.h>
47 #include <sys/stat.h>
48 #include <unistd.h>
49 #include <sys/queue.h>
50
51 #include "sysio.h"
52 #include "inode.h"
53 #include "file.h"
54
55 #include "sysio-symbols.h"
56
57 #ifndef REDSTORM
58 #undef fstat
59 #undef stat
60 #undef lstat
61 #endif
62
63 #undef __fxstat
64 #undef __xstat
65 #undef __lxstat
66
67 #if !defined(_STAT_VER)
68 #define _STAT_VER               0
69 #endif
70
71 #ifdef _LARGEFILE64_SOURCE
72 static void
73 convstat(struct stat64 *st64_buf, struct stat *st_buf)
74 {
75
76         st_buf->st_dev = st64_buf->st_dev;
77         st_buf->st_ino = st64_buf->st_ino;
78         st_buf->st_mode = st64_buf->st_mode;
79         st_buf->st_nlink = st64_buf->st_nlink;
80         st_buf->st_uid = st64_buf->st_uid;
81         st_buf->st_gid = st64_buf->st_gid;
82         st_buf->st_rdev = st64_buf->st_rdev;
83         st_buf->st_size = st64_buf->st_size;
84         st_buf->st_blksize = st64_buf->st_blksize;
85         st_buf->st_blocks = st64_buf->st_blocks;
86         st_buf->st_atime = st64_buf->st_atime;
87         st_buf->st_mtime = st64_buf->st_mtime;
88         st_buf->st_ctime = st64_buf->st_ctime;
89 }
90 #endif
91
92 int
93 PREPEND(__, SYSIO_INTERFACE_NAME(fxstat))(int __ver, 
94                                           int __fildes, 
95                                           struct stat *__stat_buf)
96 {
97         struct file *fil;
98         int     err;
99         struct intnl_stat *buf;
100 #ifdef _LARGEFILE64_SOURCE
101         struct stat64 st64;
102 #endif
103         SYSIO_INTERFACE_DISPLAY_BLOCK;
104
105         SYSIO_INTERFACE_ENTER;
106         if (__ver != _STAT_VER) {
107                 err = -ENOSYS;
108                 goto out;
109         }
110
111         err = 0;
112         fil = _sysio_fd_find(__fildes);
113         if (!fil) {
114                 err = -EBADF;
115                 goto out;
116         }
117 #ifdef _LARGEFILE64_SOURCE
118         buf = &st64;
119 #else
120         buf = __stat_buf;
121 #endif
122         /*
123          * Never use the attributes cached in the inode record. Give the
124          * driver a chance to refresh them.
125          */
126         err =
127             fil->f_ino->i_ops.inop_getattr(NULL, fil->f_ino, buf);
128 #ifdef _LARGEFILE64_SOURCE
129         if (!err)
130                 convstat(buf, __stat_buf);
131 #endif
132 out:
133         SYSIO_INTERFACE_RETURN(err ? -1 : 0, err);
134 }
135
136 #ifdef REDSTORM
137 #undef _fxstat
138 sysio_sym_weak_alias(PREPEND(__, SYSIO_INTERFACE_NAME(fxstat)), 
139                      PREPEND(_, SYSIO_INTERFACE_NAME(fxstat)))
140 #endif
141
142 #ifndef REDSTORM
143 static int
144 PREPEND(__, SYSIO_INTERFACE_NAME(fstat))(int fd, struct stat *buf)
145 {
146
147         return PREPEND(__, SYSIO_INTERFACE_NAME(fxstat))(_STAT_VER, 
148                                                          fd, 
149                                                          buf);
150 }
151
152 sysio_sym_weak_alias(PREPEND(__, SYSIO_INTERFACE_NAME(fstat)), 
153                      SYSIO_INTERFACE_NAME(fstat))
154
155 #ifdef BSD
156 #undef _fstat
157 sysio_sym_weak_alias(PREPEND(__, SYSIO_INTERFACE_NAME(fstat)),
158                      PREPEND(_, SYSIO_INTERFACE_NAME(fstat)))
159 #endif
160 #endif
161
162 int
163 PREPEND(__, SYSIO_INTERFACE_NAME(xstat))(int __ver, 
164                                          const char *__filename, 
165                                          struct stat *__stat_buf)
166 {
167         struct intent intent;
168         int     err;
169         struct pnode *pno;
170         struct inode *ino;
171         SYSIO_INTERFACE_DISPLAY_BLOCK;
172
173         SYSIO_INTERFACE_ENTER;
174         if (__ver != _STAT_VER) {
175                 err = -ENOSYS;
176                 goto out;
177         }
178
179         INTENT_INIT(&intent, INT_GETATTR, NULL, NULL);
180         err = _sysio_namei(_sysio_cwd, __filename, 0, &intent, &pno);
181         if (err)
182                 goto out;
183         /*
184          * Leverage the INT_GETATTR intent above. We are counting
185          * on the FS driver to either make sure the attributes cached in
186          * the inode are always correct or refresh them in the lookup, above.
187          */
188         ino = pno->p_base->pb_ino;
189 #ifdef _LARGEFILE64_SOURCE
190         convstat(&ino->i_stbuf, __stat_buf);
191 #else
192         (void )memcpy(__stat_buf, &ino->i_stbuf, sizeof(struct intnl_stat));
193 #endif
194         P_RELE(pno);
195 out:
196         SYSIO_INTERFACE_RETURN(err ? -1 : 0, err);
197 }
198
199 #ifdef REDSTORM
200 #undef _xstat
201 sysio_sym_weak_alias(PREPEND(__, SYSIO_INTERFACE_NAME(xstat)),
202                      PREPEND(_, SYSIO_INTERFACE_NAME(xstat)))
203 #endif
204
205 #ifndef REDSTORM
206 static int
207 PREPEND(__, SYSIO_INTERFACE_NAME(stat))(const char *filename, 
208                                         struct stat *buf)
209 {
210
211         return PREPEND(__, SYSIO_INTERFACE_NAME(xstat))(_STAT_VER, 
212                                                         filename,
213                                                         buf);
214 }
215
216 sysio_sym_weak_alias(PREPEND(__, SYSIO_INTERFACE_NAME(stat)),
217                      SYSIO_INTERFACE_NAME(stat))
218
219 #ifdef BSD
220 #undef _stat
221 sysio_sym_weak_alias(PREPEND(__, SYSIO_INTERFACE_NAME(stat)),
222                      PREPEND(_, SYSIO_INTERFACE_NAME(stat)))
223 #endif
224 #endif
225
226 int
227 PREPEND(__, SYSIO_INTERFACE_NAME(lxstat))(int __ver, 
228                                           const char *__filename, 
229                                           struct stat *__stat_buf)
230 {
231         struct intent intent;
232         int     err;
233         struct pnode *pno;
234         struct inode *ino;
235         SYSIO_INTERFACE_DISPLAY_BLOCK;
236
237         SYSIO_INTERFACE_ENTER;
238         if (__ver != _STAT_VER) {
239                 err = -ENOSYS;
240                 goto out;
241         }
242
243         INTENT_INIT(&intent, INT_GETATTR, NULL, NULL);
244         err = _sysio_namei(_sysio_cwd, __filename, ND_NOFOLLOW, &intent, &pno);
245         if (err)
246                 goto out;
247         /*
248          * Leverage the INT_GETATTR intent above. We are counting
249          * on the FS driver to either make sure the attributes cached in
250          * the inode are always correct or refresh them in the lookup, above.
251          */
252         ino = pno->p_base->pb_ino;
253 #ifdef _LARGEFILE64_SOURCE
254         convstat(&ino->i_stbuf, __stat_buf);
255 #else
256         (void )memcpy(__stat_buf, &ino->i_stbuf, sizeof(struct intnl_stat));
257 #endif
258         P_RELE(pno);
259 out:
260         SYSIO_INTERFACE_RETURN(err ? -1 : 0, err);
261 }
262
263 #ifdef REDSTORM
264 #undef _lxstat
265 sysio_sym_weak_alias(PREPEND(__, SYSIO_INTERFACE_NAME(lxstat)),
266                      PREPEND(_, SYSIO_INTERFACE_NAME(lxstat)))
267 #endif
268
269 #ifndef REDSTORM
270 static int
271 PREPEND(__, SYSIO_INTERFACE_NAME(lstat))(const char *filename, struct stat *buf)
272 {
273         return PREPEND(__, SYSIO_INTERFACE_NAME(lxstat))(_STAT_VER, 
274                                                          filename,
275                                                          buf);
276 }
277
278 sysio_sym_weak_alias(PREPEND(__, SYSIO_INTERFACE_NAME(lstat)),
279                      SYSIO_INTERFACE_NAME(lstat))
280
281 #ifdef BSD
282 #undef _lstat
283 sysio_sym_weak_alias(PREPEND(__, SYSIO_INTERFACE_NAME(lstat)),
284                      PREPEND(_, SYSIO_INTERFACE_NAME(lstat)))
285 #endif
286 #endif