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