ffs_vfsops.c revision 1.3 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.3 1994/06/28 21:50:24 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 mountp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
287 ffs_oldfscompat(fs);
288 /*
289 * Step 3: re-read summary information from disk.
290 */
291 blks = howmany(fs->fs_cssize, fs->fs_fsize);
292 space = fs->fs_csp[0];
293 for (i = 0; i < blks; i += fs->fs_frag) {
294 size = fs->fs_bsize;
295 if (i + fs->fs_frag > blks)
296 size = (blks - i) * fs->fs_fsize;
297 if (error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
298 NOCRED, &bp))
299 return (error);
300 bcopy(bp->b_data, fs->fs_csp[fragstoblks(fs, i)], (u_int)size);
301 brelse(bp);
302 }
303 loop:
304 for (vp = mountp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
305 nvp = vp->v_mntvnodes.le_next;
306 /*
307 * Step 4: invalidate all inactive vnodes.
308 */
309 if (vp->v_usecount == 0) {
310 vgone(vp);
311 continue;
312 }
313 /*
314 * Step 5: invalidate all cached file data.
315 */
316 if (vget(vp, 1))
317 goto loop;
318 if (vinvalbuf(vp, 0, cred, p, 0, 0))
319 panic("ffs_reload: dirty2");
320 /*
321 * Step 6: re-read inode data for all active vnodes.
322 */
323 ip = VTOI(vp);
324 if (error =
325 bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
326 (int)fs->fs_bsize, NOCRED, &bp)) {
327 vput(vp);
328 return (error);
329 }
330 ip->i_din = *((struct dinode *)bp->b_data +
331 ino_to_fsbo(fs, ip->i_number));
332 brelse(bp);
333 vput(vp);
334 if (vp->v_mount != mountp)
335 goto loop;
336 }
337 return (0);
338 }
339
340 /*
341 * Common code for mount and mountroot
342 */
343 int
344 ffs_mountfs(devvp, mp, p)
345 register struct vnode *devvp;
346 struct mount *mp;
347 struct proc *p;
348 {
349 register struct ufsmount *ump;
350 struct buf *bp;
351 register struct fs *fs;
352 dev_t dev = devvp->v_rdev;
353 struct partinfo dpart;
354 caddr_t base, space;
355 int blks;
356 int error, i, size;
357 int ronly;
358 extern struct vnode *rootvp;
359
360 /*
361 * Disallow multiple mounts of the same device.
362 * Disallow mounting of a device that is currently in use
363 * (except for root, which might share swap device for miniroot).
364 * Flush out any old buffers remaining from a previous use.
365 */
366 if (error = vfs_mountedon(devvp))
367 return (error);
368 if (vcount(devvp) > 1 && devvp != rootvp)
369 return (EBUSY);
370 if (error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0))
371 return (error);
372
373 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
374 if (error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p))
375 return (error);
376 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED, p) != 0)
377 size = DEV_BSIZE;
378 else
379 size = dpart.disklab->d_secsize;
380
381 bp = NULL;
382 ump = NULL;
383 if (error = bread(devvp, (daddr_t)(SBOFF / size), SBSIZE, NOCRED, &bp))
384 goto out;
385 fs = (struct fs *)bp->b_data;
386 if (fs->fs_magic != FS_MAGIC || fs->fs_bsize > MAXBSIZE ||
387 fs->fs_bsize < sizeof(struct fs)) {
388 error = EINVAL; /* XXX needs translation */
389 goto out;
390 }
391 /* XXX updating 4.2 FFS superblocks trashes rotational layout tables */
392 if (fs->fs_postblformat == FS_42POSTBLFMT && !ronly) {
393 error = EROFS; /* XXX what should be returned? */
394 goto out;
395 }
396 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
397 bzero((caddr_t)ump, sizeof *ump);
398 ump->um_fs = malloc((u_long)fs->fs_sbsize, M_UFSMNT,
399 M_WAITOK);
400 bcopy(bp->b_data, ump->um_fs, (u_int)fs->fs_sbsize);
401 if (fs->fs_sbsize < SBSIZE)
402 bp->b_flags |= B_INVAL;
403 brelse(bp);
404 bp = NULL;
405 fs = ump->um_fs;
406 fs->fs_ronly = ronly;
407 if (ronly == 0)
408 fs->fs_fmod = 1;
409 blks = howmany(fs->fs_cssize, fs->fs_fsize);
410 base = space = malloc((u_long)fs->fs_cssize, M_UFSMNT,
411 M_WAITOK);
412 for (i = 0; i < blks; i += fs->fs_frag) {
413 size = fs->fs_bsize;
414 if (i + fs->fs_frag > blks)
415 size = (blks - i) * fs->fs_fsize;
416 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
417 NOCRED, &bp);
418 if (error) {
419 free(base, M_UFSMNT);
420 goto out;
421 }
422 bcopy(bp->b_data, space, (u_int)size);
423 fs->fs_csp[fragstoblks(fs, i)] = (struct csum *)space;
424 space += size;
425 brelse(bp);
426 bp = NULL;
427 }
428 mp->mnt_data = (qaddr_t)ump;
429 mp->mnt_stat.f_fsid.val[0] = (long)dev;
430 mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_UFS);
431 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
432 mp->mnt_flag |= MNT_LOCAL;
433 ump->um_mountp = mp;
434 ump->um_dev = dev;
435 ump->um_devvp = devvp;
436 ump->um_nindir = fs->fs_nindir;
437 ump->um_bptrtodb = fs->fs_fsbtodb;
438 ump->um_seqinc = fs->fs_frag;
439 for (i = 0; i < MAXQUOTAS; i++)
440 ump->um_quotas[i] = NULLVP;
441 devvp->v_specflags |= SI_MOUNTEDON;
442 ffs_oldfscompat(fs);
443 return (0);
444 out:
445 if (bp)
446 brelse(bp);
447 (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
448 if (ump) {
449 free(ump->um_fs, M_UFSMNT);
450 free(ump, M_UFSMNT);
451 mp->mnt_data = (qaddr_t)0;
452 }
453 return (error);
454 }
455
456 /*
457 * Sanity checks for old file systems.
458 *
459 * XXX - goes away some day.
460 */
461 ffs_oldfscompat(fs)
462 struct fs *fs;
463 {
464 int i;
465
466 fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect); /* XXX */
467 fs->fs_interleave = max(fs->fs_interleave, 1); /* XXX */
468 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
469 fs->fs_nrpos = 8; /* XXX */
470 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
471 quad_t sizepb = fs->fs_bsize; /* XXX */
472 /* XXX */
473 fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1; /* XXX */
474 for (i = 0; i < NIADDR; i++) { /* XXX */
475 sizepb *= NINDIR(fs); /* XXX */
476 fs->fs_maxfilesize += sizepb; /* XXX */
477 } /* XXX */
478 fs->fs_qbmask = ~fs->fs_bmask; /* XXX */
479 fs->fs_qfmask = ~fs->fs_fmask; /* XXX */
480 } /* XXX */
481 return (0);
482 }
483
484 /*
485 * unmount system call
486 */
487 int
488 ffs_unmount(mp, mntflags, p)
489 struct mount *mp;
490 int mntflags;
491 struct proc *p;
492 {
493 register struct ufsmount *ump;
494 register struct fs *fs;
495 int error, flags, ronly;
496
497 flags = 0;
498 if (mntflags & MNT_FORCE) {
499 if (mp->mnt_flag & MNT_ROOTFS)
500 return (EINVAL);
501 flags |= FORCECLOSE;
502 }
503 if (error = ffs_flushfiles(mp, flags, p))
504 return (error);
505 ump = VFSTOUFS(mp);
506 fs = ump->um_fs;
507 ronly = !fs->fs_ronly;
508 ump->um_devvp->v_specflags &= ~SI_MOUNTEDON;
509 error = VOP_CLOSE(ump->um_devvp, ronly ? FREAD : FREAD|FWRITE,
510 NOCRED, p);
511 vrele(ump->um_devvp);
512 free(fs->fs_csp[0], M_UFSMNT);
513 free(fs, M_UFSMNT);
514 free(ump, M_UFSMNT);
515 mp->mnt_data = (qaddr_t)0;
516 mp->mnt_flag &= ~MNT_LOCAL;
517 return (error);
518 }
519
520 /*
521 * Flush out all the files in a filesystem.
522 */
523 ffs_flushfiles(mp, flags, p)
524 register struct mount *mp;
525 int flags;
526 struct proc *p;
527 {
528 extern int doforce;
529 register struct ufsmount *ump;
530 int i, error;
531
532 if (!doforce)
533 flags &= ~FORCECLOSE;
534 ump = VFSTOUFS(mp);
535 #ifdef QUOTA
536 if (mp->mnt_flag & MNT_QUOTA) {
537 if (error = vflush(mp, NULLVP, SKIPSYSTEM|flags))
538 return (error);
539 for (i = 0; i < MAXQUOTAS; i++) {
540 if (ump->um_quotas[i] == NULLVP)
541 continue;
542 quotaoff(p, mp, i);
543 }
544 /*
545 * Here we fall through to vflush again to ensure
546 * that we have gotten rid of all the system vnodes.
547 */
548 }
549 #endif
550 error = vflush(mp, NULLVP, flags);
551 return (error);
552 }
553
554 /*
555 * Get file system statistics.
556 */
557 int
558 ffs_statfs(mp, sbp, p)
559 struct mount *mp;
560 register struct statfs *sbp;
561 struct proc *p;
562 {
563 register struct ufsmount *ump;
564 register struct fs *fs;
565
566 ump = VFSTOUFS(mp);
567 fs = ump->um_fs;
568 if (fs->fs_magic != FS_MAGIC)
569 panic("ffs_statfs");
570 #ifdef COMPAT_09
571 sbp->f_type = 1;
572 #else
573 sbp->f_type = 0;
574 #endif
575 sbp->f_bsize = fs->fs_fsize;
576 sbp->f_iosize = fs->fs_bsize;
577 sbp->f_blocks = fs->fs_dsize;
578 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
579 fs->fs_cstotal.cs_nffree;
580 sbp->f_bavail = (fs->fs_dsize * (100 - fs->fs_minfree) / 100) -
581 (fs->fs_dsize - sbp->f_bfree);
582 sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO;
583 sbp->f_ffree = fs->fs_cstotal.cs_nifree;
584 if (sbp != &mp->mnt_stat) {
585 bcopy((caddr_t)mp->mnt_stat.f_mntonname,
586 (caddr_t)&sbp->f_mntonname[0], MNAMELEN);
587 bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
588 (caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
589 }
590 strncpy(&sbp->f_fstypename[0], mp->mnt_op->vfs_name, MFSNAMELEN);
591 sbp->f_fstypename[MFSNAMELEN] = '\0';
592 return (0);
593 }
594
595 /*
596 * Go through the disk queues to initiate sandbagged IO;
597 * go through the inodes to write those that have been modified;
598 * initiate the writing of the super block if it has been modified.
599 *
600 * Note: we are always called with the filesystem marked `MPBUSY'.
601 */
602 int
603 ffs_sync(mp, waitfor, cred, p)
604 struct mount *mp;
605 int waitfor;
606 struct ucred *cred;
607 struct proc *p;
608 {
609 register struct vnode *vp;
610 register struct inode *ip;
611 register struct ufsmount *ump = VFSTOUFS(mp);
612 register struct fs *fs;
613 int error, allerror = 0;
614
615 fs = ump->um_fs;
616 /*
617 * Write back modified superblock.
618 * Consistency check that the superblock
619 * is still in the buffer cache.
620 */
621 if (fs->fs_fmod != 0) {
622 if (fs->fs_ronly != 0) { /* XXX */
623 printf("fs = %s\n", fs->fs_fsmnt);
624 panic("update: rofs mod");
625 }
626 fs->fs_fmod = 0;
627 fs->fs_time = time.tv_sec;
628 allerror = ffs_sbupdate(ump, waitfor);
629 }
630 /*
631 * Write back each (modified) inode.
632 */
633 loop:
634 for (vp = mp->mnt_vnodelist.lh_first;
635 vp != NULL;
636 vp = vp->v_mntvnodes.le_next) {
637 /*
638 * If the vnode that we are about to sync is no longer
639 * associated with this mount point, start over.
640 */
641 if (vp->v_mount != mp)
642 goto loop;
643 if (VOP_ISLOCKED(vp))
644 continue;
645 ip = VTOI(vp);
646 if ((ip->i_flag &
647 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
648 vp->v_dirtyblkhd.lh_first == NULL)
649 continue;
650 if (vget(vp, 1))
651 goto loop;
652 if (error = VOP_FSYNC(vp, cred, waitfor, p))
653 allerror = error;
654 vput(vp);
655 }
656 /*
657 * Force stale file system control information to be flushed.
658 */
659 if (error = VOP_FSYNC(ump->um_devvp, cred, waitfor, p))
660 allerror = error;
661 #ifdef QUOTA
662 qsync(mp);
663 #endif
664 return (allerror);
665 }
666
667 /*
668 * Look up a FFS dinode number to find its incore vnode, otherwise read it
669 * in from disk. If it is in core, wait for the lock bit to clear, then
670 * return the inode locked. Detection and handling of mount points must be
671 * done by the calling routine.
672 */
673 int
674 ffs_vget(mp, ino, vpp)
675 struct mount *mp;
676 ino_t ino;
677 struct vnode **vpp;
678 {
679 register struct fs *fs;
680 register struct inode *ip;
681 struct ufsmount *ump;
682 struct buf *bp;
683 struct vnode *vp;
684 dev_t dev;
685 int i, type, error;
686
687 ump = VFSTOUFS(mp);
688 dev = ump->um_dev;
689 if ((*vpp = ufs_ihashget(dev, ino)) != NULL)
690 return (0);
691
692 /* Allocate a new vnode/inode. */
693 if (error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, &vp)) {
694 *vpp = NULL;
695 return (error);
696 }
697 type = ump->um_devvp->v_tag == VT_MFS ? M_MFSNODE : M_FFSNODE; /* XXX */
698 MALLOC(ip, struct inode *, sizeof(struct inode), type, M_WAITOK);
699 bzero((caddr_t)ip, sizeof(struct inode));
700 vp->v_data = ip;
701 ip->i_vnode = vp;
702 ip->i_fs = fs = ump->um_fs;
703 ip->i_dev = dev;
704 ip->i_number = ino;
705 #ifdef QUOTA
706 for (i = 0; i < MAXQUOTAS; i++)
707 ip->i_dquot[i] = NODQUOT;
708 #endif
709 /*
710 * Put it onto its hash chain and lock it so that other requests for
711 * this inode will block if they arrive while we are sleeping waiting
712 * for old data structures to be purged or for the contents of the
713 * disk portion of this inode to be read.
714 */
715 ufs_ihashins(ip);
716
717 /* Read in the disk contents for the inode, copy into the inode. */
718 if (error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
719 (int)fs->fs_bsize, NOCRED, &bp)) {
720 /*
721 * The inode does not contain anything useful, so it would
722 * be misleading to leave it on its hash chain. With mode
723 * still zero, it will be unlinked and returned to the free
724 * list by vput().
725 */
726 vput(vp);
727 brelse(bp);
728 *vpp = NULL;
729 return (error);
730 }
731 ip->i_din = *((struct dinode *)bp->b_data + ino_to_fsbo(fs, ino));
732 brelse(bp);
733
734 /*
735 * Initialize the vnode from the inode, check for aliases.
736 * Note that the underlying vnode may have changed.
737 */
738 if (error = ufs_vinit(mp, ffs_specop_p, FFS_FIFOOPS, &vp)) {
739 vput(vp);
740 *vpp = NULL;
741 return (error);
742 }
743 /*
744 * Finish inode initialization now that aliasing has been resolved.
745 */
746 ip->i_devvp = ump->um_devvp;
747 VREF(ip->i_devvp);
748 /*
749 * Set up a generation number for this inode if it does not
750 * already have one. This should only happen on old filesystems.
751 */
752 if (ip->i_gen == 0) {
753 if (++nextgennumber < (u_long)time.tv_sec)
754 nextgennumber = time.tv_sec;
755 ip->i_gen = nextgennumber;
756 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
757 ip->i_flag |= IN_MODIFIED;
758 }
759 /*
760 * Ensure that uid and gid are correct. This is a temporary
761 * fix until fsck has been changed to do the update.
762 */
763 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
764 ip->i_uid = ip->i_din.di_ouid; /* XXX */
765 ip->i_gid = ip->i_din.di_ogid; /* XXX */
766 } /* XXX */
767
768 *vpp = vp;
769 return (0);
770 }
771
772 /*
773 * File handle to vnode
774 *
775 * Have to be really careful about stale file handles:
776 * - check that the inode number is valid
777 * - call ffs_vget() to get the locked inode
778 * - check for an unallocated inode (i_mode == 0)
779 * - check that the given client host has export rights and return
780 * those rights via. exflagsp and credanonp
781 */
782 int
783 ffs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
784 register struct mount *mp;
785 struct fid *fhp;
786 struct mbuf *nam;
787 struct vnode **vpp;
788 int *exflagsp;
789 struct ucred **credanonp;
790 {
791 register struct ufid *ufhp;
792 struct fs *fs;
793
794 ufhp = (struct ufid *)fhp;
795 fs = VFSTOUFS(mp)->um_fs;
796 if (ufhp->ufid_ino < ROOTINO ||
797 ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
798 return (ESTALE);
799 return (ufs_check_export(mp, ufhp, nam, vpp, exflagsp, credanonp));
800 }
801
802 /*
803 * Vnode pointer to File handle
804 */
805 /* ARGSUSED */
806 ffs_vptofh(vp, fhp)
807 struct vnode *vp;
808 struct fid *fhp;
809 {
810 register struct inode *ip;
811 register struct ufid *ufhp;
812
813 ip = VTOI(vp);
814 ufhp = (struct ufid *)fhp;
815 ufhp->ufid_len = sizeof(struct ufid);
816 ufhp->ufid_ino = ip->i_number;
817 ufhp->ufid_gen = ip->i_gen;
818 return (0);
819 }
820
821 /*
822 * Write a superblock and associated information back to disk.
823 */
824 int
825 ffs_sbupdate(mp, waitfor)
826 struct ufsmount *mp;
827 int waitfor;
828 {
829 register struct fs *fs = mp->um_fs;
830 register struct buf *bp;
831 int blks;
832 caddr_t space;
833 int i, size, error = 0;
834
835 bp = getblk(mp->um_devvp, SBOFF >> (fs->fs_fshift - fs->fs_fsbtodb),
836 (int)fs->fs_sbsize, 0, 0);
837 bcopy((caddr_t)fs, bp->b_data, (u_int)fs->fs_sbsize);
838 /* Restore compatibility to old file systems. XXX */
839 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
840 ((struct fs *)bp->b_data)->fs_nrpos = -1; /* XXX */
841 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
842 long *lp, tmp; /* XXX */
843 /* XXX */
844 lp = (long *)&((struct fs *)bp->b_data)->fs_qbmask; /* XXX */
845 tmp = lp[4]; /* XXX */
846 for (i = 4; i > 0; i--) /* XXX */
847 lp[i] = lp[i-1]; /* XXX */
848 lp[0] = tmp; /* XXX */
849 } /* XXX */
850 if (waitfor == MNT_WAIT)
851 error = bwrite(bp);
852 else
853 bawrite(bp);
854 blks = howmany(fs->fs_cssize, fs->fs_fsize);
855 space = (caddr_t)fs->fs_csp[0];
856 for (i = 0; i < blks; i += fs->fs_frag) {
857 size = fs->fs_bsize;
858 if (i + fs->fs_frag > blks)
859 size = (blks - i) * fs->fs_fsize;
860 bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
861 size, 0, 0);
862 bcopy(space, bp->b_data, (u_int)size);
863 space += size;
864 if (waitfor == MNT_WAIT)
865 error = bwrite(bp);
866 else
867 bawrite(bp);
868 }
869 return (error);
870 }
871