vfs_syscalls_43.c revision 1.9 1 1.9 thorpej /* $NetBSD: vfs_syscalls_43.c,v 1.9 1998/02/19 00:35:26 thorpej Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.1 christos * Copyright (c) 1989, 1993
5 1.1 christos * The Regents of the University of California. All rights reserved.
6 1.1 christos * (c) UNIX System Laboratories, Inc.
7 1.1 christos * All or some portions of this file are derived from material licensed
8 1.1 christos * to the University of California by American Telephone and Telegraph
9 1.1 christos * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 1.1 christos * the permission of UNIX System Laboratories, Inc.
11 1.1 christos *
12 1.1 christos * Redistribution and use in source and binary forms, with or without
13 1.1 christos * modification, are permitted provided that the following conditions
14 1.1 christos * are met:
15 1.1 christos * 1. Redistributions of source code must retain the above copyright
16 1.1 christos * notice, this list of conditions and the following disclaimer.
17 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
18 1.1 christos * notice, this list of conditions and the following disclaimer in the
19 1.1 christos * documentation and/or other materials provided with the distribution.
20 1.1 christos * 3. All advertising materials mentioning features or use of this software
21 1.1 christos * must display the following acknowledgement:
22 1.1 christos * This product includes software developed by the University of
23 1.1 christos * California, Berkeley and its contributors.
24 1.1 christos * 4. Neither the name of the University nor the names of its contributors
25 1.1 christos * may be used to endorse or promote products derived from this software
26 1.1 christos * without specific prior written permission.
27 1.1 christos *
28 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 1.1 christos * SUCH DAMAGE.
39 1.1 christos *
40 1.1 christos * @(#)vfs_syscalls.c 8.28 (Berkeley) 12/10/94
41 1.1 christos */
42 1.9 thorpej
43 1.9 thorpej #include "fs_union.h"
44 1.1 christos
45 1.1 christos #include <sys/param.h>
46 1.1 christos #include <sys/systm.h>
47 1.1 christos #include <sys/filedesc.h>
48 1.1 christos #include <sys/kernel.h>
49 1.1 christos #include <sys/proc.h>
50 1.1 christos #include <sys/file.h>
51 1.1 christos #include <sys/vnode.h>
52 1.1 christos #include <sys/namei.h>
53 1.1 christos #include <sys/dirent.h>
54 1.1 christos #include <sys/socket.h>
55 1.1 christos #include <sys/socketvar.h>
56 1.1 christos #include <sys/stat.h>
57 1.1 christos #include <sys/ioctl.h>
58 1.1 christos #include <sys/fcntl.h>
59 1.1 christos #include <sys/malloc.h>
60 1.1 christos #include <sys/syslog.h>
61 1.1 christos #include <sys/unistd.h>
62 1.1 christos #include <sys/resourcevar.h>
63 1.1 christos
64 1.1 christos #include <sys/mount.h>
65 1.1 christos #include <sys/syscallargs.h>
66 1.1 christos
67 1.8 christos static void cvtstat __P((struct stat *, struct stat43 *));
68 1.4 christos
69 1.1 christos /*
70 1.1 christos * Convert from an old to a new stat structure.
71 1.1 christos */
72 1.4 christos static void
73 1.1 christos cvtstat(st, ost)
74 1.1 christos struct stat *st;
75 1.8 christos struct stat43 *ost;
76 1.1 christos {
77 1.1 christos
78 1.1 christos ost->st_dev = st->st_dev;
79 1.1 christos ost->st_ino = st->st_ino;
80 1.1 christos ost->st_mode = st->st_mode;
81 1.1 christos ost->st_nlink = st->st_nlink;
82 1.1 christos ost->st_uid = st->st_uid;
83 1.1 christos ost->st_gid = st->st_gid;
84 1.1 christos ost->st_rdev = st->st_rdev;
85 1.1 christos if (st->st_size < (quad_t)1 << 32)
86 1.1 christos ost->st_size = st->st_size;
87 1.1 christos else
88 1.1 christos ost->st_size = -2;
89 1.1 christos ost->st_atime = st->st_atime;
90 1.1 christos ost->st_mtime = st->st_mtime;
91 1.1 christos ost->st_ctime = st->st_ctime;
92 1.1 christos ost->st_blksize = st->st_blksize;
93 1.1 christos ost->st_blocks = st->st_blocks;
94 1.1 christos ost->st_flags = st->st_flags;
95 1.1 christos ost->st_gen = st->st_gen;
96 1.1 christos }
97 1.1 christos
98 1.1 christos /*
99 1.1 christos * Get file status; this version follows links.
100 1.1 christos */
101 1.1 christos /* ARGSUSED */
102 1.1 christos int
103 1.3 mycroft compat_43_sys_stat(p, v, retval)
104 1.1 christos struct proc *p;
105 1.2 thorpej void *v;
106 1.2 thorpej register_t *retval;
107 1.2 thorpej {
108 1.3 mycroft register struct compat_43_sys_stat_args /* {
109 1.1 christos syscallarg(char *) path;
110 1.8 christos syscallarg(struct stat43 *) ub;
111 1.2 thorpej } */ *uap = v;
112 1.1 christos struct stat sb;
113 1.8 christos struct stat43 osb;
114 1.1 christos int error;
115 1.1 christos struct nameidata nd;
116 1.1 christos
117 1.1 christos NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
118 1.1 christos SCARG(uap, path), p);
119 1.4 christos if ((error = namei(&nd)) != 0)
120 1.1 christos return (error);
121 1.1 christos error = vn_stat(nd.ni_vp, &sb, p);
122 1.1 christos vput(nd.ni_vp);
123 1.1 christos if (error)
124 1.1 christos return (error);
125 1.1 christos cvtstat(&sb, &osb);
126 1.1 christos error = copyout((caddr_t)&osb, (caddr_t)SCARG(uap, ub), sizeof (osb));
127 1.1 christos return (error);
128 1.1 christos }
129 1.1 christos
130 1.1 christos
131 1.1 christos /*
132 1.1 christos * Get file status; this version does not follow links.
133 1.1 christos */
134 1.1 christos /* ARGSUSED */
135 1.1 christos int
136 1.3 mycroft compat_43_sys_lstat(p, v, retval)
137 1.1 christos struct proc *p;
138 1.2 thorpej void *v;
139 1.2 thorpej register_t *retval;
140 1.2 thorpej {
141 1.3 mycroft register struct compat_43_sys_lstat_args /* {
142 1.1 christos syscallarg(char *) path;
143 1.8 christos syscallarg(struct stat43 *) ub;
144 1.2 thorpej } */ *uap = v;
145 1.5 christos struct stat sb;
146 1.8 christos struct stat43 osb;
147 1.1 christos int error;
148 1.1 christos struct nameidata nd;
149 1.1 christos
150 1.5 christos NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
151 1.1 christos SCARG(uap, path), p);
152 1.4 christos if ((error = namei(&nd)) != 0)
153 1.1 christos return (error);
154 1.5 christos error = vn_stat(nd.ni_vp, &sb, p);
155 1.5 christos vput(nd.ni_vp);
156 1.5 christos if (error)
157 1.5 christos return (error);
158 1.1 christos cvtstat(&sb, &osb);
159 1.5 christos error = copyout(&osb, SCARG(uap, ub), sizeof (osb));
160 1.1 christos return (error);
161 1.1 christos }
162 1.1 christos
163 1.1 christos
164 1.1 christos /*
165 1.1 christos * Return status information about a file descriptor.
166 1.1 christos */
167 1.1 christos /* ARGSUSED */
168 1.4 christos int
169 1.3 mycroft compat_43_sys_fstat(p, v, retval)
170 1.1 christos struct proc *p;
171 1.2 thorpej void *v;
172 1.2 thorpej register_t *retval;
173 1.2 thorpej {
174 1.3 mycroft register struct compat_43_sys_fstat_args /* {
175 1.1 christos syscallarg(int) fd;
176 1.8 christos syscallarg(struct stat43 *) sb;
177 1.2 thorpej } */ *uap = v;
178 1.1 christos int fd = SCARG(uap, fd);
179 1.1 christos register struct filedesc *fdp = p->p_fd;
180 1.1 christos register struct file *fp;
181 1.1 christos struct stat ub;
182 1.8 christos struct stat43 oub;
183 1.1 christos int error;
184 1.1 christos
185 1.1 christos if ((u_int)fd >= fdp->fd_nfiles ||
186 1.1 christos (fp = fdp->fd_ofiles[fd]) == NULL)
187 1.1 christos return (EBADF);
188 1.1 christos switch (fp->f_type) {
189 1.1 christos
190 1.1 christos case DTYPE_VNODE:
191 1.1 christos error = vn_stat((struct vnode *)fp->f_data, &ub, p);
192 1.1 christos break;
193 1.1 christos
194 1.1 christos case DTYPE_SOCKET:
195 1.1 christos error = soo_stat((struct socket *)fp->f_data, &ub);
196 1.1 christos break;
197 1.1 christos
198 1.1 christos default:
199 1.1 christos panic("ofstat");
200 1.1 christos /*NOTREACHED*/
201 1.1 christos }
202 1.1 christos cvtstat(&ub, &oub);
203 1.1 christos if (error == 0)
204 1.1 christos error = copyout((caddr_t)&oub, (caddr_t)SCARG(uap, sb),
205 1.1 christos sizeof (oub));
206 1.1 christos return (error);
207 1.1 christos }
208 1.1 christos
209 1.1 christos
210 1.1 christos /*
211 1.1 christos * Truncate a file given a file descriptor.
212 1.1 christos */
213 1.1 christos /* ARGSUSED */
214 1.1 christos int
215 1.3 mycroft compat_43_sys_ftruncate(p, v, retval)
216 1.1 christos struct proc *p;
217 1.2 thorpej void *v;
218 1.2 thorpej register_t *retval;
219 1.2 thorpej {
220 1.3 mycroft register struct compat_43_sys_ftruncate_args /* {
221 1.1 christos syscallarg(int) fd;
222 1.1 christos syscallarg(long) length;
223 1.2 thorpej } */ *uap = v;
224 1.3 mycroft struct sys_ftruncate_args /* {
225 1.1 christos syscallarg(int) fd;
226 1.1 christos syscallarg(int) pad;
227 1.1 christos syscallarg(off_t) length;
228 1.1 christos } */ nuap;
229 1.1 christos
230 1.1 christos SCARG(&nuap, fd) = SCARG(uap, fd);
231 1.1 christos SCARG(&nuap, length) = SCARG(uap, length);
232 1.3 mycroft return (sys_ftruncate(p, &nuap, retval));
233 1.1 christos }
234 1.1 christos
235 1.1 christos /*
236 1.1 christos * Truncate a file given its path name.
237 1.1 christos */
238 1.1 christos /* ARGSUSED */
239 1.1 christos int
240 1.3 mycroft compat_43_sys_truncate(p, v, retval)
241 1.1 christos struct proc *p;
242 1.2 thorpej void *v;
243 1.2 thorpej register_t *retval;
244 1.2 thorpej {
245 1.3 mycroft register struct compat_43_sys_truncate_args /* {
246 1.1 christos syscallarg(char *) path;
247 1.1 christos syscallarg(long) length;
248 1.2 thorpej } */ *uap = v;
249 1.3 mycroft struct sys_truncate_args /* {
250 1.1 christos syscallarg(char *) path;
251 1.1 christos syscallarg(int) pad;
252 1.1 christos syscallarg(off_t) length;
253 1.1 christos } */ nuap;
254 1.1 christos
255 1.1 christos SCARG(&nuap, path) = SCARG(uap, path);
256 1.1 christos SCARG(&nuap, length) = SCARG(uap, length);
257 1.3 mycroft return (sys_truncate(p, &nuap, retval));
258 1.1 christos }
259 1.1 christos
260 1.1 christos
261 1.1 christos /*
262 1.1 christos * Reposition read/write file offset.
263 1.1 christos */
264 1.1 christos int
265 1.3 mycroft compat_43_sys_lseek(p, v, retval)
266 1.1 christos struct proc *p;
267 1.2 thorpej void *v;
268 1.2 thorpej register_t *retval;
269 1.2 thorpej {
270 1.3 mycroft register struct compat_43_sys_lseek_args /* {
271 1.1 christos syscallarg(int) fd;
272 1.1 christos syscallarg(long) offset;
273 1.1 christos syscallarg(int) whence;
274 1.2 thorpej } */ *uap = v;
275 1.3 mycroft struct sys_lseek_args /* {
276 1.1 christos syscallarg(int) fd;
277 1.1 christos syscallarg(int) pad;
278 1.1 christos syscallarg(off_t) offset;
279 1.1 christos syscallarg(int) whence;
280 1.1 christos } */ nuap;
281 1.1 christos off_t qret;
282 1.1 christos int error;
283 1.1 christos
284 1.1 christos SCARG(&nuap, fd) = SCARG(uap, fd);
285 1.1 christos SCARG(&nuap, offset) = SCARG(uap, offset);
286 1.1 christos SCARG(&nuap, whence) = SCARG(uap, whence);
287 1.3 mycroft error = sys_lseek(p, &nuap, (register_t *)&qret);
288 1.1 christos *(long *)retval = qret;
289 1.1 christos return (error);
290 1.1 christos }
291 1.1 christos
292 1.1 christos
293 1.1 christos /*
294 1.1 christos * Create a file.
295 1.1 christos */
296 1.1 christos int
297 1.3 mycroft compat_43_sys_creat(p, v, retval)
298 1.1 christos struct proc *p;
299 1.2 thorpej void *v;
300 1.2 thorpej register_t *retval;
301 1.2 thorpej {
302 1.3 mycroft register struct compat_43_sys_creat_args /* {
303 1.1 christos syscallarg(char *) path;
304 1.1 christos syscallarg(int) mode;
305 1.2 thorpej } */ *uap = v;
306 1.3 mycroft struct sys_open_args /* {
307 1.1 christos syscallarg(char *) path;
308 1.1 christos syscallarg(int) flags;
309 1.1 christos syscallarg(int) mode;
310 1.1 christos } */ nuap;
311 1.1 christos
312 1.1 christos SCARG(&nuap, path) = SCARG(uap, path);
313 1.1 christos SCARG(&nuap, mode) = SCARG(uap, mode);
314 1.1 christos SCARG(&nuap, flags) = O_WRONLY | O_CREAT | O_TRUNC;
315 1.3 mycroft return (sys_open(p, &nuap, retval));
316 1.1 christos }
317 1.1 christos
318 1.1 christos /*ARGSUSED*/
319 1.1 christos int
320 1.3 mycroft compat_43_sys_quota(p, v, retval)
321 1.1 christos struct proc *p;
322 1.3 mycroft void *v;
323 1.1 christos register_t *retval;
324 1.1 christos {
325 1.1 christos
326 1.1 christos return (ENOSYS);
327 1.1 christos }
328 1.1 christos
329 1.1 christos
330 1.1 christos /*
331 1.1 christos * Read a block of directory entries in a file system independent format.
332 1.1 christos */
333 1.1 christos int
334 1.3 mycroft compat_43_sys_getdirentries(p, v, retval)
335 1.1 christos struct proc *p;
336 1.2 thorpej void *v;
337 1.2 thorpej register_t *retval;
338 1.2 thorpej {
339 1.3 mycroft register struct compat_43_sys_getdirentries_args /* {
340 1.1 christos syscallarg(int) fd;
341 1.1 christos syscallarg(char *) buf;
342 1.1 christos syscallarg(u_int) count;
343 1.1 christos syscallarg(long *) basep;
344 1.2 thorpej } */ *uap = v;
345 1.1 christos register struct vnode *vp;
346 1.1 christos struct file *fp;
347 1.1 christos struct uio auio, kuio;
348 1.1 christos struct iovec aiov, kiov;
349 1.1 christos struct dirent *dp, *edp;
350 1.1 christos caddr_t dirbuf;
351 1.1 christos int error, eofflag, readcnt;
352 1.1 christos long loff;
353 1.1 christos
354 1.4 christos if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
355 1.1 christos return (error);
356 1.1 christos if ((fp->f_flag & FREAD) == 0)
357 1.1 christos return (EBADF);
358 1.1 christos vp = (struct vnode *)fp->f_data;
359 1.1 christos unionread:
360 1.1 christos if (vp->v_type != VDIR)
361 1.1 christos return (EINVAL);
362 1.1 christos aiov.iov_base = SCARG(uap, buf);
363 1.1 christos aiov.iov_len = SCARG(uap, count);
364 1.1 christos auio.uio_iov = &aiov;
365 1.1 christos auio.uio_iovcnt = 1;
366 1.1 christos auio.uio_rw = UIO_READ;
367 1.1 christos auio.uio_segflg = UIO_USERSPACE;
368 1.1 christos auio.uio_procp = p;
369 1.1 christos auio.uio_resid = SCARG(uap, count);
370 1.1 christos VOP_LOCK(vp);
371 1.1 christos loff = auio.uio_offset = fp->f_offset;
372 1.1 christos # if (BYTE_ORDER != LITTLE_ENDIAN)
373 1.1 christos if (vp->v_mount->mnt_maxsymlinklen <= 0) {
374 1.1 christos error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
375 1.7 fvdl (off_t *)0, 0);
376 1.1 christos fp->f_offset = auio.uio_offset;
377 1.1 christos } else
378 1.1 christos # endif
379 1.1 christos {
380 1.1 christos kuio = auio;
381 1.1 christos kuio.uio_iov = &kiov;
382 1.1 christos kuio.uio_segflg = UIO_SYSSPACE;
383 1.1 christos kiov.iov_len = SCARG(uap, count);
384 1.1 christos MALLOC(dirbuf, caddr_t, SCARG(uap, count), M_TEMP, M_WAITOK);
385 1.1 christos kiov.iov_base = dirbuf;
386 1.1 christos error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
387 1.6 fvdl (off_t *)0, 0);
388 1.1 christos fp->f_offset = kuio.uio_offset;
389 1.1 christos if (error == 0) {
390 1.1 christos readcnt = SCARG(uap, count) - kuio.uio_resid;
391 1.1 christos edp = (struct dirent *)&dirbuf[readcnt];
392 1.1 christos for (dp = (struct dirent *)dirbuf; dp < edp; ) {
393 1.1 christos # if (BYTE_ORDER == LITTLE_ENDIAN)
394 1.1 christos /*
395 1.1 christos * The expected low byte of
396 1.1 christos * dp->d_namlen is our dp->d_type.
397 1.1 christos * The high MBZ byte of dp->d_namlen
398 1.1 christos * is our dp->d_namlen.
399 1.1 christos */
400 1.1 christos dp->d_type = dp->d_namlen;
401 1.1 christos dp->d_namlen = 0;
402 1.1 christos # else
403 1.1 christos /*
404 1.1 christos * The dp->d_type is the high byte
405 1.1 christos * of the expected dp->d_namlen,
406 1.1 christos * so must be zero'ed.
407 1.1 christos */
408 1.1 christos dp->d_type = 0;
409 1.1 christos # endif
410 1.1 christos if (dp->d_reclen > 0) {
411 1.1 christos dp = (struct dirent *)
412 1.1 christos ((char *)dp + dp->d_reclen);
413 1.1 christos } else {
414 1.1 christos error = EIO;
415 1.1 christos break;
416 1.1 christos }
417 1.1 christos }
418 1.1 christos if (dp >= edp)
419 1.1 christos error = uiomove(dirbuf, readcnt, &auio);
420 1.1 christos }
421 1.1 christos FREE(dirbuf, M_TEMP);
422 1.1 christos }
423 1.1 christos VOP_UNLOCK(vp);
424 1.1 christos if (error)
425 1.1 christos return (error);
426 1.1 christos
427 1.1 christos #ifdef UNION
428 1.1 christos {
429 1.4 christos extern int (**union_vnodeop_p) __P((void *));
430 1.1 christos extern struct vnode *union_dircache __P((struct vnode *));
431 1.1 christos
432 1.1 christos if ((SCARG(uap, count) == auio.uio_resid) &&
433 1.1 christos (vp->v_op == union_vnodeop_p)) {
434 1.1 christos struct vnode *lvp;
435 1.1 christos
436 1.1 christos lvp = union_dircache(vp);
437 1.1 christos if (lvp != NULLVP) {
438 1.1 christos struct vattr va;
439 1.1 christos
440 1.1 christos /*
441 1.1 christos * If the directory is opaque,
442 1.1 christos * then don't show lower entries
443 1.1 christos */
444 1.1 christos error = VOP_GETATTR(vp, &va, fp->f_cred, p);
445 1.1 christos if (va.va_flags & OPAQUE) {
446 1.1 christos vput(lvp);
447 1.1 christos lvp = NULL;
448 1.1 christos }
449 1.1 christos }
450 1.1 christos
451 1.1 christos if (lvp != NULLVP) {
452 1.1 christos error = VOP_OPEN(lvp, FREAD, fp->f_cred, p);
453 1.1 christos VOP_UNLOCK(lvp);
454 1.1 christos
455 1.1 christos if (error) {
456 1.1 christos vrele(lvp);
457 1.1 christos return (error);
458 1.1 christos }
459 1.1 christos fp->f_data = (caddr_t) lvp;
460 1.1 christos fp->f_offset = 0;
461 1.1 christos error = vn_close(vp, FREAD, fp->f_cred, p);
462 1.1 christos if (error)
463 1.1 christos return (error);
464 1.1 christos vp = lvp;
465 1.1 christos goto unionread;
466 1.1 christos }
467 1.1 christos }
468 1.1 christos }
469 1.1 christos #endif /* UNION */
470 1.1 christos
471 1.1 christos if ((SCARG(uap, count) == auio.uio_resid) &&
472 1.1 christos (vp->v_flag & VROOT) &&
473 1.1 christos (vp->v_mount->mnt_flag & MNT_UNION)) {
474 1.1 christos struct vnode *tvp = vp;
475 1.1 christos vp = vp->v_mount->mnt_vnodecovered;
476 1.1 christos VREF(vp);
477 1.1 christos fp->f_data = (caddr_t) vp;
478 1.1 christos fp->f_offset = 0;
479 1.1 christos vrele(tvp);
480 1.1 christos goto unionread;
481 1.1 christos }
482 1.1 christos error = copyout((caddr_t)&loff, (caddr_t)SCARG(uap, basep),
483 1.1 christos sizeof(long));
484 1.1 christos *retval = SCARG(uap, count) - auio.uio_resid;
485 1.1 christos return (error);
486 1.1 christos }
487