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