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