ext2fs_vfsops.c revision 1.27.2.1 1 /* $NetBSD: ext2fs_vfsops.c,v 1.27.2.1 1999/12/21 23:20:06 wrstuden 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 vrele(rootvp);
154 return (error);
155 }
156
157 if ((error = ext2fs_mountfs(rootvp, mp, p)) != 0) {
158 mp->mnt_op->vfs_refcount--;
159 vfs_unbusy(mp);
160 free(mp, M_MOUNT);
161 vrele(rootvp);
162 return (error);
163 }
164 simple_lock(&mountlist_slock);
165 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
166 simple_unlock(&mountlist_slock);
167 ump = VFSTOUFS(mp);
168 fs = ump->um_e2fs;
169 memset(fs->e2fs_fsmnt, 0, sizeof(fs->e2fs_fsmnt));
170 (void) copystr(mp->mnt_stat.f_mntonname, fs->e2fs_fsmnt,
171 MNAMELEN - 1, 0);
172 (void)ext2fs_statfs(mp, &mp->mnt_stat, p);
173 vfs_unbusy(mp);
174 inittodr(fs->e2fs.e2fs_wtime);
175 return (0);
176 }
177
178 /*
179 * VFS Operations.
180 *
181 * mount system call
182 */
183 int
184 ext2fs_mount(mp, path, data, ndp, p)
185 register struct mount *mp;
186 const char *path;
187 void * data;
188 struct nameidata *ndp;
189 struct proc *p;
190 {
191 struct vnode *devvp;
192 struct ufs_args args;
193 struct ufsmount *ump = NULL;
194 register struct m_ext2fs *fs;
195 size_t size;
196 int error, flags;
197 mode_t accessmode;
198
199 error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args));
200 if (error)
201 return (error);
202 /*
203 * If updating, check whether changing from read-only to
204 * read/write; if there is no device name, that's all we do.
205 */
206 if (mp->mnt_flag & MNT_UPDATE) {
207 ump = VFSTOUFS(mp);
208 fs = ump->um_e2fs;
209 if (fs->e2fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
210 flags = WRITECLOSE;
211 if (mp->mnt_flag & MNT_FORCE)
212 flags |= FORCECLOSE;
213 error = ext2fs_flushfiles(mp, flags, p);
214 if (error == 0 &&
215 ext2fs_cgupdate(ump, MNT_WAIT) == 0 &&
216 (fs->e2fs.e2fs_state & E2FS_ERRORS) == 0) {
217 fs->e2fs.e2fs_state = E2FS_ISCLEAN;
218 (void) ext2fs_sbupdate(ump, MNT_WAIT);
219 }
220 if (error)
221 return (error);
222 fs->e2fs_ronly = 1;
223 }
224 if (mp->mnt_flag & MNT_RELOAD) {
225 error = ext2fs_reload(mp, ndp->ni_cnd.cn_cred, p);
226 if (error)
227 return (error);
228 }
229 if (fs->e2fs_ronly && (mp->mnt_flag & MNT_WANTRDWR)) {
230 /*
231 * If upgrade to read-write by non-root, then verify
232 * that user has necessary permissions on the device.
233 */
234 if (p->p_ucred->cr_uid != 0) {
235 devvp = ump->um_devvp;
236 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
237 error = VOP_ACCESS(devvp, VREAD | VWRITE,
238 p->p_ucred, p);
239 VOP_UNLOCK(devvp, 0);
240 if (error)
241 return (error);
242 }
243 fs->e2fs_ronly = 0;
244 if (fs->e2fs.e2fs_state == E2FS_ISCLEAN)
245 fs->e2fs.e2fs_state = 0;
246 else
247 fs->e2fs.e2fs_state = E2FS_ERRORS;
248 fs->e2fs_fmod = 1;
249 }
250 if (args.fspec == 0) {
251 /*
252 * Process export requests.
253 */
254 return (vfs_export(mp, &ump->um_export, &args.export));
255 }
256 }
257 /*
258 * Not an update, or updating the name: look up the name
259 * and verify that it refers to a sensible block device.
260 */
261 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
262 if ((error = namei(ndp)) != 0)
263 return (error);
264 devvp = ndp->ni_vp;
265
266 if (devvp->v_type != VBLK) {
267 vrele(devvp);
268 return (ENOTBLK);
269 }
270 if (major(devvp->v_rdev) >= nblkdev) {
271 vrele(devvp);
272 return (ENXIO);
273 }
274 /*
275 * If mount by non-root, then verify that user has necessary
276 * permissions on the device.
277 */
278 if (p->p_ucred->cr_uid != 0) {
279 accessmode = VREAD;
280 if ((mp->mnt_flag & MNT_RDONLY) == 0)
281 accessmode |= VWRITE;
282 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
283 error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
284 VOP_UNLOCK(devvp, 0);
285 if (error) {
286 vrele(devvp);
287 return (error);
288 }
289 }
290 if ((mp->mnt_flag & MNT_UPDATE) == 0)
291 error = ext2fs_mountfs(devvp, mp, p);
292 else {
293 if (devvp != ump->um_devvp)
294 error = EINVAL; /* needs translation */
295 else
296 vrele(devvp);
297 }
298 if (error) {
299 vrele(devvp);
300 return (error);
301 }
302 ump = VFSTOUFS(mp);
303 fs = ump->um_e2fs;
304 (void) copyinstr(path, fs->e2fs_fsmnt, sizeof(fs->e2fs_fsmnt) - 1, &size);
305 memset(fs->e2fs_fsmnt + size, 0, sizeof(fs->e2fs_fsmnt) - size);
306 memcpy(mp->mnt_stat.f_mntonname, fs->e2fs_fsmnt, MNAMELEN);
307 (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
308 &size);
309 memset(mp->mnt_stat.f_mntfromname + size, 0, MNAMELEN - size);
310 if (fs->e2fs_fmod != 0) { /* XXX */
311 fs->e2fs_fmod = 0;
312 if (fs->e2fs.e2fs_state == 0)
313 fs->e2fs.e2fs_wtime = time.tv_sec;
314 else
315 printf("%s: file system not clean; please fsck(8)\n",
316 mp->mnt_stat.f_mntfromname);
317 (void) ext2fs_cgupdate(ump, MNT_WAIT);
318 }
319 return (0);
320 }
321
322 /*
323 * Reload all incore data for a filesystem (used after running fsck on
324 * the root filesystem and finding things to fix). The filesystem must
325 * be mounted read-only.
326 *
327 * Things to do to update the mount:
328 * 1) invalidate all cached meta-data.
329 * 2) re-read superblock from disk.
330 * 3) re-read summary information from disk.
331 * 4) invalidate all inactive vnodes.
332 * 5) invalidate all cached file data.
333 * 6) re-read inode data for all active vnodes.
334 */
335 int
336 ext2fs_reload(mountp, cred, p)
337 register struct mount *mountp;
338 struct ucred *cred;
339 struct proc *p;
340 {
341 register struct vnode *vp, *nvp, *devvp;
342 struct inode *ip;
343 struct buf *bp;
344 struct m_ext2fs *fs;
345 struct ext2fs *newfs;
346 int i, size, error;
347 caddr_t cp;
348
349 if ((mountp->mnt_flag & MNT_RDONLY) == 0)
350 return (EINVAL);
351 /*
352 * Step 1: invalidate all cached meta-data.
353 */
354 devvp = VFSTOUFS(mountp)->um_devvp;
355 if (vinvalbuf(devvp, 0, cred, p, 0, 0))
356 panic("ext2fs_reload: dirty1");
357 /*
358 * Step 2: re-read superblock from disk.
359 */
360 #if 0
361 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED, p) != 0)
362 size = DEV_BSIZE;
363 else
364 size = dpart.disklab->d_secsize;
365 #endif
366 if ((mountp->mnt_bshift = devvp->v_specbshift) <= 0)
367 return (EINVAL);
368 size = blocksize(devvp->v_specbshift);
369
370 error = bread(devvp, (ufs_daddr_t)(SBOFF / size), SBSIZE, NOCRED, &bp);
371 if (error) {
372 brelse(bp);
373 return (error);
374 }
375 newfs = (struct ext2fs *)bp->b_data;
376 if (fs2h16(newfs->e2fs_magic) != E2FS_MAGIC) {
377 brelse(bp);
378 return (EIO); /* XXX needs translation */
379 }
380 if (fs2h32(newfs->e2fs_rev) != E2FS_REV) {
381 #ifdef DIAGNOSTIC
382 printf("Ext2 fs: unsupported revision number: %x (expected %x)\n",
383 fs2h32(newfs->e2fs_rev), E2FS_REV);
384 #endif
385 brelse(bp);
386 return (EIO); /* XXX needs translation */
387 }
388 if (fs2h32(newfs->e2fs_log_bsize) > 2) { /* block size = 1024|2048|4096 */
389 #ifdef DIAGNOSTIC
390 printf("Ext2 fs: bad block size: %d (expected <=2 for ext2 fs)\n",
391 fs2h32(newfs->e2fs_log_bsize));
392 #endif
393 brelse(bp);
394 return (EIO); /* XXX needs translation */
395 }
396
397
398 fs = VFSTOUFS(mountp)->um_e2fs;
399 /*
400 * copy in new superblock, and compute in-memory values
401 */
402 e2fs_sbload(newfs, &fs->e2fs);
403 fs->e2fs_ncg =
404 howmany(fs->e2fs.e2fs_bcount - fs->e2fs.e2fs_first_dblock,
405 fs->e2fs.e2fs_bpg);
406 /* XXX assume hw bsize = 512 */
407 fs->e2fs_fsbtodb = fs->e2fs.e2fs_log_bsize + 1;
408 fs->e2fs_bsize = 1024 << fs->e2fs.e2fs_log_bsize;
409 fs->e2fs_bshift = LOG_MINBSIZE + fs->e2fs.e2fs_log_bsize;
410 fs->e2fs_qbmask = fs->e2fs_bsize - 1;
411 fs->e2fs_bmask = ~fs->e2fs_qbmask;
412 fs->e2fs_ngdb = howmany(fs->e2fs_ncg,
413 fs->e2fs_bsize / sizeof(struct ext2_gd));
414 fs->e2fs_ipb = fs->e2fs_bsize / EXT2_DINODE_SIZE;
415 fs->e2fs_itpg = fs->e2fs.e2fs_ipg/fs->e2fs_ipb;
416
417 /*
418 * Step 3: re-read summary information from disk.
419 */
420
421 for (i=0; i < fs->e2fs_ngdb; i++) {
422 error = bread(devvp ,
423 fsbtodb(fs, ((fs->e2fs_bsize>1024)? 0 : 1) + i + 1),
424 fs->e2fs_bsize, NOCRED, &bp);
425 if (error) {
426 brelse(bp);
427 return (error);
428 }
429 e2fs_cgload((struct ext2_gd*)bp->b_data,
430 &fs->e2fs_gd[i* fs->e2fs_bsize / sizeof(struct ext2_gd)],
431 fs->e2fs_bsize);
432 brelse(bp);
433 }
434
435 loop:
436 simple_lock(&mntvnode_slock);
437 for (vp = mountp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
438 if (vp->v_mount != mountp) {
439 simple_unlock(&mntvnode_slock);
440 goto loop;
441 }
442 nvp = vp->v_mntvnodes.le_next;
443 /*
444 * Step 4: invalidate all inactive vnodes.
445 */
446 if (vrecycle(vp, &mntvnode_slock, p))
447 goto loop;
448 /*
449 * Step 5: invalidate all cached file data.
450 */
451 simple_lock(&vp->v_interlock);
452 simple_unlock(&mntvnode_slock);
453 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
454 goto loop;
455 if (vinvalbuf(vp, 0, cred, p, 0, 0))
456 panic("ext2fs_reload: dirty2");
457 /*
458 * Step 6: re-read inode data for all active vnodes.
459 */
460 ip = VTOI(vp);
461 error = bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
462 (int)fs->e2fs_bsize, NOCRED, &bp);
463 if (error) {
464 vput(vp);
465 return (error);
466 }
467 cp = (caddr_t)bp->b_data +
468 (ino_to_fsbo(fs, ip->i_number) * EXT2_DINODE_SIZE);
469 e2fs_iload((struct ext2fs_dinode *)cp, &ip->i_din.e2fs_din);
470 brelse(bp);
471 vput(vp);
472 simple_lock(&mntvnode_slock);
473 }
474 simple_unlock(&mntvnode_slock);
475 return (0);
476 }
477
478 /*
479 * Common code for mount and mountroot
480 */
481 int
482 ext2fs_mountfs(devvp, mp, p)
483 register struct vnode *devvp;
484 struct mount *mp;
485 struct proc *p;
486 {
487 register struct ufsmount *ump;
488 struct buf *bp;
489 register struct ext2fs *fs;
490 register struct m_ext2fs *m_fs;
491 dev_t dev;
492 int error, i, size, ronly;
493 struct ucred *cred;
494 extern struct vnode *rootvp;
495
496 dev = devvp->v_rdev;
497 cred = p ? p->p_ucred : NOCRED;
498 /*
499 * Disallow multiple mounts of the same device.
500 * Disallow mounting of a device that is currently in use
501 * (except for root, which might share swap device for miniroot).
502 * Flush out any old buffers remaining from a previous use.
503 */
504 if ((error = vfs_mountedon(devvp)) != 0)
505 return (error);
506 if (vcount(devvp) > 1 && devvp != rootvp)
507 return (EBUSY);
508 if ((error = vinvalbuf(devvp, V_SAVE, cred, p, 0, 0)) != 0)
509 return (error);
510
511 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
512 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
513 if (error)
514 return (error);
515 #if 0
516 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, cred, p) != 0)
517 size = DEV_BSIZE;
518 else
519 size = dpart.disklab->d_secsize;
520 #endif
521 if ((mp->mnt_bshift = devvp->v_specbshift) <= 0)
522 return (EINVAL);
523 size = blocksize(devvp->v_specbshift);
524
525 bp = NULL;
526 ump = NULL;
527
528 #ifdef DEBUG_EXT2
529 printf("sb size: %d ino size %d\n", sizeof(struct ext2fs),
530 EXT2_DINODE_SIZE);
531 #endif
532 error = bread(devvp, (SBOFF >> mp->mnt_bshift), SBSIZE, cred, &bp);
533 if (error)
534 goto out;
535 fs = (struct ext2fs *)bp->b_data;
536 if (fs2h16(fs->e2fs_magic) != E2FS_MAGIC) {
537 error = EINVAL; /* XXX needs translation */
538 goto out;
539 }
540 if (fs2h32(fs->e2fs_rev) != E2FS_REV) {
541 #ifdef DIAGNOSTIC
542 printf("Ext2 fs: unsupported revision number: %x (expected %x)\n",
543 fs2h32(fs->e2fs_rev), E2FS_REV);
544 #endif
545 error = EINVAL; /* XXX needs translation */
546 goto out;
547 }
548 if (fs2h32(fs->e2fs_log_bsize) > 2) { /* block size = 1024|2048|4096 */
549 #ifdef DIAGNOSTIC
550 printf("Ext2 fs: bad block size: %d (expected <=2 for ext2 fs)\n",
551 fs2h32(fs->e2fs_log_bsize));
552 #endif
553 error = EINVAL; /* XXX needs translation */
554 goto out;
555 }
556
557 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
558 memset((caddr_t)ump, 0, sizeof *ump);
559 ump->um_e2fs = malloc(sizeof(struct m_ext2fs), M_UFSMNT, M_WAITOK);
560 memset((caddr_t)ump->um_e2fs, 0, sizeof(struct m_ext2fs));
561 e2fs_sbload((struct ext2fs*)bp->b_data, &ump->um_e2fs->e2fs);
562 brelse(bp);
563 bp = NULL;
564 m_fs = ump->um_e2fs;
565 m_fs->e2fs_ronly = ronly;
566 if (ronly == 0) {
567 if (m_fs->e2fs.e2fs_state == E2FS_ISCLEAN)
568 m_fs->e2fs.e2fs_state = 0;
569 else
570 m_fs->e2fs.e2fs_state = E2FS_ERRORS;
571 m_fs->e2fs_fmod = 1;
572 }
573
574 /* compute dynamic sb infos */
575 m_fs->e2fs_ncg =
576 howmany(m_fs->e2fs.e2fs_bcount - m_fs->e2fs.e2fs_first_dblock,
577 m_fs->e2fs.e2fs_bpg);
578 /* XXX assume hw bsize = 512 */
579 m_fs->e2fs_fsbtodb = m_fs->e2fs.e2fs_log_bsize + 1;
580 m_fs->e2fs_bsize = 1024 << m_fs->e2fs.e2fs_log_bsize;
581 m_fs->e2fs_bshift = LOG_MINBSIZE + m_fs->e2fs.e2fs_log_bsize;
582 m_fs->e2fs_qbmask = m_fs->e2fs_bsize - 1;
583 m_fs->e2fs_bmask = ~m_fs->e2fs_qbmask;
584 m_fs->e2fs_ngdb = howmany(m_fs->e2fs_ncg,
585 m_fs->e2fs_bsize / sizeof(struct ext2_gd));
586 m_fs->e2fs_ipb = m_fs->e2fs_bsize / EXT2_DINODE_SIZE;
587 m_fs->e2fs_itpg = m_fs->e2fs.e2fs_ipg/m_fs->e2fs_ipb;
588
589 m_fs->e2fs_gd = malloc(m_fs->e2fs_ngdb * m_fs->e2fs_bsize,
590 M_UFSMNT, M_WAITOK);
591 for (i=0; i < m_fs->e2fs_ngdb; i++) {
592 error = bread(devvp ,
593 fsbtodb(m_fs, ((m_fs->e2fs_bsize>1024)? 0 : 1) + i + 1),
594 m_fs->e2fs_bsize, NOCRED, &bp);
595 if (error) {
596 free(m_fs->e2fs_gd, M_UFSMNT);
597 goto out;
598 }
599 e2fs_cgload((struct ext2_gd*)bp->b_data,
600 &m_fs->e2fs_gd[
601 i * m_fs->e2fs_bsize / sizeof(struct ext2_gd)],
602 m_fs->e2fs_bsize);
603 brelse(bp);
604 bp = NULL;
605 }
606
607 mp->mnt_data = (qaddr_t)ump;
608 mp->mnt_stat.f_fsid.val[0] = (long)dev;
609 mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_EXT2FS);
610 mp->mnt_maxsymlinklen = EXT2_MAXSYMLINKLEN;
611 mp->mnt_flag |= MNT_LOCAL;
612 ump->um_flags = 0;
613 ump->um_mountp = mp;
614 ump->um_dev = dev;
615 ump->um_devvp = devvp;
616 ump->um_nindir = NINDIR(m_fs);
617 ump->um_bptrtodb = m_fs->e2fs_fsbtodb;
618 ump->um_seqinc = 1; /* no frags */
619 devvp->v_specflags |= SI_MOUNTEDON;
620 return (0);
621 out:
622 if (bp)
623 brelse(bp);
624 (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p);
625 if (ump) {
626 free(ump->um_e2fs, M_UFSMNT);
627 free(ump, M_UFSMNT);
628 mp->mnt_data = (qaddr_t)0;
629 }
630 return (error);
631 }
632
633 /*
634 * unmount system call
635 */
636 int
637 ext2fs_unmount(mp, mntflags, p)
638 struct mount *mp;
639 int mntflags;
640 struct proc *p;
641 {
642 register struct ufsmount *ump;
643 register struct m_ext2fs *fs;
644 int error, flags;
645
646 flags = 0;
647 if (mntflags & MNT_FORCE)
648 flags |= FORCECLOSE;
649 if ((error = ext2fs_flushfiles(mp, flags, p)) != 0)
650 return (error);
651 ump = VFSTOUFS(mp);
652 fs = ump->um_e2fs;
653 if (fs->e2fs_ronly == 0 &&
654 ext2fs_cgupdate(ump, MNT_WAIT) == 0 &&
655 (fs->e2fs.e2fs_state & E2FS_ERRORS) == 0) {
656 fs->e2fs.e2fs_state = E2FS_ISCLEAN;
657 (void) ext2fs_sbupdate(ump, MNT_WAIT);
658 }
659 ump->um_devvp->v_specflags &= ~SI_MOUNTEDON;
660 error = VOP_CLOSE(ump->um_devvp, fs->e2fs_ronly ? FREAD : FREAD|FWRITE,
661 NOCRED, p);
662 vrele(ump->um_devvp);
663 free(fs->e2fs_gd, M_UFSMNT);
664 free(fs, M_UFSMNT);
665 free(ump, M_UFSMNT);
666 mp->mnt_data = (qaddr_t)0;
667 mp->mnt_flag &= ~MNT_LOCAL;
668 return (error);
669 }
670
671 /*
672 * Flush out all the files in a filesystem.
673 */
674 int
675 ext2fs_flushfiles(mp, flags, p)
676 register struct mount *mp;
677 int flags;
678 struct proc *p;
679 {
680 extern int doforce;
681 register struct ufsmount *ump;
682 int error;
683
684 if (!doforce)
685 flags &= ~FORCECLOSE;
686 ump = VFSTOUFS(mp);
687 error = vflush(mp, NULLVP, flags);
688 return (error);
689 }
690
691 /*
692 * Get file system statistics.
693 */
694 int
695 ext2fs_statfs(mp, sbp, p)
696 struct mount *mp;
697 register struct statfs *sbp;
698 struct proc *p;
699 {
700 register struct ufsmount *ump;
701 register struct m_ext2fs *fs;
702 u_int32_t overhead, overhead_per_group;
703
704 ump = VFSTOUFS(mp);
705 fs = ump->um_e2fs;
706 if (fs->e2fs.e2fs_magic != E2FS_MAGIC)
707 panic("ext2fs_statfs");
708
709 #ifdef COMPAT_09
710 sbp->f_type = 1;
711 #else
712 sbp->f_type = 0;
713 #endif
714
715 /*
716 * Compute the overhead (FS structures)
717 */
718 overhead_per_group = 1 /* super block */ +
719 fs->e2fs_ngdb +
720 1 /* block bitmap */ +
721 1 /* inode bitmap */ +
722 fs->e2fs_itpg;
723 overhead = fs->e2fs.e2fs_first_dblock +
724 fs->e2fs_ncg * overhead_per_group;
725
726
727 sbp->f_bsize = fs->e2fs_bsize;
728 sbp->f_iosize = fs->e2fs_bsize;
729 sbp->f_blocks = fs->e2fs.e2fs_bcount - overhead;
730 sbp->f_bfree = fs->e2fs.e2fs_fbcount;
731 sbp->f_bavail = sbp->f_bfree - fs->e2fs.e2fs_rbcount;
732 sbp->f_files = fs->e2fs.e2fs_icount;
733 sbp->f_ffree = fs->e2fs.e2fs_ficount;
734 if (sbp != &mp->mnt_stat) {
735 memcpy(sbp->f_mntonname, mp->mnt_stat.f_mntonname, MNAMELEN);
736 memcpy(sbp->f_mntfromname, mp->mnt_stat.f_mntfromname, MNAMELEN);
737 }
738 strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN);
739 return (0);
740 }
741
742 /*
743 * Go through the disk queues to initiate sandbagged IO;
744 * go through the inodes to write those that have been modified;
745 * initiate the writing of the super block if it has been modified.
746 *
747 * Note: we are always called with the filesystem marked `MPBUSY'.
748 */
749 int
750 ext2fs_sync(mp, waitfor, cred, p)
751 struct mount *mp;
752 int waitfor;
753 struct ucred *cred;
754 struct proc *p;
755 {
756 struct vnode *vp, *nvp;
757 struct inode *ip;
758 struct ufsmount *ump = VFSTOUFS(mp);
759 struct m_ext2fs *fs;
760 int error, allerror = 0;
761
762 fs = ump->um_e2fs;
763 if (fs->e2fs_ronly != 0) { /* XXX */
764 printf("fs = %s\n", fs->e2fs_fsmnt);
765 panic("update: rofs mod");
766 }
767
768 /*
769 * Write back each (modified) inode.
770 */
771 simple_lock(&mntvnode_slock);
772 loop:
773 for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
774 /*
775 * If the vnode that we are about to sync is no longer
776 * associated with this mount point, start over.
777 */
778 if (vp->v_mount != mp)
779 goto loop;
780 simple_lock(&vp->v_interlock);
781 nvp = vp->v_mntvnodes.le_next;
782 ip = VTOI(vp);
783 if ((ip->i_flag &
784 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
785 vp->v_dirtyblkhd.lh_first == NULL) {
786 simple_unlock(&vp->v_interlock);
787 continue;
788 }
789 simple_unlock(&mntvnode_slock);
790 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
791 if (error) {
792 simple_lock(&mntvnode_slock);
793 if (error == ENOENT)
794 goto loop;
795 continue;
796 }
797 if ((error = VOP_FSYNC(vp, cred,
798 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, p)) != 0)
799 allerror = error;
800 vput(vp);
801 simple_lock(&mntvnode_slock);
802 }
803 simple_unlock(&mntvnode_slock);
804 /*
805 * Force stale file system control information to be flushed.
806 */
807 if ((error = VOP_FSYNC(ump->um_devvp, cred,
808 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, p)) != 0)
809 allerror = error;
810 /*
811 * Write back modified superblock.
812 */
813 if (fs->e2fs_fmod != 0) {
814 fs->e2fs_fmod = 0;
815 fs->e2fs.e2fs_wtime = time.tv_sec;
816 if ((error = ext2fs_cgupdate(ump, waitfor)))
817 allerror = error;
818 }
819 return (allerror);
820 }
821
822 /*
823 * Look up a EXT2FS dinode number to find its incore vnode, otherwise read it
824 * in from disk. If it is in core, wait for the lock bit to clear, then
825 * return the inode locked. Detection and handling of mount points must be
826 * done by the calling routine.
827 */
828 int
829 ext2fs_vget(mp, ino, vpp)
830 struct mount *mp;
831 ino_t ino;
832 struct vnode **vpp;
833 {
834 struct m_ext2fs *fs;
835 struct inode *ip;
836 struct ufsmount *ump;
837 struct buf *bp;
838 struct vnode *vp;
839 dev_t dev;
840 int error;
841 caddr_t cp;
842
843 ump = VFSTOUFS(mp);
844 dev = ump->um_dev;
845 do {
846 if ((*vpp = ufs_ihashget(dev, ino)) != NULL)
847 return (0);
848 } while (lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
849
850 /* Allocate a new vnode/inode. */
851 if ((error = getnewvnode(VT_EXT2FS, mp, ext2fs_vnodeop_p, &vp)) != 0) {
852 *vpp = NULL;
853 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
854 return (error);
855 }
856 ip = pool_get(&ext2fs_inode_pool, PR_WAITOK);
857 memset((caddr_t)ip, 0, sizeof(struct inode));
858 vp->v_data = ip;
859 ip->i_vnode = vp;
860 ip->i_e2fs = fs = ump->um_e2fs;
861 ip->i_dev = dev;
862 ip->i_number = ino;
863 ip->i_e2fs_last_lblk = 0;
864 ip->i_e2fs_last_blk = 0;
865
866 /*
867 * Put it onto its hash chain and lock it so that other requests for
868 * this inode will block if they arrive while we are sleeping waiting
869 * for old data structures to be purged or for the contents of the
870 * disk portion of this inode to be read.
871 */
872 ufs_ihashins(ip);
873 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
874
875 /* Read in the disk contents for the inode, copy into the inode. */
876 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
877 (int)fs->e2fs_bsize, NOCRED, &bp);
878 if (error) {
879 /*
880 * The inode does not contain anything useful, so it would
881 * be misleading to leave it on its hash chain. With mode
882 * still zero, it will be unlinked and returned to the free
883 * list by vput().
884 */
885 vput(vp);
886 brelse(bp);
887 *vpp = NULL;
888 return (error);
889 }
890 cp = (caddr_t)bp->b_data +
891 (ino_to_fsbo(fs, ino) * EXT2_DINODE_SIZE);
892 e2fs_iload((struct ext2fs_dinode *)cp, &ip->i_din.e2fs_din);
893 brelse(bp);
894
895 /* If the inode was deleted, reset all fields */
896 if (ip->i_e2fs_dtime != 0) {
897 ip->i_e2fs_mode = ip->i_e2fs_size = ip->i_e2fs_nblock = 0;
898 memset(ip->i_e2fs_blocks, 0, sizeof(ip->i_e2fs_blocks));
899 }
900
901 /*
902 * Initialize the vnode from the inode, check for aliases.
903 * Note that the underlying vnode may have changed.
904 */
905 error = ext2fs_vinit(mp, ext2fs_specop_p, ext2fs_fifoop_p, &vp);
906 if (error) {
907 vput(vp);
908 *vpp = NULL;
909 return (error);
910 }
911 /*
912 * Finish inode initialization now that aliasing has been resolved.
913 */
914 ip->i_devvp = ump->um_devvp;
915 VREF(ip->i_devvp);
916 /*
917 * Set up a generation number for this inode if it does not
918 * already have one. This should only happen on old filesystems.
919 */
920 if (ip->i_e2fs_gen == 0) {
921 if (++ext2gennumber < (u_long)time.tv_sec)
922 ext2gennumber = time.tv_sec;
923 ip->i_e2fs_gen = ext2gennumber;
924 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
925 ip->i_flag |= IN_MODIFIED;
926 }
927
928 *vpp = vp;
929 return (0);
930 }
931
932 /*
933 * File handle to vnode
934 *
935 * Have to be really careful about stale file handles:
936 * - check that the inode number is valid
937 * - call ext2fs_vget() to get the locked inode
938 * - check for an unallocated inode (i_mode == 0)
939 */
940 int
941 ext2fs_fhtovp(mp, fhp, vpp)
942 register struct mount *mp;
943 struct fid *fhp;
944 struct vnode **vpp;
945 {
946 register struct inode *ip;
947 struct vnode *nvp;
948 int error;
949 register struct ufid *ufhp;
950 struct m_ext2fs *fs;
951
952 ufhp = (struct ufid *)fhp;
953 fs = VFSTOUFS(mp)->um_e2fs;
954 if ((ufhp->ufid_ino < EXT2_FIRSTINO && ufhp->ufid_ino != EXT2_ROOTINO) ||
955 ufhp->ufid_ino >= fs->e2fs_ncg * fs->e2fs.e2fs_ipg)
956 return (ESTALE);
957
958 if ((error = VFS_VGET(mp, ufhp->ufid_ino, &nvp)) != 0) {
959 *vpp = NULLVP;
960 return (error);
961 }
962 ip = VTOI(nvp);
963 if (ip->i_e2fs_mode == 0 || ip->i_e2fs_dtime != 0 ||
964 ip->i_e2fs_gen != ufhp->ufid_gen) {
965 vput(nvp);
966 *vpp = NULLVP;
967 return (ESTALE);
968 }
969 *vpp = nvp;
970 return (0);
971 }
972
973 /*
974 * Vnode pointer to File handle
975 */
976 /* ARGSUSED */
977 int
978 ext2fs_vptofh(vp, fhp)
979 struct vnode *vp;
980 struct fid *fhp;
981 {
982 register struct inode *ip;
983 register struct ufid *ufhp;
984
985 ip = VTOI(vp);
986 ufhp = (struct ufid *)fhp;
987 ufhp->ufid_len = sizeof(struct ufid);
988 ufhp->ufid_ino = ip->i_number;
989 ufhp->ufid_gen = ip->i_e2fs_gen;
990 return (0);
991 }
992
993 int
994 ext2fs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
995 int *name;
996 u_int namelen;
997 void *oldp;
998 size_t *oldlenp;
999 void *newp;
1000 size_t newlen;
1001 struct proc *p;
1002 {
1003 return (EOPNOTSUPP);
1004 }
1005
1006 /*
1007 * Write a superblock and associated information back to disk.
1008 */
1009 int
1010 ext2fs_sbupdate(mp, waitfor)
1011 struct ufsmount *mp;
1012 int waitfor;
1013 {
1014 register struct m_ext2fs *fs = mp->um_e2fs;
1015 register struct buf *bp;
1016 int error = 0;
1017
1018 bp = getblk(mp->um_devvp, btodb(SBOFF, mp->um_mountp->mnt_bshift),
1019 SBSIZE, 0, 0);
1020 e2fs_sbsave(&fs->e2fs, (struct ext2fs*)bp->b_data);
1021 if (waitfor == MNT_WAIT)
1022 error = bwrite(bp);
1023 else
1024 bawrite(bp);
1025 return (error);
1026 }
1027
1028 int
1029 ext2fs_cgupdate(mp, waitfor)
1030 struct ufsmount *mp;
1031 int waitfor;
1032 {
1033 register struct m_ext2fs *fs = mp->um_e2fs;
1034 register struct buf *bp;
1035 int i, error = 0, allerror = 0;
1036
1037 allerror = ext2fs_sbupdate(mp, waitfor);
1038 for (i = 0; i < fs->e2fs_ngdb; i++) {
1039 bp = getblk(mp->um_devvp, fsbtodb(fs, ((fs->e2fs_bsize>1024)?0:1)+i+1),
1040 fs->e2fs_bsize, 0, 0);
1041 e2fs_cgsave(&fs->e2fs_gd[i* fs->e2fs_bsize / sizeof(struct ext2_gd)],
1042 (struct ext2_gd*)bp->b_data, fs->e2fs_bsize);
1043 if (waitfor == MNT_WAIT)
1044 error = bwrite(bp);
1045 else
1046 bawrite(bp);
1047 }
1048
1049 if (!allerror && error)
1050 allerror = error;
1051 return (allerror);
1052 }
1053