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