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