ext2fs_vfsops.c revision 1.189 1 /* $NetBSD: ext2fs_vfsops.c,v 1.189 2015/02/22 14:55:23 maxv 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 *
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.
57 *
58 * @(#)ffs_vfsops.c 8.14 (Berkeley) 11/28/94
59 * Modified for ext2fs by Manuel Bouyer.
60 */
61
62 #include <sys/cdefs.h>
63 __KERNEL_RCSID(0, "$NetBSD: ext2fs_vfsops.c,v 1.189 2015/02/22 14:55:23 maxv Exp $");
64
65 #if defined(_KERNEL_OPT)
66 #include "opt_compat_netbsd.h"
67 #endif
68
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/sysctl.h>
72 #include <sys/namei.h>
73 #include <sys/proc.h>
74 #include <sys/kernel.h>
75 #include <sys/vnode.h>
76 #include <sys/socket.h>
77 #include <sys/mount.h>
78 #include <sys/buf.h>
79 #include <sys/device.h>
80 #include <sys/mbuf.h>
81 #include <sys/file.h>
82 #include <sys/disklabel.h>
83 #include <sys/ioctl.h>
84 #include <sys/errno.h>
85 #include <sys/pool.h>
86 #include <sys/lock.h>
87 #include <sys/conf.h>
88 #include <sys/kauth.h>
89 #include <sys/module.h>
90
91 #include <miscfs/genfs/genfs.h>
92 #include <miscfs/specfs/specdev.h>
93
94 #include <ufs/ufs/quota.h>
95 #include <ufs/ufs/ufsmount.h>
96 #include <ufs/ufs/inode.h>
97 #include <ufs/ufs/dir.h>
98 #include <ufs/ufs/ufs_extern.h>
99
100 #include <ufs/ext2fs/ext2fs.h>
101 #include <ufs/ext2fs/ext2fs_dir.h>
102 #include <ufs/ext2fs/ext2fs_extern.h>
103
104 MODULE(MODULE_CLASS_VFS, ext2fs, "ffs");
105
106 int ext2fs_sbupdate(struct ufsmount *, int);
107 static int ext2fs_sbfill(struct m_ext2fs *, int);
108
109 static struct sysctllog *ext2fs_sysctl_log;
110
111 extern const struct vnodeopv_desc ext2fs_vnodeop_opv_desc;
112 extern const struct vnodeopv_desc ext2fs_specop_opv_desc;
113 extern const struct vnodeopv_desc ext2fs_fifoop_opv_desc;
114
115 const struct vnodeopv_desc * const ext2fs_vnodeopv_descs[] = {
116 &ext2fs_vnodeop_opv_desc,
117 &ext2fs_specop_opv_desc,
118 &ext2fs_fifoop_opv_desc,
119 NULL,
120 };
121
122 struct vfsops ext2fs_vfsops = {
123 .vfs_name = MOUNT_EXT2FS,
124 .vfs_min_mount_data = sizeof (struct ufs_args),
125 .vfs_mount = ext2fs_mount,
126 .vfs_start = ufs_start,
127 .vfs_unmount = ext2fs_unmount,
128 .vfs_root = ufs_root,
129 .vfs_quotactl = ufs_quotactl,
130 .vfs_statvfs = ext2fs_statvfs,
131 .vfs_sync = ext2fs_sync,
132 .vfs_vget = ufs_vget,
133 .vfs_loadvnode = ext2fs_loadvnode,
134 .vfs_fhtovp = ext2fs_fhtovp,
135 .vfs_vptofh = ext2fs_vptofh,
136 .vfs_init = ext2fs_init,
137 .vfs_reinit = ext2fs_reinit,
138 .vfs_done = ext2fs_done,
139 .vfs_mountroot = ext2fs_mountroot,
140 .vfs_snapshot = (void *)eopnotsupp,
141 .vfs_extattrctl = vfs_stdextattrctl,
142 .vfs_suspendctl = (void *)eopnotsupp,
143 .vfs_renamelock_enter = genfs_renamelock_enter,
144 .vfs_renamelock_exit = genfs_renamelock_exit,
145 .vfs_fsync = (void *)eopnotsupp,
146 .vfs_opv_descs = ext2fs_vnodeopv_descs
147 };
148
149 static const struct genfs_ops ext2fs_genfsops = {
150 .gop_size = genfs_size,
151 .gop_alloc = ext2fs_gop_alloc,
152 .gop_write = genfs_gop_write,
153 .gop_markupdate = ufs_gop_markupdate,
154 };
155
156 static const struct ufs_ops ext2fs_ufsops = {
157 .uo_itimes = ext2fs_itimes,
158 .uo_update = ext2fs_update,
159 .uo_vfree = ext2fs_vfree,
160 };
161
162 /* Fill in the inode uid/gid from ext2 halves. */
163 void
164 ext2fs_set_inode_guid(struct inode *ip)
165 {
166
167 ip->i_gid = ip->i_e2fs_gid;
168 ip->i_uid = ip->i_e2fs_uid;
169 if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0) {
170 ip->i_gid |= ip->i_e2fs_gid_high << 16;
171 ip->i_uid |= ip->i_e2fs_uid_high << 16;
172 }
173 }
174
175 static int
176 ext2fs_modcmd(modcmd_t cmd, void *arg)
177 {
178 int error;
179
180 switch (cmd) {
181 case MODULE_CMD_INIT:
182 error = vfs_attach(&ext2fs_vfsops);
183 if (error != 0)
184 break;
185 sysctl_createv(&ext2fs_sysctl_log, 0, NULL, NULL,
186 CTLFLAG_PERMANENT,
187 CTLTYPE_NODE, "ext2fs",
188 SYSCTL_DESCR("Linux EXT2FS file system"),
189 NULL, 0, NULL, 0,
190 CTL_VFS, 17, CTL_EOL);
191 /*
192 * XXX the "17" above could be dynamic, thereby eliminating
193 * one more instance of the "number to vfs" mapping problem,
194 * but "17" is the order as taken from sys/mount.h
195 */
196 break;
197 case MODULE_CMD_FINI:
198 error = vfs_detach(&ext2fs_vfsops);
199 if (error != 0)
200 break;
201 sysctl_teardown(&ext2fs_sysctl_log);
202 break;
203 default:
204 error = ENOTTY;
205 break;
206 }
207
208 return (error);
209 }
210
211 /*
212 * XXX Same structure as FFS inodes? Should we share a common pool?
213 */
214 struct pool ext2fs_inode_pool;
215 struct pool ext2fs_dinode_pool;
216
217 extern u_long ext2gennumber;
218
219 void
220 ext2fs_init(void)
221 {
222
223 pool_init(&ext2fs_inode_pool, sizeof(struct inode), 0, 0, 0,
224 "ext2fsinopl", &pool_allocator_nointr, IPL_NONE);
225 pool_init(&ext2fs_dinode_pool, sizeof(struct ext2fs_dinode), 0, 0, 0,
226 "ext2dinopl", &pool_allocator_nointr, IPL_NONE);
227 ufs_init();
228 }
229
230 void
231 ext2fs_reinit(void)
232 {
233 ufs_reinit();
234 }
235
236 void
237 ext2fs_done(void)
238 {
239
240 ufs_done();
241 pool_destroy(&ext2fs_inode_pool);
242 pool_destroy(&ext2fs_dinode_pool);
243 }
244
245 /*
246 * Called by main() when ext2fs is going to be mounted as root.
247 *
248 * Name is updated by mount(8) after booting.
249 */
250
251 int
252 ext2fs_mountroot(void)
253 {
254 extern struct vnode *rootvp;
255 struct m_ext2fs *fs;
256 struct mount *mp;
257 struct ufsmount *ump;
258 int error;
259
260 if (device_class(root_device) != DV_DISK)
261 return (ENODEV);
262
263 if ((error = vfs_rootmountalloc(MOUNT_EXT2FS, "root_device", &mp))) {
264 vrele(rootvp);
265 return (error);
266 }
267
268 if ((error = ext2fs_mountfs(rootvp, mp)) != 0) {
269 vfs_unbusy(mp, false, NULL);
270 vfs_destroy(mp);
271 return (error);
272 }
273 mountlist_append(mp);
274 ump = VFSTOUFS(mp);
275 fs = ump->um_e2fs;
276 memset(fs->e2fs_fsmnt, 0, sizeof(fs->e2fs_fsmnt));
277 (void) copystr(mp->mnt_stat.f_mntonname, fs->e2fs_fsmnt,
278 sizeof(fs->e2fs_fsmnt) - 1, 0);
279 if (fs->e2fs.e2fs_rev > E2FS_REV0) {
280 memset(fs->e2fs.e2fs_fsmnt, 0, sizeof(fs->e2fs.e2fs_fsmnt));
281 (void) copystr(mp->mnt_stat.f_mntonname, fs->e2fs.e2fs_fsmnt,
282 sizeof(fs->e2fs.e2fs_fsmnt) - 1, 0);
283 }
284 (void)ext2fs_statvfs(mp, &mp->mnt_stat);
285 vfs_unbusy(mp, false, NULL);
286 setrootfstime((time_t)fs->e2fs.e2fs_wtime);
287 return (0);
288 }
289
290 /*
291 * VFS Operations.
292 *
293 * mount system call
294 */
295 int
296 ext2fs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
297 {
298 struct lwp *l = curlwp;
299 struct vnode *devvp;
300 struct ufs_args *args = data;
301 struct ufsmount *ump = NULL;
302 struct m_ext2fs *fs;
303 size_t size;
304 int error = 0, flags, update;
305 mode_t accessmode;
306
307 if (args == NULL)
308 return EINVAL;
309 if (*data_len < sizeof *args)
310 return EINVAL;
311
312 if (mp->mnt_flag & MNT_GETARGS) {
313 ump = VFSTOUFS(mp);
314 if (ump == NULL)
315 return EIO;
316 memset(args, 0, sizeof *args);
317 args->fspec = NULL;
318 *data_len = sizeof *args;
319 return 0;
320 }
321
322 update = mp->mnt_flag & MNT_UPDATE;
323
324 /* Check arguments */
325 if (args->fspec != NULL) {
326 /*
327 * Look up the name and verify that it's sane.
328 */
329 error = namei_simple_user(args->fspec,
330 NSM_FOLLOW_NOEMULROOT, &devvp);
331 if (error != 0)
332 return (error);
333
334 if (!update) {
335 /*
336 * Be sure this is a valid block device
337 */
338 if (devvp->v_type != VBLK)
339 error = ENOTBLK;
340 else if (bdevsw_lookup(devvp->v_rdev) == NULL)
341 error = ENXIO;
342 } else {
343 /*
344 * Be sure we're still naming the same device
345 * used for our initial mount
346 */
347 ump = VFSTOUFS(mp);
348 if (devvp != ump->um_devvp) {
349 if (devvp->v_rdev != ump->um_devvp->v_rdev)
350 error = EINVAL;
351 else {
352 vrele(devvp);
353 devvp = ump->um_devvp;
354 vref(devvp);
355 }
356 }
357 }
358 } else {
359 if (!update) {
360 /* New mounts must have a filename for the device */
361 return (EINVAL);
362 } else {
363 ump = VFSTOUFS(mp);
364 devvp = ump->um_devvp;
365 vref(devvp);
366 }
367 }
368
369 /*
370 * If mount by non-root, then verify that user has necessary
371 * permissions on the device.
372 *
373 * Permission to update a mount is checked higher, so here we presume
374 * updating the mount is okay (for example, as far as securelevel goes)
375 * which leaves us with the normal check.
376 */
377 if (error == 0) {
378 accessmode = VREAD;
379 if (update ?
380 (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
381 (mp->mnt_flag & MNT_RDONLY) == 0)
382 accessmode |= VWRITE;
383 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
384 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
385 KAUTH_REQ_SYSTEM_MOUNT_DEVICE, mp, devvp,
386 KAUTH_ARG(accessmode));
387 VOP_UNLOCK(devvp);
388 }
389
390 if (error) {
391 vrele(devvp);
392 return (error);
393 }
394
395 if (!update) {
396 int xflags;
397
398 if (mp->mnt_flag & MNT_RDONLY)
399 xflags = FREAD;
400 else
401 xflags = FREAD|FWRITE;
402 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
403 error = VOP_OPEN(devvp, xflags, FSCRED);
404 VOP_UNLOCK(devvp);
405 if (error)
406 goto fail;
407 error = ext2fs_mountfs(devvp, mp);
408 if (error) {
409 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
410 (void)VOP_CLOSE(devvp, xflags, NOCRED);
411 VOP_UNLOCK(devvp);
412 goto fail;
413 }
414
415 ump = VFSTOUFS(mp);
416 fs = ump->um_e2fs;
417 } else {
418 /*
419 * Update the mount.
420 */
421
422 /*
423 * The initial mount got a reference on this
424 * device, so drop the one obtained via
425 * namei(), above.
426 */
427 vrele(devvp);
428
429 ump = VFSTOUFS(mp);
430 fs = ump->um_e2fs;
431 if (fs->e2fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
432 /*
433 * Changing from r/w to r/o
434 */
435 flags = WRITECLOSE;
436 if (mp->mnt_flag & MNT_FORCE)
437 flags |= FORCECLOSE;
438 error = ext2fs_flushfiles(mp, flags);
439 if (error == 0 &&
440 ext2fs_cgupdate(ump, MNT_WAIT) == 0 &&
441 (fs->e2fs.e2fs_state & E2FS_ERRORS) == 0) {
442 fs->e2fs.e2fs_state = E2FS_ISCLEAN;
443 (void) ext2fs_sbupdate(ump, MNT_WAIT);
444 }
445 if (error)
446 return (error);
447 fs->e2fs_ronly = 1;
448 }
449
450 if (mp->mnt_flag & MNT_RELOAD) {
451 error = ext2fs_reload(mp, l->l_cred, l);
452 if (error)
453 return (error);
454 }
455
456 if (fs->e2fs_ronly && (mp->mnt_iflag & IMNT_WANTRDWR)) {
457 /*
458 * Changing from read-only to read/write
459 */
460 fs->e2fs_ronly = 0;
461 if (fs->e2fs.e2fs_state == E2FS_ISCLEAN)
462 fs->e2fs.e2fs_state = 0;
463 else
464 fs->e2fs.e2fs_state = E2FS_ERRORS;
465 fs->e2fs_fmod = 1;
466 }
467 if (args->fspec == NULL)
468 return 0;
469 }
470
471 error = set_statvfs_info(path, UIO_USERSPACE, args->fspec,
472 UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
473 (void) copystr(mp->mnt_stat.f_mntonname, fs->e2fs_fsmnt,
474 sizeof(fs->e2fs_fsmnt) - 1, &size);
475 memset(fs->e2fs_fsmnt + size, 0, sizeof(fs->e2fs_fsmnt) - size);
476 if (fs->e2fs.e2fs_rev > E2FS_REV0) {
477 (void) copystr(mp->mnt_stat.f_mntonname, fs->e2fs.e2fs_fsmnt,
478 sizeof(fs->e2fs.e2fs_fsmnt) - 1, &size);
479 memset(fs->e2fs.e2fs_fsmnt, 0,
480 sizeof(fs->e2fs.e2fs_fsmnt) - size);
481 }
482 if (fs->e2fs_fmod != 0) { /* XXX */
483 fs->e2fs_fmod = 0;
484 if (fs->e2fs.e2fs_state == 0)
485 fs->e2fs.e2fs_wtime = time_second;
486 else
487 printf("%s: file system not clean; please fsck(8)\n",
488 mp->mnt_stat.f_mntfromname);
489 (void) ext2fs_cgupdate(ump, MNT_WAIT);
490 }
491 return (error);
492
493 fail:
494 vrele(devvp);
495 return (error);
496 }
497
498 /*
499 * Reload all incore data for a filesystem (used after running fsck on
500 * the root filesystem and finding things to fix). The filesystem must
501 * be mounted read-only.
502 *
503 * Things to do to update the mount:
504 * 1) invalidate all cached meta-data.
505 * 2) re-read superblock from disk.
506 * 3) re-read summary information from disk.
507 * 4) invalidate all inactive vnodes.
508 * 5) invalidate all cached file data.
509 * 6) re-read inode data for all active vnodes.
510 */
511 int
512 ext2fs_reload(struct mount *mp, kauth_cred_t cred, struct lwp *l)
513 {
514 struct vnode *vp, *devvp;
515 struct inode *ip;
516 struct buf *bp;
517 struct m_ext2fs *fs;
518 struct ext2fs *newfs;
519 int i, error;
520 void *cp;
521 struct ufsmount *ump;
522 struct vnode_iterator *marker;
523
524 if ((mp->mnt_flag & MNT_RDONLY) == 0)
525 return (EINVAL);
526
527 ump = VFSTOUFS(mp);
528 /*
529 * Step 1: invalidate all cached meta-data.
530 */
531 devvp = ump->um_devvp;
532 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
533 error = vinvalbuf(devvp, 0, cred, l, 0, 0);
534 VOP_UNLOCK(devvp);
535 if (error)
536 panic("ext2fs_reload: dirty1");
537
538 fs = ump->um_e2fs;
539 /*
540 * Step 2: re-read superblock from disk. Copy in new superblock, and compute
541 * in-memory values.
542 */
543 error = bread(devvp, SBLOCK, SBSIZE, NOCRED, 0, &bp);
544 if (error)
545 return error;
546 newfs = (struct ext2fs *)bp->b_data;
547 e2fs_sbload(newfs, &fs->e2fs);
548
549 brelse(bp, 0);
550
551 error = ext2fs_sbfill(fs, (mp->mnt_flag & MNT_RDONLY) != 0);
552 if (error)
553 return error;
554
555 /*
556 * Step 3: re-read summary information from disk.
557 */
558 for (i = 0; i < fs->e2fs_ngdb; i++) {
559 error = bread(devvp ,
560 EXT2_FSBTODB(fs, fs->e2fs.e2fs_first_dblock +
561 1 /* superblock */ + i),
562 fs->e2fs_bsize, NOCRED, 0, &bp);
563 if (error) {
564 return (error);
565 }
566 e2fs_cgload((struct ext2_gd *)bp->b_data,
567 &fs->e2fs_gd[i * fs->e2fs_bsize / sizeof(struct ext2_gd)],
568 fs->e2fs_bsize);
569 brelse(bp, 0);
570 }
571
572 vfs_vnode_iterator_init(mp, &marker);
573 while ((vp = vfs_vnode_iterator_next(marker, NULL, NULL))) {
574 /*
575 * Step 4: invalidate all inactive vnodes.
576 */
577 if (vrecycle(vp))
578 continue;
579 /*
580 * Step 5: invalidate all cached file data.
581 */
582 if (vn_lock(vp, LK_EXCLUSIVE)) {
583 vrele(vp);
584 continue;
585 }
586 if (vinvalbuf(vp, 0, cred, l, 0, 0))
587 panic("ext2fs_reload: dirty2");
588 /*
589 * Step 6: re-read inode data for all active vnodes.
590 */
591 ip = VTOI(vp);
592 error = bread(devvp, EXT2_FSBTODB(fs, ino_to_fsba(fs, ip->i_number)),
593 (int)fs->e2fs_bsize, NOCRED, 0, &bp);
594 if (error) {
595 vput(vp);
596 break;
597 }
598 cp = (char *)bp->b_data +
599 (ino_to_fsbo(fs, ip->i_number) * EXT2_DINODE_SIZE(fs));
600 e2fs_iload((struct ext2fs_dinode *)cp, ip->i_din.e2fs_din);
601 ext2fs_set_inode_guid(ip);
602 brelse(bp, 0);
603 vput(vp);
604 }
605 vfs_vnode_iterator_destroy(marker);
606 return (error);
607 }
608
609 /*
610 * Common code for mount and mountroot
611 */
612 int
613 ext2fs_mountfs(struct vnode *devvp, struct mount *mp)
614 {
615 struct lwp *l = curlwp;
616 struct ufsmount *ump;
617 struct buf *bp;
618 struct ext2fs *fs;
619 struct m_ext2fs *m_fs;
620 dev_t dev;
621 int error, i, ronly;
622 kauth_cred_t cred;
623
624 dev = devvp->v_rdev;
625 cred = l->l_cred;
626
627 /* Flush out any old buffers remaining from a previous use. */
628 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
629 error = vinvalbuf(devvp, V_SAVE, cred, l, 0, 0);
630 VOP_UNLOCK(devvp);
631 if (error)
632 return (error);
633
634 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
635
636 bp = NULL;
637 ump = NULL;
638
639 /* Read the superblock from disk, and swap it directly. */
640 error = bread(devvp, SBLOCK, SBSIZE, cred, 0, &bp);
641 if (error)
642 goto out;
643 fs = (struct ext2fs *)bp->b_data;
644 m_fs = kmem_zalloc(sizeof(struct m_ext2fs), KM_SLEEP);
645 e2fs_sbload(fs, &m_fs->e2fs);
646
647 brelse(bp, 0);
648 bp = NULL;
649
650 /* Once swapped, validate and fill in the superblock. */
651 error = ext2fs_sbfill(m_fs, ronly);
652 if (error) {
653 kmem_free(m_fs, sizeof(struct m_ext2fs));
654 goto out;
655 }
656 m_fs->e2fs_ronly = ronly;
657
658 ump = kmem_zalloc(sizeof(*ump), KM_SLEEP);
659 ump->um_fstype = UFS1;
660 ump->um_ops = &ext2fs_ufsops;
661 ump->um_e2fs = m_fs;
662
663 if (ronly == 0) {
664 if (m_fs->e2fs.e2fs_state == E2FS_ISCLEAN)
665 m_fs->e2fs.e2fs_state = 0;
666 else
667 m_fs->e2fs.e2fs_state = E2FS_ERRORS;
668 m_fs->e2fs_fmod = 1;
669 }
670
671 /* XXX: should be added in ext2fs_sbfill()? */
672 m_fs->e2fs_gd = kmem_alloc(m_fs->e2fs_ngdb * m_fs->e2fs_bsize, KM_SLEEP);
673 for (i = 0; i < m_fs->e2fs_ngdb; i++) {
674 error = bread(devvp,
675 EXT2_FSBTODB(m_fs, m_fs->e2fs.e2fs_first_dblock +
676 1 /* superblock */ + i),
677 m_fs->e2fs_bsize, NOCRED, 0, &bp);
678 if (error) {
679 kmem_free(m_fs->e2fs_gd,
680 m_fs->e2fs_ngdb * m_fs->e2fs_bsize);
681 goto out;
682 }
683 e2fs_cgload((struct ext2_gd *)bp->b_data,
684 &m_fs->e2fs_gd[
685 i * m_fs->e2fs_bsize / sizeof(struct ext2_gd)],
686 m_fs->e2fs_bsize);
687 brelse(bp, 0);
688 bp = NULL;
689 }
690
691 mp->mnt_data = ump;
692 mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
693 mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_EXT2FS);
694 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
695 mp->mnt_stat.f_namemax = EXT2FS_MAXNAMLEN;
696 mp->mnt_flag |= MNT_LOCAL;
697 mp->mnt_dev_bshift = DEV_BSHIFT; /* XXX */
698 mp->mnt_fs_bshift = m_fs->e2fs_bshift;
699 mp->mnt_iflag |= IMNT_DTYPE;
700 ump->um_flags = 0;
701 ump->um_mountp = mp;
702 ump->um_dev = dev;
703 ump->um_devvp = devvp;
704 ump->um_nindir = EXT2_NINDIR(m_fs);
705 ump->um_lognindir = ffs(EXT2_NINDIR(m_fs)) - 1;
706 ump->um_bptrtodb = m_fs->e2fs_fsbtodb;
707 ump->um_seqinc = 1; /* no frags */
708 ump->um_maxsymlinklen = EXT2_MAXSYMLINKLEN;
709 ump->um_dirblksiz = m_fs->e2fs_bsize;
710 ump->um_maxfilesize = ((uint64_t)0x80000000 * m_fs->e2fs_bsize - 1);
711 spec_node_setmountedfs(devvp, mp);
712 return (0);
713
714 out:
715 if (bp != NULL)
716 brelse(bp, 0);
717 if (ump) {
718 kmem_free(ump->um_e2fs, sizeof(struct m_ext2fs));
719 kmem_free(ump, sizeof(*ump));
720 mp->mnt_data = NULL;
721 }
722 return (error);
723 }
724
725 /*
726 * unmount system call
727 */
728 int
729 ext2fs_unmount(struct mount *mp, int mntflags)
730 {
731 struct ufsmount *ump;
732 struct m_ext2fs *fs;
733 int error, flags;
734
735 flags = 0;
736 if (mntflags & MNT_FORCE)
737 flags |= FORCECLOSE;
738 if ((error = ext2fs_flushfiles(mp, flags)) != 0)
739 return (error);
740 ump = VFSTOUFS(mp);
741 fs = ump->um_e2fs;
742 if (fs->e2fs_ronly == 0 &&
743 ext2fs_cgupdate(ump, MNT_WAIT) == 0 &&
744 (fs->e2fs.e2fs_state & E2FS_ERRORS) == 0) {
745 fs->e2fs.e2fs_state = E2FS_ISCLEAN;
746 (void) ext2fs_sbupdate(ump, MNT_WAIT);
747 }
748 if (ump->um_devvp->v_type != VBAD)
749 spec_node_setmountedfs(ump->um_devvp, NULL);
750 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
751 error = VOP_CLOSE(ump->um_devvp, fs->e2fs_ronly ? FREAD : FREAD|FWRITE,
752 NOCRED);
753 vput(ump->um_devvp);
754 kmem_free(fs->e2fs_gd, fs->e2fs_ngdb * fs->e2fs_bsize);
755 kmem_free(fs, sizeof(*fs));
756 kmem_free(ump, sizeof(*ump));
757 mp->mnt_data = NULL;
758 mp->mnt_flag &= ~MNT_LOCAL;
759 return (error);
760 }
761
762 /*
763 * Flush out all the files in a filesystem.
764 */
765 int
766 ext2fs_flushfiles(struct mount *mp, int flags)
767 {
768 extern int doforce;
769 int error;
770
771 if (!doforce)
772 flags &= ~FORCECLOSE;
773 error = vflush(mp, NULLVP, flags);
774 return (error);
775 }
776
777 /*
778 * Get file system statistics.
779 */
780 int
781 ext2fs_statvfs(struct mount *mp, struct statvfs *sbp)
782 {
783 struct ufsmount *ump;
784 struct m_ext2fs *fs;
785 uint32_t overhead, overhead_per_group, ngdb;
786 int i, ngroups;
787
788 ump = VFSTOUFS(mp);
789 fs = ump->um_e2fs;
790 if (fs->e2fs.e2fs_magic != E2FS_MAGIC)
791 panic("ext2fs_statvfs");
792
793 /*
794 * Compute the overhead (FS structures)
795 */
796 overhead_per_group =
797 1 /* block bitmap */ +
798 1 /* inode bitmap */ +
799 fs->e2fs_itpg;
800 overhead = fs->e2fs.e2fs_first_dblock +
801 fs->e2fs_ncg * overhead_per_group;
802 if (fs->e2fs.e2fs_rev > E2FS_REV0 &&
803 fs->e2fs.e2fs_features_rocompat & EXT2F_ROCOMPAT_SPARSESUPER) {
804 for (i = 0, ngroups = 0; i < fs->e2fs_ncg; i++) {
805 if (cg_has_sb(i))
806 ngroups++;
807 }
808 } else {
809 ngroups = fs->e2fs_ncg;
810 }
811 ngdb = fs->e2fs_ngdb;
812 if (fs->e2fs.e2fs_rev > E2FS_REV0 &&
813 fs->e2fs.e2fs_features_compat & EXT2F_COMPAT_RESIZE)
814 ngdb += fs->e2fs.e2fs_reserved_ngdb;
815 overhead += ngroups * (1 /* superblock */ + ngdb);
816
817 sbp->f_bsize = fs->e2fs_bsize;
818 sbp->f_frsize = MINBSIZE << fs->e2fs.e2fs_fsize;
819 sbp->f_iosize = fs->e2fs_bsize;
820 sbp->f_blocks = fs->e2fs.e2fs_bcount - overhead;
821 sbp->f_bfree = fs->e2fs.e2fs_fbcount;
822 sbp->f_bresvd = fs->e2fs.e2fs_rbcount;
823 if (sbp->f_bfree > sbp->f_bresvd)
824 sbp->f_bavail = sbp->f_bfree - sbp->f_bresvd;
825 else
826 sbp->f_bavail = 0;
827 sbp->f_files = fs->e2fs.e2fs_icount;
828 sbp->f_ffree = fs->e2fs.e2fs_ficount;
829 sbp->f_favail = fs->e2fs.e2fs_ficount;
830 sbp->f_fresvd = 0;
831 copy_statvfs_info(sbp, mp);
832 return (0);
833 }
834
835 static bool
836 ext2fs_sync_selector(void *cl, struct vnode *vp)
837 {
838 struct inode *ip;
839
840 ip = VTOI(vp);
841 /*
842 * Skip the vnode/inode if inaccessible.
843 */
844 if (ip == NULL || vp->v_type == VNON)
845 return false;
846
847 if (((ip->i_flag &
848 (IN_CHANGE | IN_UPDATE | IN_MODIFIED)) == 0 &&
849 LIST_EMPTY(&vp->v_dirtyblkhd) &&
850 UVM_OBJ_IS_CLEAN(&vp->v_uobj)))
851 return false;
852 return true;
853 }
854
855 /*
856 * Go through the disk queues to initiate sandbagged IO;
857 * go through the inodes to write those that have been modified;
858 * initiate the writing of the super block if it has been modified.
859 *
860 * Note: we are always called with the filesystem marked `MPBUSY'.
861 */
862 int
863 ext2fs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
864 {
865 struct vnode *vp;
866 struct ufsmount *ump = VFSTOUFS(mp);
867 struct m_ext2fs *fs;
868 struct vnode_iterator *marker;
869 int error, allerror = 0;
870
871 fs = ump->um_e2fs;
872 if (fs->e2fs_fmod != 0 && fs->e2fs_ronly != 0) { /* XXX */
873 printf("fs = %s\n", fs->e2fs_fsmnt);
874 panic("update: rofs mod");
875 }
876
877 /*
878 * Write back each (modified) inode.
879 */
880 vfs_vnode_iterator_init(mp, &marker);
881 while ((vp = vfs_vnode_iterator_next(marker, ext2fs_sync_selector,
882 NULL)))
883 {
884 error = vn_lock(vp, LK_EXCLUSIVE);
885 if (error) {
886 vrele(vp);
887 continue;
888 }
889 if (vp->v_type == VREG && waitfor == MNT_LAZY)
890 error = ext2fs_update(vp, NULL, NULL, 0);
891 else
892 error = VOP_FSYNC(vp, cred,
893 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0);
894 if (error)
895 allerror = error;
896 vput(vp);
897 }
898 vfs_vnode_iterator_destroy(marker);
899 /*
900 * Force stale file system control information to be flushed.
901 */
902 if (waitfor != MNT_LAZY) {
903 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
904 if ((error = VOP_FSYNC(ump->um_devvp, cred,
905 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0)) != 0)
906 allerror = error;
907 VOP_UNLOCK(ump->um_devvp);
908 }
909 /*
910 * Write back modified superblock.
911 */
912 if (fs->e2fs_fmod != 0) {
913 fs->e2fs_fmod = 0;
914 fs->e2fs.e2fs_wtime = time_second;
915 if ((error = ext2fs_cgupdate(ump, waitfor)))
916 allerror = error;
917 }
918 return (allerror);
919 }
920
921 /*
922 * Read an inode from disk and initialize this vnode / inode pair.
923 * Caller assures no other thread will try to load this inode.
924 */
925 int
926 ext2fs_loadvnode(struct mount *mp, struct vnode *vp,
927 const void *key, size_t key_len, const void **new_key)
928 {
929 ino_t ino;
930 struct m_ext2fs *fs;
931 struct inode *ip;
932 struct ufsmount *ump;
933 struct buf *bp;
934 dev_t dev;
935 int error;
936 void *cp;
937
938 KASSERT(key_len == sizeof(ino));
939 memcpy(&ino, key, key_len);
940 ump = VFSTOUFS(mp);
941 dev = ump->um_dev;
942 fs = ump->um_e2fs;
943
944 /* Read in the disk contents for the inode, copy into the inode. */
945 error = bread(ump->um_devvp, EXT2_FSBTODB(fs, ino_to_fsba(fs, ino)),
946 (int)fs->e2fs_bsize, NOCRED, 0, &bp);
947 if (error)
948 return error;
949
950 /* Allocate and initialize inode. */
951 ip = pool_get(&ext2fs_inode_pool, PR_WAITOK);
952 memset(ip, 0, sizeof(struct inode));
953 vp->v_tag = VT_EXT2FS;
954 vp->v_op = ext2fs_vnodeop_p;
955 vp->v_vflag |= VV_LOCKSWORK;
956 vp->v_data = ip;
957 ip->i_vnode = vp;
958 ip->i_ump = ump;
959 ip->i_e2fs = fs;
960 ip->i_dev = dev;
961 ip->i_number = ino;
962 ip->i_e2fs_last_lblk = 0;
963 ip->i_e2fs_last_blk = 0;
964
965 /* Initialize genfs node. */
966 genfs_node_init(vp, &ext2fs_genfsops);
967
968 cp = (char *)bp->b_data + (ino_to_fsbo(fs, ino) * EXT2_DINODE_SIZE(fs));
969 ip->i_din.e2fs_din = pool_get(&ext2fs_dinode_pool, PR_WAITOK);
970 e2fs_iload((struct ext2fs_dinode *)cp, ip->i_din.e2fs_din);
971 ext2fs_set_inode_guid(ip);
972 brelse(bp, 0);
973
974 /* If the inode was deleted, reset all fields */
975 if (ip->i_e2fs_dtime != 0) {
976 ip->i_e2fs_mode = 0;
977 (void)ext2fs_setsize(ip, 0);
978 (void)ext2fs_setnblock(ip, 0);
979 memset(ip->i_e2fs_blocks, 0, sizeof(ip->i_e2fs_blocks));
980 }
981
982 /* Initialize the vnode from the inode. */
983 ext2fs_vinit(mp, ext2fs_specop_p, ext2fs_fifoop_p, &vp);
984
985 /* Finish inode initialization. */
986 ip->i_devvp = ump->um_devvp;
987 vref(ip->i_devvp);
988
989 /*
990 * Set up a generation number for this inode if it does not
991 * already have one. This should only happen on old filesystems.
992 */
993
994 if (ip->i_e2fs_gen == 0) {
995 if (++ext2gennumber < (u_long)time_second)
996 ext2gennumber = time_second;
997 ip->i_e2fs_gen = ext2gennumber;
998 if ((mp->mnt_flag & MNT_RDONLY) == 0)
999 ip->i_flag |= IN_MODIFIED;
1000 }
1001 uvm_vnp_setsize(vp, ext2fs_size(ip));
1002 *new_key = &ip->i_number;
1003 return 0;
1004 }
1005
1006 /*
1007 * File handle to vnode
1008 *
1009 * Have to be really careful about stale file handles:
1010 * - check that the inode number is valid
1011 * - call ext2fs_vget() to get the locked inode
1012 * - check for an unallocated inode (i_mode == 0)
1013 */
1014 int
1015 ext2fs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
1016 {
1017 struct inode *ip;
1018 struct vnode *nvp;
1019 int error;
1020 struct ufid ufh;
1021 struct m_ext2fs *fs;
1022
1023 if (fhp->fid_len != sizeof(struct ufid))
1024 return EINVAL;
1025
1026 memcpy(&ufh, fhp, sizeof(struct ufid));
1027 fs = VFSTOUFS(mp)->um_e2fs;
1028 if ((ufh.ufid_ino < EXT2_FIRSTINO && ufh.ufid_ino != EXT2_ROOTINO) ||
1029 ufh.ufid_ino >= fs->e2fs_ncg * fs->e2fs.e2fs_ipg)
1030 return (ESTALE);
1031
1032 if ((error = VFS_VGET(mp, ufh.ufid_ino, &nvp)) != 0) {
1033 *vpp = NULLVP;
1034 return (error);
1035 }
1036 ip = VTOI(nvp);
1037 if (ip->i_e2fs_mode == 0 || ip->i_e2fs_dtime != 0 ||
1038 ip->i_e2fs_gen != ufh.ufid_gen) {
1039 vput(nvp);
1040 *vpp = NULLVP;
1041 return (ESTALE);
1042 }
1043 *vpp = nvp;
1044 return (0);
1045 }
1046
1047 /*
1048 * Vnode pointer to File handle
1049 */
1050 /* ARGSUSED */
1051 int
1052 ext2fs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
1053 {
1054 struct inode *ip;
1055 struct ufid ufh;
1056
1057 if (*fh_size < sizeof(struct ufid)) {
1058 *fh_size = sizeof(struct ufid);
1059 return E2BIG;
1060 }
1061 *fh_size = sizeof(struct ufid);
1062
1063 ip = VTOI(vp);
1064 memset(&ufh, 0, sizeof(ufh));
1065 ufh.ufid_len = sizeof(struct ufid);
1066 ufh.ufid_ino = ip->i_number;
1067 ufh.ufid_gen = ip->i_e2fs_gen;
1068 memcpy(fhp, &ufh, sizeof(ufh));
1069 return (0);
1070 }
1071
1072 /*
1073 * Write a superblock and associated information back to disk.
1074 */
1075 int
1076 ext2fs_sbupdate(struct ufsmount *mp, int waitfor)
1077 {
1078 struct m_ext2fs *fs = mp->um_e2fs;
1079 struct buf *bp;
1080 int error = 0;
1081
1082 bp = getblk(mp->um_devvp, SBLOCK, SBSIZE, 0, 0);
1083 e2fs_sbsave(&fs->e2fs, (struct ext2fs*)bp->b_data);
1084 if (waitfor == MNT_WAIT)
1085 error = bwrite(bp);
1086 else
1087 bawrite(bp);
1088 return (error);
1089 }
1090
1091 int
1092 ext2fs_cgupdate(struct ufsmount *mp, int waitfor)
1093 {
1094 struct m_ext2fs *fs = mp->um_e2fs;
1095 struct buf *bp;
1096 int i, error = 0, allerror = 0;
1097
1098 allerror = ext2fs_sbupdate(mp, waitfor);
1099 for (i = 0; i < fs->e2fs_ngdb; i++) {
1100 bp = getblk(mp->um_devvp, EXT2_FSBTODB(fs,
1101 fs->e2fs.e2fs_first_dblock +
1102 1 /* superblock */ + i), fs->e2fs_bsize, 0, 0);
1103 e2fs_cgsave(&fs->e2fs_gd[
1104 i * fs->e2fs_bsize / sizeof(struct ext2_gd)],
1105 (struct ext2_gd *)bp->b_data, fs->e2fs_bsize);
1106 if (waitfor == MNT_WAIT)
1107 error = bwrite(bp);
1108 else
1109 bawrite(bp);
1110 }
1111
1112 if (!allerror && error)
1113 allerror = error;
1114 return (allerror);
1115 }
1116
1117 /*
1118 * Fill in the m_fs structure, and validate the fields of the superblock.
1119 * NOTE: here, the superblock is already swapped.
1120 */
1121 static int
1122 ext2fs_sbfill(struct m_ext2fs *m_fs, int ronly)
1123 {
1124 uint32_t u32;
1125 struct ext2fs *fs = &m_fs->e2fs;
1126
1127 /*
1128 * General sanity checks
1129 */
1130 if (fs->e2fs_magic != E2FS_MAGIC)
1131 return EINVAL;
1132 if (fs->e2fs_rev > E2FS_REV1) {
1133 printf("ext2fs: unsupported revision number: %x\n", fs->e2fs_rev);
1134 return EINVAL;
1135 }
1136 if (fs->e2fs_log_bsize > 2) {
1137 /* block size = 1024|2048|4096 */
1138 printf("ext2fs: bad block size: %d\n", fs->e2fs_log_bsize);
1139 return EINVAL;
1140 }
1141 if (fs->e2fs_bpg == 0) {
1142 printf("ext2fs: zero blocks per group\n");
1143 return EINVAL;
1144 }
1145 if (fs->e2fs_ipg == 0) {
1146 printf("ext2fs: zero inodes per group\n");
1147 return EINVAL;
1148 }
1149
1150 if (fs->e2fs_first_dblock >= fs->e2fs_bcount) {
1151 printf("ext2fs: invalid first data block\n");
1152 return EINVAL;
1153 }
1154 if (fs->e2fs_rbcount > fs->e2fs_bcount ||
1155 fs->e2fs_fbcount > fs->e2fs_bcount) {
1156 printf("ext2fs: invalid block count\n");
1157 return EINVAL;
1158 }
1159
1160 /*
1161 * Revision-specific checks
1162 */
1163 if (fs->e2fs_rev > E2FS_REV0) {
1164 char buf[256];
1165 if (fs->e2fs_first_ino != EXT2_FIRSTINO) {
1166 printf("ext2fs: unsupported first inode position\n");
1167 return EINVAL;
1168 }
1169 u32 = fs->e2fs_features_incompat & ~EXT2F_INCOMPAT_SUPP;
1170 if (u32) {
1171 snprintb(buf, sizeof(buf), EXT2F_INCOMPAT_BITS, u32);
1172 printf("ext2fs: unsupported incompat features: %s\n", buf);
1173 return EINVAL;
1174 }
1175 u32 = fs->e2fs_features_rocompat & ~EXT2F_ROCOMPAT_SUPP;
1176 if (!ronly && u32) {
1177 snprintb(buf, sizeof(buf), EXT2F_ROCOMPAT_BITS, u32);
1178 printf("ext2fs: unsupported ro-incompat features: %s\n",
1179 buf);
1180 return EROFS;
1181 }
1182 if (fs->e2fs_inode_size == 0 || !powerof2(fs->e2fs_inode_size)) {
1183 printf("ext2fs: bad inode size\n");
1184 return EINVAL;
1185 }
1186 }
1187
1188 /*
1189 * Compute the fields of the superblock
1190 */
1191 u32 = fs->e2fs_bcount - fs->e2fs_first_dblock; /* > 0 */
1192 if (u32 < fs->e2fs_bpg) {
1193 printf("ext2fs: invalid number of cylinder groups\n");
1194 return EINVAL;
1195 }
1196 m_fs->e2fs_ncg = howmany(u32, fs->e2fs_bpg);
1197
1198 m_fs->e2fs_fsbtodb = fs->e2fs_log_bsize + LOG_MINBSIZE - DEV_BSHIFT;
1199 m_fs->e2fs_bsize = MINBSIZE << fs->e2fs_log_bsize;
1200 m_fs->e2fs_bshift = LOG_MINBSIZE + fs->e2fs_log_bsize;
1201 m_fs->e2fs_qbmask = m_fs->e2fs_bsize - 1;
1202 m_fs->e2fs_bmask = ~m_fs->e2fs_qbmask;
1203
1204 if (m_fs->e2fs_bsize < sizeof(struct ext2_gd)) {
1205 /* Unlikely to happen */
1206 printf("ext2fs: invalid block size\n");
1207 return EINVAL;
1208 }
1209 m_fs->e2fs_ngdb =
1210 howmany(m_fs->e2fs_ncg, m_fs->e2fs_bsize / sizeof(struct ext2_gd));
1211 if (m_fs->e2fs_ngdb == 0) {
1212 printf("ext2fs: invalid number of group descriptor blocks\n");
1213 return EINVAL;
1214 }
1215
1216 if (m_fs->e2fs_bsize < EXT2_DINODE_SIZE(m_fs)) {
1217 printf("ext2fs: invalid inode size\n");
1218 return EINVAL;
1219 }
1220 m_fs->e2fs_ipb = m_fs->e2fs_bsize / EXT2_DINODE_SIZE(m_fs);
1221
1222 m_fs->e2fs_itpg = fs->e2fs_ipg / m_fs->e2fs_ipb;
1223
1224 return 0;
1225 }
1226