2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)vfs_syscalls.c 8.13 (Berkeley) 4/15/94
39 * $FreeBSD: src/sys/kern/vfs_syscalls.c,v 1.151.2.18 2003/04/04 20:35:58 tegge Exp $
40 * $DragonFly: src/sys/kern/vfs_syscalls.c,v 1.135 2008/11/11 00:55:49 pavalos Exp $
43 #include <sys/param.h>
44 #include <sys/systm.h>
47 #include <sys/sysent.h>
48 #include <sys/malloc.h>
49 #include <sys/mount.h>
50 #include <sys/mountctl.h>
51 #include <sys/sysproto.h>
52 #include <sys/filedesc.h>
53 #include <sys/kernel.h>
54 #include <sys/fcntl.h>
56 #include <sys/linker.h>
58 #include <sys/unistd.h>
59 #include <sys/vnode.h>
63 #include <sys/namei.h>
64 #include <sys/nlookup.h>
65 #include <sys/dirent.h>
66 #include <sys/extattr.h>
67 #include <sys/spinlock.h>
68 #include <sys/kern_syscall.h>
69 #include <sys/objcache.h>
70 #include <sys/sysctl.h>
73 #include <sys/file2.h>
74 #include <sys/spinlock2.h>
75 #include <sys/mplock2.h>
78 #include <vm/vm_object.h>
79 #include <vm/vm_page.h>
81 #include <machine/limits.h>
82 #include <machine/stdarg.h>
84 #include <vfs/union/union.h>
86 static void mount_warning(struct mount
*mp
, const char *ctl
, ...);
87 static int mount_path(struct proc
*p
, struct mount
*mp
, char **rb
, char **fb
);
88 static int checkvp_chdir (struct vnode
*vn
, struct thread
*td
);
89 static void checkdirs (struct nchandle
*old_nch
, struct nchandle
*new_nch
);
90 static int chroot_refuse_vdir_fds (struct filedesc
*fdp
);
91 static int chroot_visible_mnt(struct mount
*mp
, struct proc
*p
);
92 static int getutimes (const struct timeval
*, struct timespec
*);
93 static int setfown (struct vnode
*, uid_t
, gid_t
);
94 static int setfmode (struct vnode
*, int);
95 static int setfflags (struct vnode
*, int);
96 static int setutimes (struct vnode
*, struct vattr
*,
97 const struct timespec
*, int);
98 static int usermount
= 0; /* if 1, non-root can mount fs. */
100 int (*union_dircheckp
) (struct thread
*, struct vnode
**, struct file
*);
102 SYSCTL_INT(_vfs
, OID_AUTO
, usermount
, CTLFLAG_RW
, &usermount
, 0, "");
105 * Virtual File System System Calls
109 * Mount a file system.
111 * mount_args(char *type, char *path, int flags, caddr_t data)
116 sys_mount(struct mount_args
*uap
)
118 struct thread
*td
= curthread
;
121 struct mount
*mp
, *nullmp
;
122 struct vfsconf
*vfsp
;
123 int error
, flag
= 0, flag2
= 0;
126 struct nlookupdata nd
;
127 char fstypename
[MFSNAMELEN
];
136 if (usermount
== 0 && (error
= priv_check(td
, PRIV_ROOT
)))
140 * Do not allow NFS export by non-root users.
142 if (uap
->flags
& MNT_EXPORTED
) {
143 error
= priv_check(td
, PRIV_ROOT
);
148 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users
150 if (priv_check(td
, PRIV_ROOT
))
151 uap
->flags
|= MNT_NOSUID
| MNT_NODEV
;
154 * Lookup the requested path and extract the nch and vnode.
156 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
158 if ((error
= nlookup(&nd
)) == 0) {
159 if (nd
.nl_nch
.ncp
->nc_vp
== NULL
)
169 * If the target filesystem is resolved via a nullfs mount, then
170 * nd.nl_nch.mount will be pointing to the nullfs mount structure
171 * instead of the target file system. We need it in case we are
174 nullmp
= nd
.nl_nch
.mount
;
177 * Extract the locked+refd ncp and cleanup the nd structure
180 cache_zero(&nd
.nl_nch
);
183 if ((nch
.ncp
->nc_flag
& NCF_ISMOUNTPT
) && cache_findmount(&nch
))
190 * now we have the locked ref'd nch and unreferenced vnode.
193 if ((error
= vget(vp
, LK_EXCLUSIVE
)) != 0) {
200 * Extract the file system type. We need to know this early, to take
201 * appropriate actions if we are dealing with a nullfs.
203 if ((error
= copyinstr(uap
->type
, fstypename
, MFSNAMELEN
, NULL
)) != 0) {
210 * Now we have an unlocked ref'd nch and a locked ref'd vp
212 if (uap
->flags
& MNT_UPDATE
) {
213 if ((vp
->v_flag
& (VROOT
|VPFSROOT
)) == 0) {
220 if (strncmp(fstypename
, "null", 5) == 0) {
228 flag2
= mp
->mnt_kern_flag
;
230 * We only allow the filesystem to be reloaded if it
231 * is currently mounted read-only.
233 if ((uap
->flags
& MNT_RELOAD
) &&
234 ((mp
->mnt_flag
& MNT_RDONLY
) == 0)) {
237 error
= EOPNOTSUPP
; /* Needs translation */
241 * Only root, or the user that did the original mount is
242 * permitted to update it.
244 if (mp
->mnt_stat
.f_owner
!= cred
->cr_uid
&&
245 (error
= priv_check(td
, PRIV_ROOT
))) {
250 if (vfs_busy(mp
, LK_NOWAIT
)) {
256 if ((vp
->v_flag
& VMOUNT
) != 0 || hasmount
) {
263 vsetflags(vp
, VMOUNT
);
265 uap
->flags
& (MNT_RELOAD
| MNT_FORCE
| MNT_UPDATE
);
270 * If the user is not root, ensure that they own the directory
271 * onto which we are attempting to mount.
273 if ((error
= VOP_GETATTR(vp
, &va
)) ||
274 (va
.va_uid
!= cred
->cr_uid
&& (error
= priv_check(td
, PRIV_ROOT
)))) {
279 if ((error
= vinvalbuf(vp
, V_SAVE
, 0, 0)) != 0) {
284 if (vp
->v_type
!= VDIR
) {
290 if (vp
->v_mount
->mnt_kern_flag
& MNTK_NOSTKMNT
) {
296 vfsp
= vfsconf_find_by_name(fstypename
);
300 /* Only load modules for root (very important!) */
301 if ((error
= priv_check(td
, PRIV_ROOT
)) != 0) {
306 error
= linker_load_file(fstypename
, &lf
);
307 if (error
|| lf
== NULL
) {
315 /* lookup again, see if the VFS was loaded */
316 vfsp
= vfsconf_find_by_name(fstypename
);
319 linker_file_unload(lf
);
326 if ((vp
->v_flag
& VMOUNT
) != 0 || hasmount
) {
332 vsetflags(vp
, VMOUNT
);
335 * Allocate and initialize the filesystem.
337 mp
= kmalloc(sizeof(struct mount
), M_MOUNT
, M_ZERO
|M_WAITOK
);
339 vfs_busy(mp
, LK_NOWAIT
);
340 mp
->mnt_op
= vfsp
->vfc_vfsops
;
342 vfsp
->vfc_refcount
++;
343 mp
->mnt_stat
.f_type
= vfsp
->vfc_typenum
;
344 mp
->mnt_flag
|= vfsp
->vfc_flags
& MNT_VISFLAGMASK
;
345 strncpy(mp
->mnt_stat
.f_fstypename
, vfsp
->vfc_name
, MFSNAMELEN
);
346 mp
->mnt_stat
.f_owner
= cred
->cr_uid
;
350 * Set the mount level flags.
352 if (uap
->flags
& MNT_RDONLY
)
353 mp
->mnt_flag
|= MNT_RDONLY
;
354 else if (mp
->mnt_flag
& MNT_RDONLY
)
355 mp
->mnt_kern_flag
|= MNTK_WANTRDWR
;
356 mp
->mnt_flag
&=~ (MNT_NOSUID
| MNT_NOEXEC
| MNT_NODEV
|
357 MNT_SYNCHRONOUS
| MNT_UNION
| MNT_ASYNC
| MNT_NOATIME
|
358 MNT_NOSYMFOLLOW
| MNT_IGNORE
|
359 MNT_NOCLUSTERR
| MNT_NOCLUSTERW
| MNT_SUIDDIR
);
360 mp
->mnt_flag
|= uap
->flags
& (MNT_NOSUID
| MNT_NOEXEC
|
361 MNT_NODEV
| MNT_SYNCHRONOUS
| MNT_UNION
| MNT_ASYNC
| MNT_FORCE
|
362 MNT_NOSYMFOLLOW
| MNT_IGNORE
|
363 MNT_NOATIME
| MNT_NOCLUSTERR
| MNT_NOCLUSTERW
| MNT_SUIDDIR
);
365 * Mount the filesystem.
366 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they
369 error
= VFS_MOUNT(mp
, uap
->path
, uap
->data
, cred
);
370 if (mp
->mnt_flag
& MNT_UPDATE
) {
371 if (mp
->mnt_kern_flag
& MNTK_WANTRDWR
)
372 mp
->mnt_flag
&= ~MNT_RDONLY
;
373 mp
->mnt_flag
&=~ (MNT_UPDATE
| MNT_RELOAD
| MNT_FORCE
);
374 mp
->mnt_kern_flag
&=~ MNTK_WANTRDWR
;
377 mp
->mnt_kern_flag
= flag2
;
380 vclrflags(vp
, VMOUNT
);
385 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
387 * Put the new filesystem on the mount list after root. The mount
388 * point gets its own mnt_ncmountpt (unless the VFS already set one
389 * up) which represents the root of the mount. The lookup code
390 * detects the mount point going forward and checks the root of
391 * the mount going backwards.
393 * It is not necessary to invalidate or purge the vnode underneath
394 * because elements under the mount will be given their own glue
398 if (mp
->mnt_ncmountpt
.ncp
== NULL
) {
400 * allocate, then unlock, but leave the ref intact
402 cache_allocroot(&mp
->mnt_ncmountpt
, mp
, NULL
);
403 cache_unlock(&mp
->mnt_ncmountpt
);
405 mp
->mnt_ncmounton
= nch
; /* inherits ref */
406 nch
.ncp
->nc_flag
|= NCF_ISMOUNTPT
;
408 /* XXX get the root of the fs and cache_setvp(mnt_ncmountpt...) */
409 vclrflags(vp
, VMOUNT
);
410 mountlist_insert(mp
, MNTINS_LAST
);
412 checkdirs(&mp
->mnt_ncmounton
, &mp
->mnt_ncmountpt
);
413 error
= vfs_allocate_syncvnode(mp
);
415 error
= VFS_START(mp
, 0);
418 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_coherency_ops
);
419 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_journal_ops
);
420 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_norm_ops
);
421 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_spec_ops
);
422 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_fifo_ops
);
423 vclrflags(vp
, VMOUNT
);
424 mp
->mnt_vfc
->vfc_refcount
--;
436 * Scan all active processes to see if any of them have a current
437 * or root directory onto which the new filesystem has just been
438 * mounted. If so, replace them with the new mount point.
440 * The passed ncp is ref'd and locked (from the mount code) and
441 * must be associated with the vnode representing the root of the
444 struct checkdirs_info
{
445 struct nchandle old_nch
;
446 struct nchandle new_nch
;
447 struct vnode
*old_vp
;
448 struct vnode
*new_vp
;
451 static int checkdirs_callback(struct proc
*p
, void *data
);
454 checkdirs(struct nchandle
*old_nch
, struct nchandle
*new_nch
)
456 struct checkdirs_info info
;
462 * If the old mount point's vnode has a usecount of 1, it is not
463 * being held as a descriptor anywhere.
465 olddp
= old_nch
->ncp
->nc_vp
;
466 if (olddp
== NULL
|| olddp
->v_sysref
.refcnt
== 1)
470 * Force the root vnode of the new mount point to be resolved
471 * so we can update any matching processes.
474 if (VFS_ROOT(mp
, &newdp
))
475 panic("mount: lost mount");
476 cache_setunresolved(new_nch
);
477 cache_setvp(new_nch
, newdp
);
480 * Special handling of the root node
482 if (rootvnode
== olddp
) {
484 vfs_cache_setroot(newdp
, cache_hold(new_nch
));
488 * Pass newdp separately so the callback does not have to access
489 * it via new_nch->ncp->nc_vp.
491 info
.old_nch
= *old_nch
;
492 info
.new_nch
= *new_nch
;
494 allproc_scan(checkdirs_callback
, &info
);
499 * NOTE: callback is not MP safe because the scanned process's filedesc
500 * structure can be ripped out from under us, amoung other things.
503 checkdirs_callback(struct proc
*p
, void *data
)
505 struct checkdirs_info
*info
= data
;
506 struct filedesc
*fdp
;
507 struct nchandle ncdrop1
;
508 struct nchandle ncdrop2
;
509 struct vnode
*vprele1
;
510 struct vnode
*vprele2
;
512 if ((fdp
= p
->p_fd
) != NULL
) {
513 cache_zero(&ncdrop1
);
514 cache_zero(&ncdrop2
);
519 * MPUNSAFE - XXX fdp can be pulled out from under a
522 * A shared filedesc is ok, we don't have to copy it
523 * because we are making this change globally.
525 spin_lock_wr(&fdp
->fd_spin
);
526 if (fdp
->fd_ncdir
.mount
== info
->old_nch
.mount
&&
527 fdp
->fd_ncdir
.ncp
== info
->old_nch
.ncp
) {
528 vprele1
= fdp
->fd_cdir
;
530 fdp
->fd_cdir
= info
->new_vp
;
531 ncdrop1
= fdp
->fd_ncdir
;
532 cache_copy(&info
->new_nch
, &fdp
->fd_ncdir
);
534 if (fdp
->fd_nrdir
.mount
== info
->old_nch
.mount
&&
535 fdp
->fd_nrdir
.ncp
== info
->old_nch
.ncp
) {
536 vprele2
= fdp
->fd_rdir
;
538 fdp
->fd_rdir
= info
->new_vp
;
539 ncdrop2
= fdp
->fd_nrdir
;
540 cache_copy(&info
->new_nch
, &fdp
->fd_nrdir
);
542 spin_unlock_wr(&fdp
->fd_spin
);
544 cache_drop(&ncdrop1
);
546 cache_drop(&ncdrop2
);
556 * Unmount a file system.
558 * Note: unmount takes a path to the vnode mounted on as argument,
559 * not special file (as before).
561 * umount_args(char *path, int flags)
566 sys_unmount(struct unmount_args
*uap
)
568 struct thread
*td
= curthread
;
569 struct proc
*p __debugvar
= td
->td_proc
;
570 struct mount
*mp
= NULL
;
571 struct nlookupdata nd
;
576 if (td
->td_ucred
->cr_prison
!= NULL
) {
580 if (usermount
== 0 && (error
= priv_check(td
, PRIV_ROOT
)))
583 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
585 error
= nlookup(&nd
);
589 mp
= nd
.nl_nch
.mount
;
592 * Only root, or the user that did the original mount is
593 * permitted to unmount this filesystem.
595 if ((mp
->mnt_stat
.f_owner
!= td
->td_ucred
->cr_uid
) &&
596 (error
= priv_check(td
, PRIV_ROOT
)))
600 * Don't allow unmounting the root file system.
602 if (mp
->mnt_flag
& MNT_ROOTFS
) {
608 * Must be the root of the filesystem
610 if (nd
.nl_nch
.ncp
!= mp
->mnt_ncmountpt
.ncp
) {
618 error
= dounmount(mp
, uap
->flags
);
625 * Do the actual file system unmount.
628 dounmount_interlock(struct mount
*mp
)
630 if (mp
->mnt_kern_flag
& MNTK_UNMOUNT
)
632 mp
->mnt_kern_flag
|= MNTK_UNMOUNT
;
637 dounmount(struct mount
*mp
, int flags
)
639 struct namecache
*ncp
;
648 * Exclusive access for unmounting purposes
650 if ((error
= mountlist_interlock(dounmount_interlock
, mp
)) != 0)
654 * Allow filesystems to detect that a forced unmount is in progress.
656 if (flags
& MNT_FORCE
)
657 mp
->mnt_kern_flag
|= MNTK_UNMOUNTF
;
658 lflags
= LK_EXCLUSIVE
| ((flags
& MNT_FORCE
) ? 0 : LK_NOWAIT
);
659 error
= lockmgr(&mp
->mnt_lock
, lflags
);
661 mp
->mnt_kern_flag
&= ~(MNTK_UNMOUNT
| MNTK_UNMOUNTF
);
662 if (mp
->mnt_kern_flag
& MNTK_MWAIT
)
667 if (mp
->mnt_flag
& MNT_EXPUBLIC
)
668 vfs_setpublicfs(NULL
, NULL
, NULL
);
670 vfs_msync(mp
, MNT_WAIT
);
671 async_flag
= mp
->mnt_flag
& MNT_ASYNC
;
672 mp
->mnt_flag
&=~ MNT_ASYNC
;
675 * If this filesystem isn't aliasing other filesystems,
676 * try to invalidate any remaining namecache entries and
677 * check the count afterwords.
679 if ((mp
->mnt_kern_flag
& MNTK_NCALIASED
) == 0) {
680 cache_lock(&mp
->mnt_ncmountpt
);
681 cache_inval(&mp
->mnt_ncmountpt
, CINV_DESTROY
|CINV_CHILDREN
);
682 cache_unlock(&mp
->mnt_ncmountpt
);
684 if ((ncp
= mp
->mnt_ncmountpt
.ncp
) != NULL
&&
685 (ncp
->nc_refs
!= 1 || TAILQ_FIRST(&ncp
->nc_list
))) {
687 if ((flags
& MNT_FORCE
) == 0) {
689 mount_warning(mp
, "Cannot unmount: "
695 mount_warning(mp
, "Forced unmount: "
706 * nchandle records ref the mount structure. Expect a count of 1
707 * (our mount->mnt_ncmountpt).
709 if (mp
->mnt_refs
!= 1) {
710 if ((flags
& MNT_FORCE
) == 0) {
711 mount_warning(mp
, "Cannot unmount: "
712 "%d process references still "
713 "present", mp
->mnt_refs
);
716 mount_warning(mp
, "Forced unmount: "
717 "%d process references still "
718 "present", mp
->mnt_refs
);
724 * Decomission our special mnt_syncer vnode. This also stops
725 * the vnlru code. If we are unable to unmount we recommission
729 if ((vp
= mp
->mnt_syncer
) != NULL
) {
730 mp
->mnt_syncer
= NULL
;
733 if (((mp
->mnt_flag
& MNT_RDONLY
) ||
734 (error
= VFS_SYNC(mp
, MNT_WAIT
)) == 0) ||
735 (flags
& MNT_FORCE
)) {
736 error
= VFS_UNMOUNT(mp
, flags
);
740 if (mp
->mnt_syncer
== NULL
)
741 vfs_allocate_syncvnode(mp
);
742 mp
->mnt_kern_flag
&= ~(MNTK_UNMOUNT
| MNTK_UNMOUNTF
);
743 mp
->mnt_flag
|= async_flag
;
744 lockmgr(&mp
->mnt_lock
, LK_RELEASE
);
745 if (mp
->mnt_kern_flag
& MNTK_MWAIT
)
750 * Clean up any journals still associated with the mount after
751 * filesystem activity has ceased.
753 journal_remove_all_journals(mp
,
754 ((flags
& MNT_FORCE
) ? MC_JOURNAL_STOP_IMM
: 0));
756 mountlist_remove(mp
);
759 * Remove any installed vnode ops here so the individual VFSs don't
762 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_coherency_ops
);
763 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_journal_ops
);
764 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_norm_ops
);
765 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_spec_ops
);
766 vfs_rm_vnodeops(mp
, NULL
, &mp
->mnt_vn_fifo_ops
);
768 if (mp
->mnt_ncmountpt
.ncp
!= NULL
) {
769 nch
= mp
->mnt_ncmountpt
;
770 cache_zero(&mp
->mnt_ncmountpt
);
771 cache_clrmountpt(&nch
);
774 if (mp
->mnt_ncmounton
.ncp
!= NULL
) {
775 nch
= mp
->mnt_ncmounton
;
776 cache_zero(&mp
->mnt_ncmounton
);
777 cache_clrmountpt(&nch
);
781 mp
->mnt_vfc
->vfc_refcount
--;
782 if (!TAILQ_EMPTY(&mp
->mnt_nvnodelist
))
783 panic("unmount: dangling vnode");
784 lockmgr(&mp
->mnt_lock
, LK_RELEASE
);
785 if (mp
->mnt_kern_flag
& MNTK_MWAIT
)
794 mount_warning(struct mount
*mp
, const char *ctl
, ...)
801 if (cache_fullpath(NULL
, &mp
->mnt_ncmounton
, &ptr
, &buf
) == 0) {
802 kprintf("unmount(%s): ", ptr
);
807 kprintf("unmount(%p", mp
);
808 if (mp
->mnt_ncmounton
.ncp
&& mp
->mnt_ncmounton
.ncp
->nc_name
)
809 kprintf(",%s", mp
->mnt_ncmounton
.ncp
->nc_name
);
818 * Shim cache_fullpath() to handle the case where a process is chrooted into
819 * a subdirectory of a mount. In this case if the root mount matches the
820 * process root directory's mount we have to specify the process's root
821 * directory instead of the mount point, because the mount point might
822 * be above the root directory.
826 mount_path(struct proc
*p
, struct mount
*mp
, char **rb
, char **fb
)
828 struct nchandle
*nch
;
830 if (p
&& p
->p_fd
->fd_nrdir
.mount
== mp
)
831 nch
= &p
->p_fd
->fd_nrdir
;
833 nch
= &mp
->mnt_ncmountpt
;
834 return(cache_fullpath(p
, nch
, rb
, fb
));
838 * Sync each mounted filesystem.
842 static int syncprt
= 0;
843 SYSCTL_INT(_debug
, OID_AUTO
, syncprt
, CTLFLAG_RW
, &syncprt
, 0, "");
846 static int sync_callback(struct mount
*mp
, void *data
);
852 sys_sync(struct sync_args
*uap
)
855 mountlist_scan(sync_callback
, NULL
, MNTSCAN_FORWARD
);
858 * print out buffer pool stat information on each sync() call.
869 sync_callback(struct mount
*mp
, void *data __unused
)
873 if ((mp
->mnt_flag
& MNT_RDONLY
) == 0) {
874 asyncflag
= mp
->mnt_flag
& MNT_ASYNC
;
875 mp
->mnt_flag
&= ~MNT_ASYNC
;
876 vfs_msync(mp
, MNT_NOWAIT
);
877 VFS_SYNC(mp
, MNT_NOWAIT
);
878 mp
->mnt_flag
|= asyncflag
;
883 /* XXX PRISON: could be per prison flag */
884 static int prison_quotas
;
886 SYSCTL_INT(_kern_prison
, OID_AUTO
, quotas
, CTLFLAG_RW
, &prison_quotas
, 0, "");
890 * quotactl_args(char *path, int fcmd, int uid, caddr_t arg)
892 * Change filesystem quotas.
897 sys_quotactl(struct quotactl_args
*uap
)
899 struct nlookupdata nd
;
908 if (td
->td_ucred
->cr_prison
&& !prison_quotas
) {
913 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
915 error
= nlookup(&nd
);
917 mp
= nd
.nl_nch
.mount
;
918 error
= VFS_QUOTACTL(mp
, uap
->cmd
, uap
->uid
,
919 uap
->arg
, nd
.nl_cred
);
928 * mountctl(char *path, int op, int fd, const void *ctl, int ctllen,
929 * void *buf, int buflen)
931 * This function operates on a mount point and executes the specified
932 * operation using the specified control data, and possibly returns data.
934 * The actual number of bytes stored in the result buffer is returned, 0
935 * if none, otherwise an error is returned.
940 sys_mountctl(struct mountctl_args
*uap
)
942 struct thread
*td
= curthread
;
943 struct proc
*p
= td
->td_proc
;
951 * Sanity and permissions checks. We must be root.
954 if (td
->td_ucred
->cr_prison
!= NULL
)
956 if ((uap
->op
!= MOUNTCTL_MOUNTFLAGS
) &&
957 (error
= priv_check(td
, PRIV_ROOT
)) != 0)
961 * Argument length checks
963 if (uap
->ctllen
< 0 || uap
->ctllen
> 1024)
965 if (uap
->buflen
< 0 || uap
->buflen
> 16 * 1024)
967 if (uap
->path
== NULL
)
971 * Allocate the necessary buffers and copyin data
973 path
= objcache_get(namei_oc
, M_WAITOK
);
974 error
= copyinstr(uap
->path
, path
, MAXPATHLEN
, NULL
);
979 ctl
= kmalloc(uap
->ctllen
+ 1, M_TEMP
, M_WAITOK
|M_ZERO
);
980 error
= copyin(uap
->ctl
, ctl
, uap
->ctllen
);
985 buf
= kmalloc(uap
->buflen
+ 1, M_TEMP
, M_WAITOK
|M_ZERO
);
988 * Validate the descriptor
991 fp
= holdfp(p
->p_fd
, uap
->fd
, -1);
1001 * Execute the internal kernel function and clean up.
1004 error
= kern_mountctl(path
, uap
->op
, fp
, ctl
, uap
->ctllen
, buf
, uap
->buflen
, &uap
->sysmsg_result
);
1008 if (error
== 0 && uap
->sysmsg_result
> 0)
1009 error
= copyout(buf
, uap
->buf
, uap
->sysmsg_result
);
1012 objcache_put(namei_oc
, path
);
1021 * Execute a mount control operation by resolving the path to a mount point
1022 * and calling vop_mountctl().
1024 * Use the mount point from the nch instead of the vnode so nullfs mounts
1025 * can properly spike the VOP.
1028 kern_mountctl(const char *path
, int op
, struct file
*fp
,
1029 const void *ctl
, int ctllen
,
1030 void *buf
, int buflen
, int *res
)
1034 struct nlookupdata nd
;
1039 error
= nlookup_init(&nd
, path
, UIO_SYSSPACE
, NLC_FOLLOW
);
1041 error
= nlookup(&nd
);
1043 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
1044 mp
= nd
.nl_nch
.mount
;
1051 * Must be the root of the filesystem
1053 if ((vp
->v_flag
& (VROOT
|VPFSROOT
)) == 0) {
1057 error
= vop_mountctl(mp
->mnt_vn_use_ops
, vp
, op
, fp
, ctl
, ctllen
,
1064 kern_statfs(struct nlookupdata
*nd
, struct statfs
*buf
)
1066 struct thread
*td
= curthread
;
1067 struct proc
*p
= td
->td_proc
;
1070 char *fullpath
, *freepath
;
1073 if ((error
= nlookup(nd
)) != 0)
1075 mp
= nd
->nl_nch
.mount
;
1077 if ((error
= VFS_STATFS(mp
, sp
, nd
->nl_cred
)) != 0)
1080 error
= mount_path(p
, mp
, &fullpath
, &freepath
);
1083 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
1084 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
1085 kfree(freepath
, M_TEMP
);
1087 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
1088 bcopy(sp
, buf
, sizeof(*buf
));
1089 /* Only root should have access to the fsid's. */
1090 if (priv_check(td
, PRIV_ROOT
))
1091 buf
->f_fsid
.val
[0] = buf
->f_fsid
.val
[1] = 0;
1096 * statfs_args(char *path, struct statfs *buf)
1098 * Get filesystem statistics.
1103 sys_statfs(struct statfs_args
*uap
)
1105 struct nlookupdata nd
;
1110 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1112 error
= kern_statfs(&nd
, &buf
);
1115 error
= copyout(&buf
, uap
->buf
, sizeof(*uap
->buf
));
1124 kern_fstatfs(int fd
, struct statfs
*buf
)
1126 struct thread
*td
= curthread
;
1127 struct proc
*p
= td
->td_proc
;
1131 char *fullpath
, *freepath
;
1135 if ((error
= holdvnode(p
->p_fd
, fd
, &fp
)) != 0)
1138 mp
= ((struct vnode
*)fp
->f_data
)->v_mount
;
1143 if (fp
->f_cred
== NULL
) {
1148 if ((error
= VFS_STATFS(mp
, sp
, fp
->f_cred
)) != 0)
1151 if ((error
= mount_path(p
, mp
, &fullpath
, &freepath
)) != 0)
1153 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
1154 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
1155 kfree(freepath
, M_TEMP
);
1157 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
1158 bcopy(sp
, buf
, sizeof(*buf
));
1160 /* Only root should have access to the fsid's. */
1161 if (priv_check(td
, PRIV_ROOT
))
1162 buf
->f_fsid
.val
[0] = buf
->f_fsid
.val
[1] = 0;
1171 * fstatfs_args(int fd, struct statfs *buf)
1173 * Get filesystem statistics.
1178 sys_fstatfs(struct fstatfs_args
*uap
)
1183 error
= kern_fstatfs(uap
->fd
, &buf
);
1186 error
= copyout(&buf
, uap
->buf
, sizeof(*uap
->buf
));
1191 kern_statvfs(struct nlookupdata
*nd
, struct statvfs
*buf
)
1197 if ((error
= nlookup(nd
)) != 0)
1199 mp
= nd
->nl_nch
.mount
;
1200 sp
= &mp
->mnt_vstat
;
1201 if ((error
= VFS_STATVFS(mp
, sp
, nd
->nl_cred
)) != 0)
1205 if (mp
->mnt_flag
& MNT_RDONLY
)
1206 sp
->f_flag
|= ST_RDONLY
;
1207 if (mp
->mnt_flag
& MNT_NOSUID
)
1208 sp
->f_flag
|= ST_NOSUID
;
1209 bcopy(sp
, buf
, sizeof(*buf
));
1214 * statfs_args(char *path, struct statfs *buf)
1216 * Get filesystem statistics.
1221 sys_statvfs(struct statvfs_args
*uap
)
1223 struct nlookupdata nd
;
1228 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1230 error
= kern_statvfs(&nd
, &buf
);
1233 error
= copyout(&buf
, uap
->buf
, sizeof(*uap
->buf
));
1239 kern_fstatvfs(int fd
, struct statvfs
*buf
)
1241 struct thread
*td
= curthread
;
1242 struct proc
*p
= td
->td_proc
;
1249 if ((error
= holdvnode(p
->p_fd
, fd
, &fp
)) != 0)
1251 mp
= ((struct vnode
*)fp
->f_data
)->v_mount
;
1256 if (fp
->f_cred
== NULL
) {
1260 sp
= &mp
->mnt_vstat
;
1261 if ((error
= VFS_STATVFS(mp
, sp
, fp
->f_cred
)) != 0)
1265 if (mp
->mnt_flag
& MNT_RDONLY
)
1266 sp
->f_flag
|= ST_RDONLY
;
1267 if (mp
->mnt_flag
& MNT_NOSUID
)
1268 sp
->f_flag
|= ST_NOSUID
;
1270 bcopy(sp
, buf
, sizeof(*buf
));
1278 * fstatfs_args(int fd, struct statfs *buf)
1280 * Get filesystem statistics.
1285 sys_fstatvfs(struct fstatvfs_args
*uap
)
1291 error
= kern_fstatvfs(uap
->fd
, &buf
);
1295 error
= copyout(&buf
, uap
->buf
, sizeof(*uap
->buf
));
1300 * getfsstat_args(struct statfs *buf, long bufsize, int flags)
1302 * Get statistics on all filesystems.
1305 struct getfsstat_info
{
1306 struct statfs
*sfsp
;
1314 static int getfsstat_callback(struct mount
*, void *);
1320 sys_getfsstat(struct getfsstat_args
*uap
)
1322 struct thread
*td
= curthread
;
1323 struct getfsstat_info info
;
1325 bzero(&info
, sizeof(info
));
1327 info
.maxcount
= uap
->bufsize
/ sizeof(struct statfs
);
1328 info
.sfsp
= uap
->buf
;
1330 info
.flags
= uap
->flags
;
1334 mountlist_scan(getfsstat_callback
, &info
, MNTSCAN_FORWARD
);
1336 if (info
.sfsp
&& info
.count
> info
.maxcount
)
1337 uap
->sysmsg_result
= info
.maxcount
;
1339 uap
->sysmsg_result
= info
.count
;
1340 return (info
.error
);
1344 getfsstat_callback(struct mount
*mp
, void *data
)
1346 struct getfsstat_info
*info
= data
;
1352 if (info
->sfsp
&& info
->count
< info
->maxcount
) {
1353 if (info
->td
->td_proc
&&
1354 !chroot_visible_mnt(mp
, info
->td
->td_proc
)) {
1360 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1361 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1362 * overrides MNT_WAIT.
1364 if (((info
->flags
& (MNT_LAZY
|MNT_NOWAIT
)) == 0 ||
1365 (info
->flags
& MNT_WAIT
)) &&
1366 (error
= VFS_STATFS(mp
, sp
, info
->td
->td_ucred
))) {
1369 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
1371 error
= mount_path(info
->td
->td_proc
, mp
, &fullpath
, &freepath
);
1373 info
->error
= error
;
1376 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
1377 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
1378 kfree(freepath
, M_TEMP
);
1380 error
= copyout(sp
, info
->sfsp
, sizeof(*sp
));
1382 info
->error
= error
;
1392 * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf,
1393 long bufsize, int flags)
1395 * Get statistics on all filesystems.
1398 struct getvfsstat_info
{
1399 struct statfs
*sfsp
;
1400 struct statvfs
*vsfsp
;
1408 static int getvfsstat_callback(struct mount
*, void *);
1414 sys_getvfsstat(struct getvfsstat_args
*uap
)
1416 struct thread
*td
= curthread
;
1417 struct getvfsstat_info info
;
1419 bzero(&info
, sizeof(info
));
1421 info
.maxcount
= uap
->vbufsize
/ sizeof(struct statvfs
);
1422 info
.sfsp
= uap
->buf
;
1423 info
.vsfsp
= uap
->vbuf
;
1425 info
.flags
= uap
->flags
;
1429 mountlist_scan(getvfsstat_callback
, &info
, MNTSCAN_FORWARD
);
1430 if (info
.vsfsp
&& info
.count
> info
.maxcount
)
1431 uap
->sysmsg_result
= info
.maxcount
;
1433 uap
->sysmsg_result
= info
.count
;
1435 return (info
.error
);
1439 getvfsstat_callback(struct mount
*mp
, void *data
)
1441 struct getvfsstat_info
*info
= data
;
1443 struct statvfs
*vsp
;
1448 if (info
->vsfsp
&& info
->count
< info
->maxcount
) {
1449 if (info
->td
->td_proc
&&
1450 !chroot_visible_mnt(mp
, info
->td
->td_proc
)) {
1454 vsp
= &mp
->mnt_vstat
;
1457 * If MNT_NOWAIT or MNT_LAZY is specified, do not
1458 * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY
1459 * overrides MNT_WAIT.
1461 if (((info
->flags
& (MNT_LAZY
|MNT_NOWAIT
)) == 0 ||
1462 (info
->flags
& MNT_WAIT
)) &&
1463 (error
= VFS_STATFS(mp
, sp
, info
->td
->td_ucred
))) {
1466 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
1468 if (((info
->flags
& (MNT_LAZY
|MNT_NOWAIT
)) == 0 ||
1469 (info
->flags
& MNT_WAIT
)) &&
1470 (error
= VFS_STATVFS(mp
, vsp
, info
->td
->td_ucred
))) {
1474 if (mp
->mnt_flag
& MNT_RDONLY
)
1475 vsp
->f_flag
|= ST_RDONLY
;
1476 if (mp
->mnt_flag
& MNT_NOSUID
)
1477 vsp
->f_flag
|= ST_NOSUID
;
1479 error
= mount_path(info
->td
->td_proc
, mp
, &fullpath
, &freepath
);
1481 info
->error
= error
;
1484 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
1485 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
1486 kfree(freepath
, M_TEMP
);
1488 error
= copyout(sp
, info
->sfsp
, sizeof(*sp
));
1490 error
= copyout(vsp
, info
->vsfsp
, sizeof(*vsp
));
1492 info
->error
= error
;
1504 * fchdir_args(int fd)
1506 * Change current working directory to a given file descriptor.
1511 sys_fchdir(struct fchdir_args
*uap
)
1513 struct thread
*td
= curthread
;
1514 struct proc
*p
= td
->td_proc
;
1515 struct filedesc
*fdp
= p
->p_fd
;
1516 struct vnode
*vp
, *ovp
;
1519 struct nchandle nch
, onch
, tnch
;
1522 if ((error
= holdvnode(fdp
, uap
->fd
, &fp
)) != 0)
1525 vp
= (struct vnode
*)fp
->f_data
;
1527 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
1528 if (fp
->f_nchandle
.ncp
== NULL
)
1531 error
= checkvp_chdir(vp
, td
);
1536 cache_copy(&fp
->f_nchandle
, &nch
);
1539 * If the ncp has become a mount point, traverse through
1543 while (!error
&& (nch
.ncp
->nc_flag
& NCF_ISMOUNTPT
) &&
1544 (mp
= cache_findmount(&nch
)) != NULL
1546 error
= nlookup_mp(mp
, &tnch
);
1548 cache_unlock(&tnch
); /* leave ref intact */
1550 vp
= tnch
.ncp
->nc_vp
;
1551 error
= vget(vp
, LK_SHARED
);
1552 KKASSERT(error
== 0);
1559 onch
= fdp
->fd_ncdir
;
1560 vn_unlock(vp
); /* leave ref intact */
1562 fdp
->fd_ncdir
= nch
;
1576 kern_chdir(struct nlookupdata
*nd
)
1578 struct thread
*td
= curthread
;
1579 struct proc
*p
= td
->td_proc
;
1580 struct filedesc
*fdp
= p
->p_fd
;
1581 struct vnode
*vp
, *ovp
;
1582 struct nchandle onch
;
1585 if ((error
= nlookup(nd
)) != 0)
1587 if ((vp
= nd
->nl_nch
.ncp
->nc_vp
) == NULL
)
1589 if ((error
= vget(vp
, LK_SHARED
)) != 0)
1592 error
= checkvp_chdir(vp
, td
);
1596 onch
= fdp
->fd_ncdir
;
1597 cache_unlock(&nd
->nl_nch
); /* leave reference intact */
1598 fdp
->fd_ncdir
= nd
->nl_nch
;
1602 cache_zero(&nd
->nl_nch
);
1610 * chdir_args(char *path)
1612 * Change current working directory (``.'').
1617 sys_chdir(struct chdir_args
*uap
)
1619 struct nlookupdata nd
;
1623 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1625 error
= kern_chdir(&nd
);
1632 * Helper function for raised chroot(2) security function: Refuse if
1633 * any filedescriptors are open directories.
1636 chroot_refuse_vdir_fds(struct filedesc
*fdp
)
1643 for (fd
= 0; fd
< fdp
->fd_nfiles
; fd
++) {
1644 if ((error
= holdvnode(fdp
, fd
, &fp
)) != 0)
1646 vp
= (struct vnode
*)fp
->f_data
;
1647 if (vp
->v_type
!= VDIR
) {
1658 * This sysctl determines if we will allow a process to chroot(2) if it
1659 * has a directory open:
1660 * 0: disallowed for all processes.
1661 * 1: allowed for processes that were not already chroot(2)'ed.
1662 * 2: allowed for all processes.
1665 static int chroot_allow_open_directories
= 1;
1667 SYSCTL_INT(_kern
, OID_AUTO
, chroot_allow_open_directories
, CTLFLAG_RW
,
1668 &chroot_allow_open_directories
, 0, "");
1671 * chroot to the specified namecache entry. We obtain the vp from the
1672 * namecache data. The passed ncp must be locked and referenced and will
1673 * remain locked and referenced on return.
1676 kern_chroot(struct nchandle
*nch
)
1678 struct thread
*td
= curthread
;
1679 struct proc
*p
= td
->td_proc
;
1680 struct filedesc
*fdp
= p
->p_fd
;
1685 * Only privileged user can chroot
1687 error
= priv_check_cred(td
->td_ucred
, PRIV_VFS_CHROOT
, 0);
1692 * Disallow open directory descriptors (fchdir() breakouts).
1694 if (chroot_allow_open_directories
== 0 ||
1695 (chroot_allow_open_directories
== 1 && fdp
->fd_rdir
!= rootvnode
)) {
1696 if ((error
= chroot_refuse_vdir_fds(fdp
)) != 0)
1699 if ((vp
= nch
->ncp
->nc_vp
) == NULL
)
1702 if ((error
= vget(vp
, LK_SHARED
)) != 0)
1706 * Check the validity of vp as a directory to change to and
1707 * associate it with rdir/jdir.
1709 error
= checkvp_chdir(vp
, td
);
1710 vn_unlock(vp
); /* leave reference intact */
1712 vrele(fdp
->fd_rdir
);
1713 fdp
->fd_rdir
= vp
; /* reference inherited by fd_rdir */
1714 cache_drop(&fdp
->fd_nrdir
);
1715 cache_copy(nch
, &fdp
->fd_nrdir
);
1716 if (fdp
->fd_jdir
== NULL
) {
1719 cache_copy(nch
, &fdp
->fd_njdir
);
1728 * chroot_args(char *path)
1730 * Change notion of root (``/'') directory.
1735 sys_chroot(struct chroot_args
*uap
)
1737 struct thread
*td __debugvar
= curthread
;
1738 struct nlookupdata nd
;
1741 KKASSERT(td
->td_proc
);
1743 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
1745 nd
.nl_flags
|= NLC_EXEC
;
1746 error
= nlookup(&nd
);
1748 error
= kern_chroot(&nd
.nl_nch
);
1756 * Common routine for chroot and chdir. Given a locked, referenced vnode,
1757 * determine whether it is legal to chdir to the vnode. The vnode's state
1758 * is not changed by this call.
1761 checkvp_chdir(struct vnode
*vp
, struct thread
*td
)
1765 if (vp
->v_type
!= VDIR
)
1768 error
= VOP_EACCESS(vp
, VEXEC
, td
->td_ucred
);
1776 kern_open(struct nlookupdata
*nd
, int oflags
, int mode
, int *res
)
1778 struct thread
*td
= curthread
;
1779 struct proc
*p
= td
->td_proc
;
1780 struct lwp
*lp
= td
->td_lwp
;
1781 struct filedesc
*fdp
= p
->p_fd
;
1786 int type
, indx
, error
;
1789 if ((oflags
& O_ACCMODE
) == O_ACCMODE
)
1791 flags
= FFLAGS(oflags
);
1792 error
= falloc(lp
, &nfp
, NULL
);
1796 cmode
= ((mode
&~ fdp
->fd_cmask
) & ALLPERMS
) & ~S_ISTXT
;
1799 * XXX p_dupfd is a real mess. It allows a device to return a
1800 * file descriptor to be duplicated rather then doing the open
1806 * Call vn_open() to do the lookup and assign the vnode to the
1807 * file pointer. vn_open() does not change the ref count on fp
1808 * and the vnode, on success, will be inherited by the file pointer
1811 nd
->nl_flags
|= NLC_LOCKVP
;
1812 error
= vn_open(nd
, fp
, flags
, cmode
);
1816 * handle special fdopen() case. bleh. dupfdopen() is
1817 * responsible for dropping the old contents of ofiles[indx]
1820 * Note that fsetfd() will add a ref to fp which represents
1821 * the fd_files[] assignment. We must still drop our
1824 if ((error
== ENODEV
|| error
== ENXIO
) && lp
->lwp_dupfd
>= 0) {
1825 if (fdalloc(p
, 0, &indx
) == 0) {
1826 error
= dupfdopen(fdp
, indx
, lp
->lwp_dupfd
, flags
, error
);
1829 fdrop(fp
); /* our ref */
1832 fsetfd(fdp
, NULL
, indx
);
1835 fdrop(fp
); /* our ref */
1836 if (error
== ERESTART
)
1842 * ref the vnode for ourselves so it can't be ripped out from under
1843 * is. XXX need an ND flag to request that the vnode be returned
1846 * Reserve a file descriptor but do not assign it until the open
1849 vp
= (struct vnode
*)fp
->f_data
;
1851 if ((error
= fdalloc(p
, 0, &indx
)) != 0) {
1858 * If no error occurs the vp will have been assigned to the file
1863 if (flags
& (O_EXLOCK
| O_SHLOCK
)) {
1864 lf
.l_whence
= SEEK_SET
;
1867 if (flags
& O_EXLOCK
)
1868 lf
.l_type
= F_WRLCK
;
1870 lf
.l_type
= F_RDLCK
;
1871 if (flags
& FNONBLOCK
)
1876 if ((error
= VOP_ADVLOCK(vp
, (caddr_t
)fp
, F_SETLK
, &lf
, type
)) != 0) {
1878 * lock request failed. Clean up the reserved
1882 fsetfd(fdp
, NULL
, indx
);
1886 fp
->f_flag
|= FHASLOCK
;
1890 * Assert that all regular file vnodes were created with a object.
1892 KASSERT(vp
->v_type
!= VREG
|| vp
->v_object
!= NULL
,
1893 ("open: regular file has no backing object after vn_open"));
1899 * release our private reference, leaving the one associated with the
1900 * descriptor table intact.
1902 fsetfd(fdp
, fp
, indx
);
1909 * open_args(char *path, int flags, int mode)
1911 * Check permissions, allocate an open file structure,
1912 * and call the device open routine if any.
1917 sys_open(struct open_args
*uap
)
1919 CACHE_MPLOCK_DECLARE
;
1920 struct nlookupdata nd
;
1924 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
1926 error
= kern_open(&nd
, uap
->flags
,
1927 uap
->mode
, &uap
->sysmsg_result
);
1935 * openat_args(int fd, char *path, int flags, int mode)
1940 sys_openat(struct openat_args
*uap
)
1942 CACHE_MPLOCK_DECLARE
;
1943 struct nlookupdata nd
;
1948 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, 0);
1950 error
= kern_open(&nd
, uap
->flags
, uap
->mode
,
1951 &uap
->sysmsg_result
);
1953 nlookup_done_at(&nd
, fp
);
1959 kern_mknod(struct nlookupdata
*nd
, int mode
, int rmajor
, int rminor
)
1961 struct thread
*td
= curthread
;
1962 struct proc
*p
= td
->td_proc
;
1971 vattr
.va_mode
= (mode
& ALLPERMS
) &~ p
->p_fd
->fd_cmask
;
1972 vattr
.va_rmajor
= rmajor
;
1973 vattr
.va_rminor
= rminor
;
1975 switch (mode
& S_IFMT
) {
1976 case S_IFMT
: /* used by badsect to flag bad sectors */
1977 error
= priv_check_cred(td
->td_ucred
, PRIV_VFS_MKNOD_BAD
, 0);
1978 vattr
.va_type
= VBAD
;
1981 error
= priv_check(td
, PRIV_VFS_MKNOD_DEV
);
1982 vattr
.va_type
= VCHR
;
1985 error
= priv_check(td
, PRIV_VFS_MKNOD_DEV
);
1986 vattr
.va_type
= VBLK
;
1989 error
= priv_check_cred(td
->td_ucred
, PRIV_VFS_MKNOD_WHT
, 0);
1992 case S_IFDIR
: /* special directories support for HAMMER */
1993 error
= priv_check_cred(td
->td_ucred
, PRIV_VFS_MKNOD_DIR
, 0);
1994 vattr
.va_type
= VDIR
;
2005 nd
->nl_flags
|= NLC_CREATE
| NLC_REFDVP
;
2006 if ((error
= nlookup(nd
)) != 0)
2008 if (nd
->nl_nch
.ncp
->nc_vp
)
2010 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
2014 error
= VOP_NWHITEOUT(&nd
->nl_nch
, nd
->nl_dvp
,
2015 nd
->nl_cred
, NAMEI_CREATE
);
2018 error
= VOP_NMKNOD(&nd
->nl_nch
, nd
->nl_dvp
,
2019 &vp
, nd
->nl_cred
, &vattr
);
2027 * mknod_args(char *path, int mode, int dev)
2029 * Create a special file.
2034 sys_mknod(struct mknod_args
*uap
)
2036 struct nlookupdata nd
;
2040 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2042 error
= kern_mknod(&nd
, uap
->mode
,
2043 umajor(uap
->dev
), uminor(uap
->dev
));
2051 kern_mkfifo(struct nlookupdata
*nd
, int mode
)
2053 struct thread
*td
= curthread
;
2054 struct proc
*p
= td
->td_proc
;
2061 nd
->nl_flags
|= NLC_CREATE
| NLC_REFDVP
;
2062 if ((error
= nlookup(nd
)) != 0)
2064 if (nd
->nl_nch
.ncp
->nc_vp
)
2066 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
2070 vattr
.va_type
= VFIFO
;
2071 vattr
.va_mode
= (mode
& ALLPERMS
) &~ p
->p_fd
->fd_cmask
;
2073 error
= VOP_NMKNOD(&nd
->nl_nch
, nd
->nl_dvp
, &vp
, nd
->nl_cred
, &vattr
);
2080 * mkfifo_args(char *path, int mode)
2082 * Create a named pipe.
2087 sys_mkfifo(struct mkfifo_args
*uap
)
2089 struct nlookupdata nd
;
2093 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2095 error
= kern_mkfifo(&nd
, uap
->mode
);
2101 static int hardlink_check_uid
= 0;
2102 SYSCTL_INT(_security
, OID_AUTO
, hardlink_check_uid
, CTLFLAG_RW
,
2103 &hardlink_check_uid
, 0,
2104 "Unprivileged processes cannot create hard links to files owned by other "
2106 static int hardlink_check_gid
= 0;
2107 SYSCTL_INT(_security
, OID_AUTO
, hardlink_check_gid
, CTLFLAG_RW
,
2108 &hardlink_check_gid
, 0,
2109 "Unprivileged processes cannot create hard links to files owned by other "
2113 can_hardlink(struct vnode
*vp
, struct thread
*td
, struct ucred
*cred
)
2119 * Shortcut if disabled
2121 if (hardlink_check_uid
== 0 && hardlink_check_gid
== 0)
2125 * Privileged user can always hardlink
2127 if (priv_check_cred(cred
, PRIV_VFS_LINK
, 0) == 0)
2131 * Otherwise only if the originating file is owned by the
2132 * same user or group. Note that any group is allowed if
2133 * the file is owned by the caller.
2135 error
= VOP_GETATTR(vp
, &va
);
2139 if (hardlink_check_uid
) {
2140 if (cred
->cr_uid
!= va
.va_uid
)
2144 if (hardlink_check_gid
) {
2145 if (cred
->cr_uid
!= va
.va_uid
&& !groupmember(va
.va_gid
, cred
))
2153 kern_link(struct nlookupdata
*nd
, struct nlookupdata
*linknd
)
2155 struct thread
*td
= curthread
;
2160 * Lookup the source and obtained a locked vnode.
2162 * You may only hardlink a file which you have write permission
2163 * on or which you own.
2165 * XXX relookup on vget failure / race ?
2168 nd
->nl_flags
|= NLC_WRITE
| NLC_OWN
| NLC_HLINK
;
2169 if ((error
= nlookup(nd
)) != 0)
2171 vp
= nd
->nl_nch
.ncp
->nc_vp
;
2172 KKASSERT(vp
!= NULL
);
2173 if (vp
->v_type
== VDIR
)
2174 return (EPERM
); /* POSIX */
2175 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
2177 if ((error
= vget(vp
, LK_EXCLUSIVE
)) != 0)
2181 * Unlock the source so we can lookup the target without deadlocking
2182 * (XXX vp is locked already, possible other deadlock?). The target
2185 KKASSERT(nd
->nl_flags
& NLC_NCPISLOCKED
);
2186 nd
->nl_flags
&= ~NLC_NCPISLOCKED
;
2187 cache_unlock(&nd
->nl_nch
);
2190 linknd
->nl_flags
|= NLC_CREATE
| NLC_REFDVP
;
2191 if ((error
= nlookup(linknd
)) != 0) {
2195 if (linknd
->nl_nch
.ncp
->nc_vp
) {
2199 if ((error
= vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
)) != 0) {
2205 * Finally run the new API VOP.
2207 error
= can_hardlink(vp
, td
, td
->td_ucred
);
2209 error
= VOP_NLINK(&linknd
->nl_nch
, linknd
->nl_dvp
,
2210 vp
, linknd
->nl_cred
);
2217 * link_args(char *path, char *link)
2219 * Make a hard file link.
2224 sys_link(struct link_args
*uap
)
2226 struct nlookupdata nd
, linknd
;
2230 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2232 error
= nlookup_init(&linknd
, uap
->link
, UIO_USERSPACE
, 0);
2234 error
= kern_link(&nd
, &linknd
);
2235 nlookup_done(&linknd
);
2243 kern_symlink(struct nlookupdata
*nd
, char *path
, int mode
)
2251 nd
->nl_flags
|= NLC_CREATE
| NLC_REFDVP
;
2252 if ((error
= nlookup(nd
)) != 0)
2254 if (nd
->nl_nch
.ncp
->nc_vp
)
2256 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
2260 vattr
.va_mode
= mode
;
2261 error
= VOP_NSYMLINK(&nd
->nl_nch
, dvp
, &vp
, nd
->nl_cred
, &vattr
, path
);
2268 * symlink(char *path, char *link)
2270 * Make a symbolic link.
2275 sys_symlink(struct symlink_args
*uap
)
2277 struct thread
*td
= curthread
;
2278 struct nlookupdata nd
;
2283 path
= objcache_get(namei_oc
, M_WAITOK
);
2284 error
= copyinstr(uap
->path
, path
, MAXPATHLEN
, NULL
);
2287 error
= nlookup_init(&nd
, uap
->link
, UIO_USERSPACE
, 0);
2289 mode
= ACCESSPERMS
& ~td
->td_proc
->p_fd
->fd_cmask
;
2290 error
= kern_symlink(&nd
, path
, mode
);
2295 objcache_put(namei_oc
, path
);
2300 * undelete_args(char *path)
2302 * Delete a whiteout from the filesystem.
2307 sys_undelete(struct undelete_args
*uap
)
2309 struct nlookupdata nd
;
2313 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2315 nd
.nl_flags
|= NLC_DELETE
| NLC_REFDVP
;
2317 error
= nlookup(&nd
);
2319 error
= ncp_writechk(&nd
.nl_nch
);
2321 error
= VOP_NWHITEOUT(&nd
.nl_nch
, nd
.nl_dvp
, nd
.nl_cred
,
2330 kern_unlink(struct nlookupdata
*nd
)
2335 nd
->nl_flags
|= NLC_DELETE
| NLC_REFDVP
;
2336 if ((error
= nlookup(nd
)) != 0)
2338 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
2340 error
= VOP_NREMOVE(&nd
->nl_nch
, nd
->nl_dvp
, nd
->nl_cred
);
2345 * unlink_args(char *path)
2347 * Delete a name from the filesystem.
2352 sys_unlink(struct unlink_args
*uap
)
2354 struct nlookupdata nd
;
2358 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2360 error
= kern_unlink(&nd
);
2368 * unlinkat_args(int fd, char *path, int flags)
2370 * Delete the file or directory entry pointed to by fd/path.
2375 sys_unlinkat(struct unlinkat_args
*uap
)
2377 struct nlookupdata nd
;
2381 if (uap
->flags
& ~AT_REMOVEDIR
)
2385 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
, 0);
2387 if (uap
->flags
& AT_REMOVEDIR
)
2388 error
= kern_rmdir(&nd
);
2390 error
= kern_unlink(&nd
);
2392 nlookup_done_at(&nd
, fp
);
2401 kern_lseek(int fd
, off_t offset
, int whence
, off_t
*res
)
2403 struct thread
*td
= curthread
;
2404 struct proc
*p
= td
->td_proc
;
2411 fp
= holdfp(p
->p_fd
, fd
, -1);
2414 if (fp
->f_type
!= DTYPE_VNODE
) {
2418 vp
= (struct vnode
*)fp
->f_data
;
2422 spin_lock_wr(&fp
->f_spin
);
2423 new_offset
= fp
->f_offset
+ offset
;
2428 error
= VOP_GETATTR(vp
, &vattr
);
2430 spin_lock_wr(&fp
->f_spin
);
2431 new_offset
= offset
+ vattr
.va_size
;
2434 new_offset
= offset
;
2436 spin_lock_wr(&fp
->f_spin
);
2441 spin_lock_wr(&fp
->f_spin
);
2446 * Validate the seek position. Negative offsets are not allowed
2447 * for regular files or directories.
2449 * Normally we would also not want to allow negative offsets for
2450 * character and block-special devices. However kvm addresses
2451 * on 64 bit architectures might appear to be negative and must
2455 if (new_offset
< 0 &&
2456 (vp
->v_type
== VREG
|| vp
->v_type
== VDIR
)) {
2459 fp
->f_offset
= new_offset
;
2462 *res
= fp
->f_offset
;
2463 spin_unlock_wr(&fp
->f_spin
);
2470 * lseek_args(int fd, int pad, off_t offset, int whence)
2472 * Reposition read/write file offset.
2477 sys_lseek(struct lseek_args
*uap
)
2481 error
= kern_lseek(uap
->fd
, uap
->offset
, uap
->whence
,
2482 &uap
->sysmsg_offset
);
2488 * Check if current process can access given file. amode is a bitmask of *_OK
2489 * access bits. flags is a bitmask of AT_* flags.
2492 kern_access(struct nlookupdata
*nd
, int amode
, int flags
)
2497 if (flags
& ~AT_EACCESS
)
2499 if ((error
= nlookup(nd
)) != 0)
2502 error
= cache_vget(&nd
->nl_nch
, nd
->nl_cred
, LK_EXCLUSIVE
, &vp
);
2506 /* Flags == 0 means only check for existence. */
2515 if ((mode
& VWRITE
) == 0 ||
2516 (error
= vn_writechk(vp
, &nd
->nl_nch
)) == 0)
2517 error
= VOP_ACCESS_FLAGS(vp
, mode
, flags
, nd
->nl_cred
);
2520 * If the file handle is stale we have to re-resolve the
2521 * entry. This is a hack at the moment.
2523 if (error
== ESTALE
) {
2525 cache_setunresolved(&nd
->nl_nch
);
2526 error
= cache_resolve(&nd
->nl_nch
, nd
->nl_cred
);
2539 * access_args(char *path, int flags)
2541 * Check access permissions.
2546 sys_access(struct access_args
*uap
)
2548 struct nlookupdata nd
;
2552 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2554 error
= kern_access(&nd
, uap
->flags
, 0);
2562 * faccessat_args(int fd, char *path, int amode, int flags)
2564 * Check access permissions.
2569 sys_faccessat(struct faccessat_args
*uap
)
2571 struct nlookupdata nd
;
2576 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
, UIO_USERSPACE
,
2579 error
= kern_access(&nd
, uap
->amode
, uap
->flags
);
2580 nlookup_done_at(&nd
, fp
);
2590 kern_stat(struct nlookupdata
*nd
, struct stat
*st
)
2596 if ((error
= nlookup(nd
)) != 0)
2599 if ((vp
= nd
->nl_nch
.ncp
->nc_vp
) == NULL
)
2603 if ((error
= vget(vp
, LK_SHARED
)) != 0)
2605 error
= vn_stat(vp
, st
, nd
->nl_cred
);
2608 * If the file handle is stale we have to re-resolve the entry. This
2609 * is a hack at the moment.
2611 if (error
== ESTALE
) {
2613 cache_setunresolved(&nd
->nl_nch
);
2614 error
= cache_resolve(&nd
->nl_nch
, nd
->nl_cred
);
2624 * stat_args(char *path, struct stat *ub)
2626 * Get file status; this version follows links.
2631 sys_stat(struct stat_args
*uap
)
2633 CACHE_MPLOCK_DECLARE
;
2634 struct nlookupdata nd
;
2639 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2641 error
= kern_stat(&nd
, &st
);
2643 error
= copyout(&st
, uap
->ub
, sizeof(*uap
->ub
));
2651 * lstat_args(char *path, struct stat *ub)
2653 * Get file status; this version does not follow links.
2658 sys_lstat(struct lstat_args
*uap
)
2660 CACHE_MPLOCK_DECLARE
;
2661 struct nlookupdata nd
;
2666 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2668 error
= kern_stat(&nd
, &st
);
2670 error
= copyout(&st
, uap
->ub
, sizeof(*uap
->ub
));
2678 * fstatat_args(int fd, char *path, struct stat *sb, int flags)
2680 * Get status of file pointed to by fd/path.
2685 sys_fstatat(struct fstatat_args
*uap
)
2687 CACHE_MPLOCK_DECLARE
;
2688 struct nlookupdata nd
;
2694 if (uap
->flags
& ~AT_SYMLINK_NOFOLLOW
)
2697 flags
= (uap
->flags
& AT_SYMLINK_NOFOLLOW
) ? 0 : NLC_FOLLOW
;
2700 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
,
2701 UIO_USERSPACE
, flags
);
2703 error
= kern_stat(&nd
, &st
);
2705 error
= copyout(&st
, uap
->sb
, sizeof(*uap
->sb
));
2707 nlookup_done_at(&nd
, fp
);
2713 * pathconf_Args(char *path, int name)
2715 * Get configurable pathname variables.
2720 sys_pathconf(struct pathconf_args
*uap
)
2722 struct nlookupdata nd
;
2728 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2730 error
= nlookup(&nd
);
2732 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
2735 error
= VOP_PATHCONF(vp
, uap
->name
, &uap
->sysmsg_reg
);
2744 * kern_readlink isn't properly split yet. There is a copyin burried
2745 * in VOP_READLINK().
2748 kern_readlink(struct nlookupdata
*nd
, char *buf
, int count
, int *res
)
2750 struct thread
*td
= curthread
;
2756 if ((error
= nlookup(nd
)) != 0)
2758 error
= cache_vget(&nd
->nl_nch
, nd
->nl_cred
, LK_EXCLUSIVE
, &vp
);
2761 if (vp
->v_type
!= VLNK
) {
2764 aiov
.iov_base
= buf
;
2765 aiov
.iov_len
= count
;
2766 auio
.uio_iov
= &aiov
;
2767 auio
.uio_iovcnt
= 1;
2768 auio
.uio_offset
= 0;
2769 auio
.uio_rw
= UIO_READ
;
2770 auio
.uio_segflg
= UIO_USERSPACE
;
2772 auio
.uio_resid
= count
;
2773 error
= VOP_READLINK(vp
, &auio
, td
->td_ucred
);
2776 *res
= count
- auio
.uio_resid
;
2781 * readlink_args(char *path, char *buf, int count)
2783 * Return target name of a symbolic link.
2788 sys_readlink(struct readlink_args
*uap
)
2790 struct nlookupdata nd
;
2794 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2796 error
= kern_readlink(&nd
, uap
->buf
, uap
->count
,
2797 &uap
->sysmsg_result
);
2805 setfflags(struct vnode
*vp
, int flags
)
2807 struct thread
*td
= curthread
;
2812 * Prevent non-root users from setting flags on devices. When
2813 * a device is reused, users can retain ownership of the device
2814 * if they are allowed to set flags and programs assume that
2815 * chown can't fail when done as root.
2817 if ((vp
->v_type
== VCHR
|| vp
->v_type
== VBLK
) &&
2818 ((error
= priv_check_cred(td
->td_ucred
, PRIV_VFS_CHFLAGS_DEV
, 0)) != 0))
2822 * note: vget is required for any operation that might mod the vnode
2823 * so VINACTIVE is properly cleared.
2825 if ((error
= vget(vp
, LK_EXCLUSIVE
)) == 0) {
2827 vattr
.va_flags
= flags
;
2828 error
= VOP_SETATTR(vp
, &vattr
, td
->td_ucred
);
2835 * chflags(char *path, int flags)
2837 * Change flags of a file given a path name.
2842 sys_chflags(struct chflags_args
*uap
)
2844 struct nlookupdata nd
;
2850 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2852 error
= nlookup(&nd
);
2854 error
= ncp_writechk(&nd
.nl_nch
);
2856 error
= cache_vref(&nd
.nl_nch
, nd
.nl_cred
, &vp
);
2859 error
= setfflags(vp
, uap
->flags
);
2867 * lchflags(char *path, int flags)
2869 * Change flags of a file given a path name, but don't follow symlinks.
2874 sys_lchflags(struct lchflags_args
*uap
)
2876 struct nlookupdata nd
;
2882 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2884 error
= nlookup(&nd
);
2886 error
= ncp_writechk(&nd
.nl_nch
);
2888 error
= cache_vref(&nd
.nl_nch
, nd
.nl_cred
, &vp
);
2891 error
= setfflags(vp
, uap
->flags
);
2899 * fchflags_args(int fd, int flags)
2901 * Change flags of a file given a file descriptor.
2906 sys_fchflags(struct fchflags_args
*uap
)
2908 struct thread
*td
= curthread
;
2909 struct proc
*p
= td
->td_proc
;
2913 if ((error
= holdvnode(p
->p_fd
, uap
->fd
, &fp
)) != 0)
2916 if (fp
->f_nchandle
.ncp
)
2917 error
= ncp_writechk(&fp
->f_nchandle
);
2919 error
= setfflags((struct vnode
*) fp
->f_data
, uap
->flags
);
2926 setfmode(struct vnode
*vp
, int mode
)
2928 struct thread
*td
= curthread
;
2933 * note: vget is required for any operation that might mod the vnode
2934 * so VINACTIVE is properly cleared.
2936 if ((error
= vget(vp
, LK_EXCLUSIVE
)) == 0) {
2938 vattr
.va_mode
= mode
& ALLPERMS
;
2939 error
= VOP_SETATTR(vp
, &vattr
, td
->td_ucred
);
2946 kern_chmod(struct nlookupdata
*nd
, int mode
)
2951 if ((error
= nlookup(nd
)) != 0)
2953 if ((error
= cache_vref(&nd
->nl_nch
, nd
->nl_cred
, &vp
)) != 0)
2955 if ((error
= ncp_writechk(&nd
->nl_nch
)) == 0)
2956 error
= setfmode(vp
, mode
);
2962 * chmod_args(char *path, int mode)
2964 * Change mode of a file given path name.
2969 sys_chmod(struct chmod_args
*uap
)
2971 struct nlookupdata nd
;
2975 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
2977 error
= kern_chmod(&nd
, uap
->mode
);
2984 * lchmod_args(char *path, int mode)
2986 * Change mode of a file given path name (don't follow links.)
2991 sys_lchmod(struct lchmod_args
*uap
)
2993 struct nlookupdata nd
;
2997 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
2999 error
= kern_chmod(&nd
, uap
->mode
);
3006 * fchmod_args(int fd, int mode)
3008 * Change mode of a file given a file descriptor.
3013 sys_fchmod(struct fchmod_args
*uap
)
3015 struct thread
*td
= curthread
;
3016 struct proc
*p
= td
->td_proc
;
3020 if ((error
= holdvnode(p
->p_fd
, uap
->fd
, &fp
)) != 0)
3023 if (fp
->f_nchandle
.ncp
)
3024 error
= ncp_writechk(&fp
->f_nchandle
);
3026 error
= setfmode((struct vnode
*)fp
->f_data
, uap
->mode
);
3033 * fchmodat_args(char *path, int mode)
3035 * Change mode of a file pointed to by fd/path.
3040 sys_fchmodat(struct fchmodat_args
*uap
)
3042 struct nlookupdata nd
;
3047 if (uap
->flags
& ~AT_SYMLINK_NOFOLLOW
)
3049 flags
= (uap
->flags
& AT_SYMLINK_NOFOLLOW
) ? 0 : NLC_FOLLOW
;
3052 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
,
3053 UIO_USERSPACE
, flags
);
3055 error
= kern_chmod(&nd
, uap
->mode
);
3056 nlookup_done_at(&nd
, fp
);
3062 setfown(struct vnode
*vp
, uid_t uid
, gid_t gid
)
3064 struct thread
*td
= curthread
;
3069 * note: vget is required for any operation that might mod the vnode
3070 * so VINACTIVE is properly cleared.
3072 if ((error
= vget(vp
, LK_EXCLUSIVE
)) == 0) {
3076 error
= VOP_SETATTR(vp
, &vattr
, td
->td_ucred
);
3083 kern_chown(struct nlookupdata
*nd
, int uid
, int gid
)
3088 if ((error
= nlookup(nd
)) != 0)
3090 if ((error
= cache_vref(&nd
->nl_nch
, nd
->nl_cred
, &vp
)) != 0)
3092 if ((error
= ncp_writechk(&nd
->nl_nch
)) == 0)
3093 error
= setfown(vp
, uid
, gid
);
3099 * chown(char *path, int uid, int gid)
3101 * Set ownership given a path name.
3106 sys_chown(struct chown_args
*uap
)
3108 struct nlookupdata nd
;
3112 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3114 error
= kern_chown(&nd
, uap
->uid
, uap
->gid
);
3121 * lchown_args(char *path, int uid, int gid)
3123 * Set ownership given a path name, do not cross symlinks.
3128 sys_lchown(struct lchown_args
*uap
)
3130 struct nlookupdata nd
;
3134 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
3136 error
= kern_chown(&nd
, uap
->uid
, uap
->gid
);
3143 * fchown_args(int fd, int uid, int gid)
3145 * Set ownership given a file descriptor.
3150 sys_fchown(struct fchown_args
*uap
)
3152 struct thread
*td
= curthread
;
3153 struct proc
*p
= td
->td_proc
;
3157 if ((error
= holdvnode(p
->p_fd
, uap
->fd
, &fp
)) != 0)
3160 if (fp
->f_nchandle
.ncp
)
3161 error
= ncp_writechk(&fp
->f_nchandle
);
3163 error
= setfown((struct vnode
*)fp
->f_data
, uap
->uid
, uap
->gid
);
3170 * fchownat(int fd, char *path, int uid, int gid, int flags)
3172 * Set ownership of file pointed to by fd/path.
3177 sys_fchownat(struct fchownat_args
*uap
)
3179 struct nlookupdata nd
;
3184 if (uap
->flags
& ~AT_SYMLINK_NOFOLLOW
)
3186 flags
= (uap
->flags
& AT_SYMLINK_NOFOLLOW
) ? 0 : NLC_FOLLOW
;
3189 error
= nlookup_init_at(&nd
, &fp
, uap
->fd
, uap
->path
,
3190 UIO_USERSPACE
, flags
);
3192 error
= kern_chown(&nd
, uap
->uid
, uap
->gid
);
3193 nlookup_done_at(&nd
, fp
);
3200 getutimes(const struct timeval
*tvp
, struct timespec
*tsp
)
3202 struct timeval tv
[2];
3206 TIMEVAL_TO_TIMESPEC(&tv
[0], &tsp
[0]);
3209 TIMEVAL_TO_TIMESPEC(&tvp
[0], &tsp
[0]);
3210 TIMEVAL_TO_TIMESPEC(&tvp
[1], &tsp
[1]);
3216 setutimes(struct vnode
*vp
, struct vattr
*vattr
,
3217 const struct timespec
*ts
, int nullflag
)
3219 struct thread
*td
= curthread
;
3223 vattr
->va_atime
= ts
[0];
3224 vattr
->va_mtime
= ts
[1];
3226 vattr
->va_vaflags
|= VA_UTIMES_NULL
;
3227 error
= VOP_SETATTR(vp
, vattr
, td
->td_ucred
);
3233 kern_utimes(struct nlookupdata
*nd
, struct timeval
*tptr
)
3235 struct timespec ts
[2];
3240 if ((error
= getutimes(tptr
, ts
)) != 0)
3244 * NOTE: utimes() succeeds for the owner even if the file
3245 * is not user-writable.
3247 nd
->nl_flags
|= NLC_OWN
| NLC_WRITE
;
3249 if ((error
= nlookup(nd
)) != 0)
3251 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
3253 if ((error
= cache_vref(&nd
->nl_nch
, nd
->nl_cred
, &vp
)) != 0)
3257 * note: vget is required for any operation that might mod the vnode
3258 * so VINACTIVE is properly cleared.
3260 if ((error
= vn_writechk(vp
, &nd
->nl_nch
)) == 0) {
3261 error
= vget(vp
, LK_EXCLUSIVE
);
3263 error
= setutimes(vp
, &vattr
, ts
, (tptr
== NULL
));
3272 * utimes_args(char *path, struct timeval *tptr)
3274 * Set the access and modification times of a file.
3279 sys_utimes(struct utimes_args
*uap
)
3281 struct timeval tv
[2];
3282 struct nlookupdata nd
;
3286 error
= copyin(uap
->tptr
, tv
, sizeof(tv
));
3291 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3293 error
= kern_utimes(&nd
, uap
->tptr
? tv
: NULL
);
3300 * lutimes_args(char *path, struct timeval *tptr)
3302 * Set the access and modification times of a file.
3307 sys_lutimes(struct lutimes_args
*uap
)
3309 struct timeval tv
[2];
3310 struct nlookupdata nd
;
3314 error
= copyin(uap
->tptr
, tv
, sizeof(tv
));
3319 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
3321 error
= kern_utimes(&nd
, uap
->tptr
? tv
: NULL
);
3328 * Set utimes on a file descriptor. The creds used to open the
3329 * file are used to determine whether the operation is allowed
3333 kern_futimes(int fd
, struct timeval
*tptr
)
3335 struct thread
*td
= curthread
;
3336 struct proc
*p
= td
->td_proc
;
3337 struct timespec ts
[2];
3343 error
= getutimes(tptr
, ts
);
3346 if ((error
= holdvnode(p
->p_fd
, fd
, &fp
)) != 0)
3348 if (fp
->f_nchandle
.ncp
)
3349 error
= ncp_writechk(&fp
->f_nchandle
);
3352 error
= vget(vp
, LK_EXCLUSIVE
);
3354 error
= VOP_GETATTR(vp
, &vattr
);
3356 error
= naccess_va(&vattr
, NLC_OWN
| NLC_WRITE
,
3360 error
= setutimes(vp
, &vattr
, ts
,
3371 * futimes_args(int fd, struct timeval *tptr)
3373 * Set the access and modification times of a file.
3378 sys_futimes(struct futimes_args
*uap
)
3380 struct timeval tv
[2];
3384 error
= copyin(uap
->tptr
, tv
, sizeof(tv
));
3389 error
= kern_futimes(uap
->fd
, uap
->tptr
? tv
: NULL
);
3396 kern_truncate(struct nlookupdata
*nd
, off_t length
)
3404 nd
->nl_flags
|= NLC_WRITE
| NLC_TRUNCATE
;
3405 if ((error
= nlookup(nd
)) != 0)
3407 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
3409 if ((error
= cache_vref(&nd
->nl_nch
, nd
->nl_cred
, &vp
)) != 0)
3411 if ((error
= vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
)) != 0) {
3415 if (vp
->v_type
== VDIR
) {
3417 } else if ((error
= vn_writechk(vp
, &nd
->nl_nch
)) == 0) {
3419 vattr
.va_size
= length
;
3420 error
= VOP_SETATTR(vp
, &vattr
, nd
->nl_cred
);
3427 * truncate(char *path, int pad, off_t length)
3429 * Truncate a file given its path name.
3434 sys_truncate(struct truncate_args
*uap
)
3436 struct nlookupdata nd
;
3440 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3442 error
= kern_truncate(&nd
, uap
->length
);
3449 kern_ftruncate(int fd
, off_t length
)
3451 struct thread
*td
= curthread
;
3452 struct proc
*p
= td
->td_proc
;
3460 if ((error
= holdvnode(p
->p_fd
, fd
, &fp
)) != 0)
3462 if (fp
->f_nchandle
.ncp
) {
3463 error
= ncp_writechk(&fp
->f_nchandle
);
3467 if ((fp
->f_flag
& FWRITE
) == 0) {
3471 if (fp
->f_flag
& FAPPENDONLY
) { /* inode was set s/uapnd */
3475 vp
= (struct vnode
*)fp
->f_data
;
3476 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
3477 if (vp
->v_type
== VDIR
) {
3479 } else if ((error
= vn_writechk(vp
, NULL
)) == 0) {
3481 vattr
.va_size
= length
;
3482 error
= VOP_SETATTR(vp
, &vattr
, fp
->f_cred
);
3491 * ftruncate_args(int fd, int pad, off_t length)
3493 * Truncate a file given a file descriptor.
3498 sys_ftruncate(struct ftruncate_args
*uap
)
3503 error
= kern_ftruncate(uap
->fd
, uap
->length
);
3512 * Sync an open file.
3517 sys_fsync(struct fsync_args
*uap
)
3519 struct thread
*td
= curthread
;
3520 struct proc
*p
= td
->td_proc
;
3526 if ((error
= holdvnode(p
->p_fd
, uap
->fd
, &fp
)) != 0)
3529 vp
= (struct vnode
*)fp
->f_data
;
3530 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
3531 if ((obj
= vp
->v_object
) != NULL
)
3532 vm_object_page_clean(obj
, 0, 0, 0);
3533 error
= VOP_FSYNC(vp
, MNT_WAIT
, VOP_FSYNC_SYSCALL
);
3534 if (error
== 0 && vp
->v_mount
)
3535 error
= buf_fsync(vp
);
3544 kern_rename(struct nlookupdata
*fromnd
, struct nlookupdata
*tond
)
3546 struct nchandle fnchd
;
3547 struct nchandle tnchd
;
3548 struct namecache
*ncp
;
3555 fromnd
->nl_flags
|= NLC_REFDVP
| NLC_RENAME_SRC
;
3556 if ((error
= nlookup(fromnd
)) != 0)
3558 if ((fnchd
.ncp
= fromnd
->nl_nch
.ncp
->nc_parent
) == NULL
)
3560 fnchd
.mount
= fromnd
->nl_nch
.mount
;
3564 * unlock the source nch so we can lookup the target nch without
3565 * deadlocking. The target may or may not exist so we do not check
3566 * for a target vp like kern_mkdir() and other creation functions do.
3568 * The source and target directories are ref'd and rechecked after
3569 * everything is relocked to determine if the source or target file
3572 KKASSERT(fromnd
->nl_flags
& NLC_NCPISLOCKED
);
3573 fromnd
->nl_flags
&= ~NLC_NCPISLOCKED
;
3574 cache_unlock(&fromnd
->nl_nch
);
3576 tond
->nl_flags
|= NLC_RENAME_DST
| NLC_REFDVP
;
3577 if ((error
= nlookup(tond
)) != 0) {
3581 if ((tnchd
.ncp
= tond
->nl_nch
.ncp
->nc_parent
) == NULL
) {
3585 tnchd
.mount
= tond
->nl_nch
.mount
;
3589 * If the source and target are the same there is nothing to do
3591 if (fromnd
->nl_nch
.ncp
== tond
->nl_nch
.ncp
) {
3598 * Mount points cannot be renamed or overwritten
3600 if ((fromnd
->nl_nch
.ncp
->nc_flag
| tond
->nl_nch
.ncp
->nc_flag
) &
3609 * Relock the source ncp. cache_relock() will deal with any
3610 * deadlocks against the already-locked tond and will also
3611 * make sure both are resolved.
3613 * NOTE AFTER RELOCKING: The source or target ncp may have become
3614 * invalid while they were unlocked, nc_vp and nc_mount could
3617 cache_relock(&fromnd
->nl_nch
, fromnd
->nl_cred
,
3618 &tond
->nl_nch
, tond
->nl_cred
);
3619 fromnd
->nl_flags
|= NLC_NCPISLOCKED
;
3622 * make sure the parent directories linkages are the same
3624 if (fnchd
.ncp
!= fromnd
->nl_nch
.ncp
->nc_parent
||
3625 tnchd
.ncp
!= tond
->nl_nch
.ncp
->nc_parent
) {
3632 * Both the source and target must be within the same filesystem and
3633 * in the same filesystem as their parent directories within the
3634 * namecache topology.
3636 * NOTE: fromnd's nc_mount or nc_vp could be NULL.
3639 if (mp
!= tnchd
.mount
|| mp
!= fromnd
->nl_nch
.mount
||
3640 mp
!= tond
->nl_nch
.mount
) {
3647 * Make sure the mount point is writable
3649 if ((error
= ncp_writechk(&tond
->nl_nch
)) != 0) {
3656 * If the target exists and either the source or target is a directory,
3657 * then both must be directories.
3659 * Due to relocking of the source, fromnd->nl_nch.ncp->nc_vp might h
3662 if (tond
->nl_nch
.ncp
->nc_vp
) {
3663 if (fromnd
->nl_nch
.ncp
->nc_vp
== NULL
) {
3665 } else if (fromnd
->nl_nch
.ncp
->nc_vp
->v_type
== VDIR
) {
3666 if (tond
->nl_nch
.ncp
->nc_vp
->v_type
!= VDIR
)
3668 } else if (tond
->nl_nch
.ncp
->nc_vp
->v_type
== VDIR
) {
3674 * You cannot rename a source into itself or a subdirectory of itself.
3675 * We check this by travsersing the target directory upwards looking
3676 * for a match against the source.
3681 for (ncp
= tnchd
.ncp
; ncp
; ncp
= ncp
->nc_parent
) {
3682 if (fromnd
->nl_nch
.ncp
== ncp
) {
3693 * Even though the namespaces are different, they may still represent
3694 * hardlinks to the same file. The filesystem might have a hard time
3695 * with this so we issue a NREMOVE of the source instead of a NRENAME
3696 * when we detect the situation.
3699 fdvp
= fromnd
->nl_dvp
;
3700 tdvp
= tond
->nl_dvp
;
3701 if (fdvp
== NULL
|| tdvp
== NULL
) {
3703 } else if (fromnd
->nl_nch
.ncp
->nc_vp
== tond
->nl_nch
.ncp
->nc_vp
) {
3704 error
= VOP_NREMOVE(&fromnd
->nl_nch
, fdvp
,
3707 error
= VOP_NRENAME(&fromnd
->nl_nch
, &tond
->nl_nch
,
3708 fdvp
, tdvp
, tond
->nl_cred
);
3715 * rename_args(char *from, char *to)
3717 * Rename files. Source and destination must either both be directories,
3718 * or both not be directories. If target is a directory, it must be empty.
3723 sys_rename(struct rename_args
*uap
)
3725 struct nlookupdata fromnd
, tond
;
3729 error
= nlookup_init(&fromnd
, uap
->from
, UIO_USERSPACE
, 0);
3731 error
= nlookup_init(&tond
, uap
->to
, UIO_USERSPACE
, 0);
3733 error
= kern_rename(&fromnd
, &tond
);
3734 nlookup_done(&tond
);
3736 nlookup_done(&fromnd
);
3742 kern_mkdir(struct nlookupdata
*nd
, int mode
)
3744 struct thread
*td
= curthread
;
3745 struct proc
*p
= td
->td_proc
;
3751 nd
->nl_flags
|= NLC_WILLBEDIR
| NLC_CREATE
| NLC_REFDVP
;
3752 if ((error
= nlookup(nd
)) != 0)
3755 if (nd
->nl_nch
.ncp
->nc_vp
)
3757 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
3760 vattr
.va_type
= VDIR
;
3761 vattr
.va_mode
= (mode
& ACCESSPERMS
) &~ p
->p_fd
->fd_cmask
;
3764 error
= VOP_NMKDIR(&nd
->nl_nch
, nd
->nl_dvp
, &vp
, td
->td_ucred
, &vattr
);
3771 * mkdir_args(char *path, int mode)
3773 * Make a directory file.
3778 sys_mkdir(struct mkdir_args
*uap
)
3780 struct nlookupdata nd
;
3784 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
3786 error
= kern_mkdir(&nd
, uap
->mode
);
3793 kern_rmdir(struct nlookupdata
*nd
)
3798 nd
->nl_flags
|= NLC_DELETE
| NLC_REFDVP
;
3799 if ((error
= nlookup(nd
)) != 0)
3803 * Do not allow directories representing mount points to be
3804 * deleted, even if empty. Check write perms on mount point
3805 * in case the vnode is aliased (aka nullfs).
3807 if (nd
->nl_nch
.ncp
->nc_flag
& (NCF_ISMOUNTPT
))
3809 if ((error
= ncp_writechk(&nd
->nl_nch
)) != 0)
3811 error
= VOP_NRMDIR(&nd
->nl_nch
, nd
->nl_dvp
, nd
->nl_cred
);
3816 * rmdir_args(char *path)
3818 * Remove a directory file.
3823 sys_rmdir(struct rmdir_args
*uap
)
3825 struct nlookupdata nd
;
3829 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, 0);
3831 error
= kern_rmdir(&nd
);
3838 kern_getdirentries(int fd
, char *buf
, u_int count
, long *basep
, int *res
,
3839 enum uio_seg direction
)
3841 struct thread
*td
= curthread
;
3842 struct proc
*p
= td
->td_proc
;
3850 if ((error
= holdvnode(p
->p_fd
, fd
, &fp
)) != 0)
3852 if ((fp
->f_flag
& FREAD
) == 0) {
3856 vp
= (struct vnode
*)fp
->f_data
;
3858 if (vp
->v_type
!= VDIR
) {
3862 aiov
.iov_base
= buf
;
3863 aiov
.iov_len
= count
;
3864 auio
.uio_iov
= &aiov
;
3865 auio
.uio_iovcnt
= 1;
3866 auio
.uio_rw
= UIO_READ
;
3867 auio
.uio_segflg
= direction
;
3869 auio
.uio_resid
= count
;
3870 loff
= auio
.uio_offset
= fp
->f_offset
;
3871 error
= VOP_READDIR(vp
, &auio
, fp
->f_cred
, &eofflag
, NULL
, NULL
);
3872 fp
->f_offset
= auio
.uio_offset
;
3875 if (count
== auio
.uio_resid
) {
3876 if (union_dircheckp
) {
3877 error
= union_dircheckp(td
, &vp
, fp
);
3884 if ((vp
->v_flag
& VROOT
) &&
3885 (vp
->v_mount
->mnt_flag
& MNT_UNION
)) {
3886 struct vnode
*tvp
= vp
;
3887 vp
= vp
->v_mount
->mnt_vnodecovered
;
3898 * WARNING! *basep may not be wide enough to accomodate the
3899 * seek offset. XXX should we hack this to return the upper 32 bits
3900 * for offsets greater then 4G?
3903 *basep
= (long)loff
;
3905 *res
= count
- auio
.uio_resid
;
3912 * getdirentries_args(int fd, char *buf, u_int conut, long *basep)
3914 * Read a block of directory entries in a file system independent format.
3919 sys_getdirentries(struct getdirentries_args
*uap
)
3925 error
= kern_getdirentries(uap
->fd
, uap
->buf
, uap
->count
, &base
,
3926 &uap
->sysmsg_result
, UIO_USERSPACE
);
3929 if (error
== 0 && uap
->basep
)
3930 error
= copyout(&base
, uap
->basep
, sizeof(*uap
->basep
));
3935 * getdents_args(int fd, char *buf, size_t count)
3940 sys_getdents(struct getdents_args
*uap
)
3945 error
= kern_getdirentries(uap
->fd
, uap
->buf
, uap
->count
, NULL
,
3946 &uap
->sysmsg_result
, UIO_USERSPACE
);
3953 * Set the mode mask for creation of filesystem nodes.
3955 * umask(int newmask)
3960 sys_umask(struct umask_args
*uap
)
3962 struct thread
*td
= curthread
;
3963 struct proc
*p
= td
->td_proc
;
3964 struct filedesc
*fdp
;
3967 uap
->sysmsg_result
= fdp
->fd_cmask
;
3968 fdp
->fd_cmask
= uap
->newmask
& ALLPERMS
;
3973 * revoke(char *path)
3975 * Void all references to file by ripping underlying filesystem
3981 sys_revoke(struct revoke_args
*uap
)
3983 struct nlookupdata nd
;
3991 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
3993 error
= nlookup(&nd
);
3995 error
= cache_vref(&nd
.nl_nch
, nd
.nl_cred
, &vp
);
3996 cred
= crhold(nd
.nl_cred
);
4000 error
= VOP_GETATTR(vp
, &vattr
);
4001 if (error
== 0 && cred
->cr_uid
!= vattr
.va_uid
)
4002 error
= priv_check_cred(cred
, PRIV_VFS_REVOKE
, 0);
4003 if (error
== 0 && (vp
->v_type
== VCHR
|| vp
->v_type
== VBLK
)) {
4005 error
= vrevoke(vp
, cred
);
4006 } else if (error
== 0) {
4007 error
= vrevoke(vp
, cred
);
4018 * getfh_args(char *fname, fhandle_t *fhp)
4020 * Get (NFS) file handle
4022 * NOTE: We use the fsid of the covering mount, even if it is a nullfs
4023 * mount. This allows nullfs mounts to be explicitly exported.
4025 * WARNING: nullfs mounts of HAMMER PFS ROOTs are safe.
4027 * nullfs mounts of subdirectories are not safe. That is, it will
4028 * work, but you do not really have protection against access to
4029 * the related parent directories.
4034 sys_getfh(struct getfh_args
*uap
)
4036 struct thread
*td
= curthread
;
4037 struct nlookupdata nd
;
4044 * Must be super user
4046 if ((error
= priv_check(td
, PRIV_ROOT
)) != 0)
4051 error
= nlookup_init(&nd
, uap
->fname
, UIO_USERSPACE
, NLC_FOLLOW
);
4053 error
= nlookup(&nd
);
4055 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
4056 mp
= nd
.nl_nch
.mount
;
4059 bzero(&fh
, sizeof(fh
));
4060 fh
.fh_fsid
= mp
->mnt_stat
.f_fsid
;
4061 error
= VFS_VPTOFH(vp
, &fh
.fh_fid
);
4064 error
= copyout(&fh
, uap
->fhp
, sizeof(fh
));
4071 * fhopen_args(const struct fhandle *u_fhp, int flags)
4073 * syscall for the rpc.lockd to use to translate a NFS file handle into
4074 * an open descriptor.
4076 * warning: do not remove the priv_check() call or this becomes one giant
4082 sys_fhopen(struct fhopen_args
*uap
)
4084 struct thread
*td
= curthread
;
4085 struct filedesc
*fdp
= td
->td_proc
->p_fd
;
4090 struct vattr
*vap
= &vat
;
4092 int fmode
, mode
, error
, type
;
4098 * Must be super user
4100 error
= priv_check(td
, PRIV_ROOT
);
4104 fmode
= FFLAGS(uap
->flags
);
4107 * Why not allow a non-read/write open for our lockd?
4109 if (((fmode
& (FREAD
| FWRITE
)) == 0) || (fmode
& O_CREAT
))
4111 error
= copyin(uap
->u_fhp
, &fhp
, sizeof(fhp
));
4116 * Find the mount point
4119 mp
= vfs_getvfs(&fhp
.fh_fsid
);
4124 /* now give me my vnode, it gets returned to me locked */
4125 error
= VFS_FHTOVP(mp
, NULL
, &fhp
.fh_fid
, &vp
);
4129 * from now on we have to make sure not
4130 * to forget about the vnode
4131 * any error that causes an abort must vput(vp)
4132 * just set error = err and 'goto bad;'.
4138 if (vp
->v_type
== VLNK
) {
4142 if (vp
->v_type
== VSOCK
) {
4147 if (fmode
& (FWRITE
| O_TRUNC
)) {
4148 if (vp
->v_type
== VDIR
) {
4152 error
= vn_writechk(vp
, NULL
);
4160 error
= VOP_ACCESS(vp
, mode
, td
->td_ucred
);
4164 if (fmode
& O_TRUNC
) {
4165 vn_unlock(vp
); /* XXX */
4166 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
); /* XXX */
4169 error
= VOP_SETATTR(vp
, vap
, td
->td_ucred
);
4175 * VOP_OPEN needs the file pointer so it can potentially override
4178 * WARNING! no f_nchandle will be associated when fhopen()ing a
4181 if ((error
= falloc(td
->td_lwp
, &nfp
, &indx
)) != 0)
4185 error
= VOP_OPEN(vp
, fmode
, td
->td_ucred
, fp
);
4188 * setting f_ops this way prevents VOP_CLOSE from being
4189 * called or fdrop() releasing the vp from v_data. Since
4190 * the VOP_OPEN failed we don't want to VOP_CLOSE.
4192 fp
->f_ops
= &badfileops
;
4198 * The fp is given its own reference, we still have our ref and lock.
4200 * Assert that all regular files must be created with a VM object.
4202 if (vp
->v_type
== VREG
&& vp
->v_object
== NULL
) {
4203 kprintf("fhopen: regular file did not have VM object: %p\n", vp
);
4208 * The open was successful. Handle any locking requirements.
4210 if (fmode
& (O_EXLOCK
| O_SHLOCK
)) {
4211 lf
.l_whence
= SEEK_SET
;
4214 if (fmode
& O_EXLOCK
)
4215 lf
.l_type
= F_WRLCK
;
4217 lf
.l_type
= F_RDLCK
;
4218 if (fmode
& FNONBLOCK
)
4223 if ((error
= VOP_ADVLOCK(vp
, (caddr_t
)fp
, F_SETLK
, &lf
, type
)) != 0) {
4225 * release our private reference.
4227 fsetfd(fdp
, NULL
, indx
);
4232 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
4233 fp
->f_flag
|= FHASLOCK
;
4237 * Clean up. Associate the file pointer with the previously
4238 * reserved descriptor and return it.
4242 fsetfd(fdp
, fp
, indx
);
4244 uap
->sysmsg_result
= indx
;
4248 fsetfd(fdp
, NULL
, indx
);
4258 * fhstat_args(struct fhandle *u_fhp, struct stat *sb)
4263 sys_fhstat(struct fhstat_args
*uap
)
4265 struct thread
*td
= curthread
;
4273 * Must be super user
4275 error
= priv_check(td
, PRIV_ROOT
);
4279 error
= copyin(uap
->u_fhp
, &fh
, sizeof(fhandle_t
));
4284 if ((mp
= vfs_getvfs(&fh
.fh_fsid
)) == NULL
)
4287 if ((error
= VFS_FHTOVP(mp
, NULL
, &fh
.fh_fid
, &vp
)) == 0) {
4288 error
= vn_stat(vp
, &sb
, td
->td_ucred
);
4294 error
= copyout(&sb
, uap
->sb
, sizeof(sb
));
4299 * fhstatfs_args(struct fhandle *u_fhp, struct statfs *buf)
4304 sys_fhstatfs(struct fhstatfs_args
*uap
)
4306 struct thread
*td
= curthread
;
4307 struct proc
*p
= td
->td_proc
;
4312 char *fullpath
, *freepath
;
4317 * Must be super user
4319 if ((error
= priv_check(td
, PRIV_ROOT
)))
4322 if ((error
= copyin(uap
->u_fhp
, &fh
, sizeof(fhandle_t
))) != 0)
4327 if ((mp
= vfs_getvfs(&fh
.fh_fsid
)) == NULL
) {
4331 if (p
!= NULL
&& !chroot_visible_mnt(mp
, p
)) {
4336 if ((error
= VFS_FHTOVP(mp
, NULL
, &fh
.fh_fid
, &vp
)) != 0)
4341 if ((error
= VFS_STATFS(mp
, sp
, td
->td_ucred
)) != 0)
4344 error
= mount_path(p
, mp
, &fullpath
, &freepath
);
4347 bzero(sp
->f_mntonname
, sizeof(sp
->f_mntonname
));
4348 strlcpy(sp
->f_mntonname
, fullpath
, sizeof(sp
->f_mntonname
));
4349 kfree(freepath
, M_TEMP
);
4351 sp
->f_flags
= mp
->mnt_flag
& MNT_VISFLAGMASK
;
4352 if (priv_check(td
, PRIV_ROOT
)) {
4353 bcopy(sp
, &sb
, sizeof(sb
));
4354 sb
.f_fsid
.val
[0] = sb
.f_fsid
.val
[1] = 0;
4357 error
= copyout(sp
, uap
->buf
, sizeof(*sp
));
4364 * fhstatvfs_args(struct fhandle *u_fhp, struct statvfs *buf)
4369 sys_fhstatvfs(struct fhstatvfs_args
*uap
)
4371 struct thread
*td
= curthread
;
4372 struct proc
*p
= td
->td_proc
;
4380 * Must be super user
4382 if ((error
= priv_check(td
, PRIV_ROOT
)))
4385 if ((error
= copyin(uap
->u_fhp
, &fh
, sizeof(fhandle_t
))) != 0)
4390 if ((mp
= vfs_getvfs(&fh
.fh_fsid
)) == NULL
) {
4394 if (p
!= NULL
&& !chroot_visible_mnt(mp
, p
)) {
4399 if ((error
= VFS_FHTOVP(mp
, NULL
, &fh
.fh_fid
, &vp
)))
4402 sp
= &mp
->mnt_vstat
;
4404 if ((error
= VFS_STATVFS(mp
, sp
, td
->td_ucred
)) != 0)
4408 if (mp
->mnt_flag
& MNT_RDONLY
)
4409 sp
->f_flag
|= ST_RDONLY
;
4410 if (mp
->mnt_flag
& MNT_NOSUID
)
4411 sp
->f_flag
|= ST_NOSUID
;
4412 error
= copyout(sp
, uap
->buf
, sizeof(*sp
));
4420 * Syscall to push extended attribute configuration information into the
4421 * VFS. Accepts a path, which it converts to a mountpoint, as well as
4422 * a command (int cmd), and attribute name and misc data. For now, the
4423 * attribute name is left in userspace for consumption by the VFS_op.
4424 * It will probably be changed to be copied into sysspace by the
4425 * syscall in the future, once issues with various consumers of the
4426 * attribute code have raised their hands.
4428 * Currently this is used only by UFS Extended Attributes.
4433 sys_extattrctl(struct extattrctl_args
*uap
)
4435 struct nlookupdata nd
;
4442 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
4444 error
= nlookup(&nd
);
4446 mp
= nd
.nl_nch
.mount
;
4447 error
= VFS_EXTATTRCTL(mp
, uap
->cmd
,
4448 uap
->attrname
, uap
->arg
,
4458 * Syscall to set a named extended attribute on a file or directory.
4459 * Accepts attribute name, and a uio structure pointing to the data to set.
4460 * The uio is consumed in the style of writev(). The real work happens
4461 * in VOP_SETEXTATTR().
4466 sys_extattr_set_file(struct extattr_set_file_args
*uap
)
4468 char attrname
[EXTATTR_MAXNAMELEN
];
4469 struct iovec aiov
[UIO_SMALLIOV
];
4470 struct iovec
*needfree
;
4471 struct nlookupdata nd
;
4480 error
= copyin(uap
->attrname
, attrname
, EXTATTR_MAXNAMELEN
);
4486 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
4488 error
= nlookup(&nd
);
4490 error
= ncp_writechk(&nd
.nl_nch
);
4492 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
4500 iovlen
= uap
->iovcnt
* sizeof(struct iovec
);
4501 if (uap
->iovcnt
> UIO_SMALLIOV
) {
4502 if (uap
->iovcnt
> UIO_MAXIOV
) {
4506 MALLOC(iov
, struct iovec
*, iovlen
, M_IOV
, M_WAITOK
);
4512 auio
.uio_iovcnt
= uap
->iovcnt
;
4513 auio
.uio_rw
= UIO_WRITE
;
4514 auio
.uio_segflg
= UIO_USERSPACE
;
4515 auio
.uio_td
= nd
.nl_td
;
4516 auio
.uio_offset
= 0;
4517 if ((error
= copyin(uap
->iovp
, iov
, iovlen
)))
4520 for (i
= 0; i
< uap
->iovcnt
; i
++) {
4521 if (iov
->iov_len
> LONG_MAX
- auio
.uio_resid
) {
4525 auio
.uio_resid
+= iov
->iov_len
;
4528 cnt
= auio
.uio_resid
;
4529 error
= VOP_SETEXTATTR(vp
, attrname
, &auio
, nd
.nl_cred
);
4530 cnt
-= auio
.uio_resid
;
4531 uap
->sysmsg_result
= cnt
;
4537 FREE(needfree
, M_IOV
);
4542 * Syscall to get a named extended attribute on a file or directory.
4543 * Accepts attribute name, and a uio structure pointing to a buffer for the
4544 * data. The uio is consumed in the style of readv(). The real work
4545 * happens in VOP_GETEXTATTR();
4550 sys_extattr_get_file(struct extattr_get_file_args
*uap
)
4552 char attrname
[EXTATTR_MAXNAMELEN
];
4553 struct iovec aiov
[UIO_SMALLIOV
];
4554 struct iovec
*needfree
;
4555 struct nlookupdata nd
;
4564 error
= copyin(uap
->attrname
, attrname
, EXTATTR_MAXNAMELEN
);
4570 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
4572 error
= nlookup(&nd
);
4574 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
4581 iovlen
= uap
->iovcnt
* sizeof (struct iovec
);
4583 if (uap
->iovcnt
> UIO_SMALLIOV
) {
4584 if (uap
->iovcnt
> UIO_MAXIOV
) {
4588 MALLOC(iov
, struct iovec
*, iovlen
, M_IOV
, M_WAITOK
);
4594 auio
.uio_iovcnt
= uap
->iovcnt
;
4595 auio
.uio_rw
= UIO_READ
;
4596 auio
.uio_segflg
= UIO_USERSPACE
;
4597 auio
.uio_td
= nd
.nl_td
;
4598 auio
.uio_offset
= 0;
4599 if ((error
= copyin(uap
->iovp
, iov
, iovlen
)))
4602 for (i
= 0; i
< uap
->iovcnt
; i
++) {
4603 if (iov
->iov_len
> LONG_MAX
- auio
.uio_resid
) {
4607 auio
.uio_resid
+= iov
->iov_len
;
4610 cnt
= auio
.uio_resid
;
4611 error
= VOP_GETEXTATTR(vp
, attrname
, &auio
, nd
.nl_cred
);
4612 cnt
-= auio
.uio_resid
;
4613 uap
->sysmsg_result
= cnt
;
4619 FREE(needfree
, M_IOV
);
4624 * Syscall to delete a named extended attribute from a file or directory.
4625 * Accepts attribute name. The real work happens in VOP_SETEXTATTR().
4630 sys_extattr_delete_file(struct extattr_delete_file_args
*uap
)
4632 char attrname
[EXTATTR_MAXNAMELEN
];
4633 struct nlookupdata nd
;
4637 error
= copyin(uap
->attrname
, attrname
, EXTATTR_MAXNAMELEN
);
4642 error
= nlookup_init(&nd
, uap
->path
, UIO_USERSPACE
, NLC_FOLLOW
);
4644 error
= nlookup(&nd
);
4646 error
= ncp_writechk(&nd
.nl_nch
);
4648 error
= cache_vget(&nd
.nl_nch
, nd
.nl_cred
, LK_EXCLUSIVE
, &vp
);
4650 error
= VOP_SETEXTATTR(vp
, attrname
, NULL
, nd
.nl_cred
);
4660 * Determine if the mount is visible to the process.
4663 chroot_visible_mnt(struct mount
*mp
, struct proc
*p
)
4665 struct nchandle nch
;
4668 * Traverse from the mount point upwards. If we hit the process
4669 * root then the mount point is visible to the process.
4671 nch
= mp
->mnt_ncmountpt
;
4673 if (nch
.mount
== p
->p_fd
->fd_nrdir
.mount
&&
4674 nch
.ncp
== p
->p_fd
->fd_nrdir
.ncp
) {
4677 if (nch
.ncp
== nch
.mount
->mnt_ncmountpt
.ncp
) {
4678 nch
= nch
.mount
->mnt_ncmounton
;
4680 nch
.ncp
= nch
.ncp
->nc_parent
;
4685 * If the mount point is not visible to the process, but the
4686 * process root is in a subdirectory of the mount, return
4689 if (p
->p_fd
->fd_nrdir
.mount
== mp
)