vfs_syscalls.c revision 1.429 1 /* $NetBSD: vfs_syscalls.c,v 1.429 2011/06/12 03:35:57 rmind Exp $ */
2
3 /*-
4 * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran.
9 *
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.
18 *
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.
30 */
31
32 /*
33 * Copyright (c) 1989, 1993
34 * The Regents of the University of California. All rights reserved.
35 * (c) UNIX System Laboratories, Inc.
36 * All or some portions of this file are derived from material licensed
37 * to the University of California by American Telephone and Telegraph
38 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
39 * the permission of UNIX System Laboratories, Inc.
40 *
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
43 * are met:
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE.
64 *
65 * @(#)vfs_syscalls.c 8.42 (Berkeley) 7/31/95
66 */
67
68 /*
69 * Virtual File System System Calls
70 */
71
72 #include <sys/cdefs.h>
73 __KERNEL_RCSID(0, "$NetBSD: vfs_syscalls.c,v 1.429 2011/06/12 03:35:57 rmind Exp $");
74
75 #ifdef _KERNEL_OPT
76 #include "opt_fileassoc.h"
77 #include "veriexec.h"
78 #endif
79
80 #include <sys/param.h>
81 #include <sys/systm.h>
82 #include <sys/namei.h>
83 #include <sys/filedesc.h>
84 #include <sys/kernel.h>
85 #include <sys/file.h>
86 #include <sys/stat.h>
87 #include <sys/vnode.h>
88 #include <sys/mount.h>
89 #include <sys/proc.h>
90 #include <sys/uio.h>
91 #include <sys/kmem.h>
92 #include <sys/dirent.h>
93 #include <sys/sysctl.h>
94 #include <sys/syscallargs.h>
95 #include <sys/vfs_syscalls.h>
96 #include <sys/ktrace.h>
97 #ifdef FILEASSOC
98 #include <sys/fileassoc.h>
99 #endif /* FILEASSOC */
100 #include <sys/verified_exec.h>
101 #include <sys/kauth.h>
102 #include <sys/atomic.h>
103 #include <sys/module.h>
104 #include <sys/buf.h>
105
106 #include <miscfs/genfs/genfs.h>
107 #include <miscfs/syncfs/syncfs.h>
108 #include <miscfs/specfs/specdev.h>
109
110 #include <nfs/rpcv2.h>
111 #include <nfs/nfsproto.h>
112 #include <nfs/nfs.h>
113 #include <nfs/nfs_var.h>
114
115 static int change_flags(struct vnode *, u_long, struct lwp *);
116 static int change_mode(struct vnode *, int, struct lwp *l);
117 static int change_owner(struct vnode *, uid_t, gid_t, struct lwp *, int);
118
119 /*
120 * This table is used to maintain compatibility with 4.3BSD
121 * and NetBSD 0.9 mount syscalls - and possibly other systems.
122 * Note, the order is important!
123 *
124 * Do not modify this table. It should only contain filesystems
125 * supported by NetBSD 0.9 and 4.3BSD.
126 */
127 const char * const mountcompatnames[] = {
128 NULL, /* 0 = MOUNT_NONE */
129 MOUNT_FFS, /* 1 = MOUNT_UFS */
130 MOUNT_NFS, /* 2 */
131 MOUNT_MFS, /* 3 */
132 MOUNT_MSDOS, /* 4 */
133 MOUNT_CD9660, /* 5 = MOUNT_ISOFS */
134 MOUNT_FDESC, /* 6 */
135 MOUNT_KERNFS, /* 7 */
136 NULL, /* 8 = MOUNT_DEVFS */
137 MOUNT_AFS, /* 9 */
138 };
139
140 const int nmountcompatnames = __arraycount(mountcompatnames);
141
142 static int
143 open_setfp(struct lwp *l, file_t *fp, struct vnode *vp, int indx, int flags)
144 {
145 int error;
146
147 fp->f_flag = flags & FMASK;
148 fp->f_type = DTYPE_VNODE;
149 fp->f_ops = &vnops;
150 fp->f_data = vp;
151
152 if (flags & (O_EXLOCK | O_SHLOCK)) {
153 struct flock lf;
154 int type;
155
156 lf.l_whence = SEEK_SET;
157 lf.l_start = 0;
158 lf.l_len = 0;
159 if (flags & O_EXLOCK)
160 lf.l_type = F_WRLCK;
161 else
162 lf.l_type = F_RDLCK;
163 type = F_FLOCK;
164 if ((flags & FNONBLOCK) == 0)
165 type |= F_WAIT;
166 VOP_UNLOCK(vp);
167 error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf, type);
168 if (error) {
169 (void) vn_close(vp, fp->f_flag, fp->f_cred);
170 fd_abort(l->l_proc, fp, indx);
171 return error;
172 }
173 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
174 atomic_or_uint(&fp->f_flag, FHASLOCK);
175 }
176 if (flags & O_CLOEXEC)
177 fd_set_exclose(l, indx, true);
178 return 0;
179 }
180
181 static int
182 mount_update(struct lwp *l, struct vnode *vp, const char *path, int flags,
183 void *data, size_t *data_len)
184 {
185 struct mount *mp;
186 int error = 0, saved_flags;
187
188 mp = vp->v_mount;
189 saved_flags = mp->mnt_flag;
190
191 /* We can operate only on VV_ROOT nodes. */
192 if ((vp->v_vflag & VV_ROOT) == 0) {
193 error = EINVAL;
194 goto out;
195 }
196
197 /*
198 * We only allow the filesystem to be reloaded if it
199 * is currently mounted read-only. Additionally, we
200 * prevent read-write to read-only downgrades.
201 */
202 if ((flags & (MNT_RELOAD | MNT_RDONLY)) != 0 &&
203 (mp->mnt_flag & MNT_RDONLY) == 0 &&
204 (mp->mnt_iflag & IMNT_CAN_RWTORO) == 0) {
205 error = EOPNOTSUPP; /* Needs translation */
206 goto out;
207 }
208
209 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
210 KAUTH_REQ_SYSTEM_MOUNT_UPDATE, mp, KAUTH_ARG(flags), data);
211 if (error)
212 goto out;
213
214 if (vfs_busy(mp, NULL)) {
215 error = EPERM;
216 goto out;
217 }
218
219 mutex_enter(&mp->mnt_updating);
220
221 mp->mnt_flag &= ~MNT_OP_FLAGS;
222 mp->mnt_flag |= flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
223
224 /*
225 * Set the mount level flags.
226 */
227 if (flags & MNT_RDONLY)
228 mp->mnt_flag |= MNT_RDONLY;
229 else if (mp->mnt_flag & MNT_RDONLY)
230 mp->mnt_iflag |= IMNT_WANTRDWR;
231 mp->mnt_flag &=
232 ~(MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
233 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOCOREDUMP |
234 MNT_NOATIME | MNT_NODEVMTIME | MNT_SYMPERM | MNT_SOFTDEP |
235 MNT_LOG);
236 mp->mnt_flag |= flags &
237 (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
238 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOCOREDUMP |
239 MNT_NOATIME | MNT_NODEVMTIME | MNT_SYMPERM | MNT_SOFTDEP |
240 MNT_LOG | MNT_IGNORE);
241
242 error = VFS_MOUNT(mp, path, data, data_len);
243
244 if (error && data != NULL) {
245 int error2;
246
247 /*
248 * Update failed; let's try and see if it was an
249 * export request. For compat with 3.0 and earlier.
250 */
251 error2 = vfs_hooks_reexport(mp, path, data);
252
253 /*
254 * Only update error code if the export request was
255 * understood but some problem occurred while
256 * processing it.
257 */
258 if (error2 != EJUSTRETURN)
259 error = error2;
260 }
261
262 if (mp->mnt_iflag & IMNT_WANTRDWR)
263 mp->mnt_flag &= ~MNT_RDONLY;
264 if (error)
265 mp->mnt_flag = saved_flags;
266 mp->mnt_flag &= ~MNT_OP_FLAGS;
267 mp->mnt_iflag &= ~IMNT_WANTRDWR;
268 if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0) {
269 if (mp->mnt_syncer == NULL)
270 error = vfs_allocate_syncvnode(mp);
271 } else {
272 if (mp->mnt_syncer != NULL)
273 vfs_deallocate_syncvnode(mp);
274 }
275 mutex_exit(&mp->mnt_updating);
276 vfs_unbusy(mp, false, NULL);
277
278 out:
279 return (error);
280 }
281
282 static int
283 mount_get_vfsops(const char *fstype, struct vfsops **vfsops)
284 {
285 char fstypename[sizeof(((struct statvfs *)NULL)->f_fstypename)];
286 int error;
287
288 /* Copy file-system type from userspace. */
289 error = copyinstr(fstype, fstypename, sizeof(fstypename), NULL);
290 if (error) {
291 /*
292 * Historically, filesystem types were identified by numbers.
293 * If we get an integer for the filesystem type instead of a
294 * string, we check to see if it matches one of the historic
295 * filesystem types.
296 */
297 u_long fsindex = (u_long)fstype;
298 if (fsindex >= nmountcompatnames ||
299 mountcompatnames[fsindex] == NULL)
300 return ENODEV;
301 strlcpy(fstypename, mountcompatnames[fsindex],
302 sizeof(fstypename));
303 }
304
305 /* Accept `ufs' as an alias for `ffs', for compatibility. */
306 if (strcmp(fstypename, "ufs") == 0)
307 fstypename[0] = 'f';
308
309 if ((*vfsops = vfs_getopsbyname(fstypename)) != NULL)
310 return 0;
311
312 /* If we can autoload a vfs module, try again */
313 (void)module_autoload(fstypename, MODULE_CLASS_VFS);
314
315 if ((*vfsops = vfs_getopsbyname(fstypename)) != NULL)
316 return 0;
317
318 return ENODEV;
319 }
320
321 static int
322 mount_getargs(struct lwp *l, struct vnode *vp, const char *path, int flags,
323 void *data, size_t *data_len)
324 {
325 struct mount *mp;
326 int error;
327
328 /* If MNT_GETARGS is specified, it should be the only flag. */
329 if (flags & ~MNT_GETARGS)
330 return EINVAL;
331
332 mp = vp->v_mount;
333
334 /* XXX: probably some notion of "can see" here if we want isolation. */
335 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
336 KAUTH_REQ_SYSTEM_MOUNT_GET, mp, data, NULL);
337 if (error)
338 return error;
339
340 if ((vp->v_vflag & VV_ROOT) == 0)
341 return EINVAL;
342
343 if (vfs_busy(mp, NULL))
344 return EPERM;
345
346 mutex_enter(&mp->mnt_updating);
347 mp->mnt_flag &= ~MNT_OP_FLAGS;
348 mp->mnt_flag |= MNT_GETARGS;
349 error = VFS_MOUNT(mp, path, data, data_len);
350 mp->mnt_flag &= ~MNT_OP_FLAGS;
351 mutex_exit(&mp->mnt_updating);
352
353 vfs_unbusy(mp, false, NULL);
354 return (error);
355 }
356
357 int
358 sys___mount50(struct lwp *l, const struct sys___mount50_args *uap, register_t *retval)
359 {
360 /* {
361 syscallarg(const char *) type;
362 syscallarg(const char *) path;
363 syscallarg(int) flags;
364 syscallarg(void *) data;
365 syscallarg(size_t) data_len;
366 } */
367
368 return do_sys_mount(l, NULL, SCARG(uap, type), SCARG(uap, path),
369 SCARG(uap, flags), SCARG(uap, data), UIO_USERSPACE,
370 SCARG(uap, data_len), retval);
371 }
372
373 int
374 do_sys_mount(struct lwp *l, struct vfsops *vfsops, const char *type,
375 const char *path, int flags, void *data, enum uio_seg data_seg,
376 size_t data_len, register_t *retval)
377 {
378 struct vnode *vp;
379 void *data_buf = data;
380 bool vfsopsrele = false;
381 int error;
382
383 /* XXX: The calling convention of this routine is totally bizarre */
384 if (vfsops)
385 vfsopsrele = true;
386
387 /*
388 * Get vnode to be covered
389 */
390 error = namei_simple_user(path, NSM_FOLLOW_TRYEMULROOT, &vp);
391 if (error != 0) {
392 vp = NULL;
393 goto done;
394 }
395
396 if (vfsops == NULL) {
397 if (flags & (MNT_GETARGS | MNT_UPDATE)) {
398 vfsops = vp->v_mount->mnt_op;
399 } else {
400 /* 'type' is userspace */
401 error = mount_get_vfsops(type, &vfsops);
402 if (error != 0)
403 goto done;
404 vfsopsrele = true;
405 }
406 }
407
408 if (data != NULL && data_seg == UIO_USERSPACE) {
409 if (data_len == 0) {
410 /* No length supplied, use default for filesystem */
411 data_len = vfsops->vfs_min_mount_data;
412 if (data_len > VFS_MAX_MOUNT_DATA) {
413 error = EINVAL;
414 goto done;
415 }
416 /*
417 * Hopefully a longer buffer won't make copyin() fail.
418 * For compatibility with 3.0 and earlier.
419 */
420 if (flags & MNT_UPDATE
421 && data_len < sizeof (struct mnt_export_args30))
422 data_len = sizeof (struct mnt_export_args30);
423 }
424 data_buf = kmem_alloc(data_len, KM_SLEEP);
425
426 /* NFS needs the buffer even for mnt_getargs .... */
427 error = copyin(data, data_buf, data_len);
428 if (error != 0)
429 goto done;
430 }
431
432 if (flags & MNT_GETARGS) {
433 if (data_len == 0) {
434 error = EINVAL;
435 goto done;
436 }
437 error = mount_getargs(l, vp, path, flags, data_buf, &data_len);
438 if (error != 0)
439 goto done;
440 if (data_seg == UIO_USERSPACE)
441 error = copyout(data_buf, data, data_len);
442 *retval = data_len;
443 } else if (flags & MNT_UPDATE) {
444 error = mount_update(l, vp, path, flags, data_buf, &data_len);
445 } else {
446 /* Locking is handled internally in mount_domount(). */
447 KASSERT(vfsopsrele == true);
448 error = mount_domount(l, &vp, vfsops, path, flags, data_buf,
449 &data_len);
450 vfsopsrele = false;
451 }
452
453 done:
454 if (vfsopsrele)
455 vfs_delref(vfsops);
456 if (vp != NULL) {
457 vrele(vp);
458 }
459 if (data_buf != data)
460 kmem_free(data_buf, data_len);
461 return (error);
462 }
463
464 /*
465 * Unmount a file system.
466 *
467 * Note: unmount takes a path to the vnode mounted on as argument,
468 * not special file (as before).
469 */
470 /* ARGSUSED */
471 int
472 sys_unmount(struct lwp *l, const struct sys_unmount_args *uap, register_t *retval)
473 {
474 /* {
475 syscallarg(const char *) path;
476 syscallarg(int) flags;
477 } */
478 struct vnode *vp;
479 struct mount *mp;
480 int error;
481 struct pathbuf *pb;
482 struct nameidata nd;
483
484 error = pathbuf_copyin(SCARG(uap, path), &pb);
485 if (error) {
486 return error;
487 }
488
489 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb);
490 if ((error = namei(&nd)) != 0) {
491 pathbuf_destroy(pb);
492 return error;
493 }
494 vp = nd.ni_vp;
495 pathbuf_destroy(pb);
496
497 mp = vp->v_mount;
498 atomic_inc_uint(&mp->mnt_refcnt);
499 VOP_UNLOCK(vp);
500
501 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
502 KAUTH_REQ_SYSTEM_MOUNT_UNMOUNT, mp, NULL, NULL);
503 if (error) {
504 vrele(vp);
505 vfs_destroy(mp);
506 return (error);
507 }
508
509 /*
510 * Don't allow unmounting the root file system.
511 */
512 if (mp->mnt_flag & MNT_ROOTFS) {
513 vrele(vp);
514 vfs_destroy(mp);
515 return (EINVAL);
516 }
517
518 /*
519 * Must be the root of the filesystem
520 */
521 if ((vp->v_vflag & VV_ROOT) == 0) {
522 vrele(vp);
523 vfs_destroy(mp);
524 return (EINVAL);
525 }
526
527 vrele(vp);
528 error = dounmount(mp, SCARG(uap, flags), l);
529 vfs_destroy(mp);
530 return error;
531 }
532
533 /*
534 * Sync each mounted filesystem.
535 */
536 #ifdef DEBUG
537 int syncprt = 0;
538 struct ctldebug debug0 = { "syncprt", &syncprt };
539 #endif
540
541 void
542 do_sys_sync(struct lwp *l)
543 {
544 struct mount *mp, *nmp;
545 int asyncflag;
546
547 mutex_enter(&mountlist_lock);
548 for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
549 mp = nmp) {
550 if (vfs_busy(mp, &nmp)) {
551 continue;
552 }
553 mutex_enter(&mp->mnt_updating);
554 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
555 asyncflag = mp->mnt_flag & MNT_ASYNC;
556 mp->mnt_flag &= ~MNT_ASYNC;
557 VFS_SYNC(mp, MNT_NOWAIT, l->l_cred);
558 if (asyncflag)
559 mp->mnt_flag |= MNT_ASYNC;
560 }
561 mutex_exit(&mp->mnt_updating);
562 vfs_unbusy(mp, false, &nmp);
563 }
564 mutex_exit(&mountlist_lock);
565 #ifdef DEBUG
566 if (syncprt)
567 vfs_bufstats();
568 #endif /* DEBUG */
569 }
570
571 /* ARGSUSED */
572 int
573 sys_sync(struct lwp *l, const void *v, register_t *retval)
574 {
575 do_sys_sync(l);
576 return (0);
577 }
578
579
580 /*
581 * Change filesystem quotas.
582 */
583 /* ARGSUSED */
584 int
585 sys___quotactl50(struct lwp *l, const struct sys___quotactl50_args *uap,
586 register_t *retval)
587 {
588 /* {
589 syscallarg(const char *) path;
590 syscallarg(struct plistref *) pref;
591 } */
592 struct mount *mp;
593 int error;
594 struct vnode *vp;
595 prop_dictionary_t dict;
596 struct plistref pref;
597
598 error = namei_simple_user(SCARG(uap, path),
599 NSM_FOLLOW_TRYEMULROOT, &vp);
600 if (error != 0)
601 return (error);
602 mp = vp->v_mount;
603 error = copyin(SCARG(uap, pref), &pref, sizeof(pref));
604 if (error)
605 return error;
606 error = prop_dictionary_copyin(&pref, &dict);
607 if (error)
608 return error;
609 error = VFS_QUOTACTL(mp, dict);
610 vrele(vp);
611 if (!error)
612 error = prop_dictionary_copyout(&pref, dict);
613 if (!error)
614 error = copyout(&pref, SCARG(uap, pref), sizeof(pref));
615 prop_object_release(dict);
616 return (error);
617 }
618
619 int
620 dostatvfs(struct mount *mp, struct statvfs *sp, struct lwp *l, int flags,
621 int root)
622 {
623 struct cwdinfo *cwdi = l->l_proc->p_cwdi;
624 int error = 0;
625
626 /*
627 * If MNT_NOWAIT or MNT_LAZY is specified, do not
628 * refresh the fsstat cache. MNT_WAIT or MNT_LAZY
629 * overrides MNT_NOWAIT.
630 */
631 if (flags == MNT_NOWAIT || flags == MNT_LAZY ||
632 (flags != MNT_WAIT && flags != 0)) {
633 memcpy(sp, &mp->mnt_stat, sizeof(*sp));
634 goto done;
635 }
636
637 /* Get the filesystem stats now */
638 memset(sp, 0, sizeof(*sp));
639 if ((error = VFS_STATVFS(mp, sp)) != 0) {
640 return error;
641 }
642
643 if (cwdi->cwdi_rdir == NULL)
644 (void)memcpy(&mp->mnt_stat, sp, sizeof(mp->mnt_stat));
645 done:
646 if (cwdi->cwdi_rdir != NULL) {
647 size_t len;
648 char *bp;
649 char c;
650 char *path = PNBUF_GET();
651
652 bp = path + MAXPATHLEN;
653 *--bp = '\0';
654 rw_enter(&cwdi->cwdi_lock, RW_READER);
655 error = getcwd_common(cwdi->cwdi_rdir, rootvnode, &bp, path,
656 MAXPATHLEN / 2, 0, l);
657 rw_exit(&cwdi->cwdi_lock);
658 if (error) {
659 PNBUF_PUT(path);
660 return error;
661 }
662 len = strlen(bp);
663 if (len != 1) {
664 /*
665 * for mount points that are below our root, we can see
666 * them, so we fix up the pathname and return them. The
667 * rest we cannot see, so we don't allow viewing the
668 * data.
669 */
670 if (strncmp(bp, sp->f_mntonname, len) == 0 &&
671 ((c = sp->f_mntonname[len]) == '/' || c == '\0')) {
672 (void)strlcpy(sp->f_mntonname,
673 c == '\0' ? "/" : &sp->f_mntonname[len],
674 sizeof(sp->f_mntonname));
675 } else {
676 if (root)
677 (void)strlcpy(sp->f_mntonname, "/",
678 sizeof(sp->f_mntonname));
679 else
680 error = EPERM;
681 }
682 }
683 PNBUF_PUT(path);
684 }
685 sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
686 return error;
687 }
688
689 /*
690 * Get filesystem statistics by path.
691 */
692 int
693 do_sys_pstatvfs(struct lwp *l, const char *path, int flags, struct statvfs *sb)
694 {
695 struct mount *mp;
696 int error;
697 struct vnode *vp;
698
699 error = namei_simple_user(path, NSM_FOLLOW_TRYEMULROOT, &vp);
700 if (error != 0)
701 return error;
702 mp = vp->v_mount;
703 error = dostatvfs(mp, sb, l, flags, 1);
704 vrele(vp);
705 return error;
706 }
707
708 /* ARGSUSED */
709 int
710 sys_statvfs1(struct lwp *l, const struct sys_statvfs1_args *uap, register_t *retval)
711 {
712 /* {
713 syscallarg(const char *) path;
714 syscallarg(struct statvfs *) buf;
715 syscallarg(int) flags;
716 } */
717 struct statvfs *sb;
718 int error;
719
720 sb = STATVFSBUF_GET();
721 error = do_sys_pstatvfs(l, SCARG(uap, path), SCARG(uap, flags), sb);
722 if (error == 0)
723 error = copyout(sb, SCARG(uap, buf), sizeof(*sb));
724 STATVFSBUF_PUT(sb);
725 return error;
726 }
727
728 /*
729 * Get filesystem statistics by fd.
730 */
731 int
732 do_sys_fstatvfs(struct lwp *l, int fd, int flags, struct statvfs *sb)
733 {
734 file_t *fp;
735 struct mount *mp;
736 int error;
737
738 /* fd_getvnode() will use the descriptor for us */
739 if ((error = fd_getvnode(fd, &fp)) != 0)
740 return (error);
741 mp = ((struct vnode *)fp->f_data)->v_mount;
742 error = dostatvfs(mp, sb, curlwp, flags, 1);
743 fd_putfile(fd);
744 return error;
745 }
746
747 /* ARGSUSED */
748 int
749 sys_fstatvfs1(struct lwp *l, const struct sys_fstatvfs1_args *uap, register_t *retval)
750 {
751 /* {
752 syscallarg(int) fd;
753 syscallarg(struct statvfs *) buf;
754 syscallarg(int) flags;
755 } */
756 struct statvfs *sb;
757 int error;
758
759 sb = STATVFSBUF_GET();
760 error = do_sys_fstatvfs(l, SCARG(uap, fd), SCARG(uap, flags), sb);
761 if (error == 0)
762 error = copyout(sb, SCARG(uap, buf), sizeof(*sb));
763 STATVFSBUF_PUT(sb);
764 return error;
765 }
766
767
768 /*
769 * Get statistics on all filesystems.
770 */
771 int
772 do_sys_getvfsstat(struct lwp *l, void *sfsp, size_t bufsize, int flags,
773 int (*copyfn)(const void *, void *, size_t), size_t entry_sz,
774 register_t *retval)
775 {
776 int root = 0;
777 struct proc *p = l->l_proc;
778 struct mount *mp, *nmp;
779 struct statvfs *sb;
780 size_t count, maxcount;
781 int error = 0;
782
783 sb = STATVFSBUF_GET();
784 maxcount = bufsize / entry_sz;
785 mutex_enter(&mountlist_lock);
786 count = 0;
787 for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
788 mp = nmp) {
789 if (vfs_busy(mp, &nmp)) {
790 continue;
791 }
792 if (sfsp && count < maxcount) {
793 error = dostatvfs(mp, sb, l, flags, 0);
794 if (error) {
795 vfs_unbusy(mp, false, &nmp);
796 error = 0;
797 continue;
798 }
799 error = copyfn(sb, sfsp, entry_sz);
800 if (error) {
801 vfs_unbusy(mp, false, NULL);
802 goto out;
803 }
804 sfsp = (char *)sfsp + entry_sz;
805 root |= strcmp(sb->f_mntonname, "/") == 0;
806 }
807 count++;
808 vfs_unbusy(mp, false, &nmp);
809 }
810 mutex_exit(&mountlist_lock);
811
812 if (root == 0 && p->p_cwdi->cwdi_rdir) {
813 /*
814 * fake a root entry
815 */
816 error = dostatvfs(p->p_cwdi->cwdi_rdir->v_mount,
817 sb, l, flags, 1);
818 if (error != 0)
819 goto out;
820 if (sfsp) {
821 error = copyfn(sb, sfsp, entry_sz);
822 if (error != 0)
823 goto out;
824 }
825 count++;
826 }
827 if (sfsp && count > maxcount)
828 *retval = maxcount;
829 else
830 *retval = count;
831 out:
832 STATVFSBUF_PUT(sb);
833 return error;
834 }
835
836 int
837 sys_getvfsstat(struct lwp *l, const struct sys_getvfsstat_args *uap, register_t *retval)
838 {
839 /* {
840 syscallarg(struct statvfs *) buf;
841 syscallarg(size_t) bufsize;
842 syscallarg(int) flags;
843 } */
844
845 return do_sys_getvfsstat(l, SCARG(uap, buf), SCARG(uap, bufsize),
846 SCARG(uap, flags), copyout, sizeof (struct statvfs), retval);
847 }
848
849 /*
850 * Change current working directory to a given file descriptor.
851 */
852 /* ARGSUSED */
853 int
854 sys_fchdir(struct lwp *l, const struct sys_fchdir_args *uap, register_t *retval)
855 {
856 /* {
857 syscallarg(int) fd;
858 } */
859 struct proc *p = l->l_proc;
860 struct cwdinfo *cwdi;
861 struct vnode *vp, *tdp;
862 struct mount *mp;
863 file_t *fp;
864 int error, fd;
865
866 /* fd_getvnode() will use the descriptor for us */
867 fd = SCARG(uap, fd);
868 if ((error = fd_getvnode(fd, &fp)) != 0)
869 return (error);
870 vp = fp->f_data;
871
872 vref(vp);
873 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
874 if (vp->v_type != VDIR)
875 error = ENOTDIR;
876 else
877 error = VOP_ACCESS(vp, VEXEC, l->l_cred);
878 if (error) {
879 vput(vp);
880 goto out;
881 }
882 while ((mp = vp->v_mountedhere) != NULL) {
883 error = vfs_busy(mp, NULL);
884 vput(vp);
885 if (error != 0)
886 goto out;
887 error = VFS_ROOT(mp, &tdp);
888 vfs_unbusy(mp, false, NULL);
889 if (error)
890 goto out;
891 vp = tdp;
892 }
893 VOP_UNLOCK(vp);
894
895 /*
896 * Disallow changing to a directory not under the process's
897 * current root directory (if there is one).
898 */
899 cwdi = p->p_cwdi;
900 rw_enter(&cwdi->cwdi_lock, RW_WRITER);
901 if (cwdi->cwdi_rdir && !vn_isunder(vp, NULL, l)) {
902 vrele(vp);
903 error = EPERM; /* operation not permitted */
904 } else {
905 vrele(cwdi->cwdi_cdir);
906 cwdi->cwdi_cdir = vp;
907 }
908 rw_exit(&cwdi->cwdi_lock);
909
910 out:
911 fd_putfile(fd);
912 return (error);
913 }
914
915 /*
916 * Change this process's notion of the root directory to a given file
917 * descriptor.
918 */
919 int
920 sys_fchroot(struct lwp *l, const struct sys_fchroot_args *uap, register_t *retval)
921 {
922 struct proc *p = l->l_proc;
923 struct vnode *vp;
924 file_t *fp;
925 int error, fd = SCARG(uap, fd);
926
927 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_CHROOT,
928 KAUTH_REQ_SYSTEM_CHROOT_FCHROOT, NULL, NULL, NULL)) != 0)
929 return error;
930 /* fd_getvnode() will use the descriptor for us */
931 if ((error = fd_getvnode(fd, &fp)) != 0)
932 return error;
933 vp = fp->f_data;
934 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
935 if (vp->v_type != VDIR)
936 error = ENOTDIR;
937 else
938 error = VOP_ACCESS(vp, VEXEC, l->l_cred);
939 VOP_UNLOCK(vp);
940 if (error)
941 goto out;
942 vref(vp);
943
944 change_root(p->p_cwdi, vp, l);
945
946 out:
947 fd_putfile(fd);
948 return (error);
949 }
950
951 /*
952 * Change current working directory (``.'').
953 */
954 /* ARGSUSED */
955 int
956 sys_chdir(struct lwp *l, const struct sys_chdir_args *uap, register_t *retval)
957 {
958 /* {
959 syscallarg(const char *) path;
960 } */
961 struct proc *p = l->l_proc;
962 struct cwdinfo *cwdi;
963 int error;
964 struct vnode *vp;
965
966 if ((error = chdir_lookup(SCARG(uap, path), UIO_USERSPACE,
967 &vp, l)) != 0)
968 return (error);
969 cwdi = p->p_cwdi;
970 rw_enter(&cwdi->cwdi_lock, RW_WRITER);
971 vrele(cwdi->cwdi_cdir);
972 cwdi->cwdi_cdir = vp;
973 rw_exit(&cwdi->cwdi_lock);
974 return (0);
975 }
976
977 /*
978 * Change notion of root (``/'') directory.
979 */
980 /* ARGSUSED */
981 int
982 sys_chroot(struct lwp *l, const struct sys_chroot_args *uap, register_t *retval)
983 {
984 /* {
985 syscallarg(const char *) path;
986 } */
987 struct proc *p = l->l_proc;
988 int error;
989 struct vnode *vp;
990
991 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_CHROOT,
992 KAUTH_REQ_SYSTEM_CHROOT_CHROOT, NULL, NULL, NULL)) != 0)
993 return (error);
994 if ((error = chdir_lookup(SCARG(uap, path), UIO_USERSPACE,
995 &vp, l)) != 0)
996 return (error);
997
998 change_root(p->p_cwdi, vp, l);
999
1000 return (0);
1001 }
1002
1003 /*
1004 * Common routine for chroot and fchroot.
1005 * NB: callers need to properly authorize the change root operation.
1006 */
1007 void
1008 change_root(struct cwdinfo *cwdi, struct vnode *vp, struct lwp *l)
1009 {
1010
1011 rw_enter(&cwdi->cwdi_lock, RW_WRITER);
1012 if (cwdi->cwdi_rdir != NULL)
1013 vrele(cwdi->cwdi_rdir);
1014 cwdi->cwdi_rdir = vp;
1015
1016 /*
1017 * Prevent escaping from chroot by putting the root under
1018 * the working directory. Silently chdir to / if we aren't
1019 * already there.
1020 */
1021 if (!vn_isunder(cwdi->cwdi_cdir, vp, l)) {
1022 /*
1023 * XXX would be more failsafe to change directory to a
1024 * deadfs node here instead
1025 */
1026 vrele(cwdi->cwdi_cdir);
1027 vref(vp);
1028 cwdi->cwdi_cdir = vp;
1029 }
1030 rw_exit(&cwdi->cwdi_lock);
1031 }
1032
1033 /*
1034 * Common routine for chroot and chdir.
1035 * XXX "where" should be enum uio_seg
1036 */
1037 int
1038 chdir_lookup(const char *path, int where, struct vnode **vpp, struct lwp *l)
1039 {
1040 struct pathbuf *pb;
1041 struct nameidata nd;
1042 int error;
1043
1044 error = pathbuf_maybe_copyin(path, where, &pb);
1045 if (error) {
1046 return error;
1047 }
1048 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb);
1049 if ((error = namei(&nd)) != 0) {
1050 pathbuf_destroy(pb);
1051 return error;
1052 }
1053 *vpp = nd.ni_vp;
1054 pathbuf_destroy(pb);
1055
1056 if ((*vpp)->v_type != VDIR)
1057 error = ENOTDIR;
1058 else
1059 error = VOP_ACCESS(*vpp, VEXEC, l->l_cred);
1060
1061 if (error)
1062 vput(*vpp);
1063 else
1064 VOP_UNLOCK(*vpp);
1065 return (error);
1066 }
1067
1068 /*
1069 * Check permissions, allocate an open file structure,
1070 * and call the device open routine if any.
1071 */
1072 int
1073 sys_open(struct lwp *l, const struct sys_open_args *uap, register_t *retval)
1074 {
1075 /* {
1076 syscallarg(const char *) path;
1077 syscallarg(int) flags;
1078 syscallarg(int) mode;
1079 } */
1080 struct proc *p = l->l_proc;
1081 struct cwdinfo *cwdi = p->p_cwdi;
1082 file_t *fp;
1083 struct vnode *vp;
1084 int flags, cmode;
1085 int indx, error;
1086 struct pathbuf *pb;
1087 struct nameidata nd;
1088
1089 flags = FFLAGS(SCARG(uap, flags));
1090 if ((flags & (FREAD | FWRITE)) == 0)
1091 return (EINVAL);
1092
1093 error = pathbuf_copyin(SCARG(uap, path), &pb);
1094 if (error) {
1095 return error;
1096 }
1097
1098 if ((error = fd_allocfile(&fp, &indx)) != 0) {
1099 pathbuf_destroy(pb);
1100 return error;
1101 }
1102 /* We're going to read cwdi->cwdi_cmask unlocked here. */
1103 cmode = ((SCARG(uap, mode) &~ cwdi->cwdi_cmask) & ALLPERMS) &~ S_ISTXT;
1104 NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, pb);
1105 l->l_dupfd = -indx - 1; /* XXX check for fdopen */
1106 if ((error = vn_open(&nd, flags, cmode)) != 0) {
1107 fd_abort(p, fp, indx);
1108 if ((error == EDUPFD || error == EMOVEFD) &&
1109 l->l_dupfd >= 0 && /* XXX from fdopen */
1110 (error =
1111 fd_dupopen(l->l_dupfd, &indx, flags, error)) == 0) {
1112 *retval = indx;
1113 pathbuf_destroy(pb);
1114 return (0);
1115 }
1116 if (error == ERESTART)
1117 error = EINTR;
1118 pathbuf_destroy(pb);
1119 return (error);
1120 }
1121
1122 l->l_dupfd = 0;
1123 vp = nd.ni_vp;
1124 pathbuf_destroy(pb);
1125
1126 if ((error = open_setfp(l, fp, vp, indx, flags)))
1127 return error;
1128
1129 VOP_UNLOCK(vp);
1130 *retval = indx;
1131 fd_affix(p, fp, indx);
1132 return (0);
1133 }
1134
1135 static void
1136 vfs__fhfree(fhandle_t *fhp)
1137 {
1138 size_t fhsize;
1139
1140 if (fhp == NULL) {
1141 return;
1142 }
1143 fhsize = FHANDLE_SIZE(fhp);
1144 kmem_free(fhp, fhsize);
1145 }
1146
1147 /*
1148 * vfs_composefh: compose a filehandle.
1149 */
1150
1151 int
1152 vfs_composefh(struct vnode *vp, fhandle_t *fhp, size_t *fh_size)
1153 {
1154 struct mount *mp;
1155 struct fid *fidp;
1156 int error;
1157 size_t needfhsize;
1158 size_t fidsize;
1159
1160 mp = vp->v_mount;
1161 fidp = NULL;
1162 if (*fh_size < FHANDLE_SIZE_MIN) {
1163 fidsize = 0;
1164 } else {
1165 fidsize = *fh_size - offsetof(fhandle_t, fh_fid);
1166 if (fhp != NULL) {
1167 memset(fhp, 0, *fh_size);
1168 fhp->fh_fsid = mp->mnt_stat.f_fsidx;
1169 fidp = &fhp->fh_fid;
1170 }
1171 }
1172 error = VFS_VPTOFH(vp, fidp, &fidsize);
1173 needfhsize = FHANDLE_SIZE_FROM_FILEID_SIZE(fidsize);
1174 if (error == 0 && *fh_size < needfhsize) {
1175 error = E2BIG;
1176 }
1177 *fh_size = needfhsize;
1178 return error;
1179 }
1180
1181 int
1182 vfs_composefh_alloc(struct vnode *vp, fhandle_t **fhpp)
1183 {
1184 struct mount *mp;
1185 fhandle_t *fhp;
1186 size_t fhsize;
1187 size_t fidsize;
1188 int error;
1189
1190 *fhpp = NULL;
1191 mp = vp->v_mount;
1192 fidsize = 0;
1193 error = VFS_VPTOFH(vp, NULL, &fidsize);
1194 KASSERT(error != 0);
1195 if (error != E2BIG) {
1196 goto out;
1197 }
1198 fhsize = FHANDLE_SIZE_FROM_FILEID_SIZE(fidsize);
1199 fhp = kmem_zalloc(fhsize, KM_SLEEP);
1200 if (fhp == NULL) {
1201 error = ENOMEM;
1202 goto out;
1203 }
1204 fhp->fh_fsid = mp->mnt_stat.f_fsidx;
1205 error = VFS_VPTOFH(vp, &fhp->fh_fid, &fidsize);
1206 if (error == 0) {
1207 KASSERT((FHANDLE_SIZE(fhp) == fhsize &&
1208 FHANDLE_FILEID(fhp)->fid_len == fidsize));
1209 *fhpp = fhp;
1210 } else {
1211 kmem_free(fhp, fhsize);
1212 }
1213 out:
1214 return error;
1215 }
1216
1217 void
1218 vfs_composefh_free(fhandle_t *fhp)
1219 {
1220
1221 vfs__fhfree(fhp);
1222 }
1223
1224 /*
1225 * vfs_fhtovp: lookup a vnode by a filehandle.
1226 */
1227
1228 int
1229 vfs_fhtovp(fhandle_t *fhp, struct vnode **vpp)
1230 {
1231 struct mount *mp;
1232 int error;
1233
1234 *vpp = NULL;
1235 mp = vfs_getvfs(FHANDLE_FSID(fhp));
1236 if (mp == NULL) {
1237 error = ESTALE;
1238 goto out;
1239 }
1240 if (mp->mnt_op->vfs_fhtovp == NULL) {
1241 error = EOPNOTSUPP;
1242 goto out;
1243 }
1244 error = VFS_FHTOVP(mp, FHANDLE_FILEID(fhp), vpp);
1245 out:
1246 return error;
1247 }
1248
1249 /*
1250 * vfs_copyinfh_alloc: allocate and copyin a filehandle, given
1251 * the needed size.
1252 */
1253
1254 int
1255 vfs_copyinfh_alloc(const void *ufhp, size_t fhsize, fhandle_t **fhpp)
1256 {
1257 fhandle_t *fhp;
1258 int error;
1259
1260 *fhpp = NULL;
1261 if (fhsize > FHANDLE_SIZE_MAX) {
1262 return EINVAL;
1263 }
1264 if (fhsize < FHANDLE_SIZE_MIN) {
1265 return EINVAL;
1266 }
1267 again:
1268 fhp = kmem_alloc(fhsize, KM_SLEEP);
1269 if (fhp == NULL) {
1270 return ENOMEM;
1271 }
1272 error = copyin(ufhp, fhp, fhsize);
1273 if (error == 0) {
1274 /* XXX this check shouldn't be here */
1275 if (FHANDLE_SIZE(fhp) == fhsize) {
1276 *fhpp = fhp;
1277 return 0;
1278 } else if (fhsize == NFSX_V2FH && FHANDLE_SIZE(fhp) < fhsize) {
1279 /*
1280 * a kludge for nfsv2 padded handles.
1281 */
1282 size_t sz;
1283
1284 sz = FHANDLE_SIZE(fhp);
1285 kmem_free(fhp, fhsize);
1286 fhsize = sz;
1287 goto again;
1288 } else {
1289 /*
1290 * userland told us wrong size.
1291 */
1292 error = EINVAL;
1293 }
1294 }
1295 kmem_free(fhp, fhsize);
1296 return error;
1297 }
1298
1299 void
1300 vfs_copyinfh_free(fhandle_t *fhp)
1301 {
1302
1303 vfs__fhfree(fhp);
1304 }
1305
1306 /*
1307 * Get file handle system call
1308 */
1309 int
1310 sys___getfh30(struct lwp *l, const struct sys___getfh30_args *uap, register_t *retval)
1311 {
1312 /* {
1313 syscallarg(char *) fname;
1314 syscallarg(fhandle_t *) fhp;
1315 syscallarg(size_t *) fh_size;
1316 } */
1317 struct vnode *vp;
1318 fhandle_t *fh;
1319 int error;
1320 struct pathbuf *pb;
1321 struct nameidata nd;
1322 size_t sz;
1323 size_t usz;
1324
1325 /*
1326 * Must be super user
1327 */
1328 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
1329 0, NULL, NULL, NULL);
1330 if (error)
1331 return (error);
1332
1333 error = pathbuf_copyin(SCARG(uap, fname), &pb);
1334 if (error) {
1335 return error;
1336 }
1337 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb);
1338 error = namei(&nd);
1339 if (error) {
1340 pathbuf_destroy(pb);
1341 return error;
1342 }
1343 vp = nd.ni_vp;
1344 pathbuf_destroy(pb);
1345
1346 error = vfs_composefh_alloc(vp, &fh);
1347 vput(vp);
1348 if (error != 0) {
1349 goto out;
1350 }
1351 error = copyin(SCARG(uap, fh_size), &usz, sizeof(size_t));
1352 if (error != 0) {
1353 goto out;
1354 }
1355 sz = FHANDLE_SIZE(fh);
1356 error = copyout(&sz, SCARG(uap, fh_size), sizeof(size_t));
1357 if (error != 0) {
1358 goto out;
1359 }
1360 if (usz >= sz) {
1361 error = copyout(fh, SCARG(uap, fhp), sz);
1362 } else {
1363 error = E2BIG;
1364 }
1365 out:
1366 vfs_composefh_free(fh);
1367 return (error);
1368 }
1369
1370 /*
1371 * Open a file given a file handle.
1372 *
1373 * Check permissions, allocate an open file structure,
1374 * and call the device open routine if any.
1375 */
1376
1377 int
1378 dofhopen(struct lwp *l, const void *ufhp, size_t fhsize, int oflags,
1379 register_t *retval)
1380 {
1381 file_t *fp;
1382 struct vnode *vp = NULL;
1383 kauth_cred_t cred = l->l_cred;
1384 file_t *nfp;
1385 int indx, error = 0;
1386 struct vattr va;
1387 fhandle_t *fh;
1388 int flags;
1389 proc_t *p;
1390
1391 p = curproc;
1392
1393 /*
1394 * Must be super user
1395 */
1396 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
1397 0, NULL, NULL, NULL)))
1398 return (error);
1399
1400 flags = FFLAGS(oflags);
1401 if ((flags & (FREAD | FWRITE)) == 0)
1402 return (EINVAL);
1403 if ((flags & O_CREAT))
1404 return (EINVAL);
1405 if ((error = fd_allocfile(&nfp, &indx)) != 0)
1406 return (error);
1407 fp = nfp;
1408 error = vfs_copyinfh_alloc(ufhp, fhsize, &fh);
1409 if (error != 0) {
1410 goto bad;
1411 }
1412 error = vfs_fhtovp(fh, &vp);
1413 if (error != 0) {
1414 goto bad;
1415 }
1416
1417 /* Now do an effective vn_open */
1418
1419 if (vp->v_type == VSOCK) {
1420 error = EOPNOTSUPP;
1421 goto bad;
1422 }
1423 error = vn_openchk(vp, cred, flags);
1424 if (error != 0)
1425 goto bad;
1426 if (flags & O_TRUNC) {
1427 VOP_UNLOCK(vp); /* XXX */
1428 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
1429 vattr_null(&va);
1430 va.va_size = 0;
1431 error = VOP_SETATTR(vp, &va, cred);
1432 if (error)
1433 goto bad;
1434 }
1435 if ((error = VOP_OPEN(vp, flags, cred)) != 0)
1436 goto bad;
1437 if (flags & FWRITE) {
1438 mutex_enter(vp->v_interlock);
1439 vp->v_writecount++;
1440 mutex_exit(vp->v_interlock);
1441 }
1442
1443 /* done with modified vn_open, now finish what sys_open does. */
1444 if ((error = open_setfp(l, fp, vp, indx, flags)))
1445 return error;
1446
1447 VOP_UNLOCK(vp);
1448 *retval = indx;
1449 fd_affix(p, fp, indx);
1450 vfs_copyinfh_free(fh);
1451 return (0);
1452
1453 bad:
1454 fd_abort(p, fp, indx);
1455 if (vp != NULL)
1456 vput(vp);
1457 vfs_copyinfh_free(fh);
1458 return (error);
1459 }
1460
1461 int
1462 sys___fhopen40(struct lwp *l, const struct sys___fhopen40_args *uap, register_t *retval)
1463 {
1464 /* {
1465 syscallarg(const void *) fhp;
1466 syscallarg(size_t) fh_size;
1467 syscallarg(int) flags;
1468 } */
1469
1470 return dofhopen(l, SCARG(uap, fhp), SCARG(uap, fh_size),
1471 SCARG(uap, flags), retval);
1472 }
1473
1474 int
1475 do_fhstat(struct lwp *l, const void *ufhp, size_t fhsize, struct stat *sb)
1476 {
1477 int error;
1478 fhandle_t *fh;
1479 struct vnode *vp;
1480
1481 /*
1482 * Must be super user
1483 */
1484 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
1485 0, NULL, NULL, NULL)))
1486 return (error);
1487
1488 error = vfs_copyinfh_alloc(ufhp, fhsize, &fh);
1489 if (error != 0)
1490 return error;
1491
1492 error = vfs_fhtovp(fh, &vp);
1493 vfs_copyinfh_free(fh);
1494 if (error != 0)
1495 return error;
1496
1497 error = vn_stat(vp, sb);
1498 vput(vp);
1499 return error;
1500 }
1501
1502
1503 /* ARGSUSED */
1504 int
1505 sys___fhstat50(struct lwp *l, const struct sys___fhstat50_args *uap, register_t *retval)
1506 {
1507 /* {
1508 syscallarg(const void *) fhp;
1509 syscallarg(size_t) fh_size;
1510 syscallarg(struct stat *) sb;
1511 } */
1512 struct stat sb;
1513 int error;
1514
1515 error = do_fhstat(l, SCARG(uap, fhp), SCARG(uap, fh_size), &sb);
1516 if (error)
1517 return error;
1518 return copyout(&sb, SCARG(uap, sb), sizeof(sb));
1519 }
1520
1521 int
1522 do_fhstatvfs(struct lwp *l, const void *ufhp, size_t fhsize, struct statvfs *sb,
1523 int flags)
1524 {
1525 fhandle_t *fh;
1526 struct mount *mp;
1527 struct vnode *vp;
1528 int error;
1529
1530 /*
1531 * Must be super user
1532 */
1533 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
1534 0, NULL, NULL, NULL)))
1535 return error;
1536
1537 error = vfs_copyinfh_alloc(ufhp, fhsize, &fh);
1538 if (error != 0)
1539 return error;
1540
1541 error = vfs_fhtovp(fh, &vp);
1542 vfs_copyinfh_free(fh);
1543 if (error != 0)
1544 return error;
1545
1546 mp = vp->v_mount;
1547 error = dostatvfs(mp, sb, l, flags, 1);
1548 vput(vp);
1549 return error;
1550 }
1551
1552 /* ARGSUSED */
1553 int
1554 sys___fhstatvfs140(struct lwp *l, const struct sys___fhstatvfs140_args *uap, register_t *retval)
1555 {
1556 /* {
1557 syscallarg(const void *) fhp;
1558 syscallarg(size_t) fh_size;
1559 syscallarg(struct statvfs *) buf;
1560 syscallarg(int) flags;
1561 } */
1562 struct statvfs *sb = STATVFSBUF_GET();
1563 int error;
1564
1565 error = do_fhstatvfs(l, SCARG(uap, fhp), SCARG(uap, fh_size), sb,
1566 SCARG(uap, flags));
1567 if (error == 0)
1568 error = copyout(sb, SCARG(uap, buf), sizeof(*sb));
1569 STATVFSBUF_PUT(sb);
1570 return error;
1571 }
1572
1573 /*
1574 * Create a special file.
1575 */
1576 /* ARGSUSED */
1577 int
1578 sys___mknod50(struct lwp *l, const struct sys___mknod50_args *uap,
1579 register_t *retval)
1580 {
1581 /* {
1582 syscallarg(const char *) path;
1583 syscallarg(mode_t) mode;
1584 syscallarg(dev_t) dev;
1585 } */
1586 return do_sys_mknod(l, SCARG(uap, path), SCARG(uap, mode),
1587 SCARG(uap, dev), retval, UIO_USERSPACE);
1588 }
1589
1590 int
1591 do_sys_mknod(struct lwp *l, const char *pathname, mode_t mode, dev_t dev,
1592 register_t *retval, enum uio_seg seg)
1593 {
1594 struct proc *p = l->l_proc;
1595 struct vnode *vp;
1596 struct vattr vattr;
1597 int error, optype;
1598 struct pathbuf *pb;
1599 struct nameidata nd;
1600 const char *pathstring;
1601
1602 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MKNOD,
1603 0, NULL, NULL, NULL)) != 0)
1604 return (error);
1605
1606 optype = VOP_MKNOD_DESCOFFSET;
1607
1608 error = pathbuf_maybe_copyin(pathname, seg, &pb);
1609 if (error) {
1610 return error;
1611 }
1612 pathstring = pathbuf_stringcopy_get(pb);
1613 if (pathstring == NULL) {
1614 pathbuf_destroy(pb);
1615 return ENOMEM;
1616 }
1617
1618 NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, pb);
1619 if ((error = namei(&nd)) != 0)
1620 goto out;
1621 vp = nd.ni_vp;
1622
1623 if (vp != NULL)
1624 error = EEXIST;
1625 else {
1626 vattr_null(&vattr);
1627 /* We will read cwdi->cwdi_cmask unlocked. */
1628 vattr.va_mode = (mode & ALLPERMS) &~ p->p_cwdi->cwdi_cmask;
1629 vattr.va_rdev = dev;
1630
1631 switch (mode & S_IFMT) {
1632 case S_IFMT: /* used by badsect to flag bad sectors */
1633 vattr.va_type = VBAD;
1634 break;
1635 case S_IFCHR:
1636 vattr.va_type = VCHR;
1637 break;
1638 case S_IFBLK:
1639 vattr.va_type = VBLK;
1640 break;
1641 case S_IFWHT:
1642 optype = VOP_WHITEOUT_DESCOFFSET;
1643 break;
1644 case S_IFREG:
1645 #if NVERIEXEC > 0
1646 error = veriexec_openchk(l, nd.ni_vp, pathstring,
1647 O_CREAT);
1648 #endif /* NVERIEXEC > 0 */
1649 vattr.va_type = VREG;
1650 vattr.va_rdev = VNOVAL;
1651 optype = VOP_CREATE_DESCOFFSET;
1652 break;
1653 default:
1654 error = EINVAL;
1655 break;
1656 }
1657 }
1658 if (!error) {
1659 switch (optype) {
1660 case VOP_WHITEOUT_DESCOFFSET:
1661 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1662 if (error)
1663 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1664 vput(nd.ni_dvp);
1665 break;
1666
1667 case VOP_MKNOD_DESCOFFSET:
1668 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1669 &nd.ni_cnd, &vattr);
1670 if (error == 0)
1671 vput(nd.ni_vp);
1672 break;
1673
1674 case VOP_CREATE_DESCOFFSET:
1675 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp,
1676 &nd.ni_cnd, &vattr);
1677 if (error == 0)
1678 vput(nd.ni_vp);
1679 break;
1680 }
1681 } else {
1682 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1683 if (nd.ni_dvp == vp)
1684 vrele(nd.ni_dvp);
1685 else
1686 vput(nd.ni_dvp);
1687 if (vp)
1688 vrele(vp);
1689 }
1690 out:
1691 pathbuf_stringcopy_put(pb, pathstring);
1692 pathbuf_destroy(pb);
1693 return (error);
1694 }
1695
1696 /*
1697 * Create a named pipe.
1698 */
1699 /* ARGSUSED */
1700 int
1701 sys_mkfifo(struct lwp *l, const struct sys_mkfifo_args *uap, register_t *retval)
1702 {
1703 /* {
1704 syscallarg(const char *) path;
1705 syscallarg(int) mode;
1706 } */
1707 struct proc *p = l->l_proc;
1708 struct vattr vattr;
1709 int error;
1710 struct pathbuf *pb;
1711 struct nameidata nd;
1712
1713 error = pathbuf_copyin(SCARG(uap, path), &pb);
1714 if (error) {
1715 return error;
1716 }
1717 NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, pb);
1718 if ((error = namei(&nd)) != 0) {
1719 pathbuf_destroy(pb);
1720 return error;
1721 }
1722 if (nd.ni_vp != NULL) {
1723 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1724 if (nd.ni_dvp == nd.ni_vp)
1725 vrele(nd.ni_dvp);
1726 else
1727 vput(nd.ni_dvp);
1728 vrele(nd.ni_vp);
1729 pathbuf_destroy(pb);
1730 return (EEXIST);
1731 }
1732 vattr_null(&vattr);
1733 vattr.va_type = VFIFO;
1734 /* We will read cwdi->cwdi_cmask unlocked. */
1735 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_cwdi->cwdi_cmask;
1736 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1737 if (error == 0)
1738 vput(nd.ni_vp);
1739 pathbuf_destroy(pb);
1740 return (error);
1741 }
1742
1743 /*
1744 * Make a hard file link.
1745 */
1746 /* ARGSUSED */
1747 int
1748 sys_link(struct lwp *l, const struct sys_link_args *uap, register_t *retval)
1749 {
1750 /* {
1751 syscallarg(const char *) path;
1752 syscallarg(const char *) link;
1753 } */
1754 struct vnode *vp;
1755 struct pathbuf *linkpb;
1756 struct nameidata nd;
1757 int error;
1758
1759 error = namei_simple_user(SCARG(uap, path),
1760 NSM_FOLLOW_TRYEMULROOT, &vp);
1761 if (error != 0)
1762 return (error);
1763 error = pathbuf_copyin(SCARG(uap, link), &linkpb);
1764 if (error) {
1765 goto out1;
1766 }
1767 NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, linkpb);
1768 if ((error = namei(&nd)) != 0)
1769 goto out2;
1770 if (nd.ni_vp) {
1771 error = EEXIST;
1772 goto abortop;
1773 }
1774 /* Prevent hard links on directories. */
1775 if (vp->v_type == VDIR) {
1776 error = EPERM;
1777 goto abortop;
1778 }
1779 /* Prevent cross-mount operation. */
1780 if (nd.ni_dvp->v_mount != vp->v_mount) {
1781 error = EXDEV;
1782 goto abortop;
1783 }
1784 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1785 out2:
1786 pathbuf_destroy(linkpb);
1787 out1:
1788 vrele(vp);
1789 return (error);
1790 abortop:
1791 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1792 if (nd.ni_dvp == nd.ni_vp)
1793 vrele(nd.ni_dvp);
1794 else
1795 vput(nd.ni_dvp);
1796 if (nd.ni_vp != NULL)
1797 vrele(nd.ni_vp);
1798 goto out2;
1799 }
1800
1801 int
1802 do_sys_symlink(const char *patharg, const char *link, enum uio_seg seg)
1803 {
1804 struct proc *p = curproc;
1805 struct vattr vattr;
1806 char *path;
1807 int error;
1808 struct pathbuf *linkpb;
1809 struct nameidata nd;
1810
1811 path = PNBUF_GET();
1812 if (seg == UIO_USERSPACE) {
1813 if ((error = copyinstr(patharg, path, MAXPATHLEN, NULL)) != 0)
1814 goto out1;
1815 if ((error = pathbuf_copyin(link, &linkpb)) != 0)
1816 goto out1;
1817 } else {
1818 KASSERT(strlen(patharg) < MAXPATHLEN);
1819 strcpy(path, patharg);
1820 linkpb = pathbuf_create(link);
1821 if (linkpb == NULL) {
1822 error = ENOMEM;
1823 goto out1;
1824 }
1825 }
1826 NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, linkpb);
1827 if ((error = namei(&nd)) != 0)
1828 goto out2;
1829 if (nd.ni_vp) {
1830 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1831 if (nd.ni_dvp == nd.ni_vp)
1832 vrele(nd.ni_dvp);
1833 else
1834 vput(nd.ni_dvp);
1835 vrele(nd.ni_vp);
1836 error = EEXIST;
1837 goto out2;
1838 }
1839 vattr_null(&vattr);
1840 vattr.va_type = VLNK;
1841 /* We will read cwdi->cwdi_cmask unlocked. */
1842 vattr.va_mode = ACCESSPERMS &~ p->p_cwdi->cwdi_cmask;
1843 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
1844 if (error == 0)
1845 vput(nd.ni_vp);
1846 out2:
1847 pathbuf_destroy(linkpb);
1848 out1:
1849 PNBUF_PUT(path);
1850 return (error);
1851 }
1852
1853 /*
1854 * Make a symbolic link.
1855 */
1856 /* ARGSUSED */
1857 int
1858 sys_symlink(struct lwp *l, const struct sys_symlink_args *uap, register_t *retval)
1859 {
1860 /* {
1861 syscallarg(const char *) path;
1862 syscallarg(const char *) link;
1863 } */
1864
1865 return do_sys_symlink(SCARG(uap, path), SCARG(uap, link),
1866 UIO_USERSPACE);
1867 }
1868
1869 /*
1870 * Delete a whiteout from the filesystem.
1871 */
1872 /* ARGSUSED */
1873 int
1874 sys_undelete(struct lwp *l, const struct sys_undelete_args *uap, register_t *retval)
1875 {
1876 /* {
1877 syscallarg(const char *) path;
1878 } */
1879 int error;
1880 struct pathbuf *pb;
1881 struct nameidata nd;
1882
1883 error = pathbuf_copyin(SCARG(uap, path), &pb);
1884 if (error) {
1885 return error;
1886 }
1887
1888 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | TRYEMULROOT, pb);
1889 error = namei(&nd);
1890 if (error) {
1891 pathbuf_destroy(pb);
1892 return (error);
1893 }
1894
1895 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1896 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1897 if (nd.ni_dvp == nd.ni_vp)
1898 vrele(nd.ni_dvp);
1899 else
1900 vput(nd.ni_dvp);
1901 if (nd.ni_vp)
1902 vrele(nd.ni_vp);
1903 pathbuf_destroy(pb);
1904 return (EEXIST);
1905 }
1906 if ((error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE)) != 0)
1907 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1908 vput(nd.ni_dvp);
1909 pathbuf_destroy(pb);
1910 return (error);
1911 }
1912
1913 /*
1914 * Delete a name from the filesystem.
1915 */
1916 /* ARGSUSED */
1917 int
1918 sys_unlink(struct lwp *l, const struct sys_unlink_args *uap, register_t *retval)
1919 {
1920 /* {
1921 syscallarg(const char *) path;
1922 } */
1923
1924 return do_sys_unlink(SCARG(uap, path), UIO_USERSPACE);
1925 }
1926
1927 int
1928 do_sys_unlink(const char *arg, enum uio_seg seg)
1929 {
1930 struct vnode *vp;
1931 int error;
1932 struct pathbuf *pb;
1933 struct nameidata nd;
1934 const char *pathstring;
1935
1936 error = pathbuf_maybe_copyin(arg, seg, &pb);
1937 if (error) {
1938 return error;
1939 }
1940 pathstring = pathbuf_stringcopy_get(pb);
1941 if (pathstring == NULL) {
1942 pathbuf_destroy(pb);
1943 return ENOMEM;
1944 }
1945
1946 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF | TRYEMULROOT, pb);
1947 if ((error = namei(&nd)) != 0)
1948 goto out;
1949 vp = nd.ni_vp;
1950
1951 /*
1952 * The root of a mounted filesystem cannot be deleted.
1953 */
1954 if (vp->v_vflag & VV_ROOT) {
1955 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1956 if (nd.ni_dvp == vp)
1957 vrele(nd.ni_dvp);
1958 else
1959 vput(nd.ni_dvp);
1960 vput(vp);
1961 error = EBUSY;
1962 goto out;
1963 }
1964
1965 #if NVERIEXEC > 0
1966 /* Handle remove requests for veriexec entries. */
1967 if ((error = veriexec_removechk(curlwp, nd.ni_vp, pathstring)) != 0) {
1968 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1969 if (nd.ni_dvp == vp)
1970 vrele(nd.ni_dvp);
1971 else
1972 vput(nd.ni_dvp);
1973 vput(vp);
1974 goto out;
1975 }
1976 #endif /* NVERIEXEC > 0 */
1977
1978 #ifdef FILEASSOC
1979 (void)fileassoc_file_delete(vp);
1980 #endif /* FILEASSOC */
1981 error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
1982 out:
1983 pathbuf_stringcopy_put(pb, pathstring);
1984 pathbuf_destroy(pb);
1985 return (error);
1986 }
1987
1988 /*
1989 * Reposition read/write file offset.
1990 */
1991 int
1992 sys_lseek(struct lwp *l, const struct sys_lseek_args *uap, register_t *retval)
1993 {
1994 /* {
1995 syscallarg(int) fd;
1996 syscallarg(int) pad;
1997 syscallarg(off_t) offset;
1998 syscallarg(int) whence;
1999 } */
2000 kauth_cred_t cred = l->l_cred;
2001 file_t *fp;
2002 struct vnode *vp;
2003 struct vattr vattr;
2004 off_t newoff;
2005 int error, fd;
2006
2007 fd = SCARG(uap, fd);
2008
2009 if ((fp = fd_getfile(fd)) == NULL)
2010 return (EBADF);
2011
2012 vp = fp->f_data;
2013 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
2014 error = ESPIPE;
2015 goto out;
2016 }
2017
2018 switch (SCARG(uap, whence)) {
2019 case SEEK_CUR:
2020 newoff = fp->f_offset + SCARG(uap, offset);
2021 break;
2022 case SEEK_END:
2023 error = VOP_GETATTR(vp, &vattr, cred);
2024 if (error) {
2025 goto out;
2026 }
2027 newoff = SCARG(uap, offset) + vattr.va_size;
2028 break;
2029 case SEEK_SET:
2030 newoff = SCARG(uap, offset);
2031 break;
2032 default:
2033 error = EINVAL;
2034 goto out;
2035 }
2036 if ((error = VOP_SEEK(vp, fp->f_offset, newoff, cred)) == 0) {
2037 *(off_t *)retval = fp->f_offset = newoff;
2038 }
2039 out:
2040 fd_putfile(fd);
2041 return (error);
2042 }
2043
2044 /*
2045 * Positional read system call.
2046 */
2047 int
2048 sys_pread(struct lwp *l, const struct sys_pread_args *uap, register_t *retval)
2049 {
2050 /* {
2051 syscallarg(int) fd;
2052 syscallarg(void *) buf;
2053 syscallarg(size_t) nbyte;
2054 syscallarg(off_t) offset;
2055 } */
2056 file_t *fp;
2057 struct vnode *vp;
2058 off_t offset;
2059 int error, fd = SCARG(uap, fd);
2060
2061 if ((fp = fd_getfile(fd)) == NULL)
2062 return (EBADF);
2063
2064 if ((fp->f_flag & FREAD) == 0) {
2065 fd_putfile(fd);
2066 return (EBADF);
2067 }
2068
2069 vp = fp->f_data;
2070 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
2071 error = ESPIPE;
2072 goto out;
2073 }
2074
2075 offset = SCARG(uap, offset);
2076
2077 /*
2078 * XXX This works because no file systems actually
2079 * XXX take any action on the seek operation.
2080 */
2081 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
2082 goto out;
2083
2084 /* dofileread() will unuse the descriptor for us */
2085 return (dofileread(fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
2086 &offset, 0, retval));
2087
2088 out:
2089 fd_putfile(fd);
2090 return (error);
2091 }
2092
2093 /*
2094 * Positional scatter read system call.
2095 */
2096 int
2097 sys_preadv(struct lwp *l, const struct sys_preadv_args *uap, register_t *retval)
2098 {
2099 /* {
2100 syscallarg(int) fd;
2101 syscallarg(const struct iovec *) iovp;
2102 syscallarg(int) iovcnt;
2103 syscallarg(off_t) offset;
2104 } */
2105 off_t offset = SCARG(uap, offset);
2106
2107 return do_filereadv(SCARG(uap, fd), SCARG(uap, iovp),
2108 SCARG(uap, iovcnt), &offset, 0, retval);
2109 }
2110
2111 /*
2112 * Positional write system call.
2113 */
2114 int
2115 sys_pwrite(struct lwp *l, const struct sys_pwrite_args *uap, register_t *retval)
2116 {
2117 /* {
2118 syscallarg(int) fd;
2119 syscallarg(const void *) buf;
2120 syscallarg(size_t) nbyte;
2121 syscallarg(off_t) offset;
2122 } */
2123 file_t *fp;
2124 struct vnode *vp;
2125 off_t offset;
2126 int error, fd = SCARG(uap, fd);
2127
2128 if ((fp = fd_getfile(fd)) == NULL)
2129 return (EBADF);
2130
2131 if ((fp->f_flag & FWRITE) == 0) {
2132 fd_putfile(fd);
2133 return (EBADF);
2134 }
2135
2136 vp = fp->f_data;
2137 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
2138 error = ESPIPE;
2139 goto out;
2140 }
2141
2142 offset = SCARG(uap, offset);
2143
2144 /*
2145 * XXX This works because no file systems actually
2146 * XXX take any action on the seek operation.
2147 */
2148 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
2149 goto out;
2150
2151 /* dofilewrite() will unuse the descriptor for us */
2152 return (dofilewrite(fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
2153 &offset, 0, retval));
2154
2155 out:
2156 fd_putfile(fd);
2157 return (error);
2158 }
2159
2160 /*
2161 * Positional gather write system call.
2162 */
2163 int
2164 sys_pwritev(struct lwp *l, const struct sys_pwritev_args *uap, register_t *retval)
2165 {
2166 /* {
2167 syscallarg(int) fd;
2168 syscallarg(const struct iovec *) iovp;
2169 syscallarg(int) iovcnt;
2170 syscallarg(off_t) offset;
2171 } */
2172 off_t offset = SCARG(uap, offset);
2173
2174 return do_filewritev(SCARG(uap, fd), SCARG(uap, iovp),
2175 SCARG(uap, iovcnt), &offset, 0, retval);
2176 }
2177
2178 /*
2179 * Check access permissions.
2180 */
2181 int
2182 sys_access(struct lwp *l, const struct sys_access_args *uap, register_t *retval)
2183 {
2184 /* {
2185 syscallarg(const char *) path;
2186 syscallarg(int) flags;
2187 } */
2188 kauth_cred_t cred;
2189 struct vnode *vp;
2190 int error, flags;
2191 struct pathbuf *pb;
2192 struct nameidata nd;
2193
2194 CTASSERT(F_OK == 0);
2195 if ((SCARG(uap, flags) & ~(R_OK | W_OK | X_OK)) != 0) {
2196 /* nonsense flags */
2197 return EINVAL;
2198 }
2199
2200 error = pathbuf_copyin(SCARG(uap, path), &pb);
2201 if (error) {
2202 return error;
2203 }
2204 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb);
2205
2206 /* Override default credentials */
2207 cred = kauth_cred_dup(l->l_cred);
2208 kauth_cred_seteuid(cred, kauth_cred_getuid(l->l_cred));
2209 kauth_cred_setegid(cred, kauth_cred_getgid(l->l_cred));
2210 nd.ni_cnd.cn_cred = cred;
2211
2212 if ((error = namei(&nd)) != 0) {
2213 pathbuf_destroy(pb);
2214 goto out;
2215 }
2216 vp = nd.ni_vp;
2217 pathbuf_destroy(pb);
2218
2219 /* Flags == 0 means only check for existence. */
2220 if (SCARG(uap, flags)) {
2221 flags = 0;
2222 if (SCARG(uap, flags) & R_OK)
2223 flags |= VREAD;
2224 if (SCARG(uap, flags) & W_OK)
2225 flags |= VWRITE;
2226 if (SCARG(uap, flags) & X_OK)
2227 flags |= VEXEC;
2228
2229 error = VOP_ACCESS(vp, flags, cred);
2230 if (!error && (flags & VWRITE))
2231 error = vn_writechk(vp);
2232 }
2233 vput(vp);
2234 out:
2235 kauth_cred_free(cred);
2236 return (error);
2237 }
2238
2239 /*
2240 * Common code for all sys_stat functions, including compat versions.
2241 */
2242 int
2243 do_sys_stat(const char *userpath, unsigned int nd_flags, struct stat *sb)
2244 {
2245 int error;
2246 struct pathbuf *pb;
2247 struct nameidata nd;
2248
2249 error = pathbuf_copyin(userpath, &pb);
2250 if (error) {
2251 return error;
2252 }
2253 NDINIT(&nd, LOOKUP, nd_flags | LOCKLEAF | TRYEMULROOT, pb);
2254 error = namei(&nd);
2255 if (error != 0) {
2256 pathbuf_destroy(pb);
2257 return error;
2258 }
2259 error = vn_stat(nd.ni_vp, sb);
2260 vput(nd.ni_vp);
2261 pathbuf_destroy(pb);
2262 return error;
2263 }
2264
2265 /*
2266 * Get file status; this version follows links.
2267 */
2268 /* ARGSUSED */
2269 int
2270 sys___stat50(struct lwp *l, const struct sys___stat50_args *uap, register_t *retval)
2271 {
2272 /* {
2273 syscallarg(const char *) path;
2274 syscallarg(struct stat *) ub;
2275 } */
2276 struct stat sb;
2277 int error;
2278
2279 error = do_sys_stat(SCARG(uap, path), FOLLOW, &sb);
2280 if (error)
2281 return error;
2282 return copyout(&sb, SCARG(uap, ub), sizeof(sb));
2283 }
2284
2285 /*
2286 * Get file status; this version does not follow links.
2287 */
2288 /* ARGSUSED */
2289 int
2290 sys___lstat50(struct lwp *l, const struct sys___lstat50_args *uap, register_t *retval)
2291 {
2292 /* {
2293 syscallarg(const char *) path;
2294 syscallarg(struct stat *) ub;
2295 } */
2296 struct stat sb;
2297 int error;
2298
2299 error = do_sys_stat(SCARG(uap, path), NOFOLLOW, &sb);
2300 if (error)
2301 return error;
2302 return copyout(&sb, SCARG(uap, ub), sizeof(sb));
2303 }
2304
2305 /*
2306 * Get configurable pathname variables.
2307 */
2308 /* ARGSUSED */
2309 int
2310 sys_pathconf(struct lwp *l, const struct sys_pathconf_args *uap, register_t *retval)
2311 {
2312 /* {
2313 syscallarg(const char *) path;
2314 syscallarg(int) name;
2315 } */
2316 int error;
2317 struct pathbuf *pb;
2318 struct nameidata nd;
2319
2320 error = pathbuf_copyin(SCARG(uap, path), &pb);
2321 if (error) {
2322 return error;
2323 }
2324 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb);
2325 if ((error = namei(&nd)) != 0) {
2326 pathbuf_destroy(pb);
2327 return (error);
2328 }
2329 error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), retval);
2330 vput(nd.ni_vp);
2331 pathbuf_destroy(pb);
2332 return (error);
2333 }
2334
2335 /*
2336 * Return target name of a symbolic link.
2337 */
2338 /* ARGSUSED */
2339 int
2340 sys_readlink(struct lwp *l, const struct sys_readlink_args *uap, register_t *retval)
2341 {
2342 /* {
2343 syscallarg(const char *) path;
2344 syscallarg(char *) buf;
2345 syscallarg(size_t) count;
2346 } */
2347 struct vnode *vp;
2348 struct iovec aiov;
2349 struct uio auio;
2350 int error;
2351 struct pathbuf *pb;
2352 struct nameidata nd;
2353
2354 error = pathbuf_copyin(SCARG(uap, path), &pb);
2355 if (error) {
2356 return error;
2357 }
2358 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | TRYEMULROOT, pb);
2359 if ((error = namei(&nd)) != 0) {
2360 pathbuf_destroy(pb);
2361 return error;
2362 }
2363 vp = nd.ni_vp;
2364 pathbuf_destroy(pb);
2365 if (vp->v_type != VLNK)
2366 error = EINVAL;
2367 else if (!(vp->v_mount->mnt_flag & MNT_SYMPERM) ||
2368 (error = VOP_ACCESS(vp, VREAD, l->l_cred)) == 0) {
2369 aiov.iov_base = SCARG(uap, buf);
2370 aiov.iov_len = SCARG(uap, count);
2371 auio.uio_iov = &aiov;
2372 auio.uio_iovcnt = 1;
2373 auio.uio_offset = 0;
2374 auio.uio_rw = UIO_READ;
2375 KASSERT(l == curlwp);
2376 auio.uio_vmspace = l->l_proc->p_vmspace;
2377 auio.uio_resid = SCARG(uap, count);
2378 error = VOP_READLINK(vp, &auio, l->l_cred);
2379 }
2380 vput(vp);
2381 *retval = SCARG(uap, count) - auio.uio_resid;
2382 return (error);
2383 }
2384
2385 /*
2386 * Change flags of a file given a path name.
2387 */
2388 /* ARGSUSED */
2389 int
2390 sys_chflags(struct lwp *l, const struct sys_chflags_args *uap, register_t *retval)
2391 {
2392 /* {
2393 syscallarg(const char *) path;
2394 syscallarg(u_long) flags;
2395 } */
2396 struct vnode *vp;
2397 int error;
2398
2399 error = namei_simple_user(SCARG(uap, path),
2400 NSM_FOLLOW_TRYEMULROOT, &vp);
2401 if (error != 0)
2402 return (error);
2403 error = change_flags(vp, SCARG(uap, flags), l);
2404 vput(vp);
2405 return (error);
2406 }
2407
2408 /*
2409 * Change flags of a file given a file descriptor.
2410 */
2411 /* ARGSUSED */
2412 int
2413 sys_fchflags(struct lwp *l, const struct sys_fchflags_args *uap, register_t *retval)
2414 {
2415 /* {
2416 syscallarg(int) fd;
2417 syscallarg(u_long) flags;
2418 } */
2419 struct vnode *vp;
2420 file_t *fp;
2421 int error;
2422
2423 /* fd_getvnode() will use the descriptor for us */
2424 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
2425 return (error);
2426 vp = fp->f_data;
2427 error = change_flags(vp, SCARG(uap, flags), l);
2428 VOP_UNLOCK(vp);
2429 fd_putfile(SCARG(uap, fd));
2430 return (error);
2431 }
2432
2433 /*
2434 * Change flags of a file given a path name; this version does
2435 * not follow links.
2436 */
2437 int
2438 sys_lchflags(struct lwp *l, const struct sys_lchflags_args *uap, register_t *retval)
2439 {
2440 /* {
2441 syscallarg(const char *) path;
2442 syscallarg(u_long) flags;
2443 } */
2444 struct vnode *vp;
2445 int error;
2446
2447 error = namei_simple_user(SCARG(uap, path),
2448 NSM_NOFOLLOW_TRYEMULROOT, &vp);
2449 if (error != 0)
2450 return (error);
2451 error = change_flags(vp, SCARG(uap, flags), l);
2452 vput(vp);
2453 return (error);
2454 }
2455
2456 /*
2457 * Common routine to change flags of a file.
2458 */
2459 int
2460 change_flags(struct vnode *vp, u_long flags, struct lwp *l)
2461 {
2462 struct vattr vattr;
2463 int error;
2464
2465 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2466 /*
2467 * Non-superusers cannot change the flags on devices, even if they
2468 * own them.
2469 */
2470 if (kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER, NULL)) {
2471 if ((error = VOP_GETATTR(vp, &vattr, l->l_cred)) != 0)
2472 goto out;
2473 if (vattr.va_type == VCHR || vattr.va_type == VBLK) {
2474 error = EINVAL;
2475 goto out;
2476 }
2477 }
2478 vattr_null(&vattr);
2479 vattr.va_flags = flags;
2480 error = VOP_SETATTR(vp, &vattr, l->l_cred);
2481 out:
2482 return (error);
2483 }
2484
2485 /*
2486 * Change mode of a file given path name; this version follows links.
2487 */
2488 /* ARGSUSED */
2489 int
2490 sys_chmod(struct lwp *l, const struct sys_chmod_args *uap, register_t *retval)
2491 {
2492 /* {
2493 syscallarg(const char *) path;
2494 syscallarg(int) mode;
2495 } */
2496 int error;
2497 struct vnode *vp;
2498
2499 error = namei_simple_user(SCARG(uap, path),
2500 NSM_FOLLOW_TRYEMULROOT, &vp);
2501 if (error != 0)
2502 return (error);
2503
2504 error = change_mode(vp, SCARG(uap, mode), l);
2505
2506 vrele(vp);
2507 return (error);
2508 }
2509
2510 /*
2511 * Change mode of a file given a file descriptor.
2512 */
2513 /* ARGSUSED */
2514 int
2515 sys_fchmod(struct lwp *l, const struct sys_fchmod_args *uap, register_t *retval)
2516 {
2517 /* {
2518 syscallarg(int) fd;
2519 syscallarg(int) mode;
2520 } */
2521 file_t *fp;
2522 int error;
2523
2524 /* fd_getvnode() will use the descriptor for us */
2525 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
2526 return (error);
2527 error = change_mode(fp->f_data, SCARG(uap, mode), l);
2528 fd_putfile(SCARG(uap, fd));
2529 return (error);
2530 }
2531
2532 /*
2533 * Change mode of a file given path name; this version does not follow links.
2534 */
2535 /* ARGSUSED */
2536 int
2537 sys_lchmod(struct lwp *l, const struct sys_lchmod_args *uap, register_t *retval)
2538 {
2539 /* {
2540 syscallarg(const char *) path;
2541 syscallarg(int) mode;
2542 } */
2543 int error;
2544 struct vnode *vp;
2545
2546 error = namei_simple_user(SCARG(uap, path),
2547 NSM_NOFOLLOW_TRYEMULROOT, &vp);
2548 if (error != 0)
2549 return (error);
2550
2551 error = change_mode(vp, SCARG(uap, mode), l);
2552
2553 vrele(vp);
2554 return (error);
2555 }
2556
2557 /*
2558 * Common routine to set mode given a vnode.
2559 */
2560 static int
2561 change_mode(struct vnode *vp, int mode, struct lwp *l)
2562 {
2563 struct vattr vattr;
2564 int error;
2565
2566 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2567 vattr_null(&vattr);
2568 vattr.va_mode = mode & ALLPERMS;
2569 error = VOP_SETATTR(vp, &vattr, l->l_cred);
2570 VOP_UNLOCK(vp);
2571 return (error);
2572 }
2573
2574 /*
2575 * Set ownership given a path name; this version follows links.
2576 */
2577 /* ARGSUSED */
2578 int
2579 sys_chown(struct lwp *l, const struct sys_chown_args *uap, register_t *retval)
2580 {
2581 /* {
2582 syscallarg(const char *) path;
2583 syscallarg(uid_t) uid;
2584 syscallarg(gid_t) gid;
2585 } */
2586 int error;
2587 struct vnode *vp;
2588
2589 error = namei_simple_user(SCARG(uap, path),
2590 NSM_FOLLOW_TRYEMULROOT, &vp);
2591 if (error != 0)
2592 return (error);
2593
2594 error = change_owner(vp, SCARG(uap, uid), SCARG(uap, gid), l, 0);
2595
2596 vrele(vp);
2597 return (error);
2598 }
2599
2600 /*
2601 * Set ownership given a path name; this version follows links.
2602 * Provides POSIX semantics.
2603 */
2604 /* ARGSUSED */
2605 int
2606 sys___posix_chown(struct lwp *l, const struct sys___posix_chown_args *uap, register_t *retval)
2607 {
2608 /* {
2609 syscallarg(const char *) path;
2610 syscallarg(uid_t) uid;
2611 syscallarg(gid_t) gid;
2612 } */
2613 int error;
2614 struct vnode *vp;
2615
2616 error = namei_simple_user(SCARG(uap, path),
2617 NSM_FOLLOW_TRYEMULROOT, &vp);
2618 if (error != 0)
2619 return (error);
2620
2621 error = change_owner(vp, SCARG(uap, uid), SCARG(uap, gid), l, 1);
2622
2623 vrele(vp);
2624 return (error);
2625 }
2626
2627 /*
2628 * Set ownership given a file descriptor.
2629 */
2630 /* ARGSUSED */
2631 int
2632 sys_fchown(struct lwp *l, const struct sys_fchown_args *uap, register_t *retval)
2633 {
2634 /* {
2635 syscallarg(int) fd;
2636 syscallarg(uid_t) uid;
2637 syscallarg(gid_t) gid;
2638 } */
2639 int error;
2640 file_t *fp;
2641
2642 /* fd_getvnode() will use the descriptor for us */
2643 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
2644 return (error);
2645 error = change_owner(fp->f_data, SCARG(uap, uid), SCARG(uap, gid),
2646 l, 0);
2647 fd_putfile(SCARG(uap, fd));
2648 return (error);
2649 }
2650
2651 /*
2652 * Set ownership given a file descriptor, providing POSIX/XPG semantics.
2653 */
2654 /* ARGSUSED */
2655 int
2656 sys___posix_fchown(struct lwp *l, const struct sys___posix_fchown_args *uap, register_t *retval)
2657 {
2658 /* {
2659 syscallarg(int) fd;
2660 syscallarg(uid_t) uid;
2661 syscallarg(gid_t) gid;
2662 } */
2663 int error;
2664 file_t *fp;
2665
2666 /* fd_getvnode() will use the descriptor for us */
2667 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
2668 return (error);
2669 error = change_owner(fp->f_data, SCARG(uap, uid), SCARG(uap, gid),
2670 l, 1);
2671 fd_putfile(SCARG(uap, fd));
2672 return (error);
2673 }
2674
2675 /*
2676 * Set ownership given a path name; this version does not follow links.
2677 */
2678 /* ARGSUSED */
2679 int
2680 sys_lchown(struct lwp *l, const struct sys_lchown_args *uap, register_t *retval)
2681 {
2682 /* {
2683 syscallarg(const char *) path;
2684 syscallarg(uid_t) uid;
2685 syscallarg(gid_t) gid;
2686 } */
2687 int error;
2688 struct vnode *vp;
2689
2690 error = namei_simple_user(SCARG(uap, path),
2691 NSM_NOFOLLOW_TRYEMULROOT, &vp);
2692 if (error != 0)
2693 return (error);
2694
2695 error = change_owner(vp, SCARG(uap, uid), SCARG(uap, gid), l, 0);
2696
2697 vrele(vp);
2698 return (error);
2699 }
2700
2701 /*
2702 * Set ownership given a path name; this version does not follow links.
2703 * Provides POSIX/XPG semantics.
2704 */
2705 /* ARGSUSED */
2706 int
2707 sys___posix_lchown(struct lwp *l, const struct sys___posix_lchown_args *uap, register_t *retval)
2708 {
2709 /* {
2710 syscallarg(const char *) path;
2711 syscallarg(uid_t) uid;
2712 syscallarg(gid_t) gid;
2713 } */
2714 int error;
2715 struct vnode *vp;
2716
2717 error = namei_simple_user(SCARG(uap, path),
2718 NSM_NOFOLLOW_TRYEMULROOT, &vp);
2719 if (error != 0)
2720 return (error);
2721
2722 error = change_owner(vp, SCARG(uap, uid), SCARG(uap, gid), l, 1);
2723
2724 vrele(vp);
2725 return (error);
2726 }
2727
2728 /*
2729 * Common routine to set ownership given a vnode.
2730 */
2731 static int
2732 change_owner(struct vnode *vp, uid_t uid, gid_t gid, struct lwp *l,
2733 int posix_semantics)
2734 {
2735 struct vattr vattr;
2736 mode_t newmode;
2737 int error;
2738
2739 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2740 if ((error = VOP_GETATTR(vp, &vattr, l->l_cred)) != 0)
2741 goto out;
2742
2743 #define CHANGED(x) ((int)(x) != -1)
2744 newmode = vattr.va_mode;
2745 if (posix_semantics) {
2746 /*
2747 * POSIX/XPG semantics: if the caller is not the super-user,
2748 * clear set-user-id and set-group-id bits. Both POSIX and
2749 * the XPG consider the behaviour for calls by the super-user
2750 * implementation-defined; we leave the set-user-id and set-
2751 * group-id settings intact in that case.
2752 */
2753 if (kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
2754 NULL) != 0)
2755 newmode &= ~(S_ISUID | S_ISGID);
2756 } else {
2757 /*
2758 * NetBSD semantics: when changing owner and/or group,
2759 * clear the respective bit(s).
2760 */
2761 if (CHANGED(uid))
2762 newmode &= ~S_ISUID;
2763 if (CHANGED(gid))
2764 newmode &= ~S_ISGID;
2765 }
2766 /* Update va_mode iff altered. */
2767 if (vattr.va_mode == newmode)
2768 newmode = VNOVAL;
2769
2770 vattr_null(&vattr);
2771 vattr.va_uid = CHANGED(uid) ? uid : (uid_t)VNOVAL;
2772 vattr.va_gid = CHANGED(gid) ? gid : (gid_t)VNOVAL;
2773 vattr.va_mode = newmode;
2774 error = VOP_SETATTR(vp, &vattr, l->l_cred);
2775 #undef CHANGED
2776
2777 out:
2778 VOP_UNLOCK(vp);
2779 return (error);
2780 }
2781
2782 /*
2783 * Set the access and modification times given a path name; this
2784 * version follows links.
2785 */
2786 /* ARGSUSED */
2787 int
2788 sys___utimes50(struct lwp *l, const struct sys___utimes50_args *uap,
2789 register_t *retval)
2790 {
2791 /* {
2792 syscallarg(const char *) path;
2793 syscallarg(const struct timeval *) tptr;
2794 } */
2795
2796 return do_sys_utimes(l, NULL, SCARG(uap, path), FOLLOW,
2797 SCARG(uap, tptr), UIO_USERSPACE);
2798 }
2799
2800 /*
2801 * Set the access and modification times given a file descriptor.
2802 */
2803 /* ARGSUSED */
2804 int
2805 sys___futimes50(struct lwp *l, const struct sys___futimes50_args *uap,
2806 register_t *retval)
2807 {
2808 /* {
2809 syscallarg(int) fd;
2810 syscallarg(const struct timeval *) tptr;
2811 } */
2812 int error;
2813 file_t *fp;
2814
2815 /* fd_getvnode() will use the descriptor for us */
2816 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
2817 return (error);
2818 error = do_sys_utimes(l, fp->f_data, NULL, 0, SCARG(uap, tptr),
2819 UIO_USERSPACE);
2820 fd_putfile(SCARG(uap, fd));
2821 return (error);
2822 }
2823
2824 /*
2825 * Set the access and modification times given a path name; this
2826 * version does not follow links.
2827 */
2828 int
2829 sys___lutimes50(struct lwp *l, const struct sys___lutimes50_args *uap,
2830 register_t *retval)
2831 {
2832 /* {
2833 syscallarg(const char *) path;
2834 syscallarg(const struct timeval *) tptr;
2835 } */
2836
2837 return do_sys_utimes(l, NULL, SCARG(uap, path), NOFOLLOW,
2838 SCARG(uap, tptr), UIO_USERSPACE);
2839 }
2840
2841 /*
2842 * Common routine to set access and modification times given a vnode.
2843 */
2844 int
2845 do_sys_utimes(struct lwp *l, struct vnode *vp, const char *path, int flag,
2846 const struct timeval *tptr, enum uio_seg seg)
2847 {
2848 struct vattr vattr;
2849 int error, dorele = 0;
2850 namei_simple_flags_t sflags;
2851
2852 bool vanull, setbirthtime;
2853 struct timespec ts[2];
2854
2855 /*
2856 * I have checked all callers and they pass either FOLLOW,
2857 * NOFOLLOW, or 0 (when they don't pass a path), and NOFOLLOW
2858 * is 0. More to the point, they don't pass anything else.
2859 * Let's keep it that way at least until the namei interfaces
2860 * are fully sanitized.
2861 */
2862 KASSERT(flag == NOFOLLOW || flag == FOLLOW);
2863 sflags = (flag == FOLLOW) ?
2864 NSM_FOLLOW_TRYEMULROOT : NSM_NOFOLLOW_TRYEMULROOT;
2865
2866 if (tptr == NULL) {
2867 vanull = true;
2868 nanotime(&ts[0]);
2869 ts[1] = ts[0];
2870 } else {
2871 struct timeval tv[2];
2872
2873 vanull = false;
2874 if (seg != UIO_SYSSPACE) {
2875 error = copyin(tptr, tv, sizeof (tv));
2876 if (error != 0)
2877 return error;
2878 tptr = tv;
2879 }
2880 TIMEVAL_TO_TIMESPEC(&tptr[0], &ts[0]);
2881 TIMEVAL_TO_TIMESPEC(&tptr[1], &ts[1]);
2882 }
2883
2884 if (vp == NULL) {
2885 /* note: SEG describes TPTR, not PATH; PATH is always user */
2886 error = namei_simple_user(path, sflags, &vp);
2887 if (error != 0)
2888 return error;
2889 dorele = 1;
2890 }
2891
2892 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2893 setbirthtime = (VOP_GETATTR(vp, &vattr, l->l_cred) == 0 &&
2894 timespeccmp(&ts[1], &vattr.va_birthtime, <));
2895 vattr_null(&vattr);
2896 vattr.va_atime = ts[0];
2897 vattr.va_mtime = ts[1];
2898 if (setbirthtime)
2899 vattr.va_birthtime = ts[1];
2900 if (vanull)
2901 vattr.va_vaflags |= VA_UTIMES_NULL;
2902 error = VOP_SETATTR(vp, &vattr, l->l_cred);
2903 VOP_UNLOCK(vp);
2904
2905 if (dorele != 0)
2906 vrele(vp);
2907
2908 return error;
2909 }
2910
2911 /*
2912 * Truncate a file given its path name.
2913 */
2914 /* ARGSUSED */
2915 int
2916 sys_truncate(struct lwp *l, const struct sys_truncate_args *uap, register_t *retval)
2917 {
2918 /* {
2919 syscallarg(const char *) path;
2920 syscallarg(int) pad;
2921 syscallarg(off_t) length;
2922 } */
2923 struct vnode *vp;
2924 struct vattr vattr;
2925 int error;
2926
2927 error = namei_simple_user(SCARG(uap, path),
2928 NSM_FOLLOW_TRYEMULROOT, &vp);
2929 if (error != 0)
2930 return (error);
2931 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2932 if (vp->v_type == VDIR)
2933 error = EISDIR;
2934 else if ((error = vn_writechk(vp)) == 0 &&
2935 (error = VOP_ACCESS(vp, VWRITE, l->l_cred)) == 0) {
2936 vattr_null(&vattr);
2937 vattr.va_size = SCARG(uap, length);
2938 error = VOP_SETATTR(vp, &vattr, l->l_cred);
2939 }
2940 vput(vp);
2941 return (error);
2942 }
2943
2944 /*
2945 * Truncate a file given a file descriptor.
2946 */
2947 /* ARGSUSED */
2948 int
2949 sys_ftruncate(struct lwp *l, const struct sys_ftruncate_args *uap, register_t *retval)
2950 {
2951 /* {
2952 syscallarg(int) fd;
2953 syscallarg(int) pad;
2954 syscallarg(off_t) length;
2955 } */
2956 struct vattr vattr;
2957 struct vnode *vp;
2958 file_t *fp;
2959 int error;
2960
2961 /* fd_getvnode() will use the descriptor for us */
2962 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
2963 return (error);
2964 if ((fp->f_flag & FWRITE) == 0) {
2965 error = EINVAL;
2966 goto out;
2967 }
2968 vp = fp->f_data;
2969 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2970 if (vp->v_type == VDIR)
2971 error = EISDIR;
2972 else if ((error = vn_writechk(vp)) == 0) {
2973 vattr_null(&vattr);
2974 vattr.va_size = SCARG(uap, length);
2975 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
2976 }
2977 VOP_UNLOCK(vp);
2978 out:
2979 fd_putfile(SCARG(uap, fd));
2980 return (error);
2981 }
2982
2983 /*
2984 * Sync an open file.
2985 */
2986 /* ARGSUSED */
2987 int
2988 sys_fsync(struct lwp *l, const struct sys_fsync_args *uap, register_t *retval)
2989 {
2990 /* {
2991 syscallarg(int) fd;
2992 } */
2993 struct vnode *vp;
2994 file_t *fp;
2995 int error;
2996
2997 /* fd_getvnode() will use the descriptor for us */
2998 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
2999 return (error);
3000 vp = fp->f_data;
3001 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3002 error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT, 0, 0);
3003 VOP_UNLOCK(vp);
3004 fd_putfile(SCARG(uap, fd));
3005 return (error);
3006 }
3007
3008 /*
3009 * Sync a range of file data. API modeled after that found in AIX.
3010 *
3011 * FDATASYNC indicates that we need only save enough metadata to be able
3012 * to re-read the written data. Note we duplicate AIX's requirement that
3013 * the file be open for writing.
3014 */
3015 /* ARGSUSED */
3016 int
3017 sys_fsync_range(struct lwp *l, const struct sys_fsync_range_args *uap, register_t *retval)
3018 {
3019 /* {
3020 syscallarg(int) fd;
3021 syscallarg(int) flags;
3022 syscallarg(off_t) start;
3023 syscallarg(off_t) length;
3024 } */
3025 struct vnode *vp;
3026 file_t *fp;
3027 int flags, nflags;
3028 off_t s, e, len;
3029 int error;
3030
3031 /* fd_getvnode() will use the descriptor for us */
3032 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
3033 return (error);
3034
3035 if ((fp->f_flag & FWRITE) == 0) {
3036 error = EBADF;
3037 goto out;
3038 }
3039
3040 flags = SCARG(uap, flags);
3041 if (((flags & (FDATASYNC | FFILESYNC)) == 0) ||
3042 ((~flags & (FDATASYNC | FFILESYNC)) == 0)) {
3043 error = EINVAL;
3044 goto out;
3045 }
3046 /* Now set up the flags for value(s) to pass to VOP_FSYNC() */
3047 if (flags & FDATASYNC)
3048 nflags = FSYNC_DATAONLY | FSYNC_WAIT;
3049 else
3050 nflags = FSYNC_WAIT;
3051 if (flags & FDISKSYNC)
3052 nflags |= FSYNC_CACHE;
3053
3054 len = SCARG(uap, length);
3055 /* If length == 0, we do the whole file, and s = e = 0 will do that */
3056 if (len) {
3057 s = SCARG(uap, start);
3058 e = s + len;
3059 if (e < s) {
3060 error = EINVAL;
3061 goto out;
3062 }
3063 } else {
3064 e = 0;
3065 s = 0;
3066 }
3067
3068 vp = fp->f_data;
3069 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3070 error = VOP_FSYNC(vp, fp->f_cred, nflags, s, e);
3071 VOP_UNLOCK(vp);
3072 out:
3073 fd_putfile(SCARG(uap, fd));
3074 return (error);
3075 }
3076
3077 /*
3078 * Sync the data of an open file.
3079 */
3080 /* ARGSUSED */
3081 int
3082 sys_fdatasync(struct lwp *l, const struct sys_fdatasync_args *uap, register_t *retval)
3083 {
3084 /* {
3085 syscallarg(int) fd;
3086 } */
3087 struct vnode *vp;
3088 file_t *fp;
3089 int error;
3090
3091 /* fd_getvnode() will use the descriptor for us */
3092 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
3093 return (error);
3094 if ((fp->f_flag & FWRITE) == 0) {
3095 fd_putfile(SCARG(uap, fd));
3096 return (EBADF);
3097 }
3098 vp = fp->f_data;
3099 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3100 error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT|FSYNC_DATAONLY, 0, 0);
3101 VOP_UNLOCK(vp);
3102 fd_putfile(SCARG(uap, fd));
3103 return (error);
3104 }
3105
3106 /*
3107 * Rename files, (standard) BSD semantics frontend.
3108 */
3109 /* ARGSUSED */
3110 int
3111 sys_rename(struct lwp *l, const struct sys_rename_args *uap, register_t *retval)
3112 {
3113 /* {
3114 syscallarg(const char *) from;
3115 syscallarg(const char *) to;
3116 } */
3117
3118 return (do_sys_rename(SCARG(uap, from), SCARG(uap, to), UIO_USERSPACE, 0));
3119 }
3120
3121 /*
3122 * Rename files, POSIX semantics frontend.
3123 */
3124 /* ARGSUSED */
3125 int
3126 sys___posix_rename(struct lwp *l, const struct sys___posix_rename_args *uap, register_t *retval)
3127 {
3128 /* {
3129 syscallarg(const char *) from;
3130 syscallarg(const char *) to;
3131 } */
3132
3133 return (do_sys_rename(SCARG(uap, from), SCARG(uap, to), UIO_USERSPACE, 1));
3134 }
3135
3136 /*
3137 * Rename files. Source and destination must either both be directories,
3138 * or both not be directories. If target is a directory, it must be empty.
3139 * If `from' and `to' refer to the same object, the value of the `retain'
3140 * argument is used to determine whether `from' will be
3141 *
3142 * (retain == 0) deleted unless `from' and `to' refer to the same
3143 * object in the file system's name space (BSD).
3144 * (retain == 1) always retained (POSIX).
3145 */
3146 int
3147 do_sys_rename(const char *from, const char *to, enum uio_seg seg, int retain)
3148 {
3149 struct vnode *tvp, *fvp, *tdvp;
3150 struct pathbuf *frompb, *topb;
3151 struct nameidata fromnd, tond;
3152 struct mount *fs;
3153 int error;
3154
3155 error = pathbuf_maybe_copyin(from, seg, &frompb);
3156 if (error) {
3157 return error;
3158 }
3159 error = pathbuf_maybe_copyin(to, seg, &topb);
3160 if (error) {
3161 pathbuf_destroy(frompb);
3162 return error;
3163 }
3164
3165 NDINIT(&fromnd, DELETE, LOCKPARENT | TRYEMULROOT | INRENAME,
3166 frompb);
3167 if ((error = namei(&fromnd)) != 0) {
3168 pathbuf_destroy(frompb);
3169 pathbuf_destroy(topb);
3170 return (error);
3171 }
3172 if (fromnd.ni_dvp != fromnd.ni_vp)
3173 VOP_UNLOCK(fromnd.ni_dvp);
3174 fvp = fromnd.ni_vp;
3175
3176 fs = fvp->v_mount;
3177 error = VFS_RENAMELOCK_ENTER(fs);
3178 if (error) {
3179 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
3180 vrele(fromnd.ni_dvp);
3181 vrele(fvp);
3182 goto out1;
3183 }
3184
3185 /*
3186 * close, partially, yet another race - ideally we should only
3187 * go as far as getting fromnd.ni_dvp before getting the per-fs
3188 * lock, and then continue to get fromnd.ni_vp, but we can't do
3189 * that with namei as it stands.
3190 *
3191 * This still won't prevent rmdir from nuking fromnd.ni_vp
3192 * under us. The real fix is to get the locks in the right
3193 * order and do the lookups in the right places, but that's a
3194 * major rototill.
3195 *
3196 * Note: this logic (as well as this whole function) is cloned
3197 * in nfs_serv.c. Proceed accordingly.
3198 */
3199 vrele(fvp);
3200 if ((fromnd.ni_cnd.cn_namelen == 1 &&
3201 fromnd.ni_cnd.cn_nameptr[0] == '.') ||
3202 (fromnd.ni_cnd.cn_namelen == 2 &&
3203 fromnd.ni_cnd.cn_nameptr[0] == '.' &&
3204 fromnd.ni_cnd.cn_nameptr[1] == '.')) {
3205 error = EINVAL;
3206 VFS_RENAMELOCK_EXIT(fs);
3207 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
3208 vrele(fromnd.ni_dvp);
3209 goto out1;
3210 }
3211 vn_lock(fromnd.ni_dvp, LK_EXCLUSIVE | LK_RETRY);
3212 error = relookup(fromnd.ni_dvp, &fromnd.ni_vp, &fromnd.ni_cnd, 0);
3213 if (error) {
3214 VOP_UNLOCK(fromnd.ni_dvp);
3215 VFS_RENAMELOCK_EXIT(fs);
3216 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
3217 vrele(fromnd.ni_dvp);
3218 goto out1;
3219 }
3220 VOP_UNLOCK(fromnd.ni_vp);
3221 if (fromnd.ni_dvp != fromnd.ni_vp)
3222 VOP_UNLOCK(fromnd.ni_dvp);
3223 fvp = fromnd.ni_vp;
3224
3225 NDINIT(&tond, RENAME,
3226 LOCKPARENT | LOCKLEAF | NOCACHE | TRYEMULROOT
3227 | INRENAME | (fvp->v_type == VDIR ? CREATEDIR : 0),
3228 topb);
3229 if ((error = namei(&tond)) != 0) {
3230 VFS_RENAMELOCK_EXIT(fs);
3231 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
3232 vrele(fromnd.ni_dvp);
3233 vrele(fvp);
3234 goto out1;
3235 }
3236 tdvp = tond.ni_dvp;
3237 tvp = tond.ni_vp;
3238
3239 if (tvp != NULL) {
3240 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3241 error = ENOTDIR;
3242 goto out;
3243 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3244 error = EISDIR;
3245 goto out;
3246 }
3247 }
3248
3249 if (fvp == tdvp)
3250 error = EINVAL;
3251
3252 /*
3253 * Source and destination refer to the same object.
3254 */
3255 if (fvp == tvp) {
3256 if (retain)
3257 error = -1;
3258 else if (fromnd.ni_dvp == tdvp &&
3259 fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
3260 !memcmp(fromnd.ni_cnd.cn_nameptr,
3261 tond.ni_cnd.cn_nameptr,
3262 fromnd.ni_cnd.cn_namelen))
3263 error = -1;
3264 }
3265 /*
3266 * Prevent cross-mount operation.
3267 */
3268 if (error == 0) {
3269 if (tond.ni_dvp->v_mount != fromnd.ni_dvp->v_mount) {
3270 error = EXDEV;
3271 }
3272 }
3273 #if NVERIEXEC > 0
3274 if (!error) {
3275 char *f1, *f2;
3276 size_t f1_len;
3277 size_t f2_len;
3278
3279 f1_len = fromnd.ni_cnd.cn_namelen + 1;
3280 f1 = kmem_alloc(f1_len, KM_SLEEP);
3281 strlcpy(f1, fromnd.ni_cnd.cn_nameptr, f1_len);
3282
3283 f2_len = tond.ni_cnd.cn_namelen + 1;
3284 f2 = kmem_alloc(f2_len, KM_SLEEP);
3285 strlcpy(f2, tond.ni_cnd.cn_nameptr, f2_len);
3286
3287 error = veriexec_renamechk(curlwp, fvp, f1, tvp, f2);
3288
3289 kmem_free(f1, f1_len);
3290 kmem_free(f2, f2_len);
3291 }
3292 #endif /* NVERIEXEC > 0 */
3293
3294 out:
3295 if (!error) {
3296 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3297 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3298 VFS_RENAMELOCK_EXIT(fs);
3299 } else {
3300 VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
3301 if (tdvp == tvp)
3302 vrele(tdvp);
3303 else
3304 vput(tdvp);
3305 if (tvp)
3306 vput(tvp);
3307 VFS_RENAMELOCK_EXIT(fs);
3308 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
3309 vrele(fromnd.ni_dvp);
3310 vrele(fvp);
3311 }
3312 out1:
3313 pathbuf_destroy(frompb);
3314 pathbuf_destroy(topb);
3315 return (error == -1 ? 0 : error);
3316 }
3317
3318 /*
3319 * Make a directory file.
3320 */
3321 /* ARGSUSED */
3322 int
3323 sys_mkdir(struct lwp *l, const struct sys_mkdir_args *uap, register_t *retval)
3324 {
3325 /* {
3326 syscallarg(const char *) path;
3327 syscallarg(int) mode;
3328 } */
3329
3330 return do_sys_mkdir(SCARG(uap, path), SCARG(uap, mode), UIO_USERSPACE);
3331 }
3332
3333 int
3334 do_sys_mkdir(const char *path, mode_t mode, enum uio_seg seg)
3335 {
3336 struct proc *p = curlwp->l_proc;
3337 struct vnode *vp;
3338 struct vattr vattr;
3339 int error;
3340 struct pathbuf *pb;
3341 struct nameidata nd;
3342
3343 /* XXX bollocks, should pass in a pathbuf */
3344 error = pathbuf_maybe_copyin(path, seg, &pb);
3345 if (error) {
3346 return error;
3347 }
3348
3349 NDINIT(&nd, CREATE, LOCKPARENT | CREATEDIR | TRYEMULROOT, pb);
3350 if ((error = namei(&nd)) != 0) {
3351 pathbuf_destroy(pb);
3352 return (error);
3353 }
3354 vp = nd.ni_vp;
3355 if (vp != NULL) {
3356 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
3357 if (nd.ni_dvp == vp)
3358 vrele(nd.ni_dvp);
3359 else
3360 vput(nd.ni_dvp);
3361 vrele(vp);
3362 pathbuf_destroy(pb);
3363 return (EEXIST);
3364 }
3365 vattr_null(&vattr);
3366 vattr.va_type = VDIR;
3367 /* We will read cwdi->cwdi_cmask unlocked. */
3368 vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_cwdi->cwdi_cmask;
3369 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3370 if (!error)
3371 vput(nd.ni_vp);
3372 pathbuf_destroy(pb);
3373 return (error);
3374 }
3375
3376 /*
3377 * Remove a directory file.
3378 */
3379 /* ARGSUSED */
3380 int
3381 sys_rmdir(struct lwp *l, const struct sys_rmdir_args *uap, register_t *retval)
3382 {
3383 /* {
3384 syscallarg(const char *) path;
3385 } */
3386 struct vnode *vp;
3387 int error;
3388 struct pathbuf *pb;
3389 struct nameidata nd;
3390
3391 error = pathbuf_copyin(SCARG(uap, path), &pb);
3392 if (error) {
3393 return error;
3394 }
3395 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF | TRYEMULROOT, pb);
3396 if ((error = namei(&nd)) != 0) {
3397 pathbuf_destroy(pb);
3398 return error;
3399 }
3400 vp = nd.ni_vp;
3401 if (vp->v_type != VDIR) {
3402 error = ENOTDIR;
3403 goto out;
3404 }
3405 /*
3406 * No rmdir "." please.
3407 */
3408 if (nd.ni_dvp == vp) {
3409 error = EINVAL;
3410 goto out;
3411 }
3412 /*
3413 * The root of a mounted filesystem cannot be deleted.
3414 */
3415 if ((vp->v_vflag & VV_ROOT) != 0 || vp->v_mountedhere != NULL) {
3416 error = EBUSY;
3417 goto out;
3418 }
3419 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3420 pathbuf_destroy(pb);
3421 return (error);
3422
3423 out:
3424 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
3425 if (nd.ni_dvp == vp)
3426 vrele(nd.ni_dvp);
3427 else
3428 vput(nd.ni_dvp);
3429 vput(vp);
3430 pathbuf_destroy(pb);
3431 return (error);
3432 }
3433
3434 /*
3435 * Read a block of directory entries in a file system independent format.
3436 */
3437 int
3438 sys___getdents30(struct lwp *l, const struct sys___getdents30_args *uap, register_t *retval)
3439 {
3440 /* {
3441 syscallarg(int) fd;
3442 syscallarg(char *) buf;
3443 syscallarg(size_t) count;
3444 } */
3445 file_t *fp;
3446 int error, done;
3447
3448 /* fd_getvnode() will use the descriptor for us */
3449 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
3450 return (error);
3451 if ((fp->f_flag & FREAD) == 0) {
3452 error = EBADF;
3453 goto out;
3454 }
3455 error = vn_readdir(fp, SCARG(uap, buf), UIO_USERSPACE,
3456 SCARG(uap, count), &done, l, 0, 0);
3457 ktrgenio(SCARG(uap, fd), UIO_READ, SCARG(uap, buf), done, error);
3458 *retval = done;
3459 out:
3460 fd_putfile(SCARG(uap, fd));
3461 return (error);
3462 }
3463
3464 /*
3465 * Set the mode mask for creation of filesystem nodes.
3466 */
3467 int
3468 sys_umask(struct lwp *l, const struct sys_umask_args *uap, register_t *retval)
3469 {
3470 /* {
3471 syscallarg(mode_t) newmask;
3472 } */
3473 struct proc *p = l->l_proc;
3474 struct cwdinfo *cwdi;
3475
3476 /*
3477 * cwdi->cwdi_cmask will be read unlocked elsewhere. What's
3478 * important is that we serialize changes to the mask. The
3479 * rw_exit() will issue a write memory barrier on our behalf,
3480 * and force the changes out to other CPUs (as it must use an
3481 * atomic operation, draining the local CPU's store buffers).
3482 */
3483 cwdi = p->p_cwdi;
3484 rw_enter(&cwdi->cwdi_lock, RW_WRITER);
3485 *retval = cwdi->cwdi_cmask;
3486 cwdi->cwdi_cmask = SCARG(uap, newmask) & ALLPERMS;
3487 rw_exit(&cwdi->cwdi_lock);
3488
3489 return (0);
3490 }
3491
3492 int
3493 dorevoke(struct vnode *vp, kauth_cred_t cred)
3494 {
3495 struct vattr vattr;
3496 int error;
3497
3498 if ((error = VOP_GETATTR(vp, &vattr, cred)) != 0)
3499 return error;
3500 if (kauth_cred_geteuid(cred) == vattr.va_uid ||
3501 (error = kauth_authorize_generic(cred,
3502 KAUTH_GENERIC_ISSUSER, NULL)) == 0)
3503 VOP_REVOKE(vp, REVOKEALL);
3504 return (error);
3505 }
3506
3507 /*
3508 * Void all references to file by ripping underlying filesystem
3509 * away from vnode.
3510 */
3511 /* ARGSUSED */
3512 int
3513 sys_revoke(struct lwp *l, const struct sys_revoke_args *uap, register_t *retval)
3514 {
3515 /* {
3516 syscallarg(const char *) path;
3517 } */
3518 struct vnode *vp;
3519 int error;
3520
3521 error = namei_simple_user(SCARG(uap, path),
3522 NSM_FOLLOW_TRYEMULROOT, &vp);
3523 if (error != 0)
3524 return (error);
3525 error = dorevoke(vp, l->l_cred);
3526 vrele(vp);
3527 return (error);
3528 }
3529