vfs_syscalls_30.c revision 1.13 1 1.13 martin /* $NetBSD: vfs_syscalls_30.c,v 1.13 2006/07/31 16:34:43 martin Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (c) 2005 The NetBSD Foundation, Inc.
5 1.1 christos * All rights reserved.
6 1.1 christos *
7 1.1 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.1 christos * by Christos Zoulas.
9 1.1 christos *
10 1.1 christos * Redistribution and use in source and binary forms, with or without
11 1.1 christos * modification, are permitted provided that the following conditions
12 1.1 christos * are met:
13 1.1 christos * 1. Redistributions of source code must retain the above copyright
14 1.1 christos * notice, this list of conditions and the following disclaimer.
15 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 christos * notice, this list of conditions and the following disclaimer in the
17 1.1 christos * documentation and/or other materials provided with the distribution.
18 1.1 christos * 3. All advertising materials mentioning features or use of this software
19 1.1 christos * must display the following acknowledgement:
20 1.1 christos * This product includes software developed by the NetBSD
21 1.1 christos * Foundation, Inc. and its contributors.
22 1.1 christos * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 christos * contributors may be used to endorse or promote products derived
24 1.1 christos * from this software without specific prior written permission.
25 1.1 christos *
26 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 christos * POSSIBILITY OF SUCH DAMAGE.
37 1.1 christos */
38 1.1 christos #include <sys/cdefs.h>
39 1.13 martin __KERNEL_RCSID(0, "$NetBSD: vfs_syscalls_30.c,v 1.13 2006/07/31 16:34:43 martin Exp $");
40 1.1 christos
41 1.1 christos #include <sys/param.h>
42 1.1 christos #include <sys/systm.h>
43 1.1 christos #include <sys/namei.h>
44 1.1 christos #include <sys/filedesc.h>
45 1.1 christos #include <sys/kernel.h>
46 1.1 christos #include <sys/file.h>
47 1.1 christos #include <sys/stat.h>
48 1.1 christos #include <sys/socketvar.h>
49 1.1 christos #include <sys/vnode.h>
50 1.1 christos #include <sys/mount.h>
51 1.1 christos #include <sys/proc.h>
52 1.1 christos #include <sys/uio.h>
53 1.1 christos #include <sys/dirent.h>
54 1.1 christos #include <sys/malloc.h>
55 1.9 elad #include <sys/kauth.h>
56 1.1 christos
57 1.1 christos #include <sys/sa.h>
58 1.1 christos #include <sys/syscallargs.h>
59 1.1 christos
60 1.1 christos #include <compat/common/compat_util.h>
61 1.5 christos #include <compat/sys/stat.h>
62 1.5 christos #include <compat/sys/dirent.h>
63 1.10 martin #include <compat/sys/mount.h>
64 1.1 christos
65 1.1 christos static void cvtstat(struct stat13 *, const struct stat *);
66 1.1 christos
67 1.1 christos /*
68 1.1 christos * Convert from a new to an old stat structure.
69 1.1 christos */
70 1.1 christos static void
71 1.1 christos cvtstat(struct stat13 *ost, const struct stat *st)
72 1.1 christos {
73 1.1 christos
74 1.1 christos ost->st_dev = st->st_dev;
75 1.1 christos ost->st_ino = (uint32_t)st->st_ino;
76 1.1 christos ost->st_mode = st->st_mode;
77 1.1 christos ost->st_nlink = st->st_nlink;
78 1.1 christos ost->st_uid = st->st_uid;
79 1.1 christos ost->st_gid = st->st_gid;
80 1.1 christos ost->st_rdev = st->st_rdev;
81 1.1 christos ost->st_atimespec = st->st_atimespec;
82 1.1 christos ost->st_mtimespec = st->st_mtimespec;
83 1.1 christos ost->st_ctimespec = st->st_ctimespec;
84 1.1 christos ost->st_birthtimespec = st->st_birthtimespec;
85 1.1 christos ost->st_size = st->st_size;
86 1.1 christos ost->st_blocks = st->st_blocks;
87 1.1 christos ost->st_blksize = st->st_blksize;
88 1.1 christos ost->st_flags = st->st_flags;
89 1.1 christos ost->st_gen = st->st_gen;
90 1.1 christos }
91 1.1 christos
92 1.1 christos /*
93 1.1 christos * Get file status; this version follows links.
94 1.1 christos */
95 1.1 christos /* ARGSUSED */
96 1.1 christos int
97 1.1 christos compat_30_sys___stat13(struct lwp *l, void *v, register_t *retval)
98 1.1 christos {
99 1.1 christos struct compat_30_sys___stat13_args /* {
100 1.1 christos syscallarg(const char *) path;
101 1.1 christos syscallarg(struct stat13 *) ub;
102 1.1 christos } */ *uap = v;
103 1.1 christos struct stat sb;
104 1.1 christos struct stat13 osb;
105 1.1 christos int error;
106 1.1 christos struct nameidata nd;
107 1.1 christos
108 1.1 christos NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
109 1.6 christos SCARG(uap, path), l);
110 1.1 christos if ((error = namei(&nd)) != 0)
111 1.1 christos return error;
112 1.6 christos error = vn_stat(nd.ni_vp, &sb, l);
113 1.1 christos vput(nd.ni_vp);
114 1.1 christos if (error)
115 1.1 christos return error;
116 1.1 christos cvtstat(&osb, &sb);
117 1.1 christos error = copyout(&osb, SCARG(uap, ub), sizeof (osb));
118 1.1 christos return error;
119 1.1 christos }
120 1.1 christos
121 1.1 christos
122 1.1 christos /*
123 1.1 christos * Get file status; this version does not follow links.
124 1.1 christos */
125 1.1 christos /* ARGSUSED */
126 1.1 christos int
127 1.1 christos compat_30_sys___lstat13(struct lwp *l, void *v, register_t *retval)
128 1.1 christos {
129 1.1 christos struct compat_30_sys___lstat13_args /* {
130 1.1 christos syscallarg(const char *) path;
131 1.1 christos syscallarg(struct stat13 *) ub;
132 1.1 christos } */ *uap = v;
133 1.1 christos struct stat sb;
134 1.1 christos struct stat13 osb;
135 1.1 christos int error;
136 1.1 christos struct nameidata nd;
137 1.1 christos
138 1.1 christos NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
139 1.6 christos SCARG(uap, path), l);
140 1.1 christos if ((error = namei(&nd)) != 0)
141 1.1 christos return error;
142 1.6 christos error = vn_stat(nd.ni_vp, &sb, l);
143 1.1 christos vput(nd.ni_vp);
144 1.1 christos if (error)
145 1.1 christos return error;
146 1.1 christos cvtstat(&osb, &sb);
147 1.1 christos error = copyout(&osb, SCARG(uap, ub), sizeof (osb));
148 1.1 christos return error;
149 1.1 christos }
150 1.1 christos
151 1.8 christos /* ARGSUSED */
152 1.8 christos int
153 1.8 christos compat_30_sys_fhstat(struct lwp *l, void *v, register_t *retval)
154 1.8 christos {
155 1.8 christos struct compat_30_sys_fhstat_args /* {
156 1.10 martin syscallarg(const struct compat_30_fhandle *) fhp;
157 1.8 christos syscallarg(struct stat13 *) sb;
158 1.8 christos } */ *uap = v;
159 1.8 christos struct stat sb;
160 1.8 christos struct stat13 osb;
161 1.8 christos int error;
162 1.10 martin struct compat_30_fhandle fh;
163 1.8 christos struct mount *mp;
164 1.8 christos struct vnode *vp;
165 1.8 christos
166 1.8 christos /*
167 1.8 christos * Must be super user
168 1.8 christos */
169 1.12 ad if ((error = kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
170 1.12 ad &l->l_acflag)))
171 1.8 christos return (error);
172 1.8 christos
173 1.10 martin if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fh))) != 0)
174 1.8 christos return (error);
175 1.8 christos
176 1.8 christos if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
177 1.8 christos return (ESTALE);
178 1.8 christos if (mp->mnt_op->vfs_fhtovp == NULL)
179 1.8 christos return EOPNOTSUPP;
180 1.10 martin if ((error = VFS_FHTOVP(mp, (struct fid*)&fh.fh_fid, &vp)))
181 1.8 christos return (error);
182 1.8 christos error = vn_stat(vp, &sb, l);
183 1.8 christos vput(vp);
184 1.8 christos if (error)
185 1.8 christos return (error);
186 1.8 christos cvtstat(&osb, &sb);
187 1.8 christos error = copyout(&osb, SCARG(uap, sb), sizeof(sb));
188 1.8 christos return (error);
189 1.8 christos }
190 1.8 christos
191 1.1 christos /*
192 1.1 christos * Return status information about a file descriptor.
193 1.1 christos */
194 1.1 christos /* ARGSUSED */
195 1.1 christos int
196 1.1 christos compat_30_sys___fstat13(struct lwp *l, void *v, register_t *retval)
197 1.1 christos {
198 1.1 christos struct compat_30_sys___fstat13_args /* {
199 1.1 christos syscallarg(int) fd;
200 1.1 christos syscallarg(struct stat13 *) sb;
201 1.1 christos } */ *uap = v;
202 1.1 christos struct proc *p = l->l_proc;
203 1.1 christos int fd = SCARG(uap, fd);
204 1.1 christos struct filedesc *fdp = p->p_fd;
205 1.1 christos struct file *fp;
206 1.1 christos struct stat sb;
207 1.1 christos struct stat13 osb;
208 1.1 christos int error;
209 1.1 christos
210 1.1 christos if ((fp = fd_getfile(fdp, fd)) == NULL)
211 1.1 christos return EBADF;
212 1.1 christos
213 1.1 christos FILE_USE(fp);
214 1.6 christos error = (*fp->f_ops->fo_stat)(fp, &sb, l);
215 1.6 christos FILE_UNUSE(fp, l);
216 1.1 christos
217 1.1 christos if (error)
218 1.1 christos return error;
219 1.1 christos cvtstat(&osb, &sb);
220 1.1 christos error = copyout(&osb, SCARG(uap, sb), sizeof (osb));
221 1.1 christos return error;
222 1.1 christos }
223 1.1 christos
224 1.1 christos /*
225 1.1 christos * Read a block of directory entries in a file system independent format.
226 1.1 christos */
227 1.1 christos int
228 1.1 christos compat_30_sys_getdents(struct lwp *l, void *v, register_t *retval)
229 1.1 christos {
230 1.1 christos struct compat_30_sys_getdents_args /* {
231 1.1 christos syscallarg(int) fd;
232 1.1 christos syscallarg(char *) buf;
233 1.1 christos syscallarg(size_t) count;
234 1.1 christos } */ *uap = v;
235 1.1 christos struct proc *p = l->l_proc;
236 1.1 christos struct dirent *bdp;
237 1.1 christos struct vnode *vp;
238 1.1 christos caddr_t inp, tbuf; /* BSD-format */
239 1.1 christos int len, reclen; /* BSD-format */
240 1.4 yamt caddr_t outp; /* NetBSD-3.0-format */
241 1.1 christos int resid;
242 1.1 christos struct file *fp;
243 1.1 christos struct uio auio;
244 1.1 christos struct iovec aiov;
245 1.1 christos struct dirent12 idb;
246 1.1 christos off_t off; /* true file offset */
247 1.1 christos int buflen, error, eofflag;
248 1.1 christos off_t *cookiebuf = NULL, *cookie;
249 1.1 christos int ncookies;
250 1.1 christos
251 1.1 christos /* getvnode() will use the descriptor for us */
252 1.1 christos if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
253 1.1 christos return error;
254 1.1 christos
255 1.1 christos if ((fp->f_flag & FREAD) == 0) {
256 1.1 christos error = EBADF;
257 1.1 christos goto out1;
258 1.1 christos }
259 1.1 christos
260 1.1 christos vp = (struct vnode *)fp->f_data;
261 1.1 christos if (vp->v_type != VDIR) {
262 1.1 christos error = EINVAL;
263 1.1 christos goto out1;
264 1.1 christos }
265 1.1 christos
266 1.1 christos buflen = min(MAXBSIZE, SCARG(uap, count));
267 1.1 christos tbuf = malloc(buflen, M_TEMP, M_WAITOK);
268 1.1 christos vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
269 1.1 christos off = fp->f_offset;
270 1.1 christos again:
271 1.1 christos aiov.iov_base = tbuf;
272 1.1 christos aiov.iov_len = buflen;
273 1.1 christos auio.uio_iov = &aiov;
274 1.1 christos auio.uio_iovcnt = 1;
275 1.1 christos auio.uio_rw = UIO_READ;
276 1.1 christos auio.uio_resid = buflen;
277 1.1 christos auio.uio_offset = off;
278 1.7 yamt UIO_SETUP_SYSSPACE(&auio);
279 1.1 christos /*
280 1.1 christos * First we read into the malloc'ed buffer, then
281 1.1 christos * we massage it into user space, one record at a time.
282 1.1 christos */
283 1.1 christos error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
284 1.1 christos &ncookies);
285 1.1 christos if (error)
286 1.1 christos goto out;
287 1.1 christos
288 1.1 christos inp = tbuf;
289 1.1 christos outp = SCARG(uap, buf);
290 1.1 christos resid = SCARG(uap, count);
291 1.1 christos if ((len = buflen - auio.uio_resid) == 0)
292 1.1 christos goto eof;
293 1.1 christos
294 1.1 christos for (cookie = cookiebuf; len > 0; len -= reclen) {
295 1.1 christos bdp = (struct dirent *)inp;
296 1.1 christos reclen = bdp->d_reclen;
297 1.2 christos if (reclen & _DIRENT_ALIGN(bdp))
298 1.1 christos panic("netbsd30_getdents: bad reclen %d", reclen);
299 1.1 christos if (cookie)
300 1.1 christos off = *cookie++; /* each entry points to the next */
301 1.1 christos else
302 1.1 christos off += reclen;
303 1.1 christos if ((off >> 32) != 0) {
304 1.4 yamt compat_offseterr(vp, "netbsd30_getdents");
305 1.1 christos error = EINVAL;
306 1.1 christos goto out;
307 1.1 christos }
308 1.1 christos if (bdp->d_namlen >= sizeof(idb.d_name))
309 1.1 christos idb.d_namlen = sizeof(idb.d_name) - 1;
310 1.1 christos else
311 1.1 christos idb.d_namlen = bdp->d_namlen;
312 1.1 christos idb.d_reclen = _DIRENT_SIZE(&idb);
313 1.1 christos if (reclen > len || resid < idb.d_reclen) {
314 1.1 christos /* entry too big for buffer, so just stop */
315 1.1 christos outp++;
316 1.1 christos break;
317 1.1 christos }
318 1.1 christos /*
319 1.4 yamt * Massage in place to make a NetBSD-3.0-shaped dirent
320 1.1 christos * (otherwise we have to worry about touching user memory
321 1.1 christos * outside of the copyout() call).
322 1.1 christos */
323 1.1 christos idb.d_fileno = (u_int32_t)bdp->d_fileno;
324 1.1 christos idb.d_type = bdp->d_type;
325 1.1 christos (void)memcpy(idb.d_name, bdp->d_name, idb.d_namlen);
326 1.3 yamt memset(idb.d_name + idb.d_namlen, 0,
327 1.3 yamt idb.d_reclen - _DIRENT_NAMEOFF(&idb) - idb.d_namlen);
328 1.1 christos if ((error = copyout(&idb, outp, idb.d_reclen)) != 0)
329 1.1 christos goto out;
330 1.1 christos /* advance past this real entry */
331 1.1 christos inp += reclen;
332 1.4 yamt /* advance output past NetBSD-3.0-shaped entry */
333 1.1 christos outp += idb.d_reclen;
334 1.1 christos resid -= idb.d_reclen;
335 1.1 christos }
336 1.1 christos
337 1.1 christos /* if we squished out the whole block, try again */
338 1.1 christos if (outp == SCARG(uap, buf))
339 1.1 christos goto again;
340 1.1 christos fp->f_offset = off; /* update the vnode offset */
341 1.1 christos
342 1.1 christos eof:
343 1.1 christos *retval = SCARG(uap, count) - resid;
344 1.1 christos out:
345 1.1 christos VOP_UNLOCK(vp, 0);
346 1.1 christos if (cookiebuf)
347 1.1 christos free(cookiebuf, M_TEMP);
348 1.1 christos free(tbuf, M_TEMP);
349 1.1 christos out1:
350 1.6 christos FILE_UNUSE(fp, l);
351 1.1 christos return error;
352 1.1 christos }
353 1.10 martin
354 1.10 martin /*
355 1.10 martin * Get file handle system call
356 1.10 martin */
357 1.10 martin int
358 1.10 martin compat_30_sys_getfh(struct lwp *l, void *v, register_t *retval)
359 1.10 martin {
360 1.10 martin struct compat_30_sys_getfh_args /* {
361 1.10 martin syscallarg(char *) fname;
362 1.10 martin syscallarg(struct compat_30_fhandle *) fhp;
363 1.10 martin } */ *uap = v;
364 1.10 martin struct vnode *vp;
365 1.11 yamt struct compat_30_fhandle fh;
366 1.10 martin int error;
367 1.10 martin struct nameidata nd;
368 1.10 martin size_t sz;
369 1.10 martin
370 1.10 martin /*
371 1.10 martin * Must be super user
372 1.10 martin */
373 1.12 ad error = kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
374 1.12 ad &l->l_acflag);
375 1.10 martin if (error)
376 1.10 martin return (error);
377 1.10 martin NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
378 1.10 martin SCARG(uap, fname), l);
379 1.10 martin error = namei(&nd);
380 1.10 martin if (error)
381 1.10 martin return (error);
382 1.10 martin vp = nd.ni_vp;
383 1.10 martin sz = sizeof(struct compat_30_fhandle);
384 1.11 yamt error = vfs_composefh(vp, (void *)&fh, &sz);
385 1.10 martin vput(vp);
386 1.10 martin if (error)
387 1.10 martin return (error);
388 1.10 martin error = copyout(&fh, (caddr_t)SCARG(uap, fhp),
389 1.10 martin sizeof(struct compat_30_fhandle));
390 1.10 martin return (error);
391 1.10 martin }
392 1.13 martin
393 1.13 martin /*
394 1.13 martin * Open a file given a file handle.
395 1.13 martin *
396 1.13 martin * Check permissions, allocate an open file structure,
397 1.13 martin * and call the device open routine if any.
398 1.13 martin */
399 1.13 martin int
400 1.13 martin compat_30_sys_fhopen(struct lwp *l, void *v, register_t *retval)
401 1.13 martin {
402 1.13 martin struct compat_30_sys_fhopen_args /* {
403 1.13 martin syscallarg(const fhandle_t *) fhp;
404 1.13 martin syscallarg(int) flags;
405 1.13 martin } */ *uap = v;
406 1.13 martin struct filedesc *fdp = l->l_proc->p_fd;
407 1.13 martin struct file *fp;
408 1.13 martin struct vnode *vp = NULL;
409 1.13 martin struct mount *mp;
410 1.13 martin kauth_cred_t cred = l->l_cred;
411 1.13 martin int flags;
412 1.13 martin struct file *nfp;
413 1.13 martin int type, indx, error=0;
414 1.13 martin struct flock lf;
415 1.13 martin struct vattr va;
416 1.13 martin fhandle_t *fh;
417 1.13 martin
418 1.13 martin /*
419 1.13 martin * Must be super user
420 1.13 martin */
421 1.13 martin if ((error = kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
422 1.13 martin &l->l_acflag)))
423 1.13 martin return (error);
424 1.13 martin
425 1.13 martin flags = FFLAGS(SCARG(uap, flags));
426 1.13 martin if ((flags & (FREAD | FWRITE)) == 0)
427 1.13 martin return (EINVAL);
428 1.13 martin if ((flags & O_CREAT))
429 1.13 martin return (EINVAL);
430 1.13 martin /* falloc() will use the file descriptor for us */
431 1.13 martin if ((error = falloc(l, &nfp, &indx)) != 0)
432 1.13 martin return (error);
433 1.13 martin fp = nfp;
434 1.13 martin error = vfs_copyinfh_alloc(SCARG(uap, fhp), &fh);
435 1.13 martin if (error != 0) {
436 1.13 martin goto bad;
437 1.13 martin }
438 1.13 martin error = vfs_fhtovp(fh, &vp);
439 1.13 martin if (error != 0) {
440 1.13 martin goto bad;
441 1.13 martin }
442 1.13 martin
443 1.13 martin /* Now do an effective vn_open */
444 1.13 martin
445 1.13 martin if (vp->v_type == VSOCK) {
446 1.13 martin error = EOPNOTSUPP;
447 1.13 martin goto bad;
448 1.13 martin }
449 1.13 martin if (flags & FREAD) {
450 1.13 martin if ((error = VOP_ACCESS(vp, VREAD, cred, l)) != 0)
451 1.13 martin goto bad;
452 1.13 martin }
453 1.13 martin if (flags & (FWRITE | O_TRUNC)) {
454 1.13 martin if (vp->v_type == VDIR) {
455 1.13 martin error = EISDIR;
456 1.13 martin goto bad;
457 1.13 martin }
458 1.13 martin if ((error = vn_writechk(vp)) != 0 ||
459 1.13 martin (error = VOP_ACCESS(vp, VWRITE, cred, l)) != 0)
460 1.13 martin goto bad;
461 1.13 martin }
462 1.13 martin if (flags & O_TRUNC) {
463 1.13 martin if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
464 1.13 martin goto bad;
465 1.13 martin VOP_UNLOCK(vp, 0); /* XXX */
466 1.13 martin VOP_LEASE(vp, l, cred, LEASE_WRITE);
467 1.13 martin vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
468 1.13 martin VATTR_NULL(&va);
469 1.13 martin va.va_size = 0;
470 1.13 martin error = VOP_SETATTR(vp, &va, cred, l);
471 1.13 martin vn_finished_write(mp, 0);
472 1.13 martin if (error)
473 1.13 martin goto bad;
474 1.13 martin }
475 1.13 martin if ((error = VOP_OPEN(vp, flags, cred, l)) != 0)
476 1.13 martin goto bad;
477 1.13 martin if (vp->v_type == VREG &&
478 1.13 martin uvn_attach(vp, flags & FWRITE ? VM_PROT_WRITE : 0) == NULL) {
479 1.13 martin error = EIO;
480 1.13 martin goto bad;
481 1.13 martin }
482 1.13 martin if (flags & FWRITE)
483 1.13 martin vp->v_writecount++;
484 1.13 martin
485 1.13 martin /* done with modified vn_open, now finish what sys_open does. */
486 1.13 martin
487 1.13 martin fp->f_flag = flags & FMASK;
488 1.13 martin fp->f_type = DTYPE_VNODE;
489 1.13 martin fp->f_ops = &vnops;
490 1.13 martin fp->f_data = vp;
491 1.13 martin if (flags & (O_EXLOCK | O_SHLOCK)) {
492 1.13 martin lf.l_whence = SEEK_SET;
493 1.13 martin lf.l_start = 0;
494 1.13 martin lf.l_len = 0;
495 1.13 martin if (flags & O_EXLOCK)
496 1.13 martin lf.l_type = F_WRLCK;
497 1.13 martin else
498 1.13 martin lf.l_type = F_RDLCK;
499 1.13 martin type = F_FLOCK;
500 1.13 martin if ((flags & FNONBLOCK) == 0)
501 1.13 martin type |= F_WAIT;
502 1.13 martin VOP_UNLOCK(vp, 0);
503 1.13 martin error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf, type);
504 1.13 martin if (error) {
505 1.13 martin (void) vn_close(vp, fp->f_flag, fp->f_cred, l);
506 1.13 martin FILE_UNUSE(fp, l);
507 1.13 martin ffree(fp);
508 1.13 martin fdremove(fdp, indx);
509 1.13 martin return (error);
510 1.13 martin }
511 1.13 martin vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
512 1.13 martin fp->f_flag |= FHASLOCK;
513 1.13 martin }
514 1.13 martin VOP_UNLOCK(vp, 0);
515 1.13 martin *retval = indx;
516 1.13 martin FILE_SET_MATURE(fp);
517 1.13 martin FILE_UNUSE(fp, l);
518 1.13 martin vfs_copyinfh_free(fh);
519 1.13 martin return (0);
520 1.13 martin
521 1.13 martin bad:
522 1.13 martin FILE_UNUSE(fp, l);
523 1.13 martin ffree(fp);
524 1.13 martin fdremove(fdp, indx);
525 1.13 martin if (vp != NULL)
526 1.13 martin vput(vp);
527 1.13 martin vfs_copyinfh_free(fh);
528 1.13 martin return (error);
529 1.13 martin }
530 1.13 martin
531 1.13 martin /* ARGSUSED */
532 1.13 martin int
533 1.13 martin compat_30_sys___fhstat30(struct lwp *l, void *v, register_t *retval)
534 1.13 martin {
535 1.13 martin struct compat_30_sys___fhstat30_args /* {
536 1.13 martin syscallarg(const fhandle_t *) fhp;
537 1.13 martin syscallarg(struct stat *) sb;
538 1.13 martin } */ *uap = v;
539 1.13 martin struct stat sb;
540 1.13 martin int error;
541 1.13 martin fhandle_t *fh;
542 1.13 martin struct vnode *vp;
543 1.13 martin
544 1.13 martin /*
545 1.13 martin * Must be super user
546 1.13 martin */
547 1.13 martin if ((error = kauth_authorize_generic(l->l_cred,
548 1.13 martin KAUTH_GENERIC_ISSUSER, &l->l_acflag)) != 0)
549 1.13 martin return (error);
550 1.13 martin
551 1.13 martin error = vfs_copyinfh_alloc(SCARG(uap, fhp), &fh);
552 1.13 martin if (error != 0) {
553 1.13 martin goto bad;
554 1.13 martin }
555 1.13 martin error = vfs_fhtovp(fh, &vp);
556 1.13 martin if (error != 0) {
557 1.13 martin goto bad;
558 1.13 martin }
559 1.13 martin error = vn_stat(vp, &sb, l);
560 1.13 martin vput(vp);
561 1.13 martin if (error) {
562 1.13 martin goto bad;
563 1.13 martin }
564 1.13 martin error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
565 1.13 martin bad:
566 1.13 martin vfs_copyinfh_free(fh);
567 1.13 martin return error;
568 1.13 martin }
569 1.13 martin
570 1.13 martin /* ARGSUSED */
571 1.13 martin int
572 1.13 martin compat_30_sys_fhstatvfs1(struct lwp *l, void *v, register_t *retval)
573 1.13 martin {
574 1.13 martin struct compat_30_sys_fhstatvfs1_args /* {
575 1.13 martin syscallarg(const fhandle_t *) fhp;
576 1.13 martin syscallarg(struct statvfs *) buf;
577 1.13 martin syscallarg(int) flags;
578 1.13 martin } */ *uap = v;
579 1.13 martin struct statvfs *sb = NULL;
580 1.13 martin fhandle_t *fh;
581 1.13 martin struct mount *mp;
582 1.13 martin struct vnode *vp;
583 1.13 martin int error;
584 1.13 martin
585 1.13 martin /*
586 1.13 martin * Must be super user
587 1.13 martin */
588 1.13 martin if ((error = kauth_authorize_generic(l->l_cred,
589 1.13 martin KAUTH_GENERIC_ISSUSER, &l->l_acflag)) != 0)
590 1.13 martin return error;
591 1.13 martin
592 1.13 martin error = vfs_copyinfh_alloc(SCARG(uap, fhp), &fh);
593 1.13 martin if (error != 0) {
594 1.13 martin goto out;
595 1.13 martin }
596 1.13 martin error = vfs_fhtovp(fh, &vp);
597 1.13 martin if (error != 0) {
598 1.13 martin goto out;
599 1.13 martin }
600 1.13 martin mp = vp->v_mount;
601 1.13 martin sb = STATVFSBUF_GET();
602 1.13 martin if ((error = dostatvfs(mp, sb, l, SCARG(uap, flags), 1)) != 0) {
603 1.13 martin vput(vp);
604 1.13 martin goto out;
605 1.13 martin }
606 1.13 martin vput(vp);
607 1.13 martin error = copyout(sb, SCARG(uap, buf), sizeof(*sb));
608 1.13 martin out:
609 1.13 martin if (sb != NULL) {
610 1.13 martin STATVFSBUF_PUT(sb);
611 1.13 martin }
612 1.13 martin vfs_copyinfh_free(fh);
613 1.13 martin return error;
614 1.13 martin }
615