| Commit | Line | Data |
|---|---|---|
| 984263bc MD |
1 | /* |
| 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. | |
| 9 | * | |
| 10 | * Redistribution and use in source and binary forms, with or without | |
| 11 | * modification, are permitted provided that the following conditions | |
| 12 | * are met: | |
| 13 | * 1. Redistributions of source code must retain the above copyright | |
| 14 | * notice, this list of conditions and the following disclaimer. | |
| 15 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 16 | * notice, this list of conditions and the following disclaimer in the | |
| 17 | * documentation and/or other materials provided with the distribution. | |
| 18 | * 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. | |
| 25 | * | |
| 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 | |
| 36 | * SUCH DAMAGE. | |
| 37 | * | |
| 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 $ | |
| 91fdc001 | 40 | * $DragonFly: src/sys/kern/vfs_syscalls.c,v 1.135 2008/11/11 00:55:49 pavalos Exp $ |
| 984263bc MD |
41 | */ |
| 42 | ||
| 984263bc MD |
43 | #include <sys/param.h> |
| 44 | #include <sys/systm.h> | |
| 45 | #include <sys/buf.h> | |
| 16df2c74 | 46 | #include <sys/conf.h> |
| 984263bc MD |
47 | #include <sys/sysent.h> |
| 48 | #include <sys/malloc.h> | |
| 49 | #include <sys/mount.h> | |
| 432b8263 | 50 | #include <sys/mountctl.h> |
| 984263bc | 51 | #include <sys/sysproto.h> |
| 984263bc MD |
52 | #include <sys/filedesc.h> |
| 53 | #include <sys/kernel.h> | |
| 54 | #include <sys/fcntl.h> | |
| 55 | #include <sys/file.h> | |
| 56 | #include <sys/linker.h> | |
| 57 | #include <sys/stat.h> | |
| 58 | #include <sys/unistd.h> | |
| 59 | #include <sys/vnode.h> | |
| 60 | #include <sys/proc.h> | |
| 895c1f85 | 61 | #include <sys/priv.h> |
| 9658f145 | 62 | #include <sys/jail.h> |
| dadab5e9 | 63 | #include <sys/namei.h> |
| 21739618 | 64 | #include <sys/nlookup.h> |
| 984263bc MD |
65 | #include <sys/dirent.h> |
| 66 | #include <sys/extattr.h> | |
| 8fa76237 | 67 | #include <sys/spinlock.h> |
| 8f6f8622 | 68 | #include <sys/kern_syscall.h> |
| 70aac194 | 69 | #include <sys/objcache.h> |
| 984263bc | 70 | #include <sys/sysctl.h> |
| 408357d8 MD |
71 | |
| 72 | #include <sys/buf2.h> | |
| 28623bf9 MD |
73 | #include <sys/file2.h> |
| 74 | #include <sys/spinlock2.h> | |
| 75 | ||
| 984263bc MD |
76 | #include <vm/vm.h> |
| 77 | #include <vm/vm_object.h> | |
| 984263bc MD |
78 | #include <vm/vm_page.h> |
| 79 | ||
| 28623bf9 MD |
80 | #include <machine/limits.h> |
| 81 | #include <machine/stdarg.h> | |
| 82 | ||
| 83 | #include <vfs/union/union.h> | |
| dadab5e9 | 84 | |
| 28623bf9 | 85 | static void mount_warning(struct mount *mp, const char *ctl, ...); |
| d3c546e6 | 86 | static int mount_path(struct proc *p, struct mount *mp, char **rb, char **fb); |
| e24b948e | 87 | static int checkvp_chdir (struct vnode *vn, struct thread *td); |
| 77480a97 | 88 | static void checkdirs (struct nchandle *old_nch, struct nchandle *new_nch); |
| 402ed7e1 | 89 | static int chroot_refuse_vdir_fds (struct filedesc *fdp); |
| 75ffff0d | 90 | static int chroot_visible_mnt(struct mount *mp, struct proc *p); |
| 402ed7e1 RG |
91 | static int getutimes (const struct timeval *, struct timespec *); |
| 92 | static int setfown (struct vnode *, uid_t, gid_t); | |
| 93 | static int setfmode (struct vnode *, int); | |
| 94 | static int setfflags (struct vnode *, int); | |
| 3a907475 MD |
95 | static int setutimes (struct vnode *, struct vattr *, |
| 96 | const struct timespec *, int); | |
| 984263bc MD |
97 | static int usermount = 0; /* if 1, non-root can mount fs. */ |
| 98 | ||
| 402ed7e1 | 99 | int (*union_dircheckp) (struct thread *, struct vnode **, struct file *); |
| 984263bc MD |
100 | |
| 101 | SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0, ""); | |
| 102 | ||
| 103 | /* | |
| 104 | * Virtual File System System Calls | |
| 105 | */ | |
| 106 | ||
| 107 | /* | |
| 108 | * Mount a file system. | |
| 109 | */ | |
| 41c20dac MD |
110 | /* |
| 111 | * mount_args(char *type, char *path, int flags, caddr_t data) | |
| 112 | */ | |
| 984263bc MD |
113 | /* ARGSUSED */ |
| 114 | int | |
| 753fd850 | 115 | sys_mount(struct mount_args *uap) |
| 984263bc | 116 | { |
| dadab5e9 MD |
117 | struct thread *td = curthread; |
| 118 | struct proc *p = td->td_proc; | |
| 984263bc | 119 | struct vnode *vp; |
| 28623bf9 | 120 | struct nchandle nch; |
| 984263bc MD |
121 | struct mount *mp; |
| 122 | struct vfsconf *vfsp; | |
| 123 | int error, flag = 0, flag2 = 0; | |
| 1d505369 | 124 | int hasmount; |
| 984263bc | 125 | struct vattr va; |
| 21739618 | 126 | struct nlookupdata nd; |
| 984263bc | 127 | char fstypename[MFSNAMELEN]; |
| acde96db | 128 | struct ucred *cred = p->p_ucred; |
| 984263bc | 129 | |
| cd0d3e17 | 130 | KKASSERT(p); |
| 57962f80 | 131 | if (jailed(cred)) |
| 84c3a710 | 132 | return (EPERM); |
| 895c1f85 | 133 | if (usermount == 0 && (error = priv_check(td, PRIV_ROOT))) |
| 984263bc MD |
134 | return (error); |
| 135 | /* | |
| 136 | * Do not allow NFS export by non-root users. | |
| 137 | */ | |
| ab2eb4eb | 138 | if (uap->flags & MNT_EXPORTED) { |
| 895c1f85 | 139 | error = priv_check(td, PRIV_ROOT); |
| 984263bc MD |
140 | if (error) |
| 141 | return (error); | |
| 142 | } | |
| 143 | /* | |
| 144 | * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users | |
| 145 | */ | |
| 895c1f85 | 146 | if (priv_check(td, PRIV_ROOT)) |
| ab2eb4eb | 147 | uap->flags |= MNT_NOSUID | MNT_NODEV; |
| 21739618 | 148 | |
| 984263bc | 149 | /* |
| 28623bf9 | 150 | * Lookup the requested path and extract the nch and vnode. |
| 984263bc | 151 | */ |
| 21739618 MD |
152 | error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); |
| 153 | if (error == 0) { | |
| 154 | if ((error = nlookup(&nd)) == 0) { | |
| 28623bf9 | 155 | if (nd.nl_nch.ncp->nc_vp == NULL) |
| 21739618 MD |
156 | error = ENOENT; |
| 157 | } | |
| 158 | } | |
| 159 | if (error) { | |
| 160 | nlookup_done(&nd); | |
| 984263bc | 161 | return (error); |
| 21739618 MD |
162 | } |
| 163 | ||
| 164 | /* | |
| 165 | * Extract the locked+refd ncp and cleanup the nd structure | |
| 166 | */ | |
| 28623bf9 MD |
167 | nch = nd.nl_nch; |
| 168 | cache_zero(&nd.nl_nch); | |
| 21739618 MD |
169 | nlookup_done(&nd); |
| 170 | ||
| 28623bf9 | 171 | if ((nch.ncp->nc_flag & NCF_ISMOUNTPT) && cache_findmount(&nch)) |
| 1d505369 MD |
172 | hasmount = 1; |
| 173 | else | |
| 174 | hasmount = 0; | |
| 175 | ||
| 176 | ||
| 21739618 | 177 | /* |
| 28623bf9 | 178 | * now we have the locked ref'd nch and unreferenced vnode. |
| 21739618 | 179 | */ |
| 28623bf9 | 180 | vp = nch.ncp->nc_vp; |
| 87de5057 | 181 | if ((error = vget(vp, LK_EXCLUSIVE)) != 0) { |
| 28623bf9 | 182 | cache_put(&nch); |
| 21739618 MD |
183 | return (error); |
| 184 | } | |
| 28623bf9 | 185 | cache_unlock(&nch); |
| 21739618 MD |
186 | |
| 187 | /* | |
| 28623bf9 | 188 | * Now we have an unlocked ref'd nch and a locked ref'd vp |
| 21739618 | 189 | */ |
| ab2eb4eb | 190 | if (uap->flags & MNT_UPDATE) { |
| 67863d04 | 191 | if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) { |
| 28623bf9 | 192 | cache_drop(&nch); |
| 984263bc MD |
193 | vput(vp); |
| 194 | return (EINVAL); | |
| 195 | } | |
| 196 | mp = vp->v_mount; | |
| 197 | flag = mp->mnt_flag; | |
| 198 | flag2 = mp->mnt_kern_flag; | |
| 199 | /* | |
| 200 | * We only allow the filesystem to be reloaded if it | |
| 201 | * is currently mounted read-only. | |
| 202 | */ | |
| ab2eb4eb | 203 | if ((uap->flags & MNT_RELOAD) && |
| 984263bc | 204 | ((mp->mnt_flag & MNT_RDONLY) == 0)) { |
| 28623bf9 | 205 | cache_drop(&nch); |
| 984263bc MD |
206 | vput(vp); |
| 207 | return (EOPNOTSUPP); /* Needs translation */ | |
| 208 | } | |
| 209 | /* | |
| 210 | * Only root, or the user that did the original mount is | |
| 211 | * permitted to update it. | |
| 212 | */ | |
| acde96db | 213 | if (mp->mnt_stat.f_owner != cred->cr_uid && |
| 895c1f85 | 214 | (error = priv_check(td, PRIV_ROOT))) { |
| 28623bf9 | 215 | cache_drop(&nch); |
| 984263bc MD |
216 | vput(vp); |
| 217 | return (error); | |
| 218 | } | |
| f9642f56 | 219 | if (vfs_busy(mp, LK_NOWAIT)) { |
| 28623bf9 | 220 | cache_drop(&nch); |
| 984263bc MD |
221 | vput(vp); |
| 222 | return (EBUSY); | |
| 223 | } | |
| 1d505369 | 224 | if ((vp->v_flag & VMOUNT) != 0 || hasmount) { |
| 28623bf9 | 225 | cache_drop(&nch); |
| f9642f56 | 226 | vfs_unbusy(mp); |
| 984263bc MD |
227 | vput(vp); |
| 228 | return (EBUSY); | |
| 229 | } | |
| 230 | vp->v_flag |= VMOUNT; | |
| 984263bc | 231 | mp->mnt_flag |= |
| ab2eb4eb | 232 | uap->flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE); |
| a11aaa81 | 233 | vn_unlock(vp); |
| 984263bc MD |
234 | goto update; |
| 235 | } | |
| 236 | /* | |
| 237 | * If the user is not root, ensure that they own the directory | |
| 238 | * onto which we are attempting to mount. | |
| 239 | */ | |
| 87de5057 | 240 | if ((error = VOP_GETATTR(vp, &va)) || |
| 895c1f85 | 241 | (va.va_uid != cred->cr_uid && (error = priv_check(td, PRIV_ROOT)))) { |
| 28623bf9 | 242 | cache_drop(&nch); |
| 984263bc MD |
243 | vput(vp); |
| 244 | return (error); | |
| 245 | } | |
| 87de5057 | 246 | if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) { |
| 28623bf9 | 247 | cache_drop(&nch); |
| 984263bc MD |
248 | vput(vp); |
| 249 | return (error); | |
| 250 | } | |
| 251 | if (vp->v_type != VDIR) { | |
| 28623bf9 | 252 | cache_drop(&nch); |
| 984263bc MD |
253 | vput(vp); |
| 254 | return (ENOTDIR); | |
| 255 | } | |
| 5dc91765 MD |
256 | if (vp->v_mount->mnt_kern_flag & MNTK_NOSTKMNT) { |
| 257 | cache_drop(&nch); | |
| 258 | vput(vp); | |
| 259 | return (EPERM); | |
| 260 | } | |
| ab2eb4eb | 261 | if ((error = copyinstr(uap->type, fstypename, MFSNAMELEN, NULL)) != 0) { |
| 28623bf9 | 262 | cache_drop(&nch); |
| 984263bc MD |
263 | vput(vp); |
| 264 | return (error); | |
| 265 | } | |
| 2613053d | 266 | vfsp = vfsconf_find_by_name(fstypename); |
| 984263bc MD |
267 | if (vfsp == NULL) { |
| 268 | linker_file_t lf; | |
| 269 | ||
| 270 | /* Only load modules for root (very important!) */ | |
| 895c1f85 | 271 | if ((error = priv_check(td, PRIV_ROOT)) != 0) { |
| 28623bf9 | 272 | cache_drop(&nch); |
| 984263bc MD |
273 | vput(vp); |
| 274 | return error; | |
| 275 | } | |
| b8fb634a | 276 | error = linker_load_file(fstypename, &lf, 0); |
| 984263bc | 277 | if (error || lf == NULL) { |
| 28623bf9 | 278 | cache_drop(&nch); |
| 984263bc MD |
279 | vput(vp); |
| 280 | if (lf == NULL) | |
| 281 | error = ENODEV; | |
| 282 | return error; | |
| 283 | } | |
| 284 | lf->userrefs++; | |
| 285 | /* lookup again, see if the VFS was loaded */ | |
| 2613053d | 286 | vfsp = vfsconf_find_by_name(fstypename); |
| 984263bc MD |
287 | if (vfsp == NULL) { |
| 288 | lf->userrefs--; | |
| 289 | linker_file_unload(lf); | |
| 28623bf9 | 290 | cache_drop(&nch); |
| 984263bc MD |
291 | vput(vp); |
| 292 | return (ENODEV); | |
| 293 | } | |
| 294 | } | |
| 1d505369 | 295 | if ((vp->v_flag & VMOUNT) != 0 || hasmount) { |
| 28623bf9 | 296 | cache_drop(&nch); |
| 984263bc MD |
297 | vput(vp); |
| 298 | return (EBUSY); | |
| 299 | } | |
| 300 | vp->v_flag |= VMOUNT; | |
| 984263bc MD |
301 | |
| 302 | /* | |
| 303 | * Allocate and initialize the filesystem. | |
| 304 | */ | |
| efda3bd0 | 305 | mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK); |
| 984263bc MD |
306 | TAILQ_INIT(&mp->mnt_nvnodelist); |
| 307 | TAILQ_INIT(&mp->mnt_reservedvnlist); | |
| 2281065e | 308 | TAILQ_INIT(&mp->mnt_jlist); |
| 984263bc | 309 | mp->mnt_nvnodelistsize = 0; |
| ab6f251b | 310 | lockinit(&mp->mnt_lock, "vfslock", 0, 0); |
| f9642f56 | 311 | vfs_busy(mp, LK_NOWAIT); |
| 984263bc MD |
312 | mp->mnt_op = vfsp->vfc_vfsops; |
| 313 | mp->mnt_vfc = vfsp; | |
| 314 | vfsp->vfc_refcount++; | |
| 315 | mp->mnt_stat.f_type = vfsp->vfc_typenum; | |
| 316 | mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK; | |
| 317 | strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN); | |
| acde96db | 318 | mp->mnt_stat.f_owner = cred->cr_uid; |
| 984263bc | 319 | mp->mnt_iosize_max = DFLTPHYS; |
| a11aaa81 | 320 | vn_unlock(vp); |
| 984263bc MD |
321 | update: |
| 322 | /* | |
| 323 | * Set the mount level flags. | |
| 324 | */ | |
| ab2eb4eb | 325 | if (uap->flags & MNT_RDONLY) |
| 984263bc MD |
326 | mp->mnt_flag |= MNT_RDONLY; |
| 327 | else if (mp->mnt_flag & MNT_RDONLY) | |
| 328 | mp->mnt_kern_flag |= MNTK_WANTRDWR; | |
| 329 | mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV | | |
| 330 | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOATIME | | |
| 331 | MNT_NOSYMFOLLOW | MNT_IGNORE | | |
| 332 | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR); | |
| ab2eb4eb | 333 | mp->mnt_flag |= uap->flags & (MNT_NOSUID | MNT_NOEXEC | |
| 984263bc MD |
334 | MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_FORCE | |
| 335 | MNT_NOSYMFOLLOW | MNT_IGNORE | | |
| 336 | MNT_NOATIME | MNT_NOCLUSTERR | MNT_NOCLUSTERW | MNT_SUIDDIR); | |
| 337 | /* | |
| 338 | * Mount the filesystem. | |
| 339 | * XXX The final recipients of VFS_MOUNT just overwrite the ndp they | |
| fad57d0e | 340 | * get. |
| 984263bc | 341 | */ |
| acde96db | 342 | error = VFS_MOUNT(mp, uap->path, uap->data, cred); |
| 984263bc MD |
343 | if (mp->mnt_flag & MNT_UPDATE) { |
| 344 | if (mp->mnt_kern_flag & MNTK_WANTRDWR) | |
| 345 | mp->mnt_flag &= ~MNT_RDONLY; | |
| 346 | mp->mnt_flag &=~ (MNT_UPDATE | MNT_RELOAD | MNT_FORCE); | |
| 347 | mp->mnt_kern_flag &=~ MNTK_WANTRDWR; | |
| 348 | if (error) { | |
| 349 | mp->mnt_flag = flag; | |
| 350 | mp->mnt_kern_flag = flag2; | |
| 351 | } | |
| f9642f56 | 352 | vfs_unbusy(mp); |
| 984263bc | 353 | vp->v_flag &= ~VMOUNT; |
| 984263bc | 354 | vrele(vp); |
| 28623bf9 | 355 | cache_drop(&nch); |
| 984263bc MD |
356 | return (error); |
| 357 | } | |
| ca466bae | 358 | vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); |
| 984263bc | 359 | /* |
| 21739618 | 360 | * Put the new filesystem on the mount list after root. The mount |
| 28623bf9 MD |
361 | * point gets its own mnt_ncmountpt (unless the VFS already set one |
| 362 | * up) which represents the root of the mount. The lookup code | |
| 363 | * detects the mount point going forward and checks the root of | |
| 364 | * the mount going backwards. | |
| 8e005a45 MD |
365 | * |
| 366 | * It is not necessary to invalidate or purge the vnode underneath | |
| 367 | * because elements under the mount will be given their own glue | |
| 368 | * namecache record. | |
| 984263bc | 369 | */ |
| 984263bc | 370 | if (!error) { |
| 28623bf9 MD |
371 | if (mp->mnt_ncmountpt.ncp == NULL) { |
| 372 | /* | |
| 373 | * allocate, then unlock, but leave the ref intact | |
| 374 | */ | |
| 375 | cache_allocroot(&mp->mnt_ncmountpt, mp, NULL); | |
| 376 | cache_unlock(&mp->mnt_ncmountpt); | |
| 377 | } | |
| 378 | mp->mnt_ncmounton = nch; /* inherits ref */ | |
| 379 | nch.ncp->nc_flag |= NCF_ISMOUNTPT; | |
| 380 | ||
| 381 | /* XXX get the root of the fs and cache_setvp(mnt_ncmountpt...) */ | |
| 984263bc | 382 | vp->v_flag &= ~VMOUNT; |
| 861905fb | 383 | mountlist_insert(mp, MNTINS_LAST); |
| a11aaa81 | 384 | vn_unlock(vp); |
| c0721f5f | 385 | checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt); |
| 41a01a4d | 386 | error = vfs_allocate_syncvnode(mp); |
| f9642f56 | 387 | vfs_unbusy(mp); |
| 1d505369 MD |
388 | error = VFS_START(mp, 0); |
| 389 | vrele(vp); | |
| 984263bc | 390 | } else { |
| 66a1ddf5 MD |
391 | vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops); |
| 392 | vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops); | |
| 393 | vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops); | |
| 394 | vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops); | |
| 395 | vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops); | |
| 984263bc | 396 | vp->v_flag &= ~VMOUNT; |
| 984263bc | 397 | mp->mnt_vfc->vfc_refcount--; |
| f9642f56 | 398 | vfs_unbusy(mp); |
| efda3bd0 | 399 | kfree(mp, M_MOUNT); |
| 28623bf9 | 400 | cache_drop(&nch); |
| 984263bc MD |
401 | vput(vp); |
| 402 | } | |
| 403 | return (error); | |
| 404 | } | |
| 405 | ||
| 406 | /* | |
| 407 | * Scan all active processes to see if any of them have a current | |
| 408 | * or root directory onto which the new filesystem has just been | |
| 409 | * mounted. If so, replace them with the new mount point. | |
| 21739618 MD |
410 | * |
| 411 | * The passed ncp is ref'd and locked (from the mount code) and | |
| 412 | * must be associated with the vnode representing the root of the | |
| 413 | * mount point. | |
| 984263bc | 414 | */ |
| 8fa76237 | 415 | struct checkdirs_info { |
| 77480a97 MD |
416 | struct nchandle old_nch; |
| 417 | struct nchandle new_nch; | |
| 418 | struct vnode *old_vp; | |
| 419 | struct vnode *new_vp; | |
| 8fa76237 MD |
420 | }; |
| 421 | ||
| 422 | static int checkdirs_callback(struct proc *p, void *data); | |
| 423 | ||
| 984263bc | 424 | static void |
| 77480a97 | 425 | checkdirs(struct nchandle *old_nch, struct nchandle *new_nch) |
| 984263bc | 426 | { |
| 8fa76237 | 427 | struct checkdirs_info info; |
| 77480a97 | 428 | struct vnode *olddp; |
| 984263bc | 429 | struct vnode *newdp; |
| 690a3127 | 430 | struct mount *mp; |
| 984263bc | 431 | |
| 77480a97 MD |
432 | /* |
| 433 | * If the old mount point's vnode has a usecount of 1, it is not | |
| 434 | * being held as a descriptor anywhere. | |
| 435 | */ | |
| 436 | olddp = old_nch->ncp->nc_vp; | |
| 3c37c940 | 437 | if (olddp == NULL || olddp->v_sysref.refcnt == 1) |
| 984263bc | 438 | return; |
| 77480a97 MD |
439 | |
| 440 | /* | |
| 441 | * Force the root vnode of the new mount point to be resolved | |
| 442 | * so we can update any matching processes. | |
| 443 | */ | |
| 444 | mp = new_nch->mount; | |
| 690a3127 | 445 | if (VFS_ROOT(mp, &newdp)) |
| 984263bc | 446 | panic("mount: lost mount"); |
| 77480a97 MD |
447 | cache_setunresolved(new_nch); |
| 448 | cache_setvp(new_nch, newdp); | |
| 21739618 | 449 | |
| 77480a97 MD |
450 | /* |
| 451 | * Special handling of the root node | |
| 452 | */ | |
| 21739618 MD |
453 | if (rootvnode == olddp) { |
| 454 | vref(newdp); | |
| 77480a97 | 455 | vfs_cache_setroot(newdp, cache_hold(new_nch)); |
| 21739618 MD |
456 | } |
| 457 | ||
| 77480a97 MD |
458 | /* |
| 459 | * Pass newdp separately so the callback does not have to access | |
| 460 | * it via new_nch->ncp->nc_vp. | |
| 461 | */ | |
| 462 | info.old_nch = *old_nch; | |
| 463 | info.new_nch = *new_nch; | |
| 464 | info.new_vp = newdp; | |
| 8fa76237 MD |
465 | allproc_scan(checkdirs_callback, &info); |
| 466 | vput(newdp); | |
| 467 | } | |
| 468 | ||
| 469 | /* | |
| 470 | * NOTE: callback is not MP safe because the scanned process's filedesc | |
| 471 | * structure can be ripped out from under us, amoung other things. | |
| 472 | */ | |
| 473 | static int | |
| 474 | checkdirs_callback(struct proc *p, void *data) | |
| 475 | { | |
| 476 | struct checkdirs_info *info = data; | |
| 477 | struct filedesc *fdp; | |
| 28623bf9 MD |
478 | struct nchandle ncdrop1; |
| 479 | struct nchandle ncdrop2; | |
| 8fa76237 MD |
480 | struct vnode *vprele1; |
| 481 | struct vnode *vprele2; | |
| 482 | ||
| 483 | if ((fdp = p->p_fd) != NULL) { | |
| 28623bf9 MD |
484 | cache_zero(&ncdrop1); |
| 485 | cache_zero(&ncdrop2); | |
| 8fa76237 MD |
486 | vprele1 = NULL; |
| 487 | vprele2 = NULL; | |
| 488 | ||
| 489 | /* | |
| 490 | * MPUNSAFE - XXX fdp can be pulled out from under a | |
| 491 | * foreign process. | |
| 492 | * | |
| 493 | * A shared filedesc is ok, we don't have to copy it | |
| 494 | * because we are making this change globally. | |
| 495 | */ | |
| 496 | spin_lock_wr(&fdp->fd_spin); | |
| 77480a97 MD |
497 | if (fdp->fd_ncdir.mount == info->old_nch.mount && |
| 498 | fdp->fd_ncdir.ncp == info->old_nch.ncp) { | |
| 8fa76237 | 499 | vprele1 = fdp->fd_cdir; |
| 77480a97 MD |
500 | vref(info->new_vp); |
| 501 | fdp->fd_cdir = info->new_vp; | |
| 8fa76237 | 502 | ncdrop1 = fdp->fd_ncdir; |
| 77480a97 | 503 | cache_copy(&info->new_nch, &fdp->fd_ncdir); |
| 984263bc | 504 | } |
| 77480a97 MD |
505 | if (fdp->fd_nrdir.mount == info->old_nch.mount && |
| 506 | fdp->fd_nrdir.ncp == info->old_nch.ncp) { | |
| 8fa76237 | 507 | vprele2 = fdp->fd_rdir; |
| 77480a97 MD |
508 | vref(info->new_vp); |
| 509 | fdp->fd_rdir = info->new_vp; | |
| 8fa76237 | 510 | ncdrop2 = fdp->fd_nrdir; |
| 77480a97 | 511 | cache_copy(&info->new_nch, &fdp->fd_nrdir); |
| 984263bc | 512 | } |
| 8fa76237 | 513 | spin_unlock_wr(&fdp->fd_spin); |
| 28623bf9 MD |
514 | if (ncdrop1.ncp) |
| 515 | cache_drop(&ncdrop1); | |
| 516 | if (ncdrop2.ncp) | |
| 517 | cache_drop(&ncdrop2); | |
| 8fa76237 MD |
518 | if (vprele1) |
| 519 | vrele(vprele1); | |
| 520 | if (vprele2) | |
| 521 | vrele(vprele2); | |
| 984263bc | 522 | } |
| 8fa76237 | 523 | return(0); |
| 984263bc MD |
524 | } |
| 525 | ||
| 526 | /* | |
| 527 | * Unmount a file system. | |
| 528 | * | |
| 529 | * Note: unmount takes a path to the vnode mounted on as argument, | |
| 530 | * not special file (as before). | |
| 531 | */ | |
| 41c20dac MD |
532 | /* |
| 533 | * umount_args(char *path, int flags) | |
| 534 | */ | |
| 984263bc MD |
535 | /* ARGSUSED */ |
| 536 | int | |
| 753fd850 | 537 | sys_unmount(struct unmount_args *uap) |
| 984263bc | 538 | { |
| dadab5e9 MD |
539 | struct thread *td = curthread; |
| 540 | struct proc *p = td->td_proc; | |
| d2293868 | 541 | struct mount *mp = NULL; |
| 984263bc | 542 | int error; |
| fad57d0e | 543 | struct nlookupdata nd; |
| 984263bc | 544 | |
| dadab5e9 | 545 | KKASSERT(p); |
| 84c3a710 HP |
546 | if (p->p_ucred->cr_prison != NULL) |
| 547 | return (EPERM); | |
| 895c1f85 | 548 | if (usermount == 0 && (error = priv_check(td, PRIV_ROOT))) |
| 84c3a710 HP |
549 | return (error); |
| 550 | ||
| ab2eb4eb | 551 | error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); |
| fad57d0e MD |
552 | if (error == 0) |
| 553 | error = nlookup(&nd); | |
| fad57d0e | 554 | if (error) |
| d2293868 | 555 | goto out; |
| fad57d0e | 556 | |
| 28623bf9 | 557 | mp = nd.nl_nch.mount; |
| 984263bc MD |
558 | |
| 559 | /* | |
| 560 | * Only root, or the user that did the original mount is | |
| 561 | * permitted to unmount this filesystem. | |
| 562 | */ | |
| 563 | if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) && | |
| 895c1f85 | 564 | (error = priv_check(td, PRIV_ROOT))) |
| d2293868 | 565 | goto out; |
| 984263bc MD |
566 | |
| 567 | /* | |
| 568 | * Don't allow unmounting the root file system. | |
| 569 | */ | |
| 570 | if (mp->mnt_flag & MNT_ROOTFS) { | |
| d2293868 MD |
571 | error = EINVAL; |
| 572 | goto out; | |
| 984263bc MD |
573 | } |
| 574 | ||
| 575 | /* | |
| 576 | * Must be the root of the filesystem | |
| 577 | */ | |
| 28623bf9 | 578 | if (nd.nl_nch.ncp != mp->mnt_ncmountpt.ncp) { |
| d2293868 MD |
579 | error = EINVAL; |
| 580 | goto out; | |
| 984263bc | 581 | } |
| d2293868 MD |
582 | |
| 583 | out: | |
| 584 | nlookup_done(&nd); | |
| 585 | if (error) | |
| 586 | return (error); | |
| acde96db | 587 | return (dounmount(mp, uap->flags)); |
| 984263bc MD |
588 | } |
| 589 | ||
| 590 | /* | |
| 591 | * Do the actual file system unmount. | |
| 592 | */ | |
| 861905fb MD |
593 | static int |
| 594 | dounmount_interlock(struct mount *mp) | |
| 595 | { | |
| 596 | if (mp->mnt_kern_flag & MNTK_UNMOUNT) | |
| 597 | return (EBUSY); | |
| 598 | mp->mnt_kern_flag |= MNTK_UNMOUNT; | |
| 599 | return(0); | |
| 600 | } | |
| 601 | ||
| 984263bc | 602 | int |
| acde96db | 603 | dounmount(struct mount *mp, int flags) |
| 984263bc | 604 | { |
| dcf26f79 | 605 | struct namecache *ncp; |
| 28623bf9 | 606 | struct nchandle nch; |
| 2ec4b00d | 607 | struct vnode *vp; |
| 984263bc MD |
608 | int error; |
| 609 | int async_flag; | |
| 2df9419c | 610 | int lflags; |
| 28623bf9 | 611 | int freeok = 1; |
| 984263bc | 612 | |
| 861905fb MD |
613 | /* |
| 614 | * Exclusive access for unmounting purposes | |
| 615 | */ | |
| 616 | if ((error = mountlist_interlock(dounmount_interlock, mp)) != 0) | |
| 617 | return (error); | |
| 618 | ||
| 619 | /* | |
| 620 | * Allow filesystems to detect that a forced unmount is in progress. | |
| 621 | */ | |
| 984263bc MD |
622 | if (flags & MNT_FORCE) |
| 623 | mp->mnt_kern_flag |= MNTK_UNMOUNTF; | |
| 2df9419c | 624 | lflags = LK_EXCLUSIVE | ((flags & MNT_FORCE) ? 0 : LK_NOWAIT); |
| df4f70a6 | 625 | error = lockmgr(&mp->mnt_lock, lflags); |
| 984263bc MD |
626 | if (error) { |
| 627 | mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF); | |
| 628 | if (mp->mnt_kern_flag & MNTK_MWAIT) | |
| e38462a2 | 629 | wakeup(mp); |
| 984263bc MD |
630 | return (error); |
| 631 | } | |
| 632 | ||
| 633 | if (mp->mnt_flag & MNT_EXPUBLIC) | |
| 634 | vfs_setpublicfs(NULL, NULL, NULL); | |
| 635 | ||
| 636 | vfs_msync(mp, MNT_WAIT); | |
| 637 | async_flag = mp->mnt_flag & MNT_ASYNC; | |
| 638 | mp->mnt_flag &=~ MNT_ASYNC; | |
| dcf26f79 MD |
639 | |
| 640 | /* | |
| 28623bf9 MD |
641 | * If this filesystem isn't aliasing other filesystems, |
| 642 | * try to invalidate any remaining namecache entries and | |
| 643 | * check the count afterwords. | |
| dcf26f79 | 644 | */ |
| 28623bf9 MD |
645 | if ((mp->mnt_kern_flag & MNTK_NCALIASED) == 0) { |
| 646 | cache_lock(&mp->mnt_ncmountpt); | |
| 647 | cache_inval(&mp->mnt_ncmountpt, CINV_DESTROY|CINV_CHILDREN); | |
| 648 | cache_unlock(&mp->mnt_ncmountpt); | |
| 649 | ||
| 650 | if ((ncp = mp->mnt_ncmountpt.ncp) != NULL && | |
| 651 | (ncp->nc_refs != 1 || TAILQ_FIRST(&ncp->nc_list))) { | |
| dcf26f79 MD |
652 | |
| 653 | if ((flags & MNT_FORCE) == 0) { | |
| 654 | error = EBUSY; | |
| 28623bf9 MD |
655 | mount_warning(mp, "Cannot unmount: " |
| 656 | "%d namecache " | |
| 657 | "references still " | |
| 658 | "present", | |
| 659 | ncp->nc_refs - 1); | |
| dcf26f79 | 660 | } else { |
| 28623bf9 MD |
661 | mount_warning(mp, "Forced unmount: " |
| 662 | "%d namecache " | |
| 663 | "references still " | |
| 664 | "present", | |
| 665 | ncp->nc_refs - 1); | |
| 666 | freeok = 0; | |
| dcf26f79 MD |
667 | } |
| 668 | } | |
| 669 | } | |
| 670 | ||
| 28623bf9 MD |
671 | /* |
| 672 | * nchandle records ref the mount structure. Expect a count of 1 | |
| 673 | * (our mount->mnt_ncmountpt). | |
| 674 | */ | |
| 675 | if (mp->mnt_refs != 1) { | |
| 676 | if ((flags & MNT_FORCE) == 0) { | |
| 677 | mount_warning(mp, "Cannot unmount: " | |
| 678 | "%d process references still " | |
| 679 | "present", mp->mnt_refs); | |
| 680 | error = EBUSY; | |
| 681 | } else { | |
| 682 | mount_warning(mp, "Forced unmount: " | |
| 683 | "%d process references still " | |
| 684 | "present", mp->mnt_refs); | |
| 685 | freeok = 0; | |
| 686 | } | |
| 687 | } | |
| 688 | ||
| 2ec4b00d MD |
689 | /* |
| 690 | * Decomission our special mnt_syncer vnode. This also stops | |
| 691 | * the vnlru code. If we are unable to unmount we recommission | |
| 692 | * the vnode. | |
| 693 | */ | |
| dcf26f79 | 694 | if (error == 0) { |
| 2ec4b00d MD |
695 | if ((vp = mp->mnt_syncer) != NULL) { |
| 696 | mp->mnt_syncer = NULL; | |
| 697 | vrele(vp); | |
| 698 | } | |
| dcf26f79 MD |
699 | if (((mp->mnt_flag & MNT_RDONLY) || |
| 700 | (error = VFS_SYNC(mp, MNT_WAIT)) == 0) || | |
| 701 | (flags & MNT_FORCE)) { | |
| 702 | error = VFS_UNMOUNT(mp, flags); | |
| 703 | } | |
| 704 | } | |
| 984263bc | 705 | if (error) { |
| 41a01a4d MD |
706 | if (mp->mnt_syncer == NULL) |
| 707 | vfs_allocate_syncvnode(mp); | |
| 984263bc MD |
708 | mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF); |
| 709 | mp->mnt_flag |= async_flag; | |
| df4f70a6 | 710 | lockmgr(&mp->mnt_lock, LK_RELEASE); |
| 984263bc | 711 | if (mp->mnt_kern_flag & MNTK_MWAIT) |
| e38462a2 | 712 | wakeup(mp); |
| 984263bc MD |
713 | return (error); |
| 714 | } | |
| 432b8263 MD |
715 | /* |
| 716 | * Clean up any journals still associated with the mount after | |
| 717 | * filesystem activity has ceased. | |
| 718 | */ | |
| 719 | journal_remove_all_journals(mp, | |
| 720 | ((flags & MNT_FORCE) ? MC_JOURNAL_STOP_IMM : 0)); | |
| 721 | ||
| 861905fb | 722 | mountlist_remove(mp); |
| 0961aa92 MD |
723 | |
| 724 | /* | |
| 725 | * Remove any installed vnode ops here so the individual VFSs don't | |
| 726 | * have to. | |
| 727 | */ | |
| 66a1ddf5 MD |
728 | vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops); |
| 729 | vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops); | |
| 730 | vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops); | |
| 731 | vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops); | |
| 732 | vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops); | |
| 0961aa92 | 733 | |
| 28623bf9 MD |
734 | if (mp->mnt_ncmountpt.ncp != NULL) { |
| 735 | nch = mp->mnt_ncmountpt; | |
| 736 | cache_zero(&mp->mnt_ncmountpt); | |
| 737 | cache_clrmountpt(&nch); | |
| 738 | cache_drop(&nch); | |
| 739 | } | |
| 740 | if (mp->mnt_ncmounton.ncp != NULL) { | |
| 741 | nch = mp->mnt_ncmounton; | |
| 742 | cache_zero(&mp->mnt_ncmounton); | |
| 743 | cache_clrmountpt(&nch); | |
| 744 | cache_drop(&nch); | |
| 984263bc | 745 | } |
| 1d505369 | 746 | |
| 984263bc MD |
747 | mp->mnt_vfc->vfc_refcount--; |
| 748 | if (!TAILQ_EMPTY(&mp->mnt_nvnodelist)) | |
| 749 | panic("unmount: dangling vnode"); | |
| df4f70a6 | 750 | lockmgr(&mp->mnt_lock, LK_RELEASE); |
| 984263bc | 751 | if (mp->mnt_kern_flag & MNTK_MWAIT) |
| e38462a2 | 752 | wakeup(mp); |
| 28623bf9 MD |
753 | if (freeok) |
| 754 | kfree(mp, M_MOUNT); | |
| 984263bc MD |
755 | return (0); |
| 756 | } | |
| 757 | ||
| 28623bf9 MD |
758 | static |
| 759 | void | |
| 760 | mount_warning(struct mount *mp, const char *ctl, ...) | |
| 761 | { | |
| 762 | char *ptr; | |
| 763 | char *buf; | |
| 764 | __va_list va; | |
| 765 | ||
| 766 | __va_start(va, ctl); | |
| 767 | if (cache_fullpath(NULL, &mp->mnt_ncmounton, &ptr, &buf) == 0) { | |
| 6ea70f76 | 768 | kprintf("unmount(%s): ", ptr); |
| 379210cb | 769 | kvprintf(ctl, va); |
| 6ea70f76 | 770 | kprintf("\n"); |
| 28623bf9 MD |
771 | kfree(buf, M_TEMP); |
| 772 | } else { | |
| 56bcacad MD |
773 | kprintf("unmount(%p", mp); |
| 774 | if (mp->mnt_ncmounton.ncp && mp->mnt_ncmounton.ncp->nc_name) | |
| 775 | kprintf(",%s", mp->mnt_ncmounton.ncp->nc_name); | |
| 776 | kprintf("): "); | |
| 379210cb | 777 | kvprintf(ctl, va); |
| 6ea70f76 | 778 | kprintf("\n"); |
| 28623bf9 MD |
779 | } |
| 780 | __va_end(va); | |
| 781 | } | |
| 782 | ||
| 984263bc | 783 | /* |
| d3c546e6 MD |
784 | * Shim cache_fullpath() to handle the case where a process is chrooted into |
| 785 | * a subdirectory of a mount. In this case if the root mount matches the | |
| 786 | * process root directory's mount we have to specify the process's root | |
| 787 | * directory instead of the mount point, because the mount point might | |
| 788 | * be above the root directory. | |
| 789 | */ | |
| 790 | static | |
| 791 | int | |
| 792 | mount_path(struct proc *p, struct mount *mp, char **rb, char **fb) | |
| 793 | { | |
| 794 | struct nchandle *nch; | |
| d3c546e6 MD |
795 | |
| 796 | if (p && p->p_fd->fd_nrdir.mount == mp) | |
| 797 | nch = &p->p_fd->fd_nrdir; | |
| 798 | else | |
| 799 | nch = &mp->mnt_ncmountpt; | |
| 800 | return(cache_fullpath(p, nch, rb, fb)); | |
| 801 | } | |
| 802 | ||
| 803 | /* | |
| 984263bc MD |
804 | * Sync each mounted filesystem. |
| 805 | */ | |
| 984263bc MD |
806 | |
| 807 | #ifdef DEBUG | |
| 808 | static int syncprt = 0; | |
| 809 | SYSCTL_INT(_debug, OID_AUTO, syncprt, CTLFLAG_RW, &syncprt, 0, ""); | |
| 6bdbb368 | 810 | #endif /* DEBUG */ |
| 984263bc | 811 | |
| 861905fb MD |
812 | static int sync_callback(struct mount *mp, void *data); |
| 813 | ||
| 984263bc MD |
814 | /* ARGSUSED */ |
| 815 | int | |
| 753fd850 | 816 | sys_sync(struct sync_args *uap) |
| 984263bc | 817 | { |
| 861905fb | 818 | mountlist_scan(sync_callback, NULL, MNTSCAN_FORWARD); |
| 6bdbb368 | 819 | #ifdef DEBUG |
| 861905fb MD |
820 | /* |
| 821 | * print out buffer pool stat information on each sync() call. | |
| 822 | */ | |
| 984263bc MD |
823 | if (syncprt) |
| 824 | vfs_bufstats(); | |
| 6bdbb368 | 825 | #endif /* DEBUG */ |
| 984263bc MD |
826 | return (0); |
| 827 | } | |
| 828 | ||
| 861905fb MD |
829 | static |
| 830 | int | |
| 831 | sync_callback(struct mount *mp, void *data __unused) | |
| 832 | { | |
| 833 | int asyncflag; | |
| 834 | ||
| 835 | if ((mp->mnt_flag & MNT_RDONLY) == 0) { | |
| 836 | asyncflag = mp->mnt_flag & MNT_ASYNC; | |
| 837 | mp->mnt_flag &= ~MNT_ASYNC; | |
| 838 | vfs_msync(mp, MNT_NOWAIT); | |
| 87de5057 | 839 | VFS_SYNC(mp, MNT_NOWAIT); |
| 861905fb MD |
840 | mp->mnt_flag |= asyncflag; |
| 841 | } | |
| 842 | return(0); | |
| 843 | } | |
| 844 | ||
| 984263bc MD |
845 | /* XXX PRISON: could be per prison flag */ |
| 846 | static int prison_quotas; | |
| 847 | #if 0 | |
| 848 | SYSCTL_INT(_kern_prison, OID_AUTO, quotas, CTLFLAG_RW, &prison_quotas, 0, ""); | |
| 849 | #endif | |
| 850 | ||
| 851 | /* | |
| 41c20dac MD |
852 | * quotactl_args(char *path, int fcmd, int uid, caddr_t arg) |
| 853 | * | |
| 984263bc MD |
854 | * Change filesystem quotas. |
| 855 | */ | |
| 984263bc MD |
856 | /* ARGSUSED */ |
| 857 | int | |
| 753fd850 | 858 | sys_quotactl(struct quotactl_args *uap) |
| 984263bc | 859 | { |
| fad57d0e MD |
860 | struct nlookupdata nd; |
| 861 | struct thread *td; | |
| 862 | struct proc *p; | |
| 41c20dac | 863 | struct mount *mp; |
| 984263bc | 864 | int error; |
| 984263bc | 865 | |
| fad57d0e MD |
866 | td = curthread; |
| 867 | p = td->td_proc; | |
| 41c20dac | 868 | if (p->p_ucred->cr_prison && !prison_quotas) |
| 984263bc | 869 | return (EPERM); |
| fad57d0e | 870 | |
| ab2eb4eb | 871 | error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); |
| fad57d0e MD |
872 | if (error == 0) |
| 873 | error = nlookup(&nd); | |
| 874 | if (error == 0) { | |
| 28623bf9 | 875 | mp = nd.nl_nch.mount; |
| ab2eb4eb | 876 | error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, |
| acde96db | 877 | uap->arg, nd.nl_cred); |
| fad57d0e MD |
878 | } |
| 879 | nlookup_done(&nd); | |
| 880 | return (error); | |
| 984263bc MD |
881 | } |
| 882 | ||
| 949ecb9b | 883 | /* |
| 2281065e | 884 | * mountctl(char *path, int op, int fd, const void *ctl, int ctllen, |
| 949ecb9b MD |
885 | * void *buf, int buflen) |
| 886 | * | |
| 887 | * This function operates on a mount point and executes the specified | |
| 888 | * operation using the specified control data, and possibly returns data. | |
| 889 | * | |
| 890 | * The actual number of bytes stored in the result buffer is returned, 0 | |
| 891 | * if none, otherwise an error is returned. | |
| 892 | */ | |
| 893 | /* ARGSUSED */ | |
| 894 | int | |
| 753fd850 | 895 | sys_mountctl(struct mountctl_args *uap) |
| 949ecb9b MD |
896 | { |
| 897 | struct thread *td = curthread; | |
| 898 | struct proc *p = td->td_proc; | |
| 2281065e | 899 | struct file *fp; |
| 949ecb9b MD |
900 | void *ctl = NULL; |
| 901 | void *buf = NULL; | |
| 902 | char *path = NULL; | |
| 903 | int error; | |
| 904 | ||
| 905 | /* | |
| 906 | * Sanity and permissions checks. We must be root. | |
| 907 | */ | |
| 908 | KKASSERT(p); | |
| 909 | if (p->p_ucred->cr_prison != NULL) | |
| 910 | return (EPERM); | |
| dad088a5 MD |
911 | if ((uap->op != MOUNTCTL_MOUNTFLAGS) && |
| 912 | (error = priv_check(td, PRIV_ROOT)) != 0) | |
| 949ecb9b MD |
913 | return (error); |
| 914 | ||
| 915 | /* | |
| 916 | * Argument length checks | |
| 917 | */ | |
| 39b13188 | 918 | if (uap->ctllen < 0 || uap->ctllen > 1024) |
| 949ecb9b | 919 | return (EINVAL); |
| 39b13188 | 920 | if (uap->buflen < 0 || uap->buflen > 16 * 1024) |
| 949ecb9b MD |
921 | return (EINVAL); |
| 922 | if (uap->path == NULL) | |
| 923 | return (EINVAL); | |
| 924 | ||
| 925 | /* | |
| 926 | * Allocate the necessary buffers and copyin data | |
| 927 | */ | |
| 70aac194 | 928 | path = objcache_get(namei_oc, M_WAITOK); |
| 949ecb9b MD |
929 | error = copyinstr(uap->path, path, MAXPATHLEN, NULL); |
| 930 | if (error) | |
| 931 | goto done; | |
| 932 | ||
| 933 | if (uap->ctllen) { | |
| efda3bd0 | 934 | ctl = kmalloc(uap->ctllen + 1, M_TEMP, M_WAITOK|M_ZERO); |
| 949ecb9b MD |
935 | error = copyin(uap->ctl, ctl, uap->ctllen); |
| 936 | if (error) | |
| 937 | goto done; | |
| 938 | } | |
| 939 | if (uap->buflen) | |
| efda3bd0 | 940 | buf = kmalloc(uap->buflen + 1, M_TEMP, M_WAITOK|M_ZERO); |
| 949ecb9b MD |
941 | |
| 942 | /* | |
| 2281065e MD |
943 | * Validate the descriptor |
| 944 | */ | |
| ecda6326 MD |
945 | if (uap->fd >= 0) { |
| 946 | fp = holdfp(p->p_fd, uap->fd, -1); | |
| 947 | if (fp == NULL) { | |
| 948 | error = EBADF; | |
| 949 | goto done; | |
| 950 | } | |
| 951 | } else { | |
| 952 | fp = NULL; | |
| 2281065e | 953 | } |
| 2281065e MD |
954 | |
| 955 | /* | |
| 949ecb9b MD |
956 | * Execute the internal kernel function and clean up. |
| 957 | */ | |
| 2281065e MD |
958 | error = kern_mountctl(path, uap->op, fp, ctl, uap->ctllen, buf, uap->buflen, &uap->sysmsg_result); |
| 959 | if (fp) | |
| 9f87144f | 960 | fdrop(fp); |
| 949ecb9b MD |
961 | if (error == 0 && uap->sysmsg_result > 0) |
| 962 | error = copyout(buf, uap->buf, uap->sysmsg_result); | |
| 963 | done: | |
| 964 | if (path) | |
| 70aac194 | 965 | objcache_put(namei_oc, path); |
| 949ecb9b | 966 | if (ctl) |
| efda3bd0 | 967 | kfree(ctl, M_TEMP); |
| 949ecb9b | 968 | if (buf) |
| efda3bd0 | 969 | kfree(buf, M_TEMP); |
| 949ecb9b MD |
970 | return (error); |
| 971 | } | |
| 972 | ||
| 973 | /* | |
| 974 | * Execute a mount control operation by resolving the path to a mount point | |
| 975 | * and calling vop_mountctl(). | |
| 67863d04 MD |
976 | * |
| 977 | * Use the mount point from the nch instead of the vnode so nullfs mounts | |
| 978 | * can properly spike the VOP. | |
| 949ecb9b MD |
979 | */ |
| 980 | int | |
| 2281065e MD |
981 | kern_mountctl(const char *path, int op, struct file *fp, |
| 982 | const void *ctl, int ctllen, | |
| 949ecb9b MD |
983 | void *buf, int buflen, int *res) |
| 984 | { | |
| 949ecb9b MD |
985 | struct vnode *vp; |
| 986 | struct mount *mp; | |
| 987 | struct nlookupdata nd; | |
| 988 | int error; | |
| 989 | ||
| 990 | *res = 0; | |
| 991 | vp = NULL; | |
| 992 | error = nlookup_init(&nd, path, UIO_SYSSPACE, NLC_FOLLOW); | |
| 993 | if (error == 0) | |
| 994 | error = nlookup(&nd); | |
| 995 | if (error == 0) | |
| 28623bf9 | 996 | error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp); |
| 67863d04 | 997 | mp = nd.nl_nch.mount; |
| 949ecb9b MD |
998 | nlookup_done(&nd); |
| 999 | if (error) | |
| 1000 | return (error); | |
| 1001 | ||
| 949ecb9b MD |
1002 | /* |
| 1003 | * Must be the root of the filesystem | |
| 1004 | */ | |
| 67863d04 | 1005 | if ((vp->v_flag & (VROOT|VPFSROOT)) == 0) { |
| 949ecb9b MD |
1006 | vput(vp); |
| 1007 | return (EINVAL); | |
| 1008 | } | |
| 2281065e | 1009 | error = vop_mountctl(mp->mnt_vn_use_ops, op, fp, ctl, ctllen, |
| 949ecb9b MD |
1010 | buf, buflen, res); |
| 1011 | vput(vp); | |
| 1012 | return (error); | |
| 1013 | } | |
| 1014 | ||
| 984263bc | 1015 | int |
| fad57d0e | 1016 | kern_statfs(struct nlookupdata *nd, struct statfs *buf) |
| 984263bc | 1017 | { |
| dadab5e9 | 1018 | struct thread *td = curthread; |
| 75ffff0d | 1019 | struct proc *p = td->td_proc; |
| 41c20dac MD |
1020 | struct mount *mp; |
| 1021 | struct statfs *sp; | |
| 75ffff0d | 1022 | char *fullpath, *freepath; |
| 984263bc | 1023 | int error; |
| 984263bc | 1024 | |
| fad57d0e | 1025 | if ((error = nlookup(nd)) != 0) |
| 984263bc | 1026 | return (error); |
| 28623bf9 | 1027 | mp = nd->nl_nch.mount; |
| 984263bc | 1028 | sp = &mp->mnt_stat; |
| acde96db | 1029 | if ((error = VFS_STATFS(mp, sp, nd->nl_cred)) != 0) |
| 984263bc | 1030 | return (error); |
| 75ffff0d | 1031 | |
| d3c546e6 | 1032 | error = mount_path(p, mp, &fullpath, &freepath); |
| 75ffff0d JS |
1033 | if (error) |
| 1034 | return(error); | |
| 1035 | bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); | |
| 1036 | strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname)); | |
| efda3bd0 | 1037 | kfree(freepath, M_TEMP); |
| 75ffff0d | 1038 | |
| 984263bc | 1039 | sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK; |
| 9697c509 DRJ |
1040 | bcopy(sp, buf, sizeof(*buf)); |
| 1041 | /* Only root should have access to the fsid's. */ | |
| 895c1f85 | 1042 | if (priv_check(td, PRIV_ROOT)) |
| 9697c509 DRJ |
1043 | buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0; |
| 1044 | return (0); | |
| 984263bc MD |
1045 | } |
| 1046 | ||
| 1047 | /* | |
| 9697c509 | 1048 | * statfs_args(char *path, struct statfs *buf) |
| 41c20dac | 1049 | * |
| 984263bc MD |
1050 | * Get filesystem statistics. |
| 1051 | */ | |
| 984263bc | 1052 | int |
| 753fd850 | 1053 | sys_statfs(struct statfs_args *uap) |
| 9697c509 | 1054 | { |
| fad57d0e | 1055 | struct nlookupdata nd; |
| 9697c509 DRJ |
1056 | struct statfs buf; |
| 1057 | int error; | |
| 1058 | ||
| fad57d0e MD |
1059 | error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); |
| 1060 | if (error == 0) | |
| 1061 | error = kern_statfs(&nd, &buf); | |
| 1062 | nlookup_done(&nd); | |
| 9697c509 DRJ |
1063 | if (error == 0) |
| 1064 | error = copyout(&buf, uap->buf, sizeof(*uap->buf)); | |
| 1065 | return (error); | |
| 1066 | } | |
| 1067 | ||
| 1068 | int | |
| 1069 | kern_fstatfs(int fd, struct statfs *buf) | |
| 984263bc | 1070 | { |
| dadab5e9 MD |
1071 | struct thread *td = curthread; |
| 1072 | struct proc *p = td->td_proc; | |
| 984263bc MD |
1073 | struct file *fp; |
| 1074 | struct mount *mp; | |
| 1fd87d54 | 1075 | struct statfs *sp; |
| 75ffff0d | 1076 | char *fullpath, *freepath; |
| 984263bc | 1077 | int error; |
| 984263bc | 1078 | |
| dadab5e9 | 1079 | KKASSERT(p); |
| 5b287bba | 1080 | if ((error = holdvnode(p->p_fd, fd, &fp)) != 0) |
| 984263bc MD |
1081 | return (error); |
| 1082 | mp = ((struct vnode *)fp->f_data)->v_mount; | |
| 5b287bba MD |
1083 | if (mp == NULL) { |
| 1084 | error = EBADF; | |
| 1085 | goto done; | |
| 1086 | } | |
| 1087 | if (fp->f_cred == NULL) { | |
| 1088 | error = EINVAL; | |
| 1089 | goto done; | |
| 1090 | } | |
| 984263bc | 1091 | sp = &mp->mnt_stat; |
| 5b287bba MD |
1092 | if ((error = VFS_STATFS(mp, sp, fp->f_cred)) != 0) |
| 1093 | goto done; | |
| 75ffff0d | 1094 | |
| d3c546e6 | 1095 | if ((error = mount_path(p, mp, &fullpath, &freepath)) != 0) |
| 5b287bba | 1096 | goto done; |
| 75ffff0d JS |
1097 | bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); |
| 1098 | strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname)); | |
| efda3bd0 | 1099 | kfree(freepath, M_TEMP); |
| 75ffff0d | 1100 | |
| 984263bc | 1101 | sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK; |
| 9697c509 | 1102 | bcopy(sp, buf, sizeof(*buf)); |
| 75ffff0d | 1103 | |
| 9697c509 | 1104 | /* Only root should have access to the fsid's. */ |
| 895c1f85 | 1105 | if (priv_check(td, PRIV_ROOT)) |
| 9697c509 | 1106 | buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0; |
| 5b287bba MD |
1107 | error = 0; |
| 1108 | done: | |
| 1109 | fdrop(fp); | |
| 1110 | return (error); | |
| 9697c509 DRJ |
1111 | } |
| 1112 | ||
| 1113 | /* | |
| 1114 | * fstatfs_args(int fd, struct statfs *buf) | |
| 1115 | * | |
| 1116 | * Get filesystem statistics. | |
| 1117 | */ | |
| 1118 | int | |
| 753fd850 | 1119 | sys_fstatfs(struct fstatfs_args *uap) |
| 9697c509 DRJ |
1120 | { |
| 1121 | struct statfs buf; | |
| 1122 | int error; | |
| 1123 | ||
| 1124 | error = kern_fstatfs(uap->fd, &buf); | |
| 1125 | ||
| 1126 | if (error == 0) | |
| 1127 | error = copyout(&buf, uap->buf, sizeof(*uap->buf)); | |
| 1128 | return (error); | |
| 984263bc MD |
1129 | } |
| 1130 | ||
| d9fad06e MD |
1131 | int |
| 1132 | kern_statvfs(struct nlookupdata *nd, struct statvfs *buf) | |
| 1133 | { | |
| 1134 | struct mount *mp; | |
| 1135 | struct statvfs *sp; | |
| 1136 | int error; | |
| 1137 | ||
| 1138 | if ((error = nlookup(nd)) != 0) | |
| 1139 | return (error); | |
| 1140 | mp = nd->nl_nch.mount; | |
| 1141 | sp = &mp->mnt_vstat; | |
| 1142 | if ((error = VFS_STATVFS(mp, sp, nd->nl_cred)) != 0) | |
| 1143 | return (error); | |
| 1144 | ||
| 1145 | sp->f_flag = 0; | |
| 1146 | if (mp->mnt_flag & MNT_RDONLY) | |
| 1147 | sp->f_flag |= ST_RDONLY; | |
| 1148 | if (mp->mnt_flag & MNT_NOSUID) | |
| 1149 | sp->f_flag |= ST_NOSUID; | |
| 1150 | bcopy(sp, buf, sizeof(*buf)); | |
| 1151 | return (0); | |
| 1152 | } | |
| 1153 | ||
| 1154 | /* | |
| 1155 | * statfs_args(char *path, struct statfs *buf) | |
| 1156 | * | |
| 1157 | * Get filesystem statistics. | |
| 1158 | */ | |
| 1159 | int | |
| 1160 | sys_statvfs(struct statvfs_args *uap) | |
| 1161 | { | |
| 1162 | struct nlookupdata nd; | |
| 1163 | struct statvfs buf; | |
| 1164 | int error; | |
| 1165 | ||
| 1166 | error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); | |
| 1167 | if (error == 0) | |
| 1168 | error = kern_statvfs(&nd, &buf); | |
| 1169 | nlookup_done(&nd); | |
| 1170 | if (error == 0) | |
| 1171 | error = copyout(&buf, uap->buf, sizeof(*uap->buf)); | |
| 1172 | return (error); | |
| 1173 | } | |
| 1174 | ||
| 1175 | int | |
| 1176 | kern_fstatvfs(int fd, struct statvfs *buf) | |
| 1177 | { | |
| 1178 | struct thread *td = curthread; | |
| 1179 | struct proc *p = td->td_proc; | |
| 1180 | struct file *fp; | |
| 1181 | struct mount *mp; | |
| 1182 | struct statvfs *sp; | |
| 1183 | int error; | |
| 1184 | ||
| 1185 | KKASSERT(p); | |
| 1186 | if ((error = holdvnode(p->p_fd, fd, &fp)) != 0) | |
| 1187 | return (error); | |
| 1188 | mp = ((struct vnode *)fp->f_data)->v_mount; | |
| 1189 | if (mp == NULL) { | |
| 1190 | error = EBADF; | |
| 1191 | goto done; | |
| 1192 | } | |
| 1193 | if (fp->f_cred == NULL) { | |
| 1194 | error = EINVAL; | |
| 1195 | goto done; | |
| 1196 | } | |
| 1197 | sp = &mp->mnt_vstat; | |
| 1198 | if ((error = VFS_STATVFS(mp, sp, fp->f_cred)) != 0) | |
| 1199 | goto done; | |
| 1200 | ||
| 1201 | sp->f_flag = 0; | |
| 1202 | if (mp->mnt_flag & MNT_RDONLY) | |
| 1203 | sp->f_flag |= ST_RDONLY; | |
| 1204 | if (mp->mnt_flag & MNT_NOSUID) | |
| 1205 | sp->f_flag |= ST_NOSUID; | |
| 1206 | ||
| 1207 | bcopy(sp, buf, sizeof(*buf)); | |
| 1208 | error = 0; | |
| 1209 | done: | |
| 1210 | fdrop(fp); | |
| 1211 | return (error); | |
| 1212 | } | |
| 1213 | ||
| 1214 | /* | |
| 1215 | * fstatfs_args(int fd, struct statfs *buf) | |
| 1216 | * | |
| 1217 | * Get filesystem statistics. | |
| 1218 | */ | |
| 1219 | int | |
| 1220 | sys_fstatvfs(struct fstatvfs_args *uap) | |
| 1221 | { | |
| 1222 | struct statvfs buf; | |
| 1223 | int error; | |
| 1224 | ||
| 1225 | error = kern_fstatvfs(uap->fd, &buf); | |
| 1226 | ||
| 1227 | if (error == 0) | |
| 1228 | error = copyout(&buf, uap->buf, sizeof(*uap->buf)); | |
| 1229 | return (error); | |
| 1230 | } | |
| 1231 | ||
| 984263bc | 1232 | /* |
| 41c20dac MD |
1233 | * getfsstat_args(struct statfs *buf, long bufsize, int flags) |
| 1234 | * | |
| 984263bc MD |
1235 | * Get statistics on all filesystems. |
| 1236 | */ | |
| 861905fb MD |
1237 | |
| 1238 | struct getfsstat_info { | |
| 1239 | struct statfs *sfsp; | |
| 1240 | long count; | |
| 1241 | long maxcount; | |
| 1242 | int error; | |
| 1243 | int flags; | |
| 861905fb MD |
1244 | struct proc *p; |
| 1245 | }; | |
| 1246 | ||
| 1247 | static int getfsstat_callback(struct mount *, void *); | |
| 1248 | ||
| 41c20dac | 1249 | /* ARGSUSED */ |
| 984263bc | 1250 | int |
| 753fd850 | 1251 | sys_getfsstat(struct getfsstat_args *uap) |
| 984263bc | 1252 | { |
| dadab5e9 | 1253 | struct thread *td = curthread; |
| 75ffff0d | 1254 | struct proc *p = td->td_proc; |
| 861905fb | 1255 | struct getfsstat_info info; |
| 75ffff0d | 1256 | |
| 861905fb | 1257 | bzero(&info, sizeof(info)); |
| 861905fb MD |
1258 | |
| 1259 | info.maxcount = uap->bufsize / sizeof(struct statfs); | |
| 1260 | info.sfsp = uap->buf; | |
| 1261 | info.count = 0; | |
| 1262 | info.flags = uap->flags; | |
| 861905fb MD |
1263 | info.p = p; |
| 1264 | ||
| 1265 | mountlist_scan(getfsstat_callback, &info, MNTSCAN_FORWARD); | |
| 1266 | if (info.sfsp && info.count > info.maxcount) | |
| 1267 | uap->sysmsg_result = info.maxcount; | |
| 75ffff0d | 1268 | else |
| 861905fb MD |
1269 | uap->sysmsg_result = info.count; |
| 1270 | return (info.error); | |
| 1271 | } | |
| 1272 | ||
| 1273 | static int | |
| 1274 | getfsstat_callback(struct mount *mp, void *data) | |
| 1275 | { | |
| 1276 | struct getfsstat_info *info = data; | |
| 1277 | struct statfs *sp; | |
| 1278 | char *freepath; | |
| 1279 | char *fullpath; | |
| 1280 | int error; | |
| 1281 | ||
| 1282 | if (info->sfsp && info->count < info->maxcount) { | |
| 28623bf9 | 1283 | if (info->p && !chroot_visible_mnt(mp, info->p)) |
| 861905fb MD |
1284 | return(0); |
| 1285 | sp = &mp->mnt_stat; | |
| 1286 | ||
| 1287 | /* | |
| 1288 | * If MNT_NOWAIT or MNT_LAZY is specified, do not | |
| 1289 | * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY | |
| 1290 | * overrides MNT_WAIT. | |
| 1291 | */ | |
| 1292 | if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 || | |
| 1293 | (info->flags & MNT_WAIT)) && | |
| acde96db | 1294 | (error = VFS_STATFS(mp, sp, info->p->p_ucred))) { |
| 861905fb MD |
1295 | return(0); |
| 1296 | } | |
| 1297 | sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK; | |
| 1298 | ||
| d3c546e6 | 1299 | error = mount_path(info->p, mp, &fullpath, &freepath); |
| 861905fb MD |
1300 | if (error) { |
| 1301 | info->error = error; | |
| 1302 | return(-1); | |
| 984263bc | 1303 | } |
| 861905fb MD |
1304 | bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); |
| 1305 | strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname)); | |
| efda3bd0 | 1306 | kfree(freepath, M_TEMP); |
| 861905fb MD |
1307 | |
| 1308 | error = copyout(sp, info->sfsp, sizeof(*sp)); | |
| 1309 | if (error) { | |
| 1310 | info->error = error; | |
| 1311 | return (-1); | |
| 984263bc | 1312 | } |
| 861905fb | 1313 | ++info->sfsp; |
| 984263bc | 1314 | } |
| 861905fb MD |
1315 | info->count++; |
| 1316 | return(0); | |
| 984263bc MD |
1317 | } |
| 1318 | ||
| 1319 | /* | |
| 00fe9d48 MD |
1320 | * getvfsstat_args(struct statfs *buf, struct statvfs *vbuf, |
| 1321 | long bufsize, int flags) | |
| 1322 | * | |
| 1323 | * Get statistics on all filesystems. | |
| 1324 | */ | |
| 1325 | ||
| 1326 | struct getvfsstat_info { | |
| 1327 | struct statfs *sfsp; | |
| 1328 | struct statvfs *vsfsp; | |
| 1329 | long count; | |
| 1330 | long maxcount; | |
| 1331 | int error; | |
| 1332 | int flags; | |
| 1333 | struct proc *p; | |
| 1334 | }; | |
| 1335 | ||
| 1336 | static int getvfsstat_callback(struct mount *, void *); | |
| 1337 | ||
| 1338 | /* ARGSUSED */ | |
| 1339 | int | |
| 1340 | sys_getvfsstat(struct getvfsstat_args *uap) | |
| 1341 | { | |
| 1342 | struct thread *td = curthread; | |
| 1343 | struct proc *p = td->td_proc; | |
| 1344 | struct getvfsstat_info info; | |
| 1345 | ||
| 1346 | bzero(&info, sizeof(info)); | |
| 1347 | ||
| 1348 | info.maxcount = uap->vbufsize / sizeof(struct statvfs); | |
| 1349 | info.sfsp = uap->buf; | |
| 1350 | info.vsfsp = uap->vbuf; | |
| 1351 | info.count = 0; | |
| 1352 | info.flags = uap->flags; | |
| 1353 | info.p = p; | |
| 1354 | ||
| 1355 | mountlist_scan(getvfsstat_callback, &info, MNTSCAN_FORWARD); | |
| 1356 | if (info.vsfsp && info.count > info.maxcount) | |
| 1357 | uap->sysmsg_result = info.maxcount; | |
| 1358 | else | |
| 1359 | uap->sysmsg_result = info.count; | |
| 1360 | return (info.error); | |
| 1361 | } | |
| 1362 | ||
| 1363 | static int | |
| 1364 | getvfsstat_callback(struct mount *mp, void *data) | |
| 1365 | { | |
| 1366 | struct getvfsstat_info *info = data; | |
| 1367 | struct statfs *sp; | |
| 1368 | struct statvfs *vsp; | |
| 1369 | char *freepath; | |
| 1370 | char *fullpath; | |
| 1371 | int error; | |
| 1372 | ||
| 1373 | if (info->vsfsp && info->count < info->maxcount) { | |
| 1374 | if (info->p && !chroot_visible_mnt(mp, info->p)) | |
| 1375 | return(0); | |
| 1376 | sp = &mp->mnt_stat; | |
| 1377 | vsp = &mp->mnt_vstat; | |
| 1378 | ||
| 1379 | /* | |
| 1380 | * If MNT_NOWAIT or MNT_LAZY is specified, do not | |
| 1381 | * refresh the fsstat cache. MNT_NOWAIT or MNT_LAZY | |
| 1382 | * overrides MNT_WAIT. | |
| 1383 | */ | |
| 1384 | if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 || | |
| 1385 | (info->flags & MNT_WAIT)) && | |
| 1386 | (error = VFS_STATFS(mp, sp, info->p->p_ucred))) { | |
| 1387 | return(0); | |
| 1388 | } | |
| 1389 | sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK; | |
| 1390 | ||
| 1391 | if (((info->flags & (MNT_LAZY|MNT_NOWAIT)) == 0 || | |
| 1392 | (info->flags & MNT_WAIT)) && | |
| 1393 | (error = VFS_STATVFS(mp, vsp, info->p->p_ucred))) { | |
| 1394 | return(0); | |
| 1395 | } | |
| 1396 | vsp->f_flag = 0; | |
| 1397 | if (mp->mnt_flag & MNT_RDONLY) | |
| 1398 | vsp->f_flag |= ST_RDONLY; | |
| 1399 | if (mp->mnt_flag & MNT_NOSUID) | |
| 1400 | vsp->f_flag |= ST_NOSUID; | |
| 1401 | ||
| 1402 | error = mount_path(info->p, mp, &fullpath, &freepath); | |
| 1403 | if (error) { | |
| 1404 | info->error = error; | |
| 1405 | return(-1); | |
| 1406 | } | |
| 1407 | bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); | |
| 1408 | strlcpy(sp->f_mntonname, fullpath, sizeof(sp->f_mntonname)); | |
| 1409 | kfree(freepath, M_TEMP); | |
| 1410 | ||
| 1411 | error = copyout(sp, info->sfsp, sizeof(*sp)); | |
| 1412 | if (error == 0) | |
| 1413 | error = copyout(vsp, info->vsfsp, sizeof(*vsp)); | |
| 1414 | if (error) { | |
| 1415 | info->error = error; | |
| 1416 | return (-1); | |
| 1417 | } | |
| 1418 | ++info->sfsp; | |
| 1419 | ++info->vsfsp; | |
| 1420 | } | |
| 1421 | info->count++; | |
| 1422 | return(0); | |
| 1423 | } | |
| 1424 | ||
| 1425 | ||
| 1426 | /* | |
| 41c20dac MD |
1427 | * fchdir_args(int fd) |
| 1428 | * | |
| 984263bc MD |
1429 | * Change current working directory to a given file descriptor. |
| 1430 | */ | |
| 984263bc MD |
1431 | /* ARGSUSED */ |
| 1432 | int | |
| 753fd850 | 1433 | sys_fchdir(struct fchdir_args *uap) |
| 984263bc | 1434 | { |
| dadab5e9 MD |
1435 | struct thread *td = curthread; |
| 1436 | struct proc *p = td->td_proc; | |
| 41c20dac | 1437 | struct filedesc *fdp = p->p_fd; |
| 21739618 | 1438 | struct vnode *vp, *ovp; |
| 984263bc MD |
1439 | struct mount *mp; |
| 1440 | struct file *fp; | |
| 28623bf9 | 1441 | struct nchandle nch, onch, tnch; |
| 984263bc MD |
1442 | int error; |
| 1443 | ||
| 5b287bba | 1444 | if ((error = holdvnode(fdp, uap->fd, &fp)) != 0) |
| 984263bc MD |
1445 | return (error); |
| 1446 | vp = (struct vnode *)fp->f_data; | |
| 597aea93 | 1447 | vref(vp); |
| ca466bae | 1448 | vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); |
| 28623bf9 | 1449 | if (vp->v_type != VDIR || fp->f_nchandle.ncp == NULL) |
| 984263bc MD |
1450 | error = ENOTDIR; |
| 1451 | else | |
| 87de5057 | 1452 | error = VOP_ACCESS(vp, VEXEC, p->p_ucred); |
| 984263bc MD |
1453 | if (error) { |
| 1454 | vput(vp); | |
| 5b287bba | 1455 | fdrop(fp); |
| 984263bc MD |
1456 | return (error); |
| 1457 | } | |
| 28623bf9 | 1458 | cache_copy(&fp->f_nchandle, &nch); |
| 1d505369 MD |
1459 | |
| 1460 | /* | |
| 1461 | * If the ncp has become a mount point, traverse through | |
| 1462 | * the mount point. | |
| 1463 | */ | |
| 1464 | ||
| 28623bf9 MD |
1465 | while (!error && (nch.ncp->nc_flag & NCF_ISMOUNTPT) && |
| 1466 | (mp = cache_findmount(&nch)) != NULL | |
| 1d505369 | 1467 | ) { |
| 28623bf9 | 1468 | error = nlookup_mp(mp, &tnch); |
| 21739618 | 1469 | if (error == 0) { |
| 28623bf9 | 1470 | cache_unlock(&tnch); /* leave ref intact */ |
| 21739618 | 1471 | vput(vp); |
| 28623bf9 | 1472 | vp = tnch.ncp->nc_vp; |
| 87de5057 | 1473 | error = vget(vp, LK_SHARED); |
| 21739618 | 1474 | KKASSERT(error == 0); |
| 28623bf9 MD |
1475 | cache_drop(&nch); |
| 1476 | nch = tnch; | |
| 21739618 MD |
1477 | } |
| 1478 | } | |
| 1479 | if (error == 0) { | |
| 1480 | ovp = fdp->fd_cdir; | |
| 28623bf9 | 1481 | onch = fdp->fd_ncdir; |
| a11aaa81 | 1482 | vn_unlock(vp); /* leave ref intact */ |
| 21739618 | 1483 | fdp->fd_cdir = vp; |
| 28623bf9 MD |
1484 | fdp->fd_ncdir = nch; |
| 1485 | cache_drop(&onch); | |
| 21739618 MD |
1486 | vrele(ovp); |
| 1487 | } else { | |
| 28623bf9 | 1488 | cache_drop(&nch); |
| 21739618 MD |
1489 | vput(vp); |
| 1490 | } | |
| 5b287bba | 1491 | fdrop(fp); |
| 21739618 | 1492 | return (error); |
| 984263bc MD |
1493 | } |
| 1494 | ||
| 9697c509 | 1495 | int |
| 21739618 | 1496 | kern_chdir(struct nlookupdata *nd) |
| 9697c509 DRJ |
1497 | { |
| 1498 | struct thread *td = curthread; | |
| 1499 | struct proc *p = td->td_proc; | |
| 1500 | struct filedesc *fdp = p->p_fd; | |
| 21739618 | 1501 | struct vnode *vp, *ovp; |
| 28623bf9 | 1502 | struct nchandle onch; |
| 9697c509 DRJ |
1503 | int error; |
| 1504 | ||
| 21739618 | 1505 | if ((error = nlookup(nd)) != 0) |
| 9697c509 | 1506 | return (error); |
| 28623bf9 | 1507 | if ((vp = nd->nl_nch.ncp->nc_vp) == NULL) |
| 21739618 | 1508 | return (ENOENT); |
| 87de5057 | 1509 | if ((error = vget(vp, LK_SHARED)) != 0) |
| 21739618 MD |
1510 | return (error); |
| 1511 | ||
| 1512 | error = checkvp_chdir(vp, td); | |
| a11aaa81 | 1513 | vn_unlock(vp); |
| 21739618 MD |
1514 | if (error == 0) { |
| 1515 | ovp = fdp->fd_cdir; | |
| 28623bf9 MD |
1516 | onch = fdp->fd_ncdir; |
| 1517 | cache_unlock(&nd->nl_nch); /* leave reference intact */ | |
| 1518 | fdp->fd_ncdir = nd->nl_nch; | |
| 21739618 | 1519 | fdp->fd_cdir = vp; |
| 28623bf9 | 1520 | cache_drop(&onch); |
| 21739618 | 1521 | vrele(ovp); |
| 28623bf9 | 1522 | cache_zero(&nd->nl_nch); |
| 21739618 MD |
1523 | } else { |
| 1524 | vrele(vp); | |
| e24b948e | 1525 | } |
| e24b948e | 1526 | return (error); |
| 9697c509 DRJ |
1527 | } |
| 1528 | ||
| 984263bc | 1529 | /* |
| 41c20dac MD |
1530 | * chdir_args(char *path) |
| 1531 | * | |
| 984263bc MD |
1532 | * Change current working directory (``.''). |
| 1533 | */ | |
| 984263bc | 1534 | int |
| 753fd850 | 1535 | sys_chdir(struct chdir_args *uap) |
| 984263bc | 1536 | { |
| 21739618 | 1537 | struct nlookupdata nd; |
| 9697c509 | 1538 | int error; |
| 984263bc | 1539 | |
| 21739618 | 1540 | error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); |
| fad57d0e | 1541 | if (error == 0) |
| 21739618 | 1542 | error = kern_chdir(&nd); |
| fad57d0e | 1543 | nlookup_done(&nd); |
| 9697c509 | 1544 | return (error); |
| 984263bc MD |
1545 | } |
| 1546 | ||
| 1547 | /* | |
| 1548 | * Helper function for raised chroot(2) security function: Refuse if | |
| 1549 | * any filedescriptors are open directories. | |
| 1550 | */ | |
| 1551 | static int | |
| c972a82f | 1552 | chroot_refuse_vdir_fds(struct filedesc *fdp) |
| 984263bc MD |
1553 | { |
| 1554 | struct vnode *vp; | |
| 1555 | struct file *fp; | |
| 1556 | int error; | |
| 1557 | int fd; | |
| 1558 | ||
| 1559 | for (fd = 0; fd < fdp->fd_nfiles ; fd++) { | |
| 5b287bba | 1560 | if ((error = holdvnode(fdp, fd, &fp)) != 0) |
| 984263bc MD |
1561 | continue; |
| 1562 | vp = (struct vnode *)fp->f_data; | |
| 5b287bba MD |
1563 | if (vp->v_type != VDIR) { |
| 1564 | fdrop(fp); | |
| 984263bc | 1565 | continue; |
| 5b287bba MD |
1566 | } |
| 1567 | fdrop(fp); | |
| 984263bc MD |
1568 | return(EPERM); |
| 1569 | } | |
| 1570 | return (0); | |
| 1571 | } | |
| 1572 | ||
| 1573 | /* | |
| 1574 | * This sysctl determines if we will allow a process to chroot(2) if it | |
| 1575 | * has a directory open: | |
| 1576 | * 0: disallowed for all processes. | |
| 1577 | * 1: allowed for processes that were not already chroot(2)'ed. | |
| 1578 | * 2: allowed for all processes. | |
| 1579 | */ | |
| 1580 | ||
| 1581 | static int chroot_allow_open_directories = 1; | |
| 1582 | ||
| 1583 | SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW, | |
| 1584 | &chroot_allow_open_directories, 0, ""); | |
| 1585 | ||
| 1586 | /* | |
| 21739618 MD |
1587 | * chroot to the specified namecache entry. We obtain the vp from the |
| 1588 | * namecache data. The passed ncp must be locked and referenced and will | |
| 1589 | * remain locked and referenced on return. | |
| e24b948e | 1590 | */ |
| 53dd6631 | 1591 | int |
| 28623bf9 | 1592 | kern_chroot(struct nchandle *nch) |
| e24b948e MD |
1593 | { |
| 1594 | struct thread *td = curthread; | |
| 1595 | struct proc *p = td->td_proc; | |
| 1596 | struct filedesc *fdp = p->p_fd; | |
| 21739618 | 1597 | struct vnode *vp; |
| e24b948e MD |
1598 | int error; |
| 1599 | ||
| 1600 | /* | |
| ca3cd02d | 1601 | * Only privileged user can chroot |
| e24b948e | 1602 | */ |
| 3a591c90 MN |
1603 | error = priv_check_cred(p->p_ucred, PRIV_VFS_CHROOT, 0); |
| 1604 | if (error) | |
| e24b948e MD |
1605 | return (error); |
| 1606 | ||
| 1607 | /* | |
| 1608 | * Disallow open directory descriptors (fchdir() breakouts). | |
| 1609 | */ | |
| 1610 | if (chroot_allow_open_directories == 0 || | |
| 1611 | (chroot_allow_open_directories == 1 && fdp->fd_rdir != rootvnode)) { | |
| 1612 | if ((error = chroot_refuse_vdir_fds(fdp)) != 0) | |
| 1613 | return (error); | |
| 1614 | } | |
| 28623bf9 | 1615 | if ((vp = nch->ncp->nc_vp) == NULL) |
| 21739618 MD |
1616 | return (ENOENT); |
| 1617 | ||
| 87de5057 | 1618 | if ((error = vget(vp, LK_SHARED)) != 0) |
| 21739618 | 1619 | return (error); |
| e24b948e MD |
1620 | |
| 1621 | /* | |
| 1622 | * Check the validity of vp as a directory to change to and | |
| 1623 | * associate it with rdir/jdir. | |
| 1624 | */ | |
| 21739618 | 1625 | error = checkvp_chdir(vp, td); |
| a11aaa81 | 1626 | vn_unlock(vp); /* leave reference intact */ |
| 21739618 | 1627 | if (error == 0) { |
| e24b948e | 1628 | vrele(fdp->fd_rdir); |
| 21739618 | 1629 | fdp->fd_rdir = vp; /* reference inherited by fd_rdir */ |
| 28623bf9 MD |
1630 | cache_drop(&fdp->fd_nrdir); |
| 1631 | cache_copy(nch, &fdp->fd_nrdir); | |
| e24b948e MD |
1632 | if (fdp->fd_jdir == NULL) { |
| 1633 | fdp->fd_jdir = vp; | |
| 597aea93 | 1634 | vref(fdp->fd_jdir); |
| 28623bf9 | 1635 | cache_copy(nch, &fdp->fd_njdir); |
| e24b948e | 1636 | } |
| 21739618 MD |
1637 | } else { |
| 1638 | vrele(vp); | |
| e24b948e MD |
1639 | } |
| 1640 | return (error); | |
| 1641 | } | |
| 1642 | ||
| 1643 | /* | |
| 41c20dac MD |
1644 | * chroot_args(char *path) |
| 1645 | * | |
| 984263bc MD |
1646 | * Change notion of root (``/'') directory. |
| 1647 | */ | |
| 984263bc MD |
1648 | /* ARGSUSED */ |
| 1649 | int | |
| 753fd850 | 1650 | sys_chroot(struct chroot_args *uap) |
| 984263bc | 1651 | { |
| dadab5e9 | 1652 | struct thread *td = curthread; |
| 21739618 | 1653 | struct nlookupdata nd; |
| e24b948e | 1654 | int error; |
| 984263bc | 1655 | |
| e24b948e | 1656 | KKASSERT(td->td_proc); |
| 21739618 | 1657 | error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); |
| 53dd6631 JS |
1658 | if (error) { |
| 1659 | nlookup_done(&nd); | |
| 1660 | return(error); | |
| 1661 | } | |
| 3a907475 | 1662 | nd.nl_flags |= NLC_EXEC; |
| 53dd6631 JS |
1663 | error = nlookup(&nd); |
| 1664 | if (error == 0) | |
| 28623bf9 | 1665 | error = kern_chroot(&nd.nl_nch); |
| fad57d0e | 1666 | nlookup_done(&nd); |
| 53dd6631 | 1667 | return(error); |
| 984263bc MD |
1668 | } |
| 1669 | ||
| 1670 | /* | |
| e24b948e MD |
1671 | * Common routine for chroot and chdir. Given a locked, referenced vnode, |
| 1672 | * determine whether it is legal to chdir to the vnode. The vnode's state | |
| 1673 | * is not changed by this call. | |
| 984263bc | 1674 | */ |
| e24b948e MD |
1675 | int |
| 1676 | checkvp_chdir(struct vnode *vp, struct thread *td) | |
| 984263bc | 1677 | { |
| 984263bc MD |
1678 | int error; |
| 1679 | ||
| 984263bc MD |
1680 | if (vp->v_type != VDIR) |
| 1681 | error = ENOTDIR; | |
| 1682 | else | |
| 87de5057 | 1683 | error = VOP_ACCESS(vp, VEXEC, td->td_proc->p_ucred); |
| 984263bc MD |
1684 | return (error); |
| 1685 | } | |
| 1686 | ||
| 984263bc | 1687 | int |
| fad57d0e | 1688 | kern_open(struct nlookupdata *nd, int oflags, int mode, int *res) |
| 984263bc | 1689 | { |
| dadab5e9 MD |
1690 | struct thread *td = curthread; |
| 1691 | struct proc *p = td->td_proc; | |
| fbfe4e7d | 1692 | struct lwp *lp = td->td_lwp; |
| 41c20dac | 1693 | struct filedesc *fdp = p->p_fd; |
| 9697c509 | 1694 | int cmode, flags; |
| 984263bc | 1695 | struct file *nfp; |
| fad57d0e MD |
1696 | struct file *fp; |
| 1697 | struct vnode *vp; | |
| 984263bc MD |
1698 | int type, indx, error; |
| 1699 | struct flock lf; | |
| 984263bc | 1700 | |
| 984263bc MD |
1701 | if ((oflags & O_ACCMODE) == O_ACCMODE) |
| 1702 | return (EINVAL); | |
| 1703 | flags = FFLAGS(oflags); | |
| fad57d0e | 1704 | error = falloc(p, &nfp, NULL); |
| 984263bc MD |
1705 | if (error) |
| 1706 | return (error); | |
| 1707 | fp = nfp; | |
| 3a907475 | 1708 | cmode = ((mode &~ fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT; |
| fad57d0e | 1709 | |
| 984263bc | 1710 | /* |
| fad57d0e MD |
1711 | * XXX p_dupfd is a real mess. It allows a device to return a |
| 1712 | * file descriptor to be duplicated rather then doing the open | |
| 1713 | * itself. | |
| 984263bc | 1714 | */ |
| fbfe4e7d | 1715 | lp->lwp_dupfd = -1; |
| 21739618 MD |
1716 | |
| 1717 | /* | |
| fad57d0e MD |
1718 | * Call vn_open() to do the lookup and assign the vnode to the |
| 1719 | * file pointer. vn_open() does not change the ref count on fp | |
| 1720 | * and the vnode, on success, will be inherited by the file pointer | |
| 1721 | * and unlocked. | |
| 21739618 | 1722 | */ |
| fad57d0e MD |
1723 | nd->nl_flags |= NLC_LOCKVP; |
| 1724 | error = vn_open(nd, fp, flags, cmode); | |
| 1725 | nlookup_done(nd); | |
| 984263bc MD |
1726 | if (error) { |
| 1727 | /* | |
| 984263bc MD |
1728 | * handle special fdopen() case. bleh. dupfdopen() is |
| 1729 | * responsible for dropping the old contents of ofiles[indx] | |
| 1730 | * if it succeeds. | |
| fad57d0e | 1731 | * |
| 259b8ea0 MD |
1732 | * Note that fsetfd() will add a ref to fp which represents |
| 1733 | * the fd_files[] assignment. We must still drop our | |
| 1734 | * reference. | |
| 984263bc | 1735 | */ |
| fbfe4e7d | 1736 | if ((error == ENODEV || error == ENXIO) && lp->lwp_dupfd >= 0) { |
| 259b8ea0 MD |
1737 | if (fdalloc(p, 0, &indx) == 0) { |
| 1738 | error = dupfdopen(p, indx, lp->lwp_dupfd, flags, error); | |
| fad57d0e MD |
1739 | if (error == 0) { |
| 1740 | *res = indx; | |
| 9f87144f | 1741 | fdrop(fp); /* our ref */ |
| fad57d0e MD |
1742 | return (0); |
| 1743 | } | |
| 259b8ea0 | 1744 | fsetfd(p, NULL, indx); |
| fad57d0e | 1745 | } |
| 984263bc | 1746 | } |
| 9f87144f | 1747 | fdrop(fp); /* our ref */ |
| 984263bc MD |
1748 | if (error == ERESTART) |
| 1749 | error = EINTR; | |
| 1750 | return (error); | |
| 1751 | } | |
| fad57d0e MD |
1752 | |
| 1753 | /* | |
| 1754 | * ref the vnode for ourselves so it can't be ripped out from under | |
| 1755 | * is. XXX need an ND flag to request that the vnode be returned | |
| 1756 | * anyway. | |
| 259b8ea0 MD |
1757 | * |
| 1758 | * Reserve a file descriptor but do not assign it until the open | |
| 1759 | * succeeds. | |
| fad57d0e MD |
1760 | */ |
| 1761 | vp = (struct vnode *)fp->f_data; | |
| 1762 | vref(vp); | |
| 259b8ea0 | 1763 | if ((error = fdalloc(p, 0, &indx)) != 0) { |
| 9f87144f | 1764 | fdrop(fp); |
| fad57d0e MD |
1765 | vrele(vp); |
| 1766 | return (error); | |
| 1767 | } | |
| 1768 | ||
| 1769 | /* | |
| 1770 | * If no error occurs the vp will have been assigned to the file | |
| 1771 | * pointer. | |
| 1772 | */ | |
| fbfe4e7d | 1773 | lp->lwp_dupfd = 0; |
| 984263bc | 1774 | |
| 984263bc MD |
1775 | if (flags & (O_EXLOCK | O_SHLOCK)) { |
| 1776 | lf.l_whence = SEEK_SET; | |
| 1777 | lf.l_start = 0; | |
| 1778 | lf.l_len = 0; | |
| 1779 | if (flags & O_EXLOCK) | |
| 1780 | lf.l_type = F_WRLCK; | |
| 1781 | else | |
| 1782 | lf.l_type = F_RDLCK; | |
| 71c18fe3 MD |
1783 | if (flags & FNONBLOCK) |
| 1784 | type = 0; | |
| 1785 | else | |
| 1786 | type = F_WAIT; | |
| fad57d0e | 1787 | |
| 984263bc MD |
1788 | if ((error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type)) != 0) { |
| 1789 | /* | |
| 259b8ea0 MD |
1790 | * lock request failed. Clean up the reserved |
| 1791 | * descriptor. | |
| 984263bc | 1792 | */ |
| fad57d0e | 1793 | vrele(vp); |
| 259b8ea0 | 1794 | fsetfd(p, NULL, indx); |
| 9f87144f | 1795 | fdrop(fp); |
| 984263bc MD |
1796 | return (error); |
| 1797 | } | |
| 984263bc MD |
1798 | fp->f_flag |= FHASLOCK; |
| 1799 | } | |
| 7540ab49 MD |
1800 | #if 0 |
| 1801 | /* | |
| 1802 | * Assert that all regular file vnodes were created with a object. | |
| 1803 | */ | |
| 1804 | KASSERT(vp->v_type != VREG || vp->v_object != NULL, | |
| 1805 | ("open: regular file has no backing object after vn_open")); | |
| 1806 | #endif | |
| 984263bc | 1807 | |
| fad57d0e | 1808 | vrele(vp); |
| 21739618 MD |
1809 | |
| 1810 | /* | |
| 984263bc MD |
1811 | * release our private reference, leaving the one associated with the |
| 1812 | * descriptor table intact. | |
| 1813 | */ | |
| 259b8ea0 | 1814 | fsetfd(p, fp, indx); |
| 9f87144f | 1815 | fdrop(fp); |
| 9697c509 | 1816 | *res = indx; |
| 984263bc MD |
1817 | return (0); |
| 1818 | } | |
| 1819 | ||
| 984263bc | 1820 | /* |
| 9697c509 | 1821 | * open_args(char *path, int flags, int mode) |
| 41c20dac | 1822 | * |
| 9697c509 DRJ |
1823 | * Check permissions, allocate an open file structure, |
| 1824 | * and call the device open routine if any. | |
| 984263bc | 1825 | */ |
| 984263bc | 1826 | int |
| 753fd850 | 1827 | sys_open(struct open_args *uap) |
| 984263bc | 1828 | { |
| fad57d0e | 1829 | struct nlookupdata nd; |
| 9697c509 DRJ |
1830 | int error; |
| 1831 | ||
| 85dbab7f | 1832 | error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0); |
| fad57d0e MD |
1833 | if (error == 0) { |
| 1834 | error = kern_open(&nd, uap->flags, | |
| 1835 | uap->mode, &uap->sysmsg_result); | |
| 1836 | } | |
| 1837 | nlookup_done(&nd); | |
| 9697c509 | 1838 | return (error); |
| 984263bc | 1839 | } |
| 984263bc | 1840 | |
| 83a11774 NT |
1841 | /* |
| 1842 | * openat_args(int fd, char *path, int flags, int mode) | |
| 1843 | */ | |
| 1844 | int | |
| 1845 | sys_openat(struct openat_args *uap) | |
| 1846 | { | |
| 83a11774 NT |
1847 | struct nlookupdata nd; |
| 1848 | int error; | |
| 1db695af | 1849 | struct file *fp; |
| 83a11774 | 1850 | |
| 1db695af NT |
1851 | error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, UIO_USERSPACE, 0); |
| 1852 | if (error == 0) { | |
| 1853 | error = kern_open(&nd, uap->flags, uap->mode, | |
| 1854 | &uap->sysmsg_result); | |
| 83a11774 | 1855 | } |
| 1db695af | 1856 | nlookup_done_at(&nd, fp); |
| 83a11774 NT |
1857 | return (error); |
| 1858 | } | |
| 1859 | ||
| 984263bc | 1860 | int |
| 0e9b9130 | 1861 | kern_mknod(struct nlookupdata *nd, int mode, int rmajor, int rminor) |
| 984263bc | 1862 | { |
| dadab5e9 MD |
1863 | struct thread *td = curthread; |
| 1864 | struct proc *p = td->td_proc; | |
| 41c20dac | 1865 | struct vnode *vp; |
| 984263bc MD |
1866 | struct vattr vattr; |
| 1867 | int error; | |
| 1868 | int whiteout = 0; | |
| 984263bc | 1869 | |
| dadab5e9 MD |
1870 | KKASSERT(p); |
| 1871 | ||
| fad57d0e MD |
1872 | VATTR_NULL(&vattr); |
| 1873 | vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask; | |
| 0e9b9130 MD |
1874 | vattr.va_rmajor = rmajor; |
| 1875 | vattr.va_rminor = rminor; | |
| fad57d0e MD |
1876 | |
| 1877 | switch (mode & S_IFMT) { | |
| 1878 | case S_IFMT: /* used by badsect to flag bad sectors */ | |
| 3a591c90 | 1879 | error = priv_check_cred(p->p_ucred, PRIV_VFS_MKNOD_BAD, 0); |
| fad57d0e MD |
1880 | vattr.va_type = VBAD; |
| 1881 | break; | |
| 1882 | case S_IFCHR: | |
| 44a36778 | 1883 | error = priv_check(td, PRIV_VFS_MKNOD_DEV); |
| fad57d0e MD |
1884 | vattr.va_type = VCHR; |
| 1885 | break; | |
| 1886 | case S_IFBLK: | |
| 44a36778 | 1887 | error = priv_check(td, PRIV_VFS_MKNOD_DEV); |
| fad57d0e MD |
1888 | vattr.va_type = VBLK; |
| 1889 | break; | |
| 1890 | case S_IFWHT: | |
| 3a591c90 | 1891 | error = priv_check_cred(p->p_ucred, PRIV_VFS_MKNOD_WHT, 0); |
| fad57d0e MD |
1892 | whiteout = 1; |
| 1893 | break; | |
| 44a36778 | 1894 | case S_IFDIR: /* special directories support for HAMMER */ |
| 3a591c90 | 1895 | error = priv_check_cred(p->p_ucred, PRIV_VFS_MKNOD_DIR, 0); |
| bed9e372 MD |
1896 | vattr.va_type = VDIR; |
| 1897 | break; | |
| fad57d0e MD |
1898 | default: |
| 1899 | error = EINVAL; | |
| 1900 | break; | |
| 984263bc | 1901 | } |
| 44a36778 MN |
1902 | |
| 1903 | if (error) | |
| 1904 | return (error); | |
| 1905 | ||
| 1906 | bwillinode(1); | |
| 1907 | nd->nl_flags |= NLC_CREATE | NLC_REFDVP; | |
| 1908 | if ((error = nlookup(nd)) != 0) | |
| 1909 | return (error); | |
| 1910 | if (nd->nl_nch.ncp->nc_vp) | |
| 1911 | return (EEXIST); | |
| 1912 | if ((error = ncp_writechk(&nd->nl_nch)) != 0) | |
| 1913 | return (error); | |
| 1914 | ||
| 1915 | if (whiteout) { | |
| 1916 | error = VOP_NWHITEOUT(&nd->nl_nch, nd->nl_dvp, | |
| 1917 | nd->nl_cred, NAMEI_CREATE); | |
| 1918 | } else { | |
| 1919 | vp = NULL; | |
| 1920 | error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, | |
| 1921 | &vp, nd->nl_cred, &vattr); | |
| 1922 | if (error == 0) | |
| 1923 | vput(vp); | |
| 984263bc | 1924 | } |
| 9697c509 DRJ |
1925 | return (error); |
| 1926 | } | |
| 1927 | ||
| 1928 | /* | |
| 1929 | * mknod_args(char *path, int mode, int dev) | |
| 1930 | * | |
| 1931 | * Create a special file. | |
| 1932 | */ | |
| 1933 | int | |
| 753fd850 | 1934 | sys_mknod(struct mknod_args *uap) |
| 9697c509 | 1935 | { |
| fad57d0e | 1936 | struct nlookupdata nd; |
| 9697c509 DRJ |
1937 | int error; |
| 1938 | ||
| fad57d0e | 1939 | error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0); |
| 0e9b9130 MD |
1940 | if (error == 0) { |
| 1941 | error = kern_mknod(&nd, uap->mode, | |
| 1942 | umajor(uap->dev), uminor(uap->dev)); | |
| 1943 | } | |
| fad57d0e | 1944 | nlookup_done(&nd); |
| 984263bc MD |
1945 | return (error); |
| 1946 | } | |
| 1947 | ||
| 984263bc | 1948 | int |
| fad57d0e | 1949 | kern_mkfifo(struct nlookupdata *nd, int mode) |
| 984263bc | 1950 | { |
| dadab5e9 MD |
1951 | struct thread *td = curthread; |
| 1952 | struct proc *p = td->td_proc; | |
| 984263bc | 1953 | struct vattr vattr; |
| fad57d0e | 1954 | struct vnode *vp; |
| 984263bc | 1955 | int error; |
| 984263bc | 1956 | |
| c4df9635 | 1957 | bwillinode(1); |
| fad57d0e | 1958 | |
| 5312fa43 | 1959 | nd->nl_flags |= NLC_CREATE | NLC_REFDVP; |
| fad57d0e | 1960 | if ((error = nlookup(nd)) != 0) |
| 984263bc | 1961 | return (error); |
| 28623bf9 | 1962 | if (nd->nl_nch.ncp->nc_vp) |
| 984263bc | 1963 | return (EEXIST); |
| 28623bf9 | 1964 | if ((error = ncp_writechk(&nd->nl_nch)) != 0) |
| 72310cfb | 1965 | return (error); |
| fad57d0e | 1966 | |
| 984263bc MD |
1967 | VATTR_NULL(&vattr); |
| 1968 | vattr.va_type = VFIFO; | |
| 136178b3 | 1969 | vattr.va_mode = (mode & ALLPERMS) &~ p->p_fd->fd_cmask; |
| fad57d0e | 1970 | vp = NULL; |
| 5312fa43 | 1971 | error = VOP_NMKNOD(&nd->nl_nch, nd->nl_dvp, &vp, nd->nl_cred, &vattr); |
| 984263bc | 1972 | if (error == 0) |
| fad57d0e | 1973 | vput(vp); |
| 136178b3 DRJ |
1974 | return (error); |
| 1975 | } | |
| 1976 | ||
| 1977 | /* | |
| 1978 | * mkfifo_args(char *path, int mode) | |
| 1979 | * | |
| 1980 | * Create a named pipe. | |
| 1981 | */ | |
| 1982 | int | |
| 753fd850 | 1983 | sys_mkfifo(struct mkfifo_args *uap) |
| 136178b3 | 1984 | { |
| fad57d0e | 1985 | struct nlookupdata nd; |
| 136178b3 DRJ |
1986 | int error; |
| 1987 | ||
| fad57d0e MD |
1988 | error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0); |
| 1989 | if (error == 0) | |
| 1990 | error = kern_mkfifo(&nd, uap->mode); | |
| 1991 | nlookup_done(&nd); | |
| 984263bc MD |
1992 | return (error); |
| 1993 | } | |
| 1994 | ||
| 8c40f81e | 1995 | static int hardlink_check_uid = 0; |
| 43a0f7ae | 1996 | SYSCTL_INT(_security, OID_AUTO, hardlink_check_uid, CTLFLAG_RW, |
| 8c40f81e MD |
1997 | &hardlink_check_uid, 0, |
| 1998 | "Unprivileged processes cannot create hard links to files owned by other " | |
| 1999 | "users"); | |
| 2000 | static int hardlink_check_gid = 0; | |
| 43a0f7ae | 2001 | SYSCTL_INT(_security, OID_AUTO, hardlink_check_gid, CTLFLAG_RW, |
| 8c40f81e MD |
2002 | &hardlink_check_gid, 0, |
| 2003 | "Unprivileged processes cannot create hard links to files owned by other " | |
| 2004 | "groups"); | |
| 2005 | ||
| 2006 | static int | |
| 2007 | can_hardlink(struct vnode *vp, struct thread *td, struct ucred *cred) | |
| 2008 | { | |
| 2009 | struct vattr va; | |
| 2010 | int error; | |
| 2011 | ||
| 2012 | /* | |
| 2013 | * Shortcut if disabled | |
| 2014 | */ | |
| 2015 | if (hardlink_check_uid == 0 && hardlink_check_gid == 0) | |
| 2016 | return (0); | |
| 2017 | ||
| 2018 | /* | |
| f6df0641 | 2019 | * Privileged user can always hardlink |
| 8c40f81e | 2020 | */ |
| f6df0641 | 2021 | if (priv_check_cred(cred, PRIV_VFS_LINK, 0) == 0) |
| 8c40f81e MD |
2022 | return (0); |
| 2023 | ||
| 2024 | /* | |
| 2025 | * Otherwise only if the originating file is owned by the | |
| 2026 | * same user or group. Note that any group is allowed if | |
| 2027 | * the file is owned by the caller. | |
| 2028 | */ | |
| 87de5057 | 2029 | error = VOP_GETATTR(vp, &va); |
| 8c40f81e MD |
2030 | if (error != 0) |
| 2031 | return (error); | |
| 2032 | ||
| 2033 | if (hardlink_check_uid) { | |
| 2034 | if (cred->cr_uid != va.va_uid) | |
| 2035 | return (EPERM); | |
| 2036 | } | |
| 2037 | ||
| 2038 | if (hardlink_check_gid) { | |
| 2039 | if (cred->cr_uid != va.va_uid && !groupmember(va.va_gid, cred)) | |
| 2040 | return (EPERM); | |
| 2041 | } | |
| 2042 | ||
| 2043 | return (0); | |
| 2044 | } | |
| 2045 | ||
| 984263bc | 2046 | int |
| fad57d0e | 2047 | kern_link(struct nlookupdata *nd, struct nlookupdata *linknd) |
| 984263bc | 2048 | { |
| dadab5e9 | 2049 | struct thread *td = curthread; |
| 41c20dac | 2050 | struct vnode *vp; |
| 984263bc MD |
2051 | int error; |
| 2052 | ||
| fad57d0e MD |
2053 | /* |
| 2054 | * Lookup the source and obtained a locked vnode. | |
| 2055 | * | |
| 3a907475 MD |
2056 | * You may only hardlink a file which you have write permission |
| 2057 | * on or which you own. | |
| 2058 | * | |
| fad57d0e MD |
2059 | * XXX relookup on vget failure / race ? |
| 2060 | */ | |
| c4df9635 | 2061 | bwillinode(1); |
| 3a907475 | 2062 | nd->nl_flags |= NLC_WRITE | NLC_OWN | NLC_HLINK; |
| fad57d0e | 2063 | if ((error = nlookup(nd)) != 0) |
| 984263bc | 2064 | return (error); |
| 28623bf9 | 2065 | vp = nd->nl_nch.ncp->nc_vp; |
| fad57d0e | 2066 | KKASSERT(vp != NULL); |
| 984263bc | 2067 | if (vp->v_type == VDIR) |
| fad57d0e | 2068 | return (EPERM); /* POSIX */ |
| 28623bf9 | 2069 | if ((error = ncp_writechk(&nd->nl_nch)) != 0) |
| 72310cfb | 2070 | return (error); |
| 87de5057 | 2071 | if ((error = vget(vp, LK_EXCLUSIVE)) != 0) |
| fad57d0e MD |
2072 | return (error); |
| 2073 | ||
| 2074 | /* | |
| 2075 | * Unlock the source so we can lookup the target without deadlocking | |
| 2076 | * (XXX vp is locked already, possible other deadlock?). The target | |
| 2077 | * must not exist. | |
| 2078 | */ | |
| 2079 | KKASSERT(nd->nl_flags & NLC_NCPISLOCKED); | |
| 2080 | nd->nl_flags &= ~NLC_NCPISLOCKED; | |
| 28623bf9 | 2081 | cache_unlock(&nd->nl_nch); |
| fad57d0e | 2082 | |
| 5312fa43 | 2083 | linknd->nl_flags |= NLC_CREATE | NLC_REFDVP; |
| fad57d0e MD |
2084 | if ((error = nlookup(linknd)) != 0) { |
| 2085 | vput(vp); | |
| 2086 | return (error); | |
| 984263bc | 2087 | } |
| 28623bf9 | 2088 | if (linknd->nl_nch.ncp->nc_vp) { |
| fad57d0e MD |
2089 | vput(vp); |
| 2090 | return (EEXIST); | |
| 2091 | } | |
| 2092 | ||
| 2093 | /* | |
| 2094 | * Finally run the new API VOP. | |
| 2095 | */ | |
| 8c40f81e | 2096 | error = can_hardlink(vp, td, td->td_proc->p_ucred); |
| 5312fa43 MD |
2097 | if (error == 0) { |
| 2098 | error = VOP_NLINK(&linknd->nl_nch, linknd->nl_dvp, | |
| 2099 | vp, linknd->nl_cred); | |
| 2100 | } | |
| fad57d0e | 2101 | vput(vp); |
| 984263bc MD |
2102 | return (error); |
| 2103 | } | |
| 2104 | ||
| 2105 | /* | |
| 9697c509 | 2106 | * link_args(char *path, char *link) |
| 41c20dac | 2107 | * |
| 9697c509 | 2108 | * Make a hard file link. |
| 984263bc | 2109 | */ |
| 984263bc | 2110 | int |
| 753fd850 | 2111 | sys_link(struct link_args *uap) |
| 9697c509 | 2112 | { |
| fad57d0e | 2113 | struct nlookupdata nd, linknd; |
| 9697c509 DRJ |
2114 | int error; |
| 2115 | ||
| fad57d0e MD |
2116 | error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); |
| 2117 | if (error == 0) { | |
| 2118 | error = nlookup_init(&linknd, uap->link, UIO_USERSPACE, 0); | |
| 2119 | if (error == 0) | |
| 2120 | error = kern_link(&nd, &linknd); | |
| 2121 | nlookup_done(&linknd); | |
| 2122 | } | |
| 2123 | nlookup_done(&nd); | |
| 9697c509 DRJ |
2124 | return (error); |
| 2125 | } | |
| 2126 | ||
| 2127 | int | |
| fad57d0e | 2128 | kern_symlink(struct nlookupdata *nd, char *path, int mode) |
| 984263bc MD |
2129 | { |
| 2130 | struct vattr vattr; | |
| fad57d0e | 2131 | struct vnode *vp; |
| dff430ab | 2132 | struct vnode *dvp; |
| 984263bc | 2133 | int error; |
| 984263bc | 2134 | |
| c4df9635 | 2135 | bwillinode(1); |
| 5312fa43 | 2136 | nd->nl_flags |= NLC_CREATE | NLC_REFDVP; |
| fad57d0e | 2137 | if ((error = nlookup(nd)) != 0) |
| 9697c509 | 2138 | return (error); |
| 28623bf9 | 2139 | if (nd->nl_nch.ncp->nc_vp) |
| 9697c509 | 2140 | return (EEXIST); |
| 28623bf9 | 2141 | if ((error = ncp_writechk(&nd->nl_nch)) != 0) |
| 72310cfb | 2142 | return (error); |
| 5312fa43 | 2143 | dvp = nd->nl_dvp; |
| 984263bc | 2144 | VATTR_NULL(&vattr); |
| fad57d0e | 2145 | vattr.va_mode = mode; |
| dff430ab | 2146 | error = VOP_NSYMLINK(&nd->nl_nch, dvp, &vp, nd->nl_cred, &vattr, path); |
| 984263bc | 2147 | if (error == 0) |
| fad57d0e | 2148 | vput(vp); |
| 9697c509 DRJ |
2149 | return (error); |
| 2150 | } | |
| 2151 | ||
| 2152 | /* | |
| 2153 | * symlink(char *path, char *link) | |
| 2154 | * | |
| 2155 | * Make a symbolic link. | |
| 2156 | */ | |
| 2157 | int | |
| 753fd850 | 2158 | sys_symlink(struct symlink_args *uap) |
| 9697c509 DRJ |
2159 | { |
| 2160 | struct thread *td = curthread; | |
| fad57d0e | 2161 | struct nlookupdata nd; |
| 9697c509 DRJ |
2162 | char *path; |
| 2163 | int error; | |
| fad57d0e | 2164 | int mode; |
| 9697c509 | 2165 | |
| 70aac194 | 2166 | path = objcache_get(namei_oc, M_WAITOK); |
| 9697c509 | 2167 | error = copyinstr(uap->path, path, MAXPATHLEN, NULL); |
| e24b948e | 2168 | if (error == 0) { |
| fad57d0e MD |
2169 | error = nlookup_init(&nd, uap->link, UIO_USERSPACE, 0); |
| 2170 | if (error == 0) { | |
| 2171 | mode = ACCESSPERMS & ~td->td_proc->p_fd->fd_cmask; | |
| 2172 | error = kern_symlink(&nd, path, mode); | |
| 2173 | } | |
| 2174 | nlookup_done(&nd); | |
| e24b948e | 2175 | } |
| 70aac194 | 2176 | objcache_put(namei_oc, path); |
| 984263bc MD |
2177 | return (error); |
| 2178 | } | |
| 2179 | ||
| 2180 | /* | |
| 41c20dac MD |
2181 | * undelete_args(char *path) |
| 2182 | * | |
| 984263bc MD |
2183 | * Delete a whiteout from the filesystem. |
| 2184 | */ | |
| 2185 | /* ARGSUSED */ | |
| 2186 | int | |
| 753fd850 | 2187 | sys_undelete(struct undelete_args *uap) |
| 984263bc | 2188 | { |
| fad57d0e | 2189 | struct nlookupdata nd; |
| 984263bc | 2190 | int error; |
| 984263bc | 2191 | |
| ab2eb4eb | 2192 | error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0); |
| c4df9635 | 2193 | bwillinode(1); |
| 5312fa43 | 2194 | nd.nl_flags |= NLC_DELETE | NLC_REFDVP; |
| fad57d0e MD |
2195 | if (error == 0) |
| 2196 | error = nlookup(&nd); | |
| b8997912 | 2197 | if (error == 0) |
| 28623bf9 | 2198 | error = ncp_writechk(&nd.nl_nch); |
| dff430ab | 2199 | if (error == 0) { |
| 5312fa43 MD |
2200 | error = VOP_NWHITEOUT(&nd.nl_nch, nd.nl_dvp, nd.nl_cred, |
| 2201 | NAMEI_DELETE); | |
| dff430ab | 2202 | } |
| fad57d0e | 2203 | nlookup_done(&nd); |
| 984263bc MD |
2204 | return (error); |
| 2205 | } | |
| 2206 | ||
| 984263bc | 2207 | int |
| fad57d0e | 2208 | kern_unlink(struct nlookupdata *nd) |
| 984263bc | 2209 | { |
| 984263bc | 2210 | int error; |
| 984263bc | 2211 | |
| c4df9635 | 2212 | bwillinode(1); |
| 5312fa43 | 2213 | nd->nl_flags |= NLC_DELETE | NLC_REFDVP; |
| fad57d0e | 2214 | if ((error = nlookup(nd)) != 0) |
| 984263bc | 2215 | return (error); |
| 28623bf9 | 2216 | if ((error = ncp_writechk(&nd->nl_nch)) != 0) |
| 72310cfb | 2217 | return (error); |
| 5312fa43 | 2218 | error = VOP_NREMOVE(&nd->nl_nch, nd->nl_dvp, nd->nl_cred); |
| 984263bc MD |
2219 | return (error); |
| 2220 | } | |
| 2221 | ||
| 2222 | /* | |
| 9697c509 | 2223 | * unlink_args(char *path) |
| 41c20dac | 2224 | * |
| 9697c509 | 2225 | * Delete a name from the filesystem. |
| 984263bc | 2226 | */ |
| 984263bc | 2227 | int |
| 753fd850 | 2228 | sys_unlink(struct unlink_args *uap) |
| 9697c509 | 2229 | { |
| fad57d0e | 2230 | struct nlookupdata nd; |
| 9697c509 DRJ |
2231 | int error; |
| 2232 | ||
| fad57d0e MD |
2233 | error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0); |
| 2234 | if (error == 0) | |
| 2235 | error = kern_unlink(&nd); | |
| 2236 | nlookup_done(&nd); | |
| 9697c509 DRJ |
2237 | return (error); |
| 2238 | } | |
| 2239 | ||
| 2240 | int | |
| 5a3fe67d | 2241 | kern_lseek(int fd, off_t offset, int whence, off_t *res) |
| 984263bc | 2242 | { |
| dadab5e9 MD |
2243 | struct thread *td = curthread; |
| 2244 | struct proc *p = td->td_proc; | |
| 41c20dac | 2245 | struct file *fp; |
| 116edfbf | 2246 | struct vnode *vp; |
| 984263bc | 2247 | struct vattr vattr; |
| 116edfbf | 2248 | off_t new_offset; |
| 984263bc MD |
2249 | int error; |
| 2250 | ||
| 228b401d MD |
2251 | fp = holdfp(p->p_fd, fd, -1); |
| 2252 | if (fp == NULL) | |
| 984263bc | 2253 | return (EBADF); |
| 228b401d MD |
2254 | if (fp->f_type != DTYPE_VNODE) { |
| 2255 | error = ESPIPE; | |
| 2256 | goto done; | |
| 2257 | } | |
| 116edfbf | 2258 | vp = (struct vnode *)fp->f_data; |
| 228b401d | 2259 | |
| 9697c509 | 2260 | switch (whence) { |
| 984263bc | 2261 | case L_INCR: |
| 116edfbf | 2262 | new_offset = fp->f_offset + offset; |
| 228b401d | 2263 | error = 0; |
| 984263bc MD |
2264 | break; |
| 2265 | case L_XTND: | |
| 116edfbf MD |
2266 | error = VOP_GETATTR(vp, &vattr); |
| 2267 | new_offset = offset + vattr.va_size; | |
| 984263bc MD |
2268 | break; |
| 2269 | case L_SET: | |
| 116edfbf | 2270 | new_offset = offset; |
| 228b401d | 2271 | error = 0; |
| 984263bc MD |
2272 | break; |
| 2273 | default: | |
| 116edfbf | 2274 | new_offset = 0; |
| 228b401d MD |
2275 | error = EINVAL; |
| 2276 | break; | |
| 984263bc | 2277 | } |
| 116edfbf MD |
2278 | |
| 2279 | /* | |
| 2280 | * Validate the seek position. Negative offsets are not allowed | |
| 58675cb0 MD |
2281 | * for regular files or directories. |
| 2282 | * | |
| 2283 | * Normally we would also not want to allow negative offsets for | |
| 2284 | * character and block-special devices. However kvm addresses | |
| 2285 | * on 64 bit architectures might appear to be negative and must | |
| 2286 | * be allowed. | |
| 116edfbf MD |
2287 | */ |
| 2288 | if (error == 0) { | |
| 2289 | if (new_offset < 0 && | |
| 58675cb0 | 2290 | (vp->v_type == VREG || vp->v_type == VDIR)) { |
| 116edfbf | 2291 | error = EINVAL; |
| b06b3b87 MD |
2292 | } else { |
| 2293 | fp->f_offset = new_offset; | |
| 116edfbf | 2294 | } |
| 116edfbf | 2295 | } |
| 9697c509 | 2296 | *res = fp->f_offset; |
| 228b401d MD |
2297 | done: |
| 2298 | fdrop(fp); | |
| 2299 | return (error); | |
| 984263bc MD |
2300 | } |
| 2301 | ||
| 984263bc | 2302 | /* |
| 9697c509 | 2303 | * lseek_args(int fd, int pad, off_t offset, int whence) |
| 41c20dac | 2304 | * |
| 9697c509 | 2305 | * Reposition read/write file offset. |
| 984263bc | 2306 | */ |
| 984263bc | 2307 | int |
| 753fd850 | 2308 | sys_lseek(struct lseek_args *uap) |
| 984263bc | 2309 | { |
| 984263bc MD |
2310 | int error; |
| 2311 | ||
| 9697c509 | 2312 | error = kern_lseek(uap->fd, uap->offset, uap->whence, |
| 116edfbf | 2313 | &uap->sysmsg_offset); |
| 9697c509 | 2314 | |
| 984263bc MD |
2315 | return (error); |
| 2316 | } | |
| 984263bc | 2317 | |
| 984263bc | 2318 | int |
| fad57d0e | 2319 | kern_access(struct nlookupdata *nd, int aflags) |
| 984263bc | 2320 | { |
| 41c20dac | 2321 | struct vnode *vp; |
| 984263bc | 2322 | int error, flags; |
| 984263bc | 2323 | |
| fad57d0e MD |
2324 | if ((error = nlookup(nd)) != 0) |
| 2325 | return (error); | |
| 806dcf9a | 2326 | retry: |
| 28623bf9 | 2327 | error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp); |
| 9697c509 | 2328 | if (error) |
| fad57d0e | 2329 | return (error); |
| 984263bc MD |
2330 | |
| 2331 | /* Flags == 0 means only check for existence. */ | |
| 9697c509 | 2332 | if (aflags) { |
| 984263bc | 2333 | flags = 0; |
| 9697c509 | 2334 | if (aflags & R_OK) |
| 984263bc | 2335 | flags |= VREAD; |
| 9697c509 | 2336 | if (aflags & W_OK) |
| 984263bc | 2337 | flags |= VWRITE; |
| 9697c509 | 2338 | if (aflags & X_OK) |
| 984263bc | 2339 | flags |= VEXEC; |
| 468bb1f9 | 2340 | if ((flags & VWRITE) == 0 || |
| 28623bf9 | 2341 | (error = vn_writechk(vp, &nd->nl_nch)) == 0) |
| 87de5057 | 2342 | error = VOP_ACCESS(vp, flags, nd->nl_cred); |
| 806dcf9a MD |
2343 | |
| 2344 | /* | |
| 2345 | * If the file handle is stale we have to re-resolve the | |
| 2346 | * entry. This is a hack at the moment. | |
| 2347 | */ | |
| 2348 | if (error == ESTALE) { | |
| 5e1a76ea | 2349 | vput(vp); |
| 28623bf9 MD |
2350 | cache_setunresolved(&nd->nl_nch); |
| 2351 | error = cache_resolve(&nd->nl_nch, nd->nl_cred); | |
| 806dcf9a | 2352 | if (error == 0) { |
| 806dcf9a MD |
2353 | vp = NULL; |
| 2354 | goto retry; | |
| 2355 | } | |
| 5e1a76ea | 2356 | return(error); |
| 806dcf9a | 2357 | } |
| 984263bc | 2358 | } |
| 984263bc | 2359 | vput(vp); |
| 984263bc MD |
2360 | return (error); |
| 2361 | } | |
| 2362 | ||
| 984263bc | 2363 | /* |
| 9697c509 | 2364 | * access_args(char *path, int flags) |
| 41c20dac | 2365 | * |
| 9697c509 | 2366 | * Check access permissions. |
| 984263bc | 2367 | */ |
| 984263bc | 2368 | int |
| 753fd850 | 2369 | sys_access(struct access_args *uap) |
| 984263bc | 2370 | { |
| fad57d0e | 2371 | struct nlookupdata nd; |
| 9697c509 | 2372 | int error; |
| 984263bc | 2373 | |
| fad57d0e MD |
2374 | error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); |
| 2375 | if (error == 0) | |
| 2376 | error = kern_access(&nd, uap->flags); | |
| 2377 | nlookup_done(&nd); | |
| 984263bc MD |
2378 | return (error); |
| 2379 | } | |
| 2380 | ||
| 984263bc | 2381 | int |
| 21739618 | 2382 | kern_stat(struct nlookupdata *nd, struct stat *st) |
| 984263bc | 2383 | { |
| 984263bc | 2384 | int error; |
| 21739618 MD |
2385 | struct vnode *vp; |
| 2386 | thread_t td; | |
| 984263bc | 2387 | |
| 21739618 | 2388 | if ((error = nlookup(nd)) != 0) |
| 984263bc | 2389 | return (error); |
| 806dcf9a | 2390 | again: |
| 28623bf9 | 2391 | if ((vp = nd->nl_nch.ncp->nc_vp) == NULL) |
| 21739618 MD |
2392 | return (ENOENT); |
| 2393 | ||
| 2394 | td = curthread; | |
| 87de5057 | 2395 | if ((error = vget(vp, LK_SHARED)) != 0) |
| 21739618 | 2396 | return (error); |
| 87de5057 | 2397 | error = vn_stat(vp, st, nd->nl_cred); |
| 806dcf9a MD |
2398 | |
| 2399 | /* | |
| 2400 | * If the file handle is stale we have to re-resolve the entry. This | |
| 2401 | * is a hack at the moment. | |
| 2402 | */ | |
| 2403 | if (error == ESTALE) { | |
| 5dc91765 | 2404 | vput(vp); |
| 28623bf9 MD |
2405 | cache_setunresolved(&nd->nl_nch); |
| 2406 | error = cache_resolve(&nd->nl_nch, nd->nl_cred); | |
| 5dc91765 | 2407 | if (error == 0) |
| 806dcf9a | 2408 | goto again; |
| 5dc91765 MD |
2409 | } else { |
| 2410 | vput(vp); | |
| 806dcf9a | 2411 | } |
| 984263bc MD |
2412 | return (error); |
| 2413 | } | |
| 2414 | ||
| 2415 | /* | |
| 41c20dac MD |
2416 | * stat_args(char *path, struct stat *ub) |
| 2417 | * | |
| 984263bc MD |
2418 | * Get file status; this version follows links. |
| 2419 | */ | |
| 984263bc | 2420 | int |
| 753fd850 | 2421 | sys_stat(struct stat_args *uap) |
| 984263bc | 2422 | { |
| 21739618 | 2423 | struct nlookupdata nd; |
| 9697c509 DRJ |
2424 | struct stat st; |
| 2425 | int error; | |
| 984263bc | 2426 | |
| 21739618 MD |
2427 | error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); |
| 2428 | if (error == 0) { | |
| 2429 | error = kern_stat(&nd, &st); | |
| 2430 | if (error == 0) | |
| 2431 | error = copyout(&st, uap->ub, sizeof(*uap->ub)); | |
| 21739618 | 2432 | } |
| fad57d0e | 2433 | nlookup_done(&nd); |
| 984263bc MD |
2434 | return (error); |
| 2435 | } | |
| 2436 | ||
| 2437 | /* | |
| 41c20dac MD |
2438 | * lstat_args(char *path, struct stat *ub) |
| 2439 | * | |
| 984263bc MD |
2440 | * Get file status; this version does not follow links. |
| 2441 | */ | |
| 984263bc | 2442 | int |
| 753fd850 | 2443 | sys_lstat(struct lstat_args *uap) |
| 984263bc | 2444 | { |
| 21739618 | 2445 | struct nlookupdata nd; |
| 9697c509 DRJ |
2446 | struct stat st; |
| 2447 | int error; | |
| 984263bc | 2448 | |
| 21739618 MD |
2449 | error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0); |
| 2450 | if (error == 0) { | |
| 2451 | error = kern_stat(&nd, &st); | |
| 2452 | if (error == 0) | |
| 2453 | error = copyout(&st, uap->ub, sizeof(*uap->ub)); | |
| 21739618 | 2454 | } |
| fad57d0e | 2455 | nlookup_done(&nd); |
| 984263bc MD |
2456 | return (error); |
| 2457 | } | |
| 2458 | ||
| 984263bc | 2459 | /* |
| e101a3f4 NT |
2460 | * fstatat_args(int fd, char *path, struct stat *sb, int flags) |
| 2461 | * | |
| 2462 | * Get status of file pointed to by fd/path. | |
| 2463 | */ | |
| 2464 | int | |
| 2465 | sys_fstatat(struct fstatat_args *uap) | |
| 2466 | { | |
| 2467 | struct nlookupdata nd; | |
| 2468 | struct stat st; | |
| 2469 | int error; | |
| 2470 | int flags; | |
| 2471 | struct file *fp; | |
| 2472 | ||
| 2473 | if (uap->flags & ~_AT_SYMLINK_MASK) | |
| 2474 | return (EINVAL); | |
| 2475 | ||
| 2476 | flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW; | |
| 2477 | ||
| 2478 | error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, | |
| 2479 | UIO_USERSPACE, flags); | |
| 2480 | if (error == 0) { | |
| 2481 | error = kern_stat(&nd, &st); | |
| 2482 | if (error == 0) | |
| 2483 | error = copyout(&st, uap->sb, sizeof(*uap->sb)); | |
| 2484 | } | |
| 2485 | nlookup_done_at(&nd, fp); | |
| 2486 | return (error); | |
| 2487 | } | |
| 2488 | ||
| 2489 | /* | |
| 41c20dac MD |
2490 | * pathconf_Args(char *path, int name) |
| 2491 | * | |
| 984263bc MD |
2492 | * Get configurable pathname variables. |
| 2493 | */ | |
| 984263bc MD |
2494 | /* ARGSUSED */ |
| 2495 | int | |
| 753fd850 | 2496 | sys_pathconf(struct pathconf_args *uap) |
| 984263bc | 2497 | { |
| fad57d0e MD |
2498 | struct nlookupdata nd; |
| 2499 | struct vnode *vp; | |
| 984263bc | 2500 | int error; |
| 984263bc | 2501 | |
| fad57d0e | 2502 | vp = NULL; |
| ab2eb4eb | 2503 | error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); |
| fad57d0e MD |
2504 | if (error == 0) |
| 2505 | error = nlookup(&nd); | |
| 2506 | if (error == 0) | |
| 28623bf9 | 2507 | error = cache_vget(&nd.nl_nch, nd.nl_cred, LK_EXCLUSIVE, &vp); |
| fad57d0e MD |
2508 | nlookup_done(&nd); |
| 2509 | if (error == 0) { | |
| a385a915 | 2510 | error = VOP_PATHCONF(vp, uap->name, &uap->sysmsg_reg); |
| fad57d0e MD |
2511 | vput(vp); |
| 2512 | } | |
| 984263bc MD |
2513 | return (error); |
| 2514 | } | |
| 2515 | ||
| bbab27f6 DRJ |
2516 | /* |
| 2517 | * XXX: daver | |
| 2518 | * kern_readlink isn't properly split yet. There is a copyin burried | |
| 2519 | * in VOP_READLINK(). | |
| 2520 | */ | |
| 984263bc | 2521 | int |
| fad57d0e | 2522 | kern_readlink(struct nlookupdata *nd, char *buf, int count, int *res) |
| 984263bc | 2523 | { |
| dadab5e9 MD |
2524 | struct thread *td = curthread; |
| 2525 | struct proc *p = td->td_proc; | |
| 41c20dac | 2526 | struct vnode *vp; |
| 984263bc MD |
2527 | struct iovec aiov; |
| 2528 | struct uio auio; | |
| 2529 | int error; | |
| 984263bc | 2530 | |
| fad57d0e MD |
2531 | if ((error = nlookup(nd)) != 0) |
| 2532 | return (error); | |
| 28623bf9 | 2533 | error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &vp); |
| 9697c509 | 2534 | if (error) |
| 984263bc | 2535 | return (error); |
| fad57d0e | 2536 | if (vp->v_type != VLNK) { |
| 984263bc | 2537 | error = EINVAL; |
| fad57d0e | 2538 | } else { |
| 9697c509 DRJ |
2539 | aiov.iov_base = buf; |
| 2540 | aiov.iov_len = count; | |
| 984263bc MD |
2541 | auio.uio_iov = &aiov; |
| 2542 | auio.uio_iovcnt = 1; | |
| 2543 | auio.uio_offset = 0; | |
| 2544 | auio.uio_rw = UIO_READ; | |
| 2545 | auio.uio_segflg = UIO_USERSPACE; | |
| dadab5e9 | 2546 | auio.uio_td = td; |
| 9697c509 | 2547 | auio.uio_resid = count; |
| 984263bc MD |
2548 | error = VOP_READLINK(vp, &auio, p->p_ucred); |
| 2549 | } | |
| 2550 | vput(vp); | |
| 9697c509 DRJ |
2551 | *res = count - auio.uio_resid; |
| 2552 | return (error); | |
| 2553 | } | |
| 2554 | ||
| 2555 | /* | |
| 2556 | * readlink_args(char *path, char *buf, int count) | |
| 2557 | * | |
| 2558 | * Return target name of a symbolic link. | |
| 2559 | */ | |
| 2560 | int | |
| 753fd850 | 2561 | sys_readlink(struct readlink_args *uap) |
| 9697c509 | 2562 | { |
| fad57d0e | 2563 | struct nlookupdata nd; |
| 9697c509 DRJ |
2564 | int error; |
| 2565 | ||
| fad57d0e MD |
2566 | error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0); |
| 2567 | if (error == 0) { | |
| 2568 | error = kern_readlink(&nd, uap->buf, uap->count, | |
| 2569 | &uap->sysmsg_result); | |
| 2570 | } | |
| 2571 | nlookup_done(&nd); | |
| 984263bc MD |
2572 | return (error); |
| 2573 | } | |
| 2574 | ||
| 2575 | static int | |
| 41c20dac | 2576 | setfflags(struct vnode *vp, int flags) |
| 984263bc | 2577 | { |
| dadab5e9 MD |
2578 | struct thread *td = curthread; |
| 2579 | struct proc *p = td->td_proc; | |
| 984263bc MD |
2580 | int error; |
| 2581 | struct vattr vattr; | |
| 2582 | ||
| 2583 | /* | |
| 2584 | * Prevent non-root users from setting flags on devices. When | |
| 2585 | * a device is reused, users can retain ownership of the device | |
| 2586 | * if they are allowed to set flags and programs assume that | |
| 2587 | * chown can't fail when done as root. | |
| 2588 | */ | |
| 2589 | if ((vp->v_type == VCHR || vp->v_type == VBLK) && | |
| 6dc79895 | 2590 | ((error = priv_check_cred(p->p_ucred, PRIV_VFS_CHFLAGS_DEV, 0)) != 0)) |
| 984263bc MD |
2591 | return (error); |
| 2592 | ||
| 186fccd6 MD |
2593 | /* |
| 2594 | * note: vget is required for any operation that might mod the vnode | |
| 2595 | * so VINACTIVE is properly cleared. | |
| 2596 | */ | |
| 87de5057 | 2597 | if ((error = vget(vp, LK_EXCLUSIVE)) == 0) { |
| 186fccd6 MD |
2598 | VATTR_NULL(&vattr); |
| 2599 | vattr.va_flags = flags; | |
| 87de5057 | 2600 | error = VOP_SETATTR(vp, &vattr, p->p_ucred); |
| 186fccd6 MD |
2601 | vput(vp); |
| 2602 | } | |
| 984263bc MD |
2603 | return (error); |
| 2604 | } | |
| 2605 | ||
| 2606 | /* | |
| 41c20dac MD |
2607 | * chflags(char *path, int flags) |
| 2608 | * | |
| 984263bc MD |
2609 | * Change flags of a file given a path name. |
| 2610 | */ | |
| 984263bc MD |
2611 | /* ARGSUSED */ |
| 2612 | int | |
| 753fd850 | 2613 | sys_chflags(struct chflags_args *uap) |
| 984263bc | 2614 | { |
| fad57d0e MD |
2615 | struct nlookupdata nd; |
| 2616 | struct vnode *vp; | |
| 984263bc | 2617 | int error; |
| 984263bc | 2618 | |
| fad57d0e | 2619 | vp = NULL; |
| ab2eb4eb | 2620 | error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); |
| fad57d0e MD |
2621 | if (error == 0) |
| 2622 | error = nlookup(&nd); | |
| 2623 | if (error == 0) | |
| 28623bf9 | 2624 | error = ncp_writechk(&nd.nl_nch); |
| 72310cfb | 2625 | if (error == 0) |
| 28623bf9 | 2626 | error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp); |
| fad57d0e MD |
2627 | nlookup_done(&nd); |
| 2628 | if (error == 0) { | |
| ab2eb4eb | 2629 | error = setfflags(vp, uap->flags); |
| fad57d0e MD |
2630 | vrele(vp); |
| 2631 | } | |
| 2632 | return (error); | |
| 984263bc MD |
2633 | } |
| 2634 | ||
| 2635 | /* | |
| 91fdc001 PA |
2636 | * lchflags(char *path, int flags) |
| 2637 | * | |
| 2638 | * Change flags of a file given a path name, but don't follow symlinks. | |
| 2639 | */ | |
| 2640 | /* ARGSUSED */ | |
| 2641 | int | |
| 2642 | sys_lchflags(struct lchflags_args *uap) | |
| 2643 | { | |
| 2644 | struct nlookupdata nd; | |
| 2645 | struct vnode *vp; | |
| 2646 | int error; | |
| 2647 | ||
| 2648 | vp = NULL; | |
| 2649 | error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0); | |
| 91fdc001 PA |
2650 | if (error == 0) |
| 2651 | error = nlookup(&nd); | |
| 2652 | if (error == 0) | |
| 2653 | error = ncp_writechk(&nd.nl_nch); | |
| 2654 | if (error == 0) | |
| 2655 | error = cache_vref(&nd.nl_nch, nd.nl_cred, &vp); | |
| 2656 | nlookup_done(&nd); | |
| 2657 | if (error == 0) { | |
| 2658 | error = setfflags(vp, uap->flags); | |
| 2659 | vrele(vp); | |
| 2660 | } | |
| 2661 | return (error); | |
| 2662 | } | |
| 2663 | ||
| 2664 | /* | |
| 41c20dac MD |
2665 | * fchflags_args(int fd, int flags) |
| 2666 | * | |
| 984263bc MD |
2667 | * Change flags of a file given a file descriptor. |
| 2668 | */ | |
| 984263bc MD |
2669 | /* ARGSUSED */ |
| 2670 | int | |
| 753fd850 | 2671 | sys_fchflags(struct fchflags_args *uap) |
| 984263bc | 2672 | { |
| dadab5e9 MD |
2673 | struct thread *td = curthread; |
| 2674 | struct proc *p = td->td_proc; | |
| 984263bc MD |
2675 | struct file *fp; |
| 2676 | int error; | |
| 2677 | ||
| 5b287bba | 2678 | if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0) |
| 984263bc | 2679 | return (error); |
| 28623bf9 MD |
2680 | if (fp->f_nchandle.ncp) |
| 2681 | error = ncp_writechk(&fp->f_nchandle); | |
| 72310cfb MD |
2682 | if (error == 0) |
| 2683 | error = setfflags((struct vnode *) fp->f_data, uap->flags); | |
| 5b287bba MD |
2684 | fdrop(fp); |
| 2685 | return (error); | |
| 984263bc MD |
2686 | } |
| 2687 | ||
| 2688 | static int | |
| 41c20dac | 2689 | setfmode(struct vnode *vp, int mode) |
| 984263bc | 2690 | { |
| dadab5e9 MD |
2691 | struct thread *td = curthread; |
| 2692 | struct proc *p = td->td_proc; | |
| 984263bc MD |
2693 | int error; |
| 2694 | struct vattr vattr; | |
| 2695 | ||
| 186fccd6 MD |
2696 | /* |
| 2697 | * note: vget is required for any operation that might mod the vnode | |
| 2698 | * so VINACTIVE is properly cleared. | |
| 2699 | */ | |
| 87de5057 | 2700 | if ((error = vget(vp, LK_EXCLUSIVE)) == 0) { |
| 186fccd6 MD |
2701 | VATTR_NULL(&vattr); |
| 2702 | vattr.va_mode = mode & ALLPERMS; | |
| 87de5057 | 2703 | error = VOP_SETATTR(vp, &vattr, p->p_ucred); |
| 186fccd6 MD |
2704 | vput(vp); |
| 2705 | } | |
| 984263bc MD |
2706 | return error; |
| 2707 | } | |
| 2708 | ||
| 9697c509 | 2709 | int |
| fad57d0e | 2710 | kern_chmod(struct nlookupdata *nd, int mode) |
| 9697c509 | 2711 | { |
| fad57d0e | 2712 | struct vnode *vp; |
| 9697c509 DRJ |
2713 | int error; |
| 2714 | ||
| fad57d0e | 2715 | if ((error = nlookup(nd)) != 0) |
| 9697c509 | 2716 | return (error); |
| 28623bf9 | 2717 | if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0) |
| fad57d0e | 2718 | return (error); |
| 28623bf9 | 2719 | if ((error = ncp_writechk(&nd->nl_nch)) == 0) |
| 72310cfb | 2720 | error = setfmode(vp, mode); |
| fad57d0e MD |
2721 | vrele(vp); |
| 2722 | return (error); | |
| 9697c509 DRJ |
2723 | } |
| 2724 | ||
| 984263bc | 2725 | /* |
| 41c20dac MD |
2726 | * chmod_args(char *path, int mode) |
| 2727 | * | |
| 984263bc MD |
2728 | * Change mode of a file given path name. |
| 2729 | */ | |
| 984263bc MD |
2730 | /* ARGSUSED */ |
| 2731 | int | |
| 753fd850 | 2732 | sys_chmod(struct chmod_args *uap) |
| 984263bc | 2733 | { |
| fad57d0e | 2734 | struct nlookupdata nd; |
| 9697c509 | 2735 | int error; |
| 984263bc | 2736 | |
| fad57d0e MD |
2737 | error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); |
| 2738 | if (error == 0) | |
| 2739 | error = kern_chmod(&nd, uap->mode); | |
| 2740 | nlookup_done(&nd); | |
| 9697c509 | 2741 | return (error); |
| 984263bc MD |
2742 | } |
| 2743 | ||
| 2744 | /* | |
| 41c20dac MD |
2745 | * lchmod_args(char *path, int mode) |
| 2746 | * | |
| 984263bc MD |
2747 | * Change mode of a file given path name (don't follow links.) |
| 2748 | */ | |
| 984263bc MD |
2749 | /* ARGSUSED */ |
| 2750 | int | |
| 753fd850 | 2751 | sys_lchmod(struct lchmod_args *uap) |
| 984263bc | 2752 | { |
| fad57d0e | 2753 | struct nlookupdata nd; |
| 984263bc | 2754 | int error; |
| 984263bc | 2755 | |
| fad57d0e MD |
2756 | error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0); |
| 2757 | if (error == 0) | |
| 2758 | error = kern_chmod(&nd, uap->mode); | |
| 2759 | nlookup_done(&nd); | |
| 2760 | return (error); | |
| 984263bc MD |
2761 | } |
| 2762 | ||
| 2763 | /* | |
| 41c20dac MD |
2764 | * fchmod_args(int fd, int mode) |
| 2765 | * | |
| 984263bc MD |
2766 | * Change mode of a file given a file descriptor. |
| 2767 | */ | |
| 984263bc MD |
2768 | /* ARGSUSED */ |
| 2769 | int | |
| 753fd850 | 2770 | sys_fchmod(struct fchmod_args *uap) |
| 984263bc | 2771 | { |
| dadab5e9 MD |
2772 | struct thread *td = curthread; |
| 2773 | struct proc *p = td->td_proc; | |
| 984263bc MD |
2774 | struct file *fp; |
| 2775 | int error; | |
| 2776 | ||
| 5b287bba | 2777 | if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0) |
| 984263bc | 2778 | return (error); |
| 28623bf9 MD |
2779 | if (fp->f_nchandle.ncp) |
| 2780 | error = ncp_writechk(&fp->f_nchandle); | |
| 72310cfb MD |
2781 | if (error == 0) |
| 2782 | error = setfmode((struct vnode *)fp->f_data, uap->mode); | |
| 5b287bba MD |
2783 | fdrop(fp); |
| 2784 | return (error); | |
| 984263bc MD |
2785 | } |
| 2786 | ||
| 8cde3c79 NT |
2787 | /* |
| 2788 | * fchmodat_args(char *path, int mode) | |
| 2789 | * | |
| 2790 | * Change mode of a file pointed to by fd/path. | |
| 2791 | */ | |
| 2792 | int | |
| 2793 | sys_fchmodat(struct fchmodat_args *uap) | |
| 2794 | { | |
| 2795 | struct nlookupdata nd; | |
| 2796 | struct file *fp; | |
| 2797 | int error; | |
| 2798 | int flags; | |
| 2799 | ||
| 2800 | if (uap->flags & ~_AT_SYMLINK_MASK) | |
| 2801 | return (EINVAL); | |
| 2802 | flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW; | |
| 2803 | ||
| 2804 | error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, | |
| 2805 | UIO_USERSPACE, flags); | |
| 2806 | if (error == 0) | |
| 2807 | error = kern_chmod(&nd, uap->mode); | |
| 2808 | nlookup_done_at(&nd, fp); | |
| 2809 | return (error); | |
| 2810 | } | |
| 2811 | ||
| 984263bc | 2812 | static int |
| 41c20dac | 2813 | setfown(struct vnode *vp, uid_t uid, gid_t gid) |
| 984263bc | 2814 | { |
| dadab5e9 MD |
2815 | struct thread *td = curthread; |
| 2816 | struct proc *p = td->td_proc; | |
| 984263bc MD |
2817 | int error; |
| 2818 | struct vattr vattr; | |
| 2819 | ||
| 186fccd6 MD |
2820 | /* |
| 2821 | * note: vget is required for any operation that might mod the vnode | |
| 2822 | * so VINACTIVE is properly cleared. | |
| 2823 | */ | |
| 87de5057 | 2824 | if ((error = vget(vp, LK_EXCLUSIVE)) == 0) { |
| 186fccd6 MD |
2825 | VATTR_NULL(&vattr); |
| 2826 | vattr.va_uid = uid; | |
| 2827 | vattr.va_gid = gid; | |
| 87de5057 | 2828 | error = VOP_SETATTR(vp, &vattr, p->p_ucred); |
| 186fccd6 MD |
2829 | vput(vp); |
| 2830 | } | |
| 984263bc MD |
2831 | return error; |
| 2832 | } | |
| 2833 | ||
| 9697c509 | 2834 | int |
| fad57d0e | 2835 | kern_chown(struct nlookupdata *nd, int uid, int gid) |
| 9697c509 | 2836 | { |
| fad57d0e | 2837 | struct vnode *vp; |
| 9697c509 DRJ |
2838 | int error; |
| 2839 | ||
| fad57d0e | 2840 | if ((error = nlookup(nd)) != 0) |
| 9697c509 | 2841 | return (error); |
| 28623bf9 | 2842 | if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0) |
| fad57d0e | 2843 | return (error); |
| 28623bf9 | 2844 | if ((error = ncp_writechk(&nd->nl_nch)) == 0) |
| 72310cfb | 2845 | error = setfown(vp, uid, gid); |
| fad57d0e | 2846 | vrele(vp); |
| 9697c509 DRJ |
2847 | return (error); |
| 2848 | } | |
| 2849 | ||
| 984263bc | 2850 | /* |
| 41c20dac MD |
2851 | * chown(char *path, int uid, int gid) |
| 2852 | * | |
| 984263bc MD |
2853 | * Set ownership given a path name. |
| 2854 | */ | |
| 984263bc | 2855 | int |
| 753fd850 | 2856 | sys_chown(struct chown_args *uap) |
| 984263bc | 2857 | { |
| fad57d0e | 2858 | struct nlookupdata nd; |
| 9697c509 DRJ |
2859 | int error; |
| 2860 | ||
| fad57d0e MD |
2861 | error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); |
| 2862 | if (error == 0) | |
| 2863 | error = kern_chown(&nd, uap->uid, uap->gid); | |
| 2864 | nlookup_done(&nd); | |
| 984263bc MD |
2865 | return (error); |
| 2866 | } | |
| 2867 | ||
| 2868 | /* | |
| 41c20dac MD |
2869 | * lchown_args(char *path, int uid, int gid) |
| 2870 | * | |
| 984263bc MD |
2871 | * Set ownership given a path name, do not cross symlinks. |
| 2872 | */ | |
| 984263bc | 2873 | int |
| 753fd850 | 2874 | sys_lchown(struct lchown_args *uap) |
| 984263bc | 2875 | { |
| fad57d0e | 2876 | struct nlookupdata nd; |
| 984263bc | 2877 | int error; |
| 9697c509 | 2878 | |
| fad57d0e MD |
2879 | error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0); |
| 2880 | if (error == 0) | |
| 2881 | error = kern_chown(&nd, uap->uid, uap->gid); | |
| 2882 | nlookup_done(&nd); | |
| 984263bc MD |
2883 | return (error); |
| 2884 | } | |
| 2885 | ||
| 2886 | /* | |
| 41c20dac MD |
2887 | * fchown_args(int fd, int uid, int gid) |
| 2888 | * | |
| 984263bc MD |
2889 | * Set ownership given a file descriptor. |
| 2890 | */ | |
| 984263bc MD |
2891 | /* ARGSUSED */ |
| 2892 | int | |
| 753fd850 | 2893 | sys_fchown(struct fchown_args *uap) |
| 984263bc | 2894 | { |
| dadab5e9 MD |
2895 | struct thread *td = curthread; |
| 2896 | struct proc *p = td->td_proc; | |
| 984263bc MD |
2897 | struct file *fp; |
| 2898 | int error; | |
| 2899 | ||
| 5b287bba | 2900 | if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0) |
| 984263bc | 2901 | return (error); |
| 28623bf9 MD |
2902 | if (fp->f_nchandle.ncp) |
| 2903 | error = ncp_writechk(&fp->f_nchandle); | |
| 72310cfb MD |
2904 | if (error == 0) |
| 2905 | error = setfown((struct vnode *)fp->f_data, uap->uid, uap->gid); | |
| 5b287bba MD |
2906 | fdrop(fp); |
| 2907 | return (error); | |
| 984263bc MD |
2908 | } |
| 2909 | ||
| 32013176 NT |
2910 | /* |
| 2911 | * fchownat(int fd, char *path, int uid, int gid, int flags) | |
| 2912 | * | |
| 2913 | * Set ownership of file pointed to by fd/path. | |
| 2914 | */ | |
| 2915 | int | |
| 2916 | sys_fchownat(struct fchownat_args *uap) | |
| 2917 | { | |
| 2918 | struct nlookupdata nd; | |
| 2919 | struct file *fp; | |
| 2920 | int error; | |
| 2921 | int flags; | |
| 2922 | ||
| 2923 | if (uap->flags & ~_AT_SYMLINK_MASK) | |
| 2924 | return (EINVAL); | |
| 2925 | flags = (uap->flags & AT_SYMLINK_NOFOLLOW) ? 0 : NLC_FOLLOW; | |
| 2926 | ||
| 2927 | error = nlookup_init_at(&nd, &fp, uap->fd, uap->path, | |
| 2928 | UIO_USERSPACE, flags); | |
| 2929 | if (error == 0) | |
| 2930 | error = kern_chown(&nd, uap->uid, uap->gid); | |
| 2931 | nlookup_done_at(&nd, fp); | |
| 2932 | return (error); | |
| 2933 | } | |
| 2934 | ||
| 2935 | ||
| 984263bc | 2936 | static int |
| 9697c509 | 2937 | getutimes(const struct timeval *tvp, struct timespec *tsp) |
| 984263bc MD |
2938 | { |
| 2939 | struct timeval tv[2]; | |
| 984263bc | 2940 | |
| 9697c509 | 2941 | if (tvp == NULL) { |
| 984263bc MD |
2942 | microtime(&tv[0]); |
| 2943 | TIMEVAL_TO_TIMESPEC(&tv[0], &tsp[0]); | |
| 2944 | tsp[1] = tsp[0]; | |
| 2945 | } else { | |
| 9697c509 DRJ |
2946 | TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]); |
| 2947 | TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]); | |
| 984263bc MD |
2948 | } |
| 2949 | return 0; | |
| 2950 | } | |
| 2951 | ||
| 2952 | static int | |
| 3a907475 MD |
2953 | setutimes(struct vnode *vp, struct vattr *vattr, |
| 2954 | const struct timespec *ts, int nullflag) | |
| 984263bc | 2955 | { |
| dadab5e9 MD |
2956 | struct thread *td = curthread; |
| 2957 | struct proc *p = td->td_proc; | |
| 984263bc | 2958 | int error; |
| 984263bc | 2959 | |
| 3a907475 MD |
2960 | VATTR_NULL(vattr); |
| 2961 | vattr->va_atime = ts[0]; | |
| 2962 | vattr->va_mtime = ts[1]; | |
| 2963 | if (nullflag) | |
| 2964 | vattr->va_vaflags |= VA_UTIMES_NULL; | |
| 2965 | error = VOP_SETATTR(vp, vattr, p->p_ucred); | |
| 2966 | ||
| 984263bc MD |
2967 | return error; |
| 2968 | } | |
| 2969 | ||
| 9697c509 | 2970 | int |
| fad57d0e | 2971 | kern_utimes(struct nlookupdata *nd, struct timeval *tptr) |
| 9697c509 DRJ |
2972 | { |
| 2973 | struct timespec ts[2]; | |
| fad57d0e | 2974 | struct vnode *vp; |
| 3a907475 | 2975 | struct vattr vattr; |
| 9697c509 DRJ |
2976 | int error; |
| 2977 | ||
| fad57d0e | 2978 | if ((error = getutimes(tptr, ts)) != 0) |
| 9697c509 | 2979 | return (error); |
| 3a907475 MD |
2980 | |
| 2981 | /* | |
| 2982 | * NOTE: utimes() succeeds for the owner even if the file | |
| 2983 | * is not user-writable. | |
| 2984 | */ | |
| 2985 | nd->nl_flags |= NLC_OWN | NLC_WRITE; | |
| 2986 | ||
| fad57d0e | 2987 | if ((error = nlookup(nd)) != 0) |
| 9697c509 | 2988 | return (error); |
| 28623bf9 | 2989 | if ((error = ncp_writechk(&nd->nl_nch)) != 0) |
| 72310cfb | 2990 | return (error); |
| 28623bf9 | 2991 | if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0) |
| fad57d0e | 2992 | return (error); |
| ee89633d MD |
2993 | |
| 2994 | /* | |
| 3a907475 MD |
2995 | * note: vget is required for any operation that might mod the vnode |
| 2996 | * so VINACTIVE is properly cleared. | |
| ee89633d | 2997 | */ |
| 3a907475 MD |
2998 | if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) { |
| 2999 | error = vget(vp, LK_EXCLUSIVE); | |
| 3000 | if (error == 0) { | |
| 3001 | error = setutimes(vp, &vattr, ts, (tptr == NULL)); | |
| 3002 | vput(vp); | |
| 3003 | } | |
| 918101d3 | 3004 | } |
| fad57d0e | 3005 | vrele(vp); |
| 9697c509 DRJ |
3006 | return (error); |
| 3007 | } | |
| 3008 | ||
| 984263bc | 3009 | /* |
| 41c20dac MD |
3010 | * utimes_args(char *path, struct timeval *tptr) |
| 3011 | * | |
| 984263bc MD |
3012 | * Set the access and modification times of a file. |
| 3013 | */ | |
| 984263bc | 3014 | int |
| 753fd850 | 3015 | sys_utimes(struct utimes_args *uap) |
| 984263bc | 3016 | { |
| 9697c509 | 3017 | struct timeval tv[2]; |
| fad57d0e | 3018 | struct nlookupdata nd; |
| 9697c509 DRJ |
3019 | int error; |
| 3020 | ||
| 3021 | if (uap->tptr) { | |
| 3022 | error = copyin(uap->tptr, tv, sizeof(tv)); | |
| 3023 | if (error) | |
| 3024 | return (error); | |
| 3025 | } | |
| fad57d0e MD |
3026 | error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); |
| 3027 | if (error == 0) | |
| 3028 | error = kern_utimes(&nd, uap->tptr ? tv : NULL); | |
| 3029 | nlookup_done(&nd); | |
| 984263bc MD |
3030 | return (error); |
| 3031 | } | |
| 3032 | ||
| 3033 | /* | |
| 41c20dac MD |
3034 | * lutimes_args(char *path, struct timeval *tptr) |
| 3035 | * | |
| 984263bc MD |
3036 | * Set the access and modification times of a file. |
| 3037 | */ | |
| 984263bc | 3038 | int |
| 753fd850 | 3039 | sys_lutimes(struct lutimes_args *uap) |
| 984263bc | 3040 | { |
| 9697c509 | 3041 | struct timeval tv[2]; |
| fad57d0e | 3042 | struct nlookupdata nd; |
| 9697c509 DRJ |
3043 | int error; |
| 3044 | ||
| 3045 | if (uap->tptr) { | |
| 3046 | error = copyin(uap->tptr, tv, sizeof(tv)); | |
| 3047 | if (error) | |
| 3048 | return (error); | |
| 3049 | } | |
| fad57d0e MD |
3050 | error = nlookup_init(&nd, uap->path, UIO_USERSPACE, 0); |
| 3051 | if (error == 0) | |
| 3052 | error = kern_utimes(&nd, uap->tptr ? tv : NULL); | |
| 3053 | nlookup_done(&nd); | |
| 984263bc MD |
3054 | return (error); |
| 3055 | } | |
| 3056 | ||
| 3a907475 MD |
3057 | /* |
| 3058 | * Set utimes on a file descriptor. The creds used to open the | |
| 3059 | * file are used to determine whether the operation is allowed | |
| 3060 | * or not. | |
| 3061 | */ | |
| 984263bc | 3062 | int |
| 9697c509 | 3063 | kern_futimes(int fd, struct timeval *tptr) |
| 984263bc | 3064 | { |
| dadab5e9 MD |
3065 | struct thread *td = curthread; |
| 3066 | struct proc *p = td->td_proc; | |
| 984263bc MD |
3067 | struct timespec ts[2]; |
| 3068 | struct file *fp; | |
| 3a907475 MD |
3069 | struct vnode *vp; |
| 3070 | struct vattr vattr; | |
| 984263bc MD |
3071 | int error; |
| 3072 | ||
| 9697c509 DRJ |
3073 | error = getutimes(tptr, ts); |
| 3074 | if (error) | |
| 984263bc | 3075 | return (error); |
| 5b287bba | 3076 | if ((error = holdvnode(p->p_fd, fd, &fp)) != 0) |
| 984263bc | 3077 | return (error); |
| 28623bf9 MD |
3078 | if (fp->f_nchandle.ncp) |
| 3079 | error = ncp_writechk(&fp->f_nchandle); | |
| 3a907475 MD |
3080 | if (error == 0) { |
| 3081 | vp = fp->f_data; | |
| 3082 | error = vget(vp, LK_EXCLUSIVE); | |
| 3083 | if (error == 0) { | |
| 3084 | error = VOP_GETATTR(vp, &vattr); | |
| 3085 | if (error == 0) { | |
| 3086 | error = naccess_va(&vattr, NLC_OWN | NLC_WRITE, | |
| 3087 | fp->f_cred); | |
| 3088 | } | |
| 3089 | if (error == 0) { | |
| 3090 | error = setutimes(vp, &vattr, ts, | |
| 3091 | (tptr == NULL)); | |
| 3092 | } | |
| 3093 | vput(vp); | |
| 3094 | } | |
| 3095 | } | |
| 5b287bba | 3096 | fdrop(fp); |
| 9697c509 | 3097 | return (error); |
| 984263bc MD |
3098 | } |
| 3099 | ||
| 3100 | /* | |
| 9697c509 | 3101 | * futimes_args(int fd, struct timeval *tptr) |
| 41c20dac | 3102 | * |
| 9697c509 | 3103 | * Set the access and modification times of a file. |
| 984263bc | 3104 | */ |
| 984263bc | 3105 | int |
| 753fd850 | 3106 | sys_futimes(struct futimes_args *uap) |
| 9697c509 DRJ |
3107 | { |
| 3108 | struct timeval tv[2]; | |
| 3109 | int error; | |
| 3110 | ||
| 3111 | if (uap->tptr) { | |
| 3112 | error = copyin(uap->tptr, tv, sizeof(tv)); | |
| 3113 | if (error) | |
| 3114 | return (error); | |
| 3115 | } | |
| 3116 | ||
| b651c485 | 3117 | error = kern_futimes(uap->fd, uap->tptr ? tv : NULL); |
| 9697c509 DRJ |
3118 | |
| 3119 | return (error); | |
| 3120 | } | |
| 3121 | ||
| 3122 | int | |
| fad57d0e | 3123 | kern_truncate(struct nlookupdata *nd, off_t length) |
| 984263bc | 3124 | { |
| 41c20dac | 3125 | struct vnode *vp; |
| 984263bc MD |
3126 | struct vattr vattr; |
| 3127 | int error; | |
| 984263bc | 3128 | |
| 9697c509 | 3129 | if (length < 0) |
| 984263bc | 3130 | return(EINVAL); |
| 3a907475 | 3131 | nd->nl_flags |= NLC_WRITE | NLC_TRUNCATE; |
| fad57d0e | 3132 | if ((error = nlookup(nd)) != 0) |
| 984263bc | 3133 | return (error); |
| 28623bf9 | 3134 | if ((error = ncp_writechk(&nd->nl_nch)) != 0) |
| 72310cfb | 3135 | return (error); |
| 28623bf9 | 3136 | if ((error = cache_vref(&nd->nl_nch, nd->nl_cred, &vp)) != 0) |
| fad57d0e | 3137 | return (error); |
| ca466bae | 3138 | if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) != 0) { |
| fad57d0e MD |
3139 | vrele(vp); |
| 3140 | return (error); | |
| 3141 | } | |
| 3142 | if (vp->v_type == VDIR) { | |
| 984263bc | 3143 | error = EISDIR; |
| 3a907475 | 3144 | } else if ((error = vn_writechk(vp, &nd->nl_nch)) == 0) { |
| 984263bc | 3145 | VATTR_NULL(&vattr); |
| 9697c509 | 3146 | vattr.va_size = length; |
| 87de5057 | 3147 | error = VOP_SETATTR(vp, &vattr, nd->nl_cred); |
| 984263bc MD |
3148 | } |
| 3149 | vput(vp); | |
| 3150 | return (error); | |
| 3151 | } | |
| 3152 | ||
| 9697c509 DRJ |
3153 | /* |
| 3154 | * truncate(char *path, int pad, off_t length) | |
| 3155 | * | |
| 3156 | * Truncate a file given its path name. | |
| 3157 | */ | |
| 3158 | int | |
| 753fd850 | 3159 | sys_truncate(struct truncate_args *uap) |
| 9697c509 | 3160 | { |
| fad57d0e | 3161 | struct nlookupdata nd; |
| 9697c509 DRJ |
3162 | int error; |
| 3163 | ||
| fad57d0e MD |
3164 | error = nlookup_init(&nd, uap->path, UIO_USERSPACE, NLC_FOLLOW); |
| 3165 | if (error == 0) | |
| 3166 | error = kern_truncate(&nd, uap->length); | |
| 3167 | nlookup_done(&nd); | |
| 9697c509 DRJ |
3168 | return error; |
| 3169 | } | |
| 3170 | ||
| 984263bc | 3171 | int |
| 8f6f8622 | 3172 | kern_ftruncate(int fd, off_t length) |
| 984263bc | 3173 | { |
| dadab5e9 MD |
3174 | struct thread *td = curthread; |
| 3175 | struct proc *p = td->td_proc; | |
| 984263bc MD |
3176 | struct vattr vattr; |
| 3177 | struct vnode *vp; | |
| 3178 | struct file *fp; | |
| 3179 | int error; | |
| 3180 | ||
| 8f6f8622 | 3181 | if (length < 0) |
| 984263bc | 3182 | return(EINVAL); |
| 5b287bba | 3183 | if ((error = holdvnode(p->p_fd, fd, &fp)) != 0) |
| 984263bc | 3184 | return (error); |
| 28623bf9 MD |
3185 | if (fp->f_nchandle.ncp) { |
| 3186 | error = ncp_writechk(&fp->f_nchandle); | |
| 72310cfb MD |
3187 | if (error) |
| 3188 | goto done; | |
| 3189 | } | |
| 5b287bba MD |
3190 | if ((fp->f_flag & FWRITE) == 0) { |
| 3191 | error = EINVAL; | |
| 3192 | goto done; | |
| 3193 | } | |
| 3a907475 MD |
3194 | if (fp->f_flag & FAPPENDONLY) { /* inode was set s/uapnd */ |
| 3195 | error = EINVAL; | |
| 3196 | goto done; | |
| 3197 | } | |
| 984263bc | 3198 | vp = (struct vnode *)fp->f_data; |
| ca466bae | 3199 | vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); |
| 5b287bba | 3200 | if (vp->v_type == VDIR) { |
| 984263bc | 3201 | error = EISDIR; |
| 468bb1f9 | 3202 | } else if ((error = vn_writechk(vp, NULL)) == 0) { |
| 984263bc | 3203 | VATTR_NULL(&vattr); |
| 8f6f8622 | 3204 | vattr.va_size = length; |
| 87de5057 | 3205 | error = VOP_SETATTR(vp, &vattr, fp->f_cred); |
| 984263bc | 3206 | } |
| a11aaa81 | 3207 | vn_unlock(vp); |
| 5b287bba MD |
3208 | done: |
| 3209 | fdrop(fp); | |
| 984263bc MD |
3210 | return (error); |
| 3211 | } | |
| 3212 | ||
| 984263bc | 3213 | /* |
| 8f6f8622 | 3214 | * ftruncate_args(int fd, int pad, off_t length) |
| 41c20dac | 3215 | * |
| 8f6f8622 | 3216 | * Truncate a file given a file descriptor. |
| 984263bc | 3217 | */ |
| 984263bc | 3218 | int |
| 753fd850 | 3219 | sys_ftruncate(struct ftruncate_args *uap) |
| 984263bc | 3220 | { |
| 8f6f8622 | 3221 | int error; |
| 984263bc | 3222 | |
| 8f6f8622 DRJ |
3223 | error = kern_ftruncate(uap->fd, uap->length); |
| 3224 | ||
| 3225 | return (error); | |
| 984263bc MD |
3226 | } |
| 3227 | ||
| 984263bc | 3228 | /* |
| 41c20dac MD |
3229 | * fsync(int fd) |
| 3230 | * | |
| 984263bc MD |
3231 | * Sync an open file. |
| 3232 | */ | |
| 984263bc MD |
3233 | /* ARGSUSED */ |
| 3234 | int | |
| 753fd850 | 3235 | sys_fsync(struct fsync_args *uap) |
| 984263bc | 3236 | { |
| dadab5e9 MD |
3237 | struct thread *td = curthread; |
| 3238 | struct proc *p = td->td_proc; | |
| 41c20dac | 3239 | struct vnode *vp; |
| 984263bc MD |
3240 | struct file *fp; |
| 3241 | vm_object_t obj; | |
| 3242 | int error; | |
| 3243 | ||
| 5b287bba | 3244 | if ((error = holdvnode(p->p_fd, uap->fd, &fp)) != 0) |
| 984263bc MD |
3245 | return (error); |
| 3246 | vp = (struct vnode *)fp->f_data; | |
| ca466bae | 3247 | vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); |
| 7540ab49 | 3248 | if ((obj = vp->v_object) != NULL) |
| 984263bc | 3249 | vm_object_page_clean(obj, 0, 0, 0); |