HAMMER 59C/Many: Stabilization pass - fixes for large file issues
[dragonfly.git] / sys / emulation / dragonfly12 / dfbsd12_getdirentries.c
blob168982ea34af6ce7b9c92437065e3b28132cc1cb
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:
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.
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.
34 * $DragonFly: src/sys/emulation/dragonfly12/dfbsd12_getdirentries.c,v 1.3 2006/09/05 00:55:44 dillon Exp $
37 #include "opt_compatdf12.h"
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>
47 #define PADDED_SIZE(x) \
48 ((sizeof(struct dfbsd12_dirent) + (x) + 1 + 3) & ~3)
49 #define MAX_NAMELEN 255
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[];
59 static int
60 common_getdirentries(long *base, int *result, int fd, char *usr_buf, size_t count)
62 int error, bytes_transfered;
63 char *buf, *outbuf;
64 size_t len;
65 struct dirent *ndp;
66 struct dfbsd12_dirent *destdp;
68 if (count > 16384)
69 len = 16384;
70 else
71 len = count;
73 buf = kmalloc(len, M_TEMP, M_WAITOK);
75 error = kern_getdirentries(fd, buf, len,
76 base, &bytes_transfered, UIO_SYSSPACE);
78 if (error) {
79 kfree(buf, M_TEMP);
80 return(error);
83 ndp = (struct dirent *)buf;
84 outbuf = usr_buf;
85 destdp = kmalloc(PADDED_SIZE(MAX_NAMELEN), M_TEMP, M_WAITOK);
87 for (; (char *)ndp < buf + bytes_transfered; ndp = _DIRENT_NEXT(ndp)) {
88 if ((char *)_DIRENT_NEXT(ndp) > buf + bytes_transfered)
89 break;
90 if (ndp->d_namlen > MAX_NAMELEN)
91 continue;
92 destdp->df12_d_fileno = ndp->d_ino;
93 destdp->df12_d_type = ndp->d_type;
94 destdp->df12_d_namlen = ndp->d_namlen;
95 destdp->df12_d_reclen = PADDED_SIZE(destdp->df12_d_namlen);
96 if (destdp->df12_d_reclen > len)
97 break; /* XXX can not happen */
98 bcopy(ndp->d_name, destdp->df12_d_name, destdp->df12_d_namlen);
99 bzero(destdp->df12_d_name + destdp->df12_d_namlen,
100 PADDED_SIZE(destdp->df12_d_namlen) - sizeof(*destdp) -
101 destdp->df12_d_namlen);
102 error = copyout(destdp, outbuf,
103 PADDED_SIZE(destdp->df12_d_namlen));
104 if (error)
105 break;
106 outbuf += PADDED_SIZE(destdp->df12_d_namlen);
107 len -= PADDED_SIZE(destdp->df12_d_namlen);
110 kfree(destdp, M_TEMP);
111 kfree(buf, M_TEMP);
112 *result = outbuf - usr_buf;
113 return (0);
117 sys_dfbsd12_getdirentries(struct dfbsd12_getdirentries_args *uap)
119 long base;
120 int error;
122 error = common_getdirentries(&base, &uap->sysmsg_result, uap->fd,
123 uap->buf, uap->count);
125 if (error == 0)
126 error = copyout(&base, uap->basep, sizeof(*uap->basep));
127 return (error);
131 sys_dfbsd12_getdents(struct dfbsd12_getdents_args *uap)
133 return(common_getdirentries(NULL, &uap->sysmsg_result, uap->fd,
134 uap->buf, uap->count));