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