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