vfs_syscalls_12.c revision 1.10 1 /* $NetBSD: vfs_syscalls_12.c,v 1.10 2001/11/13 02:08:04 lukem 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.10 2001/11/13 02:08:04 lukem 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/syscallargs.h>
62
63 static void cvtstat __P((struct stat *, struct stat12 *));
64
65 /*
66 * Convert from a new to an old stat structure.
67 */
68 static void
69 cvtstat(st, ost)
70 struct stat *st;
71 struct stat12 *ost;
72 {
73
74 ost->st_dev = st->st_dev;
75 ost->st_ino = st->st_ino;
76 ost->st_mode = st->st_mode & 0xffff;
77 if (st->st_nlink >= (1 << 15))
78 ost->st_nlink = (1 << 15) - 1;
79 else
80 ost->st_nlink = st->st_nlink;
81 ost->st_uid = st->st_uid;
82 ost->st_gid = st->st_gid;
83 ost->st_rdev = st->st_rdev;
84 ost->st_atimespec = st->st_atimespec;
85 ost->st_mtimespec = st->st_mtimespec;
86 ost->st_ctimespec = st->st_ctimespec;
87 ost->st_size = st->st_size;
88 ost->st_blocks = st->st_blocks;
89 ost->st_blksize = st->st_blksize;
90 ost->st_flags = st->st_flags;
91 ost->st_gen = st->st_gen;
92 }
93
94 /*
95 * Read a block of directory entries in a file system independent format.
96 */
97 int
98 compat_12_sys_getdirentries(p, v, retval)
99 struct proc *p;
100 void *v;
101 register_t *retval;
102 {
103 struct compat_12_sys_getdirentries_args /* {
104 syscallarg(int) fd;
105 syscallarg(char *) buf;
106 syscallarg(u_int) count;
107 syscallarg(long *) basep;
108 } */ *uap = v;
109 struct file *fp;
110 int error, done;
111 long loff;
112
113 /* getvnode() will use the descriptor for us */
114 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
115 return error;
116 if ((fp->f_flag & FREAD) == 0) {
117 error = EBADF;
118 goto out;
119 }
120
121 loff = fp->f_offset;
122
123 error = vn_readdir(fp, SCARG(uap, buf), UIO_USERSPACE,
124 SCARG(uap, count), &done, p, 0, 0);
125
126 error = copyout(&loff, SCARG(uap, basep), sizeof(long));
127 *retval = done;
128 out:
129 FILE_UNUSE(fp, p);
130 return error;
131 }
132
133 /*
134 * Get file status; this version follows links.
135 */
136 /* ARGSUSED */
137 int
138 compat_12_sys_stat(p, v, retval)
139 struct proc *p;
140 void *v;
141 register_t *retval;
142 {
143 struct compat_12_sys_stat_args /* {
144 syscallarg(const char *) path;
145 syscallarg(struct stat12 *) ub;
146 } */ *uap = v;
147 struct stat sb;
148 struct stat12 osb;
149 int error;
150 struct nameidata nd;
151
152 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
153 SCARG(uap, path), p);
154 if ((error = namei(&nd)) != 0)
155 return (error);
156 error = vn_stat(nd.ni_vp, &sb, p);
157 vput(nd.ni_vp);
158 if (error)
159 return (error);
160 cvtstat(&sb, &osb);
161 error = copyout(&osb, SCARG(uap, ub), sizeof (osb));
162 return (error);
163 }
164
165
166 /*
167 * Get file status; this version does not follow links.
168 */
169 /* ARGSUSED */
170 int
171 compat_12_sys_lstat(p, v, retval)
172 struct proc *p;
173 void *v;
174 register_t *retval;
175 {
176 struct compat_12_sys_lstat_args /* {
177 syscallarg(const char *) path;
178 syscallarg(struct stat12 *) ub;
179 } */ *uap = v;
180 struct stat sb;
181 struct stat12 osb;
182 int error;
183 struct nameidata nd;
184
185 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
186 SCARG(uap, path), p);
187 if ((error = namei(&nd)) != 0)
188 return (error);
189 error = vn_stat(nd.ni_vp, &sb, p);
190 vput(nd.ni_vp);
191 if (error)
192 return (error);
193 cvtstat(&sb, &osb);
194 error = copyout(&osb, SCARG(uap, ub), sizeof (osb));
195 return (error);
196 }
197
198 /*
199 * Return status information about a file descriptor.
200 */
201 /* ARGSUSED */
202 int
203 compat_12_sys_fstat(p, v, retval)
204 struct proc *p;
205 void *v;
206 register_t *retval;
207 {
208 struct compat_12_sys_fstat_args /* {
209 syscallarg(int) fd;
210 syscallarg(struct stat12 *) sb;
211 } */ *uap = v;
212 int fd = SCARG(uap, fd);
213 struct filedesc *fdp = p->p_fd;
214 struct file *fp;
215 struct stat ub;
216 struct stat12 oub;
217 int error;
218
219 if ((fp = fd_getfile(fdp, fd)) == NULL)
220 return (EBADF);
221
222 FILE_USE(fp);
223 error = (*fp->f_ops->fo_stat)(fp, &ub, p);
224 FILE_UNUSE(fp, p);
225
226 if (error == 0) {
227 cvtstat(&ub, &oub);
228 error = copyout(&oub, SCARG(uap, sb), sizeof (oub));
229 }
230 return (error);
231 }
232