1 /* $NetBSD: irix_dirent.c,v 1.22 2008/03/21 21:54:58 ad Exp $ */
4 * Copyright (c) 1994, 2001, 2008 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas and Emmanuel Dreyfus.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: irix_dirent.c,v 1.22 2008/03/21 21:54:58 ad Exp $");
35 #include <sys/types.h>
36 #include <sys/signal.h>
37 #include <sys/param.h>
39 #include <sys/dirent.h>
40 #include <sys/fcntl.h>
42 #include <sys/filedesc.h>
43 #include <sys/mount.h>
44 #include <sys/malloc.h>
45 #include <sys/namei.h>
46 #include <sys/systm.h>
47 #include <sys/vnode.h>
49 #include <compat/common/compat_util.h>
51 #include <compat/irix/irix_types.h>
52 #include <compat/irix/irix_signal.h>
53 #include <compat/irix/irix_syscall.h>
54 #include <compat/irix/irix_syscallargs.h>
57 * irix_sys_ngetdents() is nearly a plain copy of svr4_sys_getdents(), from
58 * sys/compat/svr4/svr4_misc.c. We need a customized version to handle the
60 * Obviously the code should be merged, but it would require some
61 * change to the way COMPAT_SVR4 code is set up.
63 #define SVR4_RECLEN(de,namlen) ALIGN((SVR4_NAMEOFF(de) + (namlen) + 1))
64 #define SVR4_NAMEOFF(dp) ((char *)&(dp)->d_name - (char *)dp)
67 irix_sys_ngetdents(struct lwp
*l
, const struct irix_sys_ngetdents_args
*uap
, register_t
*retval
)
70 syscallarg(int) fildes;
71 syscallarg(irix_dirent_t *) buf;
72 syscallarg(unsigned short) nbyte;
73 syscallarg(int *) eof;
77 char *inp
, *buf
; /* BSD-format */
78 int len
, reclen
; /* BSD-format */
79 char *outp
; /* SVR4-format */
80 int resid
, svr4_reclen
; /* SVR4-format */
84 struct irix_dirent idb
;
85 off_t off
; /* true file offset */
86 int buflen
, error
, eofflag
;
87 off_t
*cookiebuf
= NULL
, *cookie
;
90 fd
= SCARG(uap
, fildes
);
91 if ((error
= fd_getvnode(fd
, &fp
)) != 0)
94 if ((fp
->f_flag
& FREAD
) == 0) {
99 vp
= (struct vnode
*)fp
->f_data
;
100 if (vp
->v_type
!= VDIR
) {
105 buflen
= min(MAXBSIZE
, SCARG(uap
, nbyte
));
106 buf
= malloc(buflen
, M_TEMP
, M_WAITOK
);
107 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
111 aiov
.iov_len
= buflen
;
112 auio
.uio_iov
= &aiov
;
114 auio
.uio_rw
= UIO_READ
;
115 auio
.uio_resid
= buflen
;
116 auio
.uio_offset
= off
;
117 UIO_SETUP_SYSSPACE(&auio
);
119 * First we read into the malloc'ed buffer, then
120 * we massage it into user space, one record at a time.
122 error
= VOP_READDIR(vp
, &auio
, fp
->f_cred
, &eofflag
, &cookiebuf
,
128 outp
= (char *)SCARG(uap
, buf
);
129 resid
= SCARG(uap
, nbyte
);
130 if ((len
= buflen
- auio
.uio_resid
) == 0)
133 for (cookie
= cookiebuf
; len
> 0; len
-= reclen
) {
134 bdp
= (struct dirent
*)inp
;
135 reclen
= bdp
->d_reclen
;
137 panic("irix_getdents: bad reclen");
139 off
= *cookie
++; /* each entry points to the next */
142 if ((off
>> 32) != 0) {
143 compat_offseterr(vp
, "irix_getdents");
147 if (bdp
->d_fileno
== 0) {
148 inp
+= reclen
; /* it is a hole; squish it out */
151 svr4_reclen
= SVR4_RECLEN(&idb
, bdp
->d_namlen
);
152 if (reclen
> len
|| resid
< svr4_reclen
) {
153 /* entry too big for buffer, so just stop */
158 * Massage in place to make a SVR4-shaped dirent (otherwise
159 * we have to worry about touching user memory outside of
160 * the copyout() call).
162 idb
.d_ino
= (irix_ino_t
)bdp
->d_fileno
;
163 idb
.d_off
= (irix_off_t
)off
;
164 idb
.d_reclen
= (u_short
)svr4_reclen
;
165 strlcpy(idb
.d_name
, bdp
->d_name
, sizeof(idb
.d_name
));
166 if ((error
= copyout((void *)&idb
, outp
, svr4_reclen
)))
168 /* advance past this real entry */
170 /* advance output past SVR4-shaped entry */
172 resid
-= svr4_reclen
;
175 /* if we squished out the whole block, try again */
176 if (outp
== (char *)SCARG(uap
, buf
))
178 fp
->f_offset
= off
; /* update the vnode offset */
181 *retval
= SCARG(uap
, nbyte
) - resid
;
185 free(cookiebuf
, M_TEMP
);
189 if (SCARG(uap
, eof
) != NULL
)
190 error
= copyout(&eofflag
, SCARG(uap
, eof
), sizeof(int));
195 irix_sys_getdents(struct lwp
*l
, const struct irix_sys_getdents_args
*uap
, register_t
*retval
)
198 syscallarg(int) fildes;
199 syscallarg(irix_dirent_t *) buf;
200 syscallarg(unsigned short) nbyte;
201 syscallarg(int *) eof;
203 struct irix_sys_ngetdents_args cup
;
205 SCARG(&cup
, fildes
) = SCARG(uap
, fildes
);
206 SCARG(&cup
, buf
) = SCARG(uap
, buf
);
207 SCARG(&cup
, nbyte
) = SCARG(uap
, nbytes
);
208 SCARG(&cup
, eof
) = NULL
;
210 return irix_sys_ngetdents(l
, (void *)&cup
, retval
);
215 * The 64 versions are very close to the
216 * 32 bit versions (only 3 lines of diff)
219 irix_sys_ngetdents64(struct lwp
*l
, const struct irix_sys_ngetdents64_args
*uap
, register_t
*retval
)
222 syscallarg(int) fildes;
223 syscallarg(irix_dirent64_t *) buf;
224 syscallarg(unsigned short) nbyte;
225 syscallarg(int *) eof;
229 char *inp
, *buf
; /* BSD-format */
230 int len
, reclen
; /* BSD-format */
231 char *outp
; /* SVR4-format */
232 int resid
, svr4_reclen
; /* SVR4-format */
236 struct irix_dirent64 idb
;
237 off_t off
; /* true file offset */
238 int buflen
, error
, eofflag
;
239 off_t
*cookiebuf
= NULL
, *cookie
;
242 fd
= SCARG(uap
, fildes
);
243 if ((error
= fd_getvnode(fd
, &fp
)) != 0)
246 if ((fp
->f_flag
& FREAD
) == 0) {
252 if (vp
->v_type
!= VDIR
) {
257 buflen
= min(MAXBSIZE
, SCARG(uap
, nbyte
));
258 buf
= malloc(buflen
, M_TEMP
, M_WAITOK
);
259 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
263 aiov
.iov_len
= buflen
;
264 auio
.uio_iov
= &aiov
;
266 auio
.uio_rw
= UIO_READ
;
267 auio
.uio_resid
= buflen
;
268 auio
.uio_offset
= off
;
269 UIO_SETUP_SYSSPACE(&auio
);
271 * First we read into the malloc'ed buffer, then
272 * we massage it into user space, one record at a time.
274 error
= VOP_READDIR(vp
, &auio
, fp
->f_cred
, &eofflag
, &cookiebuf
,
280 outp
= (char *)SCARG(uap
, buf
);
281 resid
= SCARG(uap
, nbyte
);
282 if ((len
= buflen
- auio
.uio_resid
) == 0)
285 for (cookie
= cookiebuf
; len
> 0; len
-= reclen
) {
286 bdp
= (struct dirent
*)inp
;
287 reclen
= bdp
->d_reclen
;
289 panic("irix_getdents64: bad reclen");
290 if (bdp
->d_fileno
== 0) {
291 inp
+= reclen
; /* it is a hole; squish it out */
298 svr4_reclen
= SVR4_RECLEN(&idb
, bdp
->d_namlen
);
299 if (reclen
> len
|| resid
< svr4_reclen
) {
300 /* entry too big for buffer, so just stop */
305 off
= *cookie
++; /* each entry points to the next */
309 * Massage in place to make a SVR4-shaped dirent (otherwise
310 * we have to worry about touching user memory outside of
311 * the copyout() call).
313 idb
.d_ino
= (irix_ino64_t
)bdp
->d_fileno
;
314 idb
.d_off
= (irix_off64_t
)off
;
315 idb
.d_reclen
= (u_short
)svr4_reclen
;
316 strlcpy(idb
.d_name
, bdp
->d_name
, sizeof(idb
.d_name
));
317 if ((error
= copyout((void *)&idb
, outp
, svr4_reclen
)))
319 /* advance past this real entry */
321 /* advance output past SVR4-shaped entry */
323 resid
-= svr4_reclen
;
326 /* if we squished out the whole block, try again */
327 if (outp
== (char *)SCARG(uap
, buf
))
329 fp
->f_offset
= off
; /* update the vnode offset */
332 *retval
= SCARG(uap
, nbyte
) - resid
;
336 free(cookiebuf
, M_TEMP
);
340 if (SCARG(uap
, eof
) != NULL
)
341 error
= copyout(&eofflag
, SCARG(uap
, eof
), sizeof(int));
346 irix_sys_getdents64(struct lwp
*l
, const struct irix_sys_getdents64_args
*uap
, register_t
*retval
)
349 syscallarg(int) fildes;
350 syscallarg(irix_dirent64_t *) buf;
351 syscallarg(unsigned short) nbyte;
352 syscallarg(int *) eof;
354 struct irix_sys_ngetdents64_args cup
;
356 SCARG(&cup
, fildes
) = SCARG(uap
, fildes
);
357 SCARG(&cup
, buf
) = SCARG(uap
, buf
);
358 SCARG(&cup
, nbyte
) = SCARG(uap
, nbytes
);
359 SCARG(&cup
, eof
) = NULL
;
361 return irix_sys_ngetdents64(l
, (void *)&cup
, retval
);