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