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