ffs_vfsops.c revision 1.67.2.8 1 /* $NetBSD: ffs_vfsops.c,v 1.67.2.8 2003/10/06 09:25:23 itojun 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.31 (Berkeley) 5/20/95
36 */
37
38 #if defined(_KERNEL) && !defined(_LKM)
39 #include "opt_ffs.h"
40 #include "opt_quota.h"
41 #include "opt_compat_netbsd.h"
42 #include "opt_softdep.h"
43 #endif
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/namei.h>
48 #include <sys/proc.h>
49 #include <sys/kernel.h>
50 #include <sys/vnode.h>
51 #include <sys/socket.h>
52 #include <sys/mount.h>
53 #include <sys/buf.h>
54 #include <sys/device.h>
55 #include <sys/mbuf.h>
56 #include <sys/file.h>
57 #include <sys/disklabel.h>
58 #include <sys/ioctl.h>
59 #include <sys/errno.h>
60 #include <sys/malloc.h>
61 #include <sys/pool.h>
62 #include <sys/lock.h>
63 #include <vm/vm.h>
64 #include <sys/sysctl.h>
65
66 #include <miscfs/specfs/specdev.h>
67
68 #include <ufs/ufs/quota.h>
69 #include <ufs/ufs/ufsmount.h>
70 #include <ufs/ufs/inode.h>
71 #include <ufs/ufs/dir.h>
72 #include <ufs/ufs/ufs_extern.h>
73 #include <ufs/ufs/ufs_bswap.h>
74
75 #include <ufs/ffs/fs.h>
76 #include <ufs/ffs/ffs_extern.h>
77
78 /* how many times ffs_init() was called */
79 int ffs_initcount = 0;
80
81 extern struct lock ufs_hashlock;
82
83 extern struct vnodeopv_desc ffs_vnodeop_opv_desc;
84 extern struct vnodeopv_desc ffs_specop_opv_desc;
85 extern struct vnodeopv_desc ffs_fifoop_opv_desc;
86
87 struct vnodeopv_desc *ffs_vnodeopv_descs[] = {
88 &ffs_vnodeop_opv_desc,
89 &ffs_specop_opv_desc,
90 &ffs_fifoop_opv_desc,
91 NULL,
92 };
93
94 struct vfsops ffs_vfsops = {
95 MOUNT_FFS,
96 ffs_mount,
97 ufs_start,
98 ffs_unmount,
99 ufs_root,
100 ufs_quotactl,
101 ffs_statfs,
102 ffs_sync,
103 ffs_vget,
104 ffs_fhtovp,
105 ffs_vptofh,
106 ffs_init,
107 ffs_done,
108 ffs_sysctl,
109 ffs_mountroot,
110 ufs_check_export,
111 ffs_vnodeopv_descs,
112 };
113
114 struct pool ffs_inode_pool;
115
116 /*
117 * Called by main() when ffs is going to be mounted as root.
118 */
119
120 int
121 ffs_mountroot()
122 {
123 struct fs *fs;
124 struct mount *mp;
125 struct proc *p = curproc; /* XXX */
126 struct ufsmount *ump;
127 int error;
128
129 if (root_device->dv_class != DV_DISK)
130 return (ENODEV);
131
132 /*
133 * Get vnodes for rootdev.
134 */
135 if (bdevvp(rootdev, &rootvp))
136 panic("ffs_mountroot: can't setup bdevvp's");
137
138 if ((error = vfs_rootmountalloc(MOUNT_FFS, "root_device", &mp))) {
139 vrele(rootvp);
140 return (error);
141 }
142 if ((error = ffs_mountfs(rootvp, mp, p)) != 0) {
143 mp->mnt_op->vfs_refcount--;
144 vfs_unbusy(mp);
145 free(mp, M_MOUNT);
146 vrele(rootvp);
147 return (error);
148 }
149 simple_lock(&mountlist_slock);
150 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
151 simple_unlock(&mountlist_slock);
152 ump = VFSTOUFS(mp);
153 fs = ump->um_fs;
154 memset(fs->fs_fsmnt, 0, sizeof(fs->fs_fsmnt));
155 (void)copystr(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN - 1, 0);
156 (void)ffs_statfs(mp, &mp->mnt_stat, p);
157 vfs_unbusy(mp);
158 inittodr(fs->fs_time);
159 return (0);
160 }
161
162 /*
163 * VFS Operations.
164 *
165 * mount system call
166 */
167 int
168 ffs_mount(mp, path, data, ndp, p)
169 struct mount *mp;
170 const char *path;
171 void *data;
172 struct nameidata *ndp;
173 struct proc *p;
174 {
175 struct vnode *devvp;
176 struct ufs_args args;
177 struct ufsmount *ump = NULL;
178 struct fs *fs;
179 size_t size;
180 int error, flags;
181 mode_t accessmode;
182
183 error = copyin(data, (caddr_t)&args, sizeof (struct ufs_args));
184 if (error)
185 return (error);
186
187 #if !defined(SOFTDEP)
188 mp->mnt_flag &= ~MNT_SOFTDEP;
189 #endif
190
191 /*
192 * If updating, check whether changing from read-only to
193 * read/write; if there is no device name, that's all we do.
194 */
195 if (mp->mnt_flag & MNT_UPDATE) {
196 ump = VFSTOUFS(mp);
197 fs = ump->um_fs;
198 if (fs->fs_ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
199 flags = WRITECLOSE;
200 if (mp->mnt_flag & MNT_FORCE)
201 flags |= FORCECLOSE;
202 if (mp->mnt_flag & MNT_SOFTDEP)
203 error = softdep_flushfiles(mp, flags, p);
204 else
205 error = ffs_flushfiles(mp, flags, p);
206 if (error == 0 &&
207 ffs_cgupdate(ump, MNT_WAIT) == 0 &&
208 fs->fs_clean & FS_WASCLEAN) {
209 if (mp->mnt_flag & MNT_SOFTDEP)
210 fs->fs_flags &= ~FS_DOSOFTDEP;
211 fs->fs_clean = FS_ISCLEAN;
212 (void) ffs_sbupdate(ump, MNT_WAIT);
213 }
214 if (error)
215 return (error);
216 fs->fs_ronly = 1;
217 fs->fs_fmod = 0;
218 }
219
220 /*
221 * Flush soft dependencies if disabling it via an update
222 * mount. This may leave some items to be processed,
223 * so don't do this yet XXX.
224 */
225 if ((fs->fs_flags & FS_DOSOFTDEP) &&
226 !(mp->mnt_flag & MNT_SOFTDEP) && fs->fs_ronly == 0) {
227 #ifdef notyet
228 flags = WRITECLOSE;
229 if (mp->mnt_flag & MNT_FORCE)
230 flags |= FORCECLOSE;
231 error = softdep_flushfiles(mp, flags, p);
232 if (error == 0 && ffs_cgupdate(ump, MNT_WAIT) == 0)
233 fs->fs_flags &= ~FS_DOSOFTDEP;
234 (void) ffs_sbupdate(ump, MNT_WAIT);
235 #elif defined(SOFTDEP)
236 mp->mnt_flag |= MNT_SOFTDEP;
237 #endif
238 }
239
240 /*
241 * When upgrading to a softdep mount, we must first flush
242 * all vnodes. (not done yet -- see above)
243 */
244 if (!(fs->fs_flags & FS_DOSOFTDEP) &&
245 (mp->mnt_flag & MNT_SOFTDEP) && fs->fs_ronly == 0) {
246 #ifdef notyet
247 flags = WRITECLOSE;
248 if (mp->mnt_flag & MNT_FORCE)
249 flags |= FORCECLOSE;
250 error = ffs_flushfiles(mp, flags, p);
251 #else
252 mp->mnt_flag &= ~MNT_SOFTDEP;
253 #endif
254 }
255
256 if (mp->mnt_flag & MNT_RELOAD) {
257 error = ffs_reload(mp, ndp->ni_cnd.cn_cred, p);
258 if (error)
259 return (error);
260 }
261 if (fs->fs_ronly && (mp->mnt_flag & MNT_WANTRDWR)) {
262 /*
263 * If upgrade to read-write by non-root, then verify
264 * that user has necessary permissions on the device.
265 */
266 devvp = ump->um_devvp;
267 if (p->p_ucred->cr_uid != 0) {
268 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
269 error = VOP_ACCESS(devvp, VREAD | VWRITE,
270 p->p_ucred, p);
271 VOP_UNLOCK(devvp, 0);
272 if (error)
273 return (error);
274 }
275 fs->fs_ronly = 0;
276 fs->fs_clean <<= 1;
277 fs->fs_fmod = 1;
278 if ((fs->fs_flags & FS_DOSOFTDEP)) {
279 error = softdep_mount(devvp, mp, fs,
280 p->p_ucred);
281 if (error)
282 return (error);
283 }
284 }
285 if (args.fspec == 0) {
286 /*
287 * Process export requests.
288 */
289 return (vfs_export(mp, &ump->um_export, &args.export));
290 }
291 if ((mp->mnt_flag & (MNT_SOFTDEP | MNT_ASYNC)) ==
292 (MNT_SOFTDEP | MNT_ASYNC)) {
293 printf("%s fs uses soft updates, ignoring async mode\n",
294 fs->fs_fsmnt);
295 mp->mnt_flag &= ~MNT_ASYNC;
296 }
297 }
298 /*
299 * Not an update, or updating the name: look up the name
300 * and verify that it refers to a sensible block device.
301 */
302 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
303 if ((error = namei(ndp)) != 0)
304 return (error);
305 devvp = ndp->ni_vp;
306
307 if (devvp->v_type != VBLK) {
308 vrele(devvp);
309 return (ENOTBLK);
310 }
311 if (major(devvp->v_rdev) >= nblkdev) {
312 vrele(devvp);
313 return (ENXIO);
314 }
315 /*
316 * If mount by non-root, then verify that user has necessary
317 * permissions on the device.
318 */
319 if (p->p_ucred->cr_uid != 0) {
320 accessmode = VREAD;
321 if ((mp->mnt_flag & MNT_RDONLY) == 0)
322 accessmode |= VWRITE;
323 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
324 error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
325 VOP_UNLOCK(devvp, 0);
326 if (error) {
327 vrele(devvp);
328 return (error);
329 }
330 }
331 if ((mp->mnt_flag & MNT_UPDATE) == 0) {
332 error = ffs_mountfs(devvp, mp, p);
333 if (!error) {
334 ump = VFSTOUFS(mp);
335 fs = ump->um_fs;
336 if ((mp->mnt_flag & (MNT_SOFTDEP | MNT_ASYNC)) ==
337 (MNT_SOFTDEP | MNT_ASYNC)) {
338 printf("%s fs uses soft updates, "
339 "ignoring async mode\n",
340 fs->fs_fsmnt);
341 mp->mnt_flag &= ~MNT_ASYNC;
342 }
343 }
344 }
345 else {
346 if (devvp != ump->um_devvp)
347 error = EINVAL; /* needs translation */
348 else
349 vrele(devvp);
350 }
351 if (error) {
352 vrele(devvp);
353 return (error);
354 }
355 (void) copyinstr(path, fs->fs_fsmnt, sizeof(fs->fs_fsmnt) - 1, &size);
356 memset(fs->fs_fsmnt + size, 0, sizeof(fs->fs_fsmnt) - size);
357 memcpy(mp->mnt_stat.f_mntonname, fs->fs_fsmnt, MNAMELEN);
358 (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
359 &size);
360 memset(mp->mnt_stat.f_mntfromname + size, 0, MNAMELEN - size);
361 if (mp->mnt_flag & MNT_SOFTDEP)
362 fs->fs_flags |= FS_DOSOFTDEP;
363 if (fs->fs_fmod != 0) { /* XXX */
364 fs->fs_fmod = 0;
365 if (fs->fs_clean & FS_WASCLEAN)
366 fs->fs_time = time.tv_sec;
367 else
368 printf("%s: file system not clean (fs_clean=%x); please fsck(8)\n",
369 mp->mnt_stat.f_mntfromname, fs->fs_clean);
370 (void) ffs_cgupdate(ump, MNT_WAIT);
371 }
372 return (0);
373 }
374
375 /*
376 * Reload all incore data for a filesystem (used after running fsck on
377 * the root filesystem and finding things to fix). The filesystem must
378 * be mounted read-only.
379 *
380 * Things to do to update the mount:
381 * 1) invalidate all cached meta-data.
382 * 2) re-read superblock from disk.
383 * 3) re-read summary information from disk.
384 * 4) invalidate all inactive vnodes.
385 * 5) invalidate all cached file data.
386 * 6) re-read inode data for all active vnodes.
387 */
388 int
389 ffs_reload(mountp, cred, p)
390 struct mount *mountp;
391 struct ucred *cred;
392 struct proc *p;
393 {
394 struct vnode *vp, *nvp, *devvp;
395 struct inode *ip;
396 void *space;
397 struct buf *bp;
398 struct fs *fs, *newfs;
399 struct partinfo dpart;
400 int i, blks, size, error;
401 int32_t *lp;
402 caddr_t cp;
403
404 if ((mountp->mnt_flag & MNT_RDONLY) == 0)
405 return (EINVAL);
406 /*
407 * Step 1: invalidate all cached meta-data.
408 */
409 devvp = VFSTOUFS(mountp)->um_devvp;
410 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
411 error = vinvalbuf(devvp, 0, cred, p, 0, 0);
412 VOP_UNLOCK(devvp, 0);
413 if (error)
414 panic("ffs_reload: dirty1");
415 /*
416 * Step 2: re-read superblock from disk.
417 */
418 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED, p) != 0)
419 size = DEV_BSIZE;
420 else
421 size = dpart.disklab->d_secsize;
422 error = bread(devvp, (ufs_daddr_t)(SBOFF / size), SBSIZE, NOCRED, &bp);
423 if (error) {
424 brelse(bp);
425 return (error);
426 }
427 fs = VFSTOUFS(mountp)->um_fs;
428 newfs = malloc(fs->fs_sbsize, M_UFSMNT, M_WAITOK);
429 memcpy(newfs, bp->b_data, fs->fs_sbsize);
430 #ifdef FFS_EI
431 if (VFSTOUFS(mountp)->um_flags & UFS_NEEDSWAP) {
432 ffs_sb_swap((struct fs*)bp->b_data, newfs);
433 fs->fs_flags |= FS_SWAPPED;
434 } else
435 #endif
436 fs->fs_flags &= ~FS_SWAPPED;
437 if (newfs->fs_magic != FS_MAGIC || newfs->fs_bsize > MAXBSIZE ||
438 newfs->fs_bsize < sizeof(struct fs)) {
439 brelse(bp);
440 free(newfs, M_UFSMNT);
441 return (EIO); /* XXX needs translation */
442 }
443 /*
444 * Copy pointer fields back into superblock before copying in XXX
445 * new superblock. These should really be in the ufsmount. XXX
446 * Note that important parameters (eg fs_ncg) are unchanged.
447 */
448 newfs->fs_csp = fs->fs_csp;
449 newfs->fs_maxcluster = fs->fs_maxcluster;
450 newfs->fs_contigdirs = fs->fs_contigdirs;
451 newfs->fs_ronly = fs->fs_ronly;
452 memcpy(fs, newfs, (u_int)fs->fs_sbsize);
453 if (fs->fs_sbsize < SBSIZE)
454 bp->b_flags |= B_INVAL;
455 brelse(bp);
456 free(newfs, M_UFSMNT);
457 mountp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
458 ffs_oldfscompat(fs);
459 /* An old fsck may have zeroed these fields, so recheck them. */
460 if (fs->fs_avgfilesize <= 0)
461 fs->fs_avgfilesize = AVFILESIZ;
462 if (fs->fs_avgfpdir <= 0)
463 fs->fs_avgfpdir = AFPDIR;
464
465 ffs_statfs(mountp, &mountp->mnt_stat, p);
466 /*
467 * Step 3: re-read summary information from disk.
468 */
469 blks = howmany(fs->fs_cssize, fs->fs_fsize);
470 space = fs->fs_csp;
471 for (i = 0; i < blks; i += fs->fs_frag) {
472 size = fs->fs_bsize;
473 if (i + fs->fs_frag > blks)
474 size = (blks - i) * fs->fs_fsize;
475 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
476 NOCRED, &bp);
477 if (error) {
478 brelse(bp);
479 return (error);
480 }
481 #ifdef FFS_EI
482 if (UFS_FSNEEDSWAP(fs))
483 ffs_csum_swap((struct csum *)bp->b_data,
484 (struct csum *)space, size);
485 else
486 #endif
487 memcpy(space, bp->b_data, (size_t)size);
488 space = (char *)space + size;
489 brelse(bp);
490 }
491 if ((fs->fs_flags & FS_DOSOFTDEP))
492 softdep_mount(devvp, mountp, fs, cred);
493 /*
494 * We no longer know anything about clusters per cylinder group.
495 */
496 if (fs->fs_contigsumsize > 0) {
497 lp = fs->fs_maxcluster;
498 for (i = 0; i < fs->fs_ncg; i++)
499 *lp++ = fs->fs_contigsumsize;
500 }
501
502 loop:
503 simple_lock(&mntvnode_slock);
504 for (vp = mountp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
505 if (vp->v_mount != mountp) {
506 simple_unlock(&mntvnode_slock);
507 goto loop;
508 }
509 nvp = vp->v_mntvnodes.le_next;
510 /*
511 * Step 4: invalidate all inactive vnodes.
512 */
513 if (vrecycle(vp, &mntvnode_slock, p))
514 goto loop;
515 /*
516 * Step 5: invalidate all cached file data.
517 */
518 simple_lock(&vp->v_interlock);
519 simple_unlock(&mntvnode_slock);
520 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
521 goto loop;
522 if (vinvalbuf(vp, 0, cred, p, 0, 0))
523 panic("ffs_reload: dirty2");
524 /*
525 * Step 6: re-read inode data for all active vnodes.
526 */
527 ip = VTOI(vp);
528 error = bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
529 (int)fs->fs_bsize, NOCRED, &bp);
530 if (error) {
531 brelse(bp);
532 vput(vp);
533 return (error);
534 }
535 cp = (caddr_t)bp->b_data +
536 (ino_to_fsbo(fs, ip->i_number) * DINODE_SIZE);
537 #ifdef FFS_EI
538 if (UFS_FSNEEDSWAP(fs))
539 ffs_dinode_swap((struct dinode *)cp,
540 &ip->i_din.ffs_din);
541 else
542 #endif
543 memcpy(&ip->i_din.ffs_din, cp, DINODE_SIZE);
544 ip->i_ffs_effnlink = ip->i_ffs_nlink;
545 brelse(bp);
546 vput(vp);
547 simple_lock(&mntvnode_slock);
548 }
549 simple_unlock(&mntvnode_slock);
550 return (0);
551 }
552
553 /*
554 * Common code for mount and mountroot
555 */
556 int
557 ffs_mountfs(devvp, mp, p)
558 struct vnode *devvp;
559 struct mount *mp;
560 struct proc *p;
561 {
562 struct ufsmount *ump;
563 struct buf *bp;
564 struct fs *fs;
565 dev_t dev;
566 struct partinfo dpart;
567 void *space;
568 int blks;
569 int error, i, size, ronly;
570 #ifdef FFS_EI
571 int needswap;
572 #endif
573 int32_t *lp;
574 struct ucred *cred;
575 u_int64_t maxfilesize; /* XXX */
576 u_int32_t sbsize;
577
578 dev = devvp->v_rdev;
579 cred = p ? p->p_ucred : NOCRED;
580 /*
581 * Disallow multiple mounts of the same device.
582 * Disallow mounting of a device that is currently in use
583 * (except for root, which might share swap device for miniroot).
584 * Flush out any old buffers remaining from a previous use.
585 */
586 if ((error = vfs_mountedon(devvp)) != 0)
587 return (error);
588 if (vcount(devvp) > 1 && devvp != rootvp)
589 return (EBUSY);
590 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
591 error = vinvalbuf(devvp, V_SAVE, cred, p, 0, 0);
592 VOP_UNLOCK(devvp, 0);
593 if (error)
594 return (error);
595
596 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
597 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
598 if (error)
599 return (error);
600 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, cred, p) != 0)
601 size = DEV_BSIZE;
602 else
603 size = dpart.disklab->d_secsize;
604
605 bp = NULL;
606 ump = NULL;
607 error = bread(devvp, (ufs_daddr_t)(SBOFF / size), SBSIZE, cred, &bp);
608 if (error)
609 goto out;
610
611 fs = (struct fs*)bp->b_data;
612 if (fs->fs_magic == FS_MAGIC) {
613 sbsize = fs->fs_sbsize;
614 #ifdef FFS_EI
615 needswap = 0;
616 } else if (fs->fs_magic == bswap32(FS_MAGIC)) {
617 sbsize = bswap32(fs->fs_sbsize);
618 needswap = 1;
619 #endif
620 } else {
621 error = EINVAL;
622 goto out;
623 }
624 if (sbsize > MAXBSIZE || sbsize < sizeof(struct fs)) {
625 error = EINVAL;
626 goto out;
627 }
628
629 fs = malloc((u_long)sbsize, M_UFSMNT, M_WAITOK);
630 memcpy(fs, bp->b_data, sbsize);
631 #ifdef FFS_EI
632 if (needswap) {
633 ffs_sb_swap((struct fs*)bp->b_data, fs);
634 fs->fs_flags |= FS_SWAPPED;
635 } else
636 #endif
637 fs->fs_flags &= ~FS_SWAPPED;
638 ffs_oldfscompat(fs);
639
640 if (fs->fs_bsize > MAXBSIZE || fs->fs_bsize < sizeof(struct fs)) {
641 error = EINVAL;
642 goto out;
643 }
644 /* make sure cylinder group summary area is a reasonable size. */
645 if (fs->fs_cgsize == 0 || fs->fs_cpg == 0 ||
646 fs->fs_ncg > fs->fs_ncyl / fs->fs_cpg + 1 ||
647 fs->fs_cssize >
648 fragroundup(fs, fs->fs_ncg * sizeof(struct csum))) {
649 error = EINVAL; /* XXX needs translation */
650 goto out2;
651 }
652 /* XXX updating 4.2 FFS superblocks trashes rotational layout tables */
653 if (fs->fs_postblformat == FS_42POSTBLFMT && !ronly) {
654 error = EROFS; /* XXX what should be returned? */
655 goto out2;
656 }
657
658 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
659 memset((caddr_t)ump, 0, sizeof *ump);
660 ump->um_fs = fs;
661 if (fs->fs_sbsize < SBSIZE)
662 bp->b_flags |= B_INVAL;
663 brelse(bp);
664 bp = NULL;
665 fs->fs_ronly = ronly;
666 if (ronly == 0) {
667 fs->fs_clean <<= 1;
668 fs->fs_fmod = 1;
669 }
670 size = fs->fs_cssize;
671 blks = howmany(size, fs->fs_fsize);
672 if (fs->fs_contigsumsize > 0)
673 size += fs->fs_ncg * sizeof(int32_t);
674 size += fs->fs_ncg * sizeof(*fs->fs_contigdirs);
675 space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
676 fs->fs_csp = space;
677 for (i = 0; i < blks; i += fs->fs_frag) {
678 size = fs->fs_bsize;
679 if (i + fs->fs_frag > blks)
680 size = (blks - i) * fs->fs_fsize;
681 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
682 cred, &bp);
683 if (error) {
684 free(fs->fs_csp, M_UFSMNT);
685 goto out2;
686 }
687 #ifdef FFS_EI
688 if (needswap)
689 ffs_csum_swap((struct csum *)bp->b_data,
690 (struct csum *)space, size);
691 else
692 #endif
693 memcpy(space, bp->b_data, (u_int)size);
694
695 space = (char *)space + size;
696 brelse(bp);
697 bp = NULL;
698 }
699 if (fs->fs_contigsumsize > 0) {
700 fs->fs_maxcluster = lp = space;
701 for (i = 0; i < fs->fs_ncg; i++)
702 *lp++ = fs->fs_contigsumsize;
703 space = lp;
704 }
705 size = fs->fs_ncg * sizeof(*fs->fs_contigdirs);
706 fs->fs_contigdirs = space;
707 space = (char *)space + size;
708 memset(fs->fs_contigdirs, 0, size);
709 /* Compatibility for old filesystems - XXX */
710 if (fs->fs_avgfilesize <= 0)
711 fs->fs_avgfilesize = AVFILESIZ;
712 if (fs->fs_avgfpdir <= 0)
713 fs->fs_avgfpdir = AFPDIR;
714 mp->mnt_data = (qaddr_t)ump;
715 mp->mnt_stat.f_fsid.val[0] = (long)dev;
716 mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_FFS);
717 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
718 mp->mnt_flag |= MNT_LOCAL;
719 #ifdef FFS_EI
720 if (needswap)
721 ump->um_flags |= UFS_NEEDSWAP;
722 #endif
723 ump->um_mountp = mp;
724 ump->um_dev = dev;
725 ump->um_devvp = devvp;
726 ump->um_nindir = fs->fs_nindir;
727 ump->um_bptrtodb = fs->fs_fsbtodb;
728 ump->um_seqinc = fs->fs_frag;
729 for (i = 0; i < MAXQUOTAS; i++)
730 ump->um_quotas[i] = NULLVP;
731 devvp->v_specmountpoint = mp;
732 ump->um_savedmaxfilesize = fs->fs_maxfilesize; /* XXX */
733 maxfilesize = (u_int64_t)0x80000000 * fs->fs_bsize - 1; /* XXX */
734 if (fs->fs_maxfilesize > maxfilesize) /* XXX */
735 fs->fs_maxfilesize = maxfilesize; /* XXX */
736 if (ronly == 0 && (fs->fs_flags & FS_DOSOFTDEP)) {
737 error = softdep_mount(devvp, mp, fs, cred);
738 if (error) {
739 free(fs->fs_csp, M_UFSMNT);
740 goto out;
741 }
742 }
743 return (0);
744 out2:
745 free(fs, M_UFSMNT);
746 out:
747 devvp->v_specmountpoint = NULL;
748 if (bp)
749 brelse(bp);
750 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
751 (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p);
752 VOP_UNLOCK(devvp, 0);
753 if (ump) {
754 free(ump, M_UFSMNT);
755 mp->mnt_data = (qaddr_t)0;
756 }
757 return (error);
758 }
759
760 /*
761 * Sanity checks for old file systems.
762 *
763 * XXX - goes away some day.
764 */
765 int
766 ffs_oldfscompat(fs)
767 struct fs *fs;
768 {
769 int i;
770
771 fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect); /* XXX */
772 fs->fs_interleave = max(fs->fs_interleave, 1); /* XXX */
773 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
774 fs->fs_nrpos = 8; /* XXX */
775 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
776 u_int64_t sizepb = fs->fs_bsize; /* XXX */
777 /* XXX */
778 fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1; /* XXX */
779 for (i = 0; i < NIADDR; i++) { /* XXX */
780 sizepb *= NINDIR(fs); /* XXX */
781 fs->fs_maxfilesize += sizepb; /* XXX */
782 } /* XXX */
783 fs->fs_qbmask = ~fs->fs_bmask; /* XXX */
784 fs->fs_qfmask = ~fs->fs_fmask; /* XXX */
785 } /* XXX */
786 return (0);
787 }
788
789 /*
790 * unmount system call
791 */
792 int
793 ffs_unmount(mp, mntflags, p)
794 struct mount *mp;
795 int mntflags;
796 struct proc *p;
797 {
798 struct ufsmount *ump;
799 struct fs *fs;
800 int error, flags;
801
802 flags = 0;
803 if (mntflags & MNT_FORCE)
804 flags |= FORCECLOSE;
805 if (mp->mnt_flag & MNT_SOFTDEP) {
806 if ((error = softdep_flushfiles(mp, flags, p)) != 0)
807 return (error);
808 } else {
809 if ((error = ffs_flushfiles(mp, flags, p)) != 0)
810 return (error);
811 }
812 ump = VFSTOUFS(mp);
813 fs = ump->um_fs;
814 if (fs->fs_ronly == 0 &&
815 ffs_cgupdate(ump, MNT_WAIT) == 0 &&
816 fs->fs_clean & FS_WASCLEAN) {
817 if (mp->mnt_flag & MNT_SOFTDEP)
818 fs->fs_flags &= ~FS_DOSOFTDEP;
819 fs->fs_clean = FS_ISCLEAN;
820 (void) ffs_sbupdate(ump, MNT_WAIT);
821 }
822 if (ump->um_devvp->v_type != VBAD)
823 ump->um_devvp->v_specmountpoint = NULL;
824 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
825 error = VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD|FWRITE,
826 NOCRED, p);
827 vput(ump->um_devvp);
828 free(fs->fs_csp, M_UFSMNT);
829 free(fs, M_UFSMNT);
830 free(ump, M_UFSMNT);
831 mp->mnt_data = (qaddr_t)0;
832 mp->mnt_flag &= ~MNT_LOCAL;
833 return (error);
834 }
835
836 /*
837 * Flush out all the files in a filesystem.
838 */
839 int
840 ffs_flushfiles(mp, flags, p)
841 struct mount *mp;
842 int flags;
843 struct proc *p;
844 {
845 extern int doforce;
846 struct ufsmount *ump;
847 int error;
848
849 if (!doforce)
850 flags &= ~FORCECLOSE;
851 ump = VFSTOUFS(mp);
852 #ifdef QUOTA
853 if (mp->mnt_flag & MNT_QUOTA) {
854 int i;
855 if ((error = vflush(mp, NULLVP, SKIPSYSTEM|flags)) != 0)
856 return (error);
857 for (i = 0; i < MAXQUOTAS; i++) {
858 if (ump->um_quotas[i] == NULLVP)
859 continue;
860 quotaoff(p, mp, i);
861 }
862 /*
863 * Here we fall through to vflush again to ensure
864 * that we have gotten rid of all the system vnodes.
865 */
866 }
867 #endif
868 /*
869 * Flush all the files.
870 */
871 error = vflush(mp, NULLVP, flags);
872 if (error)
873 return (error);
874 /*
875 * Flush filesystem metadata.
876 */
877 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
878 error = VOP_FSYNC(ump->um_devvp, p->p_ucred, FSYNC_WAIT, 0, 0, p);
879 VOP_UNLOCK(ump->um_devvp, 0);
880 return (error);
881 }
882
883 /*
884 * Get file system statistics.
885 */
886 int
887 ffs_statfs(mp, sbp, p)
888 struct mount *mp;
889 struct statfs *sbp;
890 struct proc *p;
891 {
892 struct ufsmount *ump;
893 struct fs *fs;
894
895 ump = VFSTOUFS(mp);
896 fs = ump->um_fs;
897 if (fs->fs_magic != FS_MAGIC)
898 panic("ffs_statfs");
899 #ifdef COMPAT_09
900 sbp->f_type = 1;
901 #else
902 sbp->f_type = 0;
903 #endif
904 sbp->f_bsize = fs->fs_fsize;
905 sbp->f_iosize = fs->fs_bsize;
906 sbp->f_blocks = fs->fs_dsize;
907 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
908 fs->fs_cstotal.cs_nffree;
909 sbp->f_bavail = (long) (((u_int64_t) fs->fs_dsize * (u_int64_t)
910 (100 - fs->fs_minfree) / (u_int64_t) 100) -
911 (u_int64_t) (fs->fs_dsize - sbp->f_bfree));
912 sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO;
913 sbp->f_ffree = fs->fs_cstotal.cs_nifree;
914 if (sbp != &mp->mnt_stat) {
915 memcpy(sbp->f_mntonname, mp->mnt_stat.f_mntonname, MNAMELEN);
916 memcpy(sbp->f_mntfromname, mp->mnt_stat.f_mntfromname, MNAMELEN);
917 }
918 strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN);
919 return (0);
920 }
921
922 /*
923 * Go through the disk queues to initiate sandbagged IO;
924 * go through the inodes to write those that have been modified;
925 * initiate the writing of the super block if it has been modified.
926 *
927 * Note: we are always called with the filesystem marked `MPBUSY'.
928 */
929 int
930 ffs_sync(mp, waitfor, cred, p)
931 struct mount *mp;
932 int waitfor;
933 struct ucred *cred;
934 struct proc *p;
935 {
936 struct vnode *vp, *nvp;
937 struct inode *ip;
938 struct ufsmount *ump = VFSTOUFS(mp);
939 struct fs *fs;
940 int error, allerror = 0;
941
942 fs = ump->um_fs;
943 if (fs->fs_fmod != 0 && fs->fs_ronly != 0) { /* XXX */
944 printf("fs = %s\n", fs->fs_fsmnt);
945 panic("update: rofs mod");
946 }
947 /*
948 * Write back each (modified) inode.
949 */
950 simple_lock(&mntvnode_slock);
951 loop:
952 for (vp = LIST_FIRST(&mp->mnt_vnodelist); vp != NULL; vp = nvp) {
953 /*
954 * If the vnode that we are about to sync is no longer
955 * associated with this mount point, start over.
956 */
957 if (vp->v_mount != mp)
958 goto loop;
959 simple_lock(&vp->v_interlock);
960 nvp = LIST_NEXT(vp, v_mntvnodes);
961 ip = VTOI(vp);
962 if (vp->v_type == VNON ||
963 ((ip->i_flag &
964 (IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFIED | IN_ACCESSED)) == 0 &&
965 LIST_EMPTY(&vp->v_dirtyblkhd)))
966 {
967 simple_unlock(&vp->v_interlock);
968 continue;
969 }
970 simple_unlock(&mntvnode_slock);
971 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
972 if (error) {
973 simple_lock(&mntvnode_slock);
974 if (error == ENOENT)
975 goto loop;
976 continue;
977 }
978 if ((error = VOP_FSYNC(vp, cred,
979 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p)) != 0)
980 allerror = error;
981 vput(vp);
982 simple_lock(&mntvnode_slock);
983 }
984 simple_unlock(&mntvnode_slock);
985 /*
986 * Force stale file system control information to be flushed.
987 */
988 if (waitfor != MNT_LAZY) {
989 if (ump->um_mountp->mnt_flag & MNT_SOFTDEP)
990 waitfor = MNT_NOWAIT;
991 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
992 if ((error = VOP_FSYNC(ump->um_devvp, cred,
993 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p)) != 0)
994 allerror = error;
995 VOP_UNLOCK(ump->um_devvp, 0);
996 }
997 #ifdef QUOTA
998 qsync(mp);
999 #endif
1000 /*
1001 * Write back modified superblock.
1002 */
1003 if (fs->fs_fmod != 0) {
1004 fs->fs_fmod = 0;
1005 fs->fs_time = time.tv_sec;
1006 if ((error = ffs_cgupdate(ump, waitfor)))
1007 allerror = error;
1008 }
1009 return (allerror);
1010 }
1011
1012 /*
1013 * Look up a FFS dinode number to find its incore vnode, otherwise read it
1014 * in from disk. If it is in core, wait for the lock bit to clear, then
1015 * return the inode locked. Detection and handling of mount points must be
1016 * done by the calling routine.
1017 */
1018 int
1019 ffs_vget(mp, ino, vpp)
1020 struct mount *mp;
1021 ino_t ino;
1022 struct vnode **vpp;
1023 {
1024 struct fs *fs;
1025 struct inode *ip;
1026 struct ufsmount *ump;
1027 struct buf *bp;
1028 struct vnode *vp;
1029 dev_t dev;
1030 int error;
1031 caddr_t cp;
1032
1033 ump = VFSTOUFS(mp);
1034 dev = ump->um_dev;
1035
1036 if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
1037 return (0);
1038
1039 /* Allocate a new vnode/inode. */
1040 if ((error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, &vp)) != 0) {
1041 *vpp = NULL;
1042 return (error);
1043 }
1044
1045 /*
1046 * If someone beat us to it while sleeping in getnewvnode(),
1047 * push back the freshly allocated vnode we don't need, and return.
1048 */
1049 do {
1050 if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL) {
1051 ungetnewvnode(vp);
1052 return (0);
1053 }
1054 } while (lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
1055
1056 /*
1057 * XXX MFS ends up here, too, to allocate an inode. Should we
1058 * XXX create another pool for MFS inodes?
1059 */
1060 ip = pool_get(&ffs_inode_pool, PR_WAITOK);
1061 memset((caddr_t)ip, 0, sizeof(struct inode));
1062 vp->v_data = ip;
1063 ip->i_vnode = vp;
1064 ip->i_fs = fs = ump->um_fs;
1065 ip->i_dev = dev;
1066 ip->i_number = ino;
1067 #ifdef QUOTA
1068 {
1069 int i;
1070
1071 for (i = 0; i < MAXQUOTAS; i++)
1072 ip->i_dquot[i] = NODQUOT;
1073 }
1074 #endif
1075 /*
1076 * Put it onto its hash chain and lock it so that other requests for
1077 * this inode will block if they arrive while we are sleeping waiting
1078 * for old data structures to be purged or for the contents of the
1079 * disk portion of this inode to be read.
1080 */
1081 ufs_ihashins(ip);
1082 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
1083
1084 /* Read in the disk contents for the inode, copy into the inode. */
1085 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1086 (int)fs->fs_bsize, NOCRED, &bp);
1087 if (error) {
1088 /*
1089 * The inode does not contain anything useful, so it would
1090 * be misleading to leave it on its hash chain. With mode
1091 * still zero, it will be unlinked and returned to the free
1092 * list by vput().
1093 */
1094 vput(vp);
1095 brelse(bp);
1096 *vpp = NULL;
1097 return (error);
1098 }
1099 cp = (caddr_t)bp->b_data + (ino_to_fsbo(fs, ino) * DINODE_SIZE);
1100 #ifdef FFS_EI
1101 if (UFS_FSNEEDSWAP(fs))
1102 ffs_dinode_swap((struct dinode *)cp, &ip->i_din.ffs_din);
1103 else
1104 #endif
1105 memcpy(&ip->i_din.ffs_din, cp, DINODE_SIZE);
1106 if (DOINGSOFTDEP(vp))
1107 softdep_load_inodeblock(ip);
1108 else
1109 ip->i_ffs_effnlink = ip->i_ffs_nlink;
1110 brelse(bp);
1111
1112 /*
1113 * Initialize the vnode from the inode, check for aliases.
1114 * Note that the underlying vnode may have changed.
1115 */
1116 error = ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp);
1117 if (error) {
1118 vput(vp);
1119 *vpp = NULL;
1120 return (error);
1121 }
1122 /*
1123 * Finish inode initialization now that aliasing has been resolved.
1124 */
1125 ip->i_devvp = ump->um_devvp;
1126 VREF(ip->i_devvp);
1127 /*
1128 * Ensure that uid and gid are correct. This is a temporary
1129 * fix until fsck has been changed to do the update.
1130 */
1131 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
1132 ip->i_ffs_uid = ip->i_din.ffs_din.di_ouid; /* XXX */
1133 ip->i_ffs_gid = ip->i_din.ffs_din.di_ogid; /* XXX */
1134 } /* XXX */
1135
1136 *vpp = vp;
1137 return (0);
1138 }
1139
1140 /*
1141 * File handle to vnode
1142 *
1143 * Have to be really careful about stale file handles:
1144 * - check that the inode number is valid
1145 * - call ffs_vget() to get the locked inode
1146 * - check for an unallocated inode (i_mode == 0)
1147 * - check that the given client host has export rights and return
1148 * those rights via. exflagsp and credanonp
1149 */
1150 int
1151 ffs_fhtovp(mp, fhp, vpp)
1152 struct mount *mp;
1153 struct fid *fhp;
1154 struct vnode **vpp;
1155 {
1156 struct ufid *ufhp;
1157 struct fs *fs;
1158
1159 ufhp = (struct ufid *)fhp;
1160 fs = VFSTOUFS(mp)->um_fs;
1161 if (ufhp->ufid_ino < ROOTINO ||
1162 ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1163 return (ESTALE);
1164 return (ufs_fhtovp(mp, ufhp, vpp));
1165 }
1166
1167 /*
1168 * Vnode pointer to File handle
1169 */
1170 /* ARGSUSED */
1171 int
1172 ffs_vptofh(vp, fhp)
1173 struct vnode *vp;
1174 struct fid *fhp;
1175 {
1176 struct inode *ip;
1177 struct ufid *ufhp;
1178
1179 ip = VTOI(vp);
1180 ufhp = (struct ufid *)fhp;
1181 ufhp->ufid_len = sizeof(struct ufid);
1182 ufhp->ufid_ino = ip->i_number;
1183 ufhp->ufid_gen = ip->i_ffs_gen;
1184 return (0);
1185 }
1186
1187 void
1188 ffs_init()
1189 {
1190 if (ffs_initcount++ > 0)
1191 return;
1192
1193 softdep_initialize();
1194 ufs_init();
1195
1196 pool_init(&ffs_inode_pool, sizeof(struct inode), 0, 0, 0, "ffsinopl",
1197 0, pool_page_alloc_nointr, pool_page_free_nointr, M_FFSNODE);
1198 }
1199
1200 void
1201 ffs_done()
1202 {
1203 if (--ffs_initcount > 0)
1204 return;
1205
1206 /* XXX softdep cleanup ? */
1207 ufs_done();
1208 pool_destroy(&ffs_inode_pool);
1209 }
1210
1211 int
1212 ffs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
1213 int *name;
1214 u_int namelen;
1215 void *oldp;
1216 size_t *oldlenp;
1217 void *newp;
1218 size_t newlen;
1219 struct proc *p;
1220 {
1221 extern int doclusterread, doclusterwrite, doreallocblks, doasyncfree;
1222 extern int ffs_log_changeopt;
1223
1224 /* all sysctl names at this level are terminal */
1225 if (namelen != 1)
1226 return (ENOTDIR); /* overloaded */
1227
1228 switch (name[0]) {
1229 case FFS_CLUSTERREAD:
1230 return (sysctl_int(oldp, oldlenp, newp, newlen,
1231 &doclusterread));
1232 case FFS_CLUSTERWRITE:
1233 return (sysctl_int(oldp, oldlenp, newp, newlen,
1234 &doclusterwrite));
1235 case FFS_REALLOCBLKS:
1236 return (sysctl_int(oldp, oldlenp, newp, newlen,
1237 &doreallocblks));
1238 case FFS_ASYNCFREE:
1239 return (sysctl_int(oldp, oldlenp, newp, newlen, &doasyncfree));
1240 case FFS_LOG_CHANGEOPT:
1241 return (sysctl_int(oldp, oldlenp, newp, newlen,
1242 &ffs_log_changeopt));
1243 default:
1244 return (EOPNOTSUPP);
1245 }
1246 /* NOTREACHED */
1247 }
1248
1249 /*
1250 * Write a superblock and associated information back to disk.
1251 */
1252 int
1253 ffs_sbupdate(mp, waitfor)
1254 struct ufsmount *mp;
1255 int waitfor;
1256 {
1257 struct fs *fs = mp->um_fs;
1258 struct buf *bp;
1259 int i, error = 0;
1260 int32_t saved_nrpos = fs->fs_nrpos;
1261 int64_t saved_qbmask = fs->fs_qbmask;
1262 int64_t saved_qfmask = fs->fs_qfmask;
1263 u_int64_t saved_maxfilesize = fs->fs_maxfilesize;
1264 u_int8_t saveflag;
1265
1266 /* Restore compatibility to old file systems. XXX */
1267 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
1268 fs->fs_nrpos = -1; /* XXX */
1269 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
1270 int32_t *lp, tmp; /* XXX */
1271 /* XXX */
1272 lp = (int32_t *)&fs->fs_qbmask; /* XXX nuke qfmask too */
1273 tmp = lp[4]; /* XXX */
1274 for (i = 4; i > 0; i--) /* XXX */
1275 lp[i] = lp[i-1]; /* XXX */
1276 lp[0] = tmp; /* XXX */
1277 } /* XXX */
1278 fs->fs_maxfilesize = mp->um_savedmaxfilesize; /* XXX */
1279
1280 bp = getblk(mp->um_devvp, SBOFF >> (fs->fs_fshift - fs->fs_fsbtodb),
1281 (int)fs->fs_sbsize, 0, 0);
1282 saveflag = fs->fs_flags & FS_INTERNAL;
1283 fs->fs_flags &= ~FS_INTERNAL;
1284 memcpy(bp->b_data, fs, fs->fs_sbsize);
1285 #ifdef FFS_EI
1286 if (mp->um_flags & UFS_NEEDSWAP)
1287 ffs_sb_swap(fs, (struct fs*)bp->b_data);
1288 #endif
1289
1290 fs->fs_flags |= saveflag;
1291 fs->fs_nrpos = saved_nrpos; /* XXX */
1292 fs->fs_qbmask = saved_qbmask; /* XXX */
1293 fs->fs_qfmask = saved_qfmask; /* XXX */
1294 fs->fs_maxfilesize = saved_maxfilesize; /* XXX */
1295
1296 if (waitfor == MNT_WAIT)
1297 error = bwrite(bp);
1298 else
1299 bawrite(bp);
1300 return (error);
1301 }
1302
1303 int
1304 ffs_cgupdate(mp, waitfor)
1305 struct ufsmount *mp;
1306 int waitfor;
1307 {
1308 struct fs *fs = mp->um_fs;
1309 struct buf *bp;
1310 int blks;
1311 void *space;
1312 int i, size, error = 0, allerror = 0;
1313
1314 allerror = ffs_sbupdate(mp, waitfor);
1315 blks = howmany(fs->fs_cssize, fs->fs_fsize);
1316 space = fs->fs_csp;
1317 for (i = 0; i < blks; i += fs->fs_frag) {
1318 size = fs->fs_bsize;
1319 if (i + fs->fs_frag > blks)
1320 size = (blks - i) * fs->fs_fsize;
1321 bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
1322 size, 0, 0);
1323 #ifdef FFS_EI
1324 if (mp->um_flags & UFS_NEEDSWAP)
1325 ffs_csum_swap((struct csum*)space,
1326 (struct csum*)bp->b_data, size);
1327 else
1328 #endif
1329 memcpy(bp->b_data, space, (u_int)size);
1330 space = (char *)space + size;
1331 if (waitfor == MNT_WAIT)
1332 error = bwrite(bp);
1333 else
1334 bawrite(bp);
1335 }
1336 if (!allerror && error)
1337 allerror = error;
1338 return (allerror);
1339 }
1340