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