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