ext2fs_vfsops.c revision 1.9 1 /* $NetBSD: ext2fs_vfsops.c,v 1.9 1998/03/01 02:23:46 fvdl Exp $ */
2
3 /*
4 * Copyright (c) 1997 Manuel Bouyer.
5 * Copyright (c) 1989, 1991, 1993, 1994
6 * The Regents of the University of California. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)ffs_vfsops.c 8.14 (Berkeley) 11/28/94
37 * Modified for ext2fs by Manuel Bouyer.
38 */
39
40 #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 fs2h32(newfs->e2fs_rev) != E2FS_REV) {
410 #ifdef DIAGNOSTIC
411 printf("Wrong magic number: %x (expected %x for ext2 fs)",
412 fs2h16(newfs->e2fs_magic), E2FS_MAGIC);
413 printf("or wrong revision number: %x (expected %x for ext2 fs)",
414 fs2h32(newfs->e2fs_rev), E2FS_REV);
415 #endif
416 brelse(bp);
417 return (EIO); /* XXX needs translation */
418 }
419 if (fs2h32(newfs->e2fs_log_bsize) > 2) { /* block size = 1024|2048|4096 */
420 #ifdef DIAGNOSTIC
421 printf("wrong block size: %d (expected <=2 for ext2 fs)\n",
422 fs2h32(newfs->e2fs_log_bsize));
423 #endif
424 brelse(bp);
425 return (EIO); /* XXX needs translation */
426 }
427
428
429 fs = VFSTOUFS(mountp)->um_e2fs;
430 /*
431 * copy in new superblock, and compute in-memory values
432 */
433 e2fs_sbload(newfs, &fs->e2fs);
434 fs->e2fs_ncg = howmany(fs->e2fs.e2fs_bcount - fs->e2fs.e2fs_first_dblock,
435 fs->e2fs.e2fs_bpg);
436 /* XXX assume hw bsize = 512 */
437 fs->e2fs_fsbtodb = fs->e2fs.e2fs_log_bsize + 1;
438 fs->e2fs_bsize = 1024 << fs->e2fs.e2fs_log_bsize;
439 fs->e2fs_bshift = LOG_MINBSIZE + fs->e2fs.e2fs_log_bsize;
440 fs->e2fs_qbmask = fs->e2fs_bsize - 1;
441 fs->e2fs_bmask = ~fs->e2fs_qbmask;
442 fs->e2fs_ngdb = howmany(fs->e2fs_ncg,
443 fs->e2fs_bsize / sizeof(struct ext2_gd));
444 fs->e2fs_ipb = fs->e2fs_bsize / sizeof(struct ext2fs_dinode);
445 fs->e2fs_itpg = fs->e2fs.e2fs_ipg/fs->e2fs_ipb;
446
447 /*
448 * Step 3: re-read summary information from disk.
449 */
450
451 for (i=0; i < fs->e2fs_ngdb; i++) {
452 error = bread(devvp , fsbtodb(fs, ((fs->e2fs_bsize>1024)?0:1)+i+1),
453 fs->e2fs_bsize, NOCRED, &bp);
454 if (error)
455 return (error);
456 e2fs_cgload((struct ext2_gd*)bp->b_data,
457 &fs->e2fs_gd[i* fs->e2fs_bsize / sizeof(struct ext2_gd)],
458 fs->e2fs_bsize);
459 brelse(bp);
460 }
461
462 loop:
463 simple_lock(&mntvnode_slock);
464 for (vp = mountp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
465 if (vp->v_mount != mountp) {
466 simple_unlock(&mntvnode_slock);
467 goto loop;
468 }
469 nvp = vp->v_mntvnodes.le_next;
470 /*
471 * Step 4: invalidate all inactive vnodes.
472 */
473 if (vrecycle(vp, &mntvnode_slock, p))
474 goto loop;
475 /*
476 * Step 5: invalidate all cached file data.
477 */
478 simple_lock(&vp->v_interlock);
479 simple_unlock(&mntvnode_slock);
480 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
481 goto loop;
482 if (vinvalbuf(vp, 0, cred, p, 0, 0))
483 panic("ext2fs_reload: dirty2");
484 /*
485 * Step 6: re-read inode data for all active vnodes.
486 */
487 ip = VTOI(vp);
488 error = bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
489 (int)fs->e2fs_bsize, NOCRED, &bp);
490 if (error) {
491 vput(vp);
492 return (error);
493 }
494 e2fs_iload((struct ext2fs_dinode *)bp->b_data +
495 ino_to_fsbo(fs, ip->i_number),
496 &ip->i_din.e2fs_din);
497 brelse(bp);
498 vput(vp);
499 simple_lock(&mntvnode_slock);
500 }
501 simple_unlock(&mntvnode_slock);
502 return (0);
503 }
504
505 /*
506 * Common code for mount and mountroot
507 */
508 int
509 ext2fs_mountfs(devvp, mp, p)
510 register struct vnode *devvp;
511 struct mount *mp;
512 struct proc *p;
513 {
514 register struct ufsmount *ump;
515 struct buf *bp;
516 register struct ext2fs *fs;
517 register struct m_ext2fs *m_fs;
518 dev_t dev;
519 struct partinfo dpart;
520 int error, i, size, ronly;
521 struct ucred *cred;
522 extern struct vnode *rootvp;
523
524 dev = devvp->v_rdev;
525 cred = p ? p->p_ucred : NOCRED;
526 /*
527 * Disallow multiple mounts of the same device.
528 * Disallow mounting of a device that is currently in use
529 * (except for root, which might share swap device for miniroot).
530 * Flush out any old buffers remaining from a previous use.
531 */
532 if ((error = vfs_mountedon(devvp)) != 0)
533 return (error);
534 if (vcount(devvp) > 1 && devvp != rootvp)
535 return (EBUSY);
536 if ((error = vinvalbuf(devvp, V_SAVE, cred, p, 0, 0)) != 0)
537 return (error);
538
539 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
540 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
541 if (error)
542 return (error);
543 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, cred, p) != 0)
544 size = DEV_BSIZE;
545 else
546 size = dpart.disklab->d_secsize;
547
548 bp = NULL;
549 ump = NULL;
550
551 #ifdef DEBUG_EXT2
552 printf("sb size: %d ino size %d\n", sizeof(struct ext2fs),
553 sizeof(struct ext2fs_dinode));
554 #endif
555 error = bread(devvp, (SBOFF / DEV_BSIZE), SBSIZE, cred, &bp);
556 if (error)
557 goto out;
558 fs = (struct ext2fs *)bp->b_data;
559 if (fs2h16(fs->e2fs_magic) != E2FS_MAGIC ||
560 fs2h32(fs->e2fs_rev) != E2FS_REV) {
561 #ifdef DIAGNOSTIC
562 printf("Wrong magic number: %x (expected %x for ext2 fs)",
563 fs2h16(fs->e2fs_magic), E2FS_MAGIC);
564 printf(" or wrong revision number: %x (expected %x for ext2 fs)\n",
565 fs2h32(fs->e2fs_rev), E2FS_REV);
566 #endif
567 error = EINVAL; /* XXX needs translation */
568 goto out;
569 }
570
571 if (fs2h32(fs->e2fs_log_bsize) > 2) { /* block size = 1024|2048|4096 */
572 #ifdef DIAGNOSTIC
573 printf("wrong block size: %d (expected <2 for ext2 fs)\n",
574 fs2h32(fs->e2fs_log_bsize));
575 #endif
576 error = EINVAL; /* XXX needs translation */
577 goto out;
578 }
579
580 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
581 bzero((caddr_t)ump, sizeof *ump);
582 ump->um_e2fs = malloc(sizeof(struct m_ext2fs), M_UFSMNT, M_WAITOK);
583 e2fs_sbload((struct ext2fs*)bp->b_data, &ump->um_e2fs->e2fs);
584 brelse(bp);
585 bp = NULL;
586 m_fs = ump->um_e2fs;
587 m_fs->e2fs_ronly = ronly;
588 if (ronly == 0) {
589 if (m_fs->e2fs.e2fs_state == E2FS_ISCLEAN)
590 m_fs->e2fs.e2fs_state = 0;
591 else
592 m_fs->e2fs.e2fs_state = E2FS_ERRORS;
593 m_fs->e2fs_fmod = 1;
594 }
595
596 /* compute dynamic sb infos */
597 m_fs->e2fs_ncg =
598 howmany(m_fs->e2fs.e2fs_bcount - m_fs->e2fs.e2fs_first_dblock,
599 m_fs->e2fs.e2fs_bpg);
600 /* XXX assume hw bsize = 512 */
601 m_fs->e2fs_fsbtodb = m_fs->e2fs.e2fs_log_bsize + 1;
602 m_fs->e2fs_bsize = 1024 << m_fs->e2fs.e2fs_log_bsize;
603 m_fs->e2fs_bshift = LOG_MINBSIZE + m_fs->e2fs.e2fs_log_bsize;
604 m_fs->e2fs_qbmask = m_fs->e2fs_bsize - 1;
605 m_fs->e2fs_bmask = ~m_fs->e2fs_qbmask;
606 m_fs->e2fs_ngdb = howmany(m_fs->e2fs_ncg,
607 m_fs->e2fs_bsize / sizeof(struct ext2_gd));
608 m_fs->e2fs_ipb = m_fs->e2fs_bsize / sizeof(struct ext2fs_dinode);
609 m_fs->e2fs_itpg = m_fs->e2fs.e2fs_ipg/m_fs->e2fs_ipb;
610
611 m_fs->e2fs_gd = malloc(m_fs->e2fs_ngdb * m_fs->e2fs_bsize,
612 M_UFSMNT, M_WAITOK);
613 for (i=0; i < m_fs->e2fs_ngdb; i++) {
614 error = bread(devvp , fsbtodb(m_fs, ((m_fs->e2fs_bsize>1024)?0:1)+i+1),
615 m_fs->e2fs_bsize, NOCRED, &bp);
616 if (error) {
617 free(m_fs->e2fs_gd, M_UFSMNT);
618 goto out;
619 }
620 e2fs_cgload((struct ext2_gd*)bp->b_data,
621 &m_fs->e2fs_gd[i* m_fs->e2fs_bsize / sizeof(struct ext2_gd)],
622 m_fs->e2fs_bsize);
623 brelse(bp);
624 bp = NULL;
625 }
626
627 mp->mnt_data = (qaddr_t)ump;
628 mp->mnt_stat.f_fsid.val[0] = (long)dev;
629 mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_EXT2FS);
630 mp->mnt_maxsymlinklen = EXT2_MAXSYMLINKLEN;
631 mp->mnt_flag |= MNT_LOCAL;
632 ump->um_mountp = mp;
633 ump->um_dev = dev;
634 ump->um_devvp = devvp;
635 ump->um_nindir = NINDIR(m_fs);
636 ump->um_bptrtodb = m_fs->e2fs_fsbtodb;
637 ump->um_seqinc = 1; /* no frags */
638 devvp->v_specflags |= SI_MOUNTEDON;
639 return (0);
640 out:
641 if (bp)
642 brelse(bp);
643 (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p);
644 if (ump) {
645 free(ump->um_e2fs, M_UFSMNT);
646 free(ump, M_UFSMNT);
647 mp->mnt_data = (qaddr_t)0;
648 }
649 return (error);
650 }
651
652 /*
653 * unmount system call
654 */
655 int
656 ext2fs_unmount(mp, mntflags, p)
657 struct mount *mp;
658 int mntflags;
659 struct proc *p;
660 {
661 register struct ufsmount *ump;
662 register struct m_ext2fs *fs;
663 int error, flags;
664
665 flags = 0;
666 if (mntflags & MNT_FORCE)
667 flags |= FORCECLOSE;
668 if ((error = ext2fs_flushfiles(mp, flags, p)) != 0)
669 return (error);
670 ump = VFSTOUFS(mp);
671 fs = ump->um_e2fs;
672 if (fs->e2fs_ronly == 0 &&
673 ext2fs_cgupdate(ump, MNT_WAIT) == 0 &&
674 (fs->e2fs.e2fs_state & E2FS_ERRORS) == 0) {
675 fs->e2fs.e2fs_state = E2FS_ISCLEAN;
676 (void) ext2fs_sbupdate(ump, MNT_WAIT);
677 }
678 ump->um_devvp->v_specflags &= ~SI_MOUNTEDON;
679 error = VOP_CLOSE(ump->um_devvp, fs->e2fs_ronly ? FREAD : FREAD|FWRITE,
680 NOCRED, p);
681 vrele(ump->um_devvp);
682 free(fs->e2fs_gd, M_UFSMNT);
683 free(fs, M_UFSMNT);
684 free(ump, M_UFSMNT);
685 mp->mnt_data = (qaddr_t)0;
686 mp->mnt_flag &= ~MNT_LOCAL;
687 return (error);
688 }
689
690 /*
691 * Flush out all the files in a filesystem.
692 */
693 int
694 ext2fs_flushfiles(mp, flags, p)
695 register struct mount *mp;
696 int flags;
697 struct proc *p;
698 {
699 extern int doforce;
700 register struct ufsmount *ump;
701 int error;
702
703 if (!doforce)
704 flags &= ~FORCECLOSE;
705 ump = VFSTOUFS(mp);
706 error = vflush(mp, NULLVP, flags);
707 return (error);
708 }
709
710 /*
711 * Get file system statistics.
712 */
713 int
714 ext2fs_statfs(mp, sbp, p)
715 struct mount *mp;
716 register struct statfs *sbp;
717 struct proc *p;
718 {
719 register struct ufsmount *ump;
720 register struct m_ext2fs *fs;
721 u_int32_t overhead, overhead_per_group;
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 /* super block */ +
738 fs->e2fs_ngdb +
739 1 /* block bitmap */ +
740 1 /* inode bitmap */ +
741 fs->e2fs_itpg;
742 overhead = fs->e2fs.e2fs_first_dblock +
743 fs->e2fs_ncg * overhead_per_group;
744
745
746 sbp->f_bsize = fs->e2fs_bsize;
747 sbp->f_iosize = fs->e2fs_bsize;
748 sbp->f_blocks = fs->e2fs.e2fs_bcount - overhead;
749 sbp->f_bfree = fs->e2fs.e2fs_fbcount;
750 sbp->f_bavail = sbp->f_bfree - fs->e2fs.e2fs_rbcount;
751 sbp->f_files = fs->e2fs.e2fs_icount;
752 sbp->f_ffree = fs->e2fs.e2fs_ficount;
753 if (sbp != &mp->mnt_stat) {
754 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
755 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
756 }
757 strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN);
758 return (0);
759 }
760
761 /*
762 * Go through the disk queues to initiate sandbagged IO;
763 * go through the inodes to write those that have been modified;
764 * initiate the writing of the super block if it has been modified.
765 *
766 * Note: we are always called with the filesystem marked `MPBUSY'.
767 */
768 int
769 ext2fs_sync(mp, waitfor, cred, p)
770 struct mount *mp;
771 int waitfor;
772 struct ucred *cred;
773 struct proc *p;
774 {
775 struct vnode *vp, *nvp;
776 struct inode *ip;
777 struct ufsmount *ump = VFSTOUFS(mp);
778 struct m_ext2fs *fs;
779 int error, allerror = 0;
780
781 fs = ump->um_e2fs;
782 if (fs->e2fs_ronly != 0) { /* XXX */
783 printf("fs = %s\n", fs->e2fs_fsmnt);
784 panic("update: rofs mod");
785 }
786
787 /*
788 * Write back each (modified) inode.
789 */
790 simple_lock(&mntvnode_slock);
791 loop:
792 for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
793 /*
794 * If the vnode that we are about to sync is no longer
795 * associated with this mount point, start over.
796 */
797 if (vp->v_mount != mp)
798 goto loop;
799 simple_lock(&vp->v_interlock);
800 nvp = vp->v_mntvnodes.le_next;
801 ip = VTOI(vp);
802 if ((ip->i_flag &
803 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
804 vp->v_dirtyblkhd.lh_first == NULL) {
805 simple_unlock(&vp->v_interlock);
806 continue;
807 }
808 simple_unlock(&mntvnode_slock);
809 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
810 if (error) {
811 simple_lock(&mntvnode_slock);
812 if (error == ENOENT)
813 goto loop;
814 }
815 if ((error = VOP_FSYNC(vp, cred, waitfor, p)) != 0)
816 allerror = error;
817 VOP_UNLOCK(vp, 0);
818 vrele(vp);
819 simple_lock(&mntvnode_slock);
820 }
821 simple_unlock(&mntvnode_slock);
822 /*
823 * Force stale file system control information to be flushed.
824 */
825 if ((error = VOP_FSYNC(ump->um_devvp, cred, waitfor, p)) != 0)
826 allerror = error;
827 /*
828 * Write back modified superblock.
829 */
830 if (fs->e2fs_fmod != 0) {
831 fs->e2fs_fmod = 0;
832 fs->e2fs.e2fs_wtime = time.tv_sec;
833 if ((error = ext2fs_cgupdate(ump, waitfor)))
834 allerror = error;
835 }
836 return (allerror);
837 }
838
839 /*
840 * Look up a EXT2FS dinode number to find its incore vnode, otherwise read it
841 * in from disk. If it is in core, wait for the lock bit to clear, then
842 * return the inode locked. Detection and handling of mount points must be
843 * done by the calling routine.
844 */
845 int
846 ext2fs_vget(mp, ino, vpp)
847 struct mount *mp;
848 ino_t ino;
849 struct vnode **vpp;
850 {
851 struct m_ext2fs *fs;
852 struct inode *ip;
853 struct ufsmount *ump;
854 struct buf *bp;
855 struct vnode *vp;
856 dev_t dev;
857 int error;
858
859 ump = VFSTOUFS(mp);
860 dev = ump->um_dev;
861 do {
862 if ((*vpp = ufs_ihashget(dev, ino)) != NULL)
863 return (0);
864 } while (lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
865
866 /* Allocate a new vnode/inode. */
867 if ((error = getnewvnode(VT_EXT2FS, mp, ext2fs_vnodeop_p, &vp)) != 0) {
868 *vpp = NULL;
869 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
870 return (error);
871 }
872 MALLOC(ip, struct inode *, sizeof(struct inode), M_EXT2FSNODE, M_WAITOK);
873 bzero((caddr_t)ip, sizeof(struct inode));
874 lockinit(&ip->i_lock, PINOD, "inode", 0, 0);
875 vp->v_data = ip;
876 ip->i_vnode = vp;
877 ip->i_e2fs = fs = ump->um_e2fs;
878 ip->i_dev = dev;
879 ip->i_number = ino;
880 ip->i_e2fs_last_lblk = 0;
881 ip->i_e2fs_last_blk = 0;
882
883 /*
884 * Put it onto its hash chain and lock it so that other requests for
885 * this inode will block if they arrive while we are sleeping waiting
886 * for old data structures to be purged or for the contents of the
887 * disk portion of this inode to be read.
888 */
889 ufs_ihashins(ip);
890 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
891
892 /* Read in the disk contents for the inode, copy into the inode. */
893 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
894 (int)fs->e2fs_bsize, NOCRED, &bp);
895 if (error) {
896 /*
897 * The inode does not contain anything useful, so it would
898 * be misleading to leave it on its hash chain. With mode
899 * still zero, it will be unlinked and returned to the free
900 * list by vput().
901 */
902 vput(vp);
903 brelse(bp);
904 *vpp = NULL;
905 return (error);
906 }
907 e2fs_iload((struct ext2fs_dinode*)bp->b_data + ino_to_fsbo(fs, ino),
908 &ip->i_din.e2fs_din);
909 brelse(bp);
910
911 /* If the inode was deleted, reset all fields */
912 if (ip->i_e2fs_dtime != 0) {
913 ip->i_e2fs_mode = ip->i_e2fs_size = ip->i_e2fs_nblock = 0;
914 bzero(ip->i_e2fs_blocks, sizeof(ip->i_e2fs_blocks));
915 }
916
917 /*
918 * Initialize the vnode from the inode, check for aliases.
919 * Note that the underlying vnode may have changed.
920 */
921 error = ext2fs_vinit(mp, ext2fs_specop_p, EXT2FS_FIFOOPS, &vp);
922 if (error) {
923 vput(vp);
924 *vpp = NULL;
925 return (error);
926 }
927 /*
928 * Finish inode initialization now that aliasing has been resolved.
929 */
930 ip->i_devvp = ump->um_devvp;
931 VREF(ip->i_devvp);
932 /*
933 * Set up a generation number for this inode if it does not
934 * already have one. This should only happen on old filesystems.
935 */
936 if (ip->i_e2fs_gen == 0) {
937 if (++ext2gennumber < (u_long)time.tv_sec)
938 ext2gennumber = time.tv_sec;
939 ip->i_e2fs_gen = ext2gennumber;
940 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
941 ip->i_flag |= IN_MODIFIED;
942 }
943
944 *vpp = vp;
945 return (0);
946 }
947
948 /*
949 * File handle to vnode
950 *
951 * Have to be really careful about stale file handles:
952 * - check that the inode number is valid
953 * - call ext2fs_vget() to get the locked inode
954 * - check for an unallocated inode (i_mode == 0)
955 * - check that the given client host has export rights and return
956 * those rights via. exflagsp and credanonp
957 */
958 int
959 ext2fs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
960 register struct mount *mp;
961 struct fid *fhp;
962 struct mbuf *nam;
963 struct vnode **vpp;
964 int *exflagsp;
965 struct ucred **credanonp;
966 {
967 register struct ufid *ufhp;
968 struct m_ext2fs *fs;
969
970 ufhp = (struct ufid *)fhp;
971 fs = VFSTOUFS(mp)->um_e2fs;
972 if ((ufhp->ufid_ino < EXT2_FIRSTINO && ufhp->ufid_ino != EXT2_ROOTINO) ||
973 ufhp->ufid_ino >= fs->e2fs_ncg * fs->e2fs.e2fs_ipg)
974 return (ESTALE);
975 return (ext2fs_check_export(mp, ufhp, nam, vpp, exflagsp, credanonp));
976 }
977
978 /*
979 * Vnode pointer to File handle
980 */
981 /* ARGSUSED */
982 int
983 ext2fs_vptofh(vp, fhp)
984 struct vnode *vp;
985 struct fid *fhp;
986 {
987 register struct inode *ip;
988 register struct ufid *ufhp;
989
990 ip = VTOI(vp);
991 ufhp = (struct ufid *)fhp;
992 ufhp->ufid_len = sizeof(struct ufid);
993 ufhp->ufid_ino = ip->i_number;
994 ufhp->ufid_gen = ip->i_e2fs_gen;
995 return (0);
996 }
997
998 int
999 ext2fs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
1000 int *name;
1001 u_int namelen;
1002 void *oldp;
1003 size_t *oldlenp;
1004 void *newp;
1005 size_t newlen;
1006 struct proc *p;
1007 {
1008 return (EOPNOTSUPP);
1009 }
1010
1011 /*
1012 * Write a superblock and associated information back to disk.
1013 */
1014 int
1015 ext2fs_sbupdate(mp, waitfor)
1016 struct ufsmount *mp;
1017 int waitfor;
1018 {
1019 register struct m_ext2fs *fs = mp->um_e2fs;
1020 register struct buf *bp;
1021 int error = 0;
1022
1023 bp = getblk(mp->um_devvp, SBLOCK, SBSIZE, 0, 0);
1024 e2fs_sbsave(&fs->e2fs, (struct ext2fs*)bp->b_data);
1025 if (waitfor == MNT_WAIT)
1026 error = bwrite(bp);
1027 else
1028 bawrite(bp);
1029 return (error);
1030 }
1031
1032 int
1033 ext2fs_cgupdate(mp, waitfor)
1034 struct ufsmount *mp;
1035 int waitfor;
1036 {
1037 register struct m_ext2fs *fs = mp->um_e2fs;
1038 register struct buf *bp;
1039 int i, error = 0, allerror = 0;
1040
1041 allerror = ext2fs_sbupdate(mp, waitfor);
1042 for (i = 0; i < fs->e2fs_ngdb; i++) {
1043 bp = getblk(mp->um_devvp, fsbtodb(fs, ((fs->e2fs_bsize>1024)?0:1)+i+1),
1044 fs->e2fs_bsize, 0, 0);
1045 e2fs_cgsave(&fs->e2fs_gd[i* fs->e2fs_bsize / sizeof(struct ext2_gd)],
1046 (struct ext2_gd*)bp->b_data, fs->e2fs_bsize);
1047 if (waitfor == MNT_WAIT)
1048 error = bwrite(bp);
1049 else
1050 bawrite(bp);
1051 }
1052
1053 if (!allerror && error)
1054 allerror = error;
1055 return (allerror);
1056 }
1057