AMD64 - Refactor uio_resid and size_t assumptions.
[dragonfly.git] / sys / emulation / 43bsd / 43bsd_file.c
CommitLineData
8f6f8622
DRJ
1/*
2 * 43BSD_FILE.C - 4.3BSD compatibility file syscalls
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 *
efda3bd0 40 * $DragonFly: src/sys/emulation/43bsd/43bsd_file.c,v 1.11 2006/09/05 00:55:44 dillon Exp $
8f6f8622
DRJ
41 * from: DragonFly kern/vfs_syscalls.c,v 1.20
42 *
43 * These syscalls used to live in kern/vfs_syscalls.c. They are modified
44 * to use the new split syscalls.
45 */
46
47#include "opt_compat.h"
48
49#include <sys/param.h>
50#include <sys/systm.h>
51#include <sys/sysproto.h>
52#include <sys/conf.h>
9697c509
DRJ
53#include <sys/dirent.h>
54#include <sys/fcntl.h>
55#include <sys/file.h>
56#include <sys/filedesc.h>
8f6f8622
DRJ
57#include <sys/kernel.h>
58#include <sys/kern_syscall.h>
9697c509
DRJ
59#include <sys/malloc.h>
60#include <sys/mount.h>
9697c509 61#include <sys/uio.h>
fad57d0e
MD
62#include <sys/namei.h>
63#include <sys/nlookup.h>
9697c509
DRJ
64#include <sys/vnode.h>
65
66#include <vfs/union/union.h>
67
68int
753fd850 69sys_ocreat(struct ocreat_args *uap)
9697c509 70{
fad57d0e 71 struct nlookupdata nd;
9697c509
DRJ
72 int error;
73
fad57d0e
MD
74 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
75 if (error == 0) {
76 error = kern_open(&nd, O_WRONLY | O_CREAT | O_TRUNC,
e54488bb 77 uap->mode, &uap->sysmsg_iresult);
fad57d0e 78 }
9697c509
DRJ
79 return (error);
80}
8f6f8622
DRJ
81
82int
753fd850 83sys_oftruncate(struct oftruncate_args *uap)
8f6f8622
DRJ
84{
85 int error;
86
87 error = kern_ftruncate(uap->fd, uap->length);
88
89 return (error);
90}
9697c509
DRJ
91
92int
753fd850 93sys_olseek(struct olseek_args *uap)
9697c509
DRJ
94{
95 int error;
96
97 error = kern_lseek(uap->fd, uap->offset, uap->whence,
d3299adc 98 &uap->sysmsg_offset);
9697c509
DRJ
99
100 return (error);
101}
102
103int
753fd850 104sys_otruncate(struct otruncate_args *uap)
9697c509 105{
fad57d0e 106 struct nlookupdata nd;
9697c509
DRJ
107 int error;
108
fad57d0e
MD
109 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
110 if (error == 0)
111 error = kern_truncate(&nd, uap->length);
112 nlookup_done(&nd);
9697c509
DRJ
113 return (error);
114}
115
cabdd8a4
JS
116#define PADDED_SIZE(x) \
117 ((sizeof(struct odirent) + (x) + 1 + 3) & ~3)
118#define MAX_NAMELEN 255
119
120struct odirent {
121 uint32_t od_fileno;
122 uint16_t od_reclen;
123 uint8_t od_type;
124 uint8_t od_namlen;
125 char od_name[];
126};
127
9697c509 128int
753fd850 129sys_ogetdirentries(struct ogetdirentries_args *uap)
9697c509 130{
cabdd8a4
JS
131 int error, bytes_transfered;
132 char *buf, *outbuf;
133 size_t len;
134 struct dirent *ndp;
135 struct odirent *destdp;
136 long base;
137
138 if (uap->count > 16384)
139 len = 16384;
140 else
141 len = uap->count;
142
efda3bd0 143 buf = kmalloc(len, M_TEMP, M_WAITOK);
cabdd8a4
JS
144
145 error = kern_getdirentries(uap->fd, buf, len,
146 &base, &bytes_transfered, UIO_SYSSPACE);
147
148 if (error) {
efda3bd0 149 kfree(buf, M_TEMP);
cabdd8a4 150 return(error);
9697c509 151 }
cabdd8a4
JS
152
153 ndp = (struct dirent *)buf;
154 outbuf = uap->buf;
efda3bd0 155 destdp = kmalloc(PADDED_SIZE(MAX_NAMELEN), M_TEMP, M_WAITOK);
cabdd8a4
JS
156
157 for (; (char *)ndp < buf + bytes_transfered; ndp = _DIRENT_NEXT(ndp)) {
158 if ((char *)_DIRENT_NEXT(ndp) > buf + bytes_transfered)
159 break;
160 if (ndp->d_namlen > MAX_NAMELEN)
161 continue;
01f31ab3 162 destdp->od_fileno = ndp->d_ino;
cabdd8a4
JS
163#if BYTE_ORDER == LITTLE_ENDIAN
164 destdp->od_type = ndp->d_namlen;
165 destdp->od_namlen = ndp->d_type;
166#else
167 destdp->od_type = ndp->d_type;
168 destdp->od_namlen = ndp->d_namlen;
169#endif
170 destdp->od_reclen = PADDED_SIZE(destdp->od_namlen);
171 if (destdp->od_reclen > len)
172 break; /* XXX can not happen */
173 bcopy(ndp->d_name, destdp->od_name, destdp->od_namlen);
174 bzero(destdp->od_name + destdp->od_namlen,
175 PADDED_SIZE(destdp->od_namlen) - sizeof(*destdp) -
176 destdp->od_namlen);
177 error = copyout(destdp, outbuf,
178 PADDED_SIZE(destdp->od_namlen));
179 if (error)
180 break;
181 outbuf += PADDED_SIZE(destdp->od_namlen);
182 len -= PADDED_SIZE(destdp->od_namlen);
9697c509 183 }
cabdd8a4 184
efda3bd0
MD
185 kfree(destdp, M_TEMP);
186 kfree(buf, M_TEMP);
e54488bb 187 uap->sysmsg_iresult = (int)(outbuf - uap->buf);
cabdd8a4 188 return (0);
9697c509 189}