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