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