1 /* $NetBSD: ext2fs_vfsops.c,v 1.175 2013/11/23 13:35:36 christos Exp $ */
4 * Copyright (c) 1989, 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * @(#)ffs_vfsops.c 8.14 (Berkeley) 11/28/94
32 * Modified for ext2fs by Manuel Bouyer.
36 * Copyright (c) 1997 Manuel Bouyer.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
47 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
48 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
50 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
51 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
52 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
53 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
54 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
55 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
56 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58 * @(#)ffs_vfsops.c 8.14 (Berkeley) 11/28/94
59 * Modified for ext2fs by Manuel Bouyer.
62 #include <sys/cdefs.h>
63 __KERNEL_RCSID(0, "$NetBSD: ext2fs_vfsops.c,v 1.175 2013/11/23 13:35:36 christos Exp $");
65 #if defined(_KERNEL_OPT)
66 #include "opt_compat_netbsd.h"
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/sysctl.h>
72 #include <sys/namei.h>
74 #include <sys/kernel.h>
75 #include <sys/vnode.h>
76 #include <sys/socket.h>
77 #include <sys/mount.h>
79 #include <sys/device.h>
82 #include <sys/disklabel.h>
83 #include <sys/ioctl.h>
84 #include <sys/errno.h>
85 #include <sys/malloc.h>
89 #include <sys/kauth.h>
90 #include <sys/module.h>
92 #include <miscfs/genfs/genfs.h>
93 #include <miscfs/specfs/specdev.h>
95 #include <ufs/ufs/quota.h>
96 #include <ufs/ufs/ufsmount.h>
97 #include <ufs/ufs/inode.h>
98 #include <ufs/ufs/dir.h>
99 #include <ufs/ufs/ufs_extern.h>
101 #include <ufs/ext2fs/ext2fs.h>
102 #include <ufs/ext2fs/ext2fs_dir.h>
103 #include <ufs/ext2fs/ext2fs_extern.h>
105 MODULE(MODULE_CLASS_VFS
, ext2fs
, "ffs");
107 int ext2fs_sbupdate(struct ufsmount
*, int);
108 static int ext2fs_checksb(struct ext2fs
*, int);
110 static struct sysctllog
*ext2fs_sysctl_log
;
112 extern const struct vnodeopv_desc ext2fs_vnodeop_opv_desc
;
113 extern const struct vnodeopv_desc ext2fs_specop_opv_desc
;
114 extern const struct vnodeopv_desc ext2fs_fifoop_opv_desc
;
116 const struct vnodeopv_desc
* const ext2fs_vnodeopv_descs
[] = {
117 &ext2fs_vnodeop_opv_desc
,
118 &ext2fs_specop_opv_desc
,
119 &ext2fs_fifoop_opv_desc
,
123 struct vfsops ext2fs_vfsops
= {
125 sizeof (struct ufs_args
),
140 (int (*)(struct mount
*, struct vnode
*, struct timespec
*)) eopnotsupp
,
142 (void *)eopnotsupp
, /* vfs_suspendctl */
143 genfs_renamelock_enter
,
144 genfs_renamelock_exit
,
146 ext2fs_vnodeopv_descs
,
151 static const struct genfs_ops ext2fs_genfsops
= {
152 .gop_size
= genfs_size
,
153 .gop_alloc
= ext2fs_gop_alloc
,
154 .gop_write
= genfs_gop_write
,
155 .gop_markupdate
= ufs_gop_markupdate
,
158 static const struct ufs_ops ext2fs_ufsops
= {
159 .uo_itimes
= ext2fs_itimes
,
160 .uo_update
= ext2fs_update
,
161 .uo_vfree
= ext2fs_vfree
,
164 /* Fill in the inode uid/gid from ext2 halves. */
166 ext2fs_set_inode_guid(struct inode
*ip
)
169 ip
->i_gid
= ip
->i_e2fs_gid
;
170 ip
->i_uid
= ip
->i_e2fs_uid
;
171 if (ip
->i_e2fs
->e2fs
.e2fs_rev
> E2FS_REV0
) {
172 ip
->i_gid
|= ip
->i_e2fs_gid_high
<< 16;
173 ip
->i_uid
|= ip
->i_e2fs_uid_high
<< 16;
178 ext2fs_modcmd(modcmd_t cmd
, void *arg
)
183 case MODULE_CMD_INIT
:
184 error
= vfs_attach(&ext2fs_vfsops
);
187 sysctl_createv(&ext2fs_sysctl_log
, 0, NULL
, NULL
,
189 CTLTYPE_NODE
, "vfs", NULL
,
192 sysctl_createv(&ext2fs_sysctl_log
, 0, NULL
, NULL
,
194 CTLTYPE_NODE
, "ext2fs",
195 SYSCTL_DESCR("Linux EXT2FS file system"),
197 CTL_VFS
, 17, CTL_EOL
);
199 * XXX the "17" above could be dynamic, thereby eliminating
200 * one more instance of the "number to vfs" mapping problem,
201 * but "17" is the order as taken from sys/mount.h
204 case MODULE_CMD_FINI
:
205 error
= vfs_detach(&ext2fs_vfsops
);
208 sysctl_teardown(&ext2fs_sysctl_log
);
219 * XXX Same structure as FFS inodes? Should we share a common pool?
221 struct pool ext2fs_inode_pool
;
222 struct pool ext2fs_dinode_pool
;
224 extern u_long ext2gennumber
;
230 pool_init(&ext2fs_inode_pool
, sizeof(struct inode
), 0, 0, 0,
231 "ext2fsinopl", &pool_allocator_nointr
, IPL_NONE
);
232 pool_init(&ext2fs_dinode_pool
, sizeof(struct ext2fs_dinode
), 0, 0, 0,
233 "ext2dinopl", &pool_allocator_nointr
, IPL_NONE
);
248 pool_destroy(&ext2fs_inode_pool
);
249 pool_destroy(&ext2fs_dinode_pool
);
253 * Called by main() when ext2fs is going to be mounted as root.
255 * Name is updated by mount(8) after booting.
257 #define ROOTNAME "root_device"
260 ext2fs_mountroot(void)
262 extern struct vnode
*rootvp
;
265 struct ufsmount
*ump
;
268 if (device_class(root_device
) != DV_DISK
)
271 if ((error
= vfs_rootmountalloc(MOUNT_EXT2FS
, "root_device", &mp
))) {
276 if ((error
= ext2fs_mountfs(rootvp
, mp
)) != 0) {
277 vfs_unbusy(mp
, false, NULL
);
281 mountlist_append(mp
);
284 memset(fs
->e2fs_fsmnt
, 0, sizeof(fs
->e2fs_fsmnt
));
285 (void) copystr(mp
->mnt_stat
.f_mntonname
, fs
->e2fs_fsmnt
,
286 sizeof(fs
->e2fs_fsmnt
) - 1, 0);
287 if (fs
->e2fs
.e2fs_rev
> E2FS_REV0
) {
288 memset(fs
->e2fs
.e2fs_fsmnt
, 0, sizeof(fs
->e2fs
.e2fs_fsmnt
));
289 (void) copystr(mp
->mnt_stat
.f_mntonname
, fs
->e2fs
.e2fs_fsmnt
,
290 sizeof(fs
->e2fs
.e2fs_fsmnt
) - 1, 0);
292 (void)ext2fs_statvfs(mp
, &mp
->mnt_stat
);
293 vfs_unbusy(mp
, false, NULL
);
294 setrootfstime((time_t)fs
->e2fs
.e2fs_wtime
);
304 ext2fs_mount(struct mount
*mp
, const char *path
, void *data
, size_t *data_len
)
306 struct lwp
*l
= curlwp
;
308 struct ufs_args
*args
= data
;
309 struct ufsmount
*ump
= NULL
;
312 int error
= 0, flags
, update
;
315 if (*data_len
< sizeof *args
)
318 if (mp
->mnt_flag
& MNT_GETARGS
) {
322 memset(args
, 0, sizeof *args
);
324 *data_len
= sizeof *args
;
328 update
= mp
->mnt_flag
& MNT_UPDATE
;
330 /* Check arguments */
331 if (args
->fspec
!= NULL
) {
333 * Look up the name and verify that it's sane.
335 error
= namei_simple_user(args
->fspec
,
336 NSM_FOLLOW_NOEMULROOT
, &devvp
);
342 * Be sure this is a valid block device
344 if (devvp
->v_type
!= VBLK
)
346 else if (bdevsw_lookup(devvp
->v_rdev
) == NULL
)
350 * Be sure we're still naming the same device
351 * used for our initial mount
354 if (devvp
!= ump
->um_devvp
) {
355 if (devvp
->v_rdev
!= ump
->um_devvp
->v_rdev
)
359 devvp
= ump
->um_devvp
;
366 /* New mounts must have a filename for the device */
370 devvp
= ump
->um_devvp
;
376 * If mount by non-root, then verify that user has necessary
377 * permissions on the device.
379 * Permission to update a mount is checked higher, so here we presume
380 * updating the mount is okay (for example, as far as securelevel goes)
381 * which leaves us with the normal check.
386 (mp
->mnt_iflag
& IMNT_WANTRDWR
) != 0 :
387 (mp
->mnt_flag
& MNT_RDONLY
) == 0)
388 accessmode
|= VWRITE
;
389 vn_lock(devvp
, LK_EXCLUSIVE
| LK_RETRY
);
390 error
= kauth_authorize_system(l
->l_cred
, KAUTH_SYSTEM_MOUNT
,
391 KAUTH_REQ_SYSTEM_MOUNT_DEVICE
, mp
, devvp
,
392 KAUTH_ARG(accessmode
));
404 if (mp
->mnt_flag
& MNT_RDONLY
)
407 xflags
= FREAD
|FWRITE
;
408 vn_lock(devvp
, LK_EXCLUSIVE
| LK_RETRY
);
409 error
= VOP_OPEN(devvp
, xflags
, FSCRED
);
413 error
= ext2fs_mountfs(devvp
, mp
);
415 vn_lock(devvp
, LK_EXCLUSIVE
| LK_RETRY
);
416 (void)VOP_CLOSE(devvp
, xflags
, NOCRED
);
429 * The initial mount got a reference on this
430 * device, so drop the one obtained via
437 if (fs
->e2fs_ronly
== 0 && (mp
->mnt_flag
& MNT_RDONLY
)) {
439 * Changing from r/w to r/o
442 if (mp
->mnt_flag
& MNT_FORCE
)
444 error
= ext2fs_flushfiles(mp
, flags
);
446 ext2fs_cgupdate(ump
, MNT_WAIT
) == 0 &&
447 (fs
->e2fs
.e2fs_state
& E2FS_ERRORS
) == 0) {
448 fs
->e2fs
.e2fs_state
= E2FS_ISCLEAN
;
449 (void) ext2fs_sbupdate(ump
, MNT_WAIT
);
456 if (mp
->mnt_flag
& MNT_RELOAD
) {
457 error
= ext2fs_reload(mp
, l
->l_cred
, l
);
462 if (fs
->e2fs_ronly
&& (mp
->mnt_iflag
& IMNT_WANTRDWR
)) {
464 * Changing from read-only to read/write
467 if (fs
->e2fs
.e2fs_state
== E2FS_ISCLEAN
)
468 fs
->e2fs
.e2fs_state
= 0;
470 fs
->e2fs
.e2fs_state
= E2FS_ERRORS
;
473 if (args
->fspec
== NULL
)
477 error
= set_statvfs_info(path
, UIO_USERSPACE
, args
->fspec
,
478 UIO_USERSPACE
, mp
->mnt_op
->vfs_name
, mp
, l
);
479 (void) copystr(mp
->mnt_stat
.f_mntonname
, fs
->e2fs_fsmnt
,
480 sizeof(fs
->e2fs_fsmnt
) - 1, &size
);
481 memset(fs
->e2fs_fsmnt
+ size
, 0, sizeof(fs
->e2fs_fsmnt
) - size
);
482 if (fs
->e2fs
.e2fs_rev
> E2FS_REV0
) {
483 (void) copystr(mp
->mnt_stat
.f_mntonname
, fs
->e2fs
.e2fs_fsmnt
,
484 sizeof(fs
->e2fs
.e2fs_fsmnt
) - 1, &size
);
485 memset(fs
->e2fs
.e2fs_fsmnt
, 0,
486 sizeof(fs
->e2fs
.e2fs_fsmnt
) - size
);
488 if (fs
->e2fs_fmod
!= 0) { /* XXX */
490 if (fs
->e2fs
.e2fs_state
== 0)
491 fs
->e2fs
.e2fs_wtime
= time_second
;
493 printf("%s: file system not clean; please fsck(8)\n",
494 mp
->mnt_stat
.f_mntfromname
);
495 (void) ext2fs_cgupdate(ump
, MNT_WAIT
);
505 * Reload all incore data for a filesystem (used after running fsck on
506 * the root filesystem and finding things to fix). The filesystem must
507 * be mounted read-only.
509 * Things to do to update the mount:
510 * 1) invalidate all cached meta-data.
511 * 2) re-read superblock from disk.
512 * 3) re-read summary information from disk.
513 * 4) invalidate all inactive vnodes.
514 * 5) invalidate all cached file data.
515 * 6) re-read inode data for all active vnodes.
518 ext2fs_reload(struct mount
*mp
, kauth_cred_t cred
, struct lwp
*l
)
520 struct vnode
*vp
, *mvp
, *devvp
;
524 struct ext2fs
*newfs
;
527 struct ufsmount
*ump
;
529 if ((mp
->mnt_flag
& MNT_RDONLY
) == 0)
534 * Step 1: invalidate all cached meta-data.
536 devvp
= ump
->um_devvp
;
537 vn_lock(devvp
, LK_EXCLUSIVE
| LK_RETRY
);
538 error
= vinvalbuf(devvp
, 0, cred
, l
, 0, 0);
541 panic("ext2fs_reload: dirty1");
543 * Step 2: re-read superblock from disk.
545 error
= bread(devvp
, SBLOCK
, SBSIZE
, NOCRED
, 0, &bp
);
549 newfs
= (struct ext2fs
*)bp
->b_data
;
550 error
= ext2fs_checksb(newfs
, (mp
->mnt_flag
& MNT_RDONLY
) != 0);
558 * copy in new superblock, and compute in-memory values
560 e2fs_sbload(newfs
, &fs
->e2fs
);
562 howmany(fs
->e2fs
.e2fs_bcount
- fs
->e2fs
.e2fs_first_dblock
,
564 fs
->e2fs_fsbtodb
= fs
->e2fs
.e2fs_log_bsize
+ LOG_MINBSIZE
- DEV_BSHIFT
;
565 fs
->e2fs_bsize
= MINBSIZE
<< fs
->e2fs
.e2fs_log_bsize
;
566 fs
->e2fs_bshift
= LOG_MINBSIZE
+ fs
->e2fs
.e2fs_log_bsize
;
567 fs
->e2fs_qbmask
= fs
->e2fs_bsize
- 1;
568 fs
->e2fs_bmask
= ~fs
->e2fs_qbmask
;
570 howmany(fs
->e2fs_ncg
, fs
->e2fs_bsize
/ sizeof(struct ext2_gd
));
571 fs
->e2fs_ipb
= fs
->e2fs_bsize
/ EXT2_DINODE_SIZE(fs
);
572 fs
->e2fs_itpg
= fs
->e2fs
.e2fs_ipg
/ fs
->e2fs_ipb
;
576 * Step 3: re-read summary information from disk.
579 for (i
= 0; i
< fs
->e2fs_ngdb
; i
++) {
580 error
= bread(devvp
,
581 EXT2_FSBTODB(fs
, fs
->e2fs
.e2fs_first_dblock
+
582 1 /* superblock */ + i
),
583 fs
->e2fs_bsize
, NOCRED
, 0, &bp
);
587 e2fs_cgload((struct ext2_gd
*)bp
->b_data
,
588 &fs
->e2fs_gd
[i
* fs
->e2fs_bsize
/ sizeof(struct ext2_gd
)],
593 /* Allocate a marker vnode. */
596 * NOTE: not using the TAILQ_FOREACH here since in this loop vgone()
597 * and vclean() can be called indirectly
599 mutex_enter(&mntvnode_lock
);
601 for (vp
= TAILQ_FIRST(&mp
->mnt_vnodelist
); vp
; vp
= vunmark(mvp
)) {
603 if (vp
->v_mount
!= mp
|| vismarker(vp
))
606 * Step 4: invalidate all inactive vnodes.
608 if (vrecycle(vp
, &mntvnode_lock
)) {
609 mutex_enter(&mntvnode_lock
);
614 * Step 5: invalidate all cached file data.
616 mutex_enter(vp
->v_interlock
);
617 mutex_exit(&mntvnode_lock
);
618 if (vget(vp
, LK_EXCLUSIVE
)) {
619 mutex_enter(&mntvnode_lock
);
623 if (vinvalbuf(vp
, 0, cred
, l
, 0, 0))
624 panic("ext2fs_reload: dirty2");
626 * Step 6: re-read inode data for all active vnodes.
629 error
= bread(devvp
, EXT2_FSBTODB(fs
, ino_to_fsba(fs
, ip
->i_number
)),
630 (int)fs
->e2fs_bsize
, NOCRED
, 0, &bp
);
633 mutex_enter(&mntvnode_lock
);
637 cp
= (char *)bp
->b_data
+
638 (ino_to_fsbo(fs
, ip
->i_number
) * EXT2_DINODE_SIZE(fs
));
639 e2fs_iload((struct ext2fs_dinode
*)cp
, ip
->i_din
.e2fs_din
);
640 ext2fs_set_inode_guid(ip
);
643 mutex_enter(&mntvnode_lock
);
645 mutex_exit(&mntvnode_lock
);
651 * Common code for mount and mountroot
654 ext2fs_mountfs(struct vnode
*devvp
, struct mount
*mp
)
656 struct lwp
*l
= curlwp
;
657 struct ufsmount
*ump
;
660 struct m_ext2fs
*m_fs
;
666 cred
= l
? l
->l_cred
: NOCRED
;
668 /* Flush out any old buffers remaining from a previous use. */
669 vn_lock(devvp
, LK_EXCLUSIVE
| LK_RETRY
);
670 error
= vinvalbuf(devvp
, V_SAVE
, cred
, l
, 0, 0);
675 ronly
= (mp
->mnt_flag
& MNT_RDONLY
) != 0;
681 printf("ext2 sb size: %zu\n", sizeof(struct ext2fs
));
683 error
= bread(devvp
, SBLOCK
, SBSIZE
, cred
, 0, &bp
);
686 fs
= (struct ext2fs
*)bp
->b_data
;
687 error
= ext2fs_checksb(fs
, ronly
);
690 ump
= kmem_zalloc(sizeof(*ump
), KM_SLEEP
);
691 ump
->um_fstype
= UFS1
;
692 ump
->um_ops
= &ext2fs_ufsops
;
693 ump
->um_e2fs
= kmem_zalloc(sizeof(struct m_ext2fs
), KM_SLEEP
);
694 e2fs_sbload((struct ext2fs
*)bp
->b_data
, &ump
->um_e2fs
->e2fs
);
698 m_fs
->e2fs_ronly
= ronly
;
701 printf("ext2 ino size %zu\n", EXT2_DINODE_SIZE(m_fs
));
704 if (m_fs
->e2fs
.e2fs_state
== E2FS_ISCLEAN
)
705 m_fs
->e2fs
.e2fs_state
= 0;
707 m_fs
->e2fs
.e2fs_state
= E2FS_ERRORS
;
711 /* compute dynamic sb infos */
713 howmany(m_fs
->e2fs
.e2fs_bcount
- m_fs
->e2fs
.e2fs_first_dblock
,
714 m_fs
->e2fs
.e2fs_bpg
);
715 m_fs
->e2fs_fsbtodb
= m_fs
->e2fs
.e2fs_log_bsize
+ LOG_MINBSIZE
- DEV_BSHIFT
;
716 m_fs
->e2fs_bsize
= MINBSIZE
<< m_fs
->e2fs
.e2fs_log_bsize
;
717 m_fs
->e2fs_bshift
= LOG_MINBSIZE
+ m_fs
->e2fs
.e2fs_log_bsize
;
718 m_fs
->e2fs_qbmask
= m_fs
->e2fs_bsize
- 1;
719 m_fs
->e2fs_bmask
= ~m_fs
->e2fs_qbmask
;
721 howmany(m_fs
->e2fs_ncg
, m_fs
->e2fs_bsize
/ sizeof(struct ext2_gd
));
722 m_fs
->e2fs_ipb
= m_fs
->e2fs_bsize
/ EXT2_DINODE_SIZE(m_fs
);
723 m_fs
->e2fs_itpg
= m_fs
->e2fs
.e2fs_ipg
/ m_fs
->e2fs_ipb
;
725 m_fs
->e2fs_gd
= kmem_alloc(m_fs
->e2fs_ngdb
* m_fs
->e2fs_bsize
, KM_SLEEP
);
726 for (i
= 0; i
< m_fs
->e2fs_ngdb
; i
++) {
727 error
= bread(devvp
,
728 EXT2_FSBTODB(m_fs
, m_fs
->e2fs
.e2fs_first_dblock
+
729 1 /* superblock */ + i
),
730 m_fs
->e2fs_bsize
, NOCRED
, 0, &bp
);
732 kmem_free(m_fs
->e2fs_gd
,
733 m_fs
->e2fs_ngdb
* m_fs
->e2fs_bsize
);
736 e2fs_cgload((struct ext2_gd
*)bp
->b_data
,
738 i
* m_fs
->e2fs_bsize
/ sizeof(struct ext2_gd
)],
745 mp
->mnt_stat
.f_fsidx
.__fsid_val
[0] = (long)dev
;
746 mp
->mnt_stat
.f_fsidx
.__fsid_val
[1] = makefstype(MOUNT_EXT2FS
);
747 mp
->mnt_stat
.f_fsid
= mp
->mnt_stat
.f_fsidx
.__fsid_val
[0];
748 mp
->mnt_stat
.f_namemax
= EXT2FS_MAXNAMLEN
;
749 mp
->mnt_flag
|= MNT_LOCAL
;
750 mp
->mnt_dev_bshift
= DEV_BSHIFT
; /* XXX */
751 mp
->mnt_fs_bshift
= m_fs
->e2fs_bshift
;
752 mp
->mnt_iflag
|= IMNT_DTYPE
;
756 ump
->um_devvp
= devvp
;
757 ump
->um_nindir
= EXT2_NINDIR(m_fs
);
758 ump
->um_lognindir
= ffs(EXT2_NINDIR(m_fs
)) - 1;
759 ump
->um_bptrtodb
= m_fs
->e2fs_fsbtodb
;
760 ump
->um_seqinc
= 1; /* no frags */
761 ump
->um_maxsymlinklen
= EXT2_MAXSYMLINKLEN
;
762 ump
->um_dirblksiz
= m_fs
->e2fs_bsize
;
763 ump
->um_maxfilesize
= ((uint64_t)0x80000000 * m_fs
->e2fs_bsize
- 1);
764 spec_node_setmountedfs(devvp
, mp
);
771 kmem_free(ump
->um_e2fs
, sizeof(struct m_ext2fs
));
772 kmem_free(ump
, sizeof(*ump
));
779 * unmount system call
782 ext2fs_unmount(struct mount
*mp
, int mntflags
)
784 struct ufsmount
*ump
;
789 if (mntflags
& MNT_FORCE
)
791 if ((error
= ext2fs_flushfiles(mp
, flags
)) != 0)
795 if (fs
->e2fs_ronly
== 0 &&
796 ext2fs_cgupdate(ump
, MNT_WAIT
) == 0 &&
797 (fs
->e2fs
.e2fs_state
& E2FS_ERRORS
) == 0) {
798 fs
->e2fs
.e2fs_state
= E2FS_ISCLEAN
;
799 (void) ext2fs_sbupdate(ump
, MNT_WAIT
);
801 if (ump
->um_devvp
->v_type
!= VBAD
)
802 spec_node_setmountedfs(ump
->um_devvp
, NULL
);
803 vn_lock(ump
->um_devvp
, LK_EXCLUSIVE
| LK_RETRY
);
804 error
= VOP_CLOSE(ump
->um_devvp
, fs
->e2fs_ronly
? FREAD
: FREAD
|FWRITE
,
807 kmem_free(fs
->e2fs_gd
, fs
->e2fs_ngdb
* fs
->e2fs_bsize
);
808 kmem_free(fs
, sizeof(*fs
));
809 kmem_free(ump
, sizeof(*ump
));
811 mp
->mnt_flag
&= ~MNT_LOCAL
;
816 * Flush out all the files in a filesystem.
819 ext2fs_flushfiles(struct mount
*mp
, int flags
)
825 flags
&= ~FORCECLOSE
;
826 error
= vflush(mp
, NULLVP
, flags
);
831 * Get file system statistics.
834 ext2fs_statvfs(struct mount
*mp
, struct statvfs
*sbp
)
836 struct ufsmount
*ump
;
838 uint32_t overhead
, overhead_per_group
, ngdb
;
843 if (fs
->e2fs
.e2fs_magic
!= E2FS_MAGIC
)
844 panic("ext2fs_statvfs");
847 * Compute the overhead (FS structures)
850 1 /* block bitmap */ +
851 1 /* inode bitmap */ +
853 overhead
= fs
->e2fs
.e2fs_first_dblock
+
854 fs
->e2fs_ncg
* overhead_per_group
;
855 if (fs
->e2fs
.e2fs_rev
> E2FS_REV0
&&
856 fs
->e2fs
.e2fs_features_rocompat
& EXT2F_ROCOMPAT_SPARSESUPER
) {
857 for (i
= 0, ngroups
= 0; i
< fs
->e2fs_ncg
; i
++) {
862 ngroups
= fs
->e2fs_ncg
;
864 ngdb
= fs
->e2fs_ngdb
;
865 if (fs
->e2fs
.e2fs_rev
> E2FS_REV0
&&
866 fs
->e2fs
.e2fs_features_compat
& EXT2F_COMPAT_RESIZE
)
867 ngdb
+= fs
->e2fs
.e2fs_reserved_ngdb
;
868 overhead
+= ngroups
* (1 /* superblock */ + ngdb
);
870 sbp
->f_bsize
= fs
->e2fs_bsize
;
871 sbp
->f_frsize
= MINBSIZE
<< fs
->e2fs
.e2fs_fsize
;
872 sbp
->f_iosize
= fs
->e2fs_bsize
;
873 sbp
->f_blocks
= fs
->e2fs
.e2fs_bcount
- overhead
;
874 sbp
->f_bfree
= fs
->e2fs
.e2fs_fbcount
;
875 sbp
->f_bresvd
= fs
->e2fs
.e2fs_rbcount
;
876 if (sbp
->f_bfree
> sbp
->f_bresvd
)
877 sbp
->f_bavail
= sbp
->f_bfree
- sbp
->f_bresvd
;
880 sbp
->f_files
= fs
->e2fs
.e2fs_icount
;
881 sbp
->f_ffree
= fs
->e2fs
.e2fs_ficount
;
882 sbp
->f_favail
= fs
->e2fs
.e2fs_ficount
;
884 copy_statvfs_info(sbp
, mp
);
889 * Go through the disk queues to initiate sandbagged IO;
890 * go through the inodes to write those that have been modified;
891 * initiate the writing of the super block if it has been modified.
893 * Note: we are always called with the filesystem marked `MPBUSY'.
896 ext2fs_sync(struct mount
*mp
, int waitfor
, kauth_cred_t cred
)
898 struct vnode
*vp
, *mvp
;
900 struct ufsmount
*ump
= VFSTOUFS(mp
);
902 int error
, allerror
= 0;
905 if (fs
->e2fs_fmod
!= 0 && fs
->e2fs_ronly
!= 0) { /* XXX */
906 printf("fs = %s\n", fs
->e2fs_fsmnt
);
907 panic("update: rofs mod");
910 /* Allocate a marker vnode. */
914 * Write back each (modified) inode.
916 mutex_enter(&mntvnode_lock
);
919 * NOTE: not using the TAILQ_FOREACH here since in this loop vgone()
920 * and vclean() can be called indirectly
922 for (vp
= TAILQ_FIRST(&mp
->mnt_vnodelist
); vp
; vp
= vunmark(mvp
)) {
924 if (vp
->v_mount
!= mp
|| vismarker(vp
))
926 mutex_enter(vp
->v_interlock
);
928 if (ip
== NULL
|| (vp
->v_iflag
& (VI_XLOCK
|VI_CLEAN
)) != 0 ||
929 vp
->v_type
== VNON
||
931 (IN_CHANGE
| IN_UPDATE
| IN_MODIFIED
)) == 0 &&
932 LIST_EMPTY(&vp
->v_dirtyblkhd
) &&
933 UVM_OBJ_IS_CLEAN(&vp
->v_uobj
)))
935 mutex_exit(vp
->v_interlock
);
938 mutex_exit(&mntvnode_lock
);
939 error
= vget(vp
, LK_EXCLUSIVE
| LK_NOWAIT
);
941 mutex_enter(&mntvnode_lock
);
942 if (error
== ENOENT
) {
943 mutex_enter(&mntvnode_lock
);
949 if (vp
->v_type
== VREG
&& waitfor
== MNT_LAZY
)
950 error
= ext2fs_update(vp
, NULL
, NULL
, 0);
952 error
= VOP_FSYNC(vp
, cred
,
953 waitfor
== MNT_WAIT
? FSYNC_WAIT
: 0, 0, 0);
957 mutex_enter(&mntvnode_lock
);
959 mutex_exit(&mntvnode_lock
);
962 * Force stale file system control information to be flushed.
964 if (waitfor
!= MNT_LAZY
) {
965 vn_lock(ump
->um_devvp
, LK_EXCLUSIVE
| LK_RETRY
);
966 if ((error
= VOP_FSYNC(ump
->um_devvp
, cred
,
967 waitfor
== MNT_WAIT
? FSYNC_WAIT
: 0, 0, 0)) != 0)
969 VOP_UNLOCK(ump
->um_devvp
);
972 * Write back modified superblock.
974 if (fs
->e2fs_fmod
!= 0) {
976 fs
->e2fs
.e2fs_wtime
= time_second
;
977 if ((error
= ext2fs_cgupdate(ump
, waitfor
)))
984 * Look up a EXT2FS dinode number to find its incore vnode, otherwise read it
985 * in from disk. If it is in core, wait for the lock bit to clear, then
986 * return the inode locked. Detection and handling of mount points must be
987 * done by the calling routine.
990 ext2fs_vget(struct mount
*mp
, ino_t ino
, struct vnode
**vpp
)
994 struct ufsmount
*ump
;
1004 if ((*vpp
= ufs_ihashget(dev
, ino
, LK_EXCLUSIVE
)) != NULL
)
1007 /* Allocate a new vnode/inode. */
1008 error
= getnewvnode(VT_EXT2FS
, mp
, ext2fs_vnodeop_p
, NULL
, &vp
);
1013 ip
= pool_get(&ext2fs_inode_pool
, PR_WAITOK
);
1015 mutex_enter(&ufs_hashlock
);
1016 if ((*vpp
= ufs_ihashget(dev
, ino
, 0)) != NULL
) {
1017 mutex_exit(&ufs_hashlock
);
1019 pool_put(&ext2fs_inode_pool
, ip
);
1023 vp
->v_vflag
|= VV_LOCKSWORK
;
1025 memset(ip
, 0, sizeof(struct inode
));
1029 ip
->i_e2fs
= fs
= ump
->um_e2fs
;
1032 ip
->i_e2fs_last_lblk
= 0;
1033 ip
->i_e2fs_last_blk
= 0;
1034 genfs_node_init(vp
, &ext2fs_genfsops
);
1037 * Put it onto its hash chain and lock it so that other requests for
1038 * this inode will block if they arrive while we are sleeping waiting
1039 * for old data structures to be purged or for the contents of the
1040 * disk portion of this inode to be read.
1044 mutex_exit(&ufs_hashlock
);
1046 /* Read in the disk contents for the inode, copy into the inode. */
1047 error
= bread(ump
->um_devvp
, EXT2_FSBTODB(fs
, ino_to_fsba(fs
, ino
)),
1048 (int)fs
->e2fs_bsize
, NOCRED
, 0, &bp
);
1052 * The inode does not contain anything useful, so it would
1053 * be misleading to leave it on its hash chain. With mode
1054 * still zero, it will be unlinked and returned to the free
1062 cp
= (char *)bp
->b_data
+ (ino_to_fsbo(fs
, ino
) * EXT2_DINODE_SIZE(fs
));
1063 ip
->i_din
.e2fs_din
= pool_get(&ext2fs_dinode_pool
, PR_WAITOK
);
1064 e2fs_iload((struct ext2fs_dinode
*)cp
, ip
->i_din
.e2fs_din
);
1065 ext2fs_set_inode_guid(ip
);
1068 /* If the inode was deleted, reset all fields */
1069 if (ip
->i_e2fs_dtime
!= 0) {
1070 ip
->i_e2fs_mode
= 0;
1071 (void)ext2fs_setsize(ip
, 0);
1072 (void)ext2fs_setnblock(ip
, 0);
1073 memset(ip
->i_e2fs_blocks
, 0, sizeof(ip
->i_e2fs_blocks
));
1077 * Initialize the vnode from the inode, check for aliases.
1080 error
= ext2fs_vinit(mp
, ext2fs_specop_p
, ext2fs_fifoop_p
, &vp
);
1087 * Finish inode initialization now that aliasing has been resolved.
1090 ip
->i_devvp
= ump
->um_devvp
;
1094 * Set up a generation number for this inode if it does not
1095 * already have one. This should only happen on old filesystems.
1098 if (ip
->i_e2fs_gen
== 0) {
1099 if (++ext2gennumber
< (u_long
)time_second
)
1100 ext2gennumber
= time_second
;
1101 ip
->i_e2fs_gen
= ext2gennumber
;
1102 if ((vp
->v_mount
->mnt_flag
& MNT_RDONLY
) == 0)
1103 ip
->i_flag
|= IN_MODIFIED
;
1105 uvm_vnp_setsize(vp
, ext2fs_size(ip
));
1111 * File handle to vnode
1113 * Have to be really careful about stale file handles:
1114 * - check that the inode number is valid
1115 * - call ext2fs_vget() to get the locked inode
1116 * - check for an unallocated inode (i_mode == 0)
1119 ext2fs_fhtovp(struct mount
*mp
, struct fid
*fhp
, struct vnode
**vpp
)
1125 struct m_ext2fs
*fs
;
1127 if (fhp
->fid_len
!= sizeof(struct ufid
))
1130 memcpy(&ufh
, fhp
, sizeof(struct ufid
));
1131 fs
= VFSTOUFS(mp
)->um_e2fs
;
1132 if ((ufh
.ufid_ino
< EXT2_FIRSTINO
&& ufh
.ufid_ino
!= EXT2_ROOTINO
) ||
1133 ufh
.ufid_ino
>= fs
->e2fs_ncg
* fs
->e2fs
.e2fs_ipg
)
1136 if ((error
= VFS_VGET(mp
, ufh
.ufid_ino
, &nvp
)) != 0) {
1141 if (ip
->i_e2fs_mode
== 0 || ip
->i_e2fs_dtime
!= 0 ||
1142 ip
->i_e2fs_gen
!= ufh
.ufid_gen
) {
1152 * Vnode pointer to File handle
1156 ext2fs_vptofh(struct vnode
*vp
, struct fid
*fhp
, size_t *fh_size
)
1161 if (*fh_size
< sizeof(struct ufid
)) {
1162 *fh_size
= sizeof(struct ufid
);
1165 *fh_size
= sizeof(struct ufid
);
1168 memset(&ufh
, 0, sizeof(ufh
));
1169 ufh
.ufid_len
= sizeof(struct ufid
);
1170 ufh
.ufid_ino
= ip
->i_number
;
1171 ufh
.ufid_gen
= ip
->i_e2fs_gen
;
1172 memcpy(fhp
, &ufh
, sizeof(ufh
));
1177 * Write a superblock and associated information back to disk.
1180 ext2fs_sbupdate(struct ufsmount
*mp
, int waitfor
)
1182 struct m_ext2fs
*fs
= mp
->um_e2fs
;
1186 bp
= getblk(mp
->um_devvp
, SBLOCK
, SBSIZE
, 0, 0);
1187 e2fs_sbsave(&fs
->e2fs
, (struct ext2fs
*)bp
->b_data
);
1188 if (waitfor
== MNT_WAIT
)
1196 ext2fs_cgupdate(struct ufsmount
*mp
, int waitfor
)
1198 struct m_ext2fs
*fs
= mp
->um_e2fs
;
1200 int i
, error
= 0, allerror
= 0;
1202 allerror
= ext2fs_sbupdate(mp
, waitfor
);
1203 for (i
= 0; i
< fs
->e2fs_ngdb
; i
++) {
1204 bp
= getblk(mp
->um_devvp
, EXT2_FSBTODB(fs
,
1205 fs
->e2fs
.e2fs_first_dblock
+
1206 1 /* superblock */ + i
), fs
->e2fs_bsize
, 0, 0);
1207 e2fs_cgsave(&fs
->e2fs_gd
[
1208 i
* fs
->e2fs_bsize
/ sizeof(struct ext2_gd
)],
1209 (struct ext2_gd
*)bp
->b_data
, fs
->e2fs_bsize
);
1210 if (waitfor
== MNT_WAIT
)
1216 if (!allerror
&& error
)
1222 ext2fs_checksb(struct ext2fs
*fs
, int ronly
)
1226 if (fs2h16(fs
->e2fs_magic
) != E2FS_MAGIC
) {
1227 return (EINVAL
); /* XXX needs translation */
1229 if (fs2h32(fs
->e2fs_rev
) > E2FS_REV1
) {
1231 printf("ext2fs: unsupported revision number: %x\n",
1232 fs2h32(fs
->e2fs_rev
));
1234 return (EINVAL
); /* XXX needs translation */
1236 if (fs2h32(fs
->e2fs_log_bsize
) > 2) { /* block size = 1024|2048|4096 */
1238 printf("ext2fs: bad block size: %d "
1239 "(expected <= 2 for ext2 fs)\n",
1240 fs2h32(fs
->e2fs_log_bsize
));
1242 return (EINVAL
); /* XXX needs translation */
1244 if (fs2h32(fs
->e2fs_rev
) > E2FS_REV0
) {
1246 if (fs2h32(fs
->e2fs_first_ino
) != EXT2_FIRSTINO
) {
1247 printf("ext2fs: unsupported first inode position\n");
1248 return (EINVAL
); /* XXX needs translation */
1250 u32
= fs2h32(fs
->e2fs_features_incompat
) & ~EXT2F_INCOMPAT_SUPP
;
1252 snprintb(buf
, sizeof(buf
), EXT2F_INCOMPAT_BITS
, u32
);
1253 printf("ext2fs: unsupported incompat features: %s\n",
1255 return EINVAL
; /* XXX needs translation */
1257 u32
= fs2h32(fs
->e2fs_features_rocompat
) & ~EXT2F_ROCOMPAT_SUPP
;
1258 if (!ronly
&& u32
) {
1259 snprintb(buf
, sizeof(buf
), EXT2F_ROCOMPAT_BITS
, u32
);
1260 printf("ext2fs: unsupported ro-incompat features: %s\n",
1262 return EROFS
; /* XXX needs translation */