ext2fs_vfsops.c revision 1.16 1 /* $NetBSD: ext2fs_vfsops.c,v 1.16 1998/08/09 20:15:39 perry 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/lock.h>
61
62 #include <miscfs/specfs/specdev.h>
63
64 #include <ufs/ufs/quota.h>
65 #include <ufs/ufs/ufsmount.h>
66 #include <ufs/ufs/inode.h>
67 #include <ufs/ufs/dir.h>
68 #include <ufs/ufs/ufs_extern.h>
69
70 #include <ufs/ext2fs/ext2fs.h>
71 #include <ufs/ext2fs/ext2fs_extern.h>
72
73 extern struct lock ufs_hashlock;
74
75 int ext2fs_sbupdate __P((struct ufsmount *, int));
76 int ext2fs_check_export __P((struct mount *, struct ufid *, struct mbuf *,
77 struct vnode **, int *, struct ucred **));
78
79 extern struct vnodeopv_desc ext2fs_vnodeop_opv_desc;
80 extern struct vnodeopv_desc ext2fs_specop_opv_desc;
81 extern struct vnodeopv_desc ext2fs_fifoop_opv_desc;
82
83 struct vnodeopv_desc *ext2fs_vnodeopv_descs[] = {
84 &ext2fs_vnodeop_opv_desc,
85 &ext2fs_specop_opv_desc,
86 &ext2fs_fifoop_opv_desc,
87 NULL,
88 };
89
90 struct vfsops ext2fs_vfsops = {
91 MOUNT_EXT2FS,
92 ext2fs_mount,
93 ufs_start,
94 ext2fs_unmount,
95 ufs_root,
96 ufs_quotactl,
97 ext2fs_statfs,
98 ext2fs_sync,
99 ext2fs_vget,
100 ext2fs_fhtovp,
101 ext2fs_vptofh,
102 ext2fs_init,
103 ext2fs_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 memset(fs->e2fs_fsmnt, 0, 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 memset(fs->e2fs_fsmnt + size, 0, sizeof(fs->e2fs_fsmnt) - size);
346 memcpy(mp->mnt_stat.f_mntonname, fs->e2fs_fsmnt, MNAMELEN);
347 (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
348 &size);
349 memset(mp->mnt_stat.f_mntfromname + size, 0, 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 memset((caddr_t)ump, 0, 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 memcpy(sbp->f_mntonname, mp->mnt_stat.f_mntonname, MNAMELEN);
757 memcpy(sbp->f_mntfromname, mp->mnt_stat.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,
818 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, p)) != 0)
819 allerror = error;
820 VOP_UNLOCK(vp, 0);
821 vrele(vp);
822 simple_lock(&mntvnode_slock);
823 }
824 simple_unlock(&mntvnode_slock);
825 /*
826 * Force stale file system control information to be flushed.
827 */
828 if ((error = VOP_FSYNC(ump->um_devvp, cred,
829 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, p)) != 0)
830 allerror = error;
831 /*
832 * Write back modified superblock.
833 */
834 if (fs->e2fs_fmod != 0) {
835 fs->e2fs_fmod = 0;
836 fs->e2fs.e2fs_wtime = time.tv_sec;
837 if ((error = ext2fs_cgupdate(ump, waitfor)))
838 allerror = error;
839 }
840 return (allerror);
841 }
842
843 /*
844 * Look up a EXT2FS dinode number to find its incore vnode, otherwise read it
845 * in from disk. If it is in core, wait for the lock bit to clear, then
846 * return the inode locked. Detection and handling of mount points must be
847 * done by the calling routine.
848 */
849 int
850 ext2fs_vget(mp, ino, vpp)
851 struct mount *mp;
852 ino_t ino;
853 struct vnode **vpp;
854 {
855 struct m_ext2fs *fs;
856 struct inode *ip;
857 struct ufsmount *ump;
858 struct buf *bp;
859 struct vnode *vp;
860 dev_t dev;
861 int error;
862
863 ump = VFSTOUFS(mp);
864 dev = ump->um_dev;
865 do {
866 if ((*vpp = ufs_ihashget(dev, ino)) != NULL)
867 return (0);
868 } while (lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
869
870 /* Allocate a new vnode/inode. */
871 if ((error = getnewvnode(VT_EXT2FS, mp, ext2fs_vnodeop_p, &vp)) != 0) {
872 *vpp = NULL;
873 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
874 return (error);
875 }
876 MALLOC(ip, struct inode *, sizeof(struct inode), M_EXT2FSNODE, M_WAITOK);
877 memset((caddr_t)ip, 0, sizeof(struct inode));
878 lockinit(&ip->i_lock, PINOD, "inode", 0, 0);
879 vp->v_data = ip;
880 ip->i_vnode = vp;
881 ip->i_e2fs = fs = ump->um_e2fs;
882 ip->i_dev = dev;
883 ip->i_number = ino;
884 ip->i_e2fs_last_lblk = 0;
885 ip->i_e2fs_last_blk = 0;
886
887 /*
888 * Put it onto its hash chain and lock it so that other requests for
889 * this inode will block if they arrive while we are sleeping waiting
890 * for old data structures to be purged or for the contents of the
891 * disk portion of this inode to be read.
892 */
893 ufs_ihashins(ip);
894 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
895
896 /* Read in the disk contents for the inode, copy into the inode. */
897 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
898 (int)fs->e2fs_bsize, NOCRED, &bp);
899 if (error) {
900 /*
901 * The inode does not contain anything useful, so it would
902 * be misleading to leave it on its hash chain. With mode
903 * still zero, it will be unlinked and returned to the free
904 * list by vput().
905 */
906 vput(vp);
907 brelse(bp);
908 *vpp = NULL;
909 return (error);
910 }
911 e2fs_iload((struct ext2fs_dinode*)bp->b_data + ino_to_fsbo(fs, ino),
912 &ip->i_din.e2fs_din);
913 brelse(bp);
914
915 /* If the inode was deleted, reset all fields */
916 if (ip->i_e2fs_dtime != 0) {
917 ip->i_e2fs_mode = ip->i_e2fs_size = ip->i_e2fs_nblock = 0;
918 memset(ip->i_e2fs_blocks, 0, sizeof(ip->i_e2fs_blocks));
919 }
920
921 /*
922 * Initialize the vnode from the inode, check for aliases.
923 * Note that the underlying vnode may have changed.
924 */
925 error = ext2fs_vinit(mp, ext2fs_specop_p, ext2fs_fifoop_p, &vp);
926 if (error) {
927 vput(vp);
928 *vpp = NULL;
929 return (error);
930 }
931 /*
932 * Finish inode initialization now that aliasing has been resolved.
933 */
934 ip->i_devvp = ump->um_devvp;
935 VREF(ip->i_devvp);
936 /*
937 * Set up a generation number for this inode if it does not
938 * already have one. This should only happen on old filesystems.
939 */
940 if (ip->i_e2fs_gen == 0) {
941 if (++ext2gennumber < (u_long)time.tv_sec)
942 ext2gennumber = time.tv_sec;
943 ip->i_e2fs_gen = ext2gennumber;
944 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
945 ip->i_flag |= IN_MODIFIED;
946 }
947
948 *vpp = vp;
949 return (0);
950 }
951
952 /*
953 * File handle to vnode
954 *
955 * Have to be really careful about stale file handles:
956 * - check that the inode number is valid
957 * - call ext2fs_vget() to get the locked inode
958 * - check for an unallocated inode (i_mode == 0)
959 * - check that the given client host has export rights and return
960 * those rights via. exflagsp and credanonp
961 */
962 int
963 ext2fs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
964 register struct mount *mp;
965 struct fid *fhp;
966 struct mbuf *nam;
967 struct vnode **vpp;
968 int *exflagsp;
969 struct ucred **credanonp;
970 {
971 register struct ufid *ufhp;
972 struct m_ext2fs *fs;
973
974 ufhp = (struct ufid *)fhp;
975 fs = VFSTOUFS(mp)->um_e2fs;
976 if ((ufhp->ufid_ino < EXT2_FIRSTINO && ufhp->ufid_ino != EXT2_ROOTINO) ||
977 ufhp->ufid_ino >= fs->e2fs_ncg * fs->e2fs.e2fs_ipg)
978 return (ESTALE);
979 return (ext2fs_check_export(mp, ufhp, nam, vpp, exflagsp, credanonp));
980 }
981
982 /*
983 * Vnode pointer to File handle
984 */
985 /* ARGSUSED */
986 int
987 ext2fs_vptofh(vp, fhp)
988 struct vnode *vp;
989 struct fid *fhp;
990 {
991 register struct inode *ip;
992 register struct ufid *ufhp;
993
994 ip = VTOI(vp);
995 ufhp = (struct ufid *)fhp;
996 ufhp->ufid_len = sizeof(struct ufid);
997 ufhp->ufid_ino = ip->i_number;
998 ufhp->ufid_gen = ip->i_e2fs_gen;
999 return (0);
1000 }
1001
1002 int
1003 ext2fs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
1004 int *name;
1005 u_int namelen;
1006 void *oldp;
1007 size_t *oldlenp;
1008 void *newp;
1009 size_t newlen;
1010 struct proc *p;
1011 {
1012 return (EOPNOTSUPP);
1013 }
1014
1015 /*
1016 * Write a superblock and associated information back to disk.
1017 */
1018 int
1019 ext2fs_sbupdate(mp, waitfor)
1020 struct ufsmount *mp;
1021 int waitfor;
1022 {
1023 register struct m_ext2fs *fs = mp->um_e2fs;
1024 register struct buf *bp;
1025 int error = 0;
1026
1027 bp = getblk(mp->um_devvp, SBLOCK, SBSIZE, 0, 0);
1028 e2fs_sbsave(&fs->e2fs, (struct ext2fs*)bp->b_data);
1029 if (waitfor == MNT_WAIT)
1030 error = bwrite(bp);
1031 else
1032 bawrite(bp);
1033 return (error);
1034 }
1035
1036 int
1037 ext2fs_cgupdate(mp, waitfor)
1038 struct ufsmount *mp;
1039 int waitfor;
1040 {
1041 register struct m_ext2fs *fs = mp->um_e2fs;
1042 register struct buf *bp;
1043 int i, error = 0, allerror = 0;
1044
1045 allerror = ext2fs_sbupdate(mp, waitfor);
1046 for (i = 0; i < fs->e2fs_ngdb; i++) {
1047 bp = getblk(mp->um_devvp, fsbtodb(fs, ((fs->e2fs_bsize>1024)?0:1)+i+1),
1048 fs->e2fs_bsize, 0, 0);
1049 e2fs_cgsave(&fs->e2fs_gd[i* fs->e2fs_bsize / sizeof(struct ext2_gd)],
1050 (struct ext2_gd*)bp->b_data, fs->e2fs_bsize);
1051 if (waitfor == MNT_WAIT)
1052 error = bwrite(bp);
1053 else
1054 bawrite(bp);
1055 }
1056
1057 if (!allerror && error)
1058 allerror = error;
1059 return (allerror);
1060 }
1061