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