ext2fs_vfsops.c revision 1.24.2.3 1 /* $NetBSD: ext2fs_vfsops.c,v 1.24.2.3 2000/02/01 23:38:39 he Exp $ */
2
3 /*
4 * Copyright (c) 1997 Manuel Bouyer.
5 * Copyright (c) 1989, 1991, 1993, 1994
6 * The Regents of the University of California. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)ffs_vfsops.c 8.14 (Berkeley) 11/28/94
37 * Modified for ext2fs by Manuel Bouyer.
38 */
39
40 #if defined(_KERNEL) && !defined(_LKM)
41 #include "opt_compat_netbsd.h"
42 #endif
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/namei.h>
47 #include <sys/proc.h>
48 #include <sys/kernel.h>
49 #include <sys/vnode.h>
50 #include <sys/socket.h>
51 #include <sys/mount.h>
52 #include <sys/buf.h>
53 #include <sys/device.h>
54 #include <sys/mbuf.h>
55 #include <sys/file.h>
56 #include <sys/disklabel.h>
57 #include <sys/ioctl.h>
58 #include <sys/errno.h>
59 #include <sys/malloc.h>
60 #include <sys/pool.h>
61 #include <sys/lock.h>
62
63 #include <miscfs/specfs/specdev.h>
64
65 #include <ufs/ufs/quota.h>
66 #include <ufs/ufs/ufsmount.h>
67 #include <ufs/ufs/inode.h>
68 #include <ufs/ufs/dir.h>
69 #include <ufs/ufs/ufs_extern.h>
70
71 #include <ufs/ext2fs/ext2fs.h>
72 #include <ufs/ext2fs/ext2fs_extern.h>
73
74 extern struct lock ufs_hashlock;
75
76 int ext2fs_sbupdate __P((struct ufsmount *, int));
77
78 extern struct vnodeopv_desc ext2fs_vnodeop_opv_desc;
79 extern struct vnodeopv_desc ext2fs_specop_opv_desc;
80 extern struct vnodeopv_desc ext2fs_fifoop_opv_desc;
81
82 struct vnodeopv_desc *ext2fs_vnodeopv_descs[] = {
83 &ext2fs_vnodeop_opv_desc,
84 &ext2fs_specop_opv_desc,
85 &ext2fs_fifoop_opv_desc,
86 NULL,
87 };
88
89 struct vfsops ext2fs_vfsops = {
90 MOUNT_EXT2FS,
91 ext2fs_mount,
92 ufs_start,
93 ext2fs_unmount,
94 ufs_root,
95 ufs_quotactl,
96 ext2fs_statfs,
97 ext2fs_sync,
98 ext2fs_vget,
99 ext2fs_fhtovp,
100 ext2fs_vptofh,
101 ext2fs_init,
102 ext2fs_sysctl,
103 ext2fs_mountroot,
104 ufs_check_export,
105 ext2fs_vnodeopv_descs,
106 };
107
108 struct pool ext2fs_inode_pool;
109
110 extern u_long ext2gennumber;
111
112 void
113 ext2fs_init()
114 {
115 ufs_init();
116
117 /*
118 * XXX Same structure as FFS inodes? Should we share a common pool?
119 */
120 pool_init(&ext2fs_inode_pool, sizeof(struct inode), 0, 0, 0,
121 "ext2fsinopl", 0, pool_page_alloc_nointr, pool_page_free_nointr,
122 M_EXT2FSNODE);
123 }
124
125
126 /*
127 * Called by main() when ext2fs is going to be mounted as root.
128 *
129 * Name is updated by mount(8) after booting.
130 */
131 #define ROOTNAME "root_device"
132
133 int
134 ext2fs_mountroot()
135 {
136 extern struct vnode *rootvp;
137 struct m_ext2fs *fs;
138 struct mount *mp;
139 struct proc *p = curproc; /* XXX */
140 struct ufsmount *ump;
141 int error;
142
143 if (root_device->dv_class != DV_DISK)
144 return (ENODEV);
145
146 /*
147 * Get vnodes for rootdev.
148 */
149 if (bdevvp(rootdev, &rootvp))
150 panic("ext2fs_mountroot: can't setup bdevvp's");
151
152 if ((error = vfs_rootmountalloc(MOUNT_EXT2FS, "root_device", &mp)))
153 return (error);
154
155 if ((error = ext2fs_mountfs(rootvp, mp, p)) != 0) {
156 mp->mnt_op->vfs_refcount--;
157 vfs_unbusy(mp);
158 free(mp, M_MOUNT);
159 return (error);
160 }
161 simple_lock(&mountlist_slock);
162 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
163 simple_unlock(&mountlist_slock);
164 ump = VFSTOUFS(mp);
165 fs = ump->um_e2fs;
166 memset(fs->e2fs_fsmnt, 0, sizeof(fs->e2fs_fsmnt));
167 (void) copystr(mp->mnt_stat.f_mntonname, fs->e2fs_fsmnt,
168 MNAMELEN - 1, 0);
169 (void)ext2fs_statfs(mp, &mp->mnt_stat, p);
170 vfs_unbusy(mp);
171 inittodr(fs->e2fs.e2fs_wtime);
172 return (0);
173 }
174
175 /*
176 * VFS Operations.
177 *
178 * mount system call
179 */
180 int
181 ext2fs_mount(mp, path, data, ndp, p)
182 register struct mount *mp;
183 const char *path;
184 void * data;
185 struct nameidata *ndp;
186 struct proc *p;
187 {
188 struct vnode *devvp;
189 struct ufs_args args;
190 struct ufsmount *ump = NULL;
191 register struct m_ext2fs *fs;
192 size_t size;
193 int error, flags;
194 mode_t accessmode;
195
196 error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args));
197 if (error)
198 return (error);
199 /*
200 * If updating, check whether changing from read-only to
201 * read/write; if there is no device name, that's all we do.
202 */
203 if (mp->mnt_flag & MNT_UPDATE) {
204 ump = VFSTOUFS(mp);
205 fs = ump->um_e2fs;
206 if (fs->e2fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
207 flags = WRITECLOSE;
208 if (mp->mnt_flag & MNT_FORCE)
209 flags |= FORCECLOSE;
210 error = ext2fs_flushfiles(mp, flags, p);
211 if (error == 0 &&
212 ext2fs_cgupdate(ump, MNT_WAIT) == 0 &&
213 (fs->e2fs.e2fs_state & E2FS_ERRORS) == 0) {
214 fs->e2fs.e2fs_state = E2FS_ISCLEAN;
215 (void) ext2fs_sbupdate(ump, MNT_WAIT);
216 }
217 if (error)
218 return (error);
219 fs->e2fs_ronly = 1;
220 }
221 if (mp->mnt_flag & MNT_RELOAD) {
222 error = ext2fs_reload(mp, ndp->ni_cnd.cn_cred, p);
223 if (error)
224 return (error);
225 }
226 if (fs->e2fs_ronly && (mp->mnt_flag & MNT_WANTRDWR)) {
227 /*
228 * If upgrade to read-write by non-root, then verify
229 * that user has necessary permissions on the device.
230 */
231 if (p->p_ucred->cr_uid != 0) {
232 devvp = ump->um_devvp;
233 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
234 error = VOP_ACCESS(devvp, VREAD | VWRITE,
235 p->p_ucred, p);
236 VOP_UNLOCK(devvp, 0);
237 if (error)
238 return (error);
239 }
240 fs->e2fs_ronly = 0;
241 if (fs->e2fs.e2fs_state == E2FS_ISCLEAN)
242 fs->e2fs.e2fs_state = 0;
243 else
244 fs->e2fs.e2fs_state = E2FS_ERRORS;
245 fs->e2fs_fmod = 1;
246 }
247 if (args.fspec == 0) {
248 /*
249 * Process export requests.
250 */
251 return (vfs_export(mp, &ump->um_export, &args.export));
252 }
253 }
254 /*
255 * Not an update, or updating the name: look up the name
256 * and verify that it refers to a sensible block device.
257 */
258 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
259 if ((error = namei(ndp)) != 0)
260 return (error);
261 devvp = ndp->ni_vp;
262
263 if (devvp->v_type != VBLK) {
264 vrele(devvp);
265 return (ENOTBLK);
266 }
267 if (major(devvp->v_rdev) >= nblkdev) {
268 vrele(devvp);
269 return (ENXIO);
270 }
271 /*
272 * If mount by non-root, then verify that user has necessary
273 * permissions on the device.
274 */
275 if (p->p_ucred->cr_uid != 0) {
276 accessmode = VREAD;
277 if ((mp->mnt_flag & MNT_RDONLY) == 0)
278 accessmode |= VWRITE;
279 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
280 error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
281 VOP_UNLOCK(devvp, 0);
282 if (error) {
283 vrele(devvp);
284 return (error);
285 }
286 }
287 if ((mp->mnt_flag & MNT_UPDATE) == 0)
288 error = ext2fs_mountfs(devvp, mp, p);
289 else {
290 if (devvp != ump->um_devvp)
291 error = EINVAL; /* needs translation */
292 else
293 vrele(devvp);
294 }
295 if (error) {
296 vrele(devvp);
297 return (error);
298 }
299 ump = VFSTOUFS(mp);
300 fs = ump->um_e2fs;
301 (void) copyinstr(path, fs->e2fs_fsmnt, sizeof(fs->e2fs_fsmnt) - 1, &size);
302 memset(fs->e2fs_fsmnt + size, 0, sizeof(fs->e2fs_fsmnt) - size);
303 memcpy(mp->mnt_stat.f_mntonname, fs->e2fs_fsmnt, MNAMELEN);
304 (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
305 &size);
306 memset(mp->mnt_stat.f_mntfromname + size, 0, MNAMELEN - size);
307 if (fs->e2fs_fmod != 0) { /* XXX */
308 fs->e2fs_fmod = 0;
309 if (fs->e2fs.e2fs_state == 0)
310 fs->e2fs.e2fs_wtime = time.tv_sec;
311 else
312 printf("%s: file system not clean; please fsck(8)\n",
313 mp->mnt_stat.f_mntfromname);
314 (void) ext2fs_cgupdate(ump, MNT_WAIT);
315 }
316 return (0);
317 }
318
319 /*
320 * Reload all incore data for a filesystem (used after running fsck on
321 * the root filesystem and finding things to fix). The filesystem must
322 * be mounted read-only.
323 *
324 * Things to do to update the mount:
325 * 1) invalidate all cached meta-data.
326 * 2) re-read superblock from disk.
327 * 3) re-read summary information from disk.
328 * 4) invalidate all inactive vnodes.
329 * 5) invalidate all cached file data.
330 * 6) re-read inode data for all active vnodes.
331 */
332 int
333 ext2fs_reload(mountp, cred, p)
334 register struct mount *mountp;
335 struct ucred *cred;
336 struct proc *p;
337 {
338 register struct vnode *vp, *nvp, *devvp;
339 struct inode *ip;
340 struct buf *bp;
341 struct m_ext2fs *fs;
342 struct ext2fs *newfs;
343 struct partinfo dpart;
344 int i, size, error;
345 caddr_t cp;
346
347 if ((mountp->mnt_flag & MNT_RDONLY) == 0)
348 return (EINVAL);
349 /*
350 * Step 1: invalidate all cached meta-data.
351 */
352 devvp = VFSTOUFS(mountp)->um_devvp;
353 if (vinvalbuf(devvp, 0, cred, p, 0, 0))
354 panic("ext2fs_reload: dirty1");
355 /*
356 * Step 2: re-read superblock from disk.
357 */
358 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED, p) != 0)
359 size = DEV_BSIZE;
360 else
361 size = dpart.disklab->d_secsize;
362 error = bread(devvp, (ufs_daddr_t)(SBOFF / size), SBSIZE, NOCRED, &bp);
363 if (error) {
364 brelse(bp);
365 return (error);
366 }
367 newfs = (struct ext2fs *)bp->b_data;
368 if (fs2h16(newfs->e2fs_magic) != E2FS_MAGIC) {
369 brelse(bp);
370 return (EIO); /* XXX needs translation */
371 }
372 if (fs2h32(newfs->e2fs_rev) > E2FS_REV1) {
373 #ifdef DIAGNOSTIC
374 printf("Ext2 fs: unsupported revision number: %x\n",
375 fs2h32(newfs->e2fs_rev));
376 #endif
377 brelse(bp);
378 return (EIO); /* XXX needs translation */
379 }
380 if (fs2h32(newfs->e2fs_rev) > E2FS_REV0) {
381 if ((fs2h32(newfs->e2fs_features_incompat) &
382 ~EXT2F_INCOMPAT_SUPP) != 0) {
383 #ifdef DIAGNOSTIC
384 printf("Ext2 fs: unsupported features\n");
385 #endif
386 return (EINVAL);
387 }
388 if (fs2h32(newfs->e2fs_first_ino) != EXT2_FIRSTINO ||
389 fs2h16(newfs->e2fs_inode_size) != EXT2_DINODE_SIZE) {
390 printf("Ext2 fs: unsupported inode size\n");
391 return (EINVAL);
392 }
393 /*
394 * We're sure we are read-only here, no need to check
395 * e2fs_features_rocompat
396 */
397 }
398 if (fs2h32(newfs->e2fs_log_bsize) > 2) { /* block size = 1024|2048|4096 */
399 #ifdef DIAGNOSTIC
400 printf("Ext2 fs: bad block size: %d (expected <=2 for ext2 fs)\n",
401 fs2h32(newfs->e2fs_log_bsize));
402 #endif
403 brelse(bp);
404 return (EIO); /* XXX needs translation */
405 }
406
407
408 fs = VFSTOUFS(mountp)->um_e2fs;
409 /*
410 * copy in new superblock, and compute in-memory values
411 */
412 e2fs_sbload(newfs, &fs->e2fs);
413 fs->e2fs_ncg =
414 howmany(fs->e2fs.e2fs_bcount - fs->e2fs.e2fs_first_dblock,
415 fs->e2fs.e2fs_bpg);
416 /* XXX assume hw bsize = 512 */
417 fs->e2fs_fsbtodb = fs->e2fs.e2fs_log_bsize + 1;
418 fs->e2fs_bsize = 1024 << fs->e2fs.e2fs_log_bsize;
419 fs->e2fs_bshift = LOG_MINBSIZE + fs->e2fs.e2fs_log_bsize;
420 fs->e2fs_qbmask = fs->e2fs_bsize - 1;
421 fs->e2fs_bmask = ~fs->e2fs_qbmask;
422 fs->e2fs_ngdb = howmany(fs->e2fs_ncg,
423 fs->e2fs_bsize / sizeof(struct ext2_gd));
424 fs->e2fs_ipb = fs->e2fs_bsize / EXT2_DINODE_SIZE;
425 fs->e2fs_itpg = fs->e2fs.e2fs_ipg/fs->e2fs_ipb;
426
427 /*
428 * Step 3: re-read summary information from disk.
429 */
430
431 for (i=0; i < fs->e2fs_ngdb; i++) {
432 error = bread(devvp ,
433 fsbtodb(fs, ((fs->e2fs_bsize>1024)? 0 : 1) + i + 1),
434 fs->e2fs_bsize, NOCRED, &bp);
435 if (error) {
436 brelse(bp);
437 return (error);
438 }
439 e2fs_cgload((struct ext2_gd*)bp->b_data,
440 &fs->e2fs_gd[i* fs->e2fs_bsize / sizeof(struct ext2_gd)],
441 fs->e2fs_bsize);
442 brelse(bp);
443 }
444
445 loop:
446 simple_lock(&mntvnode_slock);
447 for (vp = mountp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
448 if (vp->v_mount != mountp) {
449 simple_unlock(&mntvnode_slock);
450 goto loop;
451 }
452 nvp = vp->v_mntvnodes.le_next;
453 /*
454 * Step 4: invalidate all inactive vnodes.
455 */
456 if (vrecycle(vp, &mntvnode_slock, p))
457 goto loop;
458 /*
459 * Step 5: invalidate all cached file data.
460 */
461 simple_lock(&vp->v_interlock);
462 simple_unlock(&mntvnode_slock);
463 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
464 goto loop;
465 if (vinvalbuf(vp, 0, cred, p, 0, 0))
466 panic("ext2fs_reload: dirty2");
467 /*
468 * Step 6: re-read inode data for all active vnodes.
469 */
470 ip = VTOI(vp);
471 error = bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
472 (int)fs->e2fs_bsize, NOCRED, &bp);
473 if (error) {
474 vput(vp);
475 return (error);
476 }
477 cp = (caddr_t)bp->b_data +
478 (ino_to_fsbo(fs, ip->i_number) * EXT2_DINODE_SIZE);
479 e2fs_iload((struct ext2fs_dinode *)cp, &ip->i_din.e2fs_din);
480 brelse(bp);
481 vput(vp);
482 simple_lock(&mntvnode_slock);
483 }
484 simple_unlock(&mntvnode_slock);
485 return (0);
486 }
487
488 /*
489 * Common code for mount and mountroot
490 */
491 int
492 ext2fs_mountfs(devvp, mp, p)
493 register struct vnode *devvp;
494 struct mount *mp;
495 struct proc *p;
496 {
497 register struct ufsmount *ump;
498 struct buf *bp;
499 register struct ext2fs *fs;
500 register struct m_ext2fs *m_fs;
501 dev_t dev;
502 struct partinfo dpart;
503 int error, i, size, ronly;
504 struct ucred *cred;
505 extern struct vnode *rootvp;
506
507 dev = devvp->v_rdev;
508 cred = p ? p->p_ucred : NOCRED;
509 /*
510 * Disallow multiple mounts of the same device.
511 * Disallow mounting of a device that is currently in use
512 * (except for root, which might share swap device for miniroot).
513 * Flush out any old buffers remaining from a previous use.
514 */
515 if ((error = vfs_mountedon(devvp)) != 0)
516 return (error);
517 if (vcount(devvp) > 1 && devvp != rootvp)
518 return (EBUSY);
519 if ((error = vinvalbuf(devvp, V_SAVE, cred, p, 0, 0)) != 0)
520 return (error);
521
522 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
523 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
524 if (error)
525 return (error);
526 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, cred, p) != 0)
527 size = DEV_BSIZE;
528 else
529 size = dpart.disklab->d_secsize;
530
531 bp = NULL;
532 ump = NULL;
533
534 #ifdef DEBUG_EXT2
535 printf("sb size: %d ino size %d\n", sizeof(struct ext2fs),
536 EXT2_DINODE_SIZE);
537 #endif
538 error = bread(devvp, (SBOFF / DEV_BSIZE), SBSIZE, cred, &bp);
539 if (error)
540 goto out;
541 fs = (struct ext2fs *)bp->b_data;
542 if (fs2h16(fs->e2fs_magic) != E2FS_MAGIC) {
543 error = EINVAL; /* XXX needs translation */
544 goto out;
545 }
546 if (fs2h32(fs->e2fs_rev) > E2FS_REV1) {
547 #ifdef DIAGNOSTIC
548 printf("Ext2 fs: unsupported revision number: %x\n",
549 fs2h32(fs->e2fs_rev));
550 #endif
551 error = EINVAL; /* XXX needs translation */
552 goto out;
553 }
554 if (fs2h32(fs->e2fs_rev) > E2FS_REV0) {
555 if ((fs2h32(fs->e2fs_features_incompat) &
556 ~EXT2F_INCOMPAT_SUPP) != 0) {
557 #ifdef DIAGNOSTIC
558 printf("Ext2 fs: unsupported features\n");
559 #endif
560 error = EINVAL;
561 goto out;
562 }
563 if (fs2h32(fs->e2fs_first_ino) != EXT2_FIRSTINO ||
564 fs2h16(fs->e2fs_inode_size) != EXT2_DINODE_SIZE) {
565 printf("Ext2 fs: unsupported inode size\n");
566 return (EINVAL);
567 }
568 /* EXT2F_INCOMPAT_FTYPE only supported read-only */
569 if ((fs2h32(fs->e2fs_features_incompat) &
570 EXT2F_INCOMPAT_FTYPE) != 0 ||
571 (fs2h32(fs->e2fs_features_rocompat) &
572 ~EXT2F_ROCOMPAT_SUPP) != 0) {
573 if (!ronly) {
574 error = EROFS;
575 goto out;
576 }
577 }
578 }
579 if (fs2h32(fs->e2fs_log_bsize) > 2) { /* block size = 1024|2048|4096 */
580 #ifdef DIAGNOSTIC
581 printf("Ext2 fs: bad block size: %d (expected <=2 for ext2 fs)\n",
582 fs2h32(fs->e2fs_log_bsize));
583 #endif
584 error = EINVAL; /* XXX needs translation */
585 goto out;
586 }
587
588 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
589 memset((caddr_t)ump, 0, sizeof *ump);
590 ump->um_e2fs = malloc(sizeof(struct m_ext2fs), M_UFSMNT, M_WAITOK);
591 memset((caddr_t)ump->um_e2fs, 0, sizeof(struct m_ext2fs));
592 e2fs_sbload((struct ext2fs*)bp->b_data, &ump->um_e2fs->e2fs);
593 brelse(bp);
594 bp = NULL;
595 m_fs = ump->um_e2fs;
596 m_fs->e2fs_ronly = ronly;
597 if (ronly == 0) {
598 if (m_fs->e2fs.e2fs_state == E2FS_ISCLEAN)
599 m_fs->e2fs.e2fs_state = 0;
600 else
601 m_fs->e2fs.e2fs_state = E2FS_ERRORS;
602 m_fs->e2fs_fmod = 1;
603 }
604
605 /* compute dynamic sb infos */
606 m_fs->e2fs_ncg =
607 howmany(m_fs->e2fs.e2fs_bcount - m_fs->e2fs.e2fs_first_dblock,
608 m_fs->e2fs.e2fs_bpg);
609 /* XXX assume hw bsize = 512 */
610 m_fs->e2fs_fsbtodb = m_fs->e2fs.e2fs_log_bsize + 1;
611 m_fs->e2fs_bsize = 1024 << m_fs->e2fs.e2fs_log_bsize;
612 m_fs->e2fs_bshift = LOG_MINBSIZE + m_fs->e2fs.e2fs_log_bsize;
613 m_fs->e2fs_qbmask = m_fs->e2fs_bsize - 1;
614 m_fs->e2fs_bmask = ~m_fs->e2fs_qbmask;
615 m_fs->e2fs_ngdb = howmany(m_fs->e2fs_ncg,
616 m_fs->e2fs_bsize / sizeof(struct ext2_gd));
617 m_fs->e2fs_ipb = m_fs->e2fs_bsize / EXT2_DINODE_SIZE;
618 m_fs->e2fs_itpg = m_fs->e2fs.e2fs_ipg/m_fs->e2fs_ipb;
619
620 m_fs->e2fs_gd = malloc(m_fs->e2fs_ngdb * m_fs->e2fs_bsize,
621 M_UFSMNT, M_WAITOK);
622 for (i=0; i < m_fs->e2fs_ngdb; i++) {
623 error = bread(devvp ,
624 fsbtodb(m_fs, ((m_fs->e2fs_bsize>1024)? 0 : 1) + i + 1),
625 m_fs->e2fs_bsize, NOCRED, &bp);
626 if (error) {
627 free(m_fs->e2fs_gd, M_UFSMNT);
628 goto out;
629 }
630 e2fs_cgload((struct ext2_gd*)bp->b_data,
631 &m_fs->e2fs_gd[
632 i * m_fs->e2fs_bsize / sizeof(struct ext2_gd)],
633 m_fs->e2fs_bsize);
634 brelse(bp);
635 bp = NULL;
636 }
637
638 mp->mnt_data = (qaddr_t)ump;
639 mp->mnt_stat.f_fsid.val[0] = (long)dev;
640 mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_EXT2FS);
641 mp->mnt_maxsymlinklen = EXT2_MAXSYMLINKLEN;
642 mp->mnt_flag |= MNT_LOCAL;
643 ump->um_flags = 0;
644 ump->um_mountp = mp;
645 ump->um_dev = dev;
646 ump->um_devvp = devvp;
647 ump->um_nindir = NINDIR(m_fs);
648 ump->um_bptrtodb = m_fs->e2fs_fsbtodb;
649 ump->um_seqinc = 1; /* no frags */
650 devvp->v_specflags |= SI_MOUNTEDON;
651 return (0);
652 out:
653 if (bp)
654 brelse(bp);
655 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
656 (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p);
657 VOP_UNLOCK(devvp, 0);
658 if (ump) {
659 free(ump->um_e2fs, M_UFSMNT);
660 free(ump, M_UFSMNT);
661 mp->mnt_data = (qaddr_t)0;
662 }
663 return (error);
664 }
665
666 /*
667 * unmount system call
668 */
669 int
670 ext2fs_unmount(mp, mntflags, p)
671 struct mount *mp;
672 int mntflags;
673 struct proc *p;
674 {
675 register struct ufsmount *ump;
676 register struct m_ext2fs *fs;
677 int error, flags;
678
679 flags = 0;
680 if (mntflags & MNT_FORCE)
681 flags |= FORCECLOSE;
682 if ((error = ext2fs_flushfiles(mp, flags, p)) != 0)
683 return (error);
684 ump = VFSTOUFS(mp);
685 fs = ump->um_e2fs;
686 if (fs->e2fs_ronly == 0 &&
687 ext2fs_cgupdate(ump, MNT_WAIT) == 0 &&
688 (fs->e2fs.e2fs_state & E2FS_ERRORS) == 0) {
689 fs->e2fs.e2fs_state = E2FS_ISCLEAN;
690 (void) ext2fs_sbupdate(ump, MNT_WAIT);
691 }
692 ump->um_devvp->v_specflags &= ~SI_MOUNTEDON;
693 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
694 error = VOP_CLOSE(ump->um_devvp, fs->e2fs_ronly ? FREAD : FREAD|FWRITE,
695 NOCRED, p);
696 vput(ump->um_devvp);
697 free(fs->e2fs_gd, M_UFSMNT);
698 free(fs, M_UFSMNT);
699 free(ump, M_UFSMNT);
700 mp->mnt_data = (qaddr_t)0;
701 mp->mnt_flag &= ~MNT_LOCAL;
702 return (error);
703 }
704
705 /*
706 * Flush out all the files in a filesystem.
707 */
708 int
709 ext2fs_flushfiles(mp, flags, p)
710 register struct mount *mp;
711 int flags;
712 struct proc *p;
713 {
714 extern int doforce;
715 register struct ufsmount *ump;
716 int error;
717
718 if (!doforce)
719 flags &= ~FORCECLOSE;
720 ump = VFSTOUFS(mp);
721 error = vflush(mp, NULLVP, flags);
722 return (error);
723 }
724
725 /*
726 * Get file system statistics.
727 */
728 int
729 ext2fs_statfs(mp, sbp, p)
730 struct mount *mp;
731 register struct statfs *sbp;
732 struct proc *p;
733 {
734 register struct ufsmount *ump;
735 register struct m_ext2fs *fs;
736 u_int32_t overhead, overhead_per_group;
737
738 ump = VFSTOUFS(mp);
739 fs = ump->um_e2fs;
740 if (fs->e2fs.e2fs_magic != E2FS_MAGIC)
741 panic("ext2fs_statfs");
742
743 #ifdef COMPAT_09
744 sbp->f_type = 1;
745 #else
746 sbp->f_type = 0;
747 #endif
748
749 /*
750 * Compute the overhead (FS structures)
751 */
752 overhead_per_group = 1 /* super block */ +
753 fs->e2fs_ngdb +
754 1 /* block bitmap */ +
755 1 /* inode bitmap */ +
756 fs->e2fs_itpg;
757 overhead = fs->e2fs.e2fs_first_dblock +
758 fs->e2fs_ncg * overhead_per_group;
759
760
761 sbp->f_bsize = fs->e2fs_bsize;
762 sbp->f_iosize = fs->e2fs_bsize;
763 sbp->f_blocks = fs->e2fs.e2fs_bcount - overhead;
764 sbp->f_bfree = fs->e2fs.e2fs_fbcount;
765 sbp->f_bavail = sbp->f_bfree - fs->e2fs.e2fs_rbcount;
766 sbp->f_files = fs->e2fs.e2fs_icount;
767 sbp->f_ffree = fs->e2fs.e2fs_ficount;
768 if (sbp != &mp->mnt_stat) {
769 memcpy(sbp->f_mntonname, mp->mnt_stat.f_mntonname, MNAMELEN);
770 memcpy(sbp->f_mntfromname, mp->mnt_stat.f_mntfromname, MNAMELEN);
771 }
772 strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN);
773 return (0);
774 }
775
776 /*
777 * Go through the disk queues to initiate sandbagged IO;
778 * go through the inodes to write those that have been modified;
779 * initiate the writing of the super block if it has been modified.
780 *
781 * Note: we are always called with the filesystem marked `MPBUSY'.
782 */
783 int
784 ext2fs_sync(mp, waitfor, cred, p)
785 struct mount *mp;
786 int waitfor;
787 struct ucred *cred;
788 struct proc *p;
789 {
790 struct vnode *vp, *nvp;
791 struct inode *ip;
792 struct ufsmount *ump = VFSTOUFS(mp);
793 struct m_ext2fs *fs;
794 int error, allerror = 0;
795
796 fs = ump->um_e2fs;
797 if (fs->e2fs_ronly != 0) { /* XXX */
798 printf("fs = %s\n", fs->e2fs_fsmnt);
799 panic("update: rofs mod");
800 }
801
802 /*
803 * Write back each (modified) inode.
804 */
805 simple_lock(&mntvnode_slock);
806 loop:
807 for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
808 /*
809 * If the vnode that we are about to sync is no longer
810 * associated with this mount point, start over.
811 */
812 if (vp->v_mount != mp)
813 goto loop;
814 simple_lock(&vp->v_interlock);
815 nvp = vp->v_mntvnodes.le_next;
816 ip = VTOI(vp);
817 if ((ip->i_flag &
818 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
819 vp->v_dirtyblkhd.lh_first == NULL) {
820 simple_unlock(&vp->v_interlock);
821 continue;
822 }
823 simple_unlock(&mntvnode_slock);
824 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
825 if (error) {
826 simple_lock(&mntvnode_slock);
827 if (error == ENOENT)
828 goto loop;
829 continue;
830 }
831 if ((error = VOP_FSYNC(vp, cred,
832 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, p)) != 0)
833 allerror = error;
834 vput(vp);
835 simple_lock(&mntvnode_slock);
836 }
837 simple_unlock(&mntvnode_slock);
838 /*
839 * Force stale file system control information to be flushed.
840 */
841 if ((error = VOP_FSYNC(ump->um_devvp, cred,
842 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, p)) != 0)
843 allerror = error;
844 /*
845 * Write back modified superblock.
846 */
847 if (fs->e2fs_fmod != 0) {
848 fs->e2fs_fmod = 0;
849 fs->e2fs.e2fs_wtime = time.tv_sec;
850 if ((error = ext2fs_cgupdate(ump, waitfor)))
851 allerror = error;
852 }
853 return (allerror);
854 }
855
856 /*
857 * Look up a EXT2FS dinode number to find its incore vnode, otherwise read it
858 * in from disk. If it is in core, wait for the lock bit to clear, then
859 * return the inode locked. Detection and handling of mount points must be
860 * done by the calling routine.
861 */
862 int
863 ext2fs_vget(mp, ino, vpp)
864 struct mount *mp;
865 ino_t ino;
866 struct vnode **vpp;
867 {
868 struct m_ext2fs *fs;
869 struct inode *ip;
870 struct ufsmount *ump;
871 struct buf *bp;
872 struct vnode *vp;
873 dev_t dev;
874 int error;
875 caddr_t cp;
876
877 ump = VFSTOUFS(mp);
878 dev = ump->um_dev;
879 do {
880 if ((*vpp = ufs_ihashget(dev, ino)) != NULL)
881 return (0);
882 } while (lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
883
884 /* Allocate a new vnode/inode. */
885 if ((error = getnewvnode(VT_EXT2FS, mp, ext2fs_vnodeop_p, &vp)) != 0) {
886 *vpp = NULL;
887 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
888 return (error);
889 }
890 ip = pool_get(&ext2fs_inode_pool, PR_WAITOK);
891 memset((caddr_t)ip, 0, sizeof(struct inode));
892 lockinit(&ip->i_lock, PINOD, "inode", 0, 0);
893 vp->v_data = ip;
894 ip->i_vnode = vp;
895 ip->i_e2fs = fs = ump->um_e2fs;
896 ip->i_dev = dev;
897 ip->i_number = ino;
898 ip->i_e2fs_last_lblk = 0;
899 ip->i_e2fs_last_blk = 0;
900
901 /*
902 * Put it onto its hash chain and lock it so that other requests for
903 * this inode will block if they arrive while we are sleeping waiting
904 * for old data structures to be purged or for the contents of the
905 * disk portion of this inode to be read.
906 */
907 ufs_ihashins(ip);
908 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
909
910 /* Read in the disk contents for the inode, copy into the inode. */
911 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
912 (int)fs->e2fs_bsize, NOCRED, &bp);
913 if (error) {
914 /*
915 * The inode does not contain anything useful, so it would
916 * be misleading to leave it on its hash chain. With mode
917 * still zero, it will be unlinked and returned to the free
918 * list by vput().
919 */
920 vput(vp);
921 brelse(bp);
922 *vpp = NULL;
923 return (error);
924 }
925 cp = (caddr_t)bp->b_data +
926 (ino_to_fsbo(fs, ino) * EXT2_DINODE_SIZE);
927 e2fs_iload((struct ext2fs_dinode *)cp, &ip->i_din.e2fs_din);
928 brelse(bp);
929
930 /* If the inode was deleted, reset all fields */
931 if (ip->i_e2fs_dtime != 0) {
932 ip->i_e2fs_mode = ip->i_e2fs_size = ip->i_e2fs_nblock = 0;
933 memset(ip->i_e2fs_blocks, 0, sizeof(ip->i_e2fs_blocks));
934 }
935
936 /*
937 * Initialize the vnode from the inode, check for aliases.
938 * Note that the underlying vnode may have changed.
939 */
940 error = ext2fs_vinit(mp, ext2fs_specop_p, ext2fs_fifoop_p, &vp);
941 if (error) {
942 vput(vp);
943 *vpp = NULL;
944 return (error);
945 }
946 /*
947 * Finish inode initialization now that aliasing has been resolved.
948 */
949 ip->i_devvp = ump->um_devvp;
950 VREF(ip->i_devvp);
951 /*
952 * Set up a generation number for this inode if it does not
953 * already have one. This should only happen on old filesystems.
954 */
955 if (ip->i_e2fs_gen == 0) {
956 if (++ext2gennumber < (u_long)time.tv_sec)
957 ext2gennumber = time.tv_sec;
958 ip->i_e2fs_gen = ext2gennumber;
959 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
960 ip->i_flag |= IN_MODIFIED;
961 }
962
963 *vpp = vp;
964 return (0);
965 }
966
967 /*
968 * File handle to vnode
969 *
970 * Have to be really careful about stale file handles:
971 * - check that the inode number is valid
972 * - call ext2fs_vget() to get the locked inode
973 * - check for an unallocated inode (i_mode == 0)
974 */
975 int
976 ext2fs_fhtovp(mp, fhp, vpp)
977 register struct mount *mp;
978 struct fid *fhp;
979 struct vnode **vpp;
980 {
981 register struct inode *ip;
982 struct vnode *nvp;
983 int error;
984 register struct ufid *ufhp;
985 struct m_ext2fs *fs;
986
987 ufhp = (struct ufid *)fhp;
988 fs = VFSTOUFS(mp)->um_e2fs;
989 if ((ufhp->ufid_ino < EXT2_FIRSTINO && ufhp->ufid_ino != EXT2_ROOTINO) ||
990 ufhp->ufid_ino >= fs->e2fs_ncg * fs->e2fs.e2fs_ipg)
991 return (ESTALE);
992
993 if ((error = VFS_VGET(mp, ufhp->ufid_ino, &nvp)) != 0) {
994 *vpp = NULLVP;
995 return (error);
996 }
997 ip = VTOI(nvp);
998 if (ip->i_e2fs_mode == 0 || ip->i_e2fs_dtime != 0 ||
999 ip->i_e2fs_gen != ufhp->ufid_gen) {
1000 vput(nvp);
1001 *vpp = NULLVP;
1002 return (ESTALE);
1003 }
1004 *vpp = nvp;
1005 return (0);
1006 }
1007
1008 /*
1009 * Vnode pointer to File handle
1010 */
1011 /* ARGSUSED */
1012 int
1013 ext2fs_vptofh(vp, fhp)
1014 struct vnode *vp;
1015 struct fid *fhp;
1016 {
1017 register struct inode *ip;
1018 register struct ufid *ufhp;
1019
1020 ip = VTOI(vp);
1021 ufhp = (struct ufid *)fhp;
1022 ufhp->ufid_len = sizeof(struct ufid);
1023 ufhp->ufid_ino = ip->i_number;
1024 ufhp->ufid_gen = ip->i_e2fs_gen;
1025 return (0);
1026 }
1027
1028 int
1029 ext2fs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
1030 int *name;
1031 u_int namelen;
1032 void *oldp;
1033 size_t *oldlenp;
1034 void *newp;
1035 size_t newlen;
1036 struct proc *p;
1037 {
1038 return (EOPNOTSUPP);
1039 }
1040
1041 /*
1042 * Write a superblock and associated information back to disk.
1043 */
1044 int
1045 ext2fs_sbupdate(mp, waitfor)
1046 struct ufsmount *mp;
1047 int waitfor;
1048 {
1049 register struct m_ext2fs *fs = mp->um_e2fs;
1050 register struct buf *bp;
1051 int error = 0;
1052
1053 bp = getblk(mp->um_devvp, SBLOCK, SBSIZE, 0, 0);
1054 e2fs_sbsave(&fs->e2fs, (struct ext2fs*)bp->b_data);
1055 if (waitfor == MNT_WAIT)
1056 error = bwrite(bp);
1057 else
1058 bawrite(bp);
1059 return (error);
1060 }
1061
1062 int
1063 ext2fs_cgupdate(mp, waitfor)
1064 struct ufsmount *mp;
1065 int waitfor;
1066 {
1067 register struct m_ext2fs *fs = mp->um_e2fs;
1068 register struct buf *bp;
1069 int i, error = 0, allerror = 0;
1070
1071 allerror = ext2fs_sbupdate(mp, waitfor);
1072 for (i = 0; i < fs->e2fs_ngdb; i++) {
1073 bp = getblk(mp->um_devvp, fsbtodb(fs, ((fs->e2fs_bsize>1024)?0:1)+i+1),
1074 fs->e2fs_bsize, 0, 0);
1075 e2fs_cgsave(&fs->e2fs_gd[i* fs->e2fs_bsize / sizeof(struct ext2_gd)],
1076 (struct ext2_gd*)bp->b_data, fs->e2fs_bsize);
1077 if (waitfor == MNT_WAIT)
1078 error = bwrite(bp);
1079 else
1080 bawrite(bp);
1081 }
1082
1083 if (!allerror && error)
1084 allerror = error;
1085 return (allerror);
1086 }
1087