1 /* $NetBSD: ffs_vfsops.c,v 1.384 2024/12/30 09:03:07 hannken Exp $ */ 2 3 /*- 4 * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Wasabi Systems, Inc, and by Andrew Doran. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Copyright (c) 1989, 1991, 1993, 1994 34 * The Regents of the University of California. All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions 38 * are met: 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 2. Redistributions in binary form must reproduce the above copyright 42 * notice, this list of conditions and the following disclaimer in the 43 * documentation and/or other materials provided with the distribution. 44 * 3. Neither the name of the University nor the names of its contributors 45 * may be used to endorse or promote products derived from this software 46 * without specific prior written permission. 47 * 48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 58 * SUCH DAMAGE. 59 * 60 * @(#)ffs_vfsops.c 8.31 (Berkeley) 5/20/95 61 */ 62 63 #include <sys/cdefs.h> 64 __KERNEL_RCSID(0, "$NetBSD: ffs_vfsops.c,v 1.384 2024/12/30 09:03:07 hannken Exp $"); 65 66 #if defined(_KERNEL_OPT) 67 #include "opt_ffs.h" 68 #include "opt_quota.h" 69 #include "opt_wapbl.h" 70 #endif 71 72 #include <sys/param.h> 73 #include <sys/systm.h> 74 #include <sys/namei.h> 75 #include <sys/proc.h> 76 #include <sys/kernel.h> 77 #include <sys/vnode.h> 78 #include <sys/fstrans.h> 79 #include <sys/socket.h> 80 #include <sys/mount.h> 81 #include <sys/buf.h> 82 #include <sys/device.h> 83 #include <sys/disk.h> 84 #include <sys/file.h> 85 #include <sys/disklabel.h> 86 #include <sys/ioctl.h> 87 #include <sys/errno.h> 88 #include <sys/kmem.h> 89 #include <sys/pool.h> 90 #include <sys/lock.h> 91 #include <sys/sysctl.h> 92 #include <sys/conf.h> 93 #include <sys/kauth.h> 94 #include <sys/wapbl.h> 95 #include <sys/module.h> 96 97 #include <miscfs/genfs/genfs.h> 98 #include <miscfs/specfs/specdev.h> 99 100 #include <ufs/ufs/quota.h> 101 #include <ufs/ufs/ufsmount.h> 102 #include <ufs/ufs/inode.h> 103 #include <ufs/ufs/dir.h> 104 #include <ufs/ufs/ufs_extern.h> 105 #include <ufs/ufs/ufs_bswap.h> 106 #include <ufs/ufs/ufs_wapbl.h> 107 108 #include <ufs/ffs/fs.h> 109 #include <ufs/ffs/ffs_extern.h> 110 111 #ifdef WAPBL 112 MODULE(MODULE_CLASS_VFS, ffs, "ufs,wapbl"); 113 #else 114 MODULE(MODULE_CLASS_VFS, ffs, "ufs"); 115 #endif 116 117 static int ffs_vfs_fsync(vnode_t *, int); 118 static int ffs_superblock_validate(struct fs *); 119 static int ffs_is_appleufs(struct vnode *, struct fs *); 120 121 static int ffs_init_vnode(struct ufsmount *, struct vnode *, ino_t); 122 static void ffs_deinit_vnode(struct ufsmount *, struct vnode *); 123 124 static kauth_listener_t ffs_snapshot_listener; 125 126 /* how many times ffs_init() was called */ 127 int ffs_initcount = 0; 128 129 #ifdef DEBUG_FFS_MOUNT 130 #define DPRINTF(_fmt, args...) printf("%s: " _fmt "\n", __func__, ##args) 131 #else 132 #define DPRINTF(_fmt, args...) do {} while (/*CONSTCOND*/0) 133 #endif 134 135 extern const struct vnodeopv_desc ffs_vnodeop_opv_desc; 136 extern const struct vnodeopv_desc ffs_specop_opv_desc; 137 extern const struct vnodeopv_desc ffs_fifoop_opv_desc; 138 139 const struct vnodeopv_desc * const ffs_vnodeopv_descs[] = { 140 &ffs_vnodeop_opv_desc, 141 &ffs_specop_opv_desc, 142 &ffs_fifoop_opv_desc, 143 NULL, 144 }; 145 146 struct vfsops ffs_vfsops = { 147 .vfs_name = MOUNT_FFS, 148 .vfs_min_mount_data = sizeof (struct ufs_args), 149 .vfs_mount = ffs_mount, 150 .vfs_start = ufs_start, 151 .vfs_unmount = ffs_unmount, 152 .vfs_root = ufs_root, 153 .vfs_quotactl = ufs_quotactl, 154 .vfs_statvfs = ffs_statvfs, 155 .vfs_sync = ffs_sync, 156 .vfs_vget = ufs_vget, 157 .vfs_loadvnode = ffs_loadvnode, 158 .vfs_newvnode = ffs_newvnode, 159 .vfs_fhtovp = ffs_fhtovp, 160 .vfs_vptofh = ffs_vptofh, 161 .vfs_init = ffs_init, 162 .vfs_reinit = ffs_reinit, 163 .vfs_done = ffs_done, 164 .vfs_mountroot = ffs_mountroot, 165 .vfs_snapshot = ffs_snapshot, 166 .vfs_extattrctl = ffs_extattrctl, 167 .vfs_suspendctl = genfs_suspendctl, 168 .vfs_renamelock_enter = genfs_renamelock_enter, 169 .vfs_renamelock_exit = genfs_renamelock_exit, 170 .vfs_fsync = ffs_vfs_fsync, 171 .vfs_opv_descs = ffs_vnodeopv_descs 172 }; 173 174 static const struct genfs_ops ffs_genfsops = { 175 .gop_size = ffs_gop_size, 176 .gop_alloc = ufs_gop_alloc, 177 .gop_write = genfs_gop_write, 178 .gop_markupdate = ufs_gop_markupdate, 179 .gop_putrange = genfs_gop_putrange, 180 }; 181 182 static const struct ufs_ops ffs_ufsops = { 183 .uo_itimes = ffs_itimes, 184 .uo_update = ffs_update, 185 .uo_truncate = ffs_truncate, 186 .uo_balloc = ffs_balloc, 187 .uo_snapgone = ffs_snapgone, 188 .uo_bufrd = ffs_bufrd, 189 .uo_bufwr = ffs_bufwr, 190 }; 191 192 static int 193 ffs_checkrange(struct mount *mp, ino_t ino) 194 { 195 struct fs *fs = VFSTOUFS(mp)->um_fs; 196 197 if (ino < UFS_ROOTINO || ino >= fs->fs_ncg * fs->fs_ipg) { 198 DPRINTF("out of range %" PRIu64 "\n", ino); 199 return ESTALE; 200 } 201 202 /* 203 * Need to check if inode is initialized because ffsv2 does 204 * lazy initialization and we can get here from nfs_fhtovp 205 */ 206 if (fs->fs_magic != FS_UFS2_MAGIC) 207 return 0; 208 209 struct buf *bp; 210 int cg = ino_to_cg(fs, ino); 211 struct ufsmount *ump = VFSTOUFS(mp); 212 213 int error = bread(ump->um_devvp, FFS_FSBTODB(fs, cgtod(fs, cg)), 214 (int)fs->fs_cgsize, B_MODIFY, &bp); 215 if (error) { 216 DPRINTF("error %d reading cg %d ino %" PRIu64 "\n", 217 error, cg, ino); 218 return error; 219 } 220 221 const int needswap = UFS_FSNEEDSWAP(fs); 222 223 struct cg *cgp = (struct cg *)bp->b_data; 224 if (!cg_chkmagic(cgp, needswap)) { 225 brelse(bp, 0); 226 DPRINTF("bad cylinder group magic cg %d ino %" PRIu64 "\n", 227 cg, ino); 228 return ESTALE; 229 } 230 231 int32_t initediblk = ufs_rw32(cgp->cg_initediblk, needswap); 232 brelse(bp, 0); 233 234 if (cg * fs->fs_ipg + initediblk < ino) { 235 DPRINTF("cg=%d fs->fs_ipg=%d initediblk=%d ino=%" PRIu64 "\n", 236 cg, fs->fs_ipg, initediblk, ino); 237 return ESTALE; 238 } 239 return 0; 240 } 241 242 static int 243 ffs_snapshot_cb(kauth_cred_t cred, kauth_action_t action, void *cookie, 244 void *arg0, void *arg1, void *arg2, void *arg3) 245 { 246 vnode_t *vp = arg2; 247 int result = KAUTH_RESULT_DEFER; 248 249 if (action != KAUTH_SYSTEM_FS_SNAPSHOT) 250 return result; 251 252 if (VTOI(vp)->i_uid == kauth_cred_geteuid(cred)) 253 result = KAUTH_RESULT_ALLOW; 254 255 return result; 256 } 257 258 SYSCTL_SETUP(ffs_sysctl_setup, "ffs sysctls") 259 { 260 #ifdef UFS_EXTATTR 261 extern int ufs_extattr_autocreate; 262 #endif 263 extern int ffs_log_changeopt; 264 265 sysctl_createv(clog, 0, NULL, NULL, 266 CTLFLAG_PERMANENT, 267 CTLTYPE_NODE, "ffs", 268 SYSCTL_DESCR("Berkeley Fast File System"), 269 NULL, 0, NULL, 0, 270 CTL_VFS, 1, CTL_EOL); 271 /* 272 * @@@ should we even bother with these first three? 273 */ 274 sysctl_createv(clog, 0, NULL, NULL, 275 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 276 CTLTYPE_INT, "doclusterread", NULL, 277 sysctl_notavail, 0, NULL, 0, 278 CTL_VFS, 1, FFS_CLUSTERREAD, CTL_EOL); 279 sysctl_createv(clog, 0, NULL, NULL, 280 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 281 CTLTYPE_INT, "doclusterwrite", NULL, 282 sysctl_notavail, 0, NULL, 0, 283 CTL_VFS, 1, FFS_CLUSTERWRITE, CTL_EOL); 284 sysctl_createv(clog, 0, NULL, NULL, 285 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 286 CTLTYPE_INT, "doreallocblks", NULL, 287 sysctl_notavail, 0, NULL, 0, 288 CTL_VFS, 1, FFS_REALLOCBLKS, CTL_EOL); 289 #if 0 290 sysctl_createv(clog, 0, NULL, NULL, 291 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 292 CTLTYPE_INT, "doasyncfree", 293 SYSCTL_DESCR("Release dirty blocks asynchronously"), 294 NULL, 0, &doasyncfree, 0, 295 CTL_VFS, 1, FFS_ASYNCFREE, CTL_EOL); 296 #endif 297 sysctl_createv(clog, 0, NULL, NULL, 298 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 299 CTLTYPE_INT, "log_changeopt", 300 SYSCTL_DESCR("Log changes in optimization strategy"), 301 NULL, 0, &ffs_log_changeopt, 0, 302 CTL_VFS, 1, FFS_LOG_CHANGEOPT, CTL_EOL); 303 #ifdef UFS_EXTATTR 304 sysctl_createv(clog, 0, NULL, NULL, 305 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 306 CTLTYPE_INT, "extattr_autocreate", 307 SYSCTL_DESCR("Size of attribute for " 308 "backing file autocreation"), 309 NULL, 0, &ufs_extattr_autocreate, 0, 310 CTL_VFS, 1, FFS_EXTATTR_AUTOCREATE, CTL_EOL); 311 312 #endif /* UFS_EXTATTR */ 313 } 314 315 static int 316 ffs_modcmd(modcmd_t cmd, void *arg) 317 { 318 int error; 319 320 #if 0 321 extern int doasyncfree; 322 #endif 323 324 switch (cmd) { 325 case MODULE_CMD_INIT: 326 error = vfs_attach(&ffs_vfsops); 327 if (error != 0) 328 break; 329 330 ffs_snapshot_listener = kauth_listen_scope(KAUTH_SCOPE_SYSTEM, 331 ffs_snapshot_cb, NULL); 332 if (ffs_snapshot_listener == NULL) 333 printf("ffs_modcmd: can't listen on system scope.\n"); 334 335 break; 336 case MODULE_CMD_FINI: 337 error = vfs_detach(&ffs_vfsops); 338 if (error != 0) 339 break; 340 if (ffs_snapshot_listener != NULL) 341 kauth_unlisten_scope(ffs_snapshot_listener); 342 break; 343 default: 344 error = ENOTTY; 345 break; 346 } 347 348 return (error); 349 } 350 351 pool_cache_t ffs_inode_cache; 352 pool_cache_t ffs_dinode1_cache; 353 pool_cache_t ffs_dinode2_cache; 354 355 static void ffs_oldfscompat_read(struct fs *, struct ufsmount *, daddr_t); 356 static void ffs_oldfscompat_write(struct fs *, struct ufsmount *); 357 358 /* 359 * Called by main() when ffs is going to be mounted as root. 360 */ 361 362 int 363 ffs_mountroot(void) 364 { 365 struct fs *fs; 366 struct mount *mp; 367 struct lwp *l = curlwp; /* XXX */ 368 struct ufsmount *ump; 369 int error; 370 371 if (device_class(root_device) != DV_DISK) 372 return (ENODEV); 373 374 if ((error = vfs_rootmountalloc(MOUNT_FFS, "root_device", &mp))) { 375 vrele(rootvp); 376 return (error); 377 } 378 379 /* 380 * We always need to be able to mount the root file system. 381 */ 382 mp->mnt_flag |= MNT_FORCE; 383 if ((error = ffs_mountfs(rootvp, mp, l)) != 0) { 384 vfs_unbusy(mp); 385 vfs_rele(mp); 386 return (error); 387 } 388 mp->mnt_flag &= ~MNT_FORCE; 389 mountlist_append(mp); 390 ump = VFSTOUFS(mp); 391 fs = ump->um_fs; 392 memset(fs->fs_fsmnt, 0, sizeof(fs->fs_fsmnt)); 393 (void)copystr(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN - 1, 0); 394 (void)ffs_statvfs(mp, &mp->mnt_stat); 395 vfs_unbusy(mp); 396 setrootfstime((time_t)fs->fs_time); 397 return (0); 398 } 399 400 static int 401 ffs_acls(struct mount *mp, int fs_flags) 402 { 403 struct ufsmount *ump; 404 405 ump = VFSTOUFS(mp); 406 if (ump->um_fstype == UFS2 && (ump->um_flags & UFS_EA) == 0 && 407 ((mp->mnt_flag & (MNT_POSIX1EACLS | MNT_NFS4ACLS)) != 0 || 408 (fs_flags & (FS_POSIX1EACLS | FS_NFS4ACLS)) != 0)) { 409 printf("%s: ACLs requested but not supported by this fs\n", 410 mp->mnt_stat.f_mntonname); 411 return EINVAL; 412 } 413 414 if ((fs_flags & FS_POSIX1EACLS) != 0) { 415 #ifdef UFS_ACL 416 if (mp->mnt_flag & MNT_NFS4ACLS) 417 printf("WARNING: %s: POSIX.1e ACLs flag on fs conflicts " 418 "with \"nfsv4acls\" mount option; option ignored\n", 419 mp->mnt_stat.f_mntonname); 420 mp->mnt_flag &= ~MNT_NFS4ACLS; 421 mp->mnt_flag |= MNT_POSIX1EACLS; 422 #else 423 printf("WARNING: %s: POSIX.1e ACLs flag on fs but no " 424 "ACLs support\n", mp->mnt_stat.f_mntonname); 425 #endif 426 } 427 if ((fs_flags & FS_NFS4ACLS) != 0) { 428 #ifdef UFS_ACL 429 if (mp->mnt_flag & MNT_POSIX1EACLS) 430 printf("WARNING: %s: NFSv4 ACLs flag on fs conflicts " 431 "with \"posix1eacls\" mount option; option ignored\n", 432 mp->mnt_stat.f_mntonname); 433 mp->mnt_flag &= ~MNT_POSIX1EACLS; 434 mp->mnt_flag |= MNT_NFS4ACLS; 435 436 #else 437 printf("WARNING: %s: NFSv4 ACLs flag on fs but no " 438 "ACLs support\n", mp->mnt_stat.f_mntonname); 439 #endif 440 } 441 if ((mp->mnt_flag & (MNT_NFS4ACLS | MNT_POSIX1EACLS)) 442 == (MNT_NFS4ACLS | MNT_POSIX1EACLS)) 443 { 444 printf("%s: \"posix1eacls\" and \"nfsv4acls\" options " 445 "are mutually exclusive\n", 446 mp->mnt_stat.f_mntonname); 447 return EINVAL; 448 } 449 450 if (mp->mnt_flag & (MNT_NFS4ACLS | MNT_POSIX1EACLS)) 451 mp->mnt_iflag &= ~(IMNT_SHRLOOKUP|IMNT_NCLOOKUP); 452 else 453 mp->mnt_iflag |= IMNT_SHRLOOKUP|IMNT_NCLOOKUP; 454 return 0; 455 } 456 457 /* 458 * VFS Operations. 459 * 460 * mount system call 461 */ 462 int 463 ffs_mount(struct mount *mp, const char *path, void *data, size_t *data_len) 464 { 465 struct lwp *l = curlwp; 466 struct vnode *devvp = NULL; 467 struct ufs_args *args = data; 468 struct ufsmount *ump = NULL; 469 struct fs *fs; 470 int error = 0, flags, update; 471 mode_t accessmode; 472 473 if (args == NULL) { 474 DPRINTF("NULL args"); 475 return EINVAL; 476 } 477 if (*data_len < sizeof(*args)) { 478 DPRINTF("bad size args %zu != %zu", *data_len, sizeof(*args)); 479 return EINVAL; 480 } 481 482 ump = VFSTOUFS(mp); 483 if ((mp->mnt_flag & (MNT_GETARGS|MNT_UPDATE)) && ump == NULL) { 484 DPRINTF("no ump"); 485 return EIO; 486 } 487 488 if (mp->mnt_flag & MNT_GETARGS) { 489 args->fspec = NULL; 490 *data_len = sizeof *args; 491 return 0; 492 } 493 494 update = mp->mnt_flag & MNT_UPDATE; 495 496 /* Check arguments */ 497 if (args->fspec == NULL) { 498 if (!update) { 499 /* New mounts must have a filename for the device */ 500 DPRINTF("no filename for mount"); 501 return EINVAL; 502 } 503 } else { 504 /* 505 * Look up the name and verify that it's sane. 506 */ 507 error = namei_simple_user(args->fspec, 508 NSM_FOLLOW_NOEMULROOT, &devvp); 509 if (error != 0) { 510 DPRINTF("namei_simple_user returned %d", error); 511 return error; 512 } 513 514 /* 515 * Be sure this is a valid block device 516 */ 517 if (devvp->v_type != VBLK) { 518 DPRINTF("non block device %d", devvp->v_type); 519 error = ENOTBLK; 520 goto fail; 521 } 522 523 if (bdevsw_lookup(devvp->v_rdev) == NULL) { 524 DPRINTF("can't find block device 0x%jx", 525 devvp->v_rdev); 526 error = ENXIO; 527 goto fail; 528 } 529 530 if (update) { 531 /* 532 * Be sure we're still naming the same device 533 * used for our initial mount 534 */ 535 if (devvp != ump->um_devvp && 536 devvp->v_rdev != ump->um_devvp->v_rdev) { 537 DPRINTF("wrong device 0x%jx != 0x%jx", 538 (uintmax_t)devvp->v_rdev, 539 (uintmax_t)ump->um_devvp->v_rdev); 540 error = EINVAL; 541 goto fail; 542 } 543 vrele(devvp); 544 devvp = NULL; 545 } 546 } 547 548 if (devvp == NULL) { 549 devvp = ump->um_devvp; 550 vref(devvp); 551 } 552 553 /* 554 * If mount by non-root, then verify that user has necessary 555 * permissions on the device. 556 * 557 * Permission to update a mount is checked higher, so here we presume 558 * updating the mount is okay (for example, as far as securelevel goes) 559 * which leaves us with the normal check. 560 */ 561 accessmode = VREAD; 562 if (update ? (mp->mnt_iflag & IMNT_WANTRDWR) != 0 : 563 (mp->mnt_flag & MNT_RDONLY) == 0) 564 accessmode |= VWRITE; 565 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 566 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT, 567 KAUTH_REQ_SYSTEM_MOUNT_DEVICE, mp, devvp, KAUTH_ARG(accessmode)); 568 VOP_UNLOCK(devvp); 569 if (error) { 570 DPRINTF("kauth returned %d", error); 571 goto fail; 572 } 573 574 #ifdef WAPBL 575 /* WAPBL can only be enabled on a r/w mount. */ 576 if (((mp->mnt_flag & MNT_RDONLY) && !(mp->mnt_iflag & IMNT_WANTRDWR)) || 577 (mp->mnt_iflag & IMNT_WANTRDONLY)) { 578 mp->mnt_flag &= ~MNT_LOG; 579 } 580 #else /* !WAPBL */ 581 mp->mnt_flag &= ~MNT_LOG; 582 #endif /* !WAPBL */ 583 584 error = set_statvfs_info(path, UIO_USERSPACE, args->fspec, 585 UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l); 586 if (error) 587 goto fail; 588 589 if (!update) { 590 int xflags; 591 592 if (mp->mnt_flag & MNT_RDONLY) 593 xflags = FREAD; 594 else 595 xflags = FREAD | FWRITE; 596 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 597 error = VOP_OPEN(devvp, xflags, FSCRED); 598 VOP_UNLOCK(devvp); 599 if (error) { 600 DPRINTF("VOP_OPEN returned %d", error); 601 goto fail; 602 } 603 /* Need fstrans_start() for assertion in ufs_strategy(). */ 604 if ((mp->mnt_flag & MNT_RDONLY) == 0) 605 fstrans_start(mp); 606 error = ffs_mountfs(devvp, mp, l); 607 if ((mp->mnt_flag & MNT_RDONLY) == 0) 608 fstrans_done(mp); 609 if (error) { 610 DPRINTF("ffs_mountfs returned %d", error); 611 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 612 (void)VOP_CLOSE(devvp, xflags, NOCRED); 613 VOP_UNLOCK(devvp); 614 goto fail; 615 } 616 617 ump = VFSTOUFS(mp); 618 fs = ump->um_fs; 619 } else { 620 /* 621 * Update the mount. The file system is suspended. 622 */ 623 KASSERT(fstrans_is_owner(mp)); 624 625 /* 626 * The initial mount got a reference on this 627 * device, so drop the one obtained via 628 * namei(), above. 629 */ 630 vrele(devvp); 631 632 ump = VFSTOUFS(mp); 633 fs = ump->um_fs; 634 if (fs->fs_ronly == 0 && (mp->mnt_iflag & IMNT_WANTRDONLY)) { 635 /* 636 * Changing from r/w to r/o 637 */ 638 flags = WRITECLOSE; 639 if (mp->mnt_flag & MNT_FORCE) 640 flags |= FORCECLOSE; 641 error = ffs_flushfiles(mp, flags, l); 642 if (error) 643 return error; 644 645 error = UFS_WAPBL_BEGIN(mp); 646 if (error) { 647 DPRINTF("wapbl %d", error); 648 return error; 649 } 650 651 if (ffs_cgupdate(ump, MNT_WAIT) == 0 && 652 fs->fs_clean & FS_WASCLEAN) { 653 if (mp->mnt_flag & MNT_SOFTDEP) 654 fs->fs_flags &= ~FS_DOSOFTDEP; 655 fs->fs_clean = FS_ISCLEAN; 656 (void) ffs_sbupdate(ump, MNT_WAIT); 657 } 658 659 UFS_WAPBL_END(mp); 660 } 661 662 #ifdef WAPBL 663 if ((mp->mnt_flag & MNT_LOG) == 0) { 664 error = ffs_wapbl_stop(mp, mp->mnt_flag & MNT_FORCE); 665 if (error) { 666 DPRINTF("ffs_wapbl_stop returned %d", error); 667 return error; 668 } 669 } 670 #endif /* WAPBL */ 671 672 if (fs->fs_ronly == 0 && (mp->mnt_iflag & IMNT_WANTRDONLY)) { 673 /* 674 * Finish change from r/w to r/o 675 */ 676 fs->fs_ronly = 1; 677 fs->fs_fmod = 0; 678 } 679 680 error = ffs_acls(mp, fs->fs_flags); 681 if (error) 682 return error; 683 if (mp->mnt_flag & MNT_RELOAD) { 684 error = ffs_reload(mp, l->l_cred, l); 685 if (error) { 686 DPRINTF("ffs_reload returned %d", error); 687 return error; 688 } 689 } 690 691 if (fs->fs_ronly && (mp->mnt_iflag & IMNT_WANTRDWR)) { 692 /* 693 * Changing from read-only to read/write 694 */ 695 #ifndef QUOTA2 696 if (fs->fs_flags & FS_DOQUOTA2) { 697 ump->um_flags |= UFS_QUOTA2; 698 uprintf("%s: options QUOTA2 not enabled%s\n", 699 mp->mnt_stat.f_mntonname, 700 (mp->mnt_flag & MNT_FORCE) ? "" : 701 ", not mounting"); 702 DPRINTF("ffs_quota2 %d", EINVAL); 703 return EINVAL; 704 } 705 #endif 706 fs->fs_ronly = 0; 707 fs->fs_clean = 708 fs->fs_clean == FS_ISCLEAN ? FS_WASCLEAN : 0; 709 fs->fs_fmod = 1; 710 #ifdef WAPBL 711 if (fs->fs_flags & FS_DOWAPBL) { 712 const char *nm = mp->mnt_stat.f_mntonname; 713 if (!mp->mnt_wapbl_replay) { 714 printf("%s: log corrupted;" 715 " replay cancelled\n", nm); 716 return EFTYPE; 717 } 718 printf("%s: replaying log to disk\n", nm); 719 error = wapbl_replay_write(mp->mnt_wapbl_replay, 720 devvp); 721 if (error) { 722 DPRINTF("%s: wapbl_replay_write %d", 723 nm, error); 724 return error; 725 } 726 wapbl_replay_stop(mp->mnt_wapbl_replay); 727 fs->fs_clean = FS_WASCLEAN; 728 } 729 #endif /* WAPBL */ 730 if (fs->fs_snapinum[0] != 0) 731 ffs_snapshot_mount(mp); 732 } 733 734 #ifdef WAPBL 735 error = ffs_wapbl_start(mp); 736 if (error) { 737 DPRINTF("ffs_wapbl_start returned %d", error); 738 return error; 739 } 740 #endif /* WAPBL */ 741 742 #ifdef QUOTA2 743 if (!fs->fs_ronly) { 744 error = ffs_quota2_mount(mp); 745 if (error) { 746 DPRINTF("ffs_quota2_mount returned %d", error); 747 return error; 748 } 749 } 750 #endif 751 752 if ((mp->mnt_flag & MNT_DISCARD) && !(ump->um_discarddata)) 753 ump->um_discarddata = ffs_discard_init(devvp, fs); 754 755 if (args->fspec == NULL) 756 return 0; 757 } 758 759 (void)strncpy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, 760 sizeof(fs->fs_fsmnt)); 761 762 fs->fs_flags &= ~FS_DOSOFTDEP; 763 764 if ((fs->fs_ronly && (fs->fs_clean & FS_ISCLEAN) == 0) || 765 (!fs->fs_ronly && (fs->fs_clean & FS_WASCLEAN) == 0)) { 766 printf("%s: file system not clean (fs_clean=%#x); " 767 "please fsck(8)\n", mp->mnt_stat.f_mntfromname, 768 fs->fs_clean); 769 } 770 771 if (UFS_WAPBL_BEGIN(mp) == 0) { 772 mutex_enter(&ump->um_lock); 773 if (fs->fs_fmod != 0) { 774 KASSERT(!fs->fs_ronly); 775 776 if (fs->fs_clean & FS_WASCLEAN) 777 fs->fs_time = time_second; 778 fs->fs_fmod = 0; 779 mutex_exit(&ump->um_lock); 780 (void) ffs_cgupdate(ump, MNT_WAIT); 781 } else { 782 mutex_exit(&ump->um_lock); 783 } 784 UFS_WAPBL_END(mp); 785 } 786 if ((mp->mnt_flag & MNT_SOFTDEP) != 0) { 787 printf("%s: `-o softdep' is no longer supported, " 788 "consider `-o log'\n", mp->mnt_stat.f_mntfromname); 789 mp->mnt_flag &= ~MNT_SOFTDEP; 790 } 791 792 return (error); 793 794 fail: 795 vrele(devvp); 796 return (error); 797 } 798 799 /* 800 * Reload all incore data for a filesystem (used after running fsck on 801 * the root filesystem and finding things to fix). The filesystem must 802 * be mounted read-only. 803 * 804 * Things to do to update the mount: 805 * 1) invalidate all cached meta-data. 806 * 2) re-read superblock from disk. 807 * 3) re-read summary information from disk. 808 * 4) invalidate all inactive vnodes. 809 * 5) invalidate all cached file data. 810 * 6) re-read inode data for all active vnodes. 811 */ 812 int 813 ffs_reload(struct mount *mp, kauth_cred_t cred, struct lwp *l) 814 { 815 struct vnode *vp, *devvp; 816 struct inode *ip; 817 void *space; 818 struct buf *bp; 819 struct fs *fs, *newfs; 820 int i, bsize, blks, error; 821 int32_t *lp, fs_sbsize; 822 struct ufsmount *ump; 823 daddr_t sblockloc; 824 struct vnode_iterator *marker; 825 826 if ((mp->mnt_flag & MNT_RDONLY) == 0) 827 return (EINVAL); 828 829 ump = VFSTOUFS(mp); 830 831 /* 832 * Step 1: invalidate all cached meta-data. 833 */ 834 devvp = ump->um_devvp; 835 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 836 error = vinvalbuf(devvp, 0, cred, l, 0, 0); 837 VOP_UNLOCK(devvp); 838 if (error) 839 panic("%s: dirty1", __func__); 840 841 /* 842 * Step 2: re-read superblock from disk. XXX: We don't handle 843 * possibility that superblock moved. Which implies that we don't 844 * want its size to change either. 845 */ 846 fs = ump->um_fs; 847 fs_sbsize = fs->fs_sbsize; 848 error = bread(devvp, fs->fs_sblockloc / DEV_BSIZE, fs_sbsize, 849 0, &bp); 850 if (error) 851 return (error); 852 newfs = kmem_alloc(fs_sbsize, KM_SLEEP); 853 memcpy(newfs, bp->b_data, fs_sbsize); 854 855 #ifdef FFS_EI 856 if (ump->um_flags & UFS_NEEDSWAP) { 857 ffs_sb_swap((struct fs *)bp->b_data, newfs); 858 newfs->fs_flags |= FS_SWAPPED; 859 } else 860 #endif 861 newfs->fs_flags &= ~FS_SWAPPED; 862 863 brelse(bp, 0); 864 865 /* Allow converting from UFS2 to UFS2EA but not vice versa. */ 866 if (newfs->fs_magic == FS_UFS2EA_MAGIC) { 867 ump->um_flags |= UFS_EA; 868 newfs->fs_magic = FS_UFS2_MAGIC; 869 } else { 870 if ((ump->um_flags & UFS_EA) != 0) 871 return EINVAL; 872 } 873 874 if ((newfs->fs_magic != FS_UFS1_MAGIC) && 875 (newfs->fs_magic != FS_UFS2_MAGIC)) { 876 kmem_free(newfs, fs_sbsize); 877 return (EIO); /* XXX needs translation */ 878 } 879 if (!ffs_superblock_validate(newfs)) { 880 kmem_free(newfs, fs_sbsize); 881 return (EINVAL); 882 } 883 884 /* 885 * The current implementation doesn't handle the possibility that 886 * these values may have changed. 887 */ 888 if ((newfs->fs_sbsize != fs_sbsize) || 889 (newfs->fs_cssize != fs->fs_cssize) || 890 (newfs->fs_contigsumsize != fs->fs_contigsumsize) || 891 (newfs->fs_ncg != fs->fs_ncg)) { 892 kmem_free(newfs, fs_sbsize); 893 return (EINVAL); 894 } 895 896 /* Store off old fs_sblockloc for fs_oldfscompat_read. */ 897 sblockloc = fs->fs_sblockloc; 898 /* 899 * Copy pointer fields back into superblock before copying in XXX 900 * new superblock. These should really be in the ufsmount. XXX 901 * Note that important parameters (eg fs_ncg) are unchanged. 902 */ 903 newfs->fs_csp = fs->fs_csp; 904 newfs->fs_maxcluster = fs->fs_maxcluster; 905 newfs->fs_contigdirs = fs->fs_contigdirs; 906 newfs->fs_ronly = fs->fs_ronly; 907 newfs->fs_active = fs->fs_active; 908 memcpy(fs, newfs, (u_int)fs_sbsize); 909 kmem_free(newfs, fs_sbsize); 910 911 /* 912 * Recheck for Apple UFS filesystem. 913 */ 914 ump->um_flags &= ~UFS_ISAPPLEUFS; 915 if (ffs_is_appleufs(devvp, fs)) { 916 #ifdef APPLE_UFS 917 ump->um_flags |= UFS_ISAPPLEUFS; 918 #else 919 DPRINTF("AppleUFS not supported"); 920 return (EIO); /* XXX: really? */ 921 #endif 922 } 923 924 if (UFS_MPISAPPLEUFS(ump)) { 925 /* see comment about NeXT below */ 926 ump->um_maxsymlinklen = APPLEUFS_MAXSYMLINKLEN; 927 ump->um_dirblksiz = APPLEUFS_DIRBLKSIZ; 928 mp->mnt_iflag |= IMNT_DTYPE; 929 } else { 930 ump->um_maxsymlinklen = fs->fs_maxsymlinklen; 931 ump->um_dirblksiz = UFS_DIRBLKSIZ; 932 if (ump->um_maxsymlinklen > 0) 933 mp->mnt_iflag |= IMNT_DTYPE; 934 else 935 mp->mnt_iflag &= ~IMNT_DTYPE; 936 } 937 ffs_oldfscompat_read(fs, ump, sblockloc); 938 939 mutex_enter(&ump->um_lock); 940 ump->um_maxfilesize = fs->fs_maxfilesize; 941 if (fs->fs_flags & ~(FS_KNOWN_FLAGS | FS_INTERNAL)) { 942 uprintf("%s: unknown ufs flags: 0x%08"PRIx32"%s\n", 943 mp->mnt_stat.f_mntonname, fs->fs_flags, 944 (mp->mnt_flag & MNT_FORCE) ? "" : ", not mounting"); 945 if ((mp->mnt_flag & MNT_FORCE) == 0) { 946 mutex_exit(&ump->um_lock); 947 return (EINVAL); 948 } 949 } 950 951 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) { 952 fs->fs_pendingblocks = 0; 953 fs->fs_pendinginodes = 0; 954 } 955 mutex_exit(&ump->um_lock); 956 957 ffs_statvfs(mp, &mp->mnt_stat); 958 /* 959 * Step 3: re-read summary information from disk. 960 */ 961 blks = howmany(fs->fs_cssize, fs->fs_fsize); 962 space = fs->fs_csp; 963 for (i = 0; i < blks; i += fs->fs_frag) { 964 bsize = fs->fs_bsize; 965 if (i + fs->fs_frag > blks) 966 bsize = (blks - i) * fs->fs_fsize; 967 error = bread(devvp, FFS_FSBTODB(fs, fs->fs_csaddr + i), bsize, 968 0, &bp); 969 if (error) { 970 return (error); 971 } 972 #ifdef FFS_EI 973 if (UFS_FSNEEDSWAP(fs)) 974 ffs_csum_swap((struct csum *)bp->b_data, 975 (struct csum *)space, bsize); 976 else 977 #endif 978 memcpy(space, bp->b_data, (size_t)bsize); 979 space = (char *)space + bsize; 980 brelse(bp, 0); 981 } 982 /* 983 * We no longer know anything about clusters per cylinder group. 984 */ 985 if (fs->fs_contigsumsize > 0) { 986 lp = fs->fs_maxcluster; 987 for (i = 0; i < fs->fs_ncg; i++) 988 *lp++ = fs->fs_contigsumsize; 989 } 990 991 vfs_vnode_iterator_init(mp, &marker); 992 while ((vp = vfs_vnode_iterator_next(marker, NULL, NULL))) { 993 /* 994 * Step 4: invalidate all inactive vnodes. 995 */ 996 if (vrecycle(vp)) 997 continue; 998 /* 999 * Step 5: invalidate all cached file data. 1000 */ 1001 if (vn_lock(vp, LK_EXCLUSIVE)) { 1002 vrele(vp); 1003 continue; 1004 } 1005 if (vinvalbuf(vp, 0, cred, l, 0, 0)) 1006 panic("%s: dirty2", __func__); 1007 /* 1008 * Step 6: re-read inode data for all active vnodes. 1009 */ 1010 ip = VTOI(vp); 1011 error = bread(devvp, FFS_FSBTODB(fs, ino_to_fsba(fs, ip->i_number)), 1012 (int)fs->fs_bsize, 0, &bp); 1013 if (error) { 1014 vput(vp); 1015 break; 1016 } 1017 ffs_load_inode(bp, ip, fs, ip->i_number); 1018 brelse(bp, 0); 1019 vput(vp); 1020 } 1021 vfs_vnode_iterator_destroy(marker); 1022 return (error); 1023 } 1024 1025 /* 1026 * Possible superblock locations ordered from most to least likely. 1027 */ 1028 static const int sblock_try[] = SBLOCKSEARCH; 1029 1030 1031 static int 1032 ffs_superblock_validate(struct fs *fs) 1033 { 1034 int32_t i, fs_bshift = 0, fs_fshift = 0, fs_fragshift = 0, fs_frag; 1035 int32_t fs_inopb; 1036 1037 /* Check the superblock size */ 1038 if (fs->fs_sbsize > SBLOCKSIZE || fs->fs_sbsize < sizeof(struct fs)) 1039 return 0; 1040 1041 /* Check the file system blocksize */ 1042 if (fs->fs_bsize > MAXBSIZE || fs->fs_bsize < MINBSIZE) 1043 return 0; 1044 if (!powerof2(fs->fs_bsize)) 1045 return 0; 1046 1047 /* Check the size of frag blocks */ 1048 if (!powerof2(fs->fs_fsize)) 1049 return 0; 1050 if (fs->fs_fsize == 0) 1051 return 0; 1052 1053 /* 1054 * XXX: these values are just zero-checked to prevent obvious 1055 * bugs. We need more strict checks. 1056 */ 1057 if (fs->fs_size == 0 && fs->fs_old_size == 0) 1058 return 0; 1059 if (fs->fs_cssize == 0) 1060 return 0; 1061 if (fs->fs_ipg == 0) 1062 return 0; 1063 if (fs->fs_fpg == 0) 1064 return 0; 1065 if (fs->fs_ncg == 0) 1066 return 0; 1067 if (fs->fs_maxbpg == 0) 1068 return 0; 1069 1070 /* Check the number of inodes per block */ 1071 if (fs->fs_magic == FS_UFS1_MAGIC) 1072 fs_inopb = fs->fs_bsize / sizeof(struct ufs1_dinode); 1073 else /* fs->fs_magic == FS_UFS2_MAGIC */ 1074 fs_inopb = fs->fs_bsize / sizeof(struct ufs2_dinode); 1075 if (fs->fs_inopb != fs_inopb) 1076 return 0; 1077 1078 /* Block size cannot be smaller than fragment size */ 1079 if (fs->fs_bsize < fs->fs_fsize) 1080 return 0; 1081 1082 /* Compute fs_bshift and ensure it is consistent */ 1083 for (i = fs->fs_bsize; i > 1; i >>= 1) 1084 fs_bshift++; 1085 if (fs->fs_bshift != fs_bshift) 1086 return 0; 1087 1088 /* Compute fs_fshift and ensure it is consistent */ 1089 for (i = fs->fs_fsize; i > 1; i >>= 1) 1090 fs_fshift++; 1091 if (fs->fs_fshift != fs_fshift) 1092 return 0; 1093 1094 /* Compute fs_fragshift and ensure it is consistent */ 1095 for (i = fs->fs_frag; i > 1; i >>= 1) 1096 fs_fragshift++; 1097 if (fs->fs_fragshift != fs_fragshift) 1098 return 0; 1099 1100 /* Check the masks */ 1101 if (fs->fs_bmask != ~(fs->fs_bsize - 1)) 1102 return 0; 1103 if (fs->fs_fmask != ~(fs->fs_fsize - 1)) 1104 return 0; 1105 1106 /* 1107 * Now that the shifts and masks are sanitized, we can use the ffs_ API. 1108 */ 1109 1110 /* Check the number of frag blocks */ 1111 if ((fs_frag = ffs_numfrags(fs, fs->fs_bsize)) > MAXFRAG) 1112 return 0; 1113 if (fs->fs_frag != fs_frag) 1114 return 0; 1115 1116 /* Check the size of cylinder groups */ 1117 if ((fs->fs_cgsize < sizeof(struct cg)) || 1118 (fs->fs_cgsize > fs->fs_bsize)) 1119 return 0; 1120 1121 return 1; 1122 } 1123 1124 static int 1125 ffs_is_appleufs(struct vnode *devvp, struct fs *fs) 1126 { 1127 struct dkwedge_info dkw; 1128 int ret = 0; 1129 1130 /* 1131 * First check to see if this is tagged as an Apple UFS filesystem 1132 * in the disklabel. 1133 */ 1134 if (getdiskinfo(devvp, &dkw) == 0 && 1135 strcmp(dkw.dkw_ptype, DKW_PTYPE_APPLEUFS) == 0) 1136 ret = 1; 1137 #ifdef APPLE_UFS 1138 else { 1139 struct appleufslabel *applefs; 1140 struct buf *bp; 1141 daddr_t blkno = APPLEUFS_LABEL_OFFSET / DEV_BSIZE; 1142 int error; 1143 1144 /* 1145 * Manually look for an Apple UFS label, and if a valid one 1146 * is found, then treat it like an Apple UFS filesystem anyway. 1147 */ 1148 error = bread(devvp, blkno, APPLEUFS_LABEL_SIZE, 0, &bp); 1149 if (error) { 1150 DPRINTF("bread@0x%jx returned %d", (intmax_t)blkno, error); 1151 return 0; 1152 } 1153 applefs = (struct appleufslabel *)bp->b_data; 1154 error = ffs_appleufs_validate(fs->fs_fsmnt, applefs, NULL); 1155 if (error == 0) 1156 ret = 1; 1157 brelse(bp, 0); 1158 } 1159 #endif 1160 1161 return ret; 1162 } 1163 1164 /* 1165 * Common code for mount and mountroot 1166 */ 1167 int 1168 ffs_mountfs(struct vnode *devvp, struct mount *mp, struct lwp *l) 1169 { 1170 struct ufsmount *ump = NULL; 1171 struct buf *bp = NULL; 1172 struct fs *fs = NULL; 1173 dev_t dev; 1174 void *space; 1175 daddr_t sblockloc = 0; 1176 int blks, fstype = 0; 1177 int error, i, bsize, ronly, bset = 0; 1178 #ifdef FFS_EI 1179 int needswap = 0; /* keep gcc happy */ 1180 #endif 1181 int32_t *lp; 1182 kauth_cred_t cred; 1183 u_int32_t allocsbsize, fs_sbsize = 0; 1184 1185 dev = devvp->v_rdev; 1186 cred = l ? l->l_cred : NOCRED; 1187 1188 /* Flush out any old buffers remaining from a previous use. */ 1189 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY); 1190 error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0); 1191 VOP_UNLOCK(devvp); 1192 if (error) { 1193 DPRINTF("vinvalbuf returned %d", error); 1194 return error; 1195 } 1196 1197 ronly = (mp->mnt_flag & MNT_RDONLY) != 0; 1198 1199 ump = kmem_zalloc(sizeof(*ump), KM_SLEEP); 1200 mutex_init(&ump->um_lock, MUTEX_DEFAULT, IPL_NONE); 1201 error = ffs_snapshot_init(ump); 1202 if (error) { 1203 DPRINTF("ffs_snapshot_init returned %d", error); 1204 goto out; 1205 } 1206 ump->um_ops = &ffs_ufsops; 1207 1208 #ifdef WAPBL 1209 sbagain: 1210 #endif 1211 /* 1212 * Try reading the superblock in each of its possible locations. 1213 */ 1214 for (i = 0; ; i++) { 1215 daddr_t fs_sblockloc; 1216 1217 if (bp != NULL) { 1218 brelse(bp, BC_NOCACHE); 1219 bp = NULL; 1220 } 1221 if (sblock_try[i] == -1) { 1222 DPRINTF("no superblock found"); 1223 error = EINVAL; 1224 fs = NULL; 1225 goto out; 1226 } 1227 1228 error = bread(devvp, sblock_try[i] / DEV_BSIZE, SBLOCKSIZE, 1229 0, &bp); 1230 if (error) { 1231 DPRINTF("bread@0x%x returned %d", 1232 sblock_try[i] / DEV_BSIZE, error); 1233 fs = NULL; 1234 goto out; 1235 } 1236 fs = (struct fs *)bp->b_data; 1237 1238 sblockloc = sblock_try[i]; 1239 DPRINTF("fs_magic 0x%x", fs->fs_magic); 1240 1241 /* 1242 * Swap: here, we swap fs->fs_sbsize in order to get the correct 1243 * size to read the superblock. Once read, we swap the whole 1244 * superblock structure. 1245 */ 1246 if (fs->fs_magic == FS_UFS2EA_MAGIC) { 1247 ump->um_flags |= UFS_EA; 1248 fs->fs_magic = FS_UFS2_MAGIC; 1249 } else if (fs->fs_magic == FS_UFS2EA_MAGIC_SWAPPED) { 1250 ump->um_flags |= UFS_EA; 1251 fs->fs_magic = FS_UFS2_MAGIC_SWAPPED; 1252 } 1253 if (fs->fs_magic == FS_UFS1_MAGIC) { 1254 fs_sbsize = fs->fs_sbsize; 1255 fstype = UFS1; 1256 #ifdef FFS_EI 1257 needswap = 0; 1258 } else if (fs->fs_magic == FS_UFS1_MAGIC_SWAPPED) { 1259 fs_sbsize = bswap32(fs->fs_sbsize); 1260 fstype = UFS1; 1261 needswap = 1; 1262 #endif 1263 } else if (fs->fs_magic == FS_UFS2_MAGIC) { 1264 fs_sbsize = fs->fs_sbsize; 1265 fstype = UFS2; 1266 #ifdef FFS_EI 1267 needswap = 0; 1268 } else if (fs->fs_magic == FS_UFS2_MAGIC_SWAPPED) { 1269 fs_sbsize = bswap32(fs->fs_sbsize); 1270 fstype = UFS2; 1271 needswap = 1; 1272 #endif 1273 } else 1274 continue; 1275 1276 /* fs->fs_sblockloc isn't defined for old filesystems */ 1277 if (fstype == UFS1 && !(fs->fs_old_flags & FS_FLAGS_UPDATED)) { 1278 if (sblockloc == SBLOCK_UFS2) 1279 /* 1280 * This is likely to be the first alternate 1281 * in a filesystem with 64k blocks. 1282 * Don't use it. 1283 */ 1284 continue; 1285 fs_sblockloc = sblockloc; 1286 } else { 1287 fs_sblockloc = fs->fs_sblockloc; 1288 #ifdef FFS_EI 1289 if (needswap) 1290 fs_sblockloc = bswap64(fs_sblockloc); 1291 #endif 1292 } 1293 1294 /* Check we haven't found an alternate superblock */ 1295 if (fs_sblockloc != sblockloc) 1296 continue; 1297 1298 /* Check the superblock size */ 1299 if (fs_sbsize > SBLOCKSIZE || fs_sbsize < sizeof(struct fs)) 1300 continue; 1301 fs = kmem_alloc((u_long)fs_sbsize, KM_SLEEP); 1302 memcpy(fs, bp->b_data, fs_sbsize); 1303 1304 /* Swap the whole superblock structure, if necessary. */ 1305 #ifdef FFS_EI 1306 if (needswap) { 1307 ffs_sb_swap((struct fs*)bp->b_data, fs); 1308 fs->fs_flags |= FS_SWAPPED; 1309 } else 1310 #endif 1311 fs->fs_flags &= ~FS_SWAPPED; 1312 1313 /* 1314 * Now that everything is swapped, the superblock is ready to 1315 * be sanitized. 1316 */ 1317 if (!ffs_superblock_validate(fs)) { 1318 kmem_free(fs, fs_sbsize); 1319 continue; 1320 } 1321 1322 /* Ok seems to be a good superblock */ 1323 break; 1324 } 1325 1326 ump->um_fs = fs; 1327 1328 #ifdef WAPBL 1329 if ((mp->mnt_wapbl_replay == 0) && (fs->fs_flags & FS_DOWAPBL)) { 1330 error = ffs_wapbl_replay_start(mp, fs, devvp); 1331 if (error && (mp->mnt_flag & MNT_FORCE) == 0) { 1332 DPRINTF("ffs_wapbl_replay_start returned %d", error); 1333 goto out; 1334 } 1335 if (!error) { 1336 if (!ronly) { 1337 /* XXX fsmnt may be stale. */ 1338 printf("%s: replaying log to disk\n", 1339 fs->fs_fsmnt); 1340 error = wapbl_replay_write(mp->mnt_wapbl_replay, 1341 devvp); 1342 if (error) { 1343 DPRINTF("wapbl_replay_write returned %d", 1344 error); 1345 goto out; 1346 } 1347 wapbl_replay_stop(mp->mnt_wapbl_replay); 1348 fs->fs_clean = FS_WASCLEAN; 1349 } else { 1350 /* XXX fsmnt may be stale */ 1351 printf("%s: replaying log to memory\n", 1352 fs->fs_fsmnt); 1353 } 1354 1355 /* Force a re-read of the superblock */ 1356 brelse(bp, BC_INVAL); 1357 bp = NULL; 1358 kmem_free(fs, fs_sbsize); 1359 fs = NULL; 1360 goto sbagain; 1361 } 1362 } 1363 #else /* !WAPBL */ 1364 if ((fs->fs_flags & FS_DOWAPBL) && (mp->mnt_flag & MNT_FORCE) == 0) { 1365 error = EPERM; 1366 DPRINTF("no force %d", error); 1367 goto out; 1368 } 1369 #endif /* !WAPBL */ 1370 1371 ffs_oldfscompat_read(fs, ump, sblockloc); 1372 ump->um_maxfilesize = fs->fs_maxfilesize; 1373 1374 if (fs->fs_flags & ~(FS_KNOWN_FLAGS | FS_INTERNAL)) { 1375 uprintf("%s: unknown ufs flags: 0x%08"PRIx32"%s\n", 1376 mp->mnt_stat.f_mntonname, fs->fs_flags, 1377 (mp->mnt_flag & MNT_FORCE) ? "" : ", not mounting"); 1378 if ((mp->mnt_flag & MNT_FORCE) == 0) { 1379 error = EINVAL; 1380 DPRINTF("no force %d", error); 1381 goto out; 1382 } 1383 } 1384 1385 fs->fs_fmod = 0; 1386 if (fs->fs_pendingblocks != 0 || fs->fs_pendinginodes != 0) { 1387 fs->fs_pendingblocks = 0; 1388 fs->fs_pendinginodes = 0; 1389 } 1390 1391 ump->um_fstype = fstype; 1392 if (fs->fs_sbsize < SBLOCKSIZE) 1393 brelse(bp, BC_INVAL); 1394 else 1395 brelse(bp, 0); 1396 bp = NULL; 1397 1398 if (ffs_is_appleufs(devvp, fs)) { 1399 #ifdef APPLE_UFS 1400 ump->um_flags |= UFS_ISAPPLEUFS; 1401 #else 1402 DPRINTF("AppleUFS not supported"); 1403 error = EINVAL; 1404 goto out; 1405 #endif 1406 } 1407 1408 #if 0 1409 /* 1410 * XXX This code changes the behaviour of mounting dirty filesystems, to 1411 * XXX require "mount -f ..." to mount them. This doesn't match what 1412 * XXX mount(8) describes and is disabled for now. 1413 */ 1414 /* 1415 * If the file system is not clean, don't allow it to be mounted 1416 * unless MNT_FORCE is specified. (Note: MNT_FORCE is always set 1417 * for the root file system.) 1418 */ 1419 if (fs->fs_flags & FS_DOWAPBL) { 1420 /* 1421 * wapbl normally expects to be FS_WASCLEAN when the FS_DOWAPBL 1422 * bit is set, although there's a window in unmount where it 1423 * could be FS_ISCLEAN 1424 */ 1425 if ((mp->mnt_flag & MNT_FORCE) == 0 && 1426 (fs->fs_clean & (FS_WASCLEAN | FS_ISCLEAN)) == 0) { 1427 error = EPERM; 1428 goto out; 1429 } 1430 } else 1431 if ((fs->fs_clean & FS_ISCLEAN) == 0 && 1432 (mp->mnt_flag & MNT_FORCE) == 0) { 1433 error = EPERM; 1434 goto out; 1435 } 1436 #endif 1437 1438 /* 1439 * Verify that we can access the last block in the fs 1440 * if we're mounting read/write. 1441 */ 1442 if (!ronly) { 1443 error = bread(devvp, FFS_FSBTODB(fs, fs->fs_size - 1), 1444 fs->fs_fsize, 0, &bp); 1445 if (error) { 1446 DPRINTF("bread@0x%jx returned %d", 1447 (intmax_t)FFS_FSBTODB(fs, fs->fs_size - 1), 1448 error); 1449 bset = BC_INVAL; 1450 goto out; 1451 } 1452 if (bp->b_bcount != fs->fs_fsize) { 1453 DPRINTF("bcount %x != fsize %x", bp->b_bcount, 1454 fs->fs_fsize); 1455 error = EINVAL; 1456 bset = BC_INVAL; 1457 goto out; 1458 } 1459 brelse(bp, BC_INVAL); 1460 bp = NULL; 1461 } 1462 1463 fs->fs_ronly = ronly; 1464 /* Don't bump fs_clean if we're replaying journal */ 1465 if (!((fs->fs_flags & FS_DOWAPBL) && (fs->fs_clean & FS_WASCLEAN))) { 1466 if (ronly == 0) { 1467 fs->fs_clean = 1468 fs->fs_clean == FS_ISCLEAN ? FS_WASCLEAN : 0; 1469 fs->fs_fmod = 1; 1470 } 1471 } 1472 1473 bsize = fs->fs_cssize; 1474 blks = howmany(bsize, fs->fs_fsize); 1475 if (fs->fs_contigsumsize > 0) 1476 bsize += fs->fs_ncg * sizeof(int32_t); 1477 bsize += fs->fs_ncg * sizeof(*fs->fs_contigdirs); 1478 allocsbsize = bsize; 1479 space = kmem_alloc((u_long)allocsbsize, KM_SLEEP); 1480 fs->fs_csp = space; 1481 1482 for (i = 0; i < blks; i += fs->fs_frag) { 1483 bsize = fs->fs_bsize; 1484 if (i + fs->fs_frag > blks) 1485 bsize = (blks - i) * fs->fs_fsize; 1486 error = bread(devvp, FFS_FSBTODB(fs, fs->fs_csaddr + i), bsize, 1487 0, &bp); 1488 if (error) { 1489 DPRINTF("bread@0x%jx %d", 1490 (intmax_t)FFS_FSBTODB(fs, fs->fs_csaddr + i), 1491 error); 1492 goto out1; 1493 } 1494 #ifdef FFS_EI 1495 if (needswap) 1496 ffs_csum_swap((struct csum *)bp->b_data, 1497 (struct csum *)space, bsize); 1498 else 1499 #endif 1500 memcpy(space, bp->b_data, (u_int)bsize); 1501 1502 space = (char *)space + bsize; 1503 brelse(bp, 0); 1504 bp = NULL; 1505 } 1506 if (fs->fs_contigsumsize > 0) { 1507 fs->fs_maxcluster = lp = space; 1508 for (i = 0; i < fs->fs_ncg; i++) 1509 *lp++ = fs->fs_contigsumsize; 1510 space = lp; 1511 } 1512 bsize = fs->fs_ncg * sizeof(*fs->fs_contigdirs); 1513 fs->fs_contigdirs = space; 1514 space = (char *)space + bsize; 1515 memset(fs->fs_contigdirs, 0, bsize); 1516 1517 /* Compatibility for old filesystems - XXX */ 1518 if (fs->fs_avgfilesize <= 0) 1519 fs->fs_avgfilesize = AVFILESIZ; 1520 if (fs->fs_avgfpdir <= 0) 1521 fs->fs_avgfpdir = AFPDIR; 1522 fs->fs_active = NULL; 1523 1524 mp->mnt_data = ump; 1525 mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev; 1526 mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_FFS); 1527 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0]; 1528 mp->mnt_stat.f_namemax = FFS_MAXNAMLEN; 1529 if (UFS_MPISAPPLEUFS(ump)) { 1530 /* NeXT used to keep short symlinks in the inode even 1531 * when using FS_42INODEFMT. In that case fs->fs_maxsymlinklen 1532 * is probably -1, but we still need to be able to identify 1533 * short symlinks. 1534 */ 1535 ump->um_maxsymlinklen = APPLEUFS_MAXSYMLINKLEN; 1536 ump->um_dirblksiz = APPLEUFS_DIRBLKSIZ; 1537 mp->mnt_iflag |= IMNT_DTYPE; 1538 } else { 1539 ump->um_maxsymlinklen = fs->fs_maxsymlinklen; 1540 ump->um_dirblksiz = UFS_DIRBLKSIZ; 1541 if (ump->um_maxsymlinklen > 0) 1542 mp->mnt_iflag |= IMNT_DTYPE; 1543 else 1544 mp->mnt_iflag &= ~IMNT_DTYPE; 1545 } 1546 mp->mnt_fs_bshift = fs->fs_bshift; 1547 mp->mnt_dev_bshift = DEV_BSHIFT; /* XXX */ 1548 mp->mnt_flag |= MNT_LOCAL; 1549 mp->mnt_iflag |= IMNT_MPSAFE | IMNT_CAN_RWTORO | IMNT_SHRLOOKUP | 1550 IMNT_NCLOOKUP; 1551 #ifdef FFS_EI 1552 if (needswap) 1553 ump->um_flags |= UFS_NEEDSWAP; 1554 #endif 1555 error = ffs_acls(mp, fs->fs_flags); 1556 if (error) 1557 goto out1; 1558 ump->um_mountp = mp; 1559 ump->um_dev = dev; 1560 ump->um_devvp = devvp; 1561 ump->um_nindir = fs->fs_nindir; 1562 ump->um_lognindir = ffs(fs->fs_nindir) - 1; 1563 ump->um_bptrtodb = fs->fs_fshift - DEV_BSHIFT; 1564 ump->um_seqinc = fs->fs_frag; 1565 for (i = 0; i < MAXQUOTAS; i++) 1566 ump->um_quotas[i] = NULLVP; 1567 spec_node_setmountedfs(devvp, mp); 1568 if (ronly == 0 && fs->fs_snapinum[0] != 0) 1569 ffs_snapshot_mount(mp); 1570 #ifdef WAPBL 1571 if (!ronly) { 1572 KDASSERT(fs->fs_ronly == 0); 1573 /* 1574 * ffs_wapbl_start() needs mp->mnt_stat initialised if it 1575 * needs to create a new log file in-filesystem. 1576 */ 1577 error = ffs_statvfs(mp, &mp->mnt_stat); 1578 if (error) { 1579 DPRINTF("ffs_statvfs returned %d", error); 1580 goto out1; 1581 } 1582 1583 error = ffs_wapbl_start(mp); 1584 if (error) { 1585 DPRINTF("ffs_wapbl_start returned %d", error); 1586 goto out1; 1587 } 1588 } 1589 #endif /* WAPBL */ 1590 if (ronly == 0) { 1591 #ifdef QUOTA2 1592 error = ffs_quota2_mount(mp); 1593 if (error) { 1594 DPRINTF("ffs_quota2_mount returned %d", error); 1595 goto out1; 1596 } 1597 #else 1598 if (fs->fs_flags & FS_DOQUOTA2) { 1599 ump->um_flags |= UFS_QUOTA2; 1600 uprintf("%s: options QUOTA2 not enabled%s\n", 1601 mp->mnt_stat.f_mntonname, 1602 (mp->mnt_flag & MNT_FORCE) ? "" : ", not mounting"); 1603 if ((mp->mnt_flag & MNT_FORCE) == 0) { 1604 error = EINVAL; 1605 DPRINTF("quota disabled %d", error); 1606 goto out1; 1607 } 1608 } 1609 #endif 1610 } 1611 1612 if (mp->mnt_flag & MNT_DISCARD) 1613 ump->um_discarddata = ffs_discard_init(devvp, fs); 1614 1615 return (0); 1616 out1: 1617 kmem_free(fs->fs_csp, allocsbsize); 1618 out: 1619 #ifdef WAPBL 1620 if (mp->mnt_wapbl_replay) { 1621 wapbl_replay_stop(mp->mnt_wapbl_replay); 1622 wapbl_replay_free(mp->mnt_wapbl_replay); 1623 mp->mnt_wapbl_replay = 0; 1624 } 1625 #endif 1626 1627 if (fs) 1628 kmem_free(fs, fs->fs_sbsize); 1629 spec_node_setmountedfs(devvp, NULL); 1630 if (bp) 1631 brelse(bp, bset); 1632 if (ump) { 1633 if (ump->um_oldfscompat) 1634 kmem_free(ump->um_oldfscompat, 512 + 3*sizeof(int32_t)); 1635 mutex_destroy(&ump->um_lock); 1636 kmem_free(ump, sizeof(*ump)); 1637 mp->mnt_data = NULL; 1638 } 1639 return (error); 1640 } 1641 1642 /* 1643 * Sanity checks for loading old filesystem superblocks. 1644 * See ffs_oldfscompat_write below for unwound actions. 1645 * 1646 * XXX - Parts get retired eventually. 1647 * Unfortunately new bits get added. 1648 */ 1649 static void 1650 ffs_oldfscompat_read(struct fs *fs, struct ufsmount *ump, daddr_t sblockloc) 1651 { 1652 off_t maxfilesize; 1653 int32_t *extrasave; 1654 1655 if ((fs->fs_magic != FS_UFS1_MAGIC) || 1656 (fs->fs_old_flags & FS_FLAGS_UPDATED)) 1657 return; 1658 1659 if (!ump->um_oldfscompat) 1660 ump->um_oldfscompat = kmem_alloc(512 + 3*sizeof(int32_t), 1661 KM_SLEEP); 1662 1663 memcpy(ump->um_oldfscompat, &fs->fs_old_postbl_start, 512); 1664 extrasave = ump->um_oldfscompat; 1665 extrasave += 512/sizeof(int32_t); 1666 extrasave[0] = fs->fs_old_npsect; 1667 extrasave[1] = fs->fs_old_interleave; 1668 extrasave[2] = fs->fs_old_trackskew; 1669 1670 /* These fields will be overwritten by their 1671 * original values in fs_oldfscompat_write, so it is harmless 1672 * to modify them here. 1673 */ 1674 fs->fs_cstotal.cs_ndir = fs->fs_old_cstotal.cs_ndir; 1675 fs->fs_cstotal.cs_nbfree = fs->fs_old_cstotal.cs_nbfree; 1676 fs->fs_cstotal.cs_nifree = fs->fs_old_cstotal.cs_nifree; 1677 fs->fs_cstotal.cs_nffree = fs->fs_old_cstotal.cs_nffree; 1678 1679 fs->fs_maxbsize = fs->fs_bsize; 1680 fs->fs_time = fs->fs_old_time; 1681 fs->fs_size = fs->fs_old_size; 1682 fs->fs_dsize = fs->fs_old_dsize; 1683 fs->fs_csaddr = fs->fs_old_csaddr; 1684 fs->fs_sblockloc = sblockloc; 1685 1686 fs->fs_flags = fs->fs_old_flags | (fs->fs_flags & FS_INTERNAL); 1687 1688 if (fs->fs_old_postblformat == FS_42POSTBLFMT) { 1689 fs->fs_old_nrpos = 8; 1690 fs->fs_old_npsect = fs->fs_old_nsect; 1691 fs->fs_old_interleave = 1; 1692 fs->fs_old_trackskew = 0; 1693 } 1694 1695 if (fs->fs_magic == FS_UFS1_MAGIC && 1696 fs->fs_old_inodefmt < FS_44INODEFMT) { 1697 fs->fs_maxfilesize = (u_quad_t) 1LL << 39; 1698 fs->fs_qbmask = ~fs->fs_bmask; 1699 fs->fs_qfmask = ~fs->fs_fmask; 1700 } 1701 1702 maxfilesize = (u_int64_t)0x80000000 * fs->fs_bsize - 1; 1703 if (fs->fs_maxfilesize > maxfilesize) 1704 fs->fs_maxfilesize = maxfilesize; 1705 1706 /* Compatibility for old filesystems */ 1707 if (fs->fs_avgfilesize <= 0) 1708 fs->fs_avgfilesize = AVFILESIZ; 1709 if (fs->fs_avgfpdir <= 0) 1710 fs->fs_avgfpdir = AFPDIR; 1711 1712 #if 0 1713 if (bigcgs) { 1714 fs->fs_save_cgsize = fs->fs_cgsize; 1715 fs->fs_cgsize = fs->fs_bsize; 1716 } 1717 #endif 1718 } 1719 1720 /* 1721 * Unwinding superblock updates for old filesystems. 1722 * See ffs_oldfscompat_read above for details. 1723 * 1724 * XXX - Parts get retired eventually. 1725 * Unfortunately new bits get added. 1726 */ 1727 static void 1728 ffs_oldfscompat_write(struct fs *fs, struct ufsmount *ump) 1729 { 1730 int32_t *extrasave; 1731 1732 if ((fs->fs_magic != FS_UFS1_MAGIC) || 1733 (fs->fs_old_flags & FS_FLAGS_UPDATED)) 1734 return; 1735 1736 fs->fs_old_time = fs->fs_time; 1737 fs->fs_old_cstotal.cs_ndir = fs->fs_cstotal.cs_ndir; 1738 fs->fs_old_cstotal.cs_nbfree = fs->fs_cstotal.cs_nbfree; 1739 fs->fs_old_cstotal.cs_nifree = fs->fs_cstotal.cs_nifree; 1740 fs->fs_old_cstotal.cs_nffree = fs->fs_cstotal.cs_nffree; 1741 fs->fs_old_flags = fs->fs_flags; 1742 1743 #if 0 1744 if (bigcgs) { 1745 fs->fs_cgsize = fs->fs_save_cgsize; 1746 } 1747 #endif 1748 1749 memcpy(&fs->fs_old_postbl_start, ump->um_oldfscompat, 512); 1750 extrasave = ump->um_oldfscompat; 1751 extrasave += 512/sizeof(int32_t); 1752 fs->fs_old_npsect = extrasave[0]; 1753 fs->fs_old_interleave = extrasave[1]; 1754 fs->fs_old_trackskew = extrasave[2]; 1755 1756 } 1757 1758 /* 1759 * unmount vfs operation 1760 */ 1761 int 1762 ffs_unmount(struct mount *mp, int mntflags) 1763 { 1764 struct lwp *l = curlwp; 1765 struct ufsmount *ump = VFSTOUFS(mp); 1766 struct fs *fs = ump->um_fs; 1767 int error, flags; 1768 u_int32_t bsize; 1769 #ifdef WAPBL 1770 extern int doforce; 1771 #endif 1772 1773 /* The file system is suspended. */ 1774 KASSERT(fstrans_is_owner(mp)); 1775 1776 if (ump->um_discarddata) { 1777 ffs_discard_finish(ump->um_discarddata, mntflags); 1778 ump->um_discarddata = NULL; 1779 } 1780 1781 flags = 0; 1782 if (mntflags & MNT_FORCE) 1783 flags |= FORCECLOSE; 1784 if ((error = ffs_flushfiles(mp, flags, l)) != 0) 1785 return (error); 1786 if (fs->fs_ronly == 0 && UFS_WAPBL_BEGIN(mp) == 0) { 1787 if (ffs_cgupdate(ump, MNT_WAIT) == 0 && 1788 fs->fs_clean & FS_WASCLEAN) { 1789 mutex_enter(&ump->um_lock); 1790 fs->fs_clean = FS_ISCLEAN; 1791 fs->fs_fmod = 0; 1792 mutex_exit(&ump->um_lock); 1793 (void) ffs_sbupdate(ump, MNT_WAIT); 1794 } 1795 UFS_WAPBL_END(mp); 1796 } 1797 #ifdef WAPBL 1798 KASSERT(!(mp->mnt_wapbl_replay && mp->mnt_wapbl)); 1799 if (mp->mnt_wapbl_replay) { 1800 KDASSERT(fs->fs_ronly); 1801 wapbl_replay_stop(mp->mnt_wapbl_replay); 1802 wapbl_replay_free(mp->mnt_wapbl_replay); 1803 mp->mnt_wapbl_replay = 0; 1804 } 1805 error = ffs_wapbl_stop(mp, doforce && (mntflags & MNT_FORCE)); 1806 if (error) { 1807 return error; 1808 } 1809 #endif /* WAPBL */ 1810 1811 if (ump->um_devvp->v_type != VBAD) 1812 spec_node_setmountedfs(ump->um_devvp, NULL); 1813 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY); 1814 (void)VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD | FWRITE, 1815 NOCRED); 1816 vput(ump->um_devvp); 1817 1818 bsize = fs->fs_cssize; 1819 if (fs->fs_contigsumsize > 0) 1820 bsize += fs->fs_ncg * sizeof(int32_t); 1821 bsize += fs->fs_ncg * sizeof(*fs->fs_contigdirs); 1822 kmem_free(fs->fs_csp, bsize); 1823 1824 kmem_free(fs, fs->fs_sbsize); 1825 if (ump->um_oldfscompat != NULL) 1826 kmem_free(ump->um_oldfscompat, 512 + 3*sizeof(int32_t)); 1827 mutex_destroy(&ump->um_lock); 1828 ffs_snapshot_fini(ump); 1829 kmem_free(ump, sizeof(*ump)); 1830 mp->mnt_data = NULL; 1831 mp->mnt_flag &= ~MNT_LOCAL; 1832 return (0); 1833 } 1834 1835 /* 1836 * Flush out all the files in a filesystem. 1837 */ 1838 int 1839 ffs_flushfiles(struct mount *mp, int flags, struct lwp *l) 1840 { 1841 extern int doforce; 1842 struct ufsmount *ump; 1843 int error; 1844 1845 if (!doforce) 1846 flags &= ~FORCECLOSE; 1847 ump = VFSTOUFS(mp); 1848 #ifdef QUOTA 1849 if ((error = quota1_umount(mp, flags)) != 0) 1850 return (error); 1851 #endif 1852 #ifdef QUOTA2 1853 if ((error = quota2_umount(mp, flags)) != 0) 1854 return (error); 1855 #endif 1856 #ifdef UFS_EXTATTR 1857 if (ump->um_fstype == UFS1) { 1858 if (ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_STARTED) 1859 ufs_extattr_stop(mp, l); 1860 if (ump->um_extattr.uepm_flags & UFS_EXTATTR_UEPM_INITIALIZED) 1861 ufs_extattr_uepm_destroy(&ump->um_extattr); 1862 mp->mnt_flag &= ~MNT_EXTATTR; 1863 } 1864 #endif 1865 if ((error = vflush(mp, 0, SKIPSYSTEM | flags)) != 0) 1866 return (error); 1867 ffs_snapshot_unmount(mp); 1868 /* 1869 * Flush all the files. 1870 */ 1871 error = vflush(mp, NULLVP, flags); 1872 if (error) 1873 return (error); 1874 /* 1875 * Flush filesystem metadata. 1876 */ 1877 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY); 1878 error = VOP_FSYNC(ump->um_devvp, l->l_cred, FSYNC_WAIT, 0, 0); 1879 VOP_UNLOCK(ump->um_devvp); 1880 if (flags & FORCECLOSE) /* XXXDBJ */ 1881 error = 0; 1882 1883 #ifdef WAPBL 1884 if (error) 1885 return error; 1886 if (mp->mnt_wapbl) { 1887 error = wapbl_flush(mp->mnt_wapbl, 1); 1888 if (flags & FORCECLOSE) 1889 error = 0; 1890 } 1891 #endif 1892 1893 return (error); 1894 } 1895 1896 /* 1897 * Get file system statistics. 1898 */ 1899 int 1900 ffs_statvfs(struct mount *mp, struct statvfs *sbp) 1901 { 1902 struct ufsmount *ump; 1903 struct fs *fs; 1904 1905 ump = VFSTOUFS(mp); 1906 fs = ump->um_fs; 1907 mutex_enter(&ump->um_lock); 1908 sbp->f_bsize = fs->fs_bsize; 1909 sbp->f_frsize = fs->fs_fsize; 1910 sbp->f_iosize = fs->fs_bsize; 1911 sbp->f_blocks = fs->fs_dsize; 1912 sbp->f_bfree = ffs_blkstofrags(fs, fs->fs_cstotal.cs_nbfree) + 1913 fs->fs_cstotal.cs_nffree + FFS_DBTOFSB(fs, fs->fs_pendingblocks); 1914 sbp->f_bresvd = ((u_int64_t) fs->fs_dsize * (u_int64_t) 1915 fs->fs_minfree) / (u_int64_t) 100; 1916 if (sbp->f_bfree > sbp->f_bresvd) 1917 sbp->f_bavail = sbp->f_bfree - sbp->f_bresvd; 1918 else 1919 sbp->f_bavail = 0; 1920 sbp->f_files = fs->fs_ncg * fs->fs_ipg - UFS_ROOTINO; 1921 sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes; 1922 sbp->f_favail = sbp->f_ffree; 1923 sbp->f_fresvd = 0; 1924 mutex_exit(&ump->um_lock); 1925 copy_statvfs_info(sbp, mp); 1926 1927 return (0); 1928 } 1929 1930 struct ffs_sync_ctx { 1931 int waitfor; 1932 }; 1933 1934 static bool 1935 ffs_sync_selector(void *cl, struct vnode *vp) 1936 { 1937 struct ffs_sync_ctx *c = cl; 1938 struct inode *ip; 1939 1940 KASSERT(mutex_owned(vp->v_interlock)); 1941 1942 ip = VTOI(vp); 1943 /* 1944 * Skip the vnode/inode if inaccessible. 1945 */ 1946 if (ip == NULL || vp->v_type == VNON) 1947 return false; 1948 1949 /* 1950 * We deliberately update inode times here. This will 1951 * prevent a massive queue of updates accumulating, only 1952 * to be handled by a call to unmount. 1953 * 1954 * XXX It would be better to have the syncer trickle these 1955 * out. Adjustment needed to allow registering vnodes for 1956 * sync when the vnode is clean, but the inode dirty. Or 1957 * have ufs itself trickle out inode updates. 1958 * 1959 * If doing a lazy sync, we don't care about metadata or 1960 * data updates, because they are handled by each vnode's 1961 * synclist entry. In this case we are only interested in 1962 * writing back modified inodes. 1963 */ 1964 if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE | 1965 IN_MODIFY | IN_MODIFIED | IN_ACCESSED)) == 0 && 1966 (c->waitfor == MNT_LAZY || (LIST_EMPTY(&vp->v_dirtyblkhd) && 1967 (vp->v_iflag & VI_ONWORKLST) == 0))) 1968 return false; 1969 1970 return true; 1971 } 1972 1973 /* 1974 * Go through the disk queues to initiate sandbagged IO; 1975 * go through the inodes to write those that have been modified; 1976 * initiate the writing of the super block if it has been modified. 1977 */ 1978 int 1979 ffs_sync(struct mount *mp, int waitfor, kauth_cred_t cred) 1980 { 1981 struct vnode *vp; 1982 struct ufsmount *ump = VFSTOUFS(mp); 1983 struct fs *fs; 1984 struct vnode_iterator *marker; 1985 int error, allerror = 0; 1986 struct ffs_sync_ctx ctx; 1987 1988 fs = ump->um_fs; 1989 if (fs->fs_fmod != 0 && fs->fs_ronly != 0) { /* XXX */ 1990 panic("%s: rofs mod, fs=%s", __func__, fs->fs_fsmnt); 1991 } 1992 1993 /* 1994 * Write back each (modified) inode. 1995 */ 1996 vfs_vnode_iterator_init(mp, &marker); 1997 1998 ctx.waitfor = waitfor; 1999 while ((vp = vfs_vnode_iterator_next(marker, ffs_sync_selector, &ctx))) 2000 { 2001 error = vn_lock(vp, 2002 LK_EXCLUSIVE | (waitfor == MNT_LAZY ? LK_NOWAIT : 0)); 2003 if (error) { 2004 vrele(vp); 2005 continue; 2006 } 2007 if (waitfor == MNT_LAZY) { 2008 error = UFS_WAPBL_BEGIN(vp->v_mount); 2009 if (!error) { 2010 error = ffs_update(vp, NULL, NULL, 2011 UPDATE_CLOSE); 2012 UFS_WAPBL_END(vp->v_mount); 2013 } 2014 } else { 2015 error = VOP_FSYNC(vp, cred, FSYNC_NOLOG | 2016 (waitfor == MNT_WAIT ? FSYNC_WAIT : 0), 0, 0); 2017 } 2018 if (error) 2019 allerror = error; 2020 vput(vp); 2021 } 2022 vfs_vnode_iterator_destroy(marker); 2023 2024 /* 2025 * Force stale file system control information to be flushed. 2026 */ 2027 if (waitfor != MNT_LAZY) { 2028 bool need_devvp_fsync; 2029 2030 mutex_enter(ump->um_devvp->v_interlock); 2031 need_devvp_fsync = (ump->um_devvp->v_numoutput > 0 || 2032 !LIST_EMPTY(&ump->um_devvp->v_dirtyblkhd)); 2033 mutex_exit(ump->um_devvp->v_interlock); 2034 if (need_devvp_fsync) { 2035 int flags = FSYNC_NOLOG; 2036 2037 if (waitfor == MNT_WAIT) 2038 flags |= FSYNC_WAIT; 2039 2040 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY); 2041 if ((error = VOP_FSYNC(ump->um_devvp, cred, flags, 0, 2042 0)) != 0) 2043 allerror = error; 2044 VOP_UNLOCK(ump->um_devvp); 2045 } 2046 } 2047 #if defined(QUOTA) || defined(QUOTA2) 2048 qsync(mp); 2049 #endif 2050 /* 2051 * Write back modified superblock. 2052 */ 2053 error = UFS_WAPBL_BEGIN(mp); 2054 if (error) { 2055 allerror = error; 2056 } else { 2057 mutex_enter(&ump->um_lock); 2058 if (fs->fs_fmod != 0) { 2059 fs->fs_fmod = 0; 2060 fs->fs_time = time_second; 2061 mutex_exit(&ump->um_lock); 2062 if ((error = ffs_cgupdate(ump, waitfor))) 2063 allerror = error; 2064 } else { 2065 mutex_exit(&ump->um_lock); 2066 } 2067 UFS_WAPBL_END(mp); 2068 } 2069 2070 #ifdef WAPBL 2071 if (mp->mnt_wapbl) { 2072 error = wapbl_flush(mp->mnt_wapbl, (waitfor == MNT_WAIT)); 2073 if (error) 2074 allerror = error; 2075 } 2076 #endif 2077 2078 return (allerror); 2079 } 2080 2081 /* 2082 * Load inode from disk and initialize vnode. 2083 */ 2084 static int 2085 ffs_init_vnode(struct ufsmount *ump, struct vnode *vp, ino_t ino) 2086 { 2087 struct fs *fs; 2088 struct inode *ip; 2089 struct buf *bp; 2090 int error; 2091 2092 fs = ump->um_fs; 2093 2094 /* Read in the disk contents for the inode. */ 2095 error = bread(ump->um_devvp, FFS_FSBTODB(fs, ino_to_fsba(fs, ino)), 2096 (int)fs->fs_bsize, 0, &bp); 2097 if (error) 2098 return error; 2099 2100 /* Allocate and initialize inode. */ 2101 ip = pool_cache_get(ffs_inode_cache, PR_WAITOK); 2102 memset(ip, 0, sizeof(struct inode)); 2103 ip->i_ump = ump; 2104 ip->i_fs = fs; 2105 ip->i_dev = ump->um_dev; 2106 ip->i_number = ino; 2107 if (ump->um_fstype == UFS1) 2108 ip->i_din.ffs1_din = pool_cache_get(ffs_dinode1_cache, 2109 PR_WAITOK); 2110 else 2111 ip->i_din.ffs2_din = pool_cache_get(ffs_dinode2_cache, 2112 PR_WAITOK); 2113 ffs_load_inode(bp, ip, fs, ino); 2114 brelse(bp, 0); 2115 ip->i_vnode = vp; 2116 #if defined(QUOTA) || defined(QUOTA2) 2117 ufsquota_init(ip); 2118 #endif 2119 2120 /* Initialise vnode with this inode. */ 2121 vp->v_tag = VT_UFS; 2122 vp->v_op = ffs_vnodeop_p; 2123 vp->v_data = ip; 2124 2125 /* Initialize genfs node. */ 2126 genfs_node_init(vp, &ffs_genfsops); 2127 2128 return 0; 2129 } 2130 2131 /* 2132 * Undo ffs_init_vnode(). 2133 */ 2134 static void 2135 ffs_deinit_vnode(struct ufsmount *ump, struct vnode *vp) 2136 { 2137 struct inode *ip = VTOI(vp); 2138 2139 genfs_node_destroy(vp); 2140 vp->v_data = NULL; 2141 2142 if (ump->um_fstype == UFS1) 2143 pool_cache_put(ffs_dinode1_cache, ip->i_din.ffs1_din); 2144 else 2145 pool_cache_put(ffs_dinode2_cache, ip->i_din.ffs2_din); 2146 pool_cache_put(ffs_inode_cache, ip); 2147 } 2148 2149 /* 2150 * Read an inode from disk and initialize this vnode / inode pair. 2151 * Caller assures no other thread will try to load this inode. 2152 */ 2153 int 2154 ffs_loadvnode(struct mount *mp, struct vnode *vp, 2155 const void *key, size_t key_len, const void **new_key) 2156 { 2157 ino_t ino; 2158 struct fs *fs; 2159 struct inode *ip; 2160 struct ufsmount *ump; 2161 int error; 2162 2163 KASSERT(key_len == sizeof(ino)); 2164 memcpy(&ino, key, key_len); 2165 ump = VFSTOUFS(mp); 2166 fs = ump->um_fs; 2167 2168 error = ffs_init_vnode(ump, vp, ino); 2169 if (error) 2170 return error; 2171 2172 ip = VTOI(vp); 2173 if (ip->i_mode == 0) { 2174 ffs_deinit_vnode(ump, vp); 2175 2176 return ENOENT; 2177 } 2178 2179 /* Initialize the vnode from the inode. */ 2180 ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp); 2181 2182 /* Finish inode initialization. */ 2183 ip->i_devvp = ump->um_devvp; 2184 vref(ip->i_devvp); 2185 2186 /* 2187 * Ensure that uid and gid are correct. This is a temporary 2188 * fix until fsck has been changed to do the update. 2189 */ 2190 2191 if (fs->fs_magic == FS_UFS1_MAGIC && /* XXX */ 2192 fs->fs_old_inodefmt < FS_44INODEFMT) { /* XXX */ 2193 ip->i_uid = ip->i_ffs1_ouid; /* XXX */ 2194 ip->i_gid = ip->i_ffs1_ogid; /* XXX */ 2195 } /* XXX */ 2196 uvm_vnp_setsize(vp, ip->i_size); 2197 cache_enter_id(vp, ip->i_mode, ip->i_uid, ip->i_gid, !HAS_ACLS(ip)); 2198 *new_key = &ip->i_number; 2199 return 0; 2200 } 2201 2202 /* 2203 * Create a new inode on disk and initialize this vnode / inode pair. 2204 */ 2205 int 2206 ffs_newvnode(struct mount *mp, struct vnode *dvp, struct vnode *vp, 2207 struct vattr *vap, kauth_cred_t cred, void *extra, 2208 size_t *key_len, const void **new_key) 2209 { 2210 ino_t ino; 2211 struct fs *fs; 2212 struct inode *ip; 2213 struct timespec ts; 2214 struct ufsmount *ump; 2215 int error, mode; 2216 2217 KASSERT(dvp->v_mount == mp); 2218 KASSERT(vap->va_type != VNON); 2219 2220 *key_len = sizeof(ino); 2221 ump = VFSTOUFS(mp); 2222 fs = ump->um_fs; 2223 mode = MAKEIMODE(vap->va_type, vap->va_mode); 2224 2225 /* Allocate fresh inode. */ 2226 error = ffs_valloc(dvp, mode, cred, &ino); 2227 if (error) 2228 return error; 2229 2230 /* Attach inode to vnode. */ 2231 error = ffs_init_vnode(ump, vp, ino); 2232 if (error) { 2233 if (UFS_WAPBL_BEGIN(mp) == 0) { 2234 ffs_vfree(dvp, ino, mode); 2235 UFS_WAPBL_END(mp); 2236 } 2237 return error; 2238 } 2239 2240 ip = VTOI(vp); 2241 if (ip->i_mode) { 2242 panic("%s: dup alloc ino=%" PRId64 " on %s: mode %o/%o " 2243 "gen %x/%x size %" PRIx64 " blocks %" PRIx64, 2244 __func__, ino, fs->fs_fsmnt, DIP(ip, mode), ip->i_mode, 2245 DIP(ip, gen), ip->i_gen, DIP(ip, size), DIP(ip, blocks)); 2246 } 2247 if (DIP(ip, size) || DIP(ip, blocks)) { 2248 printf("%s: ino=%" PRId64 " on %s: " 2249 "gen %x/%x has non zero blocks %" PRIx64 " or size %" 2250 PRIx64 "\n", 2251 __func__, ino, fs->fs_fsmnt, DIP(ip, gen), ip->i_gen, 2252 DIP(ip, blocks), DIP(ip, size)); 2253 if ((ip)->i_ump->um_fstype == UFS1) 2254 panic("%s: dirty filesystem?", __func__); 2255 DIP_ASSIGN(ip, blocks, 0); 2256 DIP_ASSIGN(ip, size, 0); 2257 } 2258 2259 /* Set uid / gid. */ 2260 if (cred == NOCRED || cred == FSCRED) { 2261 ip->i_gid = 0; 2262 ip->i_uid = 0; 2263 } else { 2264 ip->i_gid = VTOI(dvp)->i_gid; 2265 ip->i_uid = kauth_cred_geteuid(cred); 2266 } 2267 DIP_ASSIGN(ip, gid, ip->i_gid); 2268 DIP_ASSIGN(ip, uid, ip->i_uid); 2269 2270 #if defined(QUOTA) || defined(QUOTA2) 2271 error = UFS_WAPBL_BEGIN(mp); 2272 if (error) { 2273 ffs_deinit_vnode(ump, vp); 2274 2275 return error; 2276 } 2277 error = chkiq(ip, 1, cred, 0); 2278 if (error) { 2279 ffs_vfree(dvp, ino, mode); 2280 UFS_WAPBL_END(mp); 2281 ffs_deinit_vnode(ump, vp); 2282 2283 return error; 2284 } 2285 UFS_WAPBL_END(mp); 2286 #endif 2287 2288 /* Set type and finalize. */ 2289 ip->i_flags = 0; 2290 DIP_ASSIGN(ip, flags, 0); 2291 ip->i_mode = mode; 2292 DIP_ASSIGN(ip, mode, mode); 2293 if (vap->va_rdev != VNOVAL) { 2294 /* 2295 * Want to be able to use this to make badblock 2296 * inodes, so don't truncate the dev number. 2297 */ 2298 if (ump->um_fstype == UFS1) 2299 ip->i_ffs1_rdev = ufs_rw32(vap->va_rdev, 2300 UFS_MPNEEDSWAP(ump)); 2301 else 2302 ip->i_ffs2_rdev = ufs_rw64(vap->va_rdev, 2303 UFS_MPNEEDSWAP(ump)); 2304 } 2305 ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp); 2306 ip->i_devvp = ump->um_devvp; 2307 vref(ip->i_devvp); 2308 2309 /* Set up a new generation number for this inode. */ 2310 ip->i_gen++; 2311 DIP_ASSIGN(ip, gen, ip->i_gen); 2312 if (fs->fs_magic == FS_UFS2_MAGIC) { 2313 vfs_timestamp(&ts); 2314 ip->i_ffs2_birthtime = ts.tv_sec; 2315 ip->i_ffs2_birthnsec = ts.tv_nsec; 2316 } 2317 2318 uvm_vnp_setsize(vp, ip->i_size); 2319 cache_enter_id(vp, ip->i_mode, ip->i_uid, ip->i_gid, !HAS_ACLS(ip)); 2320 *new_key = &ip->i_number; 2321 return 0; 2322 } 2323 2324 /* 2325 * File handle to vnode 2326 * 2327 * Have to be really careful about stale file handles: 2328 * - check that the inode number is valid 2329 * - call ffs_vget() to get the locked inode 2330 * - check for an unallocated inode (i_mode == 0) 2331 * - check that the given client host has export rights and return 2332 * those rights via. exflagsp and credanonp 2333 */ 2334 int 2335 ffs_fhtovp(struct mount *mp, struct fid *fhp, int lktype, struct vnode **vpp) 2336 { 2337 struct ufid ufh; 2338 int error; 2339 2340 if (fhp->fid_len != sizeof(struct ufid)) 2341 return EINVAL; 2342 2343 memcpy(&ufh, fhp, sizeof(ufh)); 2344 if ((error = ffs_checkrange(mp, ufh.ufid_ino)) != 0) 2345 return error; 2346 2347 return (ufs_fhtovp(mp, &ufh, lktype, vpp)); 2348 } 2349 2350 /* 2351 * Vnode pointer to File handle 2352 */ 2353 /* ARGSUSED */ 2354 int 2355 ffs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size) 2356 { 2357 struct inode *ip; 2358 struct ufid ufh; 2359 2360 if (*fh_size < sizeof(struct ufid)) { 2361 *fh_size = sizeof(struct ufid); 2362 return E2BIG; 2363 } 2364 ip = VTOI(vp); 2365 *fh_size = sizeof(struct ufid); 2366 memset(&ufh, 0, sizeof(ufh)); 2367 ufh.ufid_len = sizeof(struct ufid); 2368 ufh.ufid_ino = ip->i_number; 2369 ufh.ufid_gen = ip->i_gen; 2370 memcpy(fhp, &ufh, sizeof(ufh)); 2371 return (0); 2372 } 2373 2374 void 2375 ffs_init(void) 2376 { 2377 if (ffs_initcount++ > 0) 2378 return; 2379 2380 ffs_inode_cache = pool_cache_init(sizeof(struct inode), 0, 0, 0, 2381 "ffsino", NULL, IPL_NONE, NULL, NULL, NULL); 2382 ffs_dinode1_cache = pool_cache_init(sizeof(struct ufs1_dinode), 0, 0, 0, 2383 "ffsdino1", NULL, IPL_NONE, NULL, NULL, NULL); 2384 ffs_dinode2_cache = pool_cache_init(sizeof(struct ufs2_dinode), 0, 0, 0, 2385 "ffsdino2", NULL, IPL_NONE, NULL, NULL, NULL); 2386 ufs_init(); 2387 } 2388 2389 void 2390 ffs_reinit(void) 2391 { 2392 ufs_reinit(); 2393 } 2394 2395 void 2396 ffs_done(void) 2397 { 2398 if (--ffs_initcount > 0) 2399 return; 2400 2401 ufs_done(); 2402 pool_cache_destroy(ffs_dinode2_cache); 2403 pool_cache_destroy(ffs_dinode1_cache); 2404 pool_cache_destroy(ffs_inode_cache); 2405 } 2406 2407 /* 2408 * Write a superblock and associated information back to disk. 2409 */ 2410 int 2411 ffs_sbupdate(struct ufsmount *mp, int waitfor) 2412 { 2413 struct fs *fs = mp->um_fs; 2414 struct fs *bfs; 2415 struct buf *bp; 2416 int error; 2417 2418 error = ffs_getblk(mp->um_devvp, 2419 fs->fs_sblockloc / DEV_BSIZE, FFS_NOBLK, 2420 fs->fs_sbsize, false, &bp); 2421 if (error) 2422 return error; 2423 2424 mutex_enter(&mp->um_lock); 2425 memcpy(bp->b_data, fs, fs->fs_sbsize); 2426 mutex_exit(&mp->um_lock); 2427 2428 bfs = (struct fs *)bp->b_data; 2429 2430 bfs->fs_flags &= ~FS_INTERNAL; 2431 ffs_oldfscompat_write((struct fs *)bp->b_data, mp); 2432 if (mp->um_flags & UFS_EA) { 2433 KASSERT(bfs->fs_magic == FS_UFS2_MAGIC); 2434 bfs->fs_magic = FS_UFS2EA_MAGIC; 2435 } 2436 #ifdef FFS_EI 2437 if (mp->um_flags & UFS_NEEDSWAP) 2438 ffs_sb_swap(bfs, bfs); 2439 #endif 2440 2441 if (waitfor == MNT_WAIT) 2442 error = bwrite(bp); 2443 else 2444 bawrite(bp); 2445 return (error); 2446 } 2447 2448 int 2449 ffs_cgupdate(struct ufsmount *mp, int waitfor) 2450 { 2451 struct fs *fs = mp->um_fs; 2452 struct buf *bp; 2453 int blks; 2454 void *space; 2455 int i, size, error = 0, allerror = 0; 2456 2457 UFS_WAPBL_JLOCK_ASSERT(mp->um_mountp); 2458 2459 allerror = ffs_sbupdate(mp, waitfor); 2460 blks = howmany(fs->fs_cssize, fs->fs_fsize); 2461 space = fs->fs_csp; 2462 for (i = 0; i < blks; i += fs->fs_frag) { 2463 size = fs->fs_bsize; 2464 if (i + fs->fs_frag > blks) 2465 size = (blks - i) * fs->fs_fsize; 2466 error = ffs_getblk(mp->um_devvp, FFS_FSBTODB(fs, fs->fs_csaddr + i), 2467 FFS_NOBLK, size, false, &bp); 2468 if (error) 2469 break; 2470 #ifdef FFS_EI 2471 if (mp->um_flags & UFS_NEEDSWAP) 2472 ffs_csum_swap((struct csum*)space, 2473 (struct csum*)bp->b_data, size); 2474 else 2475 #endif 2476 memcpy(bp->b_data, space, (u_int)size); 2477 space = (char *)space + size; 2478 if (waitfor == MNT_WAIT) 2479 error = bwrite(bp); 2480 else 2481 bawrite(bp); 2482 } 2483 if (!allerror && error) 2484 allerror = error; 2485 return (allerror); 2486 } 2487 2488 int 2489 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *vp, 2490 int attrnamespace, const char *attrname) 2491 { 2492 #ifdef UFS_EXTATTR 2493 /* 2494 * File-backed extended attributes are only supported on UFS1. 2495 * UFS2 has native extended attributes. 2496 */ 2497 if (VFSTOUFS(mp)->um_fstype == UFS1) 2498 return (ufs_extattrctl(mp, cmd, vp, attrnamespace, attrname)); 2499 #endif 2500 return (vfs_stdextattrctl(mp, cmd, vp, attrnamespace, attrname)); 2501 } 2502 2503 /* 2504 * Synch vnode for a mounted file system. 2505 */ 2506 static int 2507 ffs_vfs_fsync(vnode_t *vp, int flags) 2508 { 2509 int error, i, pflags; 2510 #ifdef WAPBL 2511 struct mount *mp; 2512 #endif 2513 2514 KASSERT(vp->v_type == VBLK); 2515 KASSERT(spec_node_getmountedfs(vp) != NULL); 2516 2517 /* 2518 * Flush all dirty data associated with the vnode. 2519 */ 2520 pflags = PGO_ALLPAGES | PGO_CLEANIT; 2521 if ((flags & FSYNC_WAIT) != 0) 2522 pflags |= PGO_SYNCIO; 2523 rw_enter(vp->v_uobj.vmobjlock, RW_WRITER); 2524 error = VOP_PUTPAGES(vp, 0, 0, pflags); 2525 if (error) 2526 return error; 2527 2528 #ifdef WAPBL 2529 mp = spec_node_getmountedfs(vp); 2530 if (mp && mp->mnt_wapbl) { 2531 /* 2532 * Don't bother writing out metadata if the syncer is 2533 * making the request. We will let the sync vnode 2534 * write it out in a single burst through a call to 2535 * VFS_SYNC(). 2536 */ 2537 if ((flags & (FSYNC_DATAONLY | FSYNC_LAZY | FSYNC_NOLOG)) != 0) 2538 return 0; 2539 2540 /* 2541 * Don't flush the log if the vnode being flushed 2542 * contains no dirty buffers that could be in the log. 2543 */ 2544 if (!LIST_EMPTY(&vp->v_dirtyblkhd)) { 2545 error = wapbl_flush(mp->mnt_wapbl, 0); 2546 if (error) 2547 return error; 2548 } 2549 2550 if ((flags & FSYNC_WAIT) != 0) { 2551 mutex_enter(vp->v_interlock); 2552 while (vp->v_numoutput) 2553 cv_wait(&vp->v_cv, vp->v_interlock); 2554 mutex_exit(vp->v_interlock); 2555 } 2556 2557 return 0; 2558 } 2559 #endif /* WAPBL */ 2560 2561 error = vflushbuf(vp, flags); 2562 if (error == 0 && (flags & FSYNC_CACHE) != 0) { 2563 i = 1; 2564 (void)VOP_IOCTL(vp, DIOCCACHESYNC, &i, FWRITE, 2565 kauth_cred_get()); 2566 } 2567 2568 return error; 2569 } 2570