vfs_syscalls_12.c revision 1.13 1 /* $NetBSD: vfs_syscalls_12.c,v 1.13 2003/06/29 22:29:14 fvdl Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * From: @(#)vfs_syscalls.c 8.28 (Berkeley) 12/10/94
41 */
42
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: vfs_syscalls_12.c,v 1.13 2003/06/29 22:29:14 fvdl Exp $");
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/namei.h>
49 #include <sys/filedesc.h>
50 #include <sys/kernel.h>
51 #include <sys/file.h>
52 #include <sys/stat.h>
53 #include <sys/socketvar.h>
54 #include <sys/vnode.h>
55 #include <sys/mount.h>
56 #include <sys/proc.h>
57 #include <sys/uio.h>
58 #include <sys/malloc.h>
59 #include <sys/dirent.h>
60
61 #include <sys/sa.h>
62 #include <sys/syscallargs.h>
63
64 static void cvtstat __P((struct stat *, struct stat12 *));
65
66 /*
67 * Convert from a new to an old stat structure.
68 */
69 static void
70 cvtstat(st, ost)
71 struct stat *st;
72 struct stat12 *ost;
73 {
74
75 ost->st_dev = st->st_dev;
76 ost->st_ino = st->st_ino;
77 ost->st_mode = st->st_mode & 0xffff;
78 if (st->st_nlink >= (1 << 15))
79 ost->st_nlink = (1 << 15) - 1;
80 else
81 ost->st_nlink = st->st_nlink;
82 ost->st_uid = st->st_uid;
83 ost->st_gid = st->st_gid;
84 ost->st_rdev = st->st_rdev;
85 ost->st_atimespec = st->st_atimespec;
86 ost->st_mtimespec = st->st_mtimespec;
87 ost->st_ctimespec = st->st_ctimespec;
88 ost->st_size = st->st_size;
89 ost->st_blocks = st->st_blocks;
90 ost->st_blksize = st->st_blksize;
91 ost->st_flags = st->st_flags;
92 ost->st_gen = st->st_gen;
93 }
94
95 /*
96 * Read a block of directory entries in a file system independent format.
97 */
98 int
99 compat_12_sys_getdirentries(struct lwp *l, void *v, register_t *retval)
100 {
101 struct compat_12_sys_getdirentries_args /* {
102 syscallarg(int) fd;
103 syscallarg(char *) buf;
104 syscallarg(u_int) count;
105 syscallarg(long *) basep;
106 } */ *uap = v;
107 struct proc *p = l->l_proc;
108 struct file *fp;
109 int error, done;
110 long loff;
111
112 /* getvnode() will use the descriptor for us */
113 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
114 return error;
115 if ((fp->f_flag & FREAD) == 0) {
116 error = EBADF;
117 goto out;
118 }
119
120 loff = fp->f_offset;
121
122 error = vn_readdir(fp, SCARG(uap, buf), UIO_USERSPACE,
123 SCARG(uap, count), &done, p, 0, 0);
124
125 error = copyout(&loff, SCARG(uap, basep), sizeof(long));
126 *retval = done;
127 out:
128 FILE_UNUSE(fp, p);
129 return error;
130 }
131
132 /*
133 * Get file status; this version follows links.
134 */
135 /* ARGSUSED */
136 int
137 compat_12_sys_stat(struct lwp *l, void *v, register_t *retval)
138 {
139 struct compat_12_sys_stat_args /* {
140 syscallarg(const char *) path;
141 syscallarg(struct stat12 *) ub;
142 } */ *uap = v;
143 struct proc *p = l->l_proc;
144 struct stat sb;
145 struct stat12 osb;
146 int error;
147 struct nameidata nd;
148
149 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
150 SCARG(uap, path), p);
151 if ((error = namei(&nd)) != 0)
152 return (error);
153 error = vn_stat(nd.ni_vp, &sb, p);
154 vput(nd.ni_vp);
155 if (error)
156 return (error);
157 cvtstat(&sb, &osb);
158 error = copyout(&osb, SCARG(uap, ub), sizeof (osb));
159 return (error);
160 }
161
162
163 /*
164 * Get file status; this version does not follow links.
165 */
166 /* ARGSUSED */
167 int
168 compat_12_sys_lstat(struct lwp *l, void *v, register_t *retval)
169 {
170 struct compat_12_sys_lstat_args /* {
171 syscallarg(const char *) path;
172 syscallarg(struct stat12 *) ub;
173 } */ *uap = v;
174 struct proc *p = l->l_proc;
175 struct stat sb;
176 struct stat12 osb;
177 int error;
178 struct nameidata nd;
179
180 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
181 SCARG(uap, path), p);
182 if ((error = namei(&nd)) != 0)
183 return (error);
184 error = vn_stat(nd.ni_vp, &sb, p);
185 vput(nd.ni_vp);
186 if (error)
187 return (error);
188 cvtstat(&sb, &osb);
189 error = copyout(&osb, SCARG(uap, ub), sizeof (osb));
190 return (error);
191 }
192
193 /*
194 * Return status information about a file descriptor.
195 */
196 /* ARGSUSED */
197 int
198 compat_12_sys_fstat(struct lwp *l, void *v, register_t *retval)
199 {
200 struct compat_12_sys_fstat_args /* {
201 syscallarg(int) fd;
202 syscallarg(struct stat12 *) sb;
203 } */ *uap = v;
204 struct proc *p = l->l_proc;
205 int fd = SCARG(uap, fd);
206 struct filedesc *fdp = p->p_fd;
207 struct file *fp;
208 struct stat ub;
209 struct stat12 oub;
210 int error;
211
212 if ((fp = fd_getfile(fdp, fd)) == NULL)
213 return (EBADF);
214
215 FILE_USE(fp);
216 error = (*fp->f_ops->fo_stat)(fp, &ub, p);
217 FILE_UNUSE(fp, p);
218
219 if (error == 0) {
220 cvtstat(&ub, &oub);
221 error = copyout(&oub, SCARG(uap, sb), sizeof (oub));
222 }
223 return (error);
224 }
225