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