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