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