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