ffs_vfsops.c revision 1.241.4.2 1 /* $NetBSD: ffs_vfsops.c,v 1.241.4.2 2009/07/23 23:33:03 jym 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.241.4.2 2009/07/23 23:33:03 jym 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 system call
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 #ifdef UFS_EXTATTR
1384 if (ump->um_fstype == UFS1) {
1385 ufs_extattr_stop(mp, l);
1386 ufs_extattr_uepm_destroy(&ump->um_extattr);
1387 }
1388 #endif /* UFS_EXTATTR */
1389 if ((error = ffs_flushfiles(mp, flags, l)) != 0)
1390 return (error);
1391 error = UFS_WAPBL_BEGIN(mp);
1392 if (error == 0)
1393 if (fs->fs_ronly == 0 &&
1394 ffs_cgupdate(ump, MNT_WAIT) == 0 &&
1395 fs->fs_clean & FS_WASCLEAN) {
1396 fs->fs_clean = FS_ISCLEAN;
1397 fs->fs_fmod = 0;
1398 (void) ffs_sbupdate(ump, MNT_WAIT);
1399 }
1400 if (error == 0)
1401 UFS_WAPBL_END(mp);
1402 #ifdef WAPBL
1403 KASSERT(!(mp->mnt_wapbl_replay && mp->mnt_wapbl));
1404 if (mp->mnt_wapbl_replay) {
1405 KDASSERT(fs->fs_ronly);
1406 wapbl_replay_stop(mp->mnt_wapbl_replay);
1407 wapbl_replay_free(mp->mnt_wapbl_replay);
1408 mp->mnt_wapbl_replay = 0;
1409 }
1410 error = ffs_wapbl_stop(mp, doforce && (mntflags & MNT_FORCE));
1411 if (error) {
1412 return error;
1413 }
1414 #endif /* WAPBL */
1415 if (ump->um_devvp->v_type != VBAD)
1416 ump->um_devvp->v_specmountpoint = NULL;
1417 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1418 (void)VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD | FWRITE,
1419 NOCRED);
1420 vput(ump->um_devvp);
1421 free(fs->fs_csp, M_UFSMNT);
1422 free(fs, M_UFSMNT);
1423 if (ump->um_oldfscompat != NULL)
1424 free(ump->um_oldfscompat, M_UFSMNT);
1425 mutex_destroy(&ump->um_lock);
1426 ffs_snapshot_fini(ump);
1427 free(ump, M_UFSMNT);
1428 mp->mnt_data = NULL;
1429 mp->mnt_flag &= ~MNT_LOCAL;
1430 fstrans_unmount(mp);
1431 return (0);
1432 }
1433
1434 /*
1435 * Flush out all the files in a filesystem.
1436 */
1437 int
1438 ffs_flushfiles(struct mount *mp, int flags, struct lwp *l)
1439 {
1440 extern int doforce;
1441 struct ufsmount *ump;
1442 int error;
1443
1444 if (!doforce)
1445 flags &= ~FORCECLOSE;
1446 ump = VFSTOUFS(mp);
1447 #ifdef QUOTA
1448 if (mp->mnt_flag & MNT_QUOTA) {
1449 int i;
1450 if ((error = vflush(mp, NULLVP, SKIPSYSTEM | flags)) != 0)
1451 return (error);
1452 for (i = 0; i < MAXQUOTAS; i++) {
1453 if (ump->um_quotas[i] == NULLVP)
1454 continue;
1455 quotaoff(l, mp, i);
1456 }
1457 /*
1458 * Here we fall through to vflush again to ensure
1459 * that we have gotten rid of all the system vnodes.
1460 */
1461 }
1462 #endif
1463 if ((error = vflush(mp, 0, SKIPSYSTEM | flags)) != 0)
1464 return (error);
1465 ffs_snapshot_unmount(mp);
1466 /*
1467 * Flush all the files.
1468 */
1469 error = vflush(mp, NULLVP, flags);
1470 if (error)
1471 return (error);
1472 /*
1473 * Flush filesystem metadata.
1474 */
1475 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1476 error = VOP_FSYNC(ump->um_devvp, l->l_cred, FSYNC_WAIT, 0, 0);
1477 VOP_UNLOCK(ump->um_devvp, 0);
1478 if (flags & FORCECLOSE) /* XXXDBJ */
1479 error = 0;
1480
1481 #ifdef WAPBL
1482 if (error)
1483 return error;
1484 if (mp->mnt_wapbl) {
1485 error = wapbl_flush(mp->mnt_wapbl, 1);
1486 if (flags & FORCECLOSE)
1487 error = 0;
1488 }
1489 #endif
1490
1491 return (error);
1492 }
1493
1494 /*
1495 * Get file system statistics.
1496 */
1497 int
1498 ffs_statvfs(struct mount *mp, struct statvfs *sbp)
1499 {
1500 struct ufsmount *ump;
1501 struct fs *fs;
1502
1503 ump = VFSTOUFS(mp);
1504 fs = ump->um_fs;
1505 mutex_enter(&ump->um_lock);
1506 sbp->f_bsize = fs->fs_bsize;
1507 sbp->f_frsize = fs->fs_fsize;
1508 sbp->f_iosize = fs->fs_bsize;
1509 sbp->f_blocks = fs->fs_dsize;
1510 sbp->f_bfree = blkstofrags(fs, fs->fs_cstotal.cs_nbfree) +
1511 fs->fs_cstotal.cs_nffree + dbtofsb(fs, fs->fs_pendingblocks);
1512 sbp->f_bresvd = ((u_int64_t) fs->fs_dsize * (u_int64_t)
1513 fs->fs_minfree) / (u_int64_t) 100;
1514 if (sbp->f_bfree > sbp->f_bresvd)
1515 sbp->f_bavail = sbp->f_bfree - sbp->f_bresvd;
1516 else
1517 sbp->f_bavail = 0;
1518 sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO;
1519 sbp->f_ffree = fs->fs_cstotal.cs_nifree + fs->fs_pendinginodes;
1520 sbp->f_favail = sbp->f_ffree;
1521 sbp->f_fresvd = 0;
1522 mutex_exit(&ump->um_lock);
1523 copy_statvfs_info(sbp, mp);
1524
1525 return (0);
1526 }
1527
1528 /*
1529 * Go through the disk queues to initiate sandbagged IO;
1530 * go through the inodes to write those that have been modified;
1531 * initiate the writing of the super block if it has been modified.
1532 *
1533 * Note: we are always called with the filesystem marked `MPBUSY'.
1534 */
1535 int
1536 ffs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
1537 {
1538 struct vnode *vp, *mvp, *nvp;
1539 struct inode *ip;
1540 struct ufsmount *ump = VFSTOUFS(mp);
1541 struct fs *fs;
1542 int error, allerror = 0;
1543
1544 fs = ump->um_fs;
1545 if (fs->fs_fmod != 0 && fs->fs_ronly != 0) { /* XXX */
1546 printf("fs = %s\n", fs->fs_fsmnt);
1547 panic("update: rofs mod");
1548 }
1549
1550 /* Allocate a marker vnode. */
1551 if ((mvp = vnalloc(mp)) == NULL)
1552 return (ENOMEM);
1553
1554 fstrans_start(mp, FSTRANS_SHARED);
1555 /*
1556 * Write back each (modified) inode.
1557 */
1558 mutex_enter(&mntvnode_lock);
1559 loop:
1560 /*
1561 * NOTE: not using the TAILQ_FOREACH here since in this loop vgone()
1562 * and vclean() can be called indirectly
1563 */
1564 for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = nvp) {
1565 nvp = TAILQ_NEXT(vp, v_mntvnodes);
1566 /*
1567 * If the vnode that we are about to sync is no longer
1568 * associated with this mount point, start over.
1569 */
1570 if (vp->v_mount != mp)
1571 goto loop;
1572 /*
1573 * Don't interfere with concurrent scans of this FS.
1574 */
1575 if (vismarker(vp))
1576 continue;
1577 mutex_enter(&vp->v_interlock);
1578 ip = VTOI(vp);
1579
1580 /*
1581 * Skip the vnode/inode if inaccessible.
1582 */
1583 if (ip == NULL || (vp->v_iflag & (VI_XLOCK | VI_CLEAN)) != 0 ||
1584 vp->v_type == VNON) {
1585 mutex_exit(&vp->v_interlock);
1586 continue;
1587 }
1588
1589 /*
1590 * We deliberately update inode times here. This will
1591 * prevent a massive queue of updates accumulating, only
1592 * to be handled by a call to unmount.
1593 *
1594 * XXX It would be better to have the syncer trickle these
1595 * out. Adjustment needed to allow registering vnodes for
1596 * sync when the vnode is clean, but the inode dirty. Or
1597 * have ufs itself trickle out inode updates.
1598 *
1599 * If doing a lazy sync, we don't care about metadata or
1600 * data updates, because they are handled by each vnode's
1601 * synclist entry. In this case we are only interested in
1602 * writing back modified inodes.
1603 */
1604 if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE |
1605 IN_MODIFY | IN_MODIFIED | IN_ACCESSED)) == 0 &&
1606 (waitfor == MNT_LAZY || (LIST_EMPTY(&vp->v_dirtyblkhd) &&
1607 UVM_OBJ_IS_CLEAN(&vp->v_uobj)))) {
1608 mutex_exit(&vp->v_interlock);
1609 continue;
1610 }
1611 if (vp->v_type == VBLK &&
1612 fstrans_getstate(mp) == FSTRANS_SUSPENDING) {
1613 mutex_exit(&vp->v_interlock);
1614 continue;
1615 }
1616 vmark(mvp, vp);
1617 mutex_exit(&mntvnode_lock);
1618 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
1619 if (error) {
1620 mutex_enter(&mntvnode_lock);
1621 nvp = vunmark(mvp);
1622 if (error == ENOENT) {
1623 goto loop;
1624 }
1625 continue;
1626 }
1627 if (waitfor == MNT_LAZY) {
1628 error = UFS_WAPBL_BEGIN(vp->v_mount);
1629 if (!error) {
1630 error = ffs_update(vp, NULL, NULL,
1631 UPDATE_CLOSE);
1632 UFS_WAPBL_END(vp->v_mount);
1633 }
1634 } else {
1635 error = VOP_FSYNC(vp, cred, FSYNC_NOLOG |
1636 (waitfor == MNT_WAIT ? FSYNC_WAIT : 0), 0, 0);
1637 }
1638 if (error)
1639 allerror = error;
1640 vput(vp);
1641 mutex_enter(&mntvnode_lock);
1642 nvp = vunmark(mvp);
1643 }
1644 mutex_exit(&mntvnode_lock);
1645 /*
1646 * Force stale file system control information to be flushed.
1647 */
1648 if (waitfor != MNT_LAZY && (ump->um_devvp->v_numoutput > 0 ||
1649 !LIST_EMPTY(&ump->um_devvp->v_dirtyblkhd))) {
1650 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
1651 if ((error = VOP_FSYNC(ump->um_devvp, cred,
1652 (waitfor == MNT_WAIT ? FSYNC_WAIT : 0) | FSYNC_NOLOG,
1653 0, 0)) != 0)
1654 allerror = error;
1655 VOP_UNLOCK(ump->um_devvp, 0);
1656 if (allerror == 0 && waitfor == MNT_WAIT && !mp->mnt_wapbl) {
1657 mutex_enter(&mntvnode_lock);
1658 goto loop;
1659 }
1660 }
1661 #ifdef QUOTA
1662 qsync(mp);
1663 #endif
1664 /*
1665 * Write back modified superblock.
1666 */
1667 if (fs->fs_fmod != 0) {
1668 fs->fs_fmod = 0;
1669 fs->fs_time = time_second;
1670 error = UFS_WAPBL_BEGIN(mp);
1671 if (error)
1672 allerror = error;
1673 else {
1674 if ((error = ffs_cgupdate(ump, waitfor)))
1675 allerror = error;
1676 UFS_WAPBL_END(mp);
1677 }
1678 }
1679
1680 #ifdef WAPBL
1681 if (mp->mnt_wapbl) {
1682 error = wapbl_flush(mp->mnt_wapbl, 0);
1683 if (error)
1684 allerror = error;
1685 }
1686 #endif
1687
1688 fstrans_done(mp);
1689 vnfree(mvp);
1690 return (allerror);
1691 }
1692
1693 /*
1694 * Look up a FFS dinode number to find its incore vnode, otherwise read it
1695 * in from disk. If it is in core, wait for the lock bit to clear, then
1696 * return the inode locked. Detection and handling of mount points must be
1697 * done by the calling routine.
1698 */
1699 int
1700 ffs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
1701 {
1702 struct fs *fs;
1703 struct inode *ip;
1704 struct ufsmount *ump;
1705 struct buf *bp;
1706 struct vnode *vp;
1707 dev_t dev;
1708 int error;
1709
1710 ump = VFSTOUFS(mp);
1711 dev = ump->um_dev;
1712
1713 retry:
1714 if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
1715 return (0);
1716
1717 /* Allocate a new vnode/inode. */
1718 if ((error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, &vp)) != 0) {
1719 *vpp = NULL;
1720 return (error);
1721 }
1722 ip = pool_cache_get(ffs_inode_cache, PR_WAITOK);
1723
1724 /*
1725 * If someone beat us to it, put back the freshly allocated
1726 * vnode/inode pair and retry.
1727 */
1728 mutex_enter(&ufs_hashlock);
1729 if (ufs_ihashget(dev, ino, 0) != NULL) {
1730 mutex_exit(&ufs_hashlock);
1731 ungetnewvnode(vp);
1732 pool_cache_put(ffs_inode_cache, ip);
1733 goto retry;
1734 }
1735
1736 vp->v_vflag |= VV_LOCKSWORK;
1737
1738 /*
1739 * XXX MFS ends up here, too, to allocate an inode. Should we
1740 * XXX create another pool for MFS inodes?
1741 */
1742
1743 memset(ip, 0, sizeof(struct inode));
1744 vp->v_data = ip;
1745 ip->i_vnode = vp;
1746 ip->i_ump = ump;
1747 ip->i_fs = fs = ump->um_fs;
1748 ip->i_dev = dev;
1749 ip->i_number = ino;
1750 #ifdef QUOTA
1751 ufsquota_init(ip);
1752 #endif
1753
1754 /*
1755 * Initialize genfs node, we might proceed to destroy it in
1756 * error branches.
1757 */
1758 genfs_node_init(vp, &ffs_genfsops);
1759
1760 /*
1761 * Put it onto its hash chain and lock it so that other requests for
1762 * this inode will block if they arrive while we are sleeping waiting
1763 * for old data structures to be purged or for the contents of the
1764 * disk portion of this inode to be read.
1765 */
1766
1767 ufs_ihashins(ip);
1768 mutex_exit(&ufs_hashlock);
1769
1770 /* Read in the disk contents for the inode, copy into the inode. */
1771 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1772 (int)fs->fs_bsize, NOCRED, 0, &bp);
1773 if (error) {
1774
1775 /*
1776 * The inode does not contain anything useful, so it would
1777 * be misleading to leave it on its hash chain. With mode
1778 * still zero, it will be unlinked and returned to the free
1779 * list by vput().
1780 */
1781
1782 vput(vp);
1783 brelse(bp, 0);
1784 *vpp = NULL;
1785 return (error);
1786 }
1787 if (ip->i_ump->um_fstype == UFS1)
1788 ip->i_din.ffs1_din = pool_cache_get(ffs_dinode1_cache,
1789 PR_WAITOK);
1790 else
1791 ip->i_din.ffs2_din = pool_cache_get(ffs_dinode2_cache,
1792 PR_WAITOK);
1793 ffs_load_inode(bp, ip, fs, ino);
1794 brelse(bp, 0);
1795
1796 /*
1797 * Initialize the vnode from the inode, check for aliases.
1798 * Note that the underlying vnode may have changed.
1799 */
1800
1801 ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp);
1802
1803 /*
1804 * Finish inode initialization now that aliasing has been resolved.
1805 */
1806
1807 ip->i_devvp = ump->um_devvp;
1808 VREF(ip->i_devvp);
1809
1810 /*
1811 * Ensure that uid and gid are correct. This is a temporary
1812 * fix until fsck has been changed to do the update.
1813 */
1814
1815 if (fs->fs_old_inodefmt < FS_44INODEFMT) { /* XXX */
1816 ip->i_uid = ip->i_ffs1_ouid; /* XXX */
1817 ip->i_gid = ip->i_ffs1_ogid; /* XXX */
1818 } /* XXX */
1819 uvm_vnp_setsize(vp, ip->i_size);
1820 *vpp = vp;
1821 return (0);
1822 }
1823
1824 /*
1825 * File handle to vnode
1826 *
1827 * Have to be really careful about stale file handles:
1828 * - check that the inode number is valid
1829 * - call ffs_vget() to get the locked inode
1830 * - check for an unallocated inode (i_mode == 0)
1831 * - check that the given client host has export rights and return
1832 * those rights via. exflagsp and credanonp
1833 */
1834 int
1835 ffs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
1836 {
1837 struct ufid ufh;
1838 struct fs *fs;
1839
1840 if (fhp->fid_len != sizeof(struct ufid))
1841 return EINVAL;
1842
1843 memcpy(&ufh, fhp, sizeof(ufh));
1844 fs = VFSTOUFS(mp)->um_fs;
1845 if (ufh.ufid_ino < ROOTINO ||
1846 ufh.ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1847 return (ESTALE);
1848 return (ufs_fhtovp(mp, &ufh, vpp));
1849 }
1850
1851 /*
1852 * Vnode pointer to File handle
1853 */
1854 /* ARGSUSED */
1855 int
1856 ffs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
1857 {
1858 struct inode *ip;
1859 struct ufid ufh;
1860
1861 if (*fh_size < sizeof(struct ufid)) {
1862 *fh_size = sizeof(struct ufid);
1863 return E2BIG;
1864 }
1865 ip = VTOI(vp);
1866 *fh_size = sizeof(struct ufid);
1867 memset(&ufh, 0, sizeof(ufh));
1868 ufh.ufid_len = sizeof(struct ufid);
1869 ufh.ufid_ino = ip->i_number;
1870 ufh.ufid_gen = ip->i_gen;
1871 memcpy(fhp, &ufh, sizeof(ufh));
1872 return (0);
1873 }
1874
1875 void
1876 ffs_init(void)
1877 {
1878 if (ffs_initcount++ > 0)
1879 return;
1880
1881 ffs_inode_cache = pool_cache_init(sizeof(struct inode), 0, 0, 0,
1882 "ffsino", NULL, IPL_NONE, NULL, NULL, NULL);
1883 ffs_dinode1_cache = pool_cache_init(sizeof(struct ufs1_dinode), 0, 0, 0,
1884 "ffsdino1", NULL, IPL_NONE, NULL, NULL, NULL);
1885 ffs_dinode2_cache = pool_cache_init(sizeof(struct ufs2_dinode), 0, 0, 0,
1886 "ffsdino2", NULL, IPL_NONE, NULL, NULL, NULL);
1887 ufs_init();
1888 }
1889
1890 void
1891 ffs_reinit(void)
1892 {
1893
1894 ufs_reinit();
1895 }
1896
1897 void
1898 ffs_done(void)
1899 {
1900 if (--ffs_initcount > 0)
1901 return;
1902
1903 ufs_done();
1904 pool_cache_destroy(ffs_dinode2_cache);
1905 pool_cache_destroy(ffs_dinode1_cache);
1906 pool_cache_destroy(ffs_inode_cache);
1907 }
1908
1909 /*
1910 * Write a superblock and associated information back to disk.
1911 */
1912 int
1913 ffs_sbupdate(struct ufsmount *mp, int waitfor)
1914 {
1915 struct fs *fs = mp->um_fs;
1916 struct buf *bp;
1917 int error = 0;
1918 u_int32_t saveflag;
1919
1920 error = ffs_getblk(mp->um_devvp,
1921 fs->fs_sblockloc >> (fs->fs_fshift - fs->fs_fsbtodb), FFS_NOBLK,
1922 fs->fs_sbsize, false, &bp);
1923 if (error)
1924 return error;
1925 saveflag = fs->fs_flags & FS_INTERNAL;
1926 fs->fs_flags &= ~FS_INTERNAL;
1927
1928 memcpy(bp->b_data, fs, fs->fs_sbsize);
1929
1930 ffs_oldfscompat_write((struct fs *)bp->b_data, mp);
1931 #ifdef FFS_EI
1932 if (mp->um_flags & UFS_NEEDSWAP)
1933 ffs_sb_swap((struct fs *)bp->b_data, (struct fs *)bp->b_data);
1934 #endif
1935 fs->fs_flags |= saveflag;
1936
1937 if (waitfor == MNT_WAIT)
1938 error = bwrite(bp);
1939 else
1940 bawrite(bp);
1941 return (error);
1942 }
1943
1944 int
1945 ffs_cgupdate(struct ufsmount *mp, int waitfor)
1946 {
1947 struct fs *fs = mp->um_fs;
1948 struct buf *bp;
1949 int blks;
1950 void *space;
1951 int i, size, error = 0, allerror = 0;
1952
1953 allerror = ffs_sbupdate(mp, waitfor);
1954 blks = howmany(fs->fs_cssize, fs->fs_fsize);
1955 space = fs->fs_csp;
1956 for (i = 0; i < blks; i += fs->fs_frag) {
1957 size = fs->fs_bsize;
1958 if (i + fs->fs_frag > blks)
1959 size = (blks - i) * fs->fs_fsize;
1960 error = ffs_getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
1961 FFS_NOBLK, size, false, &bp);
1962 if (error)
1963 break;
1964 #ifdef FFS_EI
1965 if (mp->um_flags & UFS_NEEDSWAP)
1966 ffs_csum_swap((struct csum*)space,
1967 (struct csum*)bp->b_data, size);
1968 else
1969 #endif
1970 memcpy(bp->b_data, space, (u_int)size);
1971 space = (char *)space + size;
1972 if (waitfor == MNT_WAIT)
1973 error = bwrite(bp);
1974 else
1975 bawrite(bp);
1976 }
1977 if (!allerror && error)
1978 allerror = error;
1979 return (allerror);
1980 }
1981
1982 int
1983 ffs_extattrctl(struct mount *mp, int cmd, struct vnode *vp,
1984 int attrnamespace, const char *attrname)
1985 {
1986 #ifdef UFS_EXTATTR
1987 /*
1988 * File-backed extended attributes are only supported on UFS1.
1989 * UFS2 has native extended attributes.
1990 */
1991 if (VFSTOUFS(mp)->um_fstype == UFS1)
1992 return (ufs_extattrctl(mp, cmd, vp, attrnamespace, attrname));
1993 #endif
1994 return (vfs_stdextattrctl(mp, cmd, vp, attrnamespace, attrname));
1995 }
1996
1997 int
1998 ffs_suspendctl(struct mount *mp, int cmd)
1999 {
2000 int error;
2001 struct lwp *l = curlwp;
2002
2003 switch (cmd) {
2004 case SUSPEND_SUSPEND:
2005 if ((error = fstrans_setstate(mp, FSTRANS_SUSPENDING)) != 0)
2006 return error;
2007 error = ffs_sync(mp, MNT_WAIT, l->l_proc->p_cred);
2008 if (error == 0)
2009 error = fstrans_setstate(mp, FSTRANS_SUSPENDED);
2010 #ifdef WAPBL
2011 if (error == 0 && mp->mnt_wapbl)
2012 error = wapbl_flush(mp->mnt_wapbl, 1);
2013 #endif
2014 if (error != 0) {
2015 (void) fstrans_setstate(mp, FSTRANS_NORMAL);
2016 return error;
2017 }
2018 return 0;
2019
2020 case SUSPEND_RESUME:
2021 return fstrans_setstate(mp, FSTRANS_NORMAL);
2022
2023 default:
2024 return EINVAL;
2025 }
2026 }
2027
2028 /*
2029 * Synch vnode for a mounted file system. This is called for foreign
2030 * vnodes, i.e. non-ffs.
2031 */
2032 static int
2033 ffs_vfs_fsync(vnode_t *vp, int flags)
2034 {
2035 int error, passes, skipmeta, i, pflags;
2036 buf_t *bp, *nbp;
2037 #ifdef WAPBL
2038 struct mount *mp;
2039 #endif
2040
2041 KASSERT(vp->v_type == VBLK);
2042 KASSERT(vp->v_specmountpoint != NULL);
2043
2044 /*
2045 * Flush all dirty data associated with the vnode.
2046 */
2047 pflags = PGO_ALLPAGES | PGO_CLEANIT;
2048 if ((flags & FSYNC_WAIT) != 0)
2049 pflags |= PGO_SYNCIO;
2050 mutex_enter(&vp->v_interlock);
2051 error = VOP_PUTPAGES(vp, 0, 0, pflags);
2052 if (error)
2053 return error;
2054
2055 #ifdef WAPBL
2056 mp = vp->v_specmountpoint;
2057 if (mp && mp->mnt_wapbl) {
2058 /*
2059 * Don't bother writing out metadata if the syncer is
2060 * making the request. We will let the sync vnode
2061 * write it out in a single burst through a call to
2062 * VFS_SYNC().
2063 */
2064 if ((flags & (FSYNC_DATAONLY | FSYNC_LAZY | FSYNC_NOLOG)) != 0)
2065 return 0;
2066
2067 /*
2068 * Don't flush the log if the vnode being flushed
2069 * contains no dirty buffers that could be in the log.
2070 */
2071 if (!LIST_EMPTY(&vp->v_dirtyblkhd)) {
2072 error = wapbl_flush(mp->mnt_wapbl, 0);
2073 if (error)
2074 return error;
2075 }
2076
2077 if ((flags & FSYNC_WAIT) != 0) {
2078 mutex_enter(&vp->v_interlock);
2079 while (vp->v_numoutput)
2080 cv_wait(&vp->v_cv, &vp->v_interlock);
2081 mutex_exit(&vp->v_interlock);
2082 }
2083
2084 return 0;
2085 }
2086 #endif /* WAPBL */
2087
2088 /*
2089 * Write out metadata for non-logging file systems. XXX This block
2090 * should be simplified now that softdep is gone.
2091 */
2092 passes = NIADDR + 1;
2093 skipmeta = 0;
2094 if (flags & FSYNC_WAIT)
2095 skipmeta = 1;
2096
2097 loop:
2098 mutex_enter(&bufcache_lock);
2099 LIST_FOREACH(bp, &vp->v_dirtyblkhd, b_vnbufs) {
2100 bp->b_cflags &= ~BC_SCANNED;
2101 }
2102 for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2103 nbp = LIST_NEXT(bp, b_vnbufs);
2104 if (bp->b_cflags & (BC_BUSY | BC_SCANNED))
2105 continue;
2106 if ((bp->b_oflags & BO_DELWRI) == 0)
2107 panic("ffs_fsync: not dirty");
2108 if (skipmeta && bp->b_lblkno < 0)
2109 continue;
2110 bp->b_cflags |= BC_BUSY | BC_VFLUSH | BC_SCANNED;
2111 mutex_exit(&bufcache_lock);
2112 /*
2113 * On our final pass through, do all I/O synchronously
2114 * so that we can find out if our flush is failing
2115 * because of write errors.
2116 */
2117 if (passes > 0 || !(flags & FSYNC_WAIT))
2118 (void) bawrite(bp);
2119 else if ((error = bwrite(bp)) != 0)
2120 return (error);
2121 /*
2122 * Since we unlocked during the I/O, we need
2123 * to start from a known point.
2124 */
2125 mutex_enter(&bufcache_lock);
2126 nbp = LIST_FIRST(&vp->v_dirtyblkhd);
2127 }
2128 mutex_exit(&bufcache_lock);
2129 if (skipmeta) {
2130 skipmeta = 0;
2131 goto loop;
2132 }
2133
2134 if ((flags & FSYNC_WAIT) != 0) {
2135 mutex_enter(&vp->v_interlock);
2136 while (vp->v_numoutput) {
2137 cv_wait(&vp->v_cv, &vp->v_interlock);
2138 }
2139 mutex_exit(&vp->v_interlock);
2140
2141 if (!LIST_EMPTY(&vp->v_dirtyblkhd)) {
2142 /*
2143 * Block devices associated with filesystems may
2144 * have new I/O requests posted for them even if
2145 * the vnode is locked, so no amount of trying will
2146 * get them clean. Thus we give block devices a
2147 * good effort, then just give up. For all other file
2148 * types, go around and try again until it is clean.
2149 */
2150 if (passes > 0) {
2151 passes--;
2152 goto loop;
2153 }
2154 #ifdef DIAGNOSTIC
2155 if (vp->v_type != VBLK)
2156 vprint("ffs_fsync: dirty", vp);
2157 #endif
2158 }
2159 }
2160
2161 if (error == 0 && (flags & FSYNC_CACHE) != 0) {
2162 (void)VOP_IOCTL(vp, DIOCCACHESYNC, &i, FWRITE,
2163 kauth_cred_get());
2164 }
2165
2166 return error;
2167 }
2168