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