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