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