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