bsd-family-tree: Sync with FreeBSD (FreeBSD 11.0).
[dragonfly.git] / sys / emulation / 43bsd / 43bsd_file.c
blobd137a1df7af2a7eb96c067a02f32965352b68c41
1 /*
2 * 43BSD_FILE.C - 4.3BSD compatibility file syscalls
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.
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.
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.
36 * These syscalls used to live in kern/vfs_syscalls.c. They are modified
37 * to use the new split syscalls.
40 #include "opt_compat.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/sysproto.h>
45 #include <sys/conf.h>
46 #include <sys/dirent.h>
47 #include <sys/fcntl.h>
48 #include <sys/file.h>
49 #include <sys/filedesc.h>
50 #include <sys/kernel.h>
51 #include <sys/kern_syscall.h>
52 #include <sys/malloc.h>
53 #include <sys/mount.h>
54 #include <sys/uio.h>
55 #include <sys/namei.h>
56 #include <sys/nlookup.h>
57 #include <sys/vnode.h>
59 #include <sys/mplock2.h>
62 * MPALMOSTSAFE
64 int
65 sys_ocreat(struct ocreat_args *uap)
67 struct nlookupdata nd;
68 int error;
70 get_mplock();
71 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
72 if (error == 0) {
73 error = kern_open(&nd, O_WRONLY | O_CREAT | O_TRUNC,
74 uap->mode, &uap->sysmsg_iresult);
76 rel_mplock();
77 return (error);
81 * MPALMOSTSAFE
83 int
84 sys_oftruncate(struct oftruncate_args *uap)
86 int error;
88 get_mplock();
89 error = kern_ftruncate(uap->fd, uap->length);
90 rel_mplock();
92 return (error);
96 * MPSAFE
98 int
99 sys_olseek(struct olseek_args *uap)
101 int error;
103 error = kern_lseek(uap->fd, uap->offset, uap->whence,
104 &uap->sysmsg_offset);
106 return (error);
110 * MPALMOSTSAFE
113 sys_otruncate(struct otruncate_args *uap)
115 struct nlookupdata nd;
116 int error;
118 get_mplock();
119 error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW);
120 if (error == 0)
121 error = kern_truncate(&nd, uap->length);
122 nlookup_done(&nd);
123 rel_mplock();
124 return (error);
127 #define PADDED_SIZE(x) \
128 ((sizeof(struct odirent) + (x) + 1 + 3) & ~3)
129 #define MAX_NAMELEN 255
131 struct odirent {
132 uint32_t od_fileno;
133 uint16_t od_reclen;
134 uint8_t od_type;
135 uint8_t od_namlen;
136 char od_name[];
140 * MPALMOSTSAFE
143 sys_ogetdirentries(struct ogetdirentries_args *uap)
145 int error, bytes_transfered;
146 char *buf, *outbuf;
147 size_t len;
148 struct dirent *ndp;
149 struct odirent *destdp;
150 long base;
152 if (uap->count > 16384)
153 len = 16384;
154 else
155 len = uap->count;
157 buf = kmalloc(len, M_TEMP, M_WAITOK);
159 get_mplock();
160 error = kern_getdirentries(uap->fd, buf, len,
161 &base, &bytes_transfered, UIO_SYSSPACE);
162 rel_mplock();
164 if (error) {
165 kfree(buf, M_TEMP);
166 return(error);
169 ndp = (struct dirent *)buf;
170 outbuf = uap->buf;
171 destdp = kmalloc(PADDED_SIZE(MAX_NAMELEN), M_TEMP, M_WAITOK);
173 for (; (char *)ndp < buf + bytes_transfered; ndp = _DIRENT_NEXT(ndp)) {
174 if ((char *)_DIRENT_NEXT(ndp) > buf + bytes_transfered)
175 break;
176 if (ndp->d_namlen > MAX_NAMELEN)
177 continue;
178 destdp->od_fileno = ndp->d_ino;
179 #if BYTE_ORDER == LITTLE_ENDIAN
180 destdp->od_type = ndp->d_namlen;
181 destdp->od_namlen = ndp->d_type;
182 #else
183 destdp->od_type = ndp->d_type;
184 destdp->od_namlen = ndp->d_namlen;
185 #endif
186 destdp->od_reclen = PADDED_SIZE(destdp->od_namlen);
187 if (destdp->od_reclen > len)
188 break; /* XXX can not happen */
189 bcopy(ndp->d_name, destdp->od_name, destdp->od_namlen);
190 bzero(destdp->od_name + destdp->od_namlen,
191 PADDED_SIZE(destdp->od_namlen) - sizeof(*destdp) -
192 destdp->od_namlen);
193 error = copyout(destdp, outbuf,
194 PADDED_SIZE(destdp->od_namlen));
195 if (error)
196 break;
197 outbuf += PADDED_SIZE(destdp->od_namlen);
198 len -= PADDED_SIZE(destdp->od_namlen);
201 kfree(destdp, M_TEMP);
202 kfree(buf, M_TEMP);
203 uap->sysmsg_iresult = (int)(outbuf - uap->buf);
204 return (0);