Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / compat / irix / irix_dirent.c
blob4ea7a0d9baa81dbf6b08c210dfed5ea05e6c7af1
1 /* $NetBSD: irix_dirent.c,v 1.22 2008/03/21 21:54:58 ad Exp $ */
3 /*-
4 * Copyright (c) 1994, 2001, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
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
12 * are met:
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>
38 #include <sys/proc.h>
39 #include <sys/dirent.h>
40 #include <sys/fcntl.h>
41 #include <sys/file.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
59 * eof flag.
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)
66 int
67 irix_sys_ngetdents(struct lwp *l, const struct irix_sys_ngetdents_args *uap, register_t *retval)
69 /* {
70 syscallarg(int) fildes;
71 syscallarg(irix_dirent_t *) buf;
72 syscallarg(unsigned short) nbyte;
73 syscallarg(int *) eof;
74 } */
75 struct dirent *bdp;
76 struct vnode *vp;
77 char *inp, *buf; /* BSD-format */
78 int len, reclen; /* BSD-format */
79 char *outp; /* SVR4-format */
80 int resid, svr4_reclen; /* SVR4-format */
81 struct file *fp;
82 struct uio auio;
83 struct iovec aiov;
84 struct irix_dirent idb;
85 off_t off; /* true file offset */
86 int buflen, error, eofflag;
87 off_t *cookiebuf = NULL, *cookie;
88 int ncookies, fd;
90 fd = SCARG(uap, fildes);
91 if ((error = fd_getvnode(fd, &fp)) != 0)
92 return (error);
94 if ((fp->f_flag & FREAD) == 0) {
95 error = EBADF;
96 goto out1;
99 vp = (struct vnode *)fp->f_data;
100 if (vp->v_type != VDIR) {
101 error = EINVAL;
102 goto out1;
105 buflen = min(MAXBSIZE, SCARG(uap, nbyte));
106 buf = malloc(buflen, M_TEMP, M_WAITOK);
107 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
108 off = fp->f_offset;
109 again:
110 aiov.iov_base = buf;
111 aiov.iov_len = buflen;
112 auio.uio_iov = &aiov;
113 auio.uio_iovcnt = 1;
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,
123 &ncookies);
124 if (error)
125 goto out;
127 inp = buf;
128 outp = (char *)SCARG(uap, buf);
129 resid = SCARG(uap, nbyte);
130 if ((len = buflen - auio.uio_resid) == 0)
131 goto eof;
133 for (cookie = cookiebuf; len > 0; len -= reclen) {
134 bdp = (struct dirent *)inp;
135 reclen = bdp->d_reclen;
136 if (reclen & 3)
137 panic("irix_getdents: bad reclen");
138 if (cookie)
139 off = *cookie++; /* each entry points to the next */
140 else
141 off += reclen;
142 if ((off >> 32) != 0) {
143 compat_offseterr(vp, "irix_getdents");
144 error = EINVAL;
145 goto out;
147 if (bdp->d_fileno == 0) {
148 inp += reclen; /* it is a hole; squish it out */
149 continue;
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 */
154 outp++;
155 break;
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)))
167 goto out;
168 /* advance past this real entry */
169 inp += reclen;
170 /* advance output past SVR4-shaped entry */
171 outp += svr4_reclen;
172 resid -= svr4_reclen;
175 /* if we squished out the whole block, try again */
176 if (outp == (char *)SCARG(uap, buf))
177 goto again;
178 fp->f_offset = off; /* update the vnode offset */
180 eof:
181 *retval = SCARG(uap, nbyte) - resid;
182 out:
183 VOP_UNLOCK(vp, 0);
184 if (cookiebuf)
185 free(cookiebuf, M_TEMP);
186 free(buf, M_TEMP);
187 out1:
188 fd_putfile(fd);
189 if (SCARG(uap, eof) != NULL)
190 error = copyout(&eofflag, SCARG(uap, eof), sizeof(int));
191 return error;
195 irix_sys_getdents(struct lwp *l, const struct irix_sys_getdents_args *uap, register_t *retval)
197 /* {
198 syscallarg(int) fildes;
199 syscallarg(irix_dirent_t *) buf;
200 syscallarg(unsigned short) nbyte;
201 syscallarg(int *) eof;
202 } */
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)
221 /* {
222 syscallarg(int) fildes;
223 syscallarg(irix_dirent64_t *) buf;
224 syscallarg(unsigned short) nbyte;
225 syscallarg(int *) eof;
226 } */
227 struct dirent *bdp;
228 struct vnode *vp;
229 char *inp, *buf; /* BSD-format */
230 int len, reclen; /* BSD-format */
231 char *outp; /* SVR4-format */
232 int resid, svr4_reclen; /* SVR4-format */
233 file_t *fp;
234 struct uio auio;
235 struct iovec aiov;
236 struct irix_dirent64 idb;
237 off_t off; /* true file offset */
238 int buflen, error, eofflag;
239 off_t *cookiebuf = NULL, *cookie;
240 int ncookies, fd;
242 fd = SCARG(uap, fildes);
243 if ((error = fd_getvnode(fd, &fp)) != 0)
244 return (error);
246 if ((fp->f_flag & FREAD) == 0) {
247 error = EBADF;
248 goto out1;
251 vp = fp->f_data;
252 if (vp->v_type != VDIR) {
253 error = EINVAL;
254 goto out1;
257 buflen = min(MAXBSIZE, SCARG(uap, nbyte));
258 buf = malloc(buflen, M_TEMP, M_WAITOK);
259 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
260 off = fp->f_offset;
261 again:
262 aiov.iov_base = buf;
263 aiov.iov_len = buflen;
264 auio.uio_iov = &aiov;
265 auio.uio_iovcnt = 1;
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,
275 &ncookies);
276 if (error)
277 goto out;
279 inp = buf;
280 outp = (char *)SCARG(uap, buf);
281 resid = SCARG(uap, nbyte);
282 if ((len = buflen - auio.uio_resid) == 0)
283 goto eof;
285 for (cookie = cookiebuf; len > 0; len -= reclen) {
286 bdp = (struct dirent *)inp;
287 reclen = bdp->d_reclen;
288 if (reclen & 3)
289 panic("irix_getdents64: bad reclen");
290 if (bdp->d_fileno == 0) {
291 inp += reclen; /* it is a hole; squish it out */
292 if (cookie)
293 off = *cookie++;
294 else
295 off += reclen;
296 continue;
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 */
301 outp++;
302 break;
304 if (cookie)
305 off = *cookie++; /* each entry points to the next */
306 else
307 off += reclen;
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)))
318 goto out;
319 /* advance past this real entry */
320 inp += reclen;
321 /* advance output past SVR4-shaped entry */
322 outp += svr4_reclen;
323 resid -= svr4_reclen;
326 /* if we squished out the whole block, try again */
327 if (outp == (char *)SCARG(uap, buf))
328 goto again;
329 fp->f_offset = off; /* update the vnode offset */
331 eof:
332 *retval = SCARG(uap, nbyte) - resid;
333 out:
334 VOP_UNLOCK(vp, 0);
335 if (cookiebuf)
336 free(cookiebuf, M_TEMP);
337 free(buf, M_TEMP);
338 out1:
339 fd_putfile(fd);
340 if (SCARG(uap, eof) != NULL)
341 error = copyout(&eofflag, SCARG(uap, eof), sizeof(int));
342 return error;
346 irix_sys_getdents64(struct lwp *l, const struct irix_sys_getdents64_args *uap, register_t *retval)
348 /* {
349 syscallarg(int) fildes;
350 syscallarg(irix_dirent64_t *) buf;
351 syscallarg(unsigned short) nbyte;
352 syscallarg(int *) eof;
353 } */
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);