vfs_syscalls_30.c revision 1.8 1 /* $NetBSD: vfs_syscalls_30.c,v 1.8 2006/05/04 17:48:57 christos Exp $ */
2
3 /*-
4 * Copyright (c) 2005 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: vfs_syscalls_30.c,v 1.8 2006/05/04 17:48:57 christos Exp $");
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/namei.h>
44 #include <sys/filedesc.h>
45 #include <sys/kernel.h>
46 #include <sys/file.h>
47 #include <sys/stat.h>
48 #include <sys/socketvar.h>
49 #include <sys/vnode.h>
50 #include <sys/mount.h>
51 #include <sys/proc.h>
52 #include <sys/uio.h>
53 #include <sys/dirent.h>
54 #include <sys/malloc.h>
55
56 #include <sys/sa.h>
57 #include <sys/syscallargs.h>
58
59 #include <compat/common/compat_util.h>
60 #include <compat/sys/stat.h>
61 #include <compat/sys/dirent.h>
62
63 static void cvtstat(struct stat13 *, const struct stat *);
64
65 /*
66 * Convert from a new to an old stat structure.
67 */
68 static void
69 cvtstat(struct stat13 *ost, const struct stat *st)
70 {
71
72 ost->st_dev = st->st_dev;
73 ost->st_ino = (uint32_t)st->st_ino;
74 ost->st_mode = st->st_mode;
75 ost->st_nlink = st->st_nlink;
76 ost->st_uid = st->st_uid;
77 ost->st_gid = st->st_gid;
78 ost->st_rdev = st->st_rdev;
79 ost->st_atimespec = st->st_atimespec;
80 ost->st_mtimespec = st->st_mtimespec;
81 ost->st_ctimespec = st->st_ctimespec;
82 ost->st_birthtimespec = st->st_birthtimespec;
83 ost->st_size = st->st_size;
84 ost->st_blocks = st->st_blocks;
85 ost->st_blksize = st->st_blksize;
86 ost->st_flags = st->st_flags;
87 ost->st_gen = st->st_gen;
88 }
89
90 /*
91 * Get file status; this version follows links.
92 */
93 /* ARGSUSED */
94 int
95 compat_30_sys___stat13(struct lwp *l, void *v, register_t *retval)
96 {
97 struct compat_30_sys___stat13_args /* {
98 syscallarg(const char *) path;
99 syscallarg(struct stat13 *) ub;
100 } */ *uap = v;
101 struct stat sb;
102 struct stat13 osb;
103 int error;
104 struct nameidata nd;
105
106 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
107 SCARG(uap, path), l);
108 if ((error = namei(&nd)) != 0)
109 return error;
110 error = vn_stat(nd.ni_vp, &sb, l);
111 vput(nd.ni_vp);
112 if (error)
113 return error;
114 cvtstat(&osb, &sb);
115 error = copyout(&osb, SCARG(uap, ub), sizeof (osb));
116 return error;
117 }
118
119
120 /*
121 * Get file status; this version does not follow links.
122 */
123 /* ARGSUSED */
124 int
125 compat_30_sys___lstat13(struct lwp *l, void *v, register_t *retval)
126 {
127 struct compat_30_sys___lstat13_args /* {
128 syscallarg(const char *) path;
129 syscallarg(struct stat13 *) ub;
130 } */ *uap = v;
131 struct stat sb;
132 struct stat13 osb;
133 int error;
134 struct nameidata nd;
135
136 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
137 SCARG(uap, path), l);
138 if ((error = namei(&nd)) != 0)
139 return error;
140 error = vn_stat(nd.ni_vp, &sb, l);
141 vput(nd.ni_vp);
142 if (error)
143 return error;
144 cvtstat(&osb, &sb);
145 error = copyout(&osb, SCARG(uap, ub), sizeof (osb));
146 return error;
147 }
148
149 /* ARGSUSED */
150 int
151 compat_30_sys_fhstat(struct lwp *l, void *v, register_t *retval)
152 {
153 struct compat_30_sys_fhstat_args /* {
154 syscallarg(const fhandle_t *) fhp;
155 syscallarg(struct stat13 *) sb;
156 } */ *uap = v;
157 struct proc *p = l->l_proc;
158 struct stat sb;
159 struct stat13 osb;
160 int error;
161 fhandle_t fh;
162 struct mount *mp;
163 struct vnode *vp;
164
165 /*
166 * Must be super user
167 */
168 if ((error = suser(p->p_ucred, &p->p_acflag)))
169 return (error);
170
171 if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fhandle_t))) != 0)
172 return (error);
173
174 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
175 return (ESTALE);
176 if (mp->mnt_op->vfs_fhtovp == NULL)
177 return EOPNOTSUPP;
178 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
179 return (error);
180 error = vn_stat(vp, &sb, l);
181 vput(vp);
182 if (error)
183 return (error);
184 cvtstat(&osb, &sb);
185 error = copyout(&osb, SCARG(uap, sb), sizeof(sb));
186 return (error);
187 }
188
189 /*
190 * Return status information about a file descriptor.
191 */
192 /* ARGSUSED */
193 int
194 compat_30_sys___fstat13(struct lwp *l, void *v, register_t *retval)
195 {
196 struct compat_30_sys___fstat13_args /* {
197 syscallarg(int) fd;
198 syscallarg(struct stat13 *) sb;
199 } */ *uap = v;
200 struct proc *p = l->l_proc;
201 int fd = SCARG(uap, fd);
202 struct filedesc *fdp = p->p_fd;
203 struct file *fp;
204 struct stat sb;
205 struct stat13 osb;
206 int error;
207
208 if ((fp = fd_getfile(fdp, fd)) == NULL)
209 return EBADF;
210
211 FILE_USE(fp);
212 error = (*fp->f_ops->fo_stat)(fp, &sb, l);
213 FILE_UNUSE(fp, l);
214
215 if (error)
216 return error;
217 cvtstat(&osb, &sb);
218 error = copyout(&osb, SCARG(uap, sb), sizeof (osb));
219 return error;
220 }
221
222 /*
223 * Read a block of directory entries in a file system independent format.
224 */
225 int
226 compat_30_sys_getdents(struct lwp *l, void *v, register_t *retval)
227 {
228 struct compat_30_sys_getdents_args /* {
229 syscallarg(int) fd;
230 syscallarg(char *) buf;
231 syscallarg(size_t) count;
232 } */ *uap = v;
233 struct proc *p = l->l_proc;
234 struct dirent *bdp;
235 struct vnode *vp;
236 caddr_t inp, tbuf; /* BSD-format */
237 int len, reclen; /* BSD-format */
238 caddr_t outp; /* NetBSD-3.0-format */
239 int resid;
240 struct file *fp;
241 struct uio auio;
242 struct iovec aiov;
243 struct dirent12 idb;
244 off_t off; /* true file offset */
245 int buflen, error, eofflag;
246 off_t *cookiebuf = NULL, *cookie;
247 int ncookies;
248
249 /* getvnode() will use the descriptor for us */
250 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
251 return error;
252
253 if ((fp->f_flag & FREAD) == 0) {
254 error = EBADF;
255 goto out1;
256 }
257
258 vp = (struct vnode *)fp->f_data;
259 if (vp->v_type != VDIR) {
260 error = EINVAL;
261 goto out1;
262 }
263
264 buflen = min(MAXBSIZE, SCARG(uap, count));
265 tbuf = malloc(buflen, M_TEMP, M_WAITOK);
266 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
267 off = fp->f_offset;
268 again:
269 aiov.iov_base = tbuf;
270 aiov.iov_len = buflen;
271 auio.uio_iov = &aiov;
272 auio.uio_iovcnt = 1;
273 auio.uio_rw = UIO_READ;
274 auio.uio_resid = buflen;
275 auio.uio_offset = off;
276 UIO_SETUP_SYSSPACE(&auio);
277 /*
278 * First we read into the malloc'ed buffer, then
279 * we massage it into user space, one record at a time.
280 */
281 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
282 &ncookies);
283 if (error)
284 goto out;
285
286 inp = tbuf;
287 outp = SCARG(uap, buf);
288 resid = SCARG(uap, count);
289 if ((len = buflen - auio.uio_resid) == 0)
290 goto eof;
291
292 for (cookie = cookiebuf; len > 0; len -= reclen) {
293 bdp = (struct dirent *)inp;
294 reclen = bdp->d_reclen;
295 if (reclen & _DIRENT_ALIGN(bdp))
296 panic("netbsd30_getdents: bad reclen %d", reclen);
297 if (cookie)
298 off = *cookie++; /* each entry points to the next */
299 else
300 off += reclen;
301 if ((off >> 32) != 0) {
302 compat_offseterr(vp, "netbsd30_getdents");
303 error = EINVAL;
304 goto out;
305 }
306 if (bdp->d_namlen >= sizeof(idb.d_name))
307 idb.d_namlen = sizeof(idb.d_name) - 1;
308 else
309 idb.d_namlen = bdp->d_namlen;
310 idb.d_reclen = _DIRENT_SIZE(&idb);
311 if (reclen > len || resid < idb.d_reclen) {
312 /* entry too big for buffer, so just stop */
313 outp++;
314 break;
315 }
316 /*
317 * Massage in place to make a NetBSD-3.0-shaped dirent
318 * (otherwise we have to worry about touching user memory
319 * outside of the copyout() call).
320 */
321 idb.d_fileno = (u_int32_t)bdp->d_fileno;
322 idb.d_type = bdp->d_type;
323 (void)memcpy(idb.d_name, bdp->d_name, idb.d_namlen);
324 memset(idb.d_name + idb.d_namlen, 0,
325 idb.d_reclen - _DIRENT_NAMEOFF(&idb) - idb.d_namlen);
326 if ((error = copyout(&idb, outp, idb.d_reclen)) != 0)
327 goto out;
328 /* advance past this real entry */
329 inp += reclen;
330 /* advance output past NetBSD-3.0-shaped entry */
331 outp += idb.d_reclen;
332 resid -= idb.d_reclen;
333 }
334
335 /* if we squished out the whole block, try again */
336 if (outp == SCARG(uap, buf))
337 goto again;
338 fp->f_offset = off; /* update the vnode offset */
339
340 eof:
341 *retval = SCARG(uap, count) - resid;
342 out:
343 VOP_UNLOCK(vp, 0);
344 if (cookiebuf)
345 free(cookiebuf, M_TEMP);
346 free(tbuf, M_TEMP);
347 out1:
348 FILE_UNUSE(fp, l);
349 return error;
350 }
351