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