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