ext2fs_vfsops.c revision 1.137.6.5.2.1 1 /* $NetBSD: ext2fs_vfsops.c,v 1.137.6.5.2.1 2014/04/28 16:03:16 sborrill Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
18 *
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
29 * SUCH DAMAGE.
30 *
31 * @(#)ffs_vfsops.c 8.14 (Berkeley) 11/28/94
32 * Modified for ext2fs by Manuel Bouyer.
33 */
34
35 /*
36 * Copyright (c) 1997 Manuel Bouyer.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
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.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by Manuel Bouyer.
49 * 4. The name of the author may not be used to endorse or promote products
50 * derived from this software without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
53 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
54 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
55 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
56 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
57 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
61 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62 *
63 * @(#)ffs_vfsops.c 8.14 (Berkeley) 11/28/94
64 * Modified for ext2fs by Manuel Bouyer.
65 */
66
67 #include <sys/cdefs.h>
68 __KERNEL_RCSID(0, "$NetBSD: ext2fs_vfsops.c,v 1.137.6.5.2.1 2014/04/28 16:03:16 sborrill Exp $");
69
70 #if defined(_KERNEL_OPT)
71 #include "opt_compat_netbsd.h"
72 #endif
73
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/sysctl.h>
77 #include <sys/namei.h>
78 #include <sys/proc.h>
79 #include <sys/kernel.h>
80 #include <sys/vnode.h>
81 #include <sys/socket.h>
82 #include <sys/mount.h>
83 #include <sys/buf.h>
84 #include <sys/device.h>
85 #include <sys/mbuf.h>
86 #include <sys/file.h>
87 #include <sys/disklabel.h>
88 #include <sys/ioctl.h>
89 #include <sys/errno.h>
90 #include <sys/malloc.h>
91 #include <sys/pool.h>
92 #include <sys/lock.h>
93 #include <sys/conf.h>
94 #include <sys/kauth.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
106 #include <ufs/ext2fs/ext2fs.h>
107 #include <ufs/ext2fs/ext2fs_dir.h>
108 #include <ufs/ext2fs/ext2fs_extern.h>
109
110 MODULE(MODULE_CLASS_VFS, ext2fs, NULL);
111
112 extern kmutex_t ufs_hashlock;
113
114 int ext2fs_sbupdate(struct ufsmount *, int);
115 static int ext2fs_checksb(struct ext2fs *, int);
116
117 static struct sysctllog *ext2fs_sysctl_log;
118
119 extern const struct vnodeopv_desc ext2fs_vnodeop_opv_desc;
120 extern const struct vnodeopv_desc ext2fs_specop_opv_desc;
121 extern const struct vnodeopv_desc ext2fs_fifoop_opv_desc;
122
123 const struct vnodeopv_desc * const ext2fs_vnodeopv_descs[] = {
124 &ext2fs_vnodeop_opv_desc,
125 &ext2fs_specop_opv_desc,
126 &ext2fs_fifoop_opv_desc,
127 NULL,
128 };
129
130 struct vfsops ext2fs_vfsops = {
131 MOUNT_EXT2FS,
132 sizeof (struct ufs_args),
133 ext2fs_mount,
134 ufs_start,
135 ext2fs_unmount,
136 ufs_root,
137 ufs_quotactl,
138 ext2fs_statvfs,
139 ext2fs_sync,
140 ext2fs_vget,
141 ext2fs_fhtovp,
142 ext2fs_vptofh,
143 ext2fs_init,
144 ext2fs_reinit,
145 ext2fs_done,
146 ext2fs_mountroot,
147 (int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
148 vfs_stdextattrctl,
149 (void *)eopnotsupp, /* vfs_suspendctl */
150 genfs_renamelock_enter,
151 genfs_renamelock_exit,
152 (void *)eopnotsupp,
153 ext2fs_vnodeopv_descs,
154 0,
155 { NULL, NULL },
156 };
157
158 static const struct genfs_ops ext2fs_genfsops = {
159 .gop_size = genfs_size,
160 .gop_alloc = ext2fs_gop_alloc,
161 .gop_write = genfs_gop_write,
162 .gop_markupdate = ufs_gop_markupdate,
163 };
164
165 static const struct ufs_ops ext2fs_ufsops = {
166 .uo_itimes = ext2fs_itimes,
167 .uo_update = ext2fs_update,
168 .uo_vfree = ext2fs_vfree,
169 };
170
171 /* Fill in the inode uid/gid from ext2 halves. */
172 void
173 ext2fs_set_inode_guid(struct inode *ip)
174 {
175
176 ip->i_gid = ip->i_e2fs_gid;
177 ip->i_uid = ip->i_e2fs_uid;
178 if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0) {
179 ip->i_gid |= ip->i_e2fs_gid_high << 16;
180 ip->i_uid |= ip->i_e2fs_uid_high << 16;
181 }
182 }
183
184 static int
185 ext2fs_modcmd(modcmd_t cmd, void *arg)
186 {
187 int error;
188
189 switch (cmd) {
190 case MODULE_CMD_INIT:
191 error = vfs_attach(&ext2fs_vfsops);
192 if (error != 0)
193 break;
194 sysctl_createv(&ext2fs_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(&ext2fs_sysctl_log, 0, NULL, NULL,
200 CTLFLAG_PERMANENT,
201 CTLTYPE_NODE, "ext2fs",
202 SYSCTL_DESCR("Linux EXT2FS file system"),
203 NULL, 0, NULL, 0,
204 CTL_VFS, 17, CTL_EOL);
205 /*
206 * XXX the "17" above could be dynamic, thereby eliminating
207 * one more instance of the "number to vfs" mapping problem,
208 * but "17" is the order as taken from sys/mount.h
209 */
210 break;
211 case MODULE_CMD_FINI:
212 error = vfs_detach(&ext2fs_vfsops);
213 if (error != 0)
214 break;
215 sysctl_teardown(&ext2fs_sysctl_log);
216 break;
217 default:
218 error = ENOTTY;
219 break;
220 }
221
222 return (error);
223 }
224
225 /*
226 * XXX Same structure as FFS inodes? Should we share a common pool?
227 */
228 struct pool ext2fs_inode_pool;
229 struct pool ext2fs_dinode_pool;
230
231 extern u_long ext2gennumber;
232
233 void
234 ext2fs_init(void)
235 {
236
237 pool_init(&ext2fs_inode_pool, sizeof(struct inode), 0, 0, 0,
238 "ext2fsinopl", &pool_allocator_nointr, IPL_NONE);
239 pool_init(&ext2fs_dinode_pool, sizeof(struct ext2fs_dinode), 0, 0, 0,
240 "ext2dinopl", &pool_allocator_nointr, IPL_NONE);
241 ufs_init();
242 }
243
244 void
245 ext2fs_reinit(void)
246 {
247 ufs_reinit();
248 }
249
250 void
251 ext2fs_done(void)
252 {
253
254 ufs_done();
255 pool_destroy(&ext2fs_inode_pool);
256 pool_destroy(&ext2fs_dinode_pool);
257 }
258
259 /*
260 * Called by main() when ext2fs is going to be mounted as root.
261 *
262 * Name is updated by mount(8) after booting.
263 */
264 #define ROOTNAME "root_device"
265
266 int
267 ext2fs_mountroot(void)
268 {
269 extern struct vnode *rootvp;
270 struct m_ext2fs *fs;
271 struct mount *mp;
272 struct ufsmount *ump;
273 int error;
274
275 if (device_class(root_device) != DV_DISK)
276 return (ENODEV);
277
278 if ((error = vfs_rootmountalloc(MOUNT_EXT2FS, "root_device", &mp))) {
279 vrele(rootvp);
280 return (error);
281 }
282
283 if ((error = ext2fs_mountfs(rootvp, mp)) != 0) {
284 vfs_unbusy(mp, false, NULL);
285 vfs_destroy(mp);
286 return (error);
287 }
288 mutex_enter(&mountlist_lock);
289 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
290 mutex_exit(&mountlist_lock);
291 ump = VFSTOUFS(mp);
292 fs = ump->um_e2fs;
293 memset(fs->e2fs_fsmnt, 0, sizeof(fs->e2fs_fsmnt));
294 (void) copystr(mp->mnt_stat.f_mntonname, fs->e2fs_fsmnt,
295 sizeof(fs->e2fs_fsmnt) - 1, 0);
296 if (fs->e2fs.e2fs_rev > E2FS_REV0) {
297 memset(fs->e2fs.e2fs_fsmnt, 0, sizeof(fs->e2fs.e2fs_fsmnt));
298 (void) copystr(mp->mnt_stat.f_mntonname, fs->e2fs.e2fs_fsmnt,
299 sizeof(fs->e2fs.e2fs_fsmnt) - 1, 0);
300 }
301 (void)ext2fs_statvfs(mp, &mp->mnt_stat);
302 vfs_unbusy(mp, false, NULL);
303 setrootfstime((time_t)fs->e2fs.e2fs_wtime);
304 return (0);
305 }
306
307 /*
308 * VFS Operations.
309 *
310 * mount system call
311 */
312 int
313 ext2fs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
314 {
315 struct lwp *l = curlwp;
316 struct nameidata nd;
317 struct vnode *devvp;
318 struct ufs_args *args = data;
319 struct ufsmount *ump = NULL;
320 struct m_ext2fs *fs;
321 size_t size;
322 int error = 0, flags, update;
323 mode_t accessmode;
324
325 if (args == NULL)
326 return EINVAL;
327 if (*data_len < sizeof *args)
328 return EINVAL;
329
330 if (mp->mnt_flag & MNT_GETARGS) {
331 ump = VFSTOUFS(mp);
332 if (ump == NULL)
333 return EIO;
334 memset(args, 0, sizeof *args);
335 args->fspec = NULL;
336 *data_len = sizeof *args;
337 return 0;
338 }
339
340 update = mp->mnt_flag & MNT_UPDATE;
341
342 /* Check arguments */
343 if (args->fspec != NULL) {
344 /*
345 * Look up the name and verify that it's sane.
346 */
347 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, args->fspec);
348 if ((error = namei(&nd)) != 0)
349 return (error);
350 devvp = nd.ni_vp;
351
352 if (!update) {
353 /*
354 * Be sure this is a valid block device
355 */
356 if (devvp->v_type != VBLK)
357 error = ENOTBLK;
358 else if (bdevsw_lookup(devvp->v_rdev) == NULL)
359 error = ENXIO;
360 } else {
361 /*
362 * Be sure we're still naming the same device
363 * used for our initial mount
364 */
365 ump = VFSTOUFS(mp);
366 if (devvp != ump->um_devvp) {
367 if (devvp->v_rdev != ump->um_devvp->v_rdev)
368 error = EINVAL;
369 else {
370 vrele(devvp);
371 devvp = ump->um_devvp;
372 vref(devvp);
373 }
374 }
375 }
376 } else {
377 if (!update) {
378 /* New mounts must have a filename for the device */
379 return (EINVAL);
380 } else {
381 ump = VFSTOUFS(mp);
382 devvp = ump->um_devvp;
383 vref(devvp);
384 }
385 }
386
387 /*
388 * If mount by non-root, then verify that user has necessary
389 * permissions on the device.
390 */
391 if (error == 0 && kauth_authorize_generic(l->l_cred,
392 KAUTH_GENERIC_ISSUSER, NULL) != 0) {
393 accessmode = VREAD;
394 if (update ?
395 (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
396 (mp->mnt_flag & MNT_RDONLY) == 0)
397 accessmode |= VWRITE;
398 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
399 error = VOP_ACCESS(devvp, accessmode, l->l_cred);
400 VOP_UNLOCK(devvp, 0);
401 }
402
403 if (error) {
404 vrele(devvp);
405 return (error);
406 }
407
408 if (!update) {
409 int xflags;
410
411 if (mp->mnt_flag & MNT_RDONLY)
412 xflags = FREAD;
413 else
414 xflags = FREAD|FWRITE;
415 error = VOP_OPEN(devvp, xflags, FSCRED);
416 if (error)
417 goto fail;
418 error = ext2fs_mountfs(devvp, mp);
419 if (error) {
420 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
421 (void)VOP_CLOSE(devvp, xflags, NOCRED);
422 VOP_UNLOCK(devvp, 0);
423 goto fail;
424 }
425
426 ump = VFSTOUFS(mp);
427 fs = ump->um_e2fs;
428 } else {
429 /*
430 * Update the mount.
431 */
432
433 /*
434 * The initial mount got a reference on this
435 * device, so drop the one obtained via
436 * namei(), above.
437 */
438 vrele(devvp);
439
440 ump = VFSTOUFS(mp);
441 fs = ump->um_e2fs;
442 if (fs->e2fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
443 /*
444 * Changing from r/w to r/o
445 */
446 flags = WRITECLOSE;
447 if (mp->mnt_flag & MNT_FORCE)
448 flags |= FORCECLOSE;
449 error = ext2fs_flushfiles(mp, flags);
450 if (error == 0 &&
451 ext2fs_cgupdate(ump, MNT_WAIT) == 0 &&
452 (fs->e2fs.e2fs_state & E2FS_ERRORS) == 0) {
453 fs->e2fs.e2fs_state = E2FS_ISCLEAN;
454 (void) ext2fs_sbupdate(ump, MNT_WAIT);
455 }
456 if (error)
457 return (error);
458 fs->e2fs_ronly = 1;
459 }
460
461 if (mp->mnt_flag & MNT_RELOAD) {
462 error = ext2fs_reload(mp, l->l_cred);
463 if (error)
464 return (error);
465 }
466
467 if (fs->e2fs_ronly && (mp->mnt_iflag & IMNT_WANTRDWR)) {
468 /*
469 * Changing from read-only to read/write
470 */
471 fs->e2fs_ronly = 0;
472 if (fs->e2fs.e2fs_state == E2FS_ISCLEAN)
473 fs->e2fs.e2fs_state = 0;
474 else
475 fs->e2fs.e2fs_state = E2FS_ERRORS;
476 fs->e2fs_fmod = 1;
477 }
478 if (args->fspec == NULL)
479 return 0;
480 }
481
482 error = set_statvfs_info(path, UIO_USERSPACE, args->fspec,
483 UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
484 (void) copystr(mp->mnt_stat.f_mntonname, fs->e2fs_fsmnt,
485 sizeof(fs->e2fs_fsmnt) - 1, &size);
486 memset(fs->e2fs_fsmnt + size, 0, sizeof(fs->e2fs_fsmnt) - size);
487 if (fs->e2fs.e2fs_rev > E2FS_REV0) {
488 (void) copystr(mp->mnt_stat.f_mntonname, fs->e2fs.e2fs_fsmnt,
489 sizeof(fs->e2fs.e2fs_fsmnt) - 1, &size);
490 memset(fs->e2fs.e2fs_fsmnt, 0,
491 sizeof(fs->e2fs.e2fs_fsmnt) - size);
492 }
493 if (fs->e2fs_fmod != 0) { /* XXX */
494 fs->e2fs_fmod = 0;
495 if (fs->e2fs.e2fs_state == 0)
496 fs->e2fs.e2fs_wtime = time_second;
497 else
498 printf("%s: file system not clean; please fsck(8)\n",
499 mp->mnt_stat.f_mntfromname);
500 (void) ext2fs_cgupdate(ump, MNT_WAIT);
501 }
502 return (error);
503
504 fail:
505 vrele(devvp);
506 return (error);
507 }
508
509 /*
510 * Reload all incore data for a filesystem (used after running fsck on
511 * the root filesystem and finding things to fix). The filesystem must
512 * be mounted read-only.
513 *
514 * Things to do to update the mount:
515 * 1) invalidate all cached meta-data.
516 * 2) re-read superblock from disk.
517 * 3) re-read summary information from disk.
518 * 4) invalidate all inactive vnodes.
519 * 5) invalidate all cached file data.
520 * 6) re-read inode data for all active vnodes.
521 */
522 int
523 ext2fs_reload(struct mount *mountp, kauth_cred_t cred)
524 {
525 struct lwp *l = curlwp;
526 struct vnode *vp, *mvp, *devvp;
527 struct inode *ip;
528 struct buf *bp;
529 struct m_ext2fs *fs;
530 struct ext2fs *newfs;
531 struct partinfo dpart;
532 int i, size, error;
533 void *cp;
534
535 if ((mountp->mnt_flag & MNT_RDONLY) == 0)
536 return (EINVAL);
537
538 /*
539 * Step 1: invalidate all cached meta-data.
540 */
541 devvp = VFSTOUFS(mountp)->um_devvp;
542 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
543 error = vinvalbuf(devvp, 0, cred, l, 0, 0);
544 VOP_UNLOCK(devvp, 0);
545 if (error)
546 panic("ext2fs_reload: dirty1");
547 /*
548 * Step 2: re-read superblock from disk.
549 */
550 if (VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, NOCRED) != 0)
551 size = DEV_BSIZE;
552 else
553 size = dpart.disklab->d_secsize;
554 error = bread(devvp, (daddr_t)(SBOFF / size), SBSIZE, NOCRED, 0, &bp);
555 if (error) {
556 brelse(bp, 0);
557 return (error);
558 }
559 newfs = (struct ext2fs *)bp->b_data;
560 error = ext2fs_checksb(newfs, (mountp->mnt_flag & MNT_RDONLY) != 0);
561 if (error) {
562 brelse(bp, 0);
563 return (error);
564 }
565
566 fs = VFSTOUFS(mountp)->um_e2fs;
567 /*
568 * copy in new superblock, and compute in-memory values
569 */
570 e2fs_sbload(newfs, &fs->e2fs);
571 fs->e2fs_ncg =
572 howmany(fs->e2fs.e2fs_bcount - fs->e2fs.e2fs_first_dblock,
573 fs->e2fs.e2fs_bpg);
574 /* XXX assume hw bsize = 512 */
575 fs->e2fs_fsbtodb = fs->e2fs.e2fs_log_bsize + 1;
576 fs->e2fs_bsize = MINBSIZE << fs->e2fs.e2fs_log_bsize;
577 fs->e2fs_bshift = LOG_MINBSIZE + fs->e2fs.e2fs_log_bsize;
578 fs->e2fs_qbmask = fs->e2fs_bsize - 1;
579 fs->e2fs_bmask = ~fs->e2fs_qbmask;
580 fs->e2fs_ngdb =
581 howmany(fs->e2fs_ncg, fs->e2fs_bsize / sizeof(struct ext2_gd));
582 fs->e2fs_ipb = fs->e2fs_bsize / EXT2_DINODE_SIZE;
583 fs->e2fs_itpg = fs->e2fs.e2fs_ipg / fs->e2fs_ipb;
584 brelse(bp, 0);
585
586 /*
587 * Step 3: re-read summary information from disk.
588 */
589
590 for (i = 0; i < fs->e2fs_ngdb; i++) {
591 error = bread(devvp ,
592 fsbtodb(fs, fs->e2fs.e2fs_first_dblock +
593 1 /* superblock */ + i),
594 fs->e2fs_bsize, NOCRED, 0, &bp);
595 if (error) {
596 brelse(bp, 0);
597 return (error);
598 }
599 e2fs_cgload((struct ext2_gd *)bp->b_data,
600 &fs->e2fs_gd[i * fs->e2fs_bsize / sizeof(struct ext2_gd)],
601 fs->e2fs_bsize);
602 brelse(bp, 0);
603 }
604
605 /* Allocate a marker vnode. */
606 if ((mvp = vnalloc(mountp)) == NULL)
607 return (ENOMEM);
608 /*
609 * NOTE: not using the TAILQ_FOREACH here since in this loop vgone()
610 * and vclean() can be called indirectly
611 */
612 mutex_enter(&mntvnode_lock);
613 loop:
614 for (vp = TAILQ_FIRST(&mountp->mnt_vnodelist); vp; vp = vunmark(mvp)) {
615 vmark(mvp, vp);
616 if (vp->v_mount != mountp || vismarker(vp))
617 continue;
618 /*
619 * Step 4: invalidate all inactive vnodes.
620 */
621 if (vrecycle(vp, &mntvnode_lock, l)) {
622 mutex_enter(&mntvnode_lock);
623 (void)vunmark(mvp);
624 goto loop;
625 }
626 /*
627 * Step 5: invalidate all cached file data.
628 */
629 mutex_enter(&vp->v_interlock);
630 mutex_exit(&mntvnode_lock);
631 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK)) {
632 mutex_enter(&mntvnode_lock);
633 (void)vunmark(mvp);
634 goto loop;
635 }
636 if (vinvalbuf(vp, 0, cred, l, 0, 0))
637 panic("ext2fs_reload: dirty2");
638 /*
639 * Step 6: re-read inode data for all active vnodes.
640 */
641 ip = VTOI(vp);
642 error = bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
643 (int)fs->e2fs_bsize, NOCRED, 0, &bp);
644 if (error) {
645 vput(vp);
646 mutex_enter(&mntvnode_lock);
647 (void)vunmark(mvp);
648 break;
649 }
650 cp = (char *)bp->b_data +
651 (ino_to_fsbo(fs, ip->i_number) * EXT2_DINODE_SIZE);
652 e2fs_iload((struct ext2fs_dinode *)cp, ip->i_din.e2fs_din);
653 ext2fs_set_inode_guid(ip);
654 brelse(bp, 0);
655 vput(vp);
656 mutex_enter(&mntvnode_lock);
657 }
658 mutex_exit(&mntvnode_lock);
659 vnfree(mvp);
660 return (error);
661 }
662
663 /*
664 * Common code for mount and mountroot
665 */
666 int
667 ext2fs_mountfs(struct vnode *devvp, struct mount *mp)
668 {
669 struct lwp *l = curlwp;
670 struct ufsmount *ump;
671 struct buf *bp;
672 struct ext2fs *fs;
673 struct m_ext2fs *m_fs;
674 dev_t dev;
675 struct partinfo dpart;
676 int error, i, size, ronly;
677 kauth_cred_t cred;
678 struct proc *p;
679
680 dev = devvp->v_rdev;
681 p = l ? l->l_proc : NULL;
682 cred = l ? l->l_cred : NOCRED;
683
684 /* Flush out any old buffers remaining from a previous use. */
685 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
686 error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0);
687 VOP_UNLOCK(devvp, 0);
688 if (error)
689 return (error);
690
691 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
692 if (VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred) != 0)
693 size = DEV_BSIZE;
694 else
695 size = dpart.disklab->d_secsize;
696
697 bp = NULL;
698 ump = NULL;
699
700 #ifdef DEBUG_EXT2
701 printf("sb size: %d ino size %d\n", sizeof(struct ext2fs),
702 EXT2_DINODE_SIZE);
703 #endif
704 error = bread(devvp, (SBOFF / size), SBSIZE, cred, 0, &bp);
705 if (error)
706 goto out;
707 fs = (struct ext2fs *)bp->b_data;
708 error = ext2fs_checksb(fs, ronly);
709 if (error)
710 goto out;
711 ump = malloc(sizeof(*ump), M_UFSMNT, M_WAITOK);
712 memset(ump, 0, sizeof(*ump));
713 ump->um_fstype = UFS1;
714 ump->um_ops = &ext2fs_ufsops;
715 ump->um_e2fs = malloc(sizeof(struct m_ext2fs), M_UFSMNT, M_WAITOK);
716 memset(ump->um_e2fs, 0, sizeof(struct m_ext2fs));
717 e2fs_sbload((struct ext2fs *)bp->b_data, &ump->um_e2fs->e2fs);
718 brelse(bp, 0);
719 bp = NULL;
720 m_fs = ump->um_e2fs;
721 m_fs->e2fs_ronly = ronly;
722 if (ronly == 0) {
723 if (m_fs->e2fs.e2fs_state == E2FS_ISCLEAN)
724 m_fs->e2fs.e2fs_state = 0;
725 else
726 m_fs->e2fs.e2fs_state = E2FS_ERRORS;
727 m_fs->e2fs_fmod = 1;
728 }
729
730 /* compute dynamic sb infos */
731 m_fs->e2fs_ncg =
732 howmany(m_fs->e2fs.e2fs_bcount - m_fs->e2fs.e2fs_first_dblock,
733 m_fs->e2fs.e2fs_bpg);
734 /* XXX assume hw bsize = 512 */
735 m_fs->e2fs_fsbtodb = m_fs->e2fs.e2fs_log_bsize + 1;
736 m_fs->e2fs_bsize = MINBSIZE << m_fs->e2fs.e2fs_log_bsize;
737 m_fs->e2fs_bshift = LOG_MINBSIZE + m_fs->e2fs.e2fs_log_bsize;
738 m_fs->e2fs_qbmask = m_fs->e2fs_bsize - 1;
739 m_fs->e2fs_bmask = ~m_fs->e2fs_qbmask;
740 m_fs->e2fs_ngdb =
741 howmany(m_fs->e2fs_ncg, m_fs->e2fs_bsize / sizeof(struct ext2_gd));
742 m_fs->e2fs_ipb = m_fs->e2fs_bsize / EXT2_DINODE_SIZE;
743 m_fs->e2fs_itpg = m_fs->e2fs.e2fs_ipg / m_fs->e2fs_ipb;
744
745 m_fs->e2fs_gd = malloc(m_fs->e2fs_ngdb * m_fs->e2fs_bsize,
746 M_UFSMNT, M_WAITOK);
747 for (i = 0; i < m_fs->e2fs_ngdb; i++) {
748 error = bread(devvp ,
749 fsbtodb(m_fs, m_fs->e2fs.e2fs_first_dblock +
750 1 /* superblock */ + i),
751 m_fs->e2fs_bsize, NOCRED, 0, &bp);
752 if (error) {
753 free(m_fs->e2fs_gd, M_UFSMNT);
754 goto out;
755 }
756 e2fs_cgload((struct ext2_gd *)bp->b_data,
757 &m_fs->e2fs_gd[
758 i * m_fs->e2fs_bsize / sizeof(struct ext2_gd)],
759 m_fs->e2fs_bsize);
760 brelse(bp, 0);
761 bp = NULL;
762 }
763
764 mp->mnt_data = ump;
765 mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
766 mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_EXT2FS);
767 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
768 mp->mnt_stat.f_namemax = EXT2FS_MAXNAMLEN;
769 mp->mnt_flag |= MNT_LOCAL;
770 mp->mnt_dev_bshift = DEV_BSHIFT; /* XXX */
771 mp->mnt_fs_bshift = m_fs->e2fs_bshift;
772 mp->mnt_iflag |= IMNT_DTYPE;
773 ump->um_flags = 0;
774 ump->um_mountp = mp;
775 ump->um_dev = dev;
776 ump->um_devvp = devvp;
777 ump->um_nindir = NINDIR(m_fs);
778 ump->um_lognindir = ffs(NINDIR(m_fs)) - 1;
779 ump->um_bptrtodb = m_fs->e2fs_fsbtodb;
780 ump->um_seqinc = 1; /* no frags */
781 ump->um_maxsymlinklen = EXT2_MAXSYMLINKLEN;
782 ump->um_dirblksiz = m_fs->e2fs_bsize;
783 ump->um_maxfilesize = ((uint64_t)0x80000000 * m_fs->e2fs_bsize - 1);
784 devvp->v_specmountpoint = mp;
785 return (0);
786
787 out:
788 KASSERT(bp != NULL);
789 brelse(bp, 0);
790 if (ump) {
791 free(ump->um_e2fs, M_UFSMNT);
792 free(ump, M_UFSMNT);
793 mp->mnt_data = NULL;
794 }
795 return (error);
796 }
797
798 /*
799 * unmount system call
800 */
801 int
802 ext2fs_unmount(struct mount *mp, int mntflags)
803 {
804 struct ufsmount *ump;
805 struct m_ext2fs *fs;
806 int error, flags;
807
808 flags = 0;
809 if (mntflags & MNT_FORCE)
810 flags |= FORCECLOSE;
811 if ((error = ext2fs_flushfiles(mp, flags)) != 0)
812 return (error);
813 ump = VFSTOUFS(mp);
814 fs = ump->um_e2fs;
815 if (fs->e2fs_ronly == 0 &&
816 ext2fs_cgupdate(ump, MNT_WAIT) == 0 &&
817 (fs->e2fs.e2fs_state & E2FS_ERRORS) == 0) {
818 fs->e2fs.e2fs_state = E2FS_ISCLEAN;
819 (void) ext2fs_sbupdate(ump, MNT_WAIT);
820 }
821 if (ump->um_devvp->v_type != VBAD)
822 ump->um_devvp->v_specmountpoint = NULL;
823 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
824 error = VOP_CLOSE(ump->um_devvp, fs->e2fs_ronly ? FREAD : FREAD|FWRITE,
825 NOCRED);
826 vput(ump->um_devvp);
827 free(fs->e2fs_gd, M_UFSMNT);
828 free(fs, M_UFSMNT);
829 free(ump, M_UFSMNT);
830 mp->mnt_data = NULL;
831 mp->mnt_flag &= ~MNT_LOCAL;
832 return (error);
833 }
834
835 /*
836 * Flush out all the files in a filesystem.
837 */
838 int
839 ext2fs_flushfiles(struct mount *mp, int flags)
840 {
841 extern int doforce;
842 int error;
843
844 if (!doforce)
845 flags &= ~FORCECLOSE;
846 error = vflush(mp, NULLVP, flags);
847 return (error);
848 }
849
850 /*
851 * Get file system statistics.
852 */
853 int
854 ext2fs_statvfs(struct mount *mp, struct statvfs *sbp)
855 {
856 struct ufsmount *ump;
857 struct m_ext2fs *fs;
858 uint32_t overhead, overhead_per_group, ngdb;
859 int i, ngroups;
860
861 ump = VFSTOUFS(mp);
862 fs = ump->um_e2fs;
863 if (fs->e2fs.e2fs_magic != E2FS_MAGIC)
864 panic("ext2fs_statvfs");
865
866 /*
867 * Compute the overhead (FS structures)
868 */
869 overhead_per_group =
870 1 /* block bitmap */ +
871 1 /* inode bitmap */ +
872 fs->e2fs_itpg;
873 overhead = fs->e2fs.e2fs_first_dblock +
874 fs->e2fs_ncg * overhead_per_group;
875 if (fs->e2fs.e2fs_rev > E2FS_REV0 &&
876 fs->e2fs.e2fs_features_rocompat & EXT2F_ROCOMPAT_SPARSESUPER) {
877 for (i = 0, ngroups = 0; i < fs->e2fs_ncg; i++) {
878 if (cg_has_sb(i))
879 ngroups++;
880 }
881 } else {
882 ngroups = fs->e2fs_ncg;
883 }
884 ngdb = fs->e2fs_ngdb;
885 if (fs->e2fs.e2fs_rev > E2FS_REV0 &&
886 fs->e2fs.e2fs_features_compat & EXT2F_COMPAT_RESIZE)
887 ngdb += fs->e2fs.e2fs_reserved_ngdb;
888 overhead += ngroups * (1 /* superblock */ + ngdb);
889
890 sbp->f_bsize = fs->e2fs_bsize;
891 sbp->f_frsize = MINBSIZE << fs->e2fs.e2fs_fsize;
892 sbp->f_iosize = fs->e2fs_bsize;
893 sbp->f_blocks = fs->e2fs.e2fs_bcount - overhead;
894 sbp->f_bfree = fs->e2fs.e2fs_fbcount;
895 sbp->f_bresvd = fs->e2fs.e2fs_rbcount;
896 if (sbp->f_bfree > sbp->f_bresvd)
897 sbp->f_bavail = sbp->f_bfree - sbp->f_bresvd;
898 else
899 sbp->f_bavail = 0;
900 sbp->f_files = fs->e2fs.e2fs_icount;
901 sbp->f_ffree = fs->e2fs.e2fs_ficount;
902 sbp->f_favail = fs->e2fs.e2fs_ficount;
903 sbp->f_fresvd = 0;
904 copy_statvfs_info(sbp, mp);
905 return (0);
906 }
907
908 /*
909 * Go through the disk queues to initiate sandbagged IO;
910 * go through the inodes to write those that have been modified;
911 * initiate the writing of the super block if it has been modified.
912 *
913 * Note: we are always called with the filesystem marked `MPBUSY'.
914 */
915 int
916 ext2fs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
917 {
918 struct vnode *vp, *mvp;
919 struct inode *ip;
920 struct ufsmount *ump = VFSTOUFS(mp);
921 struct m_ext2fs *fs;
922 int error, allerror = 0;
923
924 fs = ump->um_e2fs;
925 if (fs->e2fs_fmod != 0 && fs->e2fs_ronly != 0) { /* XXX */
926 printf("fs = %s\n", fs->e2fs_fsmnt);
927 panic("update: rofs mod");
928 }
929
930 /* Allocate a marker vnode. */
931 if ((mvp = vnalloc(mp)) == NULL)
932 return (ENOMEM);
933
934 /*
935 * Write back each (modified) inode.
936 */
937 mutex_enter(&mntvnode_lock);
938 loop:
939 /*
940 * NOTE: not using the TAILQ_FOREACH here since in this loop vgone()
941 * and vclean() can be called indirectly
942 */
943 for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = vunmark(mvp)) {
944 vmark(mvp, vp);
945 if (vp->v_mount != mp || vismarker(vp))
946 continue;
947 mutex_enter(&vp->v_interlock);
948 ip = VTOI(vp);
949 if (ip == NULL || (vp->v_iflag & (VI_XLOCK|VI_CLEAN)) != 0 ||
950 vp->v_type == VNON ||
951 ((ip->i_flag &
952 (IN_CHANGE | IN_UPDATE | IN_MODIFIED)) == 0 &&
953 LIST_EMPTY(&vp->v_dirtyblkhd) &&
954 UVM_OBJ_IS_CLEAN(&vp->v_uobj)))
955 {
956 mutex_exit(&vp->v_interlock);
957 continue;
958 }
959 mutex_exit(&mntvnode_lock);
960 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
961 if (error) {
962 mutex_enter(&mntvnode_lock);
963 if (error == ENOENT) {
964 mutex_enter(&mntvnode_lock);
965 (void)vunmark(mvp);
966 goto loop;
967 }
968 continue;
969 }
970 if (vp->v_type == VREG && waitfor == MNT_LAZY)
971 error = ext2fs_update(vp, NULL, NULL, 0);
972 else
973 error = VOP_FSYNC(vp, cred,
974 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0);
975 if (error)
976 allerror = error;
977 vput(vp);
978 mutex_enter(&mntvnode_lock);
979 }
980 mutex_exit(&mntvnode_lock);
981 vnfree(mvp);
982 /*
983 * Force stale file system control information to be flushed.
984 */
985 if (waitfor != MNT_LAZY) {
986 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
987 if ((error = VOP_FSYNC(ump->um_devvp, cred,
988 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0)) != 0)
989 allerror = error;
990 VOP_UNLOCK(ump->um_devvp, 0);
991 }
992 /*
993 * Write back modified superblock.
994 */
995 if (fs->e2fs_fmod != 0) {
996 fs->e2fs_fmod = 0;
997 fs->e2fs.e2fs_wtime = time_second;
998 if ((error = ext2fs_cgupdate(ump, waitfor)))
999 allerror = error;
1000 }
1001 return (allerror);
1002 }
1003
1004 /*
1005 * Look up a EXT2FS dinode number to find its incore vnode, otherwise read it
1006 * in from disk. If it is in core, wait for the lock bit to clear, then
1007 * return the inode locked. Detection and handling of mount points must be
1008 * done by the calling routine.
1009 */
1010 int
1011 ext2fs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
1012 {
1013 struct m_ext2fs *fs;
1014 struct inode *ip;
1015 struct ufsmount *ump;
1016 struct buf *bp;
1017 struct vnode *vp;
1018 dev_t dev;
1019 int error;
1020 void *cp;
1021
1022 ump = VFSTOUFS(mp);
1023 dev = ump->um_dev;
1024 retry:
1025 if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
1026 return (0);
1027
1028 /* Allocate a new vnode/inode. */
1029 if ((error = getnewvnode(VT_EXT2FS, mp, ext2fs_vnodeop_p, &vp)) != 0) {
1030 *vpp = NULL;
1031 return (error);
1032 }
1033 ip = pool_get(&ext2fs_inode_pool, PR_WAITOK);
1034
1035 mutex_enter(&ufs_hashlock);
1036 if ((*vpp = ufs_ihashget(dev, ino, 0)) != NULL) {
1037 mutex_exit(&ufs_hashlock);
1038 ungetnewvnode(vp);
1039 pool_put(&ext2fs_inode_pool, ip);
1040 goto retry;
1041 }
1042
1043 vp->v_vflag |= VV_LOCKSWORK;
1044
1045 memset(ip, 0, sizeof(struct inode));
1046 vp->v_data = ip;
1047 ip->i_vnode = vp;
1048 ip->i_ump = ump;
1049 ip->i_e2fs = fs = ump->um_e2fs;
1050 ip->i_dev = dev;
1051 ip->i_number = ino;
1052 ip->i_e2fs_last_lblk = 0;
1053 ip->i_e2fs_last_blk = 0;
1054 genfs_node_init(vp, &ext2fs_genfsops);
1055
1056 /*
1057 * Put it onto its hash chain and lock it so that other requests for
1058 * this inode will block if they arrive while we are sleeping waiting
1059 * for old data structures to be purged or for the contents of the
1060 * disk portion of this inode to be read.
1061 */
1062
1063 ufs_ihashins(ip);
1064 mutex_exit(&ufs_hashlock);
1065
1066 /* Read in the disk contents for the inode, copy into the inode. */
1067 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1068 (int)fs->e2fs_bsize, NOCRED, 0, &bp);
1069 if (error) {
1070
1071 /*
1072 * The inode does not contain anything useful, so it would
1073 * be misleading to leave it on its hash chain. With mode
1074 * still zero, it will be unlinked and returned to the free
1075 * list by vput().
1076 */
1077
1078 vput(vp);
1079 brelse(bp, 0);
1080 *vpp = NULL;
1081 return (error);
1082 }
1083 cp = (char *)bp->b_data + (ino_to_fsbo(fs, ino) * EXT2_DINODE_SIZE);
1084 ip->i_din.e2fs_din = pool_get(&ext2fs_dinode_pool, PR_WAITOK);
1085 e2fs_iload((struct ext2fs_dinode *)cp, ip->i_din.e2fs_din);
1086 ext2fs_set_inode_guid(ip);
1087 brelse(bp, 0);
1088
1089 /* If the inode was deleted, reset all fields */
1090 if (ip->i_e2fs_dtime != 0) {
1091 ip->i_e2fs_mode = ip->i_e2fs_nblock = 0;
1092 (void)ext2fs_setsize(ip, 0);
1093 memset(ip->i_e2fs_blocks, 0, sizeof(ip->i_e2fs_blocks));
1094 }
1095
1096 /*
1097 * Initialize the vnode from the inode, check for aliases.
1098 * Note that the underlying vnode may have changed.
1099 */
1100
1101 error = ext2fs_vinit(mp, ext2fs_specop_p, ext2fs_fifoop_p, &vp);
1102 if (error) {
1103 vput(vp);
1104 *vpp = NULL;
1105 return (error);
1106 }
1107 /*
1108 * Finish inode initialization now that aliasing has been resolved.
1109 */
1110
1111 ip->i_devvp = ump->um_devvp;
1112 VREF(ip->i_devvp);
1113
1114 /*
1115 * Set up a generation number for this inode if it does not
1116 * already have one. This should only happen on old filesystems.
1117 */
1118
1119 if (ip->i_e2fs_gen == 0) {
1120 if (++ext2gennumber < (u_long)time_second)
1121 ext2gennumber = time_second;
1122 ip->i_e2fs_gen = ext2gennumber;
1123 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
1124 ip->i_flag |= IN_MODIFIED;
1125 }
1126 uvm_vnp_setsize(vp, ext2fs_size(ip));
1127 *vpp = vp;
1128 return (0);
1129 }
1130
1131 /*
1132 * File handle to vnode
1133 *
1134 * Have to be really careful about stale file handles:
1135 * - check that the inode number is valid
1136 * - call ext2fs_vget() to get the locked inode
1137 * - check for an unallocated inode (i_mode == 0)
1138 */
1139 int
1140 ext2fs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
1141 {
1142 struct inode *ip;
1143 struct vnode *nvp;
1144 int error;
1145 struct ufid ufh;
1146 struct m_ext2fs *fs;
1147
1148 if (fhp->fid_len != sizeof(struct ufid))
1149 return EINVAL;
1150
1151 memcpy(&ufh, fhp, sizeof(struct ufid));
1152 fs = VFSTOUFS(mp)->um_e2fs;
1153 if ((ufh.ufid_ino < EXT2_FIRSTINO && ufh.ufid_ino != EXT2_ROOTINO) ||
1154 ufh.ufid_ino >= fs->e2fs_ncg * fs->e2fs.e2fs_ipg)
1155 return (ESTALE);
1156
1157 if ((error = VFS_VGET(mp, ufh.ufid_ino, &nvp)) != 0) {
1158 *vpp = NULLVP;
1159 return (error);
1160 }
1161 ip = VTOI(nvp);
1162 if (ip->i_e2fs_mode == 0 || ip->i_e2fs_dtime != 0 ||
1163 ip->i_e2fs_gen != ufh.ufid_gen) {
1164 vput(nvp);
1165 *vpp = NULLVP;
1166 return (ESTALE);
1167 }
1168 *vpp = nvp;
1169 return (0);
1170 }
1171
1172 /*
1173 * Vnode pointer to File handle
1174 */
1175 /* ARGSUSED */
1176 int
1177 ext2fs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
1178 {
1179 struct inode *ip;
1180 struct ufid ufh;
1181
1182 if (*fh_size < sizeof(struct ufid)) {
1183 *fh_size = sizeof(struct ufid);
1184 return E2BIG;
1185 }
1186 *fh_size = sizeof(struct ufid);
1187
1188 ip = VTOI(vp);
1189 memset(&ufh, 0, sizeof(ufh));
1190 ufh.ufid_len = sizeof(struct ufid);
1191 ufh.ufid_ino = ip->i_number;
1192 ufh.ufid_gen = ip->i_e2fs_gen;
1193 memcpy(fhp, &ufh, sizeof(ufh));
1194 return (0);
1195 }
1196
1197 /*
1198 * Write a superblock and associated information back to disk.
1199 */
1200 int
1201 ext2fs_sbupdate(struct ufsmount *mp, int waitfor)
1202 {
1203 struct m_ext2fs *fs = mp->um_e2fs;
1204 struct buf *bp;
1205 int error = 0;
1206
1207 bp = getblk(mp->um_devvp, SBLOCK, SBSIZE, 0, 0);
1208 e2fs_sbsave(&fs->e2fs, (struct ext2fs*)bp->b_data);
1209 if (waitfor == MNT_WAIT)
1210 error = bwrite(bp);
1211 else
1212 bawrite(bp);
1213 return (error);
1214 }
1215
1216 int
1217 ext2fs_cgupdate(struct ufsmount *mp, int waitfor)
1218 {
1219 struct m_ext2fs *fs = mp->um_e2fs;
1220 struct buf *bp;
1221 int i, error = 0, allerror = 0;
1222
1223 allerror = ext2fs_sbupdate(mp, waitfor);
1224 for (i = 0; i < fs->e2fs_ngdb; i++) {
1225 bp = getblk(mp->um_devvp, fsbtodb(fs,
1226 fs->e2fs.e2fs_first_dblock +
1227 1 /* superblock */ + i), fs->e2fs_bsize, 0, 0);
1228 e2fs_cgsave(&fs->e2fs_gd[
1229 i * fs->e2fs_bsize / sizeof(struct ext2_gd)],
1230 (struct ext2_gd *)bp->b_data, fs->e2fs_bsize);
1231 if (waitfor == MNT_WAIT)
1232 error = bwrite(bp);
1233 else
1234 bawrite(bp);
1235 }
1236
1237 if (!allerror && error)
1238 allerror = error;
1239 return (allerror);
1240 }
1241
1242 static int
1243 ext2fs_checksb(struct ext2fs *fs, int ronly)
1244 {
1245
1246 if (fs2h16(fs->e2fs_magic) != E2FS_MAGIC) {
1247 return (EINVAL); /* XXX needs translation */
1248 }
1249 if (fs2h32(fs->e2fs_rev) > E2FS_REV1) {
1250 #ifdef DIAGNOSTIC
1251 printf("Ext2 fs: unsupported revision number: %x\n",
1252 fs2h32(fs->e2fs_rev));
1253 #endif
1254 return (EINVAL); /* XXX needs translation */
1255 }
1256 if (fs2h32(fs->e2fs_log_bsize) > 2) { /* block size = 1024|2048|4096 */
1257 #ifdef DIAGNOSTIC
1258 printf("Ext2 fs: bad block size: %d "
1259 "(expected <= 2 for ext2 fs)\n",
1260 fs2h32(fs->e2fs_log_bsize));
1261 #endif
1262 return (EINVAL); /* XXX needs translation */
1263 }
1264 if (fs2h32(fs->e2fs_rev) > E2FS_REV0) {
1265 if (fs2h32(fs->e2fs_first_ino) != EXT2_FIRSTINO ||
1266 fs2h16(fs->e2fs_inode_size) != EXT2_DINODE_SIZE) {
1267 printf("Ext2 fs: unsupported inode size\n");
1268 return (EINVAL); /* XXX needs translation */
1269 }
1270 if (fs2h32(fs->e2fs_features_incompat) &
1271 ~EXT2F_INCOMPAT_SUPP) {
1272 printf("Ext2 fs: unsupported optional feature\n");
1273 return (EINVAL); /* XXX needs translation */
1274 }
1275 if (!ronly && fs2h32(fs->e2fs_features_rocompat) &
1276 ~EXT2F_ROCOMPAT_SUPP) {
1277 return (EROFS); /* XXX needs translation */
1278 }
1279 }
1280 return (0);
1281 }
1282