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