kernel - Move MP lock inward, plus misc other stuff
[dragonfly.git] / sys / emulation / dragonfly12 / dfbsd12_getdirentries.c
1 /*-
2  * Copyright (c) 2005 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Joerg Sonnenberger <joerg@bec.de>.
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * $DragonFly: src/sys/emulation/dragonfly12/dfbsd12_getdirentries.c,v 1.3 2006/09/05 00:55:44 dillon Exp $
35  */
36
37 #include "opt_compatdf12.h"
38
39 #include <sys/param.h>
40 #include <sys/dirent.h>
41 #include <sys/errno.h>
42 #include <sys/kern_syscall.h>
43 #include <sys/systm.h>
44 #include <sys/sysproto.h>
45 #include <sys/uio.h>
46
47 #define PADDED_SIZE(x)  \
48         ((sizeof(struct dfbsd12_dirent) + (x) + 1 + 3) & ~3)
49 #define MAX_NAMELEN     255
50
51 struct dfbsd12_dirent {
52         uint32_t        df12_d_fileno;
53         uint16_t        df12_d_reclen;
54         uint8_t         df12_d_type;
55         uint8_t         df12_d_namlen;
56         char            df12_d_name[];
57 };
58
59 /*
60  * MPALMOSTSAFE
61  */
62 static int
63 common_getdirentries(long *base, int *result, int fd, char *usr_buf, size_t count)
64 {
65         int error, bytes_transfered;
66         char *buf, *outbuf;
67         size_t len;
68         struct dirent *ndp;
69         struct dfbsd12_dirent *destdp;
70
71         if (count > 16384)
72                 len = 16384;
73         else
74                 len = count;
75
76         buf = kmalloc(len, M_TEMP, M_WAITOK);
77
78         get_mplock();
79         error = kern_getdirentries(fd, buf, len, base,
80                                    &bytes_transfered, UIO_SYSSPACE);
81         rel_mplock();
82
83         if (error) {
84                 kfree(buf, M_TEMP);
85                 return(error);
86         }
87
88         ndp = (struct dirent *)buf;
89         outbuf = usr_buf;
90         destdp = kmalloc(PADDED_SIZE(MAX_NAMELEN), M_TEMP, M_WAITOK);
91
92         for (; (char *)ndp < buf + bytes_transfered; ndp = _DIRENT_NEXT(ndp)) {
93                 if ((char *)_DIRENT_NEXT(ndp) > buf + bytes_transfered)
94                         break;
95                 if (ndp->d_namlen > MAX_NAMELEN)
96                         continue;
97                 destdp->df12_d_fileno = ndp->d_ino;
98                 destdp->df12_d_type = ndp->d_type;
99                 destdp->df12_d_namlen = ndp->d_namlen;
100                 destdp->df12_d_reclen = PADDED_SIZE(destdp->df12_d_namlen);
101                 if (destdp->df12_d_reclen > len)
102                         break; /* XXX can not happen */
103                 bcopy(ndp->d_name, destdp->df12_d_name, destdp->df12_d_namlen);
104                 bzero(destdp->df12_d_name + destdp->df12_d_namlen,
105                     PADDED_SIZE(destdp->df12_d_namlen) - sizeof(*destdp) -
106                     destdp->df12_d_namlen);
107                 error = copyout(destdp, outbuf,
108                     PADDED_SIZE(destdp->df12_d_namlen));
109                 if (error)
110                         break;
111                 outbuf += PADDED_SIZE(destdp->df12_d_namlen);
112                 len -= PADDED_SIZE(destdp->df12_d_namlen);
113         }
114
115         kfree(destdp, M_TEMP);
116         kfree(buf, M_TEMP);
117         *result = outbuf - usr_buf;
118         return (0);
119 }
120
121 /*
122  * MPSAFE
123  */
124 int
125 sys_dfbsd12_getdirentries(struct dfbsd12_getdirentries_args *uap)
126 {
127         long base;
128         int error;
129
130         error = common_getdirentries(&base, &uap->sysmsg_iresult, uap->fd,
131                                      uap->buf, uap->count);
132
133         if (error == 0)
134                 error = copyout(&base, uap->basep, sizeof(*uap->basep));
135         return (error);
136 }
137
138 /*
139  * MPSAFE
140  */
141 int
142 sys_dfbsd12_getdents(struct dfbsd12_getdents_args *uap)
143 {
144         return(common_getdirentries(NULL, &uap->sysmsg_iresult, uap->fd,
145                                     uap->buf, uap->count));
146 }