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