1 /* $NetBSD: ext2fs_vfsops.c,v 1.162 2011/11/14 18:35:14 hannken 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.162 2011/11/14 18:35:14 hannken 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
,
162 .uo_unmark_vnode
= (void (*)(vnode_t
*))nullop
,
165 /* Fill in the inode uid/gid from ext2 halves. */
167 ext2fs_set_inode_guid(struct inode
*ip
)
170 ip
->i_gid
= ip
->i_e2fs_gid
;
171 ip
->i_uid
= ip
->i_e2fs_uid
;
172 if (ip
->i_e2fs
->e2fs
.e2fs_rev
> E2FS_REV0
) {
173 ip
->i_gid
|= ip
->i_e2fs_gid_high
<< 16;
174 ip
->i_uid
|= ip
->i_e2fs_uid_high
<< 16;
179 ext2fs_modcmd(modcmd_t cmd
, void *arg
)
184 case MODULE_CMD_INIT
:
185 error
= vfs_attach(&ext2fs_vfsops
);
188 sysctl_createv(&ext2fs_sysctl_log
, 0, NULL
, NULL
,
190 CTLTYPE_NODE
, "vfs", NULL
,
193 sysctl_createv(&ext2fs_sysctl_log
, 0, NULL
, NULL
,
195 CTLTYPE_NODE
, "ext2fs",
196 SYSCTL_DESCR("Linux EXT2FS file system"),
198 CTL_VFS
, 17, CTL_EOL
);
200 * XXX the "17" above could be dynamic, thereby eliminating
201 * one more instance of the "number to vfs" mapping problem,
202 * but "17" is the order as taken from sys/mount.h
205 case MODULE_CMD_FINI
:
206 error
= vfs_detach(&ext2fs_vfsops
);
209 sysctl_teardown(&ext2fs_sysctl_log
);
220 * XXX Same structure as FFS inodes? Should we share a common pool?
222 struct pool ext2fs_inode_pool
;
223 struct pool ext2fs_dinode_pool
;
225 extern u_long ext2gennumber
;
231 pool_init(&ext2fs_inode_pool
, sizeof(struct inode
), 0, 0, 0,
232 "ext2fsinopl", &pool_allocator_nointr
, IPL_NONE
);
233 pool_init(&ext2fs_dinode_pool
, sizeof(struct ext2fs_dinode
), 0, 0, 0,
234 "ext2dinopl", &pool_allocator_nointr
, IPL_NONE
);
249 pool_destroy(&ext2fs_inode_pool
);
250 pool_destroy(&ext2fs_dinode_pool
);
254 * Called by main() when ext2fs is going to be mounted as root.
256 * Name is updated by mount(8) after booting.
258 #define ROOTNAME "root_device"
261 ext2fs_mountroot(void)
263 extern struct vnode
*rootvp
;
266 struct ufsmount
*ump
;
269 if (device_class(root_device
) != DV_DISK
)
272 if ((error
= vfs_rootmountalloc(MOUNT_EXT2FS
, "root_device", &mp
))) {
277 if ((error
= ext2fs_mountfs(rootvp
, mp
)) != 0) {
278 vfs_unbusy(mp
, false, NULL
);
282 mutex_enter(&mountlist_lock
);
283 CIRCLEQ_INSERT_TAIL(&mountlist
, mp
, mnt_list
);
284 mutex_exit(&mountlist_lock
);
287 memset(fs
->e2fs_fsmnt
, 0, sizeof(fs
->e2fs_fsmnt
));
288 (void) copystr(mp
->mnt_stat
.f_mntonname
, fs
->e2fs_fsmnt
,
289 sizeof(fs
->e2fs_fsmnt
) - 1, 0);
290 if (fs
->e2fs
.e2fs_rev
> E2FS_REV0
) {
291 memset(fs
->e2fs
.e2fs_fsmnt
, 0, sizeof(fs
->e2fs
.e2fs_fsmnt
));
292 (void) copystr(mp
->mnt_stat
.f_mntonname
, fs
->e2fs
.e2fs_fsmnt
,
293 sizeof(fs
->e2fs
.e2fs_fsmnt
) - 1, 0);
295 (void)ext2fs_statvfs(mp
, &mp
->mnt_stat
);
296 vfs_unbusy(mp
, false, NULL
);
297 setrootfstime((time_t)fs
->e2fs
.e2fs_wtime
);
307 ext2fs_mount(struct mount
*mp
, const char *path
, void *data
, size_t *data_len
)
309 struct lwp
*l
= curlwp
;
311 struct ufs_args
*args
= data
;
312 struct ufsmount
*ump
= NULL
;
315 int error
= 0, flags
, update
;
318 if (*data_len
< sizeof *args
)
321 if (mp
->mnt_flag
& MNT_GETARGS
) {
325 memset(args
, 0, sizeof *args
);
327 *data_len
= sizeof *args
;
331 update
= mp
->mnt_flag
& MNT_UPDATE
;
333 /* Check arguments */
334 if (args
->fspec
!= NULL
) {
336 * Look up the name and verify that it's sane.
338 error
= namei_simple_user(args
->fspec
,
339 NSM_FOLLOW_NOEMULROOT
, &devvp
);
345 * Be sure this is a valid block device
347 if (devvp
->v_type
!= VBLK
)
349 else if (bdevsw_lookup(devvp
->v_rdev
) == NULL
)
353 * Be sure we're still naming the same device
354 * used for our initial mount
357 if (devvp
!= ump
->um_devvp
) {
358 if (devvp
->v_rdev
!= ump
->um_devvp
->v_rdev
)
362 devvp
= ump
->um_devvp
;
369 /* New mounts must have a filename for the device */
373 devvp
= ump
->um_devvp
;
379 * If mount by non-root, then verify that user has necessary
380 * permissions on the device.
382 * Permission to update a mount is checked higher, so here we presume
383 * updating the mount is okay (for example, as far as securelevel goes)
384 * which leaves us with the normal check.
389 (mp
->mnt_iflag
& IMNT_WANTRDWR
) != 0 :
390 (mp
->mnt_flag
& MNT_RDONLY
) == 0)
391 accessmode
|= VWRITE
;
392 vn_lock(devvp
, LK_EXCLUSIVE
| LK_RETRY
);
393 error
= genfs_can_mount(devvp
, accessmode
, l
->l_cred
);
405 if (mp
->mnt_flag
& MNT_RDONLY
)
408 xflags
= FREAD
|FWRITE
;
409 vn_lock(devvp
, LK_EXCLUSIVE
| LK_RETRY
);
410 error
= VOP_OPEN(devvp
, xflags
, FSCRED
);
414 error
= ext2fs_mountfs(devvp
, mp
);
416 vn_lock(devvp
, LK_EXCLUSIVE
| LK_RETRY
);
417 (void)VOP_CLOSE(devvp
, xflags
, NOCRED
);
430 * The initial mount got a reference on this
431 * device, so drop the one obtained via
438 if (fs
->e2fs_ronly
== 0 && (mp
->mnt_flag
& MNT_RDONLY
)) {
440 * Changing from r/w to r/o
443 if (mp
->mnt_flag
& MNT_FORCE
)
445 error
= ext2fs_flushfiles(mp
, flags
);
447 ext2fs_cgupdate(ump
, MNT_WAIT
) == 0 &&
448 (fs
->e2fs
.e2fs_state
& E2FS_ERRORS
) == 0) {
449 fs
->e2fs
.e2fs_state
= E2FS_ISCLEAN
;
450 (void) ext2fs_sbupdate(ump
, MNT_WAIT
);
457 if (mp
->mnt_flag
& MNT_RELOAD
) {
458 error
= ext2fs_reload(mp
, l
->l_cred
, l
);
463 if (fs
->e2fs_ronly
&& (mp
->mnt_iflag
& IMNT_WANTRDWR
)) {
465 * Changing from read-only to read/write
468 if (fs
->e2fs
.e2fs_state
== E2FS_ISCLEAN
)
469 fs
->e2fs
.e2fs_state
= 0;
471 fs
->e2fs
.e2fs_state
= E2FS_ERRORS
;
474 if (args
->fspec
== NULL
)
478 error
= set_statvfs_info(path
, UIO_USERSPACE
, args
->fspec
,
479 UIO_USERSPACE
, mp
->mnt_op
->vfs_name
, mp
, l
);
480 (void) copystr(mp
->mnt_stat
.f_mntonname
, fs
->e2fs_fsmnt
,
481 sizeof(fs
->e2fs_fsmnt
) - 1, &size
);
482 memset(fs
->e2fs_fsmnt
+ size
, 0, sizeof(fs
->e2fs_fsmnt
) - size
);
483 if (fs
->e2fs
.e2fs_rev
> E2FS_REV0
) {
484 (void) copystr(mp
->mnt_stat
.f_mntonname
, fs
->e2fs
.e2fs_fsmnt
,
485 sizeof(fs
->e2fs
.e2fs_fsmnt
) - 1, &size
);
486 memset(fs
->e2fs
.e2fs_fsmnt
, 0,
487 sizeof(fs
->e2fs
.e2fs_fsmnt
) - size
);
489 if (fs
->e2fs_fmod
!= 0) { /* XXX */
491 if (fs
->e2fs
.e2fs_state
== 0)
492 fs
->e2fs
.e2fs_wtime
= time_second
;
494 printf("%s: file system not clean; please fsck(8)\n",
495 mp
->mnt_stat
.f_mntfromname
);
496 (void) ext2fs_cgupdate(ump
, MNT_WAIT
);
506 * Reload all incore data for a filesystem (used after running fsck on
507 * the root filesystem and finding things to fix). The filesystem must
508 * be mounted read-only.
510 * Things to do to update the mount:
511 * 1) invalidate all cached meta-data.
512 * 2) re-read superblock from disk.
513 * 3) re-read summary information from disk.
514 * 4) invalidate all inactive vnodes.
515 * 5) invalidate all cached file data.
516 * 6) re-read inode data for all active vnodes.
519 ext2fs_reload(struct mount
*mp
, kauth_cred_t cred
, struct lwp
*l
)
521 struct vnode
*vp
, *mvp
, *devvp
;
525 struct ext2fs
*newfs
;
528 struct ufsmount
*ump
;
530 if ((mp
->mnt_flag
& MNT_RDONLY
) == 0)
535 * Step 1: invalidate all cached meta-data.
537 devvp
= ump
->um_devvp
;
538 vn_lock(devvp
, LK_EXCLUSIVE
| LK_RETRY
);
539 error
= vinvalbuf(devvp
, 0, cred
, l
, 0, 0);
542 panic("ext2fs_reload: dirty1");
544 * Step 2: re-read superblock from disk.
546 error
= bread(devvp
, SBLOCK
, SBSIZE
, NOCRED
, 0, &bp
);
551 newfs
= (struct ext2fs
*)bp
->b_data
;
552 error
= ext2fs_checksb(newfs
, (mp
->mnt_flag
& MNT_RDONLY
) != 0);
560 * copy in new superblock, and compute in-memory values
562 e2fs_sbload(newfs
, &fs
->e2fs
);
564 howmany(fs
->e2fs
.e2fs_bcount
- fs
->e2fs
.e2fs_first_dblock
,
566 fs
->e2fs_fsbtodb
= fs
->e2fs
.e2fs_log_bsize
+ LOG_MINBSIZE
- DEV_BSHIFT
;
567 fs
->e2fs_bsize
= MINBSIZE
<< fs
->e2fs
.e2fs_log_bsize
;
568 fs
->e2fs_bshift
= LOG_MINBSIZE
+ fs
->e2fs
.e2fs_log_bsize
;
569 fs
->e2fs_qbmask
= fs
->e2fs_bsize
- 1;
570 fs
->e2fs_bmask
= ~fs
->e2fs_qbmask
;
572 howmany(fs
->e2fs_ncg
, fs
->e2fs_bsize
/ sizeof(struct ext2_gd
));
573 fs
->e2fs_ipb
= fs
->e2fs_bsize
/ EXT2_DINODE_SIZE(fs
);
574 fs
->e2fs_itpg
= fs
->e2fs
.e2fs_ipg
/ fs
->e2fs_ipb
;
578 * Step 3: re-read summary information from disk.
581 for (i
= 0; i
< fs
->e2fs_ngdb
; i
++) {
582 error
= bread(devvp
,
583 fsbtodb(fs
, fs
->e2fs
.e2fs_first_dblock
+
584 1 /* superblock */ + i
),
585 fs
->e2fs_bsize
, NOCRED
, 0, &bp
);
590 e2fs_cgload((struct ext2_gd
*)bp
->b_data
,
591 &fs
->e2fs_gd
[i
* fs
->e2fs_bsize
/ sizeof(struct ext2_gd
)],
596 /* Allocate a marker vnode. */
599 * NOTE: not using the TAILQ_FOREACH here since in this loop vgone()
600 * and vclean() can be called indirectly
602 mutex_enter(&mntvnode_lock
);
604 for (vp
= TAILQ_FIRST(&mp
->mnt_vnodelist
); vp
; vp
= vunmark(mvp
)) {
606 if (vp
->v_mount
!= mp
|| vismarker(vp
))
609 * Step 4: invalidate all inactive vnodes.
611 if (vrecycle(vp
, &mntvnode_lock
, l
)) {
612 mutex_enter(&mntvnode_lock
);
617 * Step 5: invalidate all cached file data.
619 mutex_enter(vp
->v_interlock
);
620 mutex_exit(&mntvnode_lock
);
621 if (vget(vp
, LK_EXCLUSIVE
)) {
622 mutex_enter(&mntvnode_lock
);
626 if (vinvalbuf(vp
, 0, cred
, l
, 0, 0))
627 panic("ext2fs_reload: dirty2");
629 * Step 6: re-read inode data for all active vnodes.
632 error
= bread(devvp
, fsbtodb(fs
, ino_to_fsba(fs
, ip
->i_number
)),
633 (int)fs
->e2fs_bsize
, NOCRED
, 0, &bp
);
636 mutex_enter(&mntvnode_lock
);
640 cp
= (char *)bp
->b_data
+
641 (ino_to_fsbo(fs
, ip
->i_number
) * EXT2_DINODE_SIZE(fs
));
642 e2fs_iload((struct ext2fs_dinode
*)cp
, ip
->i_din
.e2fs_din
);
643 ext2fs_set_inode_guid(ip
);
646 mutex_enter(&mntvnode_lock
);
648 mutex_exit(&mntvnode_lock
);
654 * Common code for mount and mountroot
657 ext2fs_mountfs(struct vnode
*devvp
, struct mount
*mp
)
659 struct lwp
*l
= curlwp
;
660 struct ufsmount
*ump
;
663 struct m_ext2fs
*m_fs
;
670 p
= l
? l
->l_proc
: NULL
;
671 cred
= l
? l
->l_cred
: NOCRED
;
673 /* Flush out any old buffers remaining from a previous use. */
674 vn_lock(devvp
, LK_EXCLUSIVE
| LK_RETRY
);
675 error
= vinvalbuf(devvp
, V_SAVE
, cred
, l
, 0, 0);
680 ronly
= (mp
->mnt_flag
& MNT_RDONLY
) != 0;
686 printf("ext2 sb size: %zu\n", sizeof(struct ext2fs
));
688 error
= bread(devvp
, SBLOCK
, SBSIZE
, cred
, 0, &bp
);
691 fs
= (struct ext2fs
*)bp
->b_data
;
692 error
= ext2fs_checksb(fs
, ronly
);
695 ump
= malloc(sizeof(*ump
), M_UFSMNT
, M_WAITOK
);
696 memset(ump
, 0, sizeof(*ump
));
697 ump
->um_fstype
= UFS1
;
698 ump
->um_ops
= &ext2fs_ufsops
;
699 ump
->um_e2fs
= malloc(sizeof(struct m_ext2fs
), M_UFSMNT
, M_WAITOK
);
700 memset(ump
->um_e2fs
, 0, sizeof(struct m_ext2fs
));
701 e2fs_sbload((struct ext2fs
*)bp
->b_data
, &ump
->um_e2fs
->e2fs
);
705 m_fs
->e2fs_ronly
= ronly
;
708 printf("ext2 ino size %zu\n", EXT2_DINODE_SIZE(m_fs
));
711 if (m_fs
->e2fs
.e2fs_state
== E2FS_ISCLEAN
)
712 m_fs
->e2fs
.e2fs_state
= 0;
714 m_fs
->e2fs
.e2fs_state
= E2FS_ERRORS
;
718 /* compute dynamic sb infos */
720 howmany(m_fs
->e2fs
.e2fs_bcount
- m_fs
->e2fs
.e2fs_first_dblock
,
721 m_fs
->e2fs
.e2fs_bpg
);
722 m_fs
->e2fs_fsbtodb
= m_fs
->e2fs
.e2fs_log_bsize
+ LOG_MINBSIZE
- DEV_BSHIFT
;
723 m_fs
->e2fs_bsize
= MINBSIZE
<< m_fs
->e2fs
.e2fs_log_bsize
;
724 m_fs
->e2fs_bshift
= LOG_MINBSIZE
+ m_fs
->e2fs
.e2fs_log_bsize
;
725 m_fs
->e2fs_qbmask
= m_fs
->e2fs_bsize
- 1;
726 m_fs
->e2fs_bmask
= ~m_fs
->e2fs_qbmask
;
728 howmany(m_fs
->e2fs_ncg
, m_fs
->e2fs_bsize
/ sizeof(struct ext2_gd
));
729 m_fs
->e2fs_ipb
= m_fs
->e2fs_bsize
/ EXT2_DINODE_SIZE(m_fs
);
730 m_fs
->e2fs_itpg
= m_fs
->e2fs
.e2fs_ipg
/ m_fs
->e2fs_ipb
;
732 m_fs
->e2fs_gd
= malloc(m_fs
->e2fs_ngdb
* m_fs
->e2fs_bsize
,
734 for (i
= 0; i
< m_fs
->e2fs_ngdb
; i
++) {
735 error
= bread(devvp
,
736 fsbtodb(m_fs
, m_fs
->e2fs
.e2fs_first_dblock
+
737 1 /* superblock */ + i
),
738 m_fs
->e2fs_bsize
, NOCRED
, 0, &bp
);
740 free(m_fs
->e2fs_gd
, M_UFSMNT
);
743 e2fs_cgload((struct ext2_gd
*)bp
->b_data
,
745 i
* m_fs
->e2fs_bsize
/ sizeof(struct ext2_gd
)],
752 mp
->mnt_stat
.f_fsidx
.__fsid_val
[0] = (long)dev
;
753 mp
->mnt_stat
.f_fsidx
.__fsid_val
[1] = makefstype(MOUNT_EXT2FS
);
754 mp
->mnt_stat
.f_fsid
= mp
->mnt_stat
.f_fsidx
.__fsid_val
[0];
755 mp
->mnt_stat
.f_namemax
= EXT2FS_MAXNAMLEN
;
756 mp
->mnt_flag
|= MNT_LOCAL
;
757 mp
->mnt_dev_bshift
= DEV_BSHIFT
; /* XXX */
758 mp
->mnt_fs_bshift
= m_fs
->e2fs_bshift
;
759 mp
->mnt_iflag
|= IMNT_DTYPE
;
763 ump
->um_devvp
= devvp
;
764 ump
->um_nindir
= NINDIR(m_fs
);
765 ump
->um_lognindir
= ffs(NINDIR(m_fs
)) - 1;
766 ump
->um_bptrtodb
= m_fs
->e2fs_fsbtodb
;
767 ump
->um_seqinc
= 1; /* no frags */
768 ump
->um_maxsymlinklen
= EXT2_MAXSYMLINKLEN
;
769 ump
->um_dirblksiz
= m_fs
->e2fs_bsize
;
770 ump
->um_maxfilesize
= ((uint64_t)0x80000000 * m_fs
->e2fs_bsize
- 1);
771 devvp
->v_specmountpoint
= mp
;
778 free(ump
->um_e2fs
, M_UFSMNT
);
786 * unmount system call
789 ext2fs_unmount(struct mount
*mp
, int mntflags
)
791 struct ufsmount
*ump
;
796 if (mntflags
& MNT_FORCE
)
798 if ((error
= ext2fs_flushfiles(mp
, flags
)) != 0)
802 if (fs
->e2fs_ronly
== 0 &&
803 ext2fs_cgupdate(ump
, MNT_WAIT
) == 0 &&
804 (fs
->e2fs
.e2fs_state
& E2FS_ERRORS
) == 0) {
805 fs
->e2fs
.e2fs_state
= E2FS_ISCLEAN
;
806 (void) ext2fs_sbupdate(ump
, MNT_WAIT
);
808 if (ump
->um_devvp
->v_type
!= VBAD
)
809 ump
->um_devvp
->v_specmountpoint
= NULL
;
810 vn_lock(ump
->um_devvp
, LK_EXCLUSIVE
| LK_RETRY
);
811 error
= VOP_CLOSE(ump
->um_devvp
, fs
->e2fs_ronly
? FREAD
: FREAD
|FWRITE
,
814 free(fs
->e2fs_gd
, M_UFSMNT
);
818 mp
->mnt_flag
&= ~MNT_LOCAL
;
823 * Flush out all the files in a filesystem.
826 ext2fs_flushfiles(struct mount
*mp
, int flags
)
832 flags
&= ~FORCECLOSE
;
833 error
= vflush(mp
, NULLVP
, flags
);
838 * Get file system statistics.
841 ext2fs_statvfs(struct mount
*mp
, struct statvfs
*sbp
)
843 struct ufsmount
*ump
;
845 uint32_t overhead
, overhead_per_group
, ngdb
;
850 if (fs
->e2fs
.e2fs_magic
!= E2FS_MAGIC
)
851 panic("ext2fs_statvfs");
854 * Compute the overhead (FS structures)
857 1 /* block bitmap */ +
858 1 /* inode bitmap */ +
860 overhead
= fs
->e2fs
.e2fs_first_dblock
+
861 fs
->e2fs_ncg
* overhead_per_group
;
862 if (fs
->e2fs
.e2fs_rev
> E2FS_REV0
&&
863 fs
->e2fs
.e2fs_features_rocompat
& EXT2F_ROCOMPAT_SPARSESUPER
) {
864 for (i
= 0, ngroups
= 0; i
< fs
->e2fs_ncg
; i
++) {
869 ngroups
= fs
->e2fs_ncg
;
871 ngdb
= fs
->e2fs_ngdb
;
872 if (fs
->e2fs
.e2fs_rev
> E2FS_REV0
&&
873 fs
->e2fs
.e2fs_features_compat
& EXT2F_COMPAT_RESIZE
)
874 ngdb
+= fs
->e2fs
.e2fs_reserved_ngdb
;
875 overhead
+= ngroups
* (1 /* superblock */ + ngdb
);
877 sbp
->f_bsize
= fs
->e2fs_bsize
;
878 sbp
->f_frsize
= MINBSIZE
<< fs
->e2fs
.e2fs_fsize
;
879 sbp
->f_iosize
= fs
->e2fs_bsize
;
880 sbp
->f_blocks
= fs
->e2fs
.e2fs_bcount
- overhead
;
881 sbp
->f_bfree
= fs
->e2fs
.e2fs_fbcount
;
882 sbp
->f_bresvd
= fs
->e2fs
.e2fs_rbcount
;
883 if (sbp
->f_bfree
> sbp
->f_bresvd
)
884 sbp
->f_bavail
= sbp
->f_bfree
- sbp
->f_bresvd
;
887 sbp
->f_files
= fs
->e2fs
.e2fs_icount
;
888 sbp
->f_ffree
= fs
->e2fs
.e2fs_ficount
;
889 sbp
->f_favail
= fs
->e2fs
.e2fs_ficount
;
891 copy_statvfs_info(sbp
, mp
);
896 * Go through the disk queues to initiate sandbagged IO;
897 * go through the inodes to write those that have been modified;
898 * initiate the writing of the super block if it has been modified.
900 * Note: we are always called with the filesystem marked `MPBUSY'.
903 ext2fs_sync(struct mount
*mp
, int waitfor
, kauth_cred_t cred
)
905 struct vnode
*vp
, *mvp
;
907 struct ufsmount
*ump
= VFSTOUFS(mp
);
909 int error
, allerror
= 0;
912 if (fs
->e2fs_fmod
!= 0 && fs
->e2fs_ronly
!= 0) { /* XXX */
913 printf("fs = %s\n", fs
->e2fs_fsmnt
);
914 panic("update: rofs mod");
917 /* Allocate a marker vnode. */
921 * Write back each (modified) inode.
923 mutex_enter(&mntvnode_lock
);
926 * NOTE: not using the TAILQ_FOREACH here since in this loop vgone()
927 * and vclean() can be called indirectly
929 for (vp
= TAILQ_FIRST(&mp
->mnt_vnodelist
); vp
; vp
= vunmark(mvp
)) {
931 if (vp
->v_mount
!= mp
|| vismarker(vp
))
933 mutex_enter(vp
->v_interlock
);
935 if (ip
== NULL
|| (vp
->v_iflag
& (VI_XLOCK
|VI_CLEAN
)) != 0 ||
936 vp
->v_type
== VNON
||
938 (IN_CHANGE
| IN_UPDATE
| IN_MODIFIED
)) == 0 &&
939 LIST_EMPTY(&vp
->v_dirtyblkhd
) &&
940 UVM_OBJ_IS_CLEAN(&vp
->v_uobj
)))
942 mutex_exit(vp
->v_interlock
);
945 mutex_exit(&mntvnode_lock
);
946 error
= vget(vp
, LK_EXCLUSIVE
| LK_NOWAIT
);
948 mutex_enter(&mntvnode_lock
);
949 if (error
== ENOENT
) {
950 mutex_enter(&mntvnode_lock
);
956 if (vp
->v_type
== VREG
&& waitfor
== MNT_LAZY
)
957 error
= ext2fs_update(vp
, NULL
, NULL
, 0);
959 error
= VOP_FSYNC(vp
, cred
,
960 waitfor
== MNT_WAIT
? FSYNC_WAIT
: 0, 0, 0);
964 mutex_enter(&mntvnode_lock
);
966 mutex_exit(&mntvnode_lock
);
969 * Force stale file system control information to be flushed.
971 if (waitfor
!= MNT_LAZY
) {
972 vn_lock(ump
->um_devvp
, LK_EXCLUSIVE
| LK_RETRY
);
973 if ((error
= VOP_FSYNC(ump
->um_devvp
, cred
,
974 waitfor
== MNT_WAIT
? FSYNC_WAIT
: 0, 0, 0)) != 0)
976 VOP_UNLOCK(ump
->um_devvp
);
979 * Write back modified superblock.
981 if (fs
->e2fs_fmod
!= 0) {
983 fs
->e2fs
.e2fs_wtime
= time_second
;
984 if ((error
= ext2fs_cgupdate(ump
, waitfor
)))
991 * Look up a EXT2FS dinode number to find its incore vnode, otherwise read it
992 * in from disk. If it is in core, wait for the lock bit to clear, then
993 * return the inode locked. Detection and handling of mount points must be
994 * done by the calling routine.
997 ext2fs_vget(struct mount
*mp
, ino_t ino
, struct vnode
**vpp
)
1001 struct ufsmount
*ump
;
1011 if ((*vpp
= ufs_ihashget(dev
, ino
, LK_EXCLUSIVE
)) != NULL
)
1014 /* Allocate a new vnode/inode. */
1015 error
= getnewvnode(VT_EXT2FS
, mp
, ext2fs_vnodeop_p
, NULL
, &vp
);
1020 ip
= pool_get(&ext2fs_inode_pool
, PR_WAITOK
);
1022 mutex_enter(&ufs_hashlock
);
1023 if ((*vpp
= ufs_ihashget(dev
, ino
, 0)) != NULL
) {
1024 mutex_exit(&ufs_hashlock
);
1026 pool_put(&ext2fs_inode_pool
, ip
);
1030 vp
->v_vflag
|= VV_LOCKSWORK
;
1032 memset(ip
, 0, sizeof(struct inode
));
1036 ip
->i_e2fs
= fs
= ump
->um_e2fs
;
1039 ip
->i_e2fs_last_lblk
= 0;
1040 ip
->i_e2fs_last_blk
= 0;
1041 genfs_node_init(vp
, &ext2fs_genfsops
);
1044 * Put it onto its hash chain and lock it so that other requests for
1045 * this inode will block if they arrive while we are sleeping waiting
1046 * for old data structures to be purged or for the contents of the
1047 * disk portion of this inode to be read.
1051 mutex_exit(&ufs_hashlock
);
1053 /* Read in the disk contents for the inode, copy into the inode. */
1054 error
= bread(ump
->um_devvp
, fsbtodb(fs
, ino_to_fsba(fs
, ino
)),
1055 (int)fs
->e2fs_bsize
, NOCRED
, 0, &bp
);
1059 * The inode does not contain anything useful, so it would
1060 * be misleading to leave it on its hash chain. With mode
1061 * still zero, it will be unlinked and returned to the free
1070 cp
= (char *)bp
->b_data
+ (ino_to_fsbo(fs
, ino
) * EXT2_DINODE_SIZE(fs
));
1071 ip
->i_din
.e2fs_din
= pool_get(&ext2fs_dinode_pool
, PR_WAITOK
);
1072 e2fs_iload((struct ext2fs_dinode
*)cp
, ip
->i_din
.e2fs_din
);
1073 ext2fs_set_inode_guid(ip
);
1076 /* If the inode was deleted, reset all fields */
1077 if (ip
->i_e2fs_dtime
!= 0) {
1078 ip
->i_e2fs_mode
= ip
->i_e2fs_nblock
= 0;
1079 (void)ext2fs_setsize(ip
, 0);
1080 memset(ip
->i_e2fs_blocks
, 0, sizeof(ip
->i_e2fs_blocks
));
1084 * Initialize the vnode from the inode, check for aliases.
1087 error
= ext2fs_vinit(mp
, ext2fs_specop_p
, ext2fs_fifoop_p
, &vp
);
1094 * Finish inode initialization now that aliasing has been resolved.
1097 ip
->i_devvp
= ump
->um_devvp
;
1101 * Set up a generation number for this inode if it does not
1102 * already have one. This should only happen on old filesystems.
1105 if (ip
->i_e2fs_gen
== 0) {
1106 if (++ext2gennumber
< (u_long
)time_second
)
1107 ext2gennumber
= time_second
;
1108 ip
->i_e2fs_gen
= ext2gennumber
;
1109 if ((vp
->v_mount
->mnt_flag
& MNT_RDONLY
) == 0)
1110 ip
->i_flag
|= IN_MODIFIED
;
1112 uvm_vnp_setsize(vp
, ext2fs_size(ip
));
1118 * File handle to vnode
1120 * Have to be really careful about stale file handles:
1121 * - check that the inode number is valid
1122 * - call ext2fs_vget() to get the locked inode
1123 * - check for an unallocated inode (i_mode == 0)
1126 ext2fs_fhtovp(struct mount
*mp
, struct fid
*fhp
, struct vnode
**vpp
)
1132 struct m_ext2fs
*fs
;
1134 if (fhp
->fid_len
!= sizeof(struct ufid
))
1137 memcpy(&ufh
, fhp
, sizeof(struct ufid
));
1138 fs
= VFSTOUFS(mp
)->um_e2fs
;
1139 if ((ufh
.ufid_ino
< EXT2_FIRSTINO
&& ufh
.ufid_ino
!= EXT2_ROOTINO
) ||
1140 ufh
.ufid_ino
>= fs
->e2fs_ncg
* fs
->e2fs
.e2fs_ipg
)
1143 if ((error
= VFS_VGET(mp
, ufh
.ufid_ino
, &nvp
)) != 0) {
1148 if (ip
->i_e2fs_mode
== 0 || ip
->i_e2fs_dtime
!= 0 ||
1149 ip
->i_e2fs_gen
!= ufh
.ufid_gen
) {
1159 * Vnode pointer to File handle
1163 ext2fs_vptofh(struct vnode
*vp
, struct fid
*fhp
, size_t *fh_size
)
1168 if (*fh_size
< sizeof(struct ufid
)) {
1169 *fh_size
= sizeof(struct ufid
);
1172 *fh_size
= sizeof(struct ufid
);
1175 memset(&ufh
, 0, sizeof(ufh
));
1176 ufh
.ufid_len
= sizeof(struct ufid
);
1177 ufh
.ufid_ino
= ip
->i_number
;
1178 ufh
.ufid_gen
= ip
->i_e2fs_gen
;
1179 memcpy(fhp
, &ufh
, sizeof(ufh
));
1184 * Write a superblock and associated information back to disk.
1187 ext2fs_sbupdate(struct ufsmount
*mp
, int waitfor
)
1189 struct m_ext2fs
*fs
= mp
->um_e2fs
;
1193 bp
= getblk(mp
->um_devvp
, SBLOCK
, SBSIZE
, 0, 0);
1194 e2fs_sbsave(&fs
->e2fs
, (struct ext2fs
*)bp
->b_data
);
1195 if (waitfor
== MNT_WAIT
)
1203 ext2fs_cgupdate(struct ufsmount
*mp
, int waitfor
)
1205 struct m_ext2fs
*fs
= mp
->um_e2fs
;
1207 int i
, error
= 0, allerror
= 0;
1209 allerror
= ext2fs_sbupdate(mp
, waitfor
);
1210 for (i
= 0; i
< fs
->e2fs_ngdb
; i
++) {
1211 bp
= getblk(mp
->um_devvp
, fsbtodb(fs
,
1212 fs
->e2fs
.e2fs_first_dblock
+
1213 1 /* superblock */ + i
), fs
->e2fs_bsize
, 0, 0);
1214 e2fs_cgsave(&fs
->e2fs_gd
[
1215 i
* fs
->e2fs_bsize
/ sizeof(struct ext2_gd
)],
1216 (struct ext2_gd
*)bp
->b_data
, fs
->e2fs_bsize
);
1217 if (waitfor
== MNT_WAIT
)
1223 if (!allerror
&& error
)
1229 ext2fs_checksb(struct ext2fs
*fs
, int ronly
)
1232 if (fs2h16(fs
->e2fs_magic
) != E2FS_MAGIC
) {
1233 return (EINVAL
); /* XXX needs translation */
1235 if (fs2h32(fs
->e2fs_rev
) > E2FS_REV1
) {
1237 printf("Ext2 fs: unsupported revision number: %x\n",
1238 fs2h32(fs
->e2fs_rev
));
1240 return (EINVAL
); /* XXX needs translation */
1242 if (fs2h32(fs
->e2fs_log_bsize
) > 2) { /* block size = 1024|2048|4096 */
1244 printf("Ext2 fs: bad block size: %d "
1245 "(expected <= 2 for ext2 fs)\n",
1246 fs2h32(fs
->e2fs_log_bsize
));
1248 return (EINVAL
); /* XXX needs translation */
1250 if (fs2h32(fs
->e2fs_rev
) > E2FS_REV0
) {
1251 if (fs2h32(fs
->e2fs_first_ino
) != EXT2_FIRSTINO
) {
1252 printf("Ext2 fs: unsupported first inode position\n");
1253 return (EINVAL
); /* XXX needs translation */
1255 if (fs2h32(fs
->e2fs_features_incompat
) &
1256 ~EXT2F_INCOMPAT_SUPP
) {
1257 printf("Ext2 fs: unsupported optional feature\n");
1258 return (EINVAL
); /* XXX needs translation */
1260 if (!ronly
&& fs2h32(fs
->e2fs_features_rocompat
) &
1261 ~EXT2F_ROCOMPAT_SUPP
) {
1262 return (EROFS
); /* XXX needs translation */