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