linux32_dirent.c revision 1.18 1 1.18 riastrad /* $NetBSD: linux32_dirent.c,v 1.18 2017/07/28 15:34:06 riastradh Exp $ */
2 1.1 manu
3 1.1 manu /*-
4 1.1 manu * Copyright (c) 2006 Emmanuel Dreyfus, all rights reserved.
5 1.1 manu *
6 1.1 manu * Redistribution and use in source and binary forms, with or without
7 1.1 manu * modification, are permitted provided that the following conditions
8 1.1 manu * are met:
9 1.1 manu * 1. Redistributions of source code must retain the above copyright
10 1.1 manu * notice, this list of conditions and the following disclaimer.
11 1.1 manu * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 manu * notice, this list of conditions and the following disclaimer in the
13 1.1 manu * documentation and/or other materials provided with the distribution.
14 1.1 manu * 3. All advertising materials mentioning features or use of this software
15 1.1 manu * must display the following acknowledgement:
16 1.1 manu * This product includes software developed by Emmanuel Dreyfus
17 1.1 manu * 4. The name of the author may not be used to endorse or promote
18 1.1 manu * products derived from this software without specific prior written
19 1.1 manu * permission.
20 1.1 manu *
21 1.1 manu * THIS SOFTWARE IS PROVIDED BY THE THE AUTHOR AND CONTRIBUTORS ``AS IS''
22 1.1 manu * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 1.1 manu * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 1.1 manu * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
25 1.1 manu * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 1.1 manu * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 1.1 manu * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 1.1 manu * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 1.1 manu * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 1.1 manu * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 1.1 manu * POSSIBILITY OF SUCH DAMAGE.
32 1.1 manu */
33 1.1 manu
34 1.1 manu #include <sys/cdefs.h>
35 1.1 manu
36 1.18 riastrad __KERNEL_RCSID(0, "$NetBSD: linux32_dirent.c,v 1.18 2017/07/28 15:34:06 riastradh Exp $");
37 1.1 manu
38 1.1 manu #include <sys/types.h>
39 1.1 manu #include <sys/param.h>
40 1.1 manu #include <sys/fstypes.h>
41 1.1 manu #include <sys/signal.h>
42 1.1 manu #include <sys/dirent.h>
43 1.1 manu #include <sys/kernel.h>
44 1.1 manu #include <sys/fcntl.h>
45 1.6 njoly #include <sys/file.h>
46 1.6 njoly #include <sys/filedesc.h>
47 1.6 njoly #include <sys/malloc.h>
48 1.1 manu #include <sys/select.h>
49 1.1 manu #include <sys/proc.h>
50 1.1 manu #include <sys/ucred.h>
51 1.6 njoly #include <sys/vnode.h>
52 1.1 manu #include <sys/swap.h>
53 1.1 manu
54 1.1 manu #include <machine/types.h>
55 1.1 manu
56 1.1 manu #include <sys/syscallargs.h>
57 1.1 manu
58 1.1 manu #include <compat/netbsd32/netbsd32.h>
59 1.1 manu #include <compat/netbsd32/netbsd32_conv.h>
60 1.1 manu #include <compat/netbsd32/netbsd32_syscallargs.h>
61 1.1 manu
62 1.1 manu #include <compat/linux/common/linux_types.h>
63 1.1 manu #include <compat/linux/common/linux_signal.h>
64 1.1 manu #include <compat/linux/common/linux_machdep.h>
65 1.1 manu #include <compat/linux/common/linux_misc.h>
66 1.1 manu #include <compat/linux/common/linux_oldolduname.h>
67 1.6 njoly #include <compat/linux/common/linux_dirent.h>
68 1.7 ad #include <compat/linux/common/linux_ipc.h>
69 1.7 ad #include <compat/linux/common/linux_sem.h>
70 1.1 manu #include <compat/linux/linux_syscallargs.h>
71 1.1 manu
72 1.1 manu #include <compat/linux32/common/linux32_types.h>
73 1.1 manu #include <compat/linux32/common/linux32_signal.h>
74 1.1 manu #include <compat/linux32/common/linux32_machdep.h>
75 1.1 manu #include <compat/linux32/common/linux32_sysctl.h>
76 1.1 manu #include <compat/linux32/common/linux32_socketcall.h>
77 1.1 manu #include <compat/linux32/linux32_syscallargs.h>
78 1.1 manu
79 1.6 njoly /*
80 1.6 njoly * Linux 'readdir' call. This code is mostly taken from the
81 1.6 njoly * SunOS getdents call (see compat/sunos/sunos_misc.c), though
82 1.6 njoly * an attempt has been made to keep it a little cleaner (failing
83 1.6 njoly * miserably, because of the cruft needed if count 1 is passed).
84 1.6 njoly *
85 1.6 njoly * The d_off field should contain the offset of the next valid entry,
86 1.6 njoly * but in Linux it has the offset of the entry itself. We emulate
87 1.6 njoly * that bug here.
88 1.6 njoly *
89 1.6 njoly * Read in BSD-style entries, convert them, and copy them out.
90 1.6 njoly *
91 1.6 njoly * Note that this doesn't handle union-mounted filesystems.
92 1.6 njoly */
93 1.6 njoly
94 1.1 manu int
95 1.4 dsl linux32_sys_getdents(struct lwp *l, const struct linux32_sys_getdents_args *uap, register_t *retval)
96 1.1 manu {
97 1.4 dsl /* {
98 1.6 njoly syscallarg(int) fd;
99 1.6 njoly syscallarg(linux32_direntp_t) dent;
100 1.6 njoly syscallarg(unsigned int) count;
101 1.4 dsl } */
102 1.6 njoly struct dirent *bdp;
103 1.6 njoly struct vnode *vp;
104 1.6 njoly char *inp, *tbuf; /* BSD-format */
105 1.6 njoly int len, reclen; /* BSD-format */
106 1.6 njoly char *outp; /* Linux-format */
107 1.6 njoly int resid, linux32_reclen = 0; /* Linux-format */
108 1.6 njoly struct file *fp;
109 1.6 njoly struct uio auio;
110 1.6 njoly struct iovec aiov;
111 1.6 njoly struct linux32_dirent idb;
112 1.6 njoly off_t off; /* true file offset */
113 1.6 njoly int buflen, error, eofflag, nbytes, oldcall;
114 1.6 njoly struct vattr va;
115 1.6 njoly off_t *cookiebuf = NULL, *cookie;
116 1.6 njoly int ncookies;
117 1.6 njoly
118 1.6 njoly /* fd_getvnode() will use the descriptor for us */
119 1.6 njoly if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
120 1.6 njoly return (error);
121 1.6 njoly
122 1.6 njoly if ((fp->f_flag & FREAD) == 0) {
123 1.6 njoly error = EBADF;
124 1.6 njoly goto out1;
125 1.6 njoly }
126 1.6 njoly
127 1.6 njoly vp = (struct vnode *)fp->f_data;
128 1.6 njoly if (vp->v_type != VDIR) {
129 1.9 njoly error = ENOTDIR;
130 1.6 njoly goto out1;
131 1.6 njoly }
132 1.6 njoly
133 1.13 hannken vn_lock(vp, LK_SHARED | LK_RETRY);
134 1.13 hannken error = VOP_GETATTR(vp, &va, l->l_cred);
135 1.13 hannken VOP_UNLOCK(vp);
136 1.13 hannken if (error)
137 1.6 njoly goto out1;
138 1.6 njoly
139 1.6 njoly nbytes = SCARG(uap, count);
140 1.6 njoly if (nbytes == 1) { /* emulating old, broken behaviour */
141 1.6 njoly nbytes = sizeof (idb);
142 1.6 njoly buflen = max(va.va_blocksize, nbytes);
143 1.6 njoly oldcall = 1;
144 1.6 njoly } else {
145 1.6 njoly buflen = min(MAXBSIZE, nbytes);
146 1.6 njoly if (buflen < va.va_blocksize)
147 1.6 njoly buflen = va.va_blocksize;
148 1.6 njoly oldcall = 0;
149 1.6 njoly }
150 1.6 njoly tbuf = malloc(buflen, M_TEMP, M_WAITOK);
151 1.6 njoly
152 1.6 njoly vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
153 1.6 njoly off = fp->f_offset;
154 1.6 njoly again:
155 1.6 njoly aiov.iov_base = tbuf;
156 1.6 njoly aiov.iov_len = buflen;
157 1.6 njoly auio.uio_iov = &aiov;
158 1.6 njoly auio.uio_iovcnt = 1;
159 1.6 njoly auio.uio_rw = UIO_READ;
160 1.6 njoly auio.uio_resid = buflen;
161 1.6 njoly auio.uio_offset = off;
162 1.6 njoly UIO_SETUP_SYSSPACE(&auio);
163 1.6 njoly /*
164 1.6 njoly * First we read into the malloc'ed buffer, then
165 1.6 njoly * we massage it into user space, one record at a time.
166 1.6 njoly */
167 1.6 njoly error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
168 1.6 njoly &ncookies);
169 1.6 njoly if (error)
170 1.6 njoly goto out;
171 1.6 njoly
172 1.6 njoly inp = tbuf;
173 1.6 njoly outp = (void *)SCARG_P32(uap, dent);
174 1.6 njoly resid = nbytes;
175 1.6 njoly if ((len = buflen - auio.uio_resid) == 0)
176 1.6 njoly goto eof;
177 1.6 njoly
178 1.6 njoly for (cookie = cookiebuf; len > 0; len -= reclen) {
179 1.6 njoly bdp = (struct dirent *)inp;
180 1.6 njoly reclen = bdp->d_reclen;
181 1.18 riastrad if (reclen & 3) {
182 1.18 riastrad error = EIO;
183 1.18 riastrad goto out;
184 1.18 riastrad }
185 1.6 njoly if (bdp->d_fileno == 0) {
186 1.6 njoly inp += reclen; /* it is a hole; squish it out */
187 1.6 njoly if (cookie)
188 1.6 njoly off = *cookie++;
189 1.6 njoly else
190 1.6 njoly off += reclen;
191 1.6 njoly continue;
192 1.6 njoly }
193 1.6 njoly linux32_reclen = LINUX_RECLEN(&idb, bdp->d_namlen);
194 1.6 njoly if (reclen > len || resid < linux32_reclen) {
195 1.6 njoly /* entry too big for buffer, so just stop */
196 1.6 njoly outp++;
197 1.6 njoly break;
198 1.6 njoly }
199 1.6 njoly /*
200 1.6 njoly * Massage in place to make a Linux-shaped dirent (otherwise
201 1.6 njoly * we have to worry about touching user memory outside of
202 1.6 njoly * the copyout() call).
203 1.6 njoly */
204 1.6 njoly idb.d_ino = bdp->d_fileno;
205 1.6 njoly /*
206 1.6 njoly * The old readdir() call misuses the offset and reclen fields.
207 1.6 njoly */
208 1.6 njoly if (oldcall) {
209 1.6 njoly idb.d_off = (linux32_off_t)linux32_reclen;
210 1.6 njoly idb.d_reclen = (u_short)bdp->d_namlen;
211 1.6 njoly } else {
212 1.6 njoly if (sizeof (idb.d_off) <= 4 && (off >> 32) != 0) {
213 1.6 njoly compat_offseterr(vp, "linux32_getdents");
214 1.6 njoly error = EINVAL;
215 1.6 njoly goto out;
216 1.6 njoly }
217 1.6 njoly idb.d_off = (linux32_off_t)off;
218 1.6 njoly idb.d_reclen = (u_short)linux32_reclen;
219 1.6 njoly }
220 1.17 christos size_t dirl = MIN(sizeof(idb.d_name) - 1, bdp->d_namlen + 1);
221 1.17 christos memcpy(idb.d_name, bdp->d_name, dirl);
222 1.17 christos idb.d_name[dirl + 1] = bdp->d_type;
223 1.6 njoly if ((error = copyout((void *)&idb, outp, linux32_reclen)))
224 1.6 njoly goto out;
225 1.6 njoly /* advance past this real entry */
226 1.6 njoly inp += reclen;
227 1.6 njoly if (cookie)
228 1.6 njoly off = *cookie++; /* each entry points to itself */
229 1.6 njoly else
230 1.6 njoly off += reclen;
231 1.6 njoly /* advance output past Linux-shaped entry */
232 1.6 njoly outp += linux32_reclen;
233 1.6 njoly resid -= linux32_reclen;
234 1.6 njoly if (oldcall)
235 1.6 njoly break;
236 1.6 njoly }
237 1.6 njoly
238 1.6 njoly /* if we squished out the whole block, try again */
239 1.10 he if (outp == (void *)SCARG_P32(uap, dent)) {
240 1.10 he if (cookiebuf)
241 1.10 he free(cookiebuf, M_TEMP);
242 1.10 he cookiebuf = NULL;
243 1.6 njoly goto again;
244 1.10 he }
245 1.6 njoly fp->f_offset = off; /* update the vnode offset */
246 1.6 njoly
247 1.6 njoly if (oldcall)
248 1.6 njoly nbytes = resid + linux32_reclen;
249 1.6 njoly
250 1.6 njoly eof:
251 1.6 njoly *retval = nbytes - resid;
252 1.6 njoly out:
253 1.11 hannken VOP_UNLOCK(vp);
254 1.6 njoly if (cookiebuf)
255 1.6 njoly free(cookiebuf, M_TEMP);
256 1.6 njoly free(tbuf, M_TEMP);
257 1.6 njoly out1:
258 1.6 njoly fd_putfile(SCARG(uap, fd));
259 1.6 njoly return error;
260 1.1 manu }
261 1.1 manu
262 1.1 manu int
263 1.4 dsl linux32_sys_getdents64(struct lwp *l, const struct linux32_sys_getdents64_args *uap, register_t *retval)
264 1.1 manu {
265 1.4 dsl /* {
266 1.8 njoly syscallarg(int) fd;
267 1.8 njoly syscallarg(linux32_dirent64p_t) dent;
268 1.8 njoly syscallarg(unsigned int) count;
269 1.4 dsl } */
270 1.1 manu struct linux_sys_getdents64_args ua;
271 1.1 manu
272 1.1 manu NETBSD32TO64_UAP(fd);
273 1.1 manu NETBSD32TOP_UAP(dent, struct linux_dirent64);
274 1.1 manu NETBSD32TO64_UAP(count);
275 1.1 manu
276 1.1 manu return linux_sys_getdents64(l, &ua, retval);
277 1.1 manu }
278 1.1 manu
279 1.5 njoly int
280 1.5 njoly linux32_sys_readdir(struct lwp *l, const struct linux32_sys_readdir_args *uap, register_t *retval)
281 1.5 njoly {
282 1.5 njoly /* {
283 1.5 njoly syscallarg(int) fd;
284 1.5 njoly syscallarg(struct linux32_direntp_t) dent;
285 1.5 njoly syscallarg(unsigned int) count;
286 1.5 njoly } */
287 1.5 njoly int error;
288 1.5 njoly struct linux32_sys_getdents_args da;
289 1.5 njoly
290 1.5 njoly SCARG(&da, fd) = SCARG(uap, fd);
291 1.5 njoly SCARG(&da, dent) = SCARG(uap, dent);
292 1.5 njoly SCARG(&da, count) = 1;
293 1.5 njoly
294 1.5 njoly error = linux32_sys_getdents(l, &da, retval);
295 1.5 njoly if (error == 0 && *retval > 1)
296 1.5 njoly *retval = 1;
297 1.5 njoly
298 1.5 njoly return error;
299 1.5 njoly }
300