ext2fs_vfsops.c revision 1.92 1 /* $NetBSD: ext2fs_vfsops.c,v 1.92 2005/09/27 06:48:55 yamt 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.92 2005/09/27 06:48:55 yamt 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
95 #include <miscfs/specfs/specdev.h>
96
97 #include <ufs/ufs/quota.h>
98 #include <ufs/ufs/ufsmount.h>
99 #include <ufs/ufs/inode.h>
100 #include <ufs/ufs/dir.h>
101 #include <ufs/ufs/ufs_extern.h>
102
103 #include <ufs/ext2fs/ext2fs.h>
104 #include <ufs/ext2fs/ext2fs_dir.h>
105 #include <ufs/ext2fs/ext2fs_extern.h>
106
107 extern struct lock ufs_hashlock;
108
109 int ext2fs_sbupdate(struct ufsmount *, int);
110 static int ext2fs_checksb(struct ext2fs *, int);
111
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;
115
116 const struct vnodeopv_desc * const ext2fs_vnodeopv_descs[] = {
117 &ext2fs_vnodeop_opv_desc,
118 &ext2fs_specop_opv_desc,
119 &ext2fs_fifoop_opv_desc,
120 NULL,
121 };
122
123 struct vfsops ext2fs_vfsops = {
124 MOUNT_EXT2FS,
125 ext2fs_mount,
126 ufs_start,
127 ext2fs_unmount,
128 ufs_root,
129 ufs_quotactl,
130 ext2fs_statvfs,
131 ext2fs_sync,
132 ext2fs_vget,
133 ext2fs_fhtovp,
134 ext2fs_vptofh,
135 ext2fs_init,
136 ext2fs_reinit,
137 ext2fs_done,
138 ext2fs_mountroot,
139 (int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
140 vfs_stdextattrctl,
141 ext2fs_vnodeopv_descs,
142 };
143 VFS_ATTACH(ext2fs_vfsops);
144
145 static const struct genfs_ops ext2fs_genfsops = {
146 .gop_size = genfs_size,
147 .gop_alloc = ext2fs_gop_alloc,
148 .gop_write = genfs_gop_write,
149 .gop_markupdate = ufs_gop_markupdate,
150 };
151
152 static const struct ufs_ops ext2fs_ufsops = {
153 .uo_itimes = ext2fs_itimes,
154 };
155
156 /*
157 * XXX Same structure as FFS inodes? Should we share a common pool?
158 */
159 POOL_INIT(ext2fs_inode_pool, sizeof(struct inode), 0, 0, 0, "ext2fsinopl",
160 &pool_allocator_nointr);
161 POOL_INIT(ext2fs_dinode_pool, sizeof(struct ext2fs_dinode), 0, 0, 0,
162 "ext2dinopl", &pool_allocator_nointr);
163
164 extern u_long ext2gennumber;
165
166 void
167 ext2fs_init(void)
168 {
169 #ifdef _LKM
170 pool_init(&ext2fs_inode_pool, sizeof(struct inode), 0, 0, 0,
171 "ext2fsinopl", &pool_allocator_nointr);
172 pool_init(&ext2fs_dinode_pool, sizeof(struct ext2fs_dinode), 0, 0, 0,
173 "ext2dinopl", &pool_allocator_nointr);
174 #endif
175 ufs_init();
176 }
177
178 void
179 ext2fs_reinit(void)
180 {
181 ufs_reinit();
182 }
183
184 void
185 ext2fs_done(void)
186 {
187 ufs_done();
188 #ifdef _LKM
189 pool_destroy(&ext2fs_inode_pool);
190 pool_destroy(&ext2fs_dinode_pool);
191 #endif
192 }
193
194 /*
195 * Called by main() when ext2fs is going to be mounted as root.
196 *
197 * Name is updated by mount(8) after booting.
198 */
199 #define ROOTNAME "root_device"
200
201 int
202 ext2fs_mountroot(void)
203 {
204 extern struct vnode *rootvp;
205 struct m_ext2fs *fs;
206 struct mount *mp;
207 struct proc *p = curproc; /* XXX */
208 struct ufsmount *ump;
209 int error;
210
211 if (root_device->dv_class != DV_DISK)
212 return (ENODEV);
213
214 if ((error = vfs_rootmountalloc(MOUNT_EXT2FS, "root_device", &mp))) {
215 vrele(rootvp);
216 return (error);
217 }
218
219 if ((error = ext2fs_mountfs(rootvp, mp, p)) != 0) {
220 mp->mnt_op->vfs_refcount--;
221 vfs_unbusy(mp);
222 free(mp, M_MOUNT);
223 return (error);
224 }
225 simple_lock(&mountlist_slock);
226 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
227 simple_unlock(&mountlist_slock);
228 ump = VFSTOUFS(mp);
229 fs = ump->um_e2fs;
230 memset(fs->e2fs_fsmnt, 0, sizeof(fs->e2fs_fsmnt));
231 (void) copystr(mp->mnt_stat.f_mntonname, fs->e2fs_fsmnt,
232 sizeof(fs->e2fs_fsmnt) - 1, 0);
233 if (fs->e2fs.e2fs_rev > E2FS_REV0) {
234 memset(fs->e2fs.e2fs_fsmnt, 0, sizeof(fs->e2fs.e2fs_fsmnt));
235 (void) copystr(mp->mnt_stat.f_mntonname, fs->e2fs.e2fs_fsmnt,
236 sizeof(fs->e2fs.e2fs_fsmnt) - 1, 0);
237 }
238 (void)ext2fs_statvfs(mp, &mp->mnt_stat, p);
239 vfs_unbusy(mp);
240 setrootfstime((time_t)fs->e2fs.e2fs_wtime);
241 return (0);
242 }
243
244 /*
245 * VFS Operations.
246 *
247 * mount system call
248 */
249 int
250 ext2fs_mount(struct mount *mp, const char *path, void *data,
251 struct nameidata *ndp, struct proc *p)
252 {
253 struct vnode *devvp;
254 struct ufs_args args;
255 struct ufsmount *ump = NULL;
256 struct m_ext2fs *fs;
257 size_t size;
258 int error, flags, update;
259 mode_t accessmode;
260
261 if (mp->mnt_flag & MNT_GETARGS) {
262 ump = VFSTOUFS(mp);
263 if (ump == NULL)
264 return EIO;
265 args.fspec = NULL;
266 return copyout(&args, data, sizeof(args));
267 }
268 error = copyin(data, &args, sizeof (struct ufs_args));
269 if (error)
270 return (error);
271
272 update = mp->mnt_flag & MNT_UPDATE;
273
274 /* Check arguments */
275 if (args.fspec != NULL) {
276 /*
277 * Look up the name and verify that it's sane.
278 */
279 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
280 if ((error = namei(ndp)) != 0)
281 return (error);
282 devvp = ndp->ni_vp;
283
284 if (!update) {
285 /*
286 * Be sure this is a valid block device
287 */
288 if (devvp->v_type != VBLK)
289 error = ENOTBLK;
290 else if (bdevsw_lookup(devvp->v_rdev) == NULL)
291 error = ENXIO;
292 } else {
293 /*
294 * Be sure we're still naming the same device
295 * used for our initial mount
296 */
297 ump = VFSTOUFS(mp);
298 if (devvp != ump->um_devvp)
299 error = EINVAL;
300 }
301 } else {
302 if (!update) {
303 /* New mounts must have a filename for the device */
304 return (EINVAL);
305 } else {
306 ump = VFSTOUFS(mp);
307 devvp = ump->um_devvp;
308 vref(devvp);
309 }
310 }
311
312 /*
313 * If mount by non-root, then verify that user has necessary
314 * permissions on the device.
315 */
316 if (error == 0 && p->p_ucred->cr_uid != 0) {
317 accessmode = VREAD;
318 if (update ?
319 (mp->mnt_iflag & IMNT_WANTRDWR) != 0 :
320 (mp->mnt_flag & MNT_RDONLY) == 0)
321 accessmode |= VWRITE;
322 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
323 error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
324 VOP_UNLOCK(devvp, 0);
325 }
326
327 if (error) {
328 vrele(devvp);
329 return (error);
330 }
331
332 if (!update) {
333 int xflags;
334
335 /*
336 * Disallow multiple mounts of the same device.
337 * Disallow mounting of a device that is currently in use
338 * (except for root, which might share swap device for
339 * miniroot).
340 */
341 error = vfs_mountedon(devvp);
342 if (error)
343 goto fail;
344 if (vcount(devvp) > 1 && devvp != rootvp) {
345 error = EBUSY;
346 goto fail;
347 }
348 if (mp->mnt_flag & MNT_RDONLY)
349 xflags = FREAD;
350 else
351 xflags = FREAD|FWRITE;
352 error = VOP_OPEN(devvp, xflags, FSCRED, p);
353 if (error)
354 goto fail;
355 error = ext2fs_mountfs(devvp, mp, p);
356 if (error) {
357 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
358 (void)VOP_CLOSE(devvp, xflags, NOCRED, p);
359 VOP_UNLOCK(devvp, 0);
360 goto fail;
361 }
362
363 ump = VFSTOUFS(mp);
364 fs = ump->um_e2fs;
365 } else {
366 /*
367 * Update the mount.
368 */
369
370 /*
371 * The initial mount got a reference on this
372 * device, so drop the one obtained via
373 * namei(), above.
374 */
375 vrele(devvp);
376
377 ump = VFSTOUFS(mp);
378 fs = ump->um_e2fs;
379 if (fs->e2fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
380 /*
381 * Changing from r/w to r/o
382 */
383 flags = WRITECLOSE;
384 if (mp->mnt_flag & MNT_FORCE)
385 flags |= FORCECLOSE;
386 error = ext2fs_flushfiles(mp, flags, p);
387 if (error == 0 &&
388 ext2fs_cgupdate(ump, MNT_WAIT) == 0 &&
389 (fs->e2fs.e2fs_state & E2FS_ERRORS) == 0) {
390 fs->e2fs.e2fs_state = E2FS_ISCLEAN;
391 (void) ext2fs_sbupdate(ump, MNT_WAIT);
392 }
393 if (error)
394 return (error);
395 fs->e2fs_ronly = 1;
396 }
397
398 if (mp->mnt_flag & MNT_RELOAD) {
399 error = ext2fs_reload(mp, ndp->ni_cnd.cn_cred, p);
400 if (error)
401 return (error);
402 }
403
404 if (fs->e2fs_ronly && (mp->mnt_iflag & IMNT_WANTRDWR)) {
405 /*
406 * Changing from read-only to read/write
407 */
408 fs->e2fs_ronly = 0;
409 if (fs->e2fs.e2fs_state == E2FS_ISCLEAN)
410 fs->e2fs.e2fs_state = 0;
411 else
412 fs->e2fs.e2fs_state = E2FS_ERRORS;
413 fs->e2fs_fmod = 1;
414 }
415 if (args.fspec == NULL)
416 return EINVAL;
417 }
418
419 error = set_statvfs_info(path, UIO_USERSPACE, args.fspec,
420 UIO_USERSPACE, mp, p);
421 (void) copystr(mp->mnt_stat.f_mntonname, fs->e2fs_fsmnt,
422 sizeof(fs->e2fs_fsmnt) - 1, &size);
423 memset(fs->e2fs_fsmnt + size, 0, sizeof(fs->e2fs_fsmnt) - size);
424 if (fs->e2fs.e2fs_rev > E2FS_REV0) {
425 (void) copystr(mp->mnt_stat.f_mntonname, fs->e2fs.e2fs_fsmnt,
426 sizeof(fs->e2fs.e2fs_fsmnt) - 1, &size);
427 memset(fs->e2fs.e2fs_fsmnt, 0,
428 sizeof(fs->e2fs.e2fs_fsmnt) - size);
429 }
430 if (fs->e2fs_fmod != 0) { /* XXX */
431 fs->e2fs_fmod = 0;
432 if (fs->e2fs.e2fs_state == 0)
433 fs->e2fs.e2fs_wtime = time.tv_sec;
434 else
435 printf("%s: file system not clean; please fsck(8)\n",
436 mp->mnt_stat.f_mntfromname);
437 (void) ext2fs_cgupdate(ump, MNT_WAIT);
438 }
439 return (error);
440
441 fail:
442 vrele(devvp);
443 return (error);
444 }
445
446 /*
447 * Reload all incore data for a filesystem (used after running fsck on
448 * the root filesystem and finding things to fix). The filesystem must
449 * be mounted read-only.
450 *
451 * Things to do to update the mount:
452 * 1) invalidate all cached meta-data.
453 * 2) re-read superblock from disk.
454 * 3) re-read summary information from disk.
455 * 4) invalidate all inactive vnodes.
456 * 5) invalidate all cached file data.
457 * 6) re-read inode data for all active vnodes.
458 */
459 int
460 ext2fs_reload(struct mount *mountp, struct ucred *cred, struct proc *p)
461 {
462 struct vnode *vp, *nvp, *devvp;
463 struct inode *ip;
464 struct buf *bp;
465 struct m_ext2fs *fs;
466 struct ext2fs *newfs;
467 struct partinfo dpart;
468 int i, size, error;
469 caddr_t cp;
470
471 if ((mountp->mnt_flag & MNT_RDONLY) == 0)
472 return (EINVAL);
473 /*
474 * Step 1: invalidate all cached meta-data.
475 */
476 devvp = VFSTOUFS(mountp)->um_devvp;
477 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
478 error = vinvalbuf(devvp, 0, cred, p, 0, 0);
479 VOP_UNLOCK(devvp, 0);
480 if (error)
481 panic("ext2fs_reload: dirty1");
482 /*
483 * Step 2: re-read superblock from disk.
484 */
485 if (VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, NOCRED, p) != 0)
486 size = DEV_BSIZE;
487 else
488 size = dpart.disklab->d_secsize;
489 error = bread(devvp, (daddr_t)(SBOFF / size), SBSIZE, NOCRED, &bp);
490 if (error) {
491 brelse(bp);
492 return (error);
493 }
494 newfs = (struct ext2fs *)bp->b_data;
495 error = ext2fs_checksb(newfs, (mountp->mnt_flag & MNT_RDONLY) != 0);
496 if (error) {
497 brelse(bp);
498 return (error);
499 }
500
501 fs = VFSTOUFS(mountp)->um_e2fs;
502 /*
503 * copy in new superblock, and compute in-memory values
504 */
505 e2fs_sbload(newfs, &fs->e2fs);
506 fs->e2fs_ncg =
507 howmany(fs->e2fs.e2fs_bcount - fs->e2fs.e2fs_first_dblock,
508 fs->e2fs.e2fs_bpg);
509 /* XXX assume hw bsize = 512 */
510 fs->e2fs_fsbtodb = fs->e2fs.e2fs_log_bsize + 1;
511 fs->e2fs_bsize = 1024 << fs->e2fs.e2fs_log_bsize;
512 fs->e2fs_bshift = LOG_MINBSIZE + fs->e2fs.e2fs_log_bsize;
513 fs->e2fs_qbmask = fs->e2fs_bsize - 1;
514 fs->e2fs_bmask = ~fs->e2fs_qbmask;
515 fs->e2fs_ngdb = howmany(fs->e2fs_ncg,
516 fs->e2fs_bsize / sizeof(struct ext2_gd));
517 fs->e2fs_ipb = fs->e2fs_bsize / EXT2_DINODE_SIZE;
518 fs->e2fs_itpg = fs->e2fs.e2fs_ipg/fs->e2fs_ipb;
519
520 /*
521 * Step 3: re-read summary information from disk.
522 */
523
524 for (i=0; i < fs->e2fs_ngdb; i++) {
525 error = bread(devvp ,
526 fsbtodb(fs, ((fs->e2fs_bsize>1024)? 0 : 1) + i + 1),
527 fs->e2fs_bsize, NOCRED, &bp);
528 if (error) {
529 brelse(bp);
530 return (error);
531 }
532 e2fs_cgload((struct ext2_gd*)bp->b_data,
533 &fs->e2fs_gd[i* fs->e2fs_bsize / sizeof(struct ext2_gd)],
534 fs->e2fs_bsize);
535 brelse(bp);
536 }
537
538 loop:
539 simple_lock(&mntvnode_slock);
540 for (vp = mountp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
541 if (vp->v_mount != mountp) {
542 simple_unlock(&mntvnode_slock);
543 goto loop;
544 }
545 nvp = vp->v_mntvnodes.le_next;
546 /*
547 * Step 4: invalidate all inactive vnodes.
548 */
549 if (vrecycle(vp, &mntvnode_slock, p))
550 goto loop;
551 /*
552 * Step 5: invalidate all cached file data.
553 */
554 simple_lock(&vp->v_interlock);
555 simple_unlock(&mntvnode_slock);
556 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
557 goto loop;
558 if (vinvalbuf(vp, 0, cred, p, 0, 0))
559 panic("ext2fs_reload: dirty2");
560 /*
561 * Step 6: re-read inode data for all active vnodes.
562 */
563 ip = VTOI(vp);
564 error = bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
565 (int)fs->e2fs_bsize, NOCRED, &bp);
566 if (error) {
567 vput(vp);
568 return (error);
569 }
570 cp = (caddr_t)bp->b_data +
571 (ino_to_fsbo(fs, ip->i_number) * EXT2_DINODE_SIZE);
572 e2fs_iload((struct ext2fs_dinode *)cp, ip->i_din.e2fs_din);
573 brelse(bp);
574 vput(vp);
575 simple_lock(&mntvnode_slock);
576 }
577 simple_unlock(&mntvnode_slock);
578 return (0);
579 }
580
581 /*
582 * Common code for mount and mountroot
583 */
584 int
585 ext2fs_mountfs(struct vnode *devvp, struct mount *mp, struct proc *p)
586 {
587 struct ufsmount *ump;
588 struct buf *bp;
589 struct ext2fs *fs;
590 struct m_ext2fs *m_fs;
591 dev_t dev;
592 struct partinfo dpart;
593 int error, i, size, ronly;
594 struct ucred *cred;
595
596 dev = devvp->v_rdev;
597 cred = p ? p->p_ucred : NOCRED;
598
599 /* Flush out any old buffers remaining from a previous use. */
600 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
601 error = vinvalbuf(devvp, V_SAVE, cred, p, 0, 0);
602 VOP_UNLOCK(devvp, 0);
603 if (error)
604 return (error);
605
606 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
607 if (VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, cred, p) != 0)
608 size = DEV_BSIZE;
609 else
610 size = dpart.disklab->d_secsize;
611
612 bp = NULL;
613 ump = NULL;
614
615 #ifdef DEBUG_EXT2
616 printf("sb size: %d ino size %d\n", sizeof(struct ext2fs),
617 EXT2_DINODE_SIZE);
618 #endif
619 error = bread(devvp, (SBOFF / size), SBSIZE, cred, &bp);
620 if (error)
621 goto out;
622 fs = (struct ext2fs *)bp->b_data;
623 error = ext2fs_checksb(fs, ronly);
624 if (error)
625 goto out;
626 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
627 memset(ump, 0, sizeof *ump);
628 ump->um_fstype = UFS1;
629 ump->um_ops = &ext2fs_ufsops;
630 ump->um_e2fs = malloc(sizeof(struct m_ext2fs), M_UFSMNT, M_WAITOK);
631 memset(ump->um_e2fs, 0, sizeof(struct m_ext2fs));
632 e2fs_sbload((struct ext2fs*)bp->b_data, &ump->um_e2fs->e2fs);
633 brelse(bp);
634 bp = NULL;
635 m_fs = ump->um_e2fs;
636 m_fs->e2fs_ronly = ronly;
637 if (ronly == 0) {
638 if (m_fs->e2fs.e2fs_state == E2FS_ISCLEAN)
639 m_fs->e2fs.e2fs_state = 0;
640 else
641 m_fs->e2fs.e2fs_state = E2FS_ERRORS;
642 m_fs->e2fs_fmod = 1;
643 }
644
645 /* compute dynamic sb infos */
646 m_fs->e2fs_ncg =
647 howmany(m_fs->e2fs.e2fs_bcount - m_fs->e2fs.e2fs_first_dblock,
648 m_fs->e2fs.e2fs_bpg);
649 /* XXX assume hw bsize = 512 */
650 m_fs->e2fs_fsbtodb = m_fs->e2fs.e2fs_log_bsize + 1;
651 m_fs->e2fs_bsize = 1024 << m_fs->e2fs.e2fs_log_bsize;
652 m_fs->e2fs_bshift = LOG_MINBSIZE + m_fs->e2fs.e2fs_log_bsize;
653 m_fs->e2fs_qbmask = m_fs->e2fs_bsize - 1;
654 m_fs->e2fs_bmask = ~m_fs->e2fs_qbmask;
655 m_fs->e2fs_ngdb = howmany(m_fs->e2fs_ncg,
656 m_fs->e2fs_bsize / sizeof(struct ext2_gd));
657 m_fs->e2fs_ipb = m_fs->e2fs_bsize / EXT2_DINODE_SIZE;
658 m_fs->e2fs_itpg = m_fs->e2fs.e2fs_ipg/m_fs->e2fs_ipb;
659
660 m_fs->e2fs_gd = malloc(m_fs->e2fs_ngdb * m_fs->e2fs_bsize,
661 M_UFSMNT, M_WAITOK);
662 for (i=0; i < m_fs->e2fs_ngdb; i++) {
663 error = bread(devvp ,
664 fsbtodb(m_fs, ((m_fs->e2fs_bsize>1024)? 0 : 1) + i + 1),
665 m_fs->e2fs_bsize, NOCRED, &bp);
666 if (error) {
667 free(m_fs->e2fs_gd, M_UFSMNT);
668 goto out;
669 }
670 e2fs_cgload((struct ext2_gd*)bp->b_data,
671 &m_fs->e2fs_gd[
672 i * m_fs->e2fs_bsize / sizeof(struct ext2_gd)],
673 m_fs->e2fs_bsize);
674 brelse(bp);
675 bp = NULL;
676 }
677
678 mp->mnt_data = ump;
679 mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
680 mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_EXT2FS);
681 mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
682 mp->mnt_stat.f_namemax = EXT2FS_MAXNAMLEN;
683 mp->mnt_flag |= MNT_LOCAL;
684 mp->mnt_dev_bshift = DEV_BSHIFT; /* XXX */
685 mp->mnt_fs_bshift = m_fs->e2fs_bshift;
686 mp->mnt_iflag |= IMNT_DTYPE;
687 ump->um_flags = 0;
688 ump->um_mountp = mp;
689 ump->um_dev = dev;
690 ump->um_devvp = devvp;
691 ump->um_nindir = NINDIR(m_fs);
692 ump->um_lognindir = ffs(NINDIR(m_fs)) - 1;
693 ump->um_bptrtodb = m_fs->e2fs_fsbtodb;
694 ump->um_seqinc = 1; /* no frags */
695 ump->um_maxsymlinklen = EXT2_MAXSYMLINKLEN;
696 ump->um_dirblksiz = m_fs->e2fs_bsize;
697 ump->um_maxfilesize = ((u_int64_t)0x80000000 * m_fs->e2fs_bsize - 1);
698 devvp->v_specmountpoint = mp;
699 return (0);
700
701 out:
702 if (bp)
703 brelse(bp);
704 if (ump) {
705 free(ump->um_e2fs, M_UFSMNT);
706 free(ump, M_UFSMNT);
707 mp->mnt_data = NULL;
708 }
709 return (error);
710 }
711
712 /*
713 * unmount system call
714 */
715 int
716 ext2fs_unmount(struct mount *mp, int mntflags, struct proc *p)
717 {
718 struct ufsmount *ump;
719 struct m_ext2fs *fs;
720 int error, flags;
721
722 flags = 0;
723 if (mntflags & MNT_FORCE)
724 flags |= FORCECLOSE;
725 if ((error = ext2fs_flushfiles(mp, flags, p)) != 0)
726 return (error);
727 ump = VFSTOUFS(mp);
728 fs = ump->um_e2fs;
729 if (fs->e2fs_ronly == 0 &&
730 ext2fs_cgupdate(ump, MNT_WAIT) == 0 &&
731 (fs->e2fs.e2fs_state & E2FS_ERRORS) == 0) {
732 fs->e2fs.e2fs_state = E2FS_ISCLEAN;
733 (void) ext2fs_sbupdate(ump, MNT_WAIT);
734 }
735 if (ump->um_devvp->v_type != VBAD)
736 ump->um_devvp->v_specmountpoint = NULL;
737 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
738 error = VOP_CLOSE(ump->um_devvp, fs->e2fs_ronly ? FREAD : FREAD|FWRITE,
739 NOCRED, p);
740 vput(ump->um_devvp);
741 free(fs->e2fs_gd, M_UFSMNT);
742 free(fs, M_UFSMNT);
743 free(ump, M_UFSMNT);
744 mp->mnt_data = NULL;
745 mp->mnt_flag &= ~MNT_LOCAL;
746 return (error);
747 }
748
749 /*
750 * Flush out all the files in a filesystem.
751 */
752 int
753 ext2fs_flushfiles(struct mount *mp, int flags, struct proc *p)
754 {
755 extern int doforce;
756 int error;
757
758 if (!doforce)
759 flags &= ~FORCECLOSE;
760 error = vflush(mp, NULLVP, flags);
761 return (error);
762 }
763
764 /*
765 * Get file system statistics.
766 */
767 int
768 ext2fs_statvfs(struct mount *mp, struct statvfs *sbp, struct proc *p)
769 {
770 struct ufsmount *ump;
771 struct m_ext2fs *fs;
772 u_int32_t overhead, overhead_per_group;
773 int i, ngroups;
774
775 ump = VFSTOUFS(mp);
776 fs = ump->um_e2fs;
777 if (fs->e2fs.e2fs_magic != E2FS_MAGIC)
778 panic("ext2fs_statvfs");
779
780 /*
781 * Compute the overhead (FS structures)
782 */
783 overhead_per_group = 1 /* block bitmap */ +
784 1 /* inode bitmap */ +
785 fs->e2fs_itpg;
786 overhead = fs->e2fs.e2fs_first_dblock +
787 fs->e2fs_ncg * overhead_per_group;
788 if (fs->e2fs.e2fs_rev > E2FS_REV0 &&
789 fs->e2fs.e2fs_features_rocompat & EXT2F_ROCOMPAT_SPARSESUPER) {
790 for (i = 0, ngroups = 0; i < fs->e2fs_ncg; i++) {
791 if (cg_has_sb(i))
792 ngroups++;
793 }
794 } else {
795 ngroups = fs->e2fs_ncg;
796 }
797 overhead += ngroups * (1 + fs->e2fs_ngdb);
798
799 sbp->f_bsize = fs->e2fs_bsize;
800 sbp->f_frsize = 1024 << fs->e2fs.e2fs_fsize;
801 sbp->f_iosize = fs->e2fs_bsize;
802 sbp->f_blocks = fs->e2fs.e2fs_bcount - overhead;
803 sbp->f_bfree = fs->e2fs.e2fs_fbcount;
804 sbp->f_bresvd = fs->e2fs.e2fs_rbcount;
805 if (sbp->f_bfree > sbp->f_bresvd)
806 sbp->f_bavail = sbp->f_bfree - sbp->f_bresvd;
807 else
808 sbp->f_bavail = 0;
809 sbp->f_files = fs->e2fs.e2fs_icount;
810 sbp->f_ffree = fs->e2fs.e2fs_ficount;
811 sbp->f_favail = fs->e2fs.e2fs_ficount;
812 sbp->f_fresvd = 0;
813 copy_statvfs_info(sbp, mp);
814 return (0);
815 }
816
817 /*
818 * Go through the disk queues to initiate sandbagged IO;
819 * go through the inodes to write those that have been modified;
820 * initiate the writing of the super block if it has been modified.
821 *
822 * Note: we are always called with the filesystem marked `MPBUSY'.
823 */
824 int
825 ext2fs_sync(struct mount *mp, int waitfor, struct ucred *cred, struct proc *p)
826 {
827 struct vnode *vp, *nvp;
828 struct inode *ip;
829 struct ufsmount *ump = VFSTOUFS(mp);
830 struct m_ext2fs *fs;
831 int error, allerror = 0;
832
833 fs = ump->um_e2fs;
834 if (fs->e2fs_fmod != 0 && fs->e2fs_ronly != 0) { /* XXX */
835 printf("fs = %s\n", fs->e2fs_fsmnt);
836 panic("update: rofs mod");
837 }
838 /*
839 * Write back each (modified) inode.
840 */
841 simple_lock(&mntvnode_slock);
842 loop:
843 for (vp = LIST_FIRST(&mp->mnt_vnodelist); vp != NULL; vp = nvp) {
844 /*
845 * If the vnode that we are about to sync is no longer
846 * associated with this mount point, start over.
847 */
848 if (vp->v_mount != mp)
849 goto loop;
850 simple_lock(&vp->v_interlock);
851 nvp = LIST_NEXT(vp, v_mntvnodes);
852 ip = VTOI(vp);
853 if (vp->v_type == VNON ||
854 ((ip->i_flag &
855 (IN_CHANGE | IN_UPDATE | IN_MODIFIED)) == 0 &&
856 LIST_EMPTY(&vp->v_dirtyblkhd) &&
857 vp->v_uobj.uo_npages == 0))
858 {
859 simple_unlock(&vp->v_interlock);
860 continue;
861 }
862 simple_unlock(&mntvnode_slock);
863 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
864 if (error) {
865 simple_lock(&mntvnode_slock);
866 if (error == ENOENT)
867 goto loop;
868 continue;
869 }
870 if (vp->v_type == VREG && waitfor == MNT_LAZY)
871 error = VOP_UPDATE(vp, NULL, NULL, 0);
872 else
873 error = VOP_FSYNC(vp, cred,
874 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p);
875 if (error)
876 allerror = error;
877 vput(vp);
878 simple_lock(&mntvnode_slock);
879 }
880 simple_unlock(&mntvnode_slock);
881 /*
882 * Force stale file system control information to be flushed.
883 */
884 if (waitfor != MNT_LAZY) {
885 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
886 if ((error = VOP_FSYNC(ump->um_devvp, cred,
887 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p)) != 0)
888 allerror = error;
889 VOP_UNLOCK(ump->um_devvp, 0);
890 }
891 /*
892 * Write back modified superblock.
893 */
894 if (fs->e2fs_fmod != 0) {
895 fs->e2fs_fmod = 0;
896 fs->e2fs.e2fs_wtime = time.tv_sec;
897 if ((error = ext2fs_cgupdate(ump, waitfor)))
898 allerror = error;
899 }
900 return (allerror);
901 }
902
903 /*
904 * Look up a EXT2FS dinode number to find its incore vnode, otherwise read it
905 * in from disk. If it is in core, wait for the lock bit to clear, then
906 * return the inode locked. Detection and handling of mount points must be
907 * done by the calling routine.
908 */
909 int
910 ext2fs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
911 {
912 struct m_ext2fs *fs;
913 struct inode *ip;
914 struct ufsmount *ump;
915 struct buf *bp;
916 struct vnode *vp;
917 dev_t dev;
918 int error;
919 caddr_t cp;
920
921 ump = VFSTOUFS(mp);
922 dev = ump->um_dev;
923
924 if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
925 return (0);
926
927 /* Allocate a new vnode/inode. */
928 if ((error = getnewvnode(VT_EXT2FS, mp, ext2fs_vnodeop_p, &vp)) != 0) {
929 *vpp = NULL;
930 return (error);
931 }
932
933 do {
934 if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL) {
935 ungetnewvnode(vp);
936 return (0);
937 }
938 } while (lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
939
940 vp->v_flag |= VLOCKSWORK;
941
942 ip = pool_get(&ext2fs_inode_pool, PR_WAITOK);
943 memset(ip, 0, sizeof(struct inode));
944 vp->v_data = ip;
945 ip->i_vnode = vp;
946 ip->i_ump = ump;
947 ip->i_e2fs = fs = ump->um_e2fs;
948 ip->i_dev = dev;
949 ip->i_number = ino;
950 ip->i_e2fs_last_lblk = 0;
951 ip->i_e2fs_last_blk = 0;
952
953 /*
954 * Put it onto its hash chain and lock it so that other requests for
955 * this inode will block if they arrive while we are sleeping waiting
956 * for old data structures to be purged or for the contents of the
957 * disk portion of this inode to be read.
958 */
959
960 ufs_ihashins(ip);
961 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
962
963 /* Read in the disk contents for the inode, copy into the inode. */
964 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
965 (int)fs->e2fs_bsize, NOCRED, &bp);
966 if (error) {
967
968 /*
969 * The inode does not contain anything useful, so it would
970 * be misleading to leave it on its hash chain. With mode
971 * still zero, it will be unlinked and returned to the free
972 * list by vput().
973 */
974
975 vput(vp);
976 brelse(bp);
977 *vpp = NULL;
978 return (error);
979 }
980 cp = (caddr_t)bp->b_data +
981 (ino_to_fsbo(fs, ino) * EXT2_DINODE_SIZE);
982 ip->i_din.e2fs_din = pool_get(&ext2fs_dinode_pool, PR_WAITOK);
983 e2fs_iload((struct ext2fs_dinode *)cp, ip->i_din.e2fs_din);
984 brelse(bp);
985
986 /* If the inode was deleted, reset all fields */
987 if (ip->i_e2fs_dtime != 0) {
988 ip->i_e2fs_mode = ip->i_e2fs_nblock = 0;
989 (void)ext2fs_setsize(ip, 0);
990 memset(ip->i_e2fs_blocks, 0, sizeof(ip->i_e2fs_blocks));
991 }
992
993 /*
994 * Initialize the vnode from the inode, check for aliases.
995 * Note that the underlying vnode may have changed.
996 */
997
998 error = ext2fs_vinit(mp, ext2fs_specop_p, ext2fs_fifoop_p, &vp);
999 if (error) {
1000 vput(vp);
1001 *vpp = NULL;
1002 return (error);
1003 }
1004 /*
1005 * Finish inode initialization now that aliasing has been resolved.
1006 */
1007
1008 genfs_node_init(vp, &ext2fs_genfsops);
1009 ip->i_devvp = ump->um_devvp;
1010 VREF(ip->i_devvp);
1011
1012 /*
1013 * Set up a generation number for this inode if it does not
1014 * already have one. This should only happen on old filesystems.
1015 */
1016
1017 if (ip->i_e2fs_gen == 0) {
1018 if (++ext2gennumber < (u_long)time.tv_sec)
1019 ext2gennumber = time.tv_sec;
1020 ip->i_e2fs_gen = ext2gennumber;
1021 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
1022 ip->i_flag |= IN_MODIFIED;
1023 }
1024 vp->v_size = ext2fs_size(ip);
1025 *vpp = vp;
1026 return (0);
1027 }
1028
1029 /*
1030 * File handle to vnode
1031 *
1032 * Have to be really careful about stale file handles:
1033 * - check that the inode number is valid
1034 * - call ext2fs_vget() to get the locked inode
1035 * - check for an unallocated inode (i_mode == 0)
1036 */
1037 int
1038 ext2fs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
1039 {
1040 struct inode *ip;
1041 struct vnode *nvp;
1042 int error;
1043 struct ufid *ufhp;
1044 struct m_ext2fs *fs;
1045
1046 ufhp = (struct ufid *)fhp;
1047 fs = VFSTOUFS(mp)->um_e2fs;
1048 if ((ufhp->ufid_ino < EXT2_FIRSTINO && ufhp->ufid_ino != EXT2_ROOTINO) ||
1049 ufhp->ufid_ino >= fs->e2fs_ncg * fs->e2fs.e2fs_ipg)
1050 return (ESTALE);
1051
1052 if ((error = VFS_VGET(mp, ufhp->ufid_ino, &nvp)) != 0) {
1053 *vpp = NULLVP;
1054 return (error);
1055 }
1056 ip = VTOI(nvp);
1057 if (ip->i_e2fs_mode == 0 || ip->i_e2fs_dtime != 0 ||
1058 ip->i_e2fs_gen != ufhp->ufid_gen) {
1059 vput(nvp);
1060 *vpp = NULLVP;
1061 return (ESTALE);
1062 }
1063 *vpp = nvp;
1064 return (0);
1065 }
1066
1067 /*
1068 * Vnode pointer to File handle
1069 */
1070 /* ARGSUSED */
1071 int
1072 ext2fs_vptofh(struct vnode *vp, struct fid *fhp)
1073 {
1074 struct inode *ip;
1075 struct ufid *ufhp;
1076
1077 ip = VTOI(vp);
1078 ufhp = (struct ufid *)fhp;
1079 ufhp->ufid_len = sizeof(struct ufid);
1080 ufhp->ufid_ino = ip->i_number;
1081 ufhp->ufid_gen = ip->i_e2fs_gen;
1082 return (0);
1083 }
1084
1085 SYSCTL_SETUP(sysctl_vfs_ext2fs_setup, "sysctl vfs.ext2fs subtree setup")
1086 {
1087
1088 sysctl_createv(clog, 0, NULL, NULL,
1089 CTLFLAG_PERMANENT,
1090 CTLTYPE_NODE, "vfs", NULL,
1091 NULL, 0, NULL, 0,
1092 CTL_VFS, CTL_EOL);
1093 sysctl_createv(clog, 0, NULL, NULL,
1094 CTLFLAG_PERMANENT,
1095 CTLTYPE_NODE, "ext2fs",
1096 SYSCTL_DESCR("Linux EXT2FS file system"),
1097 NULL, 0, NULL, 0,
1098 CTL_VFS, 17, CTL_EOL);
1099 /*
1100 * XXX the "17" above could be dynamic, thereby eliminating
1101 * one more instance of the "number to vfs" mapping problem,
1102 * but "17" is the order as taken from sys/mount.h
1103 */
1104 }
1105
1106 /*
1107 * Write a superblock and associated information back to disk.
1108 */
1109 int
1110 ext2fs_sbupdate(struct ufsmount *mp, int waitfor)
1111 {
1112 struct m_ext2fs *fs = mp->um_e2fs;
1113 struct buf *bp;
1114 int error = 0;
1115
1116 bp = getblk(mp->um_devvp, SBLOCK, SBSIZE, 0, 0);
1117 e2fs_sbsave(&fs->e2fs, (struct ext2fs*)bp->b_data);
1118 if (waitfor == MNT_WAIT)
1119 error = bwrite(bp);
1120 else
1121 bawrite(bp);
1122 return (error);
1123 }
1124
1125 int
1126 ext2fs_cgupdate(struct ufsmount *mp, int waitfor)
1127 {
1128 struct m_ext2fs *fs = mp->um_e2fs;
1129 struct buf *bp;
1130 int i, error = 0, allerror = 0;
1131
1132 allerror = ext2fs_sbupdate(mp, waitfor);
1133 for (i = 0; i < fs->e2fs_ngdb; i++) {
1134 bp = getblk(mp->um_devvp, fsbtodb(fs, ((fs->e2fs_bsize>1024)?0:1)+i+1),
1135 fs->e2fs_bsize, 0, 0);
1136 e2fs_cgsave(&fs->e2fs_gd[i* fs->e2fs_bsize / sizeof(struct ext2_gd)],
1137 (struct ext2_gd*)bp->b_data, fs->e2fs_bsize);
1138 if (waitfor == MNT_WAIT)
1139 error = bwrite(bp);
1140 else
1141 bawrite(bp);
1142 }
1143
1144 if (!allerror && error)
1145 allerror = error;
1146 return (allerror);
1147 }
1148
1149 static int
1150 ext2fs_checksb(struct ext2fs *fs, int ronly)
1151 {
1152 if (fs2h16(fs->e2fs_magic) != E2FS_MAGIC) {
1153 return (EINVAL); /* XXX needs translation */
1154 }
1155 if (fs2h32(fs->e2fs_rev) > E2FS_REV1) {
1156 #ifdef DIAGNOSTIC
1157 printf("Ext2 fs: unsupported revision number: %x\n",
1158 fs2h32(fs->e2fs_rev));
1159 #endif
1160 return (EINVAL); /* XXX needs translation */
1161 }
1162 if (fs2h32(fs->e2fs_log_bsize) > 2) { /* block size = 1024|2048|4096 */
1163 #ifdef DIAGNOSTIC
1164 printf("Ext2 fs: bad block size: %d (expected <=2 for ext2 fs)\n",
1165 fs2h32(fs->e2fs_log_bsize));
1166 #endif
1167 return (EINVAL); /* XXX needs translation */
1168 }
1169 if (fs2h32(fs->e2fs_rev) > E2FS_REV0) {
1170 if (fs2h32(fs->e2fs_first_ino) != EXT2_FIRSTINO ||
1171 fs2h16(fs->e2fs_inode_size) != EXT2_DINODE_SIZE) {
1172 printf("Ext2 fs: unsupported inode size\n");
1173 return (EINVAL); /* XXX needs translation */
1174 }
1175 if (fs2h32(fs->e2fs_features_incompat) &
1176 ~EXT2F_INCOMPAT_SUPP) {
1177 printf("Ext2 fs: unsupported optional feature\n");
1178 return (EINVAL); /* XXX needs translation */
1179 }
1180 if (!ronly && fs2h32(fs->e2fs_features_rocompat) &
1181 ~EXT2F_ROCOMPAT_SUPP) {
1182 return (EROFS); /* XXX needs translation */
1183 }
1184 }
1185 return (0);
1186 }
1187