1 | /* $NetBSD: vfs_syscalls_12.c,v 1.31 2014/09/05 09:21:54 matt Exp $ */ |
2 | |
3 | /* |
4 | * Copyright (c) 1989, 1993 |
5 | * The Regents of the University of California. All rights reserved. |
6 | * (c) UNIX System Laboratories, Inc. |
7 | * All or some portions of this file are derived from material licensed |
8 | * to the University of California by American Telephone and Telegraph |
9 | * Co. or Unix System Laboratories, Inc. and are reproduced herein with |
10 | * the permission of UNIX System Laboratories, Inc. |
11 | * |
12 | * Redistribution and use in source and binary forms, with or without |
13 | * modification, are permitted provided that the following conditions |
14 | * are met: |
15 | * 1. Redistributions of source code must retain the above copyright |
16 | * notice, this list of conditions and the following disclaimer. |
17 | * 2. Redistributions in binary form must reproduce the above copyright |
18 | * notice, this list of conditions and the following disclaimer in the |
19 | * documentation and/or other materials provided with the distribution. |
20 | * 3. Neither the name of the University nor the names of its contributors |
21 | * may be used to endorse or promote products derived from this software |
22 | * without specific prior written permission. |
23 | * |
24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
34 | * SUCH DAMAGE. |
35 | * |
36 | * From: @(#)vfs_syscalls.c 8.28 (Berkeley) 12/10/94 |
37 | */ |
38 | |
39 | #include <sys/cdefs.h> |
40 | __KERNEL_RCSID(0, "$NetBSD: vfs_syscalls_12.c,v 1.31 2014/09/05 09:21:54 matt Exp $" ); |
41 | |
42 | #include <sys/param.h> |
43 | #include <sys/systm.h> |
44 | #include <sys/namei.h> |
45 | #include <sys/filedesc.h> |
46 | #include <sys/kernel.h> |
47 | #include <sys/file.h> |
48 | #include <sys/stat.h> |
49 | #include <sys/socketvar.h> |
50 | #include <sys/vnode.h> |
51 | #include <sys/proc.h> |
52 | #include <sys/uio.h> |
53 | #include <sys/dirent.h> |
54 | #include <sys/vfs_syscalls.h> |
55 | |
56 | #include <sys/syscallargs.h> |
57 | |
58 | #include <compat/sys/stat.h> |
59 | #include <compat/sys/dirent.h> |
60 | |
61 | /* |
62 | * Convert from a new to an old stat structure. |
63 | */ |
64 | void |
65 | compat_12_stat_conv(const struct stat *st, struct stat12 *ost) |
66 | { |
67 | |
68 | ost->st_dev = st->st_dev; |
69 | ost->st_ino = st->st_ino; |
70 | ost->st_mode = st->st_mode & 0xffff; |
71 | if (st->st_nlink >= (1 << 15)) |
72 | ost->st_nlink = (1 << 15) - 1; |
73 | else |
74 | ost->st_nlink = st->st_nlink; |
75 | ost->st_uid = st->st_uid; |
76 | ost->st_gid = st->st_gid; |
77 | ost->st_rdev = st->st_rdev; |
78 | timespec_to_timespec50(&st->st_atimespec, &ost->st_atimespec); |
79 | timespec_to_timespec50(&st->st_mtimespec, &ost->st_mtimespec); |
80 | timespec_to_timespec50(&st->st_ctimespec, &ost->st_ctimespec); |
81 | ost->st_size = st->st_size; |
82 | ost->st_blocks = st->st_blocks; |
83 | ost->st_blksize = st->st_blksize; |
84 | ost->st_flags = st->st_flags; |
85 | ost->st_gen = st->st_gen; |
86 | } |
87 | |
88 | /* |
89 | * Read a block of directory entries in a file system independent format. |
90 | */ |
91 | int |
92 | compat_12_sys_getdirentries(struct lwp *l, const struct compat_12_sys_getdirentries_args *uap, register_t *retval) |
93 | { |
94 | /* { |
95 | syscallarg(int) fd; |
96 | syscallarg(char *) buf; |
97 | syscallarg(u_int) count; |
98 | syscallarg(long *) basep; |
99 | } */ |
100 | struct dirent *bdp; |
101 | struct vnode *vp; |
102 | char *inp, *tbuf; /* Current-format */ |
103 | int len, reclen; /* Current-format */ |
104 | char *outp; /* Dirent12-format */ |
105 | int resid, old_reclen = 0; /* Dirent12-format */ |
106 | struct file *fp; |
107 | struct uio auio; |
108 | struct iovec aiov; |
109 | struct dirent12 idb; |
110 | off_t off; /* true file offset */ |
111 | int buflen, error, eofflag, nbytes; |
112 | struct vattr va; |
113 | off_t *cookiebuf = NULL, *cookie; |
114 | int ncookies; |
115 | long loff; |
116 | |
117 | /* fd_getvnode() will use the descriptor for us */ |
118 | if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0) |
119 | return (error); |
120 | |
121 | if ((fp->f_flag & FREAD) == 0) { |
122 | error = EBADF; |
123 | goto out1; |
124 | } |
125 | |
126 | vp = (struct vnode *)fp->f_vnode; |
127 | if (vp->v_type != VDIR) { |
128 | error = ENOTDIR; |
129 | goto out1; |
130 | } |
131 | |
132 | vn_lock(vp, LK_SHARED | LK_RETRY); |
133 | error = VOP_GETATTR(vp, &va, l->l_cred); |
134 | VOP_UNLOCK(vp); |
135 | if (error) |
136 | goto out1; |
137 | |
138 | loff = fp->f_offset; |
139 | nbytes = SCARG(uap, count); |
140 | buflen = min(MAXBSIZE, nbytes); |
141 | if (buflen < va.va_blocksize) |
142 | buflen = va.va_blocksize; |
143 | tbuf = malloc(buflen, M_TEMP, M_WAITOK); |
144 | |
145 | vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); |
146 | off = fp->f_offset; |
147 | again: |
148 | aiov.iov_base = tbuf; |
149 | aiov.iov_len = buflen; |
150 | auio.uio_iov = &aiov; |
151 | auio.uio_iovcnt = 1; |
152 | auio.uio_rw = UIO_READ; |
153 | auio.uio_resid = buflen; |
154 | auio.uio_offset = off; |
155 | UIO_SETUP_SYSSPACE(&auio); |
156 | /* |
157 | * First we read into the malloc'ed buffer, then |
158 | * we massage it into user space, one record at a time. |
159 | */ |
160 | error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf, |
161 | &ncookies); |
162 | if (error) |
163 | goto out; |
164 | |
165 | inp = tbuf; |
166 | outp = SCARG(uap, buf); |
167 | resid = nbytes; |
168 | if ((len = buflen - auio.uio_resid) == 0) |
169 | goto eof; |
170 | |
171 | for (cookie = cookiebuf; len > 0; len -= reclen) { |
172 | bdp = (struct dirent *)inp; |
173 | reclen = bdp->d_reclen; |
174 | if (reclen & 3) |
175 | panic(__func__); |
176 | if (bdp->d_fileno == 0) { |
177 | inp += reclen; /* it is a hole; squish it out */ |
178 | if (cookie) |
179 | off = *cookie++; |
180 | else |
181 | off += reclen; |
182 | continue; |
183 | } |
184 | old_reclen = _DIRENT_RECLEN(&idb, bdp->d_namlen); |
185 | if (reclen > len || resid < old_reclen) { |
186 | /* entry too big for buffer, so just stop */ |
187 | outp++; |
188 | break; |
189 | } |
190 | /* |
191 | * Massage in place to make a Dirent12-shaped dirent (otherwise |
192 | * we have to worry about touching user memory outside of |
193 | * the copyout() call). |
194 | */ |
195 | idb.d_fileno = (uint32_t)bdp->d_fileno; |
196 | idb.d_reclen = (uint16_t)old_reclen; |
197 | idb.d_type = (uint8_t)bdp->d_type; |
198 | idb.d_namlen = (uint8_t)bdp->d_namlen; |
199 | strcpy(idb.d_name, bdp->d_name); |
200 | if ((error = copyout(&idb, outp, old_reclen))) |
201 | goto out; |
202 | /* advance past this real entry */ |
203 | inp += reclen; |
204 | if (cookie) |
205 | off = *cookie++; /* each entry points to itself */ |
206 | else |
207 | off += reclen; |
208 | /* advance output past Dirent12-shaped entry */ |
209 | outp += old_reclen; |
210 | resid -= old_reclen; |
211 | } |
212 | |
213 | /* if we squished out the whole block, try again */ |
214 | if (outp == SCARG(uap, buf)) { |
215 | if (cookiebuf) |
216 | free(cookiebuf, M_TEMP); |
217 | cookiebuf = NULL; |
218 | goto again; |
219 | } |
220 | fp->f_offset = off; /* update the vnode offset */ |
221 | |
222 | eof: |
223 | *retval = nbytes - resid; |
224 | out: |
225 | VOP_UNLOCK(vp); |
226 | if (cookiebuf) |
227 | free(cookiebuf, M_TEMP); |
228 | free(tbuf, M_TEMP); |
229 | out1: |
230 | fd_putfile(SCARG(uap, fd)); |
231 | if (error) |
232 | return error; |
233 | return copyout(&loff, SCARG(uap, basep), sizeof(long)); |
234 | } |
235 | |
236 | /* |
237 | * Get file status; this version follows links. |
238 | */ |
239 | /* ARGSUSED */ |
240 | int |
241 | compat_12_sys_stat(struct lwp *l, const struct compat_12_sys_stat_args *uap, register_t *retval) |
242 | { |
243 | /* { |
244 | syscallarg(const char *) path; |
245 | syscallarg(struct stat12 *) ub; |
246 | } */ |
247 | struct stat sb; |
248 | struct stat12 osb; |
249 | int error; |
250 | |
251 | error = do_sys_stat(SCARG(uap, path), FOLLOW, &sb); |
252 | if (error) |
253 | return (error); |
254 | compat_12_stat_conv(&sb, &osb); |
255 | error = copyout(&osb, SCARG(uap, ub), sizeof (osb)); |
256 | return (error); |
257 | } |
258 | |
259 | |
260 | /* |
261 | * Get file status; this version does not follow links. |
262 | */ |
263 | /* ARGSUSED */ |
264 | int |
265 | compat_12_sys_lstat(struct lwp *l, const struct compat_12_sys_lstat_args *uap, register_t *retval) |
266 | { |
267 | /* { |
268 | syscallarg(const char *) path; |
269 | syscallarg(struct stat12 *) ub; |
270 | } */ |
271 | struct stat sb; |
272 | struct stat12 osb; |
273 | int error; |
274 | |
275 | error = do_sys_stat(SCARG(uap, path), NOFOLLOW, &sb); |
276 | if (error) |
277 | return (error); |
278 | compat_12_stat_conv(&sb, &osb); |
279 | error = copyout(&osb, SCARG(uap, ub), sizeof (osb)); |
280 | return (error); |
281 | } |
282 | |
283 | /* |
284 | * Return status information about a file descriptor. |
285 | */ |
286 | /* ARGSUSED */ |
287 | int |
288 | compat_12_sys_fstat(struct lwp *l, const struct compat_12_sys_fstat_args *uap, register_t *retval) |
289 | { |
290 | /* { |
291 | syscallarg(int) fd; |
292 | syscallarg(struct stat12 *) sb; |
293 | } */ |
294 | struct stat ub; |
295 | struct stat12 oub; |
296 | int error; |
297 | |
298 | error = do_sys_fstat(SCARG(uap, fd), &ub); |
299 | if (error == 0) { |
300 | compat_12_stat_conv(&ub, &oub); |
301 | error = copyout(&oub, SCARG(uap, sb), sizeof (oub)); |
302 | } |
303 | return (error); |
304 | } |
305 | |