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