ffs_vfsops.c revision 1.18 1 /* $NetBSD: ffs_vfsops.c,v 1.18 1995/12/19 23:27:53 cgd Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)ffs_vfsops.c 8.14 (Berkeley) 11/28/94
36 */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/namei.h>
41 #include <sys/proc.h>
42 #include <sys/kernel.h>
43 #include <sys/vnode.h>
44 #include <sys/socket.h>
45 #include <sys/mount.h>
46 #include <sys/buf.h>
47 #include <sys/mbuf.h>
48 #include <sys/file.h>
49 #include <sys/disklabel.h>
50 #include <sys/ioctl.h>
51 #include <sys/errno.h>
52 #include <sys/malloc.h>
53
54 #include <miscfs/specfs/specdev.h>
55
56 #include <ufs/ufs/quota.h>
57 #include <ufs/ufs/ufsmount.h>
58 #include <ufs/ufs/inode.h>
59 #include <ufs/ufs/ufs_extern.h>
60
61 #include <ufs/ffs/fs.h>
62 #include <ufs/ffs/ffs_extern.h>
63
64 int ffs_sbupdate __P((struct ufsmount *, int));
65
66 struct vfsops ffs_vfsops = {
67 MOUNT_FFS,
68 ffs_mount,
69 ufs_start,
70 ffs_unmount,
71 ufs_root,
72 ufs_quotactl,
73 ffs_statfs,
74 ffs_sync,
75 ffs_vget,
76 ffs_fhtovp,
77 ffs_vptofh,
78 ffs_init,
79 };
80
81 extern u_long nextgennumber;
82
83 /*
84 * Called by main() when ufs is going to be mounted as root.
85 *
86 * Name is updated by mount(8) after booting.
87 */
88 #define ROOTNAME "root_device"
89
90 ffs_mountroot()
91 {
92 extern struct vnode *rootvp;
93 register struct fs *fs;
94 register struct mount *mp;
95 struct proc *p = curproc; /* XXX */
96 struct ufsmount *ump;
97 size_t size;
98 int error;
99
100 /*
101 * Get vnodes for swapdev and rootdev.
102 */
103 if (bdevvp(swapdev, &swapdev_vp) || bdevvp(rootdev, &rootvp))
104 panic("ffs_mountroot: can't setup bdevvp's");
105
106 mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK);
107 bzero((char *)mp, (u_long)sizeof(struct mount));
108 mp->mnt_op = &ffs_vfsops;
109 mp->mnt_flag = MNT_RDONLY;
110 if (error = ffs_mountfs(rootvp, mp, p)) {
111 free(mp, M_MOUNT);
112 return (error);
113 }
114 if (error = vfs_lock(mp)) {
115 (void)ffs_unmount(mp, 0, p);
116 free(mp, M_MOUNT);
117 return (error);
118 }
119 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
120 mp->mnt_vnodecovered = NULLVP;
121 ump = VFSTOUFS(mp);
122 fs = ump->um_fs;
123 bzero(fs->fs_fsmnt, sizeof(fs->fs_fsmnt));
124 fs->fs_fsmnt[0] = '/';
125 bcopy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, MNAMELEN);
126 (void) copystr(ROOTNAME, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
127 &size);
128 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
129 (void)ffs_statfs(mp, &mp->mnt_stat, p);
130 vfs_unlock(mp);
131 inittodr(fs->fs_time);
132 return (0);
133 }
134
135 /*
136 * VFS Operations.
137 *
138 * mount system call
139 */
140 int
141 ffs_mount(mp, path, data, ndp, p)
142 register struct mount *mp;
143 char *path;
144 caddr_t data;
145 struct nameidata *ndp;
146 struct proc *p;
147 {
148 struct vnode *devvp;
149 struct ufs_args args;
150 struct ufsmount *ump;
151 register struct fs *fs;
152 size_t size;
153 int error, flags;
154 mode_t accessmode;
155
156 if (error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args)))
157 return (error);
158 /*
159 * If updating, check whether changing from read-only to
160 * read/write; if there is no device name, that's all we do.
161 */
162 if (mp->mnt_flag & MNT_UPDATE) {
163 ump = VFSTOUFS(mp);
164 fs = ump->um_fs;
165 if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
166 flags = WRITECLOSE;
167 if (mp->mnt_flag & MNT_FORCE)
168 flags |= FORCECLOSE;
169 if (vfs_busy(mp))
170 return (EBUSY);
171 error = ffs_flushfiles(mp, flags, p);
172 if (error == 0 &&
173 ffs_cgupdate(ump, MNT_WAIT) == 0 &&
174 fs->fs_clean & FS_WASCLEAN) {
175 fs->fs_clean = FS_ISCLEAN;
176 (void) ffs_sbupdate(ump, MNT_WAIT);
177 }
178 vfs_unbusy(mp);
179 if (error)
180 return (error);
181 fs->fs_ronly = 1;
182 }
183 if (mp->mnt_flag & MNT_RELOAD) {
184 error = ffs_reload(mp, ndp->ni_cnd.cn_cred, p);
185 if (error)
186 return (error);
187 }
188 if (fs->fs_ronly && (mp->mnt_flag & MNT_WANTRDWR)) {
189 /*
190 * If upgrade to read-write by non-root, then verify
191 * that user has necessary permissions on the device.
192 */
193 if (p->p_ucred->cr_uid != 0) {
194 devvp = ump->um_devvp;
195 VOP_LOCK(devvp);
196 if (error = VOP_ACCESS(devvp, VREAD | VWRITE,
197 p->p_ucred, p)) {
198 VOP_UNLOCK(devvp);
199 return (error);
200 }
201 VOP_UNLOCK(devvp);
202 }
203 fs->fs_ronly = 0;
204 fs->fs_clean <<= 1;
205 fs->fs_fmod = 1;
206 }
207 if (args.fspec == 0) {
208 /*
209 * Process export requests.
210 */
211 return (vfs_export(mp, &ump->um_export, &args.export));
212 }
213 }
214 /*
215 * Not an update, or updating the name: look up the name
216 * and verify that it refers to a sensible block device.
217 */
218 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
219 if (error = namei(ndp))
220 return (error);
221 devvp = ndp->ni_vp;
222
223 if (devvp->v_type != VBLK) {
224 vrele(devvp);
225 return (ENOTBLK);
226 }
227 if (major(devvp->v_rdev) >= nblkdev) {
228 vrele(devvp);
229 return (ENXIO);
230 }
231 /*
232 * If mount by non-root, then verify that user has necessary
233 * permissions on the device.
234 */
235 if (p->p_ucred->cr_uid != 0) {
236 accessmode = VREAD;
237 if ((mp->mnt_flag & MNT_RDONLY) == 0)
238 accessmode |= VWRITE;
239 VOP_LOCK(devvp);
240 if (error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p)) {
241 vput(devvp);
242 return (error);
243 }
244 VOP_UNLOCK(devvp);
245 }
246 if ((mp->mnt_flag & MNT_UPDATE) == 0)
247 error = ffs_mountfs(devvp, mp, p);
248 else {
249 if (devvp != ump->um_devvp)
250 error = EINVAL; /* needs translation */
251 else
252 vrele(devvp);
253 }
254 if (error) {
255 vrele(devvp);
256 return (error);
257 }
258 ump = VFSTOUFS(mp);
259 fs = ump->um_fs;
260 (void) copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size);
261 bzero(fs->fs_fsmnt + size, sizeof(fs->fs_fsmnt) - size);
262 bcopy(fs->fs_fsmnt, mp->mnt_stat.f_mntonname, MNAMELEN);
263 (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
264 &size);
265 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
266 if (fs->fs_fmod != 0) { /* XXX */
267 fs->fs_fmod = 0;
268 if (fs->fs_clean & FS_WASCLEAN)
269 fs->fs_time = time.tv_sec;
270 else
271 printf("%s: file system not clean; please fsck(8)\n",
272 mp->mnt_stat.f_mntfromname);
273 (void) ffs_cgupdate(ump, MNT_WAIT);
274 }
275 return (0);
276 }
277
278 /*
279 * Reload all incore data for a filesystem (used after running fsck on
280 * the root filesystem and finding things to fix). The filesystem must
281 * be mounted read-only.
282 *
283 * Things to do to update the mount:
284 * 1) invalidate all cached meta-data.
285 * 2) re-read superblock from disk.
286 * 3) re-read summary information from disk.
287 * 4) invalidate all inactive vnodes.
288 * 5) invalidate all cached file data.
289 * 6) re-read inode data for all active vnodes.
290 */
291 ffs_reload(mountp, cred, p)
292 register struct mount *mountp;
293 struct ucred *cred;
294 struct proc *p;
295 {
296 register struct vnode *vp, *nvp, *devvp;
297 struct inode *ip;
298 struct csum *space;
299 struct buf *bp;
300 struct fs *fs, *newfs;
301 struct partinfo dpart;
302 int i, blks, size, error;
303 int32_t *lp;
304
305 if ((mountp->mnt_flag & MNT_RDONLY) == 0)
306 return (EINVAL);
307 /*
308 * Step 1: invalidate all cached meta-data.
309 */
310 devvp = VFSTOUFS(mountp)->um_devvp;
311 if (vinvalbuf(devvp, 0, cred, p, 0, 0))
312 panic("ffs_reload: dirty1");
313 /*
314 * Step 2: re-read superblock from disk.
315 */
316 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED, p) != 0)
317 size = DEV_BSIZE;
318 else
319 size = dpart.disklab->d_secsize;
320 if (error = bread(devvp, (daddr_t)(SBOFF / size), SBSIZE, NOCRED, &bp))
321 return (error);
322 newfs = (struct fs *)bp->b_data;
323 if (newfs->fs_magic != FS_MAGIC || newfs->fs_bsize > MAXBSIZE ||
324 newfs->fs_bsize < sizeof(struct fs)) {
325 brelse(bp);
326 return (EIO); /* XXX needs translation */
327 }
328 fs = VFSTOUFS(mountp)->um_fs;
329 /*
330 * Copy pointer fields back into superblock before copying in XXX
331 * new superblock. These should really be in the ufsmount. XXX
332 * Note that important parameters (eg fs_ncg) are unchanged.
333 */
334 bcopy(&fs->fs_csp[0], &newfs->fs_csp[0], sizeof(fs->fs_csp));
335 newfs->fs_maxcluster = fs->fs_maxcluster;
336 bcopy(newfs, fs, (u_int)fs->fs_sbsize);
337 if (fs->fs_sbsize < SBSIZE)
338 bp->b_flags |= B_INVAL;
339 brelse(bp);
340 mountp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
341 ffs_oldfscompat(fs);
342 /*
343 * Step 3: re-read summary information from disk.
344 */
345 blks = howmany(fs->fs_cssize, fs->fs_fsize);
346 space = fs->fs_csp[0];
347 for (i = 0; i < blks; i += fs->fs_frag) {
348 size = fs->fs_bsize;
349 if (i + fs->fs_frag > blks)
350 size = (blks - i) * fs->fs_fsize;
351 if (error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
352 NOCRED, &bp))
353 return (error);
354 bcopy(bp->b_data, fs->fs_csp[fragstoblks(fs, i)], (u_int)size);
355 brelse(bp);
356 }
357 /*
358 * We no longer know anything about clusters per cylinder group.
359 */
360 if (fs->fs_contigsumsize > 0) {
361 lp = fs->fs_maxcluster;
362 for (i = 0; i < fs->fs_ncg; i++)
363 *lp++ = fs->fs_contigsumsize;
364 }
365
366 loop:
367 for (vp = mountp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
368 nvp = vp->v_mntvnodes.le_next;
369 /*
370 * Step 4: invalidate all inactive vnodes.
371 */
372 if (vp->v_usecount == 0) {
373 vgone(vp);
374 continue;
375 }
376 /*
377 * Step 5: invalidate all cached file data.
378 */
379 if (vget(vp, 1))
380 goto loop;
381 if (vinvalbuf(vp, 0, cred, p, 0, 0))
382 panic("ffs_reload: dirty2");
383 /*
384 * Step 6: re-read inode data for all active vnodes.
385 */
386 ip = VTOI(vp);
387 if (error =
388 bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
389 (int)fs->fs_bsize, NOCRED, &bp)) {
390 vput(vp);
391 return (error);
392 }
393 ip->i_din = *((struct dinode *)bp->b_data +
394 ino_to_fsbo(fs, ip->i_number));
395 brelse(bp);
396 vput(vp);
397 if (vp->v_mount != mountp)
398 goto loop;
399 }
400 return (0);
401 }
402
403 /*
404 * Common code for mount and mountroot
405 */
406 int
407 ffs_mountfs(devvp, mp, p)
408 register struct vnode *devvp;
409 struct mount *mp;
410 struct proc *p;
411 {
412 register struct ufsmount *ump;
413 struct buf *bp;
414 register struct fs *fs;
415 dev_t dev;
416 struct partinfo dpart;
417 caddr_t base, space;
418 int blks;
419 int error, i, size, ronly;
420 int32_t *lp;
421 struct ucred *cred;
422 extern struct vnode *rootvp;
423 u_int64_t maxfilesize; /* XXX */
424
425 dev = devvp->v_rdev;
426 cred = p ? p->p_ucred : NOCRED;
427 /*
428 * Disallow multiple mounts of the same device.
429 * Disallow mounting of a device that is currently in use
430 * (except for root, which might share swap device for miniroot).
431 * Flush out any old buffers remaining from a previous use.
432 */
433 if (error = vfs_mountedon(devvp))
434 return (error);
435 if (vcount(devvp) > 1 && devvp != rootvp)
436 return (EBUSY);
437 if (error = vinvalbuf(devvp, V_SAVE, cred, p, 0, 0))
438 return (error);
439
440 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
441 if (error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p))
442 return (error);
443 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, cred, p) != 0)
444 size = DEV_BSIZE;
445 else
446 size = dpart.disklab->d_secsize;
447
448 bp = NULL;
449 ump = NULL;
450 if (error = bread(devvp, (daddr_t)(SBOFF / size), SBSIZE, cred, &bp))
451 goto out;
452 fs = (struct fs *)bp->b_data;
453 if (fs->fs_magic != FS_MAGIC || fs->fs_bsize > MAXBSIZE ||
454 fs->fs_bsize < sizeof(struct fs)) {
455 error = EINVAL; /* XXX needs translation */
456 goto out;
457 }
458 /* XXX updating 4.2 FFS superblocks trashes rotational layout tables */
459 if (fs->fs_postblformat == FS_42POSTBLFMT && !ronly) {
460 error = EROFS; /* XXX what should be returned? */
461 goto out;
462 }
463 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
464 bzero((caddr_t)ump, sizeof *ump);
465 ump->um_fs = malloc((u_long)fs->fs_sbsize, M_UFSMNT,
466 M_WAITOK);
467 bcopy(bp->b_data, ump->um_fs, (u_int)fs->fs_sbsize);
468 if (fs->fs_sbsize < SBSIZE)
469 bp->b_flags |= B_INVAL;
470 brelse(bp);
471 bp = NULL;
472 fs = ump->um_fs;
473 fs->fs_ronly = ronly;
474 if (ronly == 0) {
475 fs->fs_clean <<= 1;
476 fs->fs_fmod = 1;
477 }
478 size = fs->fs_cssize;
479 blks = howmany(size, fs->fs_fsize);
480 if (fs->fs_contigsumsize > 0)
481 size += fs->fs_ncg * sizeof(int32_t);
482 base = space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
483 for (i = 0; i < blks; i += fs->fs_frag) {
484 size = fs->fs_bsize;
485 if (i + fs->fs_frag > blks)
486 size = (blks - i) * fs->fs_fsize;
487 if (error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
488 cred, &bp)) {
489 free(base, M_UFSMNT);
490 goto out;
491 }
492 bcopy(bp->b_data, space, (u_int)size);
493 fs->fs_csp[fragstoblks(fs, i)] = (struct csum *)space;
494 space += size;
495 brelse(bp);
496 bp = NULL;
497 }
498 if (fs->fs_contigsumsize > 0) {
499 fs->fs_maxcluster = lp = (int32_t *)space;
500 for (i = 0; i < fs->fs_ncg; i++)
501 *lp++ = fs->fs_contigsumsize;
502 }
503 mp->mnt_data = (qaddr_t)ump;
504 mp->mnt_stat.f_fsid.val[0] = (long)dev;
505 mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_FFS);
506 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
507 mp->mnt_flag |= MNT_LOCAL;
508 ump->um_mountp = mp;
509 ump->um_dev = dev;
510 ump->um_devvp = devvp;
511 ump->um_nindir = fs->fs_nindir;
512 ump->um_bptrtodb = fs->fs_fsbtodb;
513 ump->um_seqinc = fs->fs_frag;
514 for (i = 0; i < MAXQUOTAS; i++)
515 ump->um_quotas[i] = NULLVP;
516 devvp->v_specflags |= SI_MOUNTEDON;
517 ffs_oldfscompat(fs);
518 ump->um_savedmaxfilesize = fs->fs_maxfilesize; /* XXX */
519 maxfilesize = (u_int64_t)0x80000000 * fs->fs_bsize - 1; /* XXX */
520 if (fs->fs_maxfilesize > maxfilesize) /* XXX */
521 fs->fs_maxfilesize = maxfilesize; /* XXX */
522 return (0);
523 out:
524 if (bp)
525 brelse(bp);
526 (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p);
527 if (ump) {
528 free(ump->um_fs, M_UFSMNT);
529 free(ump, M_UFSMNT);
530 mp->mnt_data = (qaddr_t)0;
531 }
532 return (error);
533 }
534
535 /*
536 * Sanity checks for old file systems.
537 *
538 * XXX - goes away some day.
539 */
540 ffs_oldfscompat(fs)
541 struct fs *fs;
542 {
543 int i;
544
545 fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect); /* XXX */
546 fs->fs_interleave = max(fs->fs_interleave, 1); /* XXX */
547 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
548 fs->fs_nrpos = 8; /* XXX */
549 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
550 u_int64_t sizepb = fs->fs_bsize; /* XXX */
551 /* XXX */
552 fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1; /* XXX */
553 for (i = 0; i < NIADDR; i++) { /* XXX */
554 sizepb *= NINDIR(fs); /* XXX */
555 fs->fs_maxfilesize += sizepb; /* XXX */
556 } /* XXX */
557 fs->fs_qbmask = ~fs->fs_bmask; /* XXX */
558 fs->fs_qfmask = ~fs->fs_fmask; /* XXX */
559 } /* XXX */
560 return (0);
561 }
562
563 /*
564 * unmount system call
565 */
566 int
567 ffs_unmount(mp, mntflags, p)
568 struct mount *mp;
569 int mntflags;
570 struct proc *p;
571 {
572 register struct ufsmount *ump;
573 register struct fs *fs;
574 int error, flags;
575
576 flags = 0;
577 if (mntflags & MNT_FORCE)
578 flags |= FORCECLOSE;
579 if (error = ffs_flushfiles(mp, flags, p))
580 return (error);
581 ump = VFSTOUFS(mp);
582 fs = ump->um_fs;
583 if (fs->fs_ronly == 0 &&
584 ffs_cgupdate(ump, MNT_WAIT) == 0 &&
585 fs->fs_clean & FS_WASCLEAN) {
586 fs->fs_clean = FS_ISCLEAN;
587 (void) ffs_sbupdate(ump, MNT_WAIT);
588 }
589 ump->um_devvp->v_specflags &= ~SI_MOUNTEDON;
590 error = VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD|FWRITE,
591 NOCRED, p);
592 vrele(ump->um_devvp);
593 free(fs->fs_csp[0], M_UFSMNT);
594 free(fs, M_UFSMNT);
595 free(ump, M_UFSMNT);
596 mp->mnt_data = (qaddr_t)0;
597 mp->mnt_flag &= ~MNT_LOCAL;
598 return (error);
599 }
600
601 /*
602 * Flush out all the files in a filesystem.
603 */
604 ffs_flushfiles(mp, flags, p)
605 register struct mount *mp;
606 int flags;
607 struct proc *p;
608 {
609 extern int doforce;
610 register struct ufsmount *ump;
611 int i, error;
612
613 if (!doforce)
614 flags &= ~FORCECLOSE;
615 ump = VFSTOUFS(mp);
616 #ifdef QUOTA
617 if (mp->mnt_flag & MNT_QUOTA) {
618 if (error = vflush(mp, NULLVP, SKIPSYSTEM|flags))
619 return (error);
620 for (i = 0; i < MAXQUOTAS; i++) {
621 if (ump->um_quotas[i] == NULLVP)
622 continue;
623 quotaoff(p, mp, i);
624 }
625 /*
626 * Here we fall through to vflush again to ensure
627 * that we have gotten rid of all the system vnodes.
628 */
629 }
630 #endif
631 error = vflush(mp, NULLVP, flags);
632 return (error);
633 }
634
635 /*
636 * Get file system statistics.
637 */
638 int
639 ffs_statfs(mp, sbp, p)
640 struct mount *mp;
641 register struct statfs *sbp;
642 struct proc *p;
643 {
644 register struct ufsmount *ump;
645 register struct fs *fs;
646
647 ump = VFSTOUFS(mp);
648 fs = ump->um_fs;
649 if (fs->fs_magic != FS_MAGIC)
650 panic("ffs_statfs");
651 #ifdef COMPAT_09
652 sbp->f_type = 1;
653 #else
654 sbp->f_type = 0;
655 #endif
656 sbp->f_bsize = fs->fs_fsize;
657 sbp->f_iosize = fs->fs_bsize;
658 sbp->f_blocks = fs->fs_dsize;
659 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
660 fs->fs_cstotal.cs_nffree;
661 sbp->f_bavail = (fs->fs_dsize * (100 - fs->fs_minfree) / 100) -
662 (fs->fs_dsize - sbp->f_bfree);
663 sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO;
664 sbp->f_ffree = fs->fs_cstotal.cs_nifree;
665 if (sbp != &mp->mnt_stat) {
666 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
667 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
668 }
669 strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN);
670 return (0);
671 }
672
673 /*
674 * Go through the disk queues to initiate sandbagged IO;
675 * go through the inodes to write those that have been modified;
676 * initiate the writing of the super block if it has been modified.
677 *
678 * Note: we are always called with the filesystem marked `MPBUSY'.
679 */
680 int
681 ffs_sync(mp, waitfor, cred, p)
682 struct mount *mp;
683 int waitfor;
684 struct ucred *cred;
685 struct proc *p;
686 {
687 register struct vnode *vp;
688 register struct inode *ip;
689 register struct ufsmount *ump = VFSTOUFS(mp);
690 register struct fs *fs;
691 int error, allerror = 0;
692
693 fs = ump->um_fs;
694 /*
695 * Write back modified superblock.
696 * Consistency check that the superblock
697 * is still in the buffer cache.
698 */
699 if (fs->fs_fmod != 0) {
700 if (fs->fs_ronly != 0) { /* XXX */
701 printf("fs = %s\n", fs->fs_fsmnt);
702 panic("update: rofs mod");
703 }
704 fs->fs_fmod = 0;
705 fs->fs_time = time.tv_sec;
706 allerror = ffs_cgupdate(ump, waitfor);
707 }
708 /*
709 * Write back each (modified) inode.
710 */
711 loop:
712 for (vp = mp->mnt_vnodelist.lh_first;
713 vp != NULL;
714 vp = vp->v_mntvnodes.le_next) {
715 /*
716 * If the vnode that we are about to sync is no longer
717 * associated with this mount point, start over.
718 */
719 if (vp->v_mount != mp)
720 goto loop;
721 if (VOP_ISLOCKED(vp))
722 continue;
723 ip = VTOI(vp);
724 if ((ip->i_flag &
725 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
726 vp->v_dirtyblkhd.lh_first == NULL)
727 continue;
728 if (vget(vp, 1))
729 goto loop;
730 if (error = VOP_FSYNC(vp, cred, waitfor, p))
731 allerror = error;
732 vput(vp);
733 }
734 /*
735 * Force stale file system control information to be flushed.
736 */
737 if (error = VOP_FSYNC(ump->um_devvp, cred, waitfor, p))
738 allerror = error;
739 #ifdef QUOTA
740 qsync(mp);
741 #endif
742 return (allerror);
743 }
744
745 /*
746 * Look up a FFS dinode number to find its incore vnode, otherwise read it
747 * in from disk. If it is in core, wait for the lock bit to clear, then
748 * return the inode locked. Detection and handling of mount points must be
749 * done by the calling routine.
750 */
751 int
752 ffs_vget(mp, ino, vpp)
753 struct mount *mp;
754 ino_t ino;
755 struct vnode **vpp;
756 {
757 register struct fs *fs;
758 register struct inode *ip;
759 struct ufsmount *ump;
760 struct buf *bp;
761 struct vnode *vp;
762 dev_t dev;
763 int i, type, error;
764
765 ump = VFSTOUFS(mp);
766 dev = ump->um_dev;
767 if ((*vpp = ufs_ihashget(dev, ino)) != NULL)
768 return (0);
769
770 /* Allocate a new vnode/inode. */
771 if (error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, &vp)) {
772 *vpp = NULL;
773 return (error);
774 }
775 type = ump->um_devvp->v_tag == VT_MFS ? M_MFSNODE : M_FFSNODE; /* XXX */
776 MALLOC(ip, struct inode *, sizeof(struct inode), type, M_WAITOK);
777 bzero((caddr_t)ip, sizeof(struct inode));
778 vp->v_data = ip;
779 ip->i_vnode = vp;
780 ip->i_fs = fs = ump->um_fs;
781 ip->i_dev = dev;
782 ip->i_number = ino;
783 #ifdef QUOTA
784 for (i = 0; i < MAXQUOTAS; i++)
785 ip->i_dquot[i] = NODQUOT;
786 #endif
787 /*
788 * Put it onto its hash chain and lock it so that other requests for
789 * this inode will block if they arrive while we are sleeping waiting
790 * for old data structures to be purged or for the contents of the
791 * disk portion of this inode to be read.
792 */
793 ufs_ihashins(ip);
794
795 /* Read in the disk contents for the inode, copy into the inode. */
796 if (error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
797 (int)fs->fs_bsize, NOCRED, &bp)) {
798 /*
799 * The inode does not contain anything useful, so it would
800 * be misleading to leave it on its hash chain. With mode
801 * still zero, it will be unlinked and returned to the free
802 * list by vput().
803 */
804 vput(vp);
805 brelse(bp);
806 *vpp = NULL;
807 return (error);
808 }
809 ip->i_din = *((struct dinode *)bp->b_data + ino_to_fsbo(fs, ino));
810 brelse(bp);
811
812 /*
813 * Initialize the vnode from the inode, check for aliases.
814 * Note that the underlying vnode may have changed.
815 */
816 if (error = ufs_vinit(mp, ffs_specop_p, FFS_FIFOOPS, &vp)) {
817 vput(vp);
818 *vpp = NULL;
819 return (error);
820 }
821 /*
822 * Finish inode initialization now that aliasing has been resolved.
823 */
824 ip->i_devvp = ump->um_devvp;
825 VREF(ip->i_devvp);
826 /*
827 * Set up a generation number for this inode if it does not
828 * already have one. This should only happen on old filesystems.
829 */
830 if (ip->i_gen == 0) {
831 if (++nextgennumber < (u_long)time.tv_sec)
832 nextgennumber = time.tv_sec;
833 ip->i_gen = nextgennumber;
834 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
835 ip->i_flag |= IN_MODIFIED;
836 }
837 /*
838 * Ensure that uid and gid are correct. This is a temporary
839 * fix until fsck has been changed to do the update.
840 */
841 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
842 ip->i_uid = ip->i_din.di_ouid; /* XXX */
843 ip->i_gid = ip->i_din.di_ogid; /* XXX */
844 } /* XXX */
845
846 *vpp = vp;
847 return (0);
848 }
849
850 /*
851 * File handle to vnode
852 *
853 * Have to be really careful about stale file handles:
854 * - check that the inode number is valid
855 * - call ffs_vget() to get the locked inode
856 * - check for an unallocated inode (i_mode == 0)
857 * - check that the given client host has export rights and return
858 * those rights via. exflagsp and credanonp
859 */
860 int
861 ffs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
862 register struct mount *mp;
863 struct fid *fhp;
864 struct mbuf *nam;
865 struct vnode **vpp;
866 int *exflagsp;
867 struct ucred **credanonp;
868 {
869 register struct ufid *ufhp;
870 struct fs *fs;
871
872 ufhp = (struct ufid *)fhp;
873 fs = VFSTOUFS(mp)->um_fs;
874 if (ufhp->ufid_ino < ROOTINO ||
875 ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
876 return (ESTALE);
877 return (ufs_check_export(mp, ufhp, nam, vpp, exflagsp, credanonp));
878 }
879
880 /*
881 * Vnode pointer to File handle
882 */
883 /* ARGSUSED */
884 ffs_vptofh(vp, fhp)
885 struct vnode *vp;
886 struct fid *fhp;
887 {
888 register struct inode *ip;
889 register struct ufid *ufhp;
890
891 ip = VTOI(vp);
892 ufhp = (struct ufid *)fhp;
893 ufhp->ufid_len = sizeof(struct ufid);
894 ufhp->ufid_ino = ip->i_number;
895 ufhp->ufid_gen = ip->i_gen;
896 return (0);
897 }
898
899 /*
900 * Write a superblock and associated information back to disk.
901 */
902 int
903 ffs_sbupdate(mp, waitfor)
904 struct ufsmount *mp;
905 int waitfor;
906 {
907 register struct fs *dfs, *fs = mp->um_fs;
908 register struct buf *bp;
909 int i, error = 0;
910
911 bp = getblk(mp->um_devvp, SBOFF >> (fs->fs_fshift - fs->fs_fsbtodb),
912 (int)fs->fs_sbsize, 0, 0);
913 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
914 /* Restore compatibility to old file systems. XXX */
915 dfs = (struct fs *)bp->b_data; /* XXX */
916 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
917 dfs->fs_nrpos = -1; /* XXX */
918 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
919 int32_t *lp, tmp; /* XXX */
920 /* XXX */
921 lp = (int32_t *)&dfs->fs_qbmask; /* XXX */
922 tmp = lp[4]; /* XXX */
923 for (i = 4; i > 0; i--) /* XXX */
924 lp[i] = lp[i-1]; /* XXX */
925 lp[0] = tmp; /* XXX */
926 } /* XXX */
927 dfs->fs_maxfilesize = mp->um_savedmaxfilesize; /* XXX */
928 if (waitfor == MNT_WAIT)
929 error = bwrite(bp);
930 else
931 bawrite(bp);
932 return (error);
933 }
934
935 int
936 ffs_cgupdate(mp, waitfor)
937 struct ufsmount *mp;
938 int waitfor;
939 {
940 register struct fs *fs = mp->um_fs;
941 register struct buf *bp;
942 int blks;
943 caddr_t space;
944 int i, size, error = 0, allerror = 0;
945
946 allerror = ffs_sbupdate(mp, waitfor);
947 blks = howmany(fs->fs_cssize, fs->fs_fsize);
948 space = (caddr_t)fs->fs_csp[0];
949 for (i = 0; i < blks; i += fs->fs_frag) {
950 size = fs->fs_bsize;
951 if (i + fs->fs_frag > blks)
952 size = (blks - i) * fs->fs_fsize;
953 bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
954 size, 0, 0);
955 bcopy(space, bp->b_data, (u_int)size);
956 space += size;
957 if (waitfor == MNT_WAIT)
958 error = bwrite(bp);
959 else
960 bawrite(bp);
961 }
962 if (!allerror && error)
963 allerror = error;
964 return (allerror);
965 }
966