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