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