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