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