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