1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1989, 1993 5 * The Regents of the University of California. All rights reserved. 6 * (c) UNIX System Laboratories, Inc. 7 * All or some portions of this file are derived from material licensed 8 * to the University of California by American Telephone and Telegraph 9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 10 * the permission of UNIX System Laboratories, Inc. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)vfs_syscalls.c 8.13 (Berkeley) 4/15/94 37 */ 38 39 #include <sys/cdefs.h> 40 __FBSDID("$FreeBSD$"); 41 42 #include "opt_capsicum.h" 43 #include "opt_ktrace.h" 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/bio.h> 48 #include <sys/buf.h> 49 #include <sys/capsicum.h> 50 #include <sys/disk.h> 51 #include <sys/sysent.h> 52 #include <sys/malloc.h> 53 #include <sys/mount.h> 54 #include <sys/mutex.h> 55 #include <sys/sysproto.h> 56 #include <sys/namei.h> 57 #include <sys/filedesc.h> 58 #include <sys/kernel.h> 59 #include <sys/fcntl.h> 60 #include <sys/file.h> 61 #include <sys/filio.h> 62 #include <sys/limits.h> 63 #include <sys/linker.h> 64 #include <sys/rwlock.h> 65 #include <sys/sdt.h> 66 #include <sys/stat.h> 67 #include <sys/sx.h> 68 #include <sys/unistd.h> 69 #include <sys/vnode.h> 70 #include <sys/priv.h> 71 #include <sys/proc.h> 72 #include <sys/dirent.h> 73 #include <sys/jail.h> 74 #include <sys/syscallsubr.h> 75 #include <sys/sysctl.h> 76 #ifdef KTRACE 77 #include <sys/ktrace.h> 78 #endif 79 80 #include <machine/stdarg.h> 81 82 #include <security/audit/audit.h> 83 #include <security/mac/mac_framework.h> 84 85 #include <vm/vm.h> 86 #include <vm/vm_object.h> 87 #include <vm/vm_page.h> 88 #include <vm/uma.h> 89 90 #include <ufs/ufs/quota.h> 91 92 MALLOC_DEFINE(M_FADVISE, "fadvise", "posix_fadvise(2) information"); 93 94 SDT_PROVIDER_DEFINE(vfs); 95 SDT_PROBE_DEFINE2(vfs, , stat, mode, "char *", "int"); 96 SDT_PROBE_DEFINE2(vfs, , stat, reg, "char *", "int"); 97 98 static int kern_chflagsat(struct thread *td, int fd, const char *path, 99 enum uio_seg pathseg, u_long flags, int atflag); 100 static int setfflags(struct thread *td, struct vnode *, u_long); 101 static int getutimes(const struct timeval *, enum uio_seg, struct timespec *); 102 static int getutimens(const struct timespec *, enum uio_seg, 103 struct timespec *, int *); 104 static int setutimes(struct thread *td, struct vnode *, 105 const struct timespec *, int, int); 106 static int vn_access(struct vnode *vp, int user_flags, struct ucred *cred, 107 struct thread *td); 108 static int kern_fhlinkat(struct thread *td, int fd, const char *path, 109 enum uio_seg pathseg, fhandle_t *fhp); 110 static int kern_getfhat(struct thread *td, int flags, int fd, 111 const char *path, enum uio_seg pathseg, fhandle_t *fhp); 112 static int kern_readlink_vp(struct vnode *vp, char *buf, enum uio_seg bufseg, 113 size_t count, struct thread *td); 114 static int kern_linkat_vp(struct thread *td, struct vnode *vp, int fd, 115 const char *path, enum uio_seg segflag); 116 117 int 118 kern_sync(struct thread *td) 119 { 120 struct mount *mp, *nmp; 121 int save; 122 123 mtx_lock(&mountlist_mtx); 124 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) { 125 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) { 126 nmp = TAILQ_NEXT(mp, mnt_list); 127 continue; 128 } 129 if ((mp->mnt_flag & MNT_RDONLY) == 0 && 130 vn_start_write(NULL, &mp, V_NOWAIT) == 0) { 131 save = curthread_pflags_set(TDP_SYNCIO); 132 vfs_periodic(mp, MNT_NOWAIT); 133 VFS_SYNC(mp, MNT_NOWAIT); 134 curthread_pflags_restore(save); 135 vn_finished_write(mp); 136 } 137 mtx_lock(&mountlist_mtx); 138 nmp = TAILQ_NEXT(mp, mnt_list); 139 vfs_unbusy(mp); 140 } 141 mtx_unlock(&mountlist_mtx); 142 return (0); 143 } 144 145 /* 146 * Sync each mounted filesystem. 147 */ 148 #ifndef _SYS_SYSPROTO_H_ 149 struct sync_args { 150 int dummy; 151 }; 152 #endif 153 /* ARGSUSED */ 154 int 155 sys_sync(struct thread *td, struct sync_args *uap) 156 { 157 158 return (kern_sync(td)); 159 } 160 161 /* 162 * Change filesystem quotas. 163 */ 164 #ifndef _SYS_SYSPROTO_H_ 165 struct quotactl_args { 166 char *path; 167 int cmd; 168 int uid; 169 caddr_t arg; 170 }; 171 #endif 172 int 173 sys_quotactl(struct thread *td, struct quotactl_args *uap) 174 { 175 struct mount *mp; 176 struct nameidata nd; 177 int error; 178 179 AUDIT_ARG_CMD(uap->cmd); 180 AUDIT_ARG_UID(uap->uid); 181 if (!prison_allow(td->td_ucred, PR_ALLOW_QUOTAS)) 182 return (EPERM); 183 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE, 184 uap->path, td); 185 if ((error = namei(&nd)) != 0) 186 return (error); 187 NDFREE(&nd, NDF_ONLY_PNBUF); 188 mp = nd.ni_vp->v_mount; 189 vfs_ref(mp); 190 vput(nd.ni_vp); 191 error = vfs_busy(mp, 0); 192 if (error != 0) { 193 vfs_rel(mp); 194 return (error); 195 } 196 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg); 197 198 /* 199 * Since quota on operation typically needs to open quota 200 * file, the Q_QUOTAON handler needs to unbusy the mount point 201 * before calling into namei. Otherwise, unmount might be 202 * started between two vfs_busy() invocations (first is our, 203 * second is from mount point cross-walk code in lookup()), 204 * causing deadlock. 205 * 206 * Require that Q_QUOTAON handles the vfs_busy() reference on 207 * its own, always returning with ubusied mount point. 208 */ 209 if ((uap->cmd >> SUBCMDSHIFT) != Q_QUOTAON && 210 (uap->cmd >> SUBCMDSHIFT) != Q_QUOTAOFF) 211 vfs_unbusy(mp); 212 vfs_rel(mp); 213 return (error); 214 } 215 216 /* 217 * Used by statfs conversion routines to scale the block size up if 218 * necessary so that all of the block counts are <= 'max_size'. Note 219 * that 'max_size' should be a bitmask, i.e. 2^n - 1 for some non-zero 220 * value of 'n'. 221 */ 222 void 223 statfs_scale_blocks(struct statfs *sf, long max_size) 224 { 225 uint64_t count; 226 int shift; 227 228 KASSERT(powerof2(max_size + 1), ("%s: invalid max_size", __func__)); 229 230 /* 231 * Attempt to scale the block counts to give a more accurate 232 * overview to userland of the ratio of free space to used 233 * space. To do this, find the largest block count and compute 234 * a divisor that lets it fit into a signed integer <= max_size. 235 */ 236 if (sf->f_bavail < 0) 237 count = -sf->f_bavail; 238 else 239 count = sf->f_bavail; 240 count = MAX(sf->f_blocks, MAX(sf->f_bfree, count)); 241 if (count <= max_size) 242 return; 243 244 count >>= flsl(max_size); 245 shift = 0; 246 while (count > 0) { 247 shift++; 248 count >>=1; 249 } 250 251 sf->f_bsize <<= shift; 252 sf->f_blocks >>= shift; 253 sf->f_bfree >>= shift; 254 sf->f_bavail >>= shift; 255 } 256 257 static int 258 kern_do_statfs(struct thread *td, struct mount *mp, struct statfs *buf) 259 { 260 int error; 261 262 if (mp == NULL) 263 return (EBADF); 264 error = vfs_busy(mp, 0); 265 vfs_rel(mp); 266 if (error != 0) 267 return (error); 268 #ifdef MAC 269 error = mac_mount_check_stat(td->td_ucred, mp); 270 if (error != 0) 271 goto out; 272 #endif 273 error = VFS_STATFS(mp, buf); 274 if (error != 0) 275 goto out; 276 if (priv_check_cred_vfs_generation(td->td_ucred)) { 277 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0; 278 prison_enforce_statfs(td->td_ucred, mp, buf); 279 } 280 out: 281 vfs_unbusy(mp); 282 return (error); 283 } 284 285 /* 286 * Get filesystem statistics. 287 */ 288 #ifndef _SYS_SYSPROTO_H_ 289 struct statfs_args { 290 char *path; 291 struct statfs *buf; 292 }; 293 #endif 294 int 295 sys_statfs(struct thread *td, struct statfs_args *uap) 296 { 297 struct statfs *sfp; 298 int error; 299 300 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK); 301 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp); 302 if (error == 0) 303 error = copyout(sfp, uap->buf, sizeof(struct statfs)); 304 free(sfp, M_STATFS); 305 return (error); 306 } 307 308 int 309 kern_statfs(struct thread *td, const char *path, enum uio_seg pathseg, 310 struct statfs *buf) 311 { 312 struct mount *mp; 313 struct nameidata nd; 314 int error; 315 316 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1, 317 pathseg, path, td); 318 error = namei(&nd); 319 if (error != 0) 320 return (error); 321 mp = nd.ni_vp->v_mount; 322 vfs_ref(mp); 323 NDFREE(&nd, NDF_ONLY_PNBUF); 324 vput(nd.ni_vp); 325 return (kern_do_statfs(td, mp, buf)); 326 } 327 328 /* 329 * Get filesystem statistics. 330 */ 331 #ifndef _SYS_SYSPROTO_H_ 332 struct fstatfs_args { 333 int fd; 334 struct statfs *buf; 335 }; 336 #endif 337 int 338 sys_fstatfs(struct thread *td, struct fstatfs_args *uap) 339 { 340 struct statfs *sfp; 341 int error; 342 343 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK); 344 error = kern_fstatfs(td, uap->fd, sfp); 345 if (error == 0) 346 error = copyout(sfp, uap->buf, sizeof(struct statfs)); 347 free(sfp, M_STATFS); 348 return (error); 349 } 350 351 int 352 kern_fstatfs(struct thread *td, int fd, struct statfs *buf) 353 { 354 struct file *fp; 355 struct mount *mp; 356 struct vnode *vp; 357 int error; 358 359 AUDIT_ARG_FD(fd); 360 error = getvnode(td, fd, &cap_fstatfs_rights, &fp); 361 if (error != 0) 362 return (error); 363 vp = fp->f_vnode; 364 vn_lock(vp, LK_SHARED | LK_RETRY); 365 #ifdef AUDIT 366 AUDIT_ARG_VNODE1(vp); 367 #endif 368 mp = vp->v_mount; 369 if (mp != NULL) 370 vfs_ref(mp); 371 VOP_UNLOCK(vp); 372 fdrop(fp, td); 373 return (kern_do_statfs(td, mp, buf)); 374 } 375 376 /* 377 * Get statistics on all filesystems. 378 */ 379 #ifndef _SYS_SYSPROTO_H_ 380 struct getfsstat_args { 381 struct statfs *buf; 382 long bufsize; 383 int mode; 384 }; 385 #endif 386 int 387 sys_getfsstat(struct thread *td, struct getfsstat_args *uap) 388 { 389 size_t count; 390 int error; 391 392 if (uap->bufsize < 0 || uap->bufsize > SIZE_MAX) 393 return (EINVAL); 394 error = kern_getfsstat(td, &uap->buf, uap->bufsize, &count, 395 UIO_USERSPACE, uap->mode); 396 if (error == 0) 397 td->td_retval[0] = count; 398 return (error); 399 } 400 401 /* 402 * If (bufsize > 0 && bufseg == UIO_SYSSPACE) 403 * The caller is responsible for freeing memory which will be allocated 404 * in '*buf'. 405 */ 406 int 407 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize, 408 size_t *countp, enum uio_seg bufseg, int mode) 409 { 410 struct mount *mp, *nmp; 411 struct statfs *sfsp, *sp, *sptmp, *tofree; 412 size_t count, maxcount; 413 int error; 414 415 switch (mode) { 416 case MNT_WAIT: 417 case MNT_NOWAIT: 418 break; 419 default: 420 if (bufseg == UIO_SYSSPACE) 421 *buf = NULL; 422 return (EINVAL); 423 } 424 restart: 425 maxcount = bufsize / sizeof(struct statfs); 426 if (bufsize == 0) { 427 sfsp = NULL; 428 tofree = NULL; 429 } else if (bufseg == UIO_USERSPACE) { 430 sfsp = *buf; 431 tofree = NULL; 432 } else /* if (bufseg == UIO_SYSSPACE) */ { 433 count = 0; 434 mtx_lock(&mountlist_mtx); 435 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 436 count++; 437 } 438 mtx_unlock(&mountlist_mtx); 439 if (maxcount > count) 440 maxcount = count; 441 tofree = sfsp = *buf = malloc(maxcount * sizeof(struct statfs), 442 M_STATFS, M_WAITOK); 443 } 444 count = 0; 445 mtx_lock(&mountlist_mtx); 446 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) { 447 if (prison_canseemount(td->td_ucred, mp) != 0) { 448 nmp = TAILQ_NEXT(mp, mnt_list); 449 continue; 450 } 451 #ifdef MAC 452 if (mac_mount_check_stat(td->td_ucred, mp) != 0) { 453 nmp = TAILQ_NEXT(mp, mnt_list); 454 continue; 455 } 456 #endif 457 if (mode == MNT_WAIT) { 458 if (vfs_busy(mp, MBF_MNTLSTLOCK) != 0) { 459 /* 460 * If vfs_busy() failed, and MBF_NOWAIT 461 * wasn't passed, then the mp is gone. 462 * Furthermore, because of MBF_MNTLSTLOCK, 463 * the mountlist_mtx was dropped. We have 464 * no other choice than to start over. 465 */ 466 mtx_unlock(&mountlist_mtx); 467 free(tofree, M_STATFS); 468 goto restart; 469 } 470 } else { 471 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) != 0) { 472 nmp = TAILQ_NEXT(mp, mnt_list); 473 continue; 474 } 475 } 476 if (sfsp != NULL && count < maxcount) { 477 sp = &mp->mnt_stat; 478 /* 479 * If MNT_NOWAIT is specified, do not refresh 480 * the fsstat cache. 481 */ 482 if (mode != MNT_NOWAIT) { 483 error = VFS_STATFS(mp, sp); 484 if (error != 0) { 485 mtx_lock(&mountlist_mtx); 486 nmp = TAILQ_NEXT(mp, mnt_list); 487 vfs_unbusy(mp); 488 continue; 489 } 490 } 491 if (priv_check_cred_vfs_generation(td->td_ucred)) { 492 sptmp = malloc(sizeof(struct statfs), M_STATFS, 493 M_WAITOK); 494 *sptmp = *sp; 495 sptmp->f_fsid.val[0] = sptmp->f_fsid.val[1] = 0; 496 prison_enforce_statfs(td->td_ucred, mp, sptmp); 497 sp = sptmp; 498 } else 499 sptmp = NULL; 500 if (bufseg == UIO_SYSSPACE) { 501 bcopy(sp, sfsp, sizeof(*sp)); 502 free(sptmp, M_STATFS); 503 } else /* if (bufseg == UIO_USERSPACE) */ { 504 error = copyout(sp, sfsp, sizeof(*sp)); 505 free(sptmp, M_STATFS); 506 if (error != 0) { 507 vfs_unbusy(mp); 508 return (error); 509 } 510 } 511 sfsp++; 512 } 513 count++; 514 mtx_lock(&mountlist_mtx); 515 nmp = TAILQ_NEXT(mp, mnt_list); 516 vfs_unbusy(mp); 517 } 518 mtx_unlock(&mountlist_mtx); 519 if (sfsp != NULL && count > maxcount) 520 *countp = maxcount; 521 else 522 *countp = count; 523 return (0); 524 } 525 526 #ifdef COMPAT_FREEBSD4 527 /* 528 * Get old format filesystem statistics. 529 */ 530 static void freebsd4_cvtstatfs(struct statfs *, struct ostatfs *); 531 532 #ifndef _SYS_SYSPROTO_H_ 533 struct freebsd4_statfs_args { 534 char *path; 535 struct ostatfs *buf; 536 }; 537 #endif 538 int 539 freebsd4_statfs(struct thread *td, struct freebsd4_statfs_args *uap) 540 { 541 struct ostatfs osb; 542 struct statfs *sfp; 543 int error; 544 545 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK); 546 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp); 547 if (error == 0) { 548 freebsd4_cvtstatfs(sfp, &osb); 549 error = copyout(&osb, uap->buf, sizeof(osb)); 550 } 551 free(sfp, M_STATFS); 552 return (error); 553 } 554 555 /* 556 * Get filesystem statistics. 557 */ 558 #ifndef _SYS_SYSPROTO_H_ 559 struct freebsd4_fstatfs_args { 560 int fd; 561 struct ostatfs *buf; 562 }; 563 #endif 564 int 565 freebsd4_fstatfs(struct thread *td, struct freebsd4_fstatfs_args *uap) 566 { 567 struct ostatfs osb; 568 struct statfs *sfp; 569 int error; 570 571 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK); 572 error = kern_fstatfs(td, uap->fd, sfp); 573 if (error == 0) { 574 freebsd4_cvtstatfs(sfp, &osb); 575 error = copyout(&osb, uap->buf, sizeof(osb)); 576 } 577 free(sfp, M_STATFS); 578 return (error); 579 } 580 581 /* 582 * Get statistics on all filesystems. 583 */ 584 #ifndef _SYS_SYSPROTO_H_ 585 struct freebsd4_getfsstat_args { 586 struct ostatfs *buf; 587 long bufsize; 588 int mode; 589 }; 590 #endif 591 int 592 freebsd4_getfsstat(struct thread *td, struct freebsd4_getfsstat_args *uap) 593 { 594 struct statfs *buf, *sp; 595 struct ostatfs osb; 596 size_t count, size; 597 int error; 598 599 if (uap->bufsize < 0) 600 return (EINVAL); 601 count = uap->bufsize / sizeof(struct ostatfs); 602 if (count > SIZE_MAX / sizeof(struct statfs)) 603 return (EINVAL); 604 size = count * sizeof(struct statfs); 605 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE, 606 uap->mode); 607 if (error == 0) 608 td->td_retval[0] = count; 609 if (size != 0) { 610 sp = buf; 611 while (count != 0 && error == 0) { 612 freebsd4_cvtstatfs(sp, &osb); 613 error = copyout(&osb, uap->buf, sizeof(osb)); 614 sp++; 615 uap->buf++; 616 count--; 617 } 618 free(buf, M_STATFS); 619 } 620 return (error); 621 } 622 623 /* 624 * Implement fstatfs() for (NFS) file handles. 625 */ 626 #ifndef _SYS_SYSPROTO_H_ 627 struct freebsd4_fhstatfs_args { 628 struct fhandle *u_fhp; 629 struct ostatfs *buf; 630 }; 631 #endif 632 int 633 freebsd4_fhstatfs(struct thread *td, struct freebsd4_fhstatfs_args *uap) 634 { 635 struct ostatfs osb; 636 struct statfs *sfp; 637 fhandle_t fh; 638 int error; 639 640 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t)); 641 if (error != 0) 642 return (error); 643 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK); 644 error = kern_fhstatfs(td, fh, sfp); 645 if (error == 0) { 646 freebsd4_cvtstatfs(sfp, &osb); 647 error = copyout(&osb, uap->buf, sizeof(osb)); 648 } 649 free(sfp, M_STATFS); 650 return (error); 651 } 652 653 /* 654 * Convert a new format statfs structure to an old format statfs structure. 655 */ 656 static void 657 freebsd4_cvtstatfs(struct statfs *nsp, struct ostatfs *osp) 658 { 659 660 statfs_scale_blocks(nsp, LONG_MAX); 661 bzero(osp, sizeof(*osp)); 662 osp->f_bsize = nsp->f_bsize; 663 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX); 664 osp->f_blocks = nsp->f_blocks; 665 osp->f_bfree = nsp->f_bfree; 666 osp->f_bavail = nsp->f_bavail; 667 osp->f_files = MIN(nsp->f_files, LONG_MAX); 668 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX); 669 osp->f_owner = nsp->f_owner; 670 osp->f_type = nsp->f_type; 671 osp->f_flags = nsp->f_flags; 672 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX); 673 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX); 674 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX); 675 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX); 676 strlcpy(osp->f_fstypename, nsp->f_fstypename, 677 MIN(MFSNAMELEN, OMFSNAMELEN)); 678 strlcpy(osp->f_mntonname, nsp->f_mntonname, 679 MIN(MNAMELEN, OMNAMELEN)); 680 strlcpy(osp->f_mntfromname, nsp->f_mntfromname, 681 MIN(MNAMELEN, OMNAMELEN)); 682 osp->f_fsid = nsp->f_fsid; 683 } 684 #endif /* COMPAT_FREEBSD4 */ 685 686 #if defined(COMPAT_FREEBSD11) 687 /* 688 * Get old format filesystem statistics. 689 */ 690 static void freebsd11_cvtstatfs(struct statfs *, struct freebsd11_statfs *); 691 692 int 693 freebsd11_statfs(struct thread *td, struct freebsd11_statfs_args *uap) 694 { 695 struct freebsd11_statfs osb; 696 struct statfs *sfp; 697 int error; 698 699 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK); 700 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp); 701 if (error == 0) { 702 freebsd11_cvtstatfs(sfp, &osb); 703 error = copyout(&osb, uap->buf, sizeof(osb)); 704 } 705 free(sfp, M_STATFS); 706 return (error); 707 } 708 709 /* 710 * Get filesystem statistics. 711 */ 712 int 713 freebsd11_fstatfs(struct thread *td, struct freebsd11_fstatfs_args *uap) 714 { 715 struct freebsd11_statfs osb; 716 struct statfs *sfp; 717 int error; 718 719 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK); 720 error = kern_fstatfs(td, uap->fd, sfp); 721 if (error == 0) { 722 freebsd11_cvtstatfs(sfp, &osb); 723 error = copyout(&osb, uap->buf, sizeof(osb)); 724 } 725 free(sfp, M_STATFS); 726 return (error); 727 } 728 729 /* 730 * Get statistics on all filesystems. 731 */ 732 int 733 freebsd11_getfsstat(struct thread *td, struct freebsd11_getfsstat_args *uap) 734 { 735 struct freebsd11_statfs osb; 736 struct statfs *buf, *sp; 737 size_t count, size; 738 int error; 739 740 count = uap->bufsize / sizeof(struct ostatfs); 741 size = count * sizeof(struct statfs); 742 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE, 743 uap->mode); 744 if (error == 0) 745 td->td_retval[0] = count; 746 if (size > 0) { 747 sp = buf; 748 while (count > 0 && error == 0) { 749 freebsd11_cvtstatfs(sp, &osb); 750 error = copyout(&osb, uap->buf, sizeof(osb)); 751 sp++; 752 uap->buf++; 753 count--; 754 } 755 free(buf, M_STATFS); 756 } 757 return (error); 758 } 759 760 /* 761 * Implement fstatfs() for (NFS) file handles. 762 */ 763 int 764 freebsd11_fhstatfs(struct thread *td, struct freebsd11_fhstatfs_args *uap) 765 { 766 struct freebsd11_statfs osb; 767 struct statfs *sfp; 768 fhandle_t fh; 769 int error; 770 771 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t)); 772 if (error) 773 return (error); 774 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK); 775 error = kern_fhstatfs(td, fh, sfp); 776 if (error == 0) { 777 freebsd11_cvtstatfs(sfp, &osb); 778 error = copyout(&osb, uap->buf, sizeof(osb)); 779 } 780 free(sfp, M_STATFS); 781 return (error); 782 } 783 784 /* 785 * Convert a new format statfs structure to an old format statfs structure. 786 */ 787 static void 788 freebsd11_cvtstatfs(struct statfs *nsp, struct freebsd11_statfs *osp) 789 { 790 791 bzero(osp, sizeof(*osp)); 792 osp->f_version = FREEBSD11_STATFS_VERSION; 793 osp->f_type = nsp->f_type; 794 osp->f_flags = nsp->f_flags; 795 osp->f_bsize = nsp->f_bsize; 796 osp->f_iosize = nsp->f_iosize; 797 osp->f_blocks = nsp->f_blocks; 798 osp->f_bfree = nsp->f_bfree; 799 osp->f_bavail = nsp->f_bavail; 800 osp->f_files = nsp->f_files; 801 osp->f_ffree = nsp->f_ffree; 802 osp->f_syncwrites = nsp->f_syncwrites; 803 osp->f_asyncwrites = nsp->f_asyncwrites; 804 osp->f_syncreads = nsp->f_syncreads; 805 osp->f_asyncreads = nsp->f_asyncreads; 806 osp->f_namemax = nsp->f_namemax; 807 osp->f_owner = nsp->f_owner; 808 osp->f_fsid = nsp->f_fsid; 809 strlcpy(osp->f_fstypename, nsp->f_fstypename, 810 MIN(MFSNAMELEN, sizeof(osp->f_fstypename))); 811 strlcpy(osp->f_mntonname, nsp->f_mntonname, 812 MIN(MNAMELEN, sizeof(osp->f_mntonname))); 813 strlcpy(osp->f_mntfromname, nsp->f_mntfromname, 814 MIN(MNAMELEN, sizeof(osp->f_mntfromname))); 815 } 816 #endif /* COMPAT_FREEBSD11 */ 817 818 /* 819 * Change current working directory to a given file descriptor. 820 */ 821 #ifndef _SYS_SYSPROTO_H_ 822 struct fchdir_args { 823 int fd; 824 }; 825 #endif 826 int 827 sys_fchdir(struct thread *td, struct fchdir_args *uap) 828 { 829 struct vnode *vp, *tdp; 830 struct mount *mp; 831 struct file *fp; 832 int error; 833 834 AUDIT_ARG_FD(uap->fd); 835 error = getvnode(td, uap->fd, &cap_fchdir_rights, 836 &fp); 837 if (error != 0) 838 return (error); 839 vp = fp->f_vnode; 840 vrefact(vp); 841 fdrop(fp, td); 842 vn_lock(vp, LK_SHARED | LK_RETRY); 843 AUDIT_ARG_VNODE1(vp); 844 error = change_dir(vp, td); 845 while (!error && (mp = vp->v_mountedhere) != NULL) { 846 if (vfs_busy(mp, 0)) 847 continue; 848 error = VFS_ROOT(mp, LK_SHARED, &tdp); 849 vfs_unbusy(mp); 850 if (error != 0) 851 break; 852 vput(vp); 853 vp = tdp; 854 } 855 if (error != 0) { 856 vput(vp); 857 return (error); 858 } 859 VOP_UNLOCK(vp); 860 pwd_chdir(td, vp); 861 return (0); 862 } 863 864 /* 865 * Change current working directory (``.''). 866 */ 867 #ifndef _SYS_SYSPROTO_H_ 868 struct chdir_args { 869 char *path; 870 }; 871 #endif 872 int 873 sys_chdir(struct thread *td, struct chdir_args *uap) 874 { 875 876 return (kern_chdir(td, uap->path, UIO_USERSPACE)); 877 } 878 879 int 880 kern_chdir(struct thread *td, const char *path, enum uio_seg pathseg) 881 { 882 struct nameidata nd; 883 int error; 884 885 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1, 886 pathseg, path, td); 887 if ((error = namei(&nd)) != 0) 888 return (error); 889 if ((error = change_dir(nd.ni_vp, td)) != 0) { 890 vput(nd.ni_vp); 891 NDFREE(&nd, NDF_ONLY_PNBUF); 892 return (error); 893 } 894 VOP_UNLOCK(nd.ni_vp); 895 NDFREE(&nd, NDF_ONLY_PNBUF); 896 pwd_chdir(td, nd.ni_vp); 897 return (0); 898 } 899 900 /* 901 * Change notion of root (``/'') directory. 902 */ 903 #ifndef _SYS_SYSPROTO_H_ 904 struct chroot_args { 905 char *path; 906 }; 907 #endif 908 int 909 sys_chroot(struct thread *td, struct chroot_args *uap) 910 { 911 struct nameidata nd; 912 int error; 913 914 error = priv_check(td, PRIV_VFS_CHROOT); 915 if (error != 0) 916 return (error); 917 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1, 918 UIO_USERSPACE, uap->path, td); 919 error = namei(&nd); 920 if (error != 0) 921 goto error; 922 error = change_dir(nd.ni_vp, td); 923 if (error != 0) 924 goto e_vunlock; 925 #ifdef MAC 926 error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp); 927 if (error != 0) 928 goto e_vunlock; 929 #endif 930 VOP_UNLOCK(nd.ni_vp); 931 error = pwd_chroot(td, nd.ni_vp); 932 vrele(nd.ni_vp); 933 NDFREE(&nd, NDF_ONLY_PNBUF); 934 return (error); 935 e_vunlock: 936 vput(nd.ni_vp); 937 error: 938 NDFREE(&nd, NDF_ONLY_PNBUF); 939 return (error); 940 } 941 942 /* 943 * Common routine for chroot and chdir. Callers must provide a locked vnode 944 * instance. 945 */ 946 int 947 change_dir(struct vnode *vp, struct thread *td) 948 { 949 #ifdef MAC 950 int error; 951 #endif 952 953 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked"); 954 if (vp->v_type != VDIR) 955 return (ENOTDIR); 956 #ifdef MAC 957 error = mac_vnode_check_chdir(td->td_ucred, vp); 958 if (error != 0) 959 return (error); 960 #endif 961 return (VOP_ACCESS(vp, VEXEC, td->td_ucred, td)); 962 } 963 964 static __inline void 965 flags_to_rights(int flags, cap_rights_t *rightsp) 966 { 967 968 if (flags & O_EXEC) { 969 cap_rights_set_one(rightsp, CAP_FEXECVE); 970 } else { 971 switch ((flags & O_ACCMODE)) { 972 case O_RDONLY: 973 cap_rights_set_one(rightsp, CAP_READ); 974 break; 975 case O_RDWR: 976 cap_rights_set_one(rightsp, CAP_READ); 977 /* FALLTHROUGH */ 978 case O_WRONLY: 979 cap_rights_set_one(rightsp, CAP_WRITE); 980 if (!(flags & (O_APPEND | O_TRUNC))) 981 cap_rights_set_one(rightsp, CAP_SEEK); 982 break; 983 } 984 } 985 986 if (flags & O_CREAT) 987 cap_rights_set_one(rightsp, CAP_CREATE); 988 989 if (flags & O_TRUNC) 990 cap_rights_set_one(rightsp, CAP_FTRUNCATE); 991 992 if (flags & (O_SYNC | O_FSYNC)) 993 cap_rights_set_one(rightsp, CAP_FSYNC); 994 995 if (flags & (O_EXLOCK | O_SHLOCK)) 996 cap_rights_set_one(rightsp, CAP_FLOCK); 997 } 998 999 /* 1000 * Check permissions, allocate an open file structure, and call the device 1001 * open routine if any. 1002 */ 1003 #ifndef _SYS_SYSPROTO_H_ 1004 struct open_args { 1005 char *path; 1006 int flags; 1007 int mode; 1008 }; 1009 #endif 1010 int 1011 sys_open(struct thread *td, struct open_args *uap) 1012 { 1013 1014 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 1015 uap->flags, uap->mode)); 1016 } 1017 1018 #ifndef _SYS_SYSPROTO_H_ 1019 struct openat_args { 1020 int fd; 1021 char *path; 1022 int flag; 1023 int mode; 1024 }; 1025 #endif 1026 int 1027 sys_openat(struct thread *td, struct openat_args *uap) 1028 { 1029 1030 AUDIT_ARG_FD(uap->fd); 1031 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag, 1032 uap->mode)); 1033 } 1034 1035 int 1036 kern_openat(struct thread *td, int fd, const char *path, enum uio_seg pathseg, 1037 int flags, int mode) 1038 { 1039 struct proc *p = td->td_proc; 1040 struct filedesc *fdp = p->p_fd; 1041 struct file *fp; 1042 struct vnode *vp; 1043 struct nameidata nd; 1044 cap_rights_t rights; 1045 int cmode, error, indx; 1046 1047 indx = -1; 1048 1049 AUDIT_ARG_FFLAGS(flags); 1050 AUDIT_ARG_MODE(mode); 1051 cap_rights_init_one(&rights, CAP_LOOKUP); 1052 flags_to_rights(flags, &rights); 1053 /* 1054 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags 1055 * may be specified. 1056 */ 1057 if (flags & O_EXEC) { 1058 if (flags & O_ACCMODE) 1059 return (EINVAL); 1060 } else if ((flags & O_ACCMODE) == O_ACCMODE) { 1061 return (EINVAL); 1062 } else { 1063 flags = FFLAGS(flags); 1064 } 1065 1066 /* 1067 * Allocate a file structure. The descriptor to reference it 1068 * is allocated and set by finstall() below. 1069 */ 1070 error = falloc_noinstall(td, &fp); 1071 if (error != 0) 1072 return (error); 1073 /* 1074 * An extra reference on `fp' has been held for us by 1075 * falloc_noinstall(). 1076 */ 1077 /* Set the flags early so the finit in devfs can pick them up. */ 1078 fp->f_flag = flags & FMASK; 1079 cmode = ((mode & ~fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT; 1080 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd, 1081 &rights, td); 1082 td->td_dupfd = -1; /* XXX check for fdopen */ 1083 error = vn_open(&nd, &flags, cmode, fp); 1084 if (error != 0) { 1085 /* 1086 * If the vn_open replaced the method vector, something 1087 * wonderous happened deep below and we just pass it up 1088 * pretending we know what we do. 1089 */ 1090 if (error == ENXIO && fp->f_ops != &badfileops) 1091 goto success; 1092 1093 /* 1094 * Handle special fdopen() case. bleh. 1095 * 1096 * Don't do this for relative (capability) lookups; we don't 1097 * understand exactly what would happen, and we don't think 1098 * that it ever should. 1099 */ 1100 if ((nd.ni_lcf & NI_LCF_STRICTRELATIVE) == 0 && 1101 (error == ENODEV || error == ENXIO) && 1102 td->td_dupfd >= 0) { 1103 error = dupfdopen(td, fdp, td->td_dupfd, flags, error, 1104 &indx); 1105 if (error == 0) 1106 goto success; 1107 } 1108 1109 goto bad; 1110 } 1111 td->td_dupfd = 0; 1112 NDFREE(&nd, NDF_ONLY_PNBUF); 1113 vp = nd.ni_vp; 1114 1115 /* 1116 * Store the vnode, for any f_type. Typically, the vnode use 1117 * count is decremented by direct call to vn_closefile() for 1118 * files that switched type in the cdevsw fdopen() method. 1119 */ 1120 fp->f_vnode = vp; 1121 /* 1122 * If the file wasn't claimed by devfs bind it to the normal 1123 * vnode operations here. 1124 */ 1125 if (fp->f_ops == &badfileops) { 1126 KASSERT(vp->v_type != VFIFO, ("Unexpected fifo.")); 1127 fp->f_seqcount = 1; 1128 finit(fp, (flags & FMASK) | (fp->f_flag & FHASLOCK), 1129 DTYPE_VNODE, vp, &vnops); 1130 } 1131 1132 VOP_UNLOCK(vp); 1133 if (flags & O_TRUNC) { 1134 error = fo_truncate(fp, 0, td->td_ucred, td); 1135 if (error != 0) 1136 goto bad; 1137 } 1138 success: 1139 /* 1140 * If we haven't already installed the FD (for dupfdopen), do so now. 1141 */ 1142 if (indx == -1) { 1143 struct filecaps *fcaps; 1144 1145 #ifdef CAPABILITIES 1146 if ((nd.ni_lcf & NI_LCF_STRICTRELATIVE) != 0) 1147 fcaps = &nd.ni_filecaps; 1148 else 1149 #endif 1150 fcaps = NULL; 1151 error = finstall(td, fp, &indx, flags, fcaps); 1152 /* On success finstall() consumes fcaps. */ 1153 if (error != 0) { 1154 filecaps_free(&nd.ni_filecaps); 1155 goto bad; 1156 } 1157 } else { 1158 filecaps_free(&nd.ni_filecaps); 1159 } 1160 1161 /* 1162 * Release our private reference, leaving the one associated with 1163 * the descriptor table intact. 1164 */ 1165 fdrop(fp, td); 1166 td->td_retval[0] = indx; 1167 return (0); 1168 bad: 1169 KASSERT(indx == -1, ("indx=%d, should be -1", indx)); 1170 fdrop(fp, td); 1171 return (error); 1172 } 1173 1174 #ifdef COMPAT_43 1175 /* 1176 * Create a file. 1177 */ 1178 #ifndef _SYS_SYSPROTO_H_ 1179 struct ocreat_args { 1180 char *path; 1181 int mode; 1182 }; 1183 #endif 1184 int 1185 ocreat(struct thread *td, struct ocreat_args *uap) 1186 { 1187 1188 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 1189 O_WRONLY | O_CREAT | O_TRUNC, uap->mode)); 1190 } 1191 #endif /* COMPAT_43 */ 1192 1193 /* 1194 * Create a special file. 1195 */ 1196 #ifndef _SYS_SYSPROTO_H_ 1197 struct mknodat_args { 1198 int fd; 1199 char *path; 1200 mode_t mode; 1201 dev_t dev; 1202 }; 1203 #endif 1204 int 1205 sys_mknodat(struct thread *td, struct mknodat_args *uap) 1206 { 1207 1208 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode, 1209 uap->dev)); 1210 } 1211 1212 #if defined(COMPAT_FREEBSD11) 1213 int 1214 freebsd11_mknod(struct thread *td, 1215 struct freebsd11_mknod_args *uap) 1216 { 1217 1218 return (kern_mknodat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 1219 uap->mode, uap->dev)); 1220 } 1221 1222 int 1223 freebsd11_mknodat(struct thread *td, 1224 struct freebsd11_mknodat_args *uap) 1225 { 1226 1227 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode, 1228 uap->dev)); 1229 } 1230 #endif /* COMPAT_FREEBSD11 */ 1231 1232 int 1233 kern_mknodat(struct thread *td, int fd, const char *path, enum uio_seg pathseg, 1234 int mode, dev_t dev) 1235 { 1236 struct vnode *vp; 1237 struct mount *mp; 1238 struct vattr vattr; 1239 struct nameidata nd; 1240 int error, whiteout = 0; 1241 1242 AUDIT_ARG_MODE(mode); 1243 AUDIT_ARG_DEV(dev); 1244 switch (mode & S_IFMT) { 1245 case S_IFCHR: 1246 case S_IFBLK: 1247 error = priv_check(td, PRIV_VFS_MKNOD_DEV); 1248 if (error == 0 && dev == VNOVAL) 1249 error = EINVAL; 1250 break; 1251 case S_IFWHT: 1252 error = priv_check(td, PRIV_VFS_MKNOD_WHT); 1253 break; 1254 case S_IFIFO: 1255 if (dev == 0) 1256 return (kern_mkfifoat(td, fd, path, pathseg, mode)); 1257 /* FALLTHROUGH */ 1258 default: 1259 error = EINVAL; 1260 break; 1261 } 1262 if (error != 0) 1263 return (error); 1264 restart: 1265 bwillwrite(); 1266 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 | 1267 NOCACHE, pathseg, path, fd, &cap_mknodat_rights, 1268 td); 1269 if ((error = namei(&nd)) != 0) 1270 return (error); 1271 vp = nd.ni_vp; 1272 if (vp != NULL) { 1273 NDFREE(&nd, NDF_ONLY_PNBUF); 1274 if (vp == nd.ni_dvp) 1275 vrele(nd.ni_dvp); 1276 else 1277 vput(nd.ni_dvp); 1278 vrele(vp); 1279 return (EEXIST); 1280 } else { 1281 VATTR_NULL(&vattr); 1282 vattr.va_mode = (mode & ALLPERMS) & 1283 ~td->td_proc->p_fd->fd_cmask; 1284 vattr.va_rdev = dev; 1285 whiteout = 0; 1286 1287 switch (mode & S_IFMT) { 1288 case S_IFCHR: 1289 vattr.va_type = VCHR; 1290 break; 1291 case S_IFBLK: 1292 vattr.va_type = VBLK; 1293 break; 1294 case S_IFWHT: 1295 whiteout = 1; 1296 break; 1297 default: 1298 panic("kern_mknod: invalid mode"); 1299 } 1300 } 1301 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 1302 NDFREE(&nd, NDF_ONLY_PNBUF); 1303 vput(nd.ni_dvp); 1304 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0) 1305 return (error); 1306 goto restart; 1307 } 1308 #ifdef MAC 1309 if (error == 0 && !whiteout) 1310 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, 1311 &nd.ni_cnd, &vattr); 1312 #endif 1313 if (error == 0) { 1314 if (whiteout) 1315 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE); 1316 else { 1317 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, 1318 &nd.ni_cnd, &vattr); 1319 if (error == 0) 1320 vput(nd.ni_vp); 1321 } 1322 } 1323 NDFREE(&nd, NDF_ONLY_PNBUF); 1324 vput(nd.ni_dvp); 1325 vn_finished_write(mp); 1326 return (error); 1327 } 1328 1329 /* 1330 * Create a named pipe. 1331 */ 1332 #ifndef _SYS_SYSPROTO_H_ 1333 struct mkfifo_args { 1334 char *path; 1335 int mode; 1336 }; 1337 #endif 1338 int 1339 sys_mkfifo(struct thread *td, struct mkfifo_args *uap) 1340 { 1341 1342 return (kern_mkfifoat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 1343 uap->mode)); 1344 } 1345 1346 #ifndef _SYS_SYSPROTO_H_ 1347 struct mkfifoat_args { 1348 int fd; 1349 char *path; 1350 mode_t mode; 1351 }; 1352 #endif 1353 int 1354 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap) 1355 { 1356 1357 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE, 1358 uap->mode)); 1359 } 1360 1361 int 1362 kern_mkfifoat(struct thread *td, int fd, const char *path, 1363 enum uio_seg pathseg, int mode) 1364 { 1365 struct mount *mp; 1366 struct vattr vattr; 1367 struct nameidata nd; 1368 int error; 1369 1370 AUDIT_ARG_MODE(mode); 1371 restart: 1372 bwillwrite(); 1373 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 | 1374 NOCACHE, pathseg, path, fd, &cap_mkfifoat_rights, 1375 td); 1376 if ((error = namei(&nd)) != 0) 1377 return (error); 1378 if (nd.ni_vp != NULL) { 1379 NDFREE(&nd, NDF_ONLY_PNBUF); 1380 if (nd.ni_vp == nd.ni_dvp) 1381 vrele(nd.ni_dvp); 1382 else 1383 vput(nd.ni_dvp); 1384 vrele(nd.ni_vp); 1385 return (EEXIST); 1386 } 1387 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 1388 NDFREE(&nd, NDF_ONLY_PNBUF); 1389 vput(nd.ni_dvp); 1390 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0) 1391 return (error); 1392 goto restart; 1393 } 1394 VATTR_NULL(&vattr); 1395 vattr.va_type = VFIFO; 1396 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_fd->fd_cmask; 1397 #ifdef MAC 1398 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd, 1399 &vattr); 1400 if (error != 0) 1401 goto out; 1402 #endif 1403 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr); 1404 if (error == 0) 1405 vput(nd.ni_vp); 1406 #ifdef MAC 1407 out: 1408 #endif 1409 vput(nd.ni_dvp); 1410 vn_finished_write(mp); 1411 NDFREE(&nd, NDF_ONLY_PNBUF); 1412 return (error); 1413 } 1414 1415 /* 1416 * Make a hard file link. 1417 */ 1418 #ifndef _SYS_SYSPROTO_H_ 1419 struct link_args { 1420 char *path; 1421 char *link; 1422 }; 1423 #endif 1424 int 1425 sys_link(struct thread *td, struct link_args *uap) 1426 { 1427 1428 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, uap->path, uap->link, 1429 UIO_USERSPACE, FOLLOW)); 1430 } 1431 1432 #ifndef _SYS_SYSPROTO_H_ 1433 struct linkat_args { 1434 int fd1; 1435 char *path1; 1436 int fd2; 1437 char *path2; 1438 int flag; 1439 }; 1440 #endif 1441 int 1442 sys_linkat(struct thread *td, struct linkat_args *uap) 1443 { 1444 int flag; 1445 1446 flag = uap->flag; 1447 if ((flag & ~(AT_SYMLINK_FOLLOW | AT_BENEATH)) != 0) 1448 return (EINVAL); 1449 1450 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2, 1451 UIO_USERSPACE, ((flag & AT_SYMLINK_FOLLOW) != 0 ? FOLLOW : 1452 NOFOLLOW) | ((flag & AT_BENEATH) != 0 ? BENEATH : 0))); 1453 } 1454 1455 int hardlink_check_uid = 0; 1456 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW, 1457 &hardlink_check_uid, 0, 1458 "Unprivileged processes cannot create hard links to files owned by other " 1459 "users"); 1460 static int hardlink_check_gid = 0; 1461 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW, 1462 &hardlink_check_gid, 0, 1463 "Unprivileged processes cannot create hard links to files owned by other " 1464 "groups"); 1465 1466 static int 1467 can_hardlink(struct vnode *vp, struct ucred *cred) 1468 { 1469 struct vattr va; 1470 int error; 1471 1472 if (!hardlink_check_uid && !hardlink_check_gid) 1473 return (0); 1474 1475 error = VOP_GETATTR(vp, &va, cred); 1476 if (error != 0) 1477 return (error); 1478 1479 if (hardlink_check_uid && cred->cr_uid != va.va_uid) { 1480 error = priv_check_cred(cred, PRIV_VFS_LINK); 1481 if (error != 0) 1482 return (error); 1483 } 1484 1485 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) { 1486 error = priv_check_cred(cred, PRIV_VFS_LINK); 1487 if (error != 0) 1488 return (error); 1489 } 1490 1491 return (0); 1492 } 1493 1494 int 1495 kern_linkat(struct thread *td, int fd1, int fd2, const char *path1, 1496 const char *path2, enum uio_seg segflag, int follow) 1497 { 1498 struct nameidata nd; 1499 int error; 1500 1501 do { 1502 bwillwrite(); 1503 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, segflag, 1504 path1, fd1, &cap_linkat_source_rights, td); 1505 if ((error = namei(&nd)) != 0) 1506 return (error); 1507 NDFREE(&nd, NDF_ONLY_PNBUF); 1508 error = kern_linkat_vp(td, nd.ni_vp, fd2, path2, segflag); 1509 } while (error == EAGAIN); 1510 return (error); 1511 } 1512 1513 static int 1514 kern_linkat_vp(struct thread *td, struct vnode *vp, int fd, const char *path, 1515 enum uio_seg segflag) 1516 { 1517 struct nameidata nd; 1518 struct mount *mp; 1519 int error; 1520 1521 if (vp->v_type == VDIR) { 1522 vrele(vp); 1523 return (EPERM); /* POSIX */ 1524 } 1525 NDINIT_ATRIGHTS(&nd, CREATE, 1526 LOCKPARENT | SAVENAME | AUDITVNODE2 | NOCACHE, segflag, path, fd, 1527 &cap_linkat_target_rights, td); 1528 if ((error = namei(&nd)) == 0) { 1529 if (nd.ni_vp != NULL) { 1530 NDFREE(&nd, NDF_ONLY_PNBUF); 1531 if (nd.ni_dvp == nd.ni_vp) 1532 vrele(nd.ni_dvp); 1533 else 1534 vput(nd.ni_dvp); 1535 vrele(nd.ni_vp); 1536 vrele(vp); 1537 return (EEXIST); 1538 } else if (nd.ni_dvp->v_mount != vp->v_mount) { 1539 /* 1540 * Cross-device link. No need to recheck 1541 * vp->v_type, since it cannot change, except 1542 * to VBAD. 1543 */ 1544 NDFREE(&nd, NDF_ONLY_PNBUF); 1545 vput(nd.ni_dvp); 1546 vrele(vp); 1547 return (EXDEV); 1548 } else if ((error = vn_lock(vp, LK_EXCLUSIVE)) == 0) { 1549 error = can_hardlink(vp, td->td_ucred); 1550 #ifdef MAC 1551 if (error == 0) 1552 error = mac_vnode_check_link(td->td_ucred, 1553 nd.ni_dvp, vp, &nd.ni_cnd); 1554 #endif 1555 if (error != 0) { 1556 vput(vp); 1557 vput(nd.ni_dvp); 1558 NDFREE(&nd, NDF_ONLY_PNBUF); 1559 return (error); 1560 } 1561 error = vn_start_write(vp, &mp, V_NOWAIT); 1562 if (error != 0) { 1563 vput(vp); 1564 vput(nd.ni_dvp); 1565 NDFREE(&nd, NDF_ONLY_PNBUF); 1566 error = vn_start_write(NULL, &mp, 1567 V_XSLEEP | PCATCH); 1568 if (error != 0) 1569 return (error); 1570 return (EAGAIN); 1571 } 1572 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd); 1573 VOP_UNLOCK(vp); 1574 vput(nd.ni_dvp); 1575 vn_finished_write(mp); 1576 NDFREE(&nd, NDF_ONLY_PNBUF); 1577 } else { 1578 vput(nd.ni_dvp); 1579 NDFREE(&nd, NDF_ONLY_PNBUF); 1580 vrele(vp); 1581 return (EAGAIN); 1582 } 1583 } 1584 vrele(vp); 1585 return (error); 1586 } 1587 1588 /* 1589 * Make a symbolic link. 1590 */ 1591 #ifndef _SYS_SYSPROTO_H_ 1592 struct symlink_args { 1593 char *path; 1594 char *link; 1595 }; 1596 #endif 1597 int 1598 sys_symlink(struct thread *td, struct symlink_args *uap) 1599 { 1600 1601 return (kern_symlinkat(td, uap->path, AT_FDCWD, uap->link, 1602 UIO_USERSPACE)); 1603 } 1604 1605 #ifndef _SYS_SYSPROTO_H_ 1606 struct symlinkat_args { 1607 char *path; 1608 int fd; 1609 char *path2; 1610 }; 1611 #endif 1612 int 1613 sys_symlinkat(struct thread *td, struct symlinkat_args *uap) 1614 { 1615 1616 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2, 1617 UIO_USERSPACE)); 1618 } 1619 1620 int 1621 kern_symlinkat(struct thread *td, const char *path1, int fd, const char *path2, 1622 enum uio_seg segflg) 1623 { 1624 struct mount *mp; 1625 struct vattr vattr; 1626 const char *syspath; 1627 char *tmppath; 1628 struct nameidata nd; 1629 int error; 1630 1631 if (segflg == UIO_SYSSPACE) { 1632 syspath = path1; 1633 } else { 1634 tmppath = uma_zalloc(namei_zone, M_WAITOK); 1635 if ((error = copyinstr(path1, tmppath, MAXPATHLEN, NULL)) != 0) 1636 goto out; 1637 syspath = tmppath; 1638 } 1639 AUDIT_ARG_TEXT(syspath); 1640 restart: 1641 bwillwrite(); 1642 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 | 1643 NOCACHE, segflg, path2, fd, &cap_symlinkat_rights, 1644 td); 1645 if ((error = namei(&nd)) != 0) 1646 goto out; 1647 if (nd.ni_vp) { 1648 NDFREE(&nd, NDF_ONLY_PNBUF); 1649 if (nd.ni_vp == nd.ni_dvp) 1650 vrele(nd.ni_dvp); 1651 else 1652 vput(nd.ni_dvp); 1653 vrele(nd.ni_vp); 1654 error = EEXIST; 1655 goto out; 1656 } 1657 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 1658 NDFREE(&nd, NDF_ONLY_PNBUF); 1659 vput(nd.ni_dvp); 1660 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0) 1661 goto out; 1662 goto restart; 1663 } 1664 VATTR_NULL(&vattr); 1665 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_fd->fd_cmask; 1666 #ifdef MAC 1667 vattr.va_type = VLNK; 1668 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd, 1669 &vattr); 1670 if (error != 0) 1671 goto out2; 1672 #endif 1673 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath); 1674 if (error == 0) 1675 vput(nd.ni_vp); 1676 #ifdef MAC 1677 out2: 1678 #endif 1679 NDFREE(&nd, NDF_ONLY_PNBUF); 1680 vput(nd.ni_dvp); 1681 vn_finished_write(mp); 1682 out: 1683 if (segflg != UIO_SYSSPACE) 1684 uma_zfree(namei_zone, tmppath); 1685 return (error); 1686 } 1687 1688 /* 1689 * Delete a whiteout from the filesystem. 1690 */ 1691 #ifndef _SYS_SYSPROTO_H_ 1692 struct undelete_args { 1693 char *path; 1694 }; 1695 #endif 1696 int 1697 sys_undelete(struct thread *td, struct undelete_args *uap) 1698 { 1699 struct mount *mp; 1700 struct nameidata nd; 1701 int error; 1702 1703 restart: 1704 bwillwrite(); 1705 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1, 1706 UIO_USERSPACE, uap->path, td); 1707 error = namei(&nd); 1708 if (error != 0) 1709 return (error); 1710 1711 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) { 1712 NDFREE(&nd, NDF_ONLY_PNBUF); 1713 if (nd.ni_vp == nd.ni_dvp) 1714 vrele(nd.ni_dvp); 1715 else 1716 vput(nd.ni_dvp); 1717 if (nd.ni_vp) 1718 vrele(nd.ni_vp); 1719 return (EEXIST); 1720 } 1721 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 1722 NDFREE(&nd, NDF_ONLY_PNBUF); 1723 vput(nd.ni_dvp); 1724 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0) 1725 return (error); 1726 goto restart; 1727 } 1728 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE); 1729 NDFREE(&nd, NDF_ONLY_PNBUF); 1730 vput(nd.ni_dvp); 1731 vn_finished_write(mp); 1732 return (error); 1733 } 1734 1735 /* 1736 * Delete a name from the filesystem. 1737 */ 1738 #ifndef _SYS_SYSPROTO_H_ 1739 struct unlink_args { 1740 char *path; 1741 }; 1742 #endif 1743 int 1744 sys_unlink(struct thread *td, struct unlink_args *uap) 1745 { 1746 1747 return (kern_funlinkat(td, AT_FDCWD, uap->path, FD_NONE, UIO_USERSPACE, 1748 0, 0)); 1749 } 1750 1751 static int 1752 kern_funlinkat_ex(struct thread *td, int dfd, const char *path, int fd, 1753 int flag, enum uio_seg pathseg, ino_t oldinum) 1754 { 1755 1756 if ((flag & ~AT_REMOVEDIR) != 0) 1757 return (EINVAL); 1758 1759 if ((flag & AT_REMOVEDIR) != 0) 1760 return (kern_frmdirat(td, dfd, path, fd, UIO_USERSPACE, 0)); 1761 1762 return (kern_funlinkat(td, dfd, path, fd, UIO_USERSPACE, 0, 0)); 1763 } 1764 1765 #ifndef _SYS_SYSPROTO_H_ 1766 struct unlinkat_args { 1767 int fd; 1768 char *path; 1769 int flag; 1770 }; 1771 #endif 1772 int 1773 sys_unlinkat(struct thread *td, struct unlinkat_args *uap) 1774 { 1775 1776 return (kern_funlinkat_ex(td, uap->fd, uap->path, FD_NONE, uap->flag, 1777 UIO_USERSPACE, 0)); 1778 } 1779 1780 #ifndef _SYS_SYSPROTO_H_ 1781 struct funlinkat_args { 1782 int dfd; 1783 const char *path; 1784 int fd; 1785 int flag; 1786 }; 1787 #endif 1788 int 1789 sys_funlinkat(struct thread *td, struct funlinkat_args *uap) 1790 { 1791 1792 return (kern_funlinkat_ex(td, uap->dfd, uap->path, uap->fd, uap->flag, 1793 UIO_USERSPACE, 0)); 1794 } 1795 1796 int 1797 kern_funlinkat(struct thread *td, int dfd, const char *path, int fd, 1798 enum uio_seg pathseg, int flag, ino_t oldinum) 1799 { 1800 struct mount *mp; 1801 struct file *fp; 1802 struct vnode *vp; 1803 struct nameidata nd; 1804 struct stat sb; 1805 int error; 1806 1807 fp = NULL; 1808 if (fd != FD_NONE) { 1809 error = getvnode(td, fd, &cap_no_rights, &fp); 1810 if (error != 0) 1811 return (error); 1812 } 1813 1814 restart: 1815 bwillwrite(); 1816 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 | 1817 ((flag & AT_BENEATH) != 0 ? BENEATH : 0), 1818 pathseg, path, dfd, &cap_unlinkat_rights, td); 1819 if ((error = namei(&nd)) != 0) { 1820 if (error == EINVAL) 1821 error = EPERM; 1822 goto fdout; 1823 } 1824 vp = nd.ni_vp; 1825 if (vp->v_type == VDIR && oldinum == 0) { 1826 error = EPERM; /* POSIX */ 1827 } else if (oldinum != 0 && 1828 ((error = vn_stat(vp, &sb, td->td_ucred, NOCRED, td)) == 0) && 1829 sb.st_ino != oldinum) { 1830 error = EIDRM; /* Identifier removed */ 1831 } else if (fp != NULL && fp->f_vnode != vp) { 1832 if (VN_IS_DOOMED(fp->f_vnode)) 1833 error = EBADF; 1834 else 1835 error = EDEADLK; 1836 } else { 1837 /* 1838 * The root of a mounted filesystem cannot be deleted. 1839 * 1840 * XXX: can this only be a VDIR case? 1841 */ 1842 if (vp->v_vflag & VV_ROOT) 1843 error = EBUSY; 1844 } 1845 if (error == 0) { 1846 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 1847 NDFREE(&nd, NDF_ONLY_PNBUF); 1848 vput(nd.ni_dvp); 1849 if (vp == nd.ni_dvp) 1850 vrele(vp); 1851 else 1852 vput(vp); 1853 if ((error = vn_start_write(NULL, &mp, 1854 V_XSLEEP | PCATCH)) != 0) { 1855 goto fdout; 1856 } 1857 goto restart; 1858 } 1859 #ifdef MAC 1860 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp, 1861 &nd.ni_cnd); 1862 if (error != 0) 1863 goto out; 1864 #endif 1865 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK); 1866 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd); 1867 #ifdef MAC 1868 out: 1869 #endif 1870 vn_finished_write(mp); 1871 } 1872 NDFREE(&nd, NDF_ONLY_PNBUF); 1873 vput(nd.ni_dvp); 1874 if (vp == nd.ni_dvp) 1875 vrele(vp); 1876 else 1877 vput(vp); 1878 fdout: 1879 if (fp != NULL) 1880 fdrop(fp, td); 1881 return (error); 1882 } 1883 1884 /* 1885 * Reposition read/write file offset. 1886 */ 1887 #ifndef _SYS_SYSPROTO_H_ 1888 struct lseek_args { 1889 int fd; 1890 int pad; 1891 off_t offset; 1892 int whence; 1893 }; 1894 #endif 1895 int 1896 sys_lseek(struct thread *td, struct lseek_args *uap) 1897 { 1898 1899 return (kern_lseek(td, uap->fd, uap->offset, uap->whence)); 1900 } 1901 1902 int 1903 kern_lseek(struct thread *td, int fd, off_t offset, int whence) 1904 { 1905 struct file *fp; 1906 int error; 1907 1908 AUDIT_ARG_FD(fd); 1909 error = fget(td, fd, &cap_seek_rights, &fp); 1910 if (error != 0) 1911 return (error); 1912 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ? 1913 fo_seek(fp, offset, whence, td) : ESPIPE; 1914 fdrop(fp, td); 1915 return (error); 1916 } 1917 1918 #if defined(COMPAT_43) 1919 /* 1920 * Reposition read/write file offset. 1921 */ 1922 #ifndef _SYS_SYSPROTO_H_ 1923 struct olseek_args { 1924 int fd; 1925 long offset; 1926 int whence; 1927 }; 1928 #endif 1929 int 1930 olseek(struct thread *td, struct olseek_args *uap) 1931 { 1932 1933 return (kern_lseek(td, uap->fd, uap->offset, uap->whence)); 1934 } 1935 #endif /* COMPAT_43 */ 1936 1937 #if defined(COMPAT_FREEBSD6) 1938 /* Version with the 'pad' argument */ 1939 int 1940 freebsd6_lseek(struct thread *td, struct freebsd6_lseek_args *uap) 1941 { 1942 1943 return (kern_lseek(td, uap->fd, uap->offset, uap->whence)); 1944 } 1945 #endif 1946 1947 /* 1948 * Check access permissions using passed credentials. 1949 */ 1950 static int 1951 vn_access(struct vnode *vp, int user_flags, struct ucred *cred, 1952 struct thread *td) 1953 { 1954 accmode_t accmode; 1955 int error; 1956 1957 /* Flags == 0 means only check for existence. */ 1958 if (user_flags == 0) 1959 return (0); 1960 1961 accmode = 0; 1962 if (user_flags & R_OK) 1963 accmode |= VREAD; 1964 if (user_flags & W_OK) 1965 accmode |= VWRITE; 1966 if (user_flags & X_OK) 1967 accmode |= VEXEC; 1968 #ifdef MAC 1969 error = mac_vnode_check_access(cred, vp, accmode); 1970 if (error != 0) 1971 return (error); 1972 #endif 1973 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0) 1974 error = VOP_ACCESS(vp, accmode, cred, td); 1975 return (error); 1976 } 1977 1978 /* 1979 * Check access permissions using "real" credentials. 1980 */ 1981 #ifndef _SYS_SYSPROTO_H_ 1982 struct access_args { 1983 char *path; 1984 int amode; 1985 }; 1986 #endif 1987 int 1988 sys_access(struct thread *td, struct access_args *uap) 1989 { 1990 1991 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 1992 0, uap->amode)); 1993 } 1994 1995 #ifndef _SYS_SYSPROTO_H_ 1996 struct faccessat_args { 1997 int dirfd; 1998 char *path; 1999 int amode; 2000 int flag; 2001 } 2002 #endif 2003 int 2004 sys_faccessat(struct thread *td, struct faccessat_args *uap) 2005 { 2006 2007 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag, 2008 uap->amode)); 2009 } 2010 2011 int 2012 kern_accessat(struct thread *td, int fd, const char *path, 2013 enum uio_seg pathseg, int flag, int amode) 2014 { 2015 struct ucred *cred, *usecred; 2016 struct vnode *vp; 2017 struct nameidata nd; 2018 int error; 2019 2020 if ((flag & ~(AT_EACCESS | AT_BENEATH)) != 0) 2021 return (EINVAL); 2022 if (amode != F_OK && (amode & ~(R_OK | W_OK | X_OK)) != 0) 2023 return (EINVAL); 2024 2025 /* 2026 * Create and modify a temporary credential instead of one that 2027 * is potentially shared (if we need one). 2028 */ 2029 cred = td->td_ucred; 2030 if ((flag & AT_EACCESS) == 0 && 2031 ((cred->cr_uid != cred->cr_ruid || 2032 cred->cr_rgid != cred->cr_groups[0]))) { 2033 usecred = crdup(cred); 2034 usecred->cr_uid = cred->cr_ruid; 2035 usecred->cr_groups[0] = cred->cr_rgid; 2036 td->td_ucred = usecred; 2037 } else 2038 usecred = cred; 2039 AUDIT_ARG_VALUE(amode); 2040 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | 2041 AUDITVNODE1 | ((flag & AT_BENEATH) != 0 ? BENEATH : 0), 2042 pathseg, path, fd, &cap_fstat_rights, td); 2043 if ((error = namei(&nd)) != 0) 2044 goto out; 2045 vp = nd.ni_vp; 2046 2047 error = vn_access(vp, amode, usecred, td); 2048 NDFREE(&nd, NDF_ONLY_PNBUF); 2049 vput(vp); 2050 out: 2051 if (usecred != cred) { 2052 td->td_ucred = cred; 2053 crfree(usecred); 2054 } 2055 return (error); 2056 } 2057 2058 /* 2059 * Check access permissions using "effective" credentials. 2060 */ 2061 #ifndef _SYS_SYSPROTO_H_ 2062 struct eaccess_args { 2063 char *path; 2064 int amode; 2065 }; 2066 #endif 2067 int 2068 sys_eaccess(struct thread *td, struct eaccess_args *uap) 2069 { 2070 2071 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 2072 AT_EACCESS, uap->amode)); 2073 } 2074 2075 #if defined(COMPAT_43) 2076 /* 2077 * Get file status; this version follows links. 2078 */ 2079 #ifndef _SYS_SYSPROTO_H_ 2080 struct ostat_args { 2081 char *path; 2082 struct ostat *ub; 2083 }; 2084 #endif 2085 int 2086 ostat(struct thread *td, struct ostat_args *uap) 2087 { 2088 struct stat sb; 2089 struct ostat osb; 2090 int error; 2091 2092 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE, 2093 &sb, NULL); 2094 if (error != 0) 2095 return (error); 2096 cvtstat(&sb, &osb); 2097 return (copyout(&osb, uap->ub, sizeof (osb))); 2098 } 2099 2100 /* 2101 * Get file status; this version does not follow links. 2102 */ 2103 #ifndef _SYS_SYSPROTO_H_ 2104 struct olstat_args { 2105 char *path; 2106 struct ostat *ub; 2107 }; 2108 #endif 2109 int 2110 olstat(struct thread *td, struct olstat_args *uap) 2111 { 2112 struct stat sb; 2113 struct ostat osb; 2114 int error; 2115 2116 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path, 2117 UIO_USERSPACE, &sb, NULL); 2118 if (error != 0) 2119 return (error); 2120 cvtstat(&sb, &osb); 2121 return (copyout(&osb, uap->ub, sizeof (osb))); 2122 } 2123 2124 /* 2125 * Convert from an old to a new stat structure. 2126 * XXX: many values are blindly truncated. 2127 */ 2128 void 2129 cvtstat(struct stat *st, struct ostat *ost) 2130 { 2131 2132 bzero(ost, sizeof(*ost)); 2133 ost->st_dev = st->st_dev; 2134 ost->st_ino = st->st_ino; 2135 ost->st_mode = st->st_mode; 2136 ost->st_nlink = st->st_nlink; 2137 ost->st_uid = st->st_uid; 2138 ost->st_gid = st->st_gid; 2139 ost->st_rdev = st->st_rdev; 2140 ost->st_size = MIN(st->st_size, INT32_MAX); 2141 ost->st_atim = st->st_atim; 2142 ost->st_mtim = st->st_mtim; 2143 ost->st_ctim = st->st_ctim; 2144 ost->st_blksize = st->st_blksize; 2145 ost->st_blocks = st->st_blocks; 2146 ost->st_flags = st->st_flags; 2147 ost->st_gen = st->st_gen; 2148 } 2149 #endif /* COMPAT_43 */ 2150 2151 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11) 2152 int ino64_trunc_error; 2153 SYSCTL_INT(_vfs, OID_AUTO, ino64_trunc_error, CTLFLAG_RW, 2154 &ino64_trunc_error, 0, 2155 "Error on truncation of device, file or inode number, or link count"); 2156 2157 int 2158 freebsd11_cvtstat(struct stat *st, struct freebsd11_stat *ost) 2159 { 2160 2161 ost->st_dev = st->st_dev; 2162 if (ost->st_dev != st->st_dev) { 2163 switch (ino64_trunc_error) { 2164 default: 2165 /* 2166 * Since dev_t is almost raw, don't clamp to the 2167 * maximum for case 2, but ignore the error. 2168 */ 2169 break; 2170 case 1: 2171 return (EOVERFLOW); 2172 } 2173 } 2174 ost->st_ino = st->st_ino; 2175 if (ost->st_ino != st->st_ino) { 2176 switch (ino64_trunc_error) { 2177 default: 2178 case 0: 2179 break; 2180 case 1: 2181 return (EOVERFLOW); 2182 case 2: 2183 ost->st_ino = UINT32_MAX; 2184 break; 2185 } 2186 } 2187 ost->st_mode = st->st_mode; 2188 ost->st_nlink = st->st_nlink; 2189 if (ost->st_nlink != st->st_nlink) { 2190 switch (ino64_trunc_error) { 2191 default: 2192 case 0: 2193 break; 2194 case 1: 2195 return (EOVERFLOW); 2196 case 2: 2197 ost->st_nlink = UINT16_MAX; 2198 break; 2199 } 2200 } 2201 ost->st_uid = st->st_uid; 2202 ost->st_gid = st->st_gid; 2203 ost->st_rdev = st->st_rdev; 2204 if (ost->st_rdev != st->st_rdev) { 2205 switch (ino64_trunc_error) { 2206 default: 2207 break; 2208 case 1: 2209 return (EOVERFLOW); 2210 } 2211 } 2212 ost->st_atim = st->st_atim; 2213 ost->st_mtim = st->st_mtim; 2214 ost->st_ctim = st->st_ctim; 2215 ost->st_size = st->st_size; 2216 ost->st_blocks = st->st_blocks; 2217 ost->st_blksize = st->st_blksize; 2218 ost->st_flags = st->st_flags; 2219 ost->st_gen = st->st_gen; 2220 ost->st_lspare = 0; 2221 ost->st_birthtim = st->st_birthtim; 2222 bzero((char *)&ost->st_birthtim + sizeof(ost->st_birthtim), 2223 sizeof(*ost) - offsetof(struct freebsd11_stat, 2224 st_birthtim) - sizeof(ost->st_birthtim)); 2225 return (0); 2226 } 2227 2228 int 2229 freebsd11_stat(struct thread *td, struct freebsd11_stat_args* uap) 2230 { 2231 struct stat sb; 2232 struct freebsd11_stat osb; 2233 int error; 2234 2235 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE, 2236 &sb, NULL); 2237 if (error != 0) 2238 return (error); 2239 error = freebsd11_cvtstat(&sb, &osb); 2240 if (error == 0) 2241 error = copyout(&osb, uap->ub, sizeof(osb)); 2242 return (error); 2243 } 2244 2245 int 2246 freebsd11_lstat(struct thread *td, struct freebsd11_lstat_args* uap) 2247 { 2248 struct stat sb; 2249 struct freebsd11_stat osb; 2250 int error; 2251 2252 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path, 2253 UIO_USERSPACE, &sb, NULL); 2254 if (error != 0) 2255 return (error); 2256 error = freebsd11_cvtstat(&sb, &osb); 2257 if (error == 0) 2258 error = copyout(&osb, uap->ub, sizeof(osb)); 2259 return (error); 2260 } 2261 2262 int 2263 freebsd11_fhstat(struct thread *td, struct freebsd11_fhstat_args* uap) 2264 { 2265 struct fhandle fh; 2266 struct stat sb; 2267 struct freebsd11_stat osb; 2268 int error; 2269 2270 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t)); 2271 if (error != 0) 2272 return (error); 2273 error = kern_fhstat(td, fh, &sb); 2274 if (error != 0) 2275 return (error); 2276 error = freebsd11_cvtstat(&sb, &osb); 2277 if (error == 0) 2278 error = copyout(&osb, uap->sb, sizeof(osb)); 2279 return (error); 2280 } 2281 2282 int 2283 freebsd11_fstatat(struct thread *td, struct freebsd11_fstatat_args* uap) 2284 { 2285 struct stat sb; 2286 struct freebsd11_stat osb; 2287 int error; 2288 2289 error = kern_statat(td, uap->flag, uap->fd, uap->path, 2290 UIO_USERSPACE, &sb, NULL); 2291 if (error != 0) 2292 return (error); 2293 error = freebsd11_cvtstat(&sb, &osb); 2294 if (error == 0) 2295 error = copyout(&osb, uap->buf, sizeof(osb)); 2296 return (error); 2297 } 2298 #endif /* COMPAT_FREEBSD11 */ 2299 2300 /* 2301 * Get file status 2302 */ 2303 #ifndef _SYS_SYSPROTO_H_ 2304 struct fstatat_args { 2305 int fd; 2306 char *path; 2307 struct stat *buf; 2308 int flag; 2309 } 2310 #endif 2311 int 2312 sys_fstatat(struct thread *td, struct fstatat_args *uap) 2313 { 2314 struct stat sb; 2315 int error; 2316 2317 error = kern_statat(td, uap->flag, uap->fd, uap->path, 2318 UIO_USERSPACE, &sb, NULL); 2319 if (error == 0) 2320 error = copyout(&sb, uap->buf, sizeof (sb)); 2321 return (error); 2322 } 2323 2324 int 2325 kern_statat(struct thread *td, int flag, int fd, const char *path, 2326 enum uio_seg pathseg, struct stat *sbp, 2327 void (*hook)(struct vnode *vp, struct stat *sbp)) 2328 { 2329 struct nameidata nd; 2330 int error; 2331 2332 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH)) != 0) 2333 return (EINVAL); 2334 2335 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) != 0 ? 2336 NOFOLLOW : FOLLOW) | ((flag & AT_BENEATH) != 0 ? BENEATH : 0) | 2337 LOCKSHARED | LOCKLEAF | AUDITVNODE1, pathseg, path, fd, 2338 &cap_fstat_rights, td); 2339 2340 if ((error = namei(&nd)) != 0) 2341 return (error); 2342 error = vn_stat(nd.ni_vp, sbp, td->td_ucred, NOCRED, td); 2343 if (error == 0) { 2344 SDT_PROBE2(vfs, , stat, mode, path, sbp->st_mode); 2345 if (S_ISREG(sbp->st_mode)) 2346 SDT_PROBE2(vfs, , stat, reg, path, pathseg); 2347 if (__predict_false(hook != NULL)) 2348 hook(nd.ni_vp, sbp); 2349 } 2350 NDFREE(&nd, NDF_ONLY_PNBUF); 2351 vput(nd.ni_vp); 2352 #ifdef __STAT_TIME_T_EXT 2353 sbp->st_atim_ext = 0; 2354 sbp->st_mtim_ext = 0; 2355 sbp->st_ctim_ext = 0; 2356 sbp->st_btim_ext = 0; 2357 #endif 2358 #ifdef KTRACE 2359 if (KTRPOINT(td, KTR_STRUCT)) 2360 ktrstat_error(sbp, error); 2361 #endif 2362 return (error); 2363 } 2364 2365 #if defined(COMPAT_FREEBSD11) 2366 /* 2367 * Implementation of the NetBSD [l]stat() functions. 2368 */ 2369 void 2370 freebsd11_cvtnstat(struct stat *sb, struct nstat *nsb) 2371 { 2372 2373 bzero(nsb, sizeof(*nsb)); 2374 nsb->st_dev = sb->st_dev; 2375 nsb->st_ino = sb->st_ino; 2376 nsb->st_mode = sb->st_mode; 2377 nsb->st_nlink = sb->st_nlink; 2378 nsb->st_uid = sb->st_uid; 2379 nsb->st_gid = sb->st_gid; 2380 nsb->st_rdev = sb->st_rdev; 2381 nsb->st_atim = sb->st_atim; 2382 nsb->st_mtim = sb->st_mtim; 2383 nsb->st_ctim = sb->st_ctim; 2384 nsb->st_size = sb->st_size; 2385 nsb->st_blocks = sb->st_blocks; 2386 nsb->st_blksize = sb->st_blksize; 2387 nsb->st_flags = sb->st_flags; 2388 nsb->st_gen = sb->st_gen; 2389 nsb->st_birthtim = sb->st_birthtim; 2390 } 2391 2392 #ifndef _SYS_SYSPROTO_H_ 2393 struct freebsd11_nstat_args { 2394 char *path; 2395 struct nstat *ub; 2396 }; 2397 #endif 2398 int 2399 freebsd11_nstat(struct thread *td, struct freebsd11_nstat_args *uap) 2400 { 2401 struct stat sb; 2402 struct nstat nsb; 2403 int error; 2404 2405 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE, 2406 &sb, NULL); 2407 if (error != 0) 2408 return (error); 2409 freebsd11_cvtnstat(&sb, &nsb); 2410 return (copyout(&nsb, uap->ub, sizeof (nsb))); 2411 } 2412 2413 /* 2414 * NetBSD lstat. Get file status; this version does not follow links. 2415 */ 2416 #ifndef _SYS_SYSPROTO_H_ 2417 struct freebsd11_nlstat_args { 2418 char *path; 2419 struct nstat *ub; 2420 }; 2421 #endif 2422 int 2423 freebsd11_nlstat(struct thread *td, struct freebsd11_nlstat_args *uap) 2424 { 2425 struct stat sb; 2426 struct nstat nsb; 2427 int error; 2428 2429 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path, 2430 UIO_USERSPACE, &sb, NULL); 2431 if (error != 0) 2432 return (error); 2433 freebsd11_cvtnstat(&sb, &nsb); 2434 return (copyout(&nsb, uap->ub, sizeof (nsb))); 2435 } 2436 #endif /* COMPAT_FREEBSD11 */ 2437 2438 /* 2439 * Get configurable pathname variables. 2440 */ 2441 #ifndef _SYS_SYSPROTO_H_ 2442 struct pathconf_args { 2443 char *path; 2444 int name; 2445 }; 2446 #endif 2447 int 2448 sys_pathconf(struct thread *td, struct pathconf_args *uap) 2449 { 2450 long value; 2451 int error; 2452 2453 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW, 2454 &value); 2455 if (error == 0) 2456 td->td_retval[0] = value; 2457 return (error); 2458 } 2459 2460 #ifndef _SYS_SYSPROTO_H_ 2461 struct lpathconf_args { 2462 char *path; 2463 int name; 2464 }; 2465 #endif 2466 int 2467 sys_lpathconf(struct thread *td, struct lpathconf_args *uap) 2468 { 2469 long value; 2470 int error; 2471 2472 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, 2473 NOFOLLOW, &value); 2474 if (error == 0) 2475 td->td_retval[0] = value; 2476 return (error); 2477 } 2478 2479 int 2480 kern_pathconf(struct thread *td, const char *path, enum uio_seg pathseg, 2481 int name, u_long flags, long *valuep) 2482 { 2483 struct nameidata nd; 2484 int error; 2485 2486 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags, 2487 pathseg, path, td); 2488 if ((error = namei(&nd)) != 0) 2489 return (error); 2490 NDFREE(&nd, NDF_ONLY_PNBUF); 2491 2492 error = VOP_PATHCONF(nd.ni_vp, name, valuep); 2493 vput(nd.ni_vp); 2494 return (error); 2495 } 2496 2497 /* 2498 * Return target name of a symbolic link. 2499 */ 2500 #ifndef _SYS_SYSPROTO_H_ 2501 struct readlink_args { 2502 char *path; 2503 char *buf; 2504 size_t count; 2505 }; 2506 #endif 2507 int 2508 sys_readlink(struct thread *td, struct readlink_args *uap) 2509 { 2510 2511 return (kern_readlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 2512 uap->buf, UIO_USERSPACE, uap->count)); 2513 } 2514 #ifndef _SYS_SYSPROTO_H_ 2515 struct readlinkat_args { 2516 int fd; 2517 char *path; 2518 char *buf; 2519 size_t bufsize; 2520 }; 2521 #endif 2522 int 2523 sys_readlinkat(struct thread *td, struct readlinkat_args *uap) 2524 { 2525 2526 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE, 2527 uap->buf, UIO_USERSPACE, uap->bufsize)); 2528 } 2529 2530 int 2531 kern_readlinkat(struct thread *td, int fd, const char *path, 2532 enum uio_seg pathseg, char *buf, enum uio_seg bufseg, size_t count) 2533 { 2534 struct vnode *vp; 2535 struct nameidata nd; 2536 int error; 2537 2538 if (count > IOSIZE_MAX) 2539 return (EINVAL); 2540 2541 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1, 2542 pathseg, path, fd, td); 2543 2544 if ((error = namei(&nd)) != 0) 2545 return (error); 2546 NDFREE(&nd, NDF_ONLY_PNBUF); 2547 vp = nd.ni_vp; 2548 2549 error = kern_readlink_vp(vp, buf, bufseg, count, td); 2550 vput(vp); 2551 2552 return (error); 2553 } 2554 2555 /* 2556 * Helper function to readlink from a vnode 2557 */ 2558 static int 2559 kern_readlink_vp(struct vnode *vp, char *buf, enum uio_seg bufseg, size_t count, 2560 struct thread *td) 2561 { 2562 struct iovec aiov; 2563 struct uio auio; 2564 int error; 2565 2566 ASSERT_VOP_LOCKED(vp, "kern_readlink_vp(): vp not locked"); 2567 #ifdef MAC 2568 error = mac_vnode_check_readlink(td->td_ucred, vp); 2569 if (error != 0) 2570 return (error); 2571 #endif 2572 if (vp->v_type != VLNK && (vp->v_vflag & VV_READLINK) == 0) 2573 return (EINVAL); 2574 2575 aiov.iov_base = buf; 2576 aiov.iov_len = count; 2577 auio.uio_iov = &aiov; 2578 auio.uio_iovcnt = 1; 2579 auio.uio_offset = 0; 2580 auio.uio_rw = UIO_READ; 2581 auio.uio_segflg = bufseg; 2582 auio.uio_td = td; 2583 auio.uio_resid = count; 2584 error = VOP_READLINK(vp, &auio, td->td_ucred); 2585 td->td_retval[0] = count - auio.uio_resid; 2586 return (error); 2587 } 2588 2589 /* 2590 * Common implementation code for chflags() and fchflags(). 2591 */ 2592 static int 2593 setfflags(struct thread *td, struct vnode *vp, u_long flags) 2594 { 2595 struct mount *mp; 2596 struct vattr vattr; 2597 int error; 2598 2599 /* We can't support the value matching VNOVAL. */ 2600 if (flags == VNOVAL) 2601 return (EOPNOTSUPP); 2602 2603 /* 2604 * Prevent non-root users from setting flags on devices. When 2605 * a device is reused, users can retain ownership of the device 2606 * if they are allowed to set flags and programs assume that 2607 * chown can't fail when done as root. 2608 */ 2609 if (vp->v_type == VCHR || vp->v_type == VBLK) { 2610 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV); 2611 if (error != 0) 2612 return (error); 2613 } 2614 2615 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) 2616 return (error); 2617 VATTR_NULL(&vattr); 2618 vattr.va_flags = flags; 2619 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 2620 #ifdef MAC 2621 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags); 2622 if (error == 0) 2623 #endif 2624 error = VOP_SETATTR(vp, &vattr, td->td_ucred); 2625 VOP_UNLOCK(vp); 2626 vn_finished_write(mp); 2627 return (error); 2628 } 2629 2630 /* 2631 * Change flags of a file given a path name. 2632 */ 2633 #ifndef _SYS_SYSPROTO_H_ 2634 struct chflags_args { 2635 const char *path; 2636 u_long flags; 2637 }; 2638 #endif 2639 int 2640 sys_chflags(struct thread *td, struct chflags_args *uap) 2641 { 2642 2643 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 2644 uap->flags, 0)); 2645 } 2646 2647 #ifndef _SYS_SYSPROTO_H_ 2648 struct chflagsat_args { 2649 int fd; 2650 const char *path; 2651 u_long flags; 2652 int atflag; 2653 } 2654 #endif 2655 int 2656 sys_chflagsat(struct thread *td, struct chflagsat_args *uap) 2657 { 2658 2659 if ((uap->atflag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH)) != 0) 2660 return (EINVAL); 2661 2662 return (kern_chflagsat(td, uap->fd, uap->path, UIO_USERSPACE, 2663 uap->flags, uap->atflag)); 2664 } 2665 2666 /* 2667 * Same as chflags() but doesn't follow symlinks. 2668 */ 2669 #ifndef _SYS_SYSPROTO_H_ 2670 struct lchflags_args { 2671 const char *path; 2672 u_long flags; 2673 }; 2674 #endif 2675 int 2676 sys_lchflags(struct thread *td, struct lchflags_args *uap) 2677 { 2678 2679 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 2680 uap->flags, AT_SYMLINK_NOFOLLOW)); 2681 } 2682 2683 static int 2684 kern_chflagsat(struct thread *td, int fd, const char *path, 2685 enum uio_seg pathseg, u_long flags, int atflag) 2686 { 2687 struct nameidata nd; 2688 int error, follow; 2689 2690 AUDIT_ARG_FFLAGS(flags); 2691 follow = (atflag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW; 2692 follow |= (atflag & AT_BENEATH) != 0 ? BENEATH : 0; 2693 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd, 2694 &cap_fchflags_rights, td); 2695 if ((error = namei(&nd)) != 0) 2696 return (error); 2697 NDFREE(&nd, NDF_ONLY_PNBUF); 2698 error = setfflags(td, nd.ni_vp, flags); 2699 vrele(nd.ni_vp); 2700 return (error); 2701 } 2702 2703 /* 2704 * Change flags of a file given a file descriptor. 2705 */ 2706 #ifndef _SYS_SYSPROTO_H_ 2707 struct fchflags_args { 2708 int fd; 2709 u_long flags; 2710 }; 2711 #endif 2712 int 2713 sys_fchflags(struct thread *td, struct fchflags_args *uap) 2714 { 2715 struct file *fp; 2716 int error; 2717 2718 AUDIT_ARG_FD(uap->fd); 2719 AUDIT_ARG_FFLAGS(uap->flags); 2720 error = getvnode(td, uap->fd, &cap_fchflags_rights, 2721 &fp); 2722 if (error != 0) 2723 return (error); 2724 #ifdef AUDIT 2725 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY); 2726 AUDIT_ARG_VNODE1(fp->f_vnode); 2727 VOP_UNLOCK(fp->f_vnode); 2728 #endif 2729 error = setfflags(td, fp->f_vnode, uap->flags); 2730 fdrop(fp, td); 2731 return (error); 2732 } 2733 2734 /* 2735 * Common implementation code for chmod(), lchmod() and fchmod(). 2736 */ 2737 int 2738 setfmode(struct thread *td, struct ucred *cred, struct vnode *vp, int mode) 2739 { 2740 struct mount *mp; 2741 struct vattr vattr; 2742 int error; 2743 2744 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) 2745 return (error); 2746 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 2747 VATTR_NULL(&vattr); 2748 vattr.va_mode = mode & ALLPERMS; 2749 #ifdef MAC 2750 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode); 2751 if (error == 0) 2752 #endif 2753 error = VOP_SETATTR(vp, &vattr, cred); 2754 VOP_UNLOCK(vp); 2755 vn_finished_write(mp); 2756 return (error); 2757 } 2758 2759 /* 2760 * Change mode of a file given path name. 2761 */ 2762 #ifndef _SYS_SYSPROTO_H_ 2763 struct chmod_args { 2764 char *path; 2765 int mode; 2766 }; 2767 #endif 2768 int 2769 sys_chmod(struct thread *td, struct chmod_args *uap) 2770 { 2771 2772 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 2773 uap->mode, 0)); 2774 } 2775 2776 #ifndef _SYS_SYSPROTO_H_ 2777 struct fchmodat_args { 2778 int dirfd; 2779 char *path; 2780 mode_t mode; 2781 int flag; 2782 } 2783 #endif 2784 int 2785 sys_fchmodat(struct thread *td, struct fchmodat_args *uap) 2786 { 2787 2788 if ((uap->flag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH)) != 0) 2789 return (EINVAL); 2790 2791 return (kern_fchmodat(td, uap->fd, uap->path, UIO_USERSPACE, 2792 uap->mode, uap->flag)); 2793 } 2794 2795 /* 2796 * Change mode of a file given path name (don't follow links.) 2797 */ 2798 #ifndef _SYS_SYSPROTO_H_ 2799 struct lchmod_args { 2800 char *path; 2801 int mode; 2802 }; 2803 #endif 2804 int 2805 sys_lchmod(struct thread *td, struct lchmod_args *uap) 2806 { 2807 2808 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 2809 uap->mode, AT_SYMLINK_NOFOLLOW)); 2810 } 2811 2812 int 2813 kern_fchmodat(struct thread *td, int fd, const char *path, 2814 enum uio_seg pathseg, mode_t mode, int flag) 2815 { 2816 struct nameidata nd; 2817 int error, follow; 2818 2819 AUDIT_ARG_MODE(mode); 2820 follow = (flag & AT_SYMLINK_NOFOLLOW) != 0 ? NOFOLLOW : FOLLOW; 2821 follow |= (flag & AT_BENEATH) != 0 ? BENEATH : 0; 2822 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd, 2823 &cap_fchmod_rights, td); 2824 if ((error = namei(&nd)) != 0) 2825 return (error); 2826 NDFREE(&nd, NDF_ONLY_PNBUF); 2827 error = setfmode(td, td->td_ucred, nd.ni_vp, mode); 2828 vrele(nd.ni_vp); 2829 return (error); 2830 } 2831 2832 /* 2833 * Change mode of a file given a file descriptor. 2834 */ 2835 #ifndef _SYS_SYSPROTO_H_ 2836 struct fchmod_args { 2837 int fd; 2838 int mode; 2839 }; 2840 #endif 2841 int 2842 sys_fchmod(struct thread *td, struct fchmod_args *uap) 2843 { 2844 struct file *fp; 2845 int error; 2846 2847 AUDIT_ARG_FD(uap->fd); 2848 AUDIT_ARG_MODE(uap->mode); 2849 2850 error = fget(td, uap->fd, &cap_fchmod_rights, &fp); 2851 if (error != 0) 2852 return (error); 2853 error = fo_chmod(fp, uap->mode, td->td_ucred, td); 2854 fdrop(fp, td); 2855 return (error); 2856 } 2857 2858 /* 2859 * Common implementation for chown(), lchown(), and fchown() 2860 */ 2861 int 2862 setfown(struct thread *td, struct ucred *cred, struct vnode *vp, uid_t uid, 2863 gid_t gid) 2864 { 2865 struct mount *mp; 2866 struct vattr vattr; 2867 int error; 2868 2869 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) 2870 return (error); 2871 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 2872 VATTR_NULL(&vattr); 2873 vattr.va_uid = uid; 2874 vattr.va_gid = gid; 2875 #ifdef MAC 2876 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid, 2877 vattr.va_gid); 2878 if (error == 0) 2879 #endif 2880 error = VOP_SETATTR(vp, &vattr, cred); 2881 VOP_UNLOCK(vp); 2882 vn_finished_write(mp); 2883 return (error); 2884 } 2885 2886 /* 2887 * Set ownership given a path name. 2888 */ 2889 #ifndef _SYS_SYSPROTO_H_ 2890 struct chown_args { 2891 char *path; 2892 int uid; 2893 int gid; 2894 }; 2895 #endif 2896 int 2897 sys_chown(struct thread *td, struct chown_args *uap) 2898 { 2899 2900 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->uid, 2901 uap->gid, 0)); 2902 } 2903 2904 #ifndef _SYS_SYSPROTO_H_ 2905 struct fchownat_args { 2906 int fd; 2907 const char * path; 2908 uid_t uid; 2909 gid_t gid; 2910 int flag; 2911 }; 2912 #endif 2913 int 2914 sys_fchownat(struct thread *td, struct fchownat_args *uap) 2915 { 2916 2917 if ((uap->flag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH)) != 0) 2918 return (EINVAL); 2919 2920 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid, 2921 uap->gid, uap->flag)); 2922 } 2923 2924 int 2925 kern_fchownat(struct thread *td, int fd, const char *path, 2926 enum uio_seg pathseg, int uid, int gid, int flag) 2927 { 2928 struct nameidata nd; 2929 int error, follow; 2930 2931 AUDIT_ARG_OWNER(uid, gid); 2932 follow = (flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW; 2933 follow |= (flag & AT_BENEATH) != 0 ? BENEATH : 0; 2934 NDINIT_ATRIGHTS(&nd, LOOKUP, follow | AUDITVNODE1, pathseg, path, fd, 2935 &cap_fchown_rights, td); 2936 2937 if ((error = namei(&nd)) != 0) 2938 return (error); 2939 NDFREE(&nd, NDF_ONLY_PNBUF); 2940 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid); 2941 vrele(nd.ni_vp); 2942 return (error); 2943 } 2944 2945 /* 2946 * Set ownership given a path name, do not cross symlinks. 2947 */ 2948 #ifndef _SYS_SYSPROTO_H_ 2949 struct lchown_args { 2950 char *path; 2951 int uid; 2952 int gid; 2953 }; 2954 #endif 2955 int 2956 sys_lchown(struct thread *td, struct lchown_args *uap) 2957 { 2958 2959 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 2960 uap->uid, uap->gid, AT_SYMLINK_NOFOLLOW)); 2961 } 2962 2963 /* 2964 * Set ownership given a file descriptor. 2965 */ 2966 #ifndef _SYS_SYSPROTO_H_ 2967 struct fchown_args { 2968 int fd; 2969 int uid; 2970 int gid; 2971 }; 2972 #endif 2973 int 2974 sys_fchown(struct thread *td, struct fchown_args *uap) 2975 { 2976 struct file *fp; 2977 int error; 2978 2979 AUDIT_ARG_FD(uap->fd); 2980 AUDIT_ARG_OWNER(uap->uid, uap->gid); 2981 error = fget(td, uap->fd, &cap_fchown_rights, &fp); 2982 if (error != 0) 2983 return (error); 2984 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td); 2985 fdrop(fp, td); 2986 return (error); 2987 } 2988 2989 /* 2990 * Common implementation code for utimes(), lutimes(), and futimes(). 2991 */ 2992 static int 2993 getutimes(const struct timeval *usrtvp, enum uio_seg tvpseg, 2994 struct timespec *tsp) 2995 { 2996 struct timeval tv[2]; 2997 const struct timeval *tvp; 2998 int error; 2999 3000 if (usrtvp == NULL) { 3001 vfs_timestamp(&tsp[0]); 3002 tsp[1] = tsp[0]; 3003 } else { 3004 if (tvpseg == UIO_SYSSPACE) { 3005 tvp = usrtvp; 3006 } else { 3007 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0) 3008 return (error); 3009 tvp = tv; 3010 } 3011 3012 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 || 3013 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000) 3014 return (EINVAL); 3015 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]); 3016 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]); 3017 } 3018 return (0); 3019 } 3020 3021 /* 3022 * Common implementation code for futimens(), utimensat(). 3023 */ 3024 #define UTIMENS_NULL 0x1 3025 #define UTIMENS_EXIT 0x2 3026 static int 3027 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg, 3028 struct timespec *tsp, int *retflags) 3029 { 3030 struct timespec tsnow; 3031 int error; 3032 3033 vfs_timestamp(&tsnow); 3034 *retflags = 0; 3035 if (usrtsp == NULL) { 3036 tsp[0] = tsnow; 3037 tsp[1] = tsnow; 3038 *retflags |= UTIMENS_NULL; 3039 return (0); 3040 } 3041 if (tspseg == UIO_SYSSPACE) { 3042 tsp[0] = usrtsp[0]; 3043 tsp[1] = usrtsp[1]; 3044 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0) 3045 return (error); 3046 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT) 3047 *retflags |= UTIMENS_EXIT; 3048 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW) 3049 *retflags |= UTIMENS_NULL; 3050 if (tsp[0].tv_nsec == UTIME_OMIT) 3051 tsp[0].tv_sec = VNOVAL; 3052 else if (tsp[0].tv_nsec == UTIME_NOW) 3053 tsp[0] = tsnow; 3054 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L) 3055 return (EINVAL); 3056 if (tsp[1].tv_nsec == UTIME_OMIT) 3057 tsp[1].tv_sec = VNOVAL; 3058 else if (tsp[1].tv_nsec == UTIME_NOW) 3059 tsp[1] = tsnow; 3060 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L) 3061 return (EINVAL); 3062 3063 return (0); 3064 } 3065 3066 /* 3067 * Common implementation code for utimes(), lutimes(), futimes(), futimens(), 3068 * and utimensat(). 3069 */ 3070 static int 3071 setutimes(struct thread *td, struct vnode *vp, const struct timespec *ts, 3072 int numtimes, int nullflag) 3073 { 3074 struct mount *mp; 3075 struct vattr vattr; 3076 int error, setbirthtime; 3077 3078 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) 3079 return (error); 3080 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 3081 setbirthtime = 0; 3082 if (numtimes < 3 && !VOP_GETATTR(vp, &vattr, td->td_ucred) && 3083 timespeccmp(&ts[1], &vattr.va_birthtime, < )) 3084 setbirthtime = 1; 3085 VATTR_NULL(&vattr); 3086 vattr.va_atime = ts[0]; 3087 vattr.va_mtime = ts[1]; 3088 if (setbirthtime) 3089 vattr.va_birthtime = ts[1]; 3090 if (numtimes > 2) 3091 vattr.va_birthtime = ts[2]; 3092 if (nullflag) 3093 vattr.va_vaflags |= VA_UTIMES_NULL; 3094 #ifdef MAC 3095 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime, 3096 vattr.va_mtime); 3097 #endif 3098 if (error == 0) 3099 error = VOP_SETATTR(vp, &vattr, td->td_ucred); 3100 VOP_UNLOCK(vp); 3101 vn_finished_write(mp); 3102 return (error); 3103 } 3104 3105 /* 3106 * Set the access and modification times of a file. 3107 */ 3108 #ifndef _SYS_SYSPROTO_H_ 3109 struct utimes_args { 3110 char *path; 3111 struct timeval *tptr; 3112 }; 3113 #endif 3114 int 3115 sys_utimes(struct thread *td, struct utimes_args *uap) 3116 { 3117 3118 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 3119 uap->tptr, UIO_USERSPACE)); 3120 } 3121 3122 #ifndef _SYS_SYSPROTO_H_ 3123 struct futimesat_args { 3124 int fd; 3125 const char * path; 3126 const struct timeval * times; 3127 }; 3128 #endif 3129 int 3130 sys_futimesat(struct thread *td, struct futimesat_args *uap) 3131 { 3132 3133 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE, 3134 uap->times, UIO_USERSPACE)); 3135 } 3136 3137 int 3138 kern_utimesat(struct thread *td, int fd, const char *path, 3139 enum uio_seg pathseg, struct timeval *tptr, enum uio_seg tptrseg) 3140 { 3141 struct nameidata nd; 3142 struct timespec ts[2]; 3143 int error; 3144 3145 if ((error = getutimes(tptr, tptrseg, ts)) != 0) 3146 return (error); 3147 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd, 3148 &cap_futimes_rights, td); 3149 3150 if ((error = namei(&nd)) != 0) 3151 return (error); 3152 NDFREE(&nd, NDF_ONLY_PNBUF); 3153 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL); 3154 vrele(nd.ni_vp); 3155 return (error); 3156 } 3157 3158 /* 3159 * Set the access and modification times of a file. 3160 */ 3161 #ifndef _SYS_SYSPROTO_H_ 3162 struct lutimes_args { 3163 char *path; 3164 struct timeval *tptr; 3165 }; 3166 #endif 3167 int 3168 sys_lutimes(struct thread *td, struct lutimes_args *uap) 3169 { 3170 3171 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr, 3172 UIO_USERSPACE)); 3173 } 3174 3175 int 3176 kern_lutimes(struct thread *td, const char *path, enum uio_seg pathseg, 3177 struct timeval *tptr, enum uio_seg tptrseg) 3178 { 3179 struct timespec ts[2]; 3180 struct nameidata nd; 3181 int error; 3182 3183 if ((error = getutimes(tptr, tptrseg, ts)) != 0) 3184 return (error); 3185 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path, td); 3186 if ((error = namei(&nd)) != 0) 3187 return (error); 3188 NDFREE(&nd, NDF_ONLY_PNBUF); 3189 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL); 3190 vrele(nd.ni_vp); 3191 return (error); 3192 } 3193 3194 /* 3195 * Set the access and modification times of a file. 3196 */ 3197 #ifndef _SYS_SYSPROTO_H_ 3198 struct futimes_args { 3199 int fd; 3200 struct timeval *tptr; 3201 }; 3202 #endif 3203 int 3204 sys_futimes(struct thread *td, struct futimes_args *uap) 3205 { 3206 3207 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE)); 3208 } 3209 3210 int 3211 kern_futimes(struct thread *td, int fd, struct timeval *tptr, 3212 enum uio_seg tptrseg) 3213 { 3214 struct timespec ts[2]; 3215 struct file *fp; 3216 int error; 3217 3218 AUDIT_ARG_FD(fd); 3219 error = getutimes(tptr, tptrseg, ts); 3220 if (error != 0) 3221 return (error); 3222 error = getvnode(td, fd, &cap_futimes_rights, &fp); 3223 if (error != 0) 3224 return (error); 3225 #ifdef AUDIT 3226 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY); 3227 AUDIT_ARG_VNODE1(fp->f_vnode); 3228 VOP_UNLOCK(fp->f_vnode); 3229 #endif 3230 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL); 3231 fdrop(fp, td); 3232 return (error); 3233 } 3234 3235 int 3236 sys_futimens(struct thread *td, struct futimens_args *uap) 3237 { 3238 3239 return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE)); 3240 } 3241 3242 int 3243 kern_futimens(struct thread *td, int fd, struct timespec *tptr, 3244 enum uio_seg tptrseg) 3245 { 3246 struct timespec ts[2]; 3247 struct file *fp; 3248 int error, flags; 3249 3250 AUDIT_ARG_FD(fd); 3251 error = getutimens(tptr, tptrseg, ts, &flags); 3252 if (error != 0) 3253 return (error); 3254 if (flags & UTIMENS_EXIT) 3255 return (0); 3256 error = getvnode(td, fd, &cap_futimes_rights, &fp); 3257 if (error != 0) 3258 return (error); 3259 #ifdef AUDIT 3260 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY); 3261 AUDIT_ARG_VNODE1(fp->f_vnode); 3262 VOP_UNLOCK(fp->f_vnode); 3263 #endif 3264 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL); 3265 fdrop(fp, td); 3266 return (error); 3267 } 3268 3269 int 3270 sys_utimensat(struct thread *td, struct utimensat_args *uap) 3271 { 3272 3273 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE, 3274 uap->times, UIO_USERSPACE, uap->flag)); 3275 } 3276 3277 int 3278 kern_utimensat(struct thread *td, int fd, const char *path, 3279 enum uio_seg pathseg, struct timespec *tptr, enum uio_seg tptrseg, 3280 int flag) 3281 { 3282 struct nameidata nd; 3283 struct timespec ts[2]; 3284 int error, flags; 3285 3286 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH)) != 0) 3287 return (EINVAL); 3288 3289 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0) 3290 return (error); 3291 NDINIT_ATRIGHTS(&nd, LOOKUP, ((flag & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : 3292 FOLLOW) | ((flag & AT_BENEATH) != 0 ? BENEATH : 0) | AUDITVNODE1, 3293 pathseg, path, fd, &cap_futimes_rights, td); 3294 if ((error = namei(&nd)) != 0) 3295 return (error); 3296 /* 3297 * We are allowed to call namei() regardless of 2xUTIME_OMIT. 3298 * POSIX states: 3299 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected." 3300 * "Search permission is denied by a component of the path prefix." 3301 */ 3302 NDFREE(&nd, NDF_ONLY_PNBUF); 3303 if ((flags & UTIMENS_EXIT) == 0) 3304 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL); 3305 vrele(nd.ni_vp); 3306 return (error); 3307 } 3308 3309 /* 3310 * Truncate a file given its path name. 3311 */ 3312 #ifndef _SYS_SYSPROTO_H_ 3313 struct truncate_args { 3314 char *path; 3315 int pad; 3316 off_t length; 3317 }; 3318 #endif 3319 int 3320 sys_truncate(struct thread *td, struct truncate_args *uap) 3321 { 3322 3323 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length)); 3324 } 3325 3326 int 3327 kern_truncate(struct thread *td, const char *path, enum uio_seg pathseg, 3328 off_t length) 3329 { 3330 struct mount *mp; 3331 struct vnode *vp; 3332 void *rl_cookie; 3333 struct vattr vattr; 3334 struct nameidata nd; 3335 int error; 3336 3337 if (length < 0) 3338 return(EINVAL); 3339 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, td); 3340 if ((error = namei(&nd)) != 0) 3341 return (error); 3342 vp = nd.ni_vp; 3343 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX); 3344 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0) { 3345 vn_rangelock_unlock(vp, rl_cookie); 3346 vrele(vp); 3347 return (error); 3348 } 3349 NDFREE(&nd, NDF_ONLY_PNBUF); 3350 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 3351 if (vp->v_type == VDIR) 3352 error = EISDIR; 3353 #ifdef MAC 3354 else if ((error = mac_vnode_check_write(td->td_ucred, NOCRED, vp))) { 3355 } 3356 #endif 3357 else if ((error = vn_writechk(vp)) == 0 && 3358 (error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td)) == 0) { 3359 VATTR_NULL(&vattr); 3360 vattr.va_size = length; 3361 error = VOP_SETATTR(vp, &vattr, td->td_ucred); 3362 } 3363 VOP_UNLOCK(vp); 3364 vn_finished_write(mp); 3365 vn_rangelock_unlock(vp, rl_cookie); 3366 vrele(vp); 3367 return (error); 3368 } 3369 3370 #if defined(COMPAT_43) 3371 /* 3372 * Truncate a file given its path name. 3373 */ 3374 #ifndef _SYS_SYSPROTO_H_ 3375 struct otruncate_args { 3376 char *path; 3377 long length; 3378 }; 3379 #endif 3380 int 3381 otruncate(struct thread *td, struct otruncate_args *uap) 3382 { 3383 3384 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length)); 3385 } 3386 #endif /* COMPAT_43 */ 3387 3388 #if defined(COMPAT_FREEBSD6) 3389 /* Versions with the pad argument */ 3390 int 3391 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap) 3392 { 3393 3394 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length)); 3395 } 3396 3397 int 3398 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap) 3399 { 3400 3401 return (kern_ftruncate(td, uap->fd, uap->length)); 3402 } 3403 #endif 3404 3405 int 3406 kern_fsync(struct thread *td, int fd, bool fullsync) 3407 { 3408 struct vnode *vp; 3409 struct mount *mp; 3410 struct file *fp; 3411 int error, lock_flags; 3412 3413 AUDIT_ARG_FD(fd); 3414 error = getvnode(td, fd, &cap_fsync_rights, &fp); 3415 if (error != 0) 3416 return (error); 3417 vp = fp->f_vnode; 3418 #if 0 3419 if (!fullsync) 3420 /* XXXKIB: compete outstanding aio writes */; 3421 #endif 3422 error = vn_start_write(vp, &mp, V_WAIT | PCATCH); 3423 if (error != 0) 3424 goto drop; 3425 if (MNT_SHARED_WRITES(mp) || 3426 ((mp == NULL) && MNT_SHARED_WRITES(vp->v_mount))) { 3427 lock_flags = LK_SHARED; 3428 } else { 3429 lock_flags = LK_EXCLUSIVE; 3430 } 3431 vn_lock(vp, lock_flags | LK_RETRY); 3432 AUDIT_ARG_VNODE1(vp); 3433 if (vp->v_object != NULL) { 3434 VM_OBJECT_WLOCK(vp->v_object); 3435 vm_object_page_clean(vp->v_object, 0, 0, 0); 3436 VM_OBJECT_WUNLOCK(vp->v_object); 3437 } 3438 error = fullsync ? VOP_FSYNC(vp, MNT_WAIT, td) : VOP_FDATASYNC(vp, td); 3439 VOP_UNLOCK(vp); 3440 vn_finished_write(mp); 3441 drop: 3442 fdrop(fp, td); 3443 return (error); 3444 } 3445 3446 /* 3447 * Sync an open file. 3448 */ 3449 #ifndef _SYS_SYSPROTO_H_ 3450 struct fsync_args { 3451 int fd; 3452 }; 3453 #endif 3454 int 3455 sys_fsync(struct thread *td, struct fsync_args *uap) 3456 { 3457 3458 return (kern_fsync(td, uap->fd, true)); 3459 } 3460 3461 int 3462 sys_fdatasync(struct thread *td, struct fdatasync_args *uap) 3463 { 3464 3465 return (kern_fsync(td, uap->fd, false)); 3466 } 3467 3468 /* 3469 * Rename files. Source and destination must either both be directories, or 3470 * both not be directories. If target is a directory, it must be empty. 3471 */ 3472 #ifndef _SYS_SYSPROTO_H_ 3473 struct rename_args { 3474 char *from; 3475 char *to; 3476 }; 3477 #endif 3478 int 3479 sys_rename(struct thread *td, struct rename_args *uap) 3480 { 3481 3482 return (kern_renameat(td, AT_FDCWD, uap->from, AT_FDCWD, 3483 uap->to, UIO_USERSPACE)); 3484 } 3485 3486 #ifndef _SYS_SYSPROTO_H_ 3487 struct renameat_args { 3488 int oldfd; 3489 char *old; 3490 int newfd; 3491 char *new; 3492 }; 3493 #endif 3494 int 3495 sys_renameat(struct thread *td, struct renameat_args *uap) 3496 { 3497 3498 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new, 3499 UIO_USERSPACE)); 3500 } 3501 3502 int 3503 kern_renameat(struct thread *td, int oldfd, const char *old, int newfd, 3504 const char *new, enum uio_seg pathseg) 3505 { 3506 struct mount *mp = NULL; 3507 struct vnode *tvp, *fvp, *tdvp; 3508 struct nameidata fromnd, tond; 3509 int error; 3510 3511 again: 3512 bwillwrite(); 3513 #ifdef MAC 3514 NDINIT_ATRIGHTS(&fromnd, DELETE, LOCKPARENT | LOCKLEAF | SAVESTART | 3515 AUDITVNODE1, pathseg, old, oldfd, 3516 &cap_renameat_source_rights, td); 3517 #else 3518 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | SAVESTART | AUDITVNODE1, 3519 pathseg, old, oldfd, 3520 &cap_renameat_source_rights, td); 3521 #endif 3522 3523 if ((error = namei(&fromnd)) != 0) 3524 return (error); 3525 #ifdef MAC 3526 error = mac_vnode_check_rename_from(td->td_ucred, fromnd.ni_dvp, 3527 fromnd.ni_vp, &fromnd.ni_cnd); 3528 VOP_UNLOCK(fromnd.ni_dvp); 3529 if (fromnd.ni_dvp != fromnd.ni_vp) 3530 VOP_UNLOCK(fromnd.ni_vp); 3531 #endif 3532 fvp = fromnd.ni_vp; 3533 NDINIT_ATRIGHTS(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | 3534 SAVESTART | AUDITVNODE2, pathseg, new, newfd, 3535 &cap_renameat_target_rights, td); 3536 if (fromnd.ni_vp->v_type == VDIR) 3537 tond.ni_cnd.cn_flags |= WILLBEDIR; 3538 if ((error = namei(&tond)) != 0) { 3539 /* Translate error code for rename("dir1", "dir2/."). */ 3540 if (error == EISDIR && fvp->v_type == VDIR) 3541 error = EINVAL; 3542 NDFREE(&fromnd, NDF_ONLY_PNBUF); 3543 vrele(fromnd.ni_dvp); 3544 vrele(fvp); 3545 goto out1; 3546 } 3547 tdvp = tond.ni_dvp; 3548 tvp = tond.ni_vp; 3549 error = vn_start_write(fvp, &mp, V_NOWAIT); 3550 if (error != 0) { 3551 NDFREE(&fromnd, NDF_ONLY_PNBUF); 3552 NDFREE(&tond, NDF_ONLY_PNBUF); 3553 if (tvp != NULL) 3554 vput(tvp); 3555 if (tdvp == tvp) 3556 vrele(tdvp); 3557 else 3558 vput(tdvp); 3559 vrele(fromnd.ni_dvp); 3560 vrele(fvp); 3561 vrele(tond.ni_startdir); 3562 if (fromnd.ni_startdir != NULL) 3563 vrele(fromnd.ni_startdir); 3564 error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH); 3565 if (error != 0) 3566 return (error); 3567 goto again; 3568 } 3569 if (tvp != NULL) { 3570 if (fvp->v_type == VDIR && tvp->v_type != VDIR) { 3571 error = ENOTDIR; 3572 goto out; 3573 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) { 3574 error = EISDIR; 3575 goto out; 3576 } 3577 #ifdef CAPABILITIES 3578 if (newfd != AT_FDCWD && (tond.ni_resflags & NIRES_ABS) == 0) { 3579 /* 3580 * If the target already exists we require CAP_UNLINKAT 3581 * from 'newfd', when newfd was used for the lookup. 3582 */ 3583 error = cap_check(&tond.ni_filecaps.fc_rights, 3584 &cap_unlinkat_rights); 3585 if (error != 0) 3586 goto out; 3587 } 3588 #endif 3589 } 3590 if (fvp == tdvp) { 3591 error = EINVAL; 3592 goto out; 3593 } 3594 /* 3595 * If the source is the same as the destination (that is, if they 3596 * are links to the same vnode), then there is nothing to do. 3597 */ 3598 if (fvp == tvp) 3599 error = -1; 3600 #ifdef MAC 3601 else 3602 error = mac_vnode_check_rename_to(td->td_ucred, tdvp, 3603 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd); 3604 #endif 3605 out: 3606 if (error == 0) { 3607 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd, 3608 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd); 3609 NDFREE(&fromnd, NDF_ONLY_PNBUF); 3610 NDFREE(&tond, NDF_ONLY_PNBUF); 3611 } else { 3612 NDFREE(&fromnd, NDF_ONLY_PNBUF); 3613 NDFREE(&tond, NDF_ONLY_PNBUF); 3614 if (tvp != NULL) 3615 vput(tvp); 3616 if (tdvp == tvp) 3617 vrele(tdvp); 3618 else 3619 vput(tdvp); 3620 vrele(fromnd.ni_dvp); 3621 vrele(fvp); 3622 } 3623 vrele(tond.ni_startdir); 3624 vn_finished_write(mp); 3625 out1: 3626 if (fromnd.ni_startdir) 3627 vrele(fromnd.ni_startdir); 3628 if (error == -1) 3629 return (0); 3630 return (error); 3631 } 3632 3633 /* 3634 * Make a directory file. 3635 */ 3636 #ifndef _SYS_SYSPROTO_H_ 3637 struct mkdir_args { 3638 char *path; 3639 int mode; 3640 }; 3641 #endif 3642 int 3643 sys_mkdir(struct thread *td, struct mkdir_args *uap) 3644 { 3645 3646 return (kern_mkdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 3647 uap->mode)); 3648 } 3649 3650 #ifndef _SYS_SYSPROTO_H_ 3651 struct mkdirat_args { 3652 int fd; 3653 char *path; 3654 mode_t mode; 3655 }; 3656 #endif 3657 int 3658 sys_mkdirat(struct thread *td, struct mkdirat_args *uap) 3659 { 3660 3661 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode)); 3662 } 3663 3664 int 3665 kern_mkdirat(struct thread *td, int fd, const char *path, enum uio_seg segflg, 3666 int mode) 3667 { 3668 struct mount *mp; 3669 struct vnode *vp; 3670 struct vattr vattr; 3671 struct nameidata nd; 3672 int error; 3673 3674 AUDIT_ARG_MODE(mode); 3675 restart: 3676 bwillwrite(); 3677 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | SAVENAME | AUDITVNODE1 | 3678 NOCACHE, segflg, path, fd, &cap_mkdirat_rights, 3679 td); 3680 nd.ni_cnd.cn_flags |= WILLBEDIR; 3681 if ((error = namei(&nd)) != 0) 3682 return (error); 3683 vp = nd.ni_vp; 3684 if (vp != NULL) { 3685 NDFREE(&nd, NDF_ONLY_PNBUF); 3686 /* 3687 * XXX namei called with LOCKPARENT but not LOCKLEAF has 3688 * the strange behaviour of leaving the vnode unlocked 3689 * if the target is the same vnode as the parent. 3690 */ 3691 if (vp == nd.ni_dvp) 3692 vrele(nd.ni_dvp); 3693 else 3694 vput(nd.ni_dvp); 3695 vrele(vp); 3696 return (EEXIST); 3697 } 3698 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 3699 NDFREE(&nd, NDF_ONLY_PNBUF); 3700 vput(nd.ni_dvp); 3701 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0) 3702 return (error); 3703 goto restart; 3704 } 3705 VATTR_NULL(&vattr); 3706 vattr.va_type = VDIR; 3707 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_fd->fd_cmask; 3708 #ifdef MAC 3709 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd, 3710 &vattr); 3711 if (error != 0) 3712 goto out; 3713 #endif 3714 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr); 3715 #ifdef MAC 3716 out: 3717 #endif 3718 NDFREE(&nd, NDF_ONLY_PNBUF); 3719 vput(nd.ni_dvp); 3720 if (error == 0) 3721 vput(nd.ni_vp); 3722 vn_finished_write(mp); 3723 return (error); 3724 } 3725 3726 /* 3727 * Remove a directory file. 3728 */ 3729 #ifndef _SYS_SYSPROTO_H_ 3730 struct rmdir_args { 3731 char *path; 3732 }; 3733 #endif 3734 int 3735 sys_rmdir(struct thread *td, struct rmdir_args *uap) 3736 { 3737 3738 return (kern_frmdirat(td, AT_FDCWD, uap->path, FD_NONE, UIO_USERSPACE, 3739 0)); 3740 } 3741 3742 int 3743 kern_frmdirat(struct thread *td, int dfd, const char *path, int fd, 3744 enum uio_seg pathseg, int flag) 3745 { 3746 struct mount *mp; 3747 struct vnode *vp; 3748 struct file *fp; 3749 struct nameidata nd; 3750 cap_rights_t rights; 3751 int error; 3752 3753 fp = NULL; 3754 if (fd != FD_NONE) { 3755 error = getvnode(td, fd, cap_rights_init_one(&rights, CAP_LOOKUP), 3756 &fp); 3757 if (error != 0) 3758 return (error); 3759 } 3760 3761 restart: 3762 bwillwrite(); 3763 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 | 3764 ((flag & AT_BENEATH) != 0 ? BENEATH : 0), 3765 pathseg, path, dfd, &cap_unlinkat_rights, td); 3766 if ((error = namei(&nd)) != 0) 3767 goto fdout; 3768 vp = nd.ni_vp; 3769 if (vp->v_type != VDIR) { 3770 error = ENOTDIR; 3771 goto out; 3772 } 3773 /* 3774 * No rmdir "." please. 3775 */ 3776 if (nd.ni_dvp == vp) { 3777 error = EINVAL; 3778 goto out; 3779 } 3780 /* 3781 * The root of a mounted filesystem cannot be deleted. 3782 */ 3783 if (vp->v_vflag & VV_ROOT) { 3784 error = EBUSY; 3785 goto out; 3786 } 3787 3788 if (fp != NULL && fp->f_vnode != vp) { 3789 if (VN_IS_DOOMED(fp->f_vnode)) 3790 error = EBADF; 3791 else 3792 error = EDEADLK; 3793 goto out; 3794 } 3795 3796 #ifdef MAC 3797 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp, 3798 &nd.ni_cnd); 3799 if (error != 0) 3800 goto out; 3801 #endif 3802 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 3803 NDFREE(&nd, NDF_ONLY_PNBUF); 3804 vput(vp); 3805 if (nd.ni_dvp == vp) 3806 vrele(nd.ni_dvp); 3807 else 3808 vput(nd.ni_dvp); 3809 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0) 3810 goto fdout; 3811 goto restart; 3812 } 3813 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK); 3814 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd); 3815 vn_finished_write(mp); 3816 out: 3817 NDFREE(&nd, NDF_ONLY_PNBUF); 3818 vput(vp); 3819 if (nd.ni_dvp == vp) 3820 vrele(nd.ni_dvp); 3821 else 3822 vput(nd.ni_dvp); 3823 fdout: 3824 if (fp != NULL) 3825 fdrop(fp, td); 3826 return (error); 3827 } 3828 3829 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11) 3830 int 3831 freebsd11_kern_getdirentries(struct thread *td, int fd, char *ubuf, u_int count, 3832 long *basep, void (*func)(struct freebsd11_dirent *)) 3833 { 3834 struct freebsd11_dirent dstdp; 3835 struct dirent *dp, *edp; 3836 char *dirbuf; 3837 off_t base; 3838 ssize_t resid, ucount; 3839 int error; 3840 3841 /* XXX arbitrary sanity limit on `count'. */ 3842 count = min(count, 64 * 1024); 3843 3844 dirbuf = malloc(count, M_TEMP, M_WAITOK); 3845 3846 error = kern_getdirentries(td, fd, dirbuf, count, &base, &resid, 3847 UIO_SYSSPACE); 3848 if (error != 0) 3849 goto done; 3850 if (basep != NULL) 3851 *basep = base; 3852 3853 ucount = 0; 3854 for (dp = (struct dirent *)dirbuf, 3855 edp = (struct dirent *)&dirbuf[count - resid]; 3856 ucount < count && dp < edp; ) { 3857 if (dp->d_reclen == 0) 3858 break; 3859 MPASS(dp->d_reclen >= _GENERIC_DIRLEN(0)); 3860 if (dp->d_namlen >= sizeof(dstdp.d_name)) 3861 continue; 3862 dstdp.d_type = dp->d_type; 3863 dstdp.d_namlen = dp->d_namlen; 3864 dstdp.d_fileno = dp->d_fileno; /* truncate */ 3865 if (dstdp.d_fileno != dp->d_fileno) { 3866 switch (ino64_trunc_error) { 3867 default: 3868 case 0: 3869 break; 3870 case 1: 3871 error = EOVERFLOW; 3872 goto done; 3873 case 2: 3874 dstdp.d_fileno = UINT32_MAX; 3875 break; 3876 } 3877 } 3878 dstdp.d_reclen = sizeof(dstdp) - sizeof(dstdp.d_name) + 3879 ((dp->d_namlen + 1 + 3) &~ 3); 3880 bcopy(dp->d_name, dstdp.d_name, dstdp.d_namlen); 3881 bzero(dstdp.d_name + dstdp.d_namlen, 3882 dstdp.d_reclen - offsetof(struct freebsd11_dirent, d_name) - 3883 dstdp.d_namlen); 3884 MPASS(dstdp.d_reclen <= dp->d_reclen); 3885 MPASS(ucount + dstdp.d_reclen <= count); 3886 if (func != NULL) 3887 func(&dstdp); 3888 error = copyout(&dstdp, ubuf + ucount, dstdp.d_reclen); 3889 if (error != 0) 3890 break; 3891 dp = (struct dirent *)((char *)dp + dp->d_reclen); 3892 ucount += dstdp.d_reclen; 3893 } 3894 3895 done: 3896 free(dirbuf, M_TEMP); 3897 if (error == 0) 3898 td->td_retval[0] = ucount; 3899 return (error); 3900 } 3901 #endif /* COMPAT */ 3902 3903 #ifdef COMPAT_43 3904 static void 3905 ogetdirentries_cvt(struct freebsd11_dirent *dp) 3906 { 3907 #if (BYTE_ORDER == LITTLE_ENDIAN) 3908 /* 3909 * The expected low byte of dp->d_namlen is our dp->d_type. 3910 * The high MBZ byte of dp->d_namlen is our dp->d_namlen. 3911 */ 3912 dp->d_type = dp->d_namlen; 3913 dp->d_namlen = 0; 3914 #else 3915 /* 3916 * The dp->d_type is the high byte of the expected dp->d_namlen, 3917 * so must be zero'ed. 3918 */ 3919 dp->d_type = 0; 3920 #endif 3921 } 3922 3923 /* 3924 * Read a block of directory entries in a filesystem independent format. 3925 */ 3926 #ifndef _SYS_SYSPROTO_H_ 3927 struct ogetdirentries_args { 3928 int fd; 3929 char *buf; 3930 u_int count; 3931 long *basep; 3932 }; 3933 #endif 3934 int 3935 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap) 3936 { 3937 long loff; 3938 int error; 3939 3940 error = kern_ogetdirentries(td, uap, &loff); 3941 if (error == 0) 3942 error = copyout(&loff, uap->basep, sizeof(long)); 3943 return (error); 3944 } 3945 3946 int 3947 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap, 3948 long *ploff) 3949 { 3950 long base; 3951 int error; 3952 3953 /* XXX arbitrary sanity limit on `count'. */ 3954 if (uap->count > 64 * 1024) 3955 return (EINVAL); 3956 3957 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count, 3958 &base, ogetdirentries_cvt); 3959 3960 if (error == 0 && uap->basep != NULL) 3961 error = copyout(&base, uap->basep, sizeof(long)); 3962 3963 return (error); 3964 } 3965 #endif /* COMPAT_43 */ 3966 3967 #if defined(COMPAT_FREEBSD11) 3968 #ifndef _SYS_SYSPROTO_H_ 3969 struct freebsd11_getdirentries_args { 3970 int fd; 3971 char *buf; 3972 u_int count; 3973 long *basep; 3974 }; 3975 #endif 3976 int 3977 freebsd11_getdirentries(struct thread *td, 3978 struct freebsd11_getdirentries_args *uap) 3979 { 3980 long base; 3981 int error; 3982 3983 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count, 3984 &base, NULL); 3985 3986 if (error == 0 && uap->basep != NULL) 3987 error = copyout(&base, uap->basep, sizeof(long)); 3988 return (error); 3989 } 3990 3991 int 3992 freebsd11_getdents(struct thread *td, struct freebsd11_getdents_args *uap) 3993 { 3994 struct freebsd11_getdirentries_args ap; 3995 3996 ap.fd = uap->fd; 3997 ap.buf = uap->buf; 3998 ap.count = uap->count; 3999 ap.basep = NULL; 4000 return (freebsd11_getdirentries(td, &ap)); 4001 } 4002 #endif /* COMPAT_FREEBSD11 */ 4003 4004 /* 4005 * Read a block of directory entries in a filesystem independent format. 4006 */ 4007 int 4008 sys_getdirentries(struct thread *td, struct getdirentries_args *uap) 4009 { 4010 off_t base; 4011 int error; 4012 4013 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base, 4014 NULL, UIO_USERSPACE); 4015 if (error != 0) 4016 return (error); 4017 if (uap->basep != NULL) 4018 error = copyout(&base, uap->basep, sizeof(off_t)); 4019 return (error); 4020 } 4021 4022 int 4023 kern_getdirentries(struct thread *td, int fd, char *buf, size_t count, 4024 off_t *basep, ssize_t *residp, enum uio_seg bufseg) 4025 { 4026 struct vnode *vp; 4027 struct file *fp; 4028 struct uio auio; 4029 struct iovec aiov; 4030 off_t loff; 4031 int error, eofflag; 4032 off_t foffset; 4033 4034 AUDIT_ARG_FD(fd); 4035 if (count > IOSIZE_MAX) 4036 return (EINVAL); 4037 auio.uio_resid = count; 4038 error = getvnode(td, fd, &cap_read_rights, &fp); 4039 if (error != 0) 4040 return (error); 4041 if ((fp->f_flag & FREAD) == 0) { 4042 fdrop(fp, td); 4043 return (EBADF); 4044 } 4045 vp = fp->f_vnode; 4046 foffset = foffset_lock(fp, 0); 4047 unionread: 4048 if (vp->v_type != VDIR) { 4049 error = EINVAL; 4050 goto fail; 4051 } 4052 aiov.iov_base = buf; 4053 aiov.iov_len = count; 4054 auio.uio_iov = &aiov; 4055 auio.uio_iovcnt = 1; 4056 auio.uio_rw = UIO_READ; 4057 auio.uio_segflg = bufseg; 4058 auio.uio_td = td; 4059 vn_lock(vp, LK_SHARED | LK_RETRY); 4060 AUDIT_ARG_VNODE1(vp); 4061 loff = auio.uio_offset = foffset; 4062 #ifdef MAC 4063 error = mac_vnode_check_readdir(td->td_ucred, vp); 4064 if (error == 0) 4065 #endif 4066 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, 4067 NULL); 4068 foffset = auio.uio_offset; 4069 if (error != 0) { 4070 VOP_UNLOCK(vp); 4071 goto fail; 4072 } 4073 if (count == auio.uio_resid && 4074 (vp->v_vflag & VV_ROOT) && 4075 (vp->v_mount->mnt_flag & MNT_UNION)) { 4076 struct vnode *tvp = vp; 4077 4078 vp = vp->v_mount->mnt_vnodecovered; 4079 VREF(vp); 4080 fp->f_vnode = vp; 4081 fp->f_data = vp; 4082 foffset = 0; 4083 vput(tvp); 4084 goto unionread; 4085 } 4086 VOP_UNLOCK(vp); 4087 *basep = loff; 4088 if (residp != NULL) 4089 *residp = auio.uio_resid; 4090 td->td_retval[0] = count - auio.uio_resid; 4091 fail: 4092 foffset_unlock(fp, foffset, 0); 4093 fdrop(fp, td); 4094 return (error); 4095 } 4096 4097 /* 4098 * Set the mode mask for creation of filesystem nodes. 4099 */ 4100 #ifndef _SYS_SYSPROTO_H_ 4101 struct umask_args { 4102 int newmask; 4103 }; 4104 #endif 4105 int 4106 sys_umask(struct thread *td, struct umask_args *uap) 4107 { 4108 struct filedesc *fdp; 4109 4110 fdp = td->td_proc->p_fd; 4111 FILEDESC_XLOCK(fdp); 4112 td->td_retval[0] = fdp->fd_cmask; 4113 fdp->fd_cmask = uap->newmask & ALLPERMS; 4114 FILEDESC_XUNLOCK(fdp); 4115 return (0); 4116 } 4117 4118 /* 4119 * Void all references to file by ripping underlying filesystem away from 4120 * vnode. 4121 */ 4122 #ifndef _SYS_SYSPROTO_H_ 4123 struct revoke_args { 4124 char *path; 4125 }; 4126 #endif 4127 int 4128 sys_revoke(struct thread *td, struct revoke_args *uap) 4129 { 4130 struct vnode *vp; 4131 struct vattr vattr; 4132 struct nameidata nd; 4133 int error; 4134 4135 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE, 4136 uap->path, td); 4137 if ((error = namei(&nd)) != 0) 4138 return (error); 4139 vp = nd.ni_vp; 4140 NDFREE(&nd, NDF_ONLY_PNBUF); 4141 if (vp->v_type != VCHR || vp->v_rdev == NULL) { 4142 error = EINVAL; 4143 goto out; 4144 } 4145 #ifdef MAC 4146 error = mac_vnode_check_revoke(td->td_ucred, vp); 4147 if (error != 0) 4148 goto out; 4149 #endif 4150 error = VOP_GETATTR(vp, &vattr, td->td_ucred); 4151 if (error != 0) 4152 goto out; 4153 if (td->td_ucred->cr_uid != vattr.va_uid) { 4154 error = priv_check(td, PRIV_VFS_ADMIN); 4155 if (error != 0) 4156 goto out; 4157 } 4158 if (vp->v_usecount > 1 || vcount(vp) > 1) 4159 VOP_REVOKE(vp, REVOKEALL); 4160 out: 4161 vput(vp); 4162 return (error); 4163 } 4164 4165 /* 4166 * Convert a user file descriptor to a kernel file entry and check that, if it 4167 * is a capability, the correct rights are present. A reference on the file 4168 * entry is held upon returning. 4169 */ 4170 int 4171 getvnode(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp) 4172 { 4173 struct file *fp; 4174 int error; 4175 4176 error = fget_unlocked(td->td_proc->p_fd, fd, rightsp, &fp); 4177 if (error != 0) 4178 return (error); 4179 4180 /* 4181 * The file could be not of the vnode type, or it may be not 4182 * yet fully initialized, in which case the f_vnode pointer 4183 * may be set, but f_ops is still badfileops. E.g., 4184 * devfs_open() transiently create such situation to 4185 * facilitate csw d_fdopen(). 4186 * 4187 * Dupfdopen() handling in kern_openat() installs the 4188 * half-baked file into the process descriptor table, allowing 4189 * other thread to dereference it. Guard against the race by 4190 * checking f_ops. 4191 */ 4192 if (fp->f_vnode == NULL || fp->f_ops == &badfileops) { 4193 fdrop(fp, td); 4194 return (EINVAL); 4195 } 4196 *fpp = fp; 4197 return (0); 4198 } 4199 4200 /* 4201 * Get an (NFS) file handle. 4202 */ 4203 #ifndef _SYS_SYSPROTO_H_ 4204 struct lgetfh_args { 4205 char *fname; 4206 fhandle_t *fhp; 4207 }; 4208 #endif 4209 int 4210 sys_lgetfh(struct thread *td, struct lgetfh_args *uap) 4211 { 4212 4213 return (kern_getfhat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->fname, 4214 UIO_USERSPACE, uap->fhp)); 4215 } 4216 4217 #ifndef _SYS_SYSPROTO_H_ 4218 struct getfh_args { 4219 char *fname; 4220 fhandle_t *fhp; 4221 }; 4222 #endif 4223 int 4224 sys_getfh(struct thread *td, struct getfh_args *uap) 4225 { 4226 4227 return (kern_getfhat(td, 0, AT_FDCWD, uap->fname, UIO_USERSPACE, 4228 uap->fhp)); 4229 } 4230 4231 /* 4232 * syscall for the rpc.lockd to use to translate an open descriptor into 4233 * a NFS file handle. 4234 * 4235 * warning: do not remove the priv_check() call or this becomes one giant 4236 * security hole. 4237 */ 4238 #ifndef _SYS_SYSPROTO_H_ 4239 struct getfhat_args { 4240 int fd; 4241 char *path; 4242 fhandle_t *fhp; 4243 int flags; 4244 }; 4245 #endif 4246 int 4247 sys_getfhat(struct thread *td, struct getfhat_args *uap) 4248 { 4249 4250 if ((uap->flags & ~(AT_SYMLINK_NOFOLLOW | AT_BENEATH)) != 0) 4251 return (EINVAL); 4252 return (kern_getfhat(td, uap->flags, uap->fd, uap->path, UIO_USERSPACE, 4253 uap->fhp)); 4254 } 4255 4256 static int 4257 kern_getfhat(struct thread *td, int flags, int fd, const char *path, 4258 enum uio_seg pathseg, fhandle_t *fhp) 4259 { 4260 struct nameidata nd; 4261 fhandle_t fh; 4262 struct vnode *vp; 4263 int error; 4264 4265 error = priv_check(td, PRIV_VFS_GETFH); 4266 if (error != 0) 4267 return (error); 4268 NDINIT_AT(&nd, LOOKUP, ((flags & AT_SYMLINK_NOFOLLOW) != 0 ? NOFOLLOW : 4269 FOLLOW) | ((flags & AT_BENEATH) != 0 ? BENEATH : 0) | LOCKLEAF | 4270 AUDITVNODE1, pathseg, path, fd, td); 4271 error = namei(&nd); 4272 if (error != 0) 4273 return (error); 4274 NDFREE(&nd, NDF_ONLY_PNBUF); 4275 vp = nd.ni_vp; 4276 bzero(&fh, sizeof(fh)); 4277 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid; 4278 error = VOP_VPTOFH(vp, &fh.fh_fid); 4279 vput(vp); 4280 if (error == 0) 4281 error = copyout(&fh, fhp, sizeof (fh)); 4282 return (error); 4283 } 4284 4285 #ifndef _SYS_SYSPROTO_H_ 4286 struct fhlink_args { 4287 fhandle_t *fhp; 4288 const char *to; 4289 }; 4290 #endif 4291 int 4292 sys_fhlink(struct thread *td, struct fhlink_args *uap) 4293 { 4294 4295 return (kern_fhlinkat(td, AT_FDCWD, uap->to, UIO_USERSPACE, uap->fhp)); 4296 } 4297 4298 #ifndef _SYS_SYSPROTO_H_ 4299 struct fhlinkat_args { 4300 fhandle_t *fhp; 4301 int tofd; 4302 const char *to; 4303 }; 4304 #endif 4305 int 4306 sys_fhlinkat(struct thread *td, struct fhlinkat_args *uap) 4307 { 4308 4309 return (kern_fhlinkat(td, uap->tofd, uap->to, UIO_USERSPACE, uap->fhp)); 4310 } 4311 4312 static int 4313 kern_fhlinkat(struct thread *td, int fd, const char *path, 4314 enum uio_seg pathseg, fhandle_t *fhp) 4315 { 4316 fhandle_t fh; 4317 struct mount *mp; 4318 struct vnode *vp; 4319 int error; 4320 4321 error = priv_check(td, PRIV_VFS_GETFH); 4322 if (error != 0) 4323 return (error); 4324 error = copyin(fhp, &fh, sizeof(fh)); 4325 if (error != 0) 4326 return (error); 4327 do { 4328 bwillwrite(); 4329 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL) 4330 return (ESTALE); 4331 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp); 4332 vfs_unbusy(mp); 4333 if (error != 0) 4334 return (error); 4335 VOP_UNLOCK(vp); 4336 } while ((error = kern_linkat_vp(td, vp, fd, path, pathseg)) == EAGAIN); 4337 return (error); 4338 } 4339 4340 #ifndef _SYS_SYSPROTO_H_ 4341 struct fhreadlink_args { 4342 fhandle_t *fhp; 4343 char *buf; 4344 size_t bufsize; 4345 }; 4346 #endif 4347 int 4348 sys_fhreadlink(struct thread *td, struct fhreadlink_args *uap) 4349 { 4350 fhandle_t fh; 4351 struct mount *mp; 4352 struct vnode *vp; 4353 int error; 4354 4355 error = priv_check(td, PRIV_VFS_GETFH); 4356 if (error != 0) 4357 return (error); 4358 if (uap->bufsize > IOSIZE_MAX) 4359 return (EINVAL); 4360 error = copyin(uap->fhp, &fh, sizeof(fh)); 4361 if (error != 0) 4362 return (error); 4363 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL) 4364 return (ESTALE); 4365 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp); 4366 vfs_unbusy(mp); 4367 if (error != 0) 4368 return (error); 4369 error = kern_readlink_vp(vp, uap->buf, UIO_USERSPACE, uap->bufsize, td); 4370 vput(vp); 4371 return (error); 4372 } 4373 4374 /* 4375 * syscall for the rpc.lockd to use to translate a NFS file handle into an 4376 * open descriptor. 4377 * 4378 * warning: do not remove the priv_check() call or this becomes one giant 4379 * security hole. 4380 */ 4381 #ifndef _SYS_SYSPROTO_H_ 4382 struct fhopen_args { 4383 const struct fhandle *u_fhp; 4384 int flags; 4385 }; 4386 #endif 4387 int 4388 sys_fhopen(struct thread *td, struct fhopen_args *uap) 4389 { 4390 struct mount *mp; 4391 struct vnode *vp; 4392 struct fhandle fhp; 4393 struct file *fp; 4394 int fmode, error; 4395 int indx; 4396 4397 error = priv_check(td, PRIV_VFS_FHOPEN); 4398 if (error != 0) 4399 return (error); 4400 indx = -1; 4401 fmode = FFLAGS(uap->flags); 4402 /* why not allow a non-read/write open for our lockd? */ 4403 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT)) 4404 return (EINVAL); 4405 error = copyin(uap->u_fhp, &fhp, sizeof(fhp)); 4406 if (error != 0) 4407 return(error); 4408 /* find the mount point */ 4409 mp = vfs_busyfs(&fhp.fh_fsid); 4410 if (mp == NULL) 4411 return (ESTALE); 4412 /* now give me my vnode, it gets returned to me locked */ 4413 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp); 4414 vfs_unbusy(mp); 4415 if (error != 0) 4416 return (error); 4417 4418 error = falloc_noinstall(td, &fp); 4419 if (error != 0) { 4420 vput(vp); 4421 return (error); 4422 } 4423 /* 4424 * An extra reference on `fp' has been held for us by 4425 * falloc_noinstall(). 4426 */ 4427 4428 #ifdef INVARIANTS 4429 td->td_dupfd = -1; 4430 #endif 4431 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp); 4432 if (error != 0) { 4433 KASSERT(fp->f_ops == &badfileops, 4434 ("VOP_OPEN in fhopen() set f_ops")); 4435 KASSERT(td->td_dupfd < 0, 4436 ("fhopen() encountered fdopen()")); 4437 4438 vput(vp); 4439 goto bad; 4440 } 4441 #ifdef INVARIANTS 4442 td->td_dupfd = 0; 4443 #endif 4444 fp->f_vnode = vp; 4445 fp->f_seqcount = 1; 4446 finit(fp, (fmode & FMASK) | (fp->f_flag & FHASLOCK), DTYPE_VNODE, vp, 4447 &vnops); 4448 VOP_UNLOCK(vp); 4449 if ((fmode & O_TRUNC) != 0) { 4450 error = fo_truncate(fp, 0, td->td_ucred, td); 4451 if (error != 0) 4452 goto bad; 4453 } 4454 4455 error = finstall(td, fp, &indx, fmode, NULL); 4456 bad: 4457 fdrop(fp, td); 4458 td->td_retval[0] = indx; 4459 return (error); 4460 } 4461 4462 /* 4463 * Stat an (NFS) file handle. 4464 */ 4465 #ifndef _SYS_SYSPROTO_H_ 4466 struct fhstat_args { 4467 struct fhandle *u_fhp; 4468 struct stat *sb; 4469 }; 4470 #endif 4471 int 4472 sys_fhstat(struct thread *td, struct fhstat_args *uap) 4473 { 4474 struct stat sb; 4475 struct fhandle fh; 4476 int error; 4477 4478 error = copyin(uap->u_fhp, &fh, sizeof(fh)); 4479 if (error != 0) 4480 return (error); 4481 error = kern_fhstat(td, fh, &sb); 4482 if (error == 0) 4483 error = copyout(&sb, uap->sb, sizeof(sb)); 4484 return (error); 4485 } 4486 4487 int 4488 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb) 4489 { 4490 struct mount *mp; 4491 struct vnode *vp; 4492 int error; 4493 4494 error = priv_check(td, PRIV_VFS_FHSTAT); 4495 if (error != 0) 4496 return (error); 4497 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL) 4498 return (ESTALE); 4499 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp); 4500 vfs_unbusy(mp); 4501 if (error != 0) 4502 return (error); 4503 error = vn_stat(vp, sb, td->td_ucred, NOCRED, td); 4504 vput(vp); 4505 return (error); 4506 } 4507 4508 /* 4509 * Implement fstatfs() for (NFS) file handles. 4510 */ 4511 #ifndef _SYS_SYSPROTO_H_ 4512 struct fhstatfs_args { 4513 struct fhandle *u_fhp; 4514 struct statfs *buf; 4515 }; 4516 #endif 4517 int 4518 sys_fhstatfs(struct thread *td, struct fhstatfs_args *uap) 4519 { 4520 struct statfs *sfp; 4521 fhandle_t fh; 4522 int error; 4523 4524 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t)); 4525 if (error != 0) 4526 return (error); 4527 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK); 4528 error = kern_fhstatfs(td, fh, sfp); 4529 if (error == 0) 4530 error = copyout(sfp, uap->buf, sizeof(*sfp)); 4531 free(sfp, M_STATFS); 4532 return (error); 4533 } 4534 4535 int 4536 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf) 4537 { 4538 struct mount *mp; 4539 struct vnode *vp; 4540 int error; 4541 4542 error = priv_check(td, PRIV_VFS_FHSTATFS); 4543 if (error != 0) 4544 return (error); 4545 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL) 4546 return (ESTALE); 4547 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp); 4548 if (error != 0) { 4549 vfs_unbusy(mp); 4550 return (error); 4551 } 4552 vput(vp); 4553 error = prison_canseemount(td->td_ucred, mp); 4554 if (error != 0) 4555 goto out; 4556 #ifdef MAC 4557 error = mac_mount_check_stat(td->td_ucred, mp); 4558 if (error != 0) 4559 goto out; 4560 #endif 4561 error = VFS_STATFS(mp, buf); 4562 out: 4563 vfs_unbusy(mp); 4564 return (error); 4565 } 4566 4567 /* 4568 * Unlike madvise(2), we do not make a best effort to remember every 4569 * possible caching hint. Instead, we remember the last setting with 4570 * the exception that we will allow POSIX_FADV_NORMAL to adjust the 4571 * region of any current setting. 4572 */ 4573 int 4574 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len, 4575 int advice) 4576 { 4577 struct fadvise_info *fa, *new; 4578 struct file *fp; 4579 struct vnode *vp; 4580 off_t end; 4581 int error; 4582 4583 if (offset < 0 || len < 0 || offset > OFF_MAX - len) 4584 return (EINVAL); 4585 AUDIT_ARG_VALUE(advice); 4586 switch (advice) { 4587 case POSIX_FADV_SEQUENTIAL: 4588 case POSIX_FADV_RANDOM: 4589 case POSIX_FADV_NOREUSE: 4590 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK); 4591 break; 4592 case POSIX_FADV_NORMAL: 4593 case POSIX_FADV_WILLNEED: 4594 case POSIX_FADV_DONTNEED: 4595 new = NULL; 4596 break; 4597 default: 4598 return (EINVAL); 4599 } 4600 /* XXX: CAP_POSIX_FADVISE? */ 4601 AUDIT_ARG_FD(fd); 4602 error = fget(td, fd, &cap_no_rights, &fp); 4603 if (error != 0) 4604 goto out; 4605 AUDIT_ARG_FILE(td->td_proc, fp); 4606 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) { 4607 error = ESPIPE; 4608 goto out; 4609 } 4610 if (fp->f_type != DTYPE_VNODE) { 4611 error = ENODEV; 4612 goto out; 4613 } 4614 vp = fp->f_vnode; 4615 if (vp->v_type != VREG) { 4616 error = ENODEV; 4617 goto out; 4618 } 4619 if (len == 0) 4620 end = OFF_MAX; 4621 else 4622 end = offset + len - 1; 4623 switch (advice) { 4624 case POSIX_FADV_SEQUENTIAL: 4625 case POSIX_FADV_RANDOM: 4626 case POSIX_FADV_NOREUSE: 4627 /* 4628 * Try to merge any existing non-standard region with 4629 * this new region if possible, otherwise create a new 4630 * non-standard region for this request. 4631 */ 4632 mtx_pool_lock(mtxpool_sleep, fp); 4633 fa = fp->f_advice; 4634 if (fa != NULL && fa->fa_advice == advice && 4635 ((fa->fa_start <= end && fa->fa_end >= offset) || 4636 (end != OFF_MAX && fa->fa_start == end + 1) || 4637 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) { 4638 if (offset < fa->fa_start) 4639 fa->fa_start = offset; 4640 if (end > fa->fa_end) 4641 fa->fa_end = end; 4642 } else { 4643 new->fa_advice = advice; 4644 new->fa_start = offset; 4645 new->fa_end = end; 4646 fp->f_advice = new; 4647 new = fa; 4648 } 4649 mtx_pool_unlock(mtxpool_sleep, fp); 4650 break; 4651 case POSIX_FADV_NORMAL: 4652 /* 4653 * If a the "normal" region overlaps with an existing 4654 * non-standard region, trim or remove the 4655 * non-standard region. 4656 */ 4657 mtx_pool_lock(mtxpool_sleep, fp); 4658 fa = fp->f_advice; 4659 if (fa != NULL) { 4660 if (offset <= fa->fa_start && end >= fa->fa_end) { 4661 new = fa; 4662 fp->f_advice = NULL; 4663 } else if (offset <= fa->fa_start && 4664 end >= fa->fa_start) 4665 fa->fa_start = end + 1; 4666 else if (offset <= fa->fa_end && end >= fa->fa_end) 4667 fa->fa_end = offset - 1; 4668 else if (offset >= fa->fa_start && end <= fa->fa_end) { 4669 /* 4670 * If the "normal" region is a middle 4671 * portion of the existing 4672 * non-standard region, just remove 4673 * the whole thing rather than picking 4674 * one side or the other to 4675 * preserve. 4676 */ 4677 new = fa; 4678 fp->f_advice = NULL; 4679 } 4680 } 4681 mtx_pool_unlock(mtxpool_sleep, fp); 4682 break; 4683 case POSIX_FADV_WILLNEED: 4684 case POSIX_FADV_DONTNEED: 4685 error = VOP_ADVISE(vp, offset, end, advice); 4686 break; 4687 } 4688 out: 4689 if (fp != NULL) 4690 fdrop(fp, td); 4691 free(new, M_FADVISE); 4692 return (error); 4693 } 4694 4695 int 4696 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap) 4697 { 4698 int error; 4699 4700 error = kern_posix_fadvise(td, uap->fd, uap->offset, uap->len, 4701 uap->advice); 4702 return (kern_posix_error(td, error)); 4703 } 4704 4705 int 4706 kern_copy_file_range(struct thread *td, int infd, off_t *inoffp, int outfd, 4707 off_t *outoffp, size_t len, unsigned int flags) 4708 { 4709 struct file *infp, *outfp; 4710 struct vnode *invp, *outvp; 4711 int error; 4712 size_t retlen; 4713 void *rl_rcookie, *rl_wcookie; 4714 off_t savinoff, savoutoff; 4715 4716 infp = outfp = NULL; 4717 rl_rcookie = rl_wcookie = NULL; 4718 savinoff = -1; 4719 error = 0; 4720 retlen = 0; 4721 4722 if (flags != 0) { 4723 error = EINVAL; 4724 goto out; 4725 } 4726 if (len > SSIZE_MAX) 4727 /* 4728 * Although the len argument is size_t, the return argument 4729 * is ssize_t (which is signed). Therefore a size that won't 4730 * fit in ssize_t can't be returned. 4731 */ 4732 len = SSIZE_MAX; 4733 4734 /* Get the file structures for the file descriptors. */ 4735 error = fget_read(td, infd, &cap_read_rights, &infp); 4736 if (error != 0) 4737 goto out; 4738 if (infp->f_ops == &badfileops) { 4739 error = EBADF; 4740 goto out; 4741 } 4742 if (infp->f_vnode == NULL) { 4743 error = EINVAL; 4744 goto out; 4745 } 4746 error = fget_write(td, outfd, &cap_write_rights, &outfp); 4747 if (error != 0) 4748 goto out; 4749 if (outfp->f_ops == &badfileops) { 4750 error = EBADF; 4751 goto out; 4752 } 4753 if (outfp->f_vnode == NULL) { 4754 error = EINVAL; 4755 goto out; 4756 } 4757 4758 /* Set the offset pointers to the correct place. */ 4759 if (inoffp == NULL) 4760 inoffp = &infp->f_offset; 4761 if (outoffp == NULL) 4762 outoffp = &outfp->f_offset; 4763 savinoff = *inoffp; 4764 savoutoff = *outoffp; 4765 4766 invp = infp->f_vnode; 4767 outvp = outfp->f_vnode; 4768 /* Sanity check the f_flag bits. */ 4769 if ((outfp->f_flag & (FWRITE | FAPPEND)) != FWRITE || 4770 (infp->f_flag & FREAD) == 0) { 4771 error = EBADF; 4772 goto out; 4773 } 4774 4775 /* If len == 0, just return 0. */ 4776 if (len == 0) 4777 goto out; 4778 4779 /* 4780 * If infp and outfp refer to the same file, the byte ranges cannot 4781 * overlap. 4782 */ 4783 if (invp == outvp && ((savinoff <= savoutoff && savinoff + len > 4784 savoutoff) || (savinoff > savoutoff && savoutoff + len > 4785 savinoff))) { 4786 error = EINVAL; 4787 goto out; 4788 } 4789 4790 /* Range lock the byte ranges for both invp and outvp. */ 4791 for (;;) { 4792 rl_wcookie = vn_rangelock_wlock(outvp, *outoffp, *outoffp + 4793 len); 4794 rl_rcookie = vn_rangelock_tryrlock(invp, *inoffp, *inoffp + 4795 len); 4796 if (rl_rcookie != NULL) 4797 break; 4798 vn_rangelock_unlock(outvp, rl_wcookie); 4799 rl_rcookie = vn_rangelock_rlock(invp, *inoffp, *inoffp + len); 4800 vn_rangelock_unlock(invp, rl_rcookie); 4801 } 4802 4803 retlen = len; 4804 error = vn_copy_file_range(invp, inoffp, outvp, outoffp, &retlen, 4805 flags, infp->f_cred, outfp->f_cred, td); 4806 out: 4807 if (rl_rcookie != NULL) 4808 vn_rangelock_unlock(invp, rl_rcookie); 4809 if (rl_wcookie != NULL) 4810 vn_rangelock_unlock(outvp, rl_wcookie); 4811 if (savinoff != -1 && (error == EINTR || error == ERESTART)) { 4812 *inoffp = savinoff; 4813 *outoffp = savoutoff; 4814 } 4815 if (outfp != NULL) 4816 fdrop(outfp, td); 4817 if (infp != NULL) 4818 fdrop(infp, td); 4819 td->td_retval[0] = retlen; 4820 return (error); 4821 } 4822 4823 int 4824 sys_copy_file_range(struct thread *td, struct copy_file_range_args *uap) 4825 { 4826 off_t inoff, outoff, *inoffp, *outoffp; 4827 int error; 4828 4829 inoffp = outoffp = NULL; 4830 if (uap->inoffp != NULL) { 4831 error = copyin(uap->inoffp, &inoff, sizeof(off_t)); 4832 if (error != 0) 4833 return (error); 4834 inoffp = &inoff; 4835 } 4836 if (uap->outoffp != NULL) { 4837 error = copyin(uap->outoffp, &outoff, sizeof(off_t)); 4838 if (error != 0) 4839 return (error); 4840 outoffp = &outoff; 4841 } 4842 error = kern_copy_file_range(td, uap->infd, inoffp, uap->outfd, 4843 outoffp, uap->len, uap->flags); 4844 if (error == 0 && uap->inoffp != NULL) 4845 error = copyout(inoffp, uap->inoffp, sizeof(off_t)); 4846 if (error == 0 && uap->outoffp != NULL) 4847 error = copyout(outoffp, uap->outoffp, sizeof(off_t)); 4848 return (error); 4849 } 4850