xref: /netbsd-src/sys/compat/common/vfs_syscalls_12.c (revision 3b01aba77a7a698587faaae455bbfe740923c1f5)
1 /*	$NetBSD: vfs_syscalls_12.c,v 1.9 2001/06/14 20:32:41 thorpej 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/param.h>
44 #include <sys/systm.h>
45 #include <sys/namei.h>
46 #include <sys/filedesc.h>
47 #include <sys/kernel.h>
48 #include <sys/file.h>
49 #include <sys/stat.h>
50 #include <sys/socketvar.h>
51 #include <sys/vnode.h>
52 #include <sys/mount.h>
53 #include <sys/proc.h>
54 #include <sys/uio.h>
55 #include <sys/malloc.h>
56 #include <sys/dirent.h>
57 
58 #include <sys/syscallargs.h>
59 
60 static void cvtstat __P((struct stat *, struct stat12 *));
61 
62 /*
63  * Convert from a new to an old stat structure.
64  */
65 static void
66 cvtstat(st, ost)
67 	struct stat *st;
68 	struct stat12 *ost;
69 {
70 
71 	ost->st_dev = st->st_dev;
72 	ost->st_ino = st->st_ino;
73 	ost->st_mode = st->st_mode & 0xffff;
74 	if (st->st_nlink >= (1 << 15))
75 		ost->st_nlink = (1 << 15) - 1;
76 	else
77 		ost->st_nlink = st->st_nlink;
78 	ost->st_uid = st->st_uid;
79 	ost->st_gid = st->st_gid;
80 	ost->st_rdev = st->st_rdev;
81 	ost->st_atimespec = st->st_atimespec;
82 	ost->st_mtimespec = st->st_mtimespec;
83 	ost->st_ctimespec = st->st_ctimespec;
84 	ost->st_size = st->st_size;
85 	ost->st_blocks = st->st_blocks;
86 	ost->st_blksize = st->st_blksize;
87 	ost->st_flags = st->st_flags;
88 	ost->st_gen = st->st_gen;
89 }
90 
91 /*
92  * Read a block of directory entries in a file system independent format.
93  */
94 int
95 compat_12_sys_getdirentries(p, v, retval)
96 	struct proc *p;
97 	void *v;
98 	register_t *retval;
99 {
100 	struct compat_12_sys_getdirentries_args /* {
101 		syscallarg(int) fd;
102 		syscallarg(char *) buf;
103 		syscallarg(u_int) count;
104 		syscallarg(long *) basep;
105 	} */ *uap = v;
106 	struct file *fp;
107 	int error, done;
108 	long loff;
109 
110 	/* getvnode() will use the descriptor for us */
111 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
112 		return error;
113 	if ((fp->f_flag & FREAD) == 0) {
114 		error = EBADF;
115 		goto out;
116 	}
117 
118 	loff = fp->f_offset;
119 
120 	error = vn_readdir(fp, SCARG(uap, buf), UIO_USERSPACE,
121 			SCARG(uap, count), &done, p, 0, 0);
122 
123 	error = copyout(&loff, SCARG(uap, basep), sizeof(long));
124 	*retval = done;
125  out:
126 	FILE_UNUSE(fp, p);
127 	return error;
128 }
129 
130 /*
131  * Get file status; this version follows links.
132  */
133 /* ARGSUSED */
134 int
135 compat_12_sys_stat(p, v, retval)
136 	struct proc *p;
137 	void *v;
138 	register_t *retval;
139 {
140 	struct compat_12_sys_stat_args /* {
141 		syscallarg(const char *) path;
142 		syscallarg(struct stat12 *) ub;
143 	} */ *uap = v;
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(p, v, retval)
169 	struct proc *p;
170 	void *v;
171 	register_t *retval;
172 {
173 	struct compat_12_sys_lstat_args /* {
174 		syscallarg(const char *) path;
175 		syscallarg(struct stat12 *) ub;
176 	} */ *uap = v;
177 	struct stat sb;
178 	struct stat12 osb;
179 	int error;
180 	struct nameidata nd;
181 
182 	NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
183 	    SCARG(uap, path), p);
184 	if ((error = namei(&nd)) != 0)
185 		return (error);
186 	error = vn_stat(nd.ni_vp, &sb, p);
187 	vput(nd.ni_vp);
188 	if (error)
189 		return (error);
190 	cvtstat(&sb, &osb);
191 	error = copyout(&osb, SCARG(uap, ub), sizeof (osb));
192 	return (error);
193 }
194 
195 /*
196  * Return status information about a file descriptor.
197  */
198 /* ARGSUSED */
199 int
200 compat_12_sys_fstat(p, v, retval)
201 	struct proc *p;
202 	void *v;
203 	register_t *retval;
204 {
205 	struct compat_12_sys_fstat_args /* {
206 		syscallarg(int) fd;
207 		syscallarg(struct stat12 *) sb;
208 	} */ *uap = v;
209 	int fd = SCARG(uap, fd);
210 	struct filedesc *fdp = p->p_fd;
211 	struct file *fp;
212 	struct stat ub;
213 	struct stat12 oub;
214 	int error;
215 
216 	if ((fp = fd_getfile(fdp, fd)) == NULL)
217 		return (EBADF);
218 
219 	FILE_USE(fp);
220 	error = (*fp->f_ops->fo_stat)(fp, &ub, p);
221 	FILE_UNUSE(fp, p);
222 
223 	if (error == 0) {
224 		cvtstat(&ub, &oub);
225 		error = copyout(&oub, SCARG(uap, sb), sizeof (oub));
226 	}
227 	return (error);
228 }
229