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