xref: /netbsd-src/sys/compat/common/vfs_syscalls_43.c (revision de1dfb1250df962f1ff3a011772cf58e605aed11)
1 /*	$NetBSD: vfs_syscalls_43.c,v 1.28 2004/08/15 07:19:54 mycroft 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  *	@(#)vfs_syscalls.c	8.28 (Berkeley) 12/10/94
37  */
38 
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: vfs_syscalls_43.c,v 1.28 2004/08/15 07:19:54 mycroft Exp $");
41 
42 #if defined(_KERNEL_OPT)
43 #include "fs_union.h"
44 #endif
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/filedesc.h>
49 #include <sys/kernel.h>
50 #include <sys/proc.h>
51 #include <sys/file.h>
52 #include <sys/vnode.h>
53 #include <sys/namei.h>
54 #include <sys/dirent.h>
55 #include <sys/socket.h>
56 #include <sys/socketvar.h>
57 #include <sys/stat.h>
58 #include <sys/malloc.h>
59 #include <sys/ioctl.h>
60 #include <sys/fcntl.h>
61 #include <sys/syslog.h>
62 #include <sys/unistd.h>
63 #include <sys/resourcevar.h>
64 
65 #include <sys/mount.h>
66 #include <sys/sa.h>
67 #include <sys/syscallargs.h>
68 
69 static void cvtstat __P((struct stat *, struct stat43 *));
70 
71 /*
72  * Convert from an old to a new stat structure.
73  */
74 static void
75 cvtstat(st, ost)
76 	struct stat *st;
77 	struct stat43 *ost;
78 {
79 
80 	ost->st_dev = st->st_dev;
81 	ost->st_ino = st->st_ino;
82 	ost->st_mode = st->st_mode & 0xffff;
83 	ost->st_nlink = st->st_nlink;
84 	ost->st_uid = st->st_uid;
85 	ost->st_gid = st->st_gid;
86 	ost->st_rdev = st->st_rdev;
87 	if (st->st_size < (quad_t)1 << 32)
88 		ost->st_size = st->st_size;
89 	else
90 		ost->st_size = -2;
91 	ost->st_atime = st->st_atime;
92 	ost->st_mtime = st->st_mtime;
93 	ost->st_ctime = st->st_ctime;
94 	ost->st_blksize = st->st_blksize;
95 	ost->st_blocks = st->st_blocks;
96 	ost->st_flags = st->st_flags;
97 	ost->st_gen = st->st_gen;
98 }
99 
100 /*
101  * Get file status; this version follows links.
102  */
103 /* ARGSUSED */
104 int
105 compat_43_sys_stat(struct lwp *l, void *v, register_t *retval)
106 {
107 	struct compat_43_sys_stat_args /* {
108 		syscallarg(char *) path;
109 		syscallarg(struct stat43 *) ub;
110 	} */ *uap = v;
111 	struct proc *p = l->l_proc;
112 	struct stat sb;
113 	struct stat43 osb;
114 	int error;
115 	struct nameidata nd;
116 
117 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
118 	    SCARG(uap, path), p);
119 	if ((error = namei(&nd)) != 0)
120 		return (error);
121 	error = vn_stat(nd.ni_vp, &sb, p);
122 	vput(nd.ni_vp);
123 	if (error)
124 		return (error);
125 	cvtstat(&sb, &osb);
126 	error = copyout((caddr_t)&osb, (caddr_t)SCARG(uap, ub), sizeof (osb));
127 	return (error);
128 }
129 
130 /*
131  * Get file status; this version does not follow links.
132  */
133 /* ARGSUSED */
134 int
135 compat_43_sys_lstat(struct lwp *l, void *v, register_t *retval)
136 {
137 	struct compat_43_sys_lstat_args /* {
138 		syscallarg(char *) path;
139 		syscallarg(struct ostat *) ub;
140 	} */ *uap = v;
141 	struct proc *p = l->l_proc;
142 	struct vnode *vp, *dvp;
143 	struct stat sb, sb1;
144 	struct stat43 osb;
145 	int error;
146 	struct nameidata nd;
147 	int ndflags;
148 
149 	ndflags = NOFOLLOW | LOCKLEAF | LOCKPARENT;
150 again:
151 	NDINIT(&nd, LOOKUP, ndflags, UIO_USERSPACE, SCARG(uap, path), p);
152 	if ((error = namei(&nd))) {
153 		if (error == EISDIR && (ndflags & LOCKPARENT) != 0) {
154 			/*
155 			 * Should only happen on '/'. Retry without LOCKPARENT;
156 			 * this is safe since the vnode won't be a VLNK.
157 			 */
158 			ndflags &= ~LOCKPARENT;
159 			goto again;
160 		}
161 		return (error);
162 	}
163 	/*
164 	 * For symbolic links, always return the attributes of its
165 	 * containing directory, except for mode, size, and links.
166 	 */
167 	vp = nd.ni_vp;
168 	dvp = nd.ni_dvp;
169 	if (vp->v_type != VLNK) {
170 		if ((ndflags & LOCKPARENT) != 0) {
171 			if (dvp == vp)
172 				vrele(dvp);
173 			else
174 				vput(dvp);
175 		}
176 		error = vn_stat(vp, &sb, p);
177 		vput(vp);
178 		if (error)
179 			return (error);
180 	} else {
181 		error = vn_stat(dvp, &sb, p);
182 		vput(dvp);
183 		if (error) {
184 			vput(vp);
185 			return (error);
186 		}
187 		error = vn_stat(vp, &sb1, p);
188 		vput(vp);
189 		if (error)
190 			return (error);
191 		sb.st_mode &= ~S_IFDIR;
192 		sb.st_mode |= S_IFLNK;
193 		sb.st_nlink = sb1.st_nlink;
194 		sb.st_size = sb1.st_size;
195 		sb.st_blocks = sb1.st_blocks;
196 	}
197 	cvtstat(&sb, &osb);
198 	error = copyout((caddr_t)&osb, (caddr_t)SCARG(uap, ub), sizeof (osb));
199 	return (error);
200 }
201 
202 /*
203  * Return status information about a file descriptor.
204  */
205 /* ARGSUSED */
206 int
207 compat_43_sys_fstat(struct lwp *l, void *v, register_t *retval)
208 {
209 	struct compat_43_sys_fstat_args /* {
210 		syscallarg(int) fd;
211 		syscallarg(struct stat43 *) sb;
212 	} */ *uap = v;
213 	struct proc *p = l->l_proc;
214 	int fd = SCARG(uap, fd);
215 	struct filedesc *fdp = p->p_fd;
216 	struct file *fp;
217 	struct stat ub;
218 	struct stat43 oub;
219 	int error;
220 
221 	if ((fp = fd_getfile(fdp, fd)) == NULL)
222 		return (EBADF);
223 
224 	FILE_USE(fp);
225 	error = (*fp->f_ops->fo_stat)(fp, &ub, p);
226 	FILE_UNUSE(fp, p);
227 
228 	if (error == 0) {
229 		cvtstat(&ub, &oub);
230 		error = copyout((caddr_t)&oub, (caddr_t)SCARG(uap, sb),
231 		    sizeof (oub));
232 	}
233 
234 
235 	return (error);
236 }
237 
238 
239 /*
240  * Truncate a file given a file descriptor.
241  */
242 /* ARGSUSED */
243 int
244 compat_43_sys_ftruncate(struct lwp *l, void *v, register_t *retval)
245 {
246 	struct compat_43_sys_ftruncate_args /* {
247 		syscallarg(int) fd;
248 		syscallarg(long) length;
249 	} */ *uap = v;
250 	struct sys_ftruncate_args /* {
251 		syscallarg(int) fd;
252 		syscallarg(int) pad;
253 		syscallarg(off_t) length;
254 	} */ nuap;
255 
256 	SCARG(&nuap, fd) = SCARG(uap, fd);
257 	SCARG(&nuap, length) = SCARG(uap, length);
258 	return (sys_ftruncate(l, &nuap, retval));
259 }
260 
261 /*
262  * Truncate a file given its path name.
263  */
264 /* ARGSUSED */
265 int
266 compat_43_sys_truncate(struct lwp *l, void *v, register_t *retval)
267 {
268 	struct compat_43_sys_truncate_args /* {
269 		syscallarg(char *) path;
270 		syscallarg(long) length;
271 	} */ *uap = v;
272 	struct sys_truncate_args /* {
273 		syscallarg(char *) path;
274 		syscallarg(int) pad;
275 		syscallarg(off_t) length;
276 	} */ nuap;
277 
278 	SCARG(&nuap, path) = SCARG(uap, path);
279 	SCARG(&nuap, length) = SCARG(uap, length);
280 	return (sys_truncate(l, &nuap, retval));
281 }
282 
283 
284 /*
285  * Reposition read/write file offset.
286  */
287 int
288 compat_43_sys_lseek(struct lwp *l, void *v, register_t *retval)
289 {
290 	struct compat_43_sys_lseek_args /* {
291 		syscallarg(int) fd;
292 		syscallarg(long) offset;
293 		syscallarg(int) whence;
294 	} */ *uap = v;
295 	struct sys_lseek_args /* {
296 		syscallarg(int) fd;
297 		syscallarg(int) pad;
298 		syscallarg(off_t) offset;
299 		syscallarg(int) whence;
300 	} */ nuap;
301 	off_t qret;
302 	int error;
303 
304 	SCARG(&nuap, fd) = SCARG(uap, fd);
305 	SCARG(&nuap, offset) = SCARG(uap, offset);
306 	SCARG(&nuap, whence) = SCARG(uap, whence);
307 	error = sys_lseek(l, &nuap, (void *)&qret);
308 	*(long *)retval = qret;
309 	return (error);
310 }
311 
312 
313 /*
314  * Create a file.
315  */
316 int
317 compat_43_sys_creat(struct lwp *l, void *v, register_t *retval)
318 {
319 	struct compat_43_sys_creat_args /* {
320 		syscallarg(char *) path;
321 		syscallarg(int) mode;
322 	} */ *uap = v;
323 	struct sys_open_args /* {
324 		syscallarg(char *) path;
325 		syscallarg(int) flags;
326 		syscallarg(int) mode;
327 	} */ nuap;
328 
329 	SCARG(&nuap, path) = SCARG(uap, path);
330 	SCARG(&nuap, mode) = SCARG(uap, mode);
331 	SCARG(&nuap, flags) = O_WRONLY | O_CREAT | O_TRUNC;
332 	return (sys_open(l, &nuap, retval));
333 }
334 
335 /*ARGSUSED*/
336 int
337 compat_43_sys_quota(struct lwp *l, void *v, register_t *retval)
338 {
339 
340 	return (ENOSYS);
341 }
342 
343 
344 /*
345  * Read a block of directory entries in a file system independent format.
346  */
347 int
348 compat_43_sys_getdirentries(struct lwp *l, void *v, register_t *retval)
349 {
350 	struct compat_43_sys_getdirentries_args /* {
351 		syscallarg(int) fd;
352 		syscallarg(char *) buf;
353 		syscallarg(u_int) count;
354 		syscallarg(long *) basep;
355 	} */ *uap = v;
356 	struct proc *p = l->l_proc;
357 	struct vnode *vp;
358 	struct file *fp;
359 	struct uio auio, kuio;
360 	struct iovec aiov, kiov;
361 	struct dirent *dp, *edp;
362 	caddr_t dirbuf;
363 	size_t count = min(MAXBSIZE, (size_t)SCARG(uap, count));
364 
365 	int error, eofflag, readcnt;
366 	long loff;
367 
368 	/* getvnode() will use the descriptor for us */
369 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
370 		return (error);
371 	if ((fp->f_flag & FREAD) == 0) {
372 		error = EBADF;
373 		goto out;
374 	}
375 	vp = (struct vnode *)fp->f_data;
376 unionread:
377 	if (vp->v_type != VDIR) {
378 		error = EINVAL;
379 		goto out;
380 	}
381 	aiov.iov_base = SCARG(uap, buf);
382 	aiov.iov_len = count;
383 	auio.uio_iov = &aiov;
384 	auio.uio_iovcnt = 1;
385 	auio.uio_rw = UIO_READ;
386 	auio.uio_segflg = UIO_USERSPACE;
387 	auio.uio_procp = p;
388 	auio.uio_resid = count;
389 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
390 	loff = auio.uio_offset = fp->f_offset;
391 #	if (BYTE_ORDER != LITTLE_ENDIAN)
392 		if ((vp->v_mount->mnt_iflag & IMNT_DTYPE) == 0) {
393 			error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
394 			    (off_t **)0, (int *)0);
395 			fp->f_offset = auio.uio_offset;
396 		} else
397 #	endif
398 	{
399 		kuio = auio;
400 		kuio.uio_iov = &kiov;
401 		kuio.uio_segflg = UIO_SYSSPACE;
402 		kiov.iov_len = count;
403 		dirbuf = malloc(count, M_TEMP, M_WAITOK);
404 		kiov.iov_base = dirbuf;
405 		error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
406 			    (off_t **)0, (int *)0);
407 		fp->f_offset = kuio.uio_offset;
408 		if (error == 0) {
409 			readcnt = count - kuio.uio_resid;
410 			edp = (struct dirent *)&dirbuf[readcnt];
411 			for (dp = (struct dirent *)dirbuf; dp < edp; ) {
412 #				if (BYTE_ORDER == LITTLE_ENDIAN)
413 					/*
414 					 * The expected low byte of
415 					 * dp->d_namlen is our dp->d_type.
416 					 * The high MBZ byte of dp->d_namlen
417 					 * is our dp->d_namlen.
418 					 */
419 					dp->d_type = dp->d_namlen;
420 					dp->d_namlen = 0;
421 #				else
422 					/*
423 					 * The dp->d_type is the high byte
424 					 * of the expected dp->d_namlen,
425 					 * so must be zero'ed.
426 					 */
427 					dp->d_type = 0;
428 #				endif
429 				if (dp->d_reclen > 0) {
430 					dp = (struct dirent *)
431 					    ((char *)dp + dp->d_reclen);
432 				} else {
433 					error = EIO;
434 					break;
435 				}
436 			}
437 			if (dp >= edp)
438 				error = uiomove(dirbuf, readcnt, &auio);
439 		}
440 		free(dirbuf, M_TEMP);
441 	}
442 	VOP_UNLOCK(vp, 0);
443 	if (error)
444 		goto out;
445 
446 #ifdef UNION
447 {
448 	extern int (**union_vnodeop_p) __P((void *));
449 	extern struct vnode *union_dircache __P((struct vnode *));
450 
451 	if ((count == auio.uio_resid) &&
452 	    (vp->v_op == union_vnodeop_p)) {
453 		struct vnode *lvp;
454 
455 		lvp = union_dircache(vp);
456 		if (lvp != NULLVP) {
457 			struct vattr va;
458 
459 			/*
460 			 * If the directory is opaque,
461 			 * then don't show lower entries
462 			 */
463 			error = VOP_GETATTR(vp, &va, fp->f_cred, p);
464 			if (va.va_flags & OPAQUE) {
465 				vput(lvp);
466 				lvp = NULL;
467 			}
468 		}
469 
470 		if (lvp != NULLVP) {
471 			error = VOP_OPEN(lvp, FREAD, fp->f_cred, p);
472 			VOP_UNLOCK(lvp, 0);
473 
474 			if (error) {
475 				vrele(lvp);
476 				goto out;
477 			}
478 			fp->f_data = (caddr_t) lvp;
479 			fp->f_offset = 0;
480 			error = vn_close(vp, FREAD, fp->f_cred, p);
481 			if (error)
482 				goto out;
483 			vp = lvp;
484 			goto unionread;
485 		}
486 	}
487 }
488 #endif /* UNION */
489 
490 	if ((count == auio.uio_resid) &&
491 	    (vp->v_flag & VROOT) &&
492 	    (vp->v_mount->mnt_flag & MNT_UNION)) {
493 		struct vnode *tvp = vp;
494 		vp = vp->v_mount->mnt_vnodecovered;
495 		VREF(vp);
496 		fp->f_data = (caddr_t) vp;
497 		fp->f_offset = 0;
498 		vrele(tvp);
499 		goto unionread;
500 	}
501 	error = copyout((caddr_t)&loff, (caddr_t)SCARG(uap, basep),
502 	    sizeof(long));
503 	*retval = count - auio.uio_resid;
504  out:
505 	FILE_UNUSE(fp, p);
506 	return (error);
507 }
508