ffs_vfsops.c revision 1.80.2.3 1 /* $NetBSD: ffs_vfsops.c,v 1.80.2.3 2001/08/24 00:13:18 nathanw 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_OPT)
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/lwp.h>
49 #include <sys/proc.h>
50 #include <sys/kernel.h>
51 #include <sys/vnode.h>
52 #include <sys/socket.h>
53 #include <sys/mount.h>
54 #include <sys/buf.h>
55 #include <sys/device.h>
56 #include <sys/mbuf.h>
57 #include <sys/file.h>
58 #include <sys/disklabel.h>
59 #include <sys/ioctl.h>
60 #include <sys/errno.h>
61 #include <sys/malloc.h>
62 #include <sys/pool.h>
63 #include <sys/lock.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 const struct vnodeopv_desc * const 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->l_proc; /* 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 else
364 fs->fs_flags &= ~FS_DOSOFTDEP;
365 if (fs->fs_fmod != 0) { /* XXX */
366 fs->fs_fmod = 0;
367 if (fs->fs_clean & FS_WASCLEAN)
368 fs->fs_time = time.tv_sec;
369 else
370 printf("%s: file system not clean (fs_clean=%x); please fsck(8)\n",
371 mp->mnt_stat.f_mntfromname, fs->fs_clean);
372 (void) ffs_cgupdate(ump, MNT_WAIT);
373 }
374 return (0);
375 }
376
377 /*
378 * Reload all incore data for a filesystem (used after running fsck on
379 * the root filesystem and finding things to fix). The filesystem must
380 * be mounted read-only.
381 *
382 * Things to do to update the mount:
383 * 1) invalidate all cached meta-data.
384 * 2) re-read superblock from disk.
385 * 3) re-read summary information from disk.
386 * 4) invalidate all inactive vnodes.
387 * 5) invalidate all cached file data.
388 * 6) re-read inode data for all active vnodes.
389 */
390 int
391 ffs_reload(mountp, cred, p)
392 struct mount *mountp;
393 struct ucred *cred;
394 struct proc *p;
395 {
396 struct vnode *vp, *nvp, *devvp;
397 struct inode *ip;
398 struct buf *bp;
399 struct fs *fs, *newfs;
400 struct partinfo dpart;
401 int i, blks, size, error;
402 int32_t *lp;
403 caddr_t cp;
404
405 if ((mountp->mnt_flag & MNT_RDONLY) == 0)
406 return (EINVAL);
407 /*
408 * Step 1: invalidate all cached meta-data.
409 */
410 devvp = VFSTOUFS(mountp)->um_devvp;
411 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
412 error = vinvalbuf(devvp, 0, cred, p, 0, 0);
413 VOP_UNLOCK(devvp, 0);
414 if (error)
415 panic("ffs_reload: dirty1");
416 /*
417 * Step 2: re-read superblock from disk.
418 */
419 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, NOCRED, p) != 0)
420 size = DEV_BSIZE;
421 else
422 size = dpart.disklab->d_secsize;
423 error = bread(devvp, (ufs_daddr_t)(SBOFF / size), SBSIZE, NOCRED, &bp);
424 if (error) {
425 brelse(bp);
426 return (error);
427 }
428 fs = VFSTOUFS(mountp)->um_fs;
429 newfs = malloc(fs->fs_sbsize, M_UFSMNT, M_WAITOK);
430 memcpy(newfs, bp->b_data, fs->fs_sbsize);
431 #ifdef FFS_EI
432 if (VFSTOUFS(mountp)->um_flags & UFS_NEEDSWAP) {
433 ffs_sb_swap((struct fs*)bp->b_data, newfs);
434 fs->fs_flags |= FS_SWAPPED;
435 }
436 #endif
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 memcpy(&newfs->fs_csp[0], &fs->fs_csp[0], sizeof(fs->fs_csp));
449 newfs->fs_maxcluster = fs->fs_maxcluster;
450 newfs->fs_ronly = fs->fs_ronly;
451 memcpy(fs, newfs, (u_int)fs->fs_sbsize);
452 if (fs->fs_sbsize < SBSIZE)
453 bp->b_flags |= B_INVAL;
454 brelse(bp);
455 free(newfs, M_UFSMNT);
456 mountp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
457 ffs_oldfscompat(fs);
458 ffs_statfs(mountp, &mountp->mnt_stat, p);
459 /*
460 * Step 3: re-read summary information from disk.
461 */
462 blks = howmany(fs->fs_cssize, fs->fs_fsize);
463 for (i = 0; i < blks; i += fs->fs_frag) {
464 size = fs->fs_bsize;
465 if (i + fs->fs_frag > blks)
466 size = (blks - i) * fs->fs_fsize;
467 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
468 NOCRED, &bp);
469 if (error) {
470 brelse(bp);
471 return (error);
472 }
473 #ifdef FFS_EI
474 if (UFS_FSNEEDSWAP(fs))
475 ffs_csum_swap((struct csum*)bp->b_data,
476 (struct csum*)fs->fs_csp[fragstoblks(fs, i)], size);
477 else
478 #endif
479 memcpy(fs->fs_csp[fragstoblks(fs, i)], bp->b_data,
480 (size_t)size);
481 brelse(bp);
482 }
483 if ((fs->fs_flags & FS_DOSOFTDEP))
484 softdep_mount(devvp, mountp, fs, cred);
485 /*
486 * We no longer know anything about clusters per cylinder group.
487 */
488 if (fs->fs_contigsumsize > 0) {
489 lp = fs->fs_maxcluster;
490 for (i = 0; i < fs->fs_ncg; i++)
491 *lp++ = fs->fs_contigsumsize;
492 }
493
494 loop:
495 simple_lock(&mntvnode_slock);
496 for (vp = mountp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
497 if (vp->v_mount != mountp) {
498 simple_unlock(&mntvnode_slock);
499 goto loop;
500 }
501 nvp = vp->v_mntvnodes.le_next;
502 /*
503 * Step 4: invalidate all inactive vnodes.
504 */
505 if (vrecycle(vp, &mntvnode_slock, p))
506 goto loop;
507 /*
508 * Step 5: invalidate all cached file data.
509 */
510 simple_lock(&vp->v_interlock);
511 simple_unlock(&mntvnode_slock);
512 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
513 goto loop;
514 if (vinvalbuf(vp, 0, cred, p, 0, 0))
515 panic("ffs_reload: dirty2");
516 /*
517 * Step 6: re-read inode data for all active vnodes.
518 */
519 ip = VTOI(vp);
520 error = bread(devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
521 (int)fs->fs_bsize, NOCRED, &bp);
522 if (error) {
523 brelse(bp);
524 vput(vp);
525 return (error);
526 }
527 cp = (caddr_t)bp->b_data +
528 (ino_to_fsbo(fs, ip->i_number) * DINODE_SIZE);
529 #ifdef FFS_EI
530 if (UFS_FSNEEDSWAP(fs))
531 ffs_dinode_swap((struct dinode *)cp,
532 &ip->i_din.ffs_din);
533 else
534 #endif
535 memcpy(&ip->i_din.ffs_din, cp, DINODE_SIZE);
536 ip->i_ffs_effnlink = ip->i_ffs_nlink;
537 brelse(bp);
538 vput(vp);
539 simple_lock(&mntvnode_slock);
540 }
541 simple_unlock(&mntvnode_slock);
542 return (0);
543 }
544
545 /*
546 * Common code for mount and mountroot
547 */
548 int
549 ffs_mountfs(devvp, mp, p)
550 struct vnode *devvp;
551 struct mount *mp;
552 struct proc *p;
553 {
554 struct ufsmount *ump;
555 struct buf *bp;
556 struct fs *fs;
557 dev_t dev;
558 struct partinfo dpart;
559 caddr_t base, space;
560 int blks;
561 int error, i, size, ronly;
562 #ifdef FFS_EI
563 int needswap;
564 #endif
565 int32_t *lp;
566 struct ucred *cred;
567 u_int64_t maxfilesize; /* XXX */
568 u_int32_t sbsize;
569
570 dev = devvp->v_rdev;
571 cred = p ? p->p_ucred : NOCRED;
572 /*
573 * Disallow multiple mounts of the same device.
574 * Disallow mounting of a device that is currently in use
575 * (except for root, which might share swap device for miniroot).
576 * Flush out any old buffers remaining from a previous use.
577 */
578 if ((error = vfs_mountedon(devvp)) != 0)
579 return (error);
580 if (vcount(devvp) > 1 && devvp != rootvp)
581 return (EBUSY);
582 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
583 error = vinvalbuf(devvp, V_SAVE, cred, p, 0, 0);
584 VOP_UNLOCK(devvp, 0);
585 if (error)
586 return (error);
587
588 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
589 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
590 if (error)
591 return (error);
592 if (VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart, FREAD, cred, p) != 0)
593 size = DEV_BSIZE;
594 else
595 size = dpart.disklab->d_secsize;
596
597 bp = NULL;
598 ump = NULL;
599 error = bread(devvp, (ufs_daddr_t)(SBOFF / size), SBSIZE, cred, &bp);
600 if (error)
601 goto out;
602
603 fs = (struct fs*)bp->b_data;
604 if (fs->fs_magic == FS_MAGIC) {
605 sbsize = fs->fs_sbsize;
606 #ifdef FFS_EI
607 needswap = 0;
608 } else if (fs->fs_magic == bswap32(FS_MAGIC)) {
609 sbsize = bswap32(fs->fs_sbsize);
610 needswap = 1;
611 #endif
612 } else {
613 error = EINVAL;
614 goto out;
615 }
616 if (sbsize > MAXBSIZE || sbsize < sizeof(struct fs)) {
617 error = EINVAL;
618 goto out;
619 }
620
621 fs = malloc((u_long)sbsize, M_UFSMNT, M_WAITOK);
622 memcpy(fs, bp->b_data, sbsize);
623 #ifdef FFS_EI
624 if (needswap) {
625 ffs_sb_swap((struct fs*)bp->b_data, fs);
626 fs->fs_flags |= FS_SWAPPED;
627 }
628 #endif
629 ffs_oldfscompat(fs);
630
631 if (fs->fs_bsize > MAXBSIZE || fs->fs_bsize < sizeof(struct fs)) {
632 error = EINVAL;
633 goto out;
634 }
635 /* make sure cylinder group summary area is a reasonable size. */
636 if (fs->fs_cgsize == 0 || fs->fs_cpg == 0 ||
637 fs->fs_ncg > fs->fs_ncyl / fs->fs_cpg + 1 ||
638 fs->fs_cssize >
639 fragroundup(fs, fs->fs_ncg * sizeof(struct csum))) {
640 error = EINVAL; /* XXX needs translation */
641 goto out2;
642 }
643 /* XXX updating 4.2 FFS superblocks trashes rotational layout tables */
644 if (fs->fs_postblformat == FS_42POSTBLFMT && !ronly) {
645 error = EROFS; /* XXX what should be returned? */
646 goto out2;
647 }
648
649 ump = malloc(sizeof *ump, M_UFSMNT, M_WAITOK);
650 memset((caddr_t)ump, 0, sizeof *ump);
651 ump->um_fs = fs;
652 if (fs->fs_sbsize < SBSIZE)
653 bp->b_flags |= B_INVAL;
654 brelse(bp);
655 bp = NULL;
656 fs->fs_ronly = ronly;
657 if (ronly == 0) {
658 fs->fs_clean <<= 1;
659 fs->fs_fmod = 1;
660 }
661 size = fs->fs_cssize;
662 blks = howmany(size, fs->fs_fsize);
663 if (fs->fs_contigsumsize > 0)
664 size += fs->fs_ncg * sizeof(int32_t);
665 base = space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
666 for (i = 0; i < blks; i += fs->fs_frag) {
667 size = fs->fs_bsize;
668 if (i + fs->fs_frag > blks)
669 size = (blks - i) * fs->fs_fsize;
670 error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + i), size,
671 cred, &bp);
672 if (error) {
673 free(base, M_UFSMNT);
674 goto out2;
675 }
676 #ifdef FFS_EI
677 if (needswap)
678 ffs_csum_swap((struct csum*)bp->b_data,
679 (struct csum*)space, size);
680 else
681 #endif
682 memcpy(space, bp->b_data, (u_int)size);
683
684 fs->fs_csp[fragstoblks(fs, i)] = (struct csum *)space;
685 space += size;
686 brelse(bp);
687 bp = NULL;
688 }
689 if (fs->fs_contigsumsize > 0) {
690 fs->fs_maxcluster = lp = (int32_t *)space;
691 for (i = 0; i < fs->fs_ncg; i++)
692 *lp++ = fs->fs_contigsumsize;
693 }
694 mp->mnt_data = (qaddr_t)ump;
695 mp->mnt_stat.f_fsid.val[0] = (long)dev;
696 mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_FFS);
697 mp->mnt_maxsymlinklen = fs->fs_maxsymlinklen;
698 mp->mnt_fs_bshift = fs->fs_bshift;
699 mp->mnt_dev_bshift = DEV_BSHIFT; /* XXX */
700 mp->mnt_flag |= MNT_LOCAL;
701 #ifdef FFS_EI
702 if (needswap)
703 ump->um_flags |= UFS_NEEDSWAP;
704 #endif
705 ump->um_mountp = mp;
706 ump->um_dev = dev;
707 ump->um_devvp = devvp;
708 ump->um_nindir = fs->fs_nindir;
709 ump->um_lognindir = ffs(fs->fs_nindir) - 1;
710 ump->um_bptrtodb = fs->fs_fsbtodb;
711 ump->um_seqinc = fs->fs_frag;
712 for (i = 0; i < MAXQUOTAS; i++)
713 ump->um_quotas[i] = NULLVP;
714 devvp->v_specmountpoint = mp;
715 ump->um_savedmaxfilesize = fs->fs_maxfilesize; /* XXX */
716 maxfilesize = (u_int64_t)0x80000000 * fs->fs_bsize - 1; /* XXX */
717 if (fs->fs_maxfilesize > maxfilesize) /* XXX */
718 fs->fs_maxfilesize = maxfilesize; /* XXX */
719 if (ronly == 0 && (fs->fs_flags & FS_DOSOFTDEP)) {
720 error = softdep_mount(devvp, mp, fs, cred);
721 if (error) {
722 free(base, M_UFSMNT);
723 goto out;
724 }
725 }
726 return (0);
727 out2:
728 free(fs, M_UFSMNT);
729 out:
730 devvp->v_specmountpoint = NULL;
731 if (bp)
732 brelse(bp);
733 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
734 (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, cred, p);
735 VOP_UNLOCK(devvp, 0);
736 if (ump) {
737 free(ump, M_UFSMNT);
738 mp->mnt_data = (qaddr_t)0;
739 }
740 return (error);
741 }
742
743 /*
744 * Sanity checks for old file systems.
745 *
746 * XXX - goes away some day.
747 */
748 int
749 ffs_oldfscompat(fs)
750 struct fs *fs;
751 {
752 int i;
753
754 fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect); /* XXX */
755 fs->fs_interleave = max(fs->fs_interleave, 1); /* XXX */
756 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
757 fs->fs_nrpos = 8; /* XXX */
758 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
759 u_int64_t sizepb = fs->fs_bsize; /* XXX */
760 /* XXX */
761 fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1; /* XXX */
762 for (i = 0; i < NIADDR; i++) { /* XXX */
763 sizepb *= NINDIR(fs); /* XXX */
764 fs->fs_maxfilesize += sizepb; /* XXX */
765 } /* XXX */
766 fs->fs_qbmask = ~fs->fs_bmask; /* XXX */
767 fs->fs_qfmask = ~fs->fs_fmask; /* XXX */
768 } /* XXX */
769 return (0);
770 }
771
772 /*
773 * unmount system call
774 */
775 int
776 ffs_unmount(mp, mntflags, p)
777 struct mount *mp;
778 int mntflags;
779 struct proc *p;
780 {
781 struct ufsmount *ump;
782 struct fs *fs;
783 int error, flags;
784
785 flags = 0;
786 if (mntflags & MNT_FORCE)
787 flags |= FORCECLOSE;
788 if (mp->mnt_flag & MNT_SOFTDEP) {
789 if ((error = softdep_flushfiles(mp, flags, p)) != 0)
790 return (error);
791 } else {
792 if ((error = ffs_flushfiles(mp, flags, p)) != 0)
793 return (error);
794 }
795 ump = VFSTOUFS(mp);
796 fs = ump->um_fs;
797 if (fs->fs_ronly == 0 &&
798 ffs_cgupdate(ump, MNT_WAIT) == 0 &&
799 fs->fs_clean & FS_WASCLEAN) {
800 if (mp->mnt_flag & MNT_SOFTDEP)
801 fs->fs_flags &= ~FS_DOSOFTDEP;
802 fs->fs_clean = FS_ISCLEAN;
803 (void) ffs_sbupdate(ump, MNT_WAIT);
804 }
805 if (ump->um_devvp->v_type != VBAD)
806 ump->um_devvp->v_specmountpoint = NULL;
807 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
808 error = VOP_CLOSE(ump->um_devvp, fs->fs_ronly ? FREAD : FREAD|FWRITE,
809 NOCRED, p);
810 vput(ump->um_devvp);
811 free(fs->fs_csp[0], M_UFSMNT);
812 free(fs, M_UFSMNT);
813 free(ump, M_UFSMNT);
814 mp->mnt_data = (qaddr_t)0;
815 mp->mnt_flag &= ~MNT_LOCAL;
816 return (error);
817 }
818
819 /*
820 * Flush out all the files in a filesystem.
821 */
822 int
823 ffs_flushfiles(mp, flags, p)
824 struct mount *mp;
825 int flags;
826 struct proc *p;
827 {
828 extern int doforce;
829 struct ufsmount *ump;
830 int error;
831
832 if (!doforce)
833 flags &= ~FORCECLOSE;
834 ump = VFSTOUFS(mp);
835 #ifdef QUOTA
836 if (mp->mnt_flag & MNT_QUOTA) {
837 int i;
838 if ((error = vflush(mp, NULLVP, SKIPSYSTEM|flags)) != 0)
839 return (error);
840 for (i = 0; i < MAXQUOTAS; i++) {
841 if (ump->um_quotas[i] == NULLVP)
842 continue;
843 quotaoff(p, mp, i);
844 }
845 /*
846 * Here we fall through to vflush again to ensure
847 * that we have gotten rid of all the system vnodes.
848 */
849 }
850 #endif
851 /*
852 * Flush all the files.
853 */
854 error = vflush(mp, NULLVP, flags);
855 if (error)
856 return (error);
857 /*
858 * Flush filesystem metadata.
859 */
860 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
861 error = VOP_FSYNC(ump->um_devvp, p->p_ucred, FSYNC_WAIT, 0, 0, p);
862 VOP_UNLOCK(ump->um_devvp, 0);
863 return (error);
864 }
865
866 /*
867 * Get file system statistics.
868 */
869 int
870 ffs_statfs(mp, sbp, p)
871 struct mount *mp;
872 struct statfs *sbp;
873 struct proc *p;
874 {
875 struct ufsmount *ump;
876 struct fs *fs;
877
878 ump = VFSTOUFS(mp);
879 fs = ump->um_fs;
880 if (fs->fs_magic != FS_MAGIC)
881 panic("ffs_statfs");
882 #ifdef COMPAT_09
883 sbp->f_type = 1;
884 #else
885 sbp->f_type = 0;
886 #endif
887 sbp->f_bsize = fs->fs_fsize;
888 sbp->f_iosize = fs->fs_bsize;
889 sbp->f_blocks = fs->fs_dsize;
890 sbp->f_bfree = fs->fs_cstotal.cs_nbfree * fs->fs_frag +
891 fs->fs_cstotal.cs_nffree;
892 sbp->f_bavail = (long) (((u_int64_t) fs->fs_dsize * (u_int64_t)
893 (100 - fs->fs_minfree) / (u_int64_t) 100) -
894 (u_int64_t) (fs->fs_dsize - sbp->f_bfree));
895 sbp->f_files = fs->fs_ncg * fs->fs_ipg - ROOTINO;
896 sbp->f_ffree = fs->fs_cstotal.cs_nifree;
897 if (sbp != &mp->mnt_stat) {
898 memcpy(sbp->f_mntonname, mp->mnt_stat.f_mntonname, MNAMELEN);
899 memcpy(sbp->f_mntfromname, mp->mnt_stat.f_mntfromname, MNAMELEN);
900 }
901 strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN);
902 return (0);
903 }
904
905 /*
906 * Go through the disk queues to initiate sandbagged IO;
907 * go through the inodes to write those that have been modified;
908 * initiate the writing of the super block if it has been modified.
909 *
910 * Note: we are always called with the filesystem marked `MPBUSY'.
911 */
912 int
913 ffs_sync(mp, waitfor, cred, p)
914 struct mount *mp;
915 int waitfor;
916 struct ucred *cred;
917 struct proc *p;
918 {
919 struct vnode *vp, *nvp;
920 struct inode *ip;
921 struct ufsmount *ump = VFSTOUFS(mp);
922 struct fs *fs;
923 int error, allerror = 0;
924
925 fs = ump->um_fs;
926 if (fs->fs_fmod != 0 && fs->fs_ronly != 0) { /* XXX */
927 printf("fs = %s\n", fs->fs_fsmnt);
928 panic("update: rofs mod");
929 }
930 /*
931 * Write back each (modified) inode.
932 */
933 simple_lock(&mntvnode_slock);
934 loop:
935 for (vp = LIST_FIRST(&mp->mnt_vnodelist); vp != NULL; vp = nvp) {
936 /*
937 * If the vnode that we are about to sync is no longer
938 * associated with this mount point, start over.
939 */
940 if (vp->v_mount != mp)
941 goto loop;
942 simple_lock(&vp->v_interlock);
943 nvp = LIST_NEXT(vp, v_mntvnodes);
944 ip = VTOI(vp);
945 if (vp->v_type == VNON ||
946 ((ip->i_flag &
947 (IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFIED | IN_ACCESSED)) == 0 &&
948 LIST_EMPTY(&vp->v_dirtyblkhd) &&
949 vp->v_uvm.u_obj.uo_npages == 0))
950 {
951 simple_unlock(&vp->v_interlock);
952 continue;
953 }
954 simple_unlock(&mntvnode_slock);
955 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
956 if (error) {
957 simple_lock(&mntvnode_slock);
958 if (error == ENOENT)
959 goto loop;
960 continue;
961 }
962 if ((error = VOP_FSYNC(vp, cred,
963 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p)) != 0)
964 allerror = error;
965 vput(vp);
966 simple_lock(&mntvnode_slock);
967 }
968 simple_unlock(&mntvnode_slock);
969 /*
970 * Force stale file system control information to be flushed.
971 */
972 if (waitfor != MNT_LAZY) {
973 if (ump->um_mountp->mnt_flag & MNT_SOFTDEP)
974 waitfor = MNT_NOWAIT;
975 vn_lock(ump->um_devvp, LK_EXCLUSIVE | LK_RETRY);
976 if ((error = VOP_FSYNC(ump->um_devvp, cred,
977 waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p)) != 0)
978 allerror = error;
979 VOP_UNLOCK(ump->um_devvp, 0);
980 }
981 #ifdef QUOTA
982 qsync(mp);
983 #endif
984 /*
985 * Write back modified superblock.
986 */
987 if (fs->fs_fmod != 0) {
988 fs->fs_fmod = 0;
989 fs->fs_time = time.tv_sec;
990 if ((error = ffs_cgupdate(ump, waitfor)))
991 allerror = error;
992 }
993 return (allerror);
994 }
995
996 /*
997 * Look up a FFS dinode number to find its incore vnode, otherwise read it
998 * in from disk. If it is in core, wait for the lock bit to clear, then
999 * return the inode locked. Detection and handling of mount points must be
1000 * done by the calling routine.
1001 */
1002 int
1003 ffs_vget(mp, ino, vpp)
1004 struct mount *mp;
1005 ino_t ino;
1006 struct vnode **vpp;
1007 {
1008 struct fs *fs;
1009 struct inode *ip;
1010 struct ufsmount *ump;
1011 struct buf *bp;
1012 struct vnode *vp;
1013 dev_t dev;
1014 int error;
1015 caddr_t cp;
1016
1017 ump = VFSTOUFS(mp);
1018 dev = ump->um_dev;
1019
1020 if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL)
1021 return (0);
1022
1023 /* Allocate a new vnode/inode. */
1024 if ((error = getnewvnode(VT_UFS, mp, ffs_vnodeop_p, &vp)) != 0) {
1025 *vpp = NULL;
1026 return (error);
1027 }
1028
1029 /*
1030 * If someone beat us to it while sleeping in getnewvnode(),
1031 * push back the freshly allocated vnode we don't need, and return.
1032 */
1033 do {
1034 if ((*vpp = ufs_ihashget(dev, ino, LK_EXCLUSIVE)) != NULL) {
1035 ungetnewvnode(vp);
1036 return (0);
1037 }
1038 } while (lockmgr(&ufs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
1039
1040 /*
1041 * XXX MFS ends up here, too, to allocate an inode. Should we
1042 * XXX create another pool for MFS inodes?
1043 */
1044 ip = pool_get(&ffs_inode_pool, PR_WAITOK);
1045 memset((caddr_t)ip, 0, sizeof(struct inode));
1046 vp->v_data = ip;
1047 ip->i_vnode = vp;
1048 ip->i_fs = fs = ump->um_fs;
1049 ip->i_dev = dev;
1050 ip->i_number = ino;
1051 LIST_INIT(&ip->i_pcbufhd);
1052 #ifdef QUOTA
1053 {
1054 int i;
1055
1056 for (i = 0; i < MAXQUOTAS; i++)
1057 ip->i_dquot[i] = NODQUOT;
1058 }
1059 #endif
1060 /*
1061 * Put it onto its hash chain and lock it so that other requests for
1062 * this inode will block if they arrive while we are sleeping waiting
1063 * for old data structures to be purged or for the contents of the
1064 * disk portion of this inode to be read.
1065 */
1066 ufs_ihashins(ip);
1067 lockmgr(&ufs_hashlock, LK_RELEASE, 0);
1068
1069 /* Read in the disk contents for the inode, copy into the inode. */
1070 error = bread(ump->um_devvp, fsbtodb(fs, ino_to_fsba(fs, ino)),
1071 (int)fs->fs_bsize, NOCRED, &bp);
1072 if (error) {
1073 /*
1074 * The inode does not contain anything useful, so it would
1075 * be misleading to leave it on its hash chain. With mode
1076 * still zero, it will be unlinked and returned to the free
1077 * list by vput().
1078 */
1079 vput(vp);
1080 brelse(bp);
1081 *vpp = NULL;
1082 return (error);
1083 }
1084 cp = (caddr_t)bp->b_data + (ino_to_fsbo(fs, ino) * DINODE_SIZE);
1085 #ifdef FFS_EI
1086 if (UFS_FSNEEDSWAP(fs))
1087 ffs_dinode_swap((struct dinode *)cp, &ip->i_din.ffs_din);
1088 else
1089 #endif
1090 memcpy(&ip->i_din.ffs_din, cp, DINODE_SIZE);
1091 if (DOINGSOFTDEP(vp))
1092 softdep_load_inodeblock(ip);
1093 else
1094 ip->i_ffs_effnlink = ip->i_ffs_nlink;
1095 brelse(bp);
1096
1097 /*
1098 * Initialize the vnode from the inode, check for aliases.
1099 * Note that the underlying vnode may have changed.
1100 */
1101 error = ufs_vinit(mp, ffs_specop_p, ffs_fifoop_p, &vp);
1102 if (error) {
1103 vput(vp);
1104 *vpp = NULL;
1105 return (error);
1106 }
1107 /*
1108 * Finish inode initialization now that aliasing has been resolved.
1109 */
1110 ip->i_devvp = ump->um_devvp;
1111 VREF(ip->i_devvp);
1112 /*
1113 * Ensure that uid and gid are correct. This is a temporary
1114 * fix until fsck has been changed to do the update.
1115 */
1116 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
1117 ip->i_ffs_uid = ip->i_din.ffs_din.di_ouid; /* XXX */
1118 ip->i_ffs_gid = ip->i_din.ffs_din.di_ogid; /* XXX */
1119 } /* XXX */
1120 uvm_vnp_setsize(vp, ip->i_ffs_size);
1121
1122 *vpp = vp;
1123 return (0);
1124 }
1125
1126 /*
1127 * File handle to vnode
1128 *
1129 * Have to be really careful about stale file handles:
1130 * - check that the inode number is valid
1131 * - call ffs_vget() to get the locked inode
1132 * - check for an unallocated inode (i_mode == 0)
1133 * - check that the given client host has export rights and return
1134 * those rights via. exflagsp and credanonp
1135 */
1136 int
1137 ffs_fhtovp(mp, fhp, vpp)
1138 struct mount *mp;
1139 struct fid *fhp;
1140 struct vnode **vpp;
1141 {
1142 struct ufid *ufhp;
1143 struct fs *fs;
1144
1145 ufhp = (struct ufid *)fhp;
1146 fs = VFSTOUFS(mp)->um_fs;
1147 if (ufhp->ufid_ino < ROOTINO ||
1148 ufhp->ufid_ino >= fs->fs_ncg * fs->fs_ipg)
1149 return (ESTALE);
1150 return (ufs_fhtovp(mp, ufhp, vpp));
1151 }
1152
1153 /*
1154 * Vnode pointer to File handle
1155 */
1156 /* ARGSUSED */
1157 int
1158 ffs_vptofh(vp, fhp)
1159 struct vnode *vp;
1160 struct fid *fhp;
1161 {
1162 struct inode *ip;
1163 struct ufid *ufhp;
1164
1165 ip = VTOI(vp);
1166 ufhp = (struct ufid *)fhp;
1167 ufhp->ufid_len = sizeof(struct ufid);
1168 ufhp->ufid_ino = ip->i_number;
1169 ufhp->ufid_gen = ip->i_ffs_gen;
1170 return (0);
1171 }
1172
1173 void
1174 ffs_init()
1175 {
1176 if (ffs_initcount++ > 0)
1177 return;
1178
1179 softdep_initialize();
1180 ufs_init();
1181
1182 pool_init(&ffs_inode_pool, sizeof(struct inode), 0, 0, 0, "ffsinopl",
1183 0, pool_page_alloc_nointr, pool_page_free_nointr, M_FFSNODE);
1184 }
1185
1186 void
1187 ffs_done()
1188 {
1189 if (--ffs_initcount > 0)
1190 return;
1191
1192 /* XXX softdep cleanup ? */
1193 ufs_done();
1194 pool_destroy(&ffs_inode_pool);
1195 }
1196
1197 int
1198 ffs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
1199 int *name;
1200 u_int namelen;
1201 void *oldp;
1202 size_t *oldlenp;
1203 void *newp;
1204 size_t newlen;
1205 struct proc *p;
1206 {
1207 extern int doclusterread, doclusterwrite, doreallocblks, doasyncfree;
1208 extern int ffs_log_changeopt;
1209
1210 /* all sysctl names at this level are terminal */
1211 if (namelen != 1)
1212 return (ENOTDIR); /* overloaded */
1213
1214 switch (name[0]) {
1215 case FFS_CLUSTERREAD:
1216 return (sysctl_int(oldp, oldlenp, newp, newlen,
1217 &doclusterread));
1218 case FFS_CLUSTERWRITE:
1219 return (sysctl_int(oldp, oldlenp, newp, newlen,
1220 &doclusterwrite));
1221 case FFS_REALLOCBLKS:
1222 return (sysctl_int(oldp, oldlenp, newp, newlen,
1223 &doreallocblks));
1224 case FFS_ASYNCFREE:
1225 return (sysctl_int(oldp, oldlenp, newp, newlen, &doasyncfree));
1226 case FFS_LOG_CHANGEOPT:
1227 return (sysctl_int(oldp, oldlenp, newp, newlen,
1228 &ffs_log_changeopt));
1229 default:
1230 return (EOPNOTSUPP);
1231 }
1232 /* NOTREACHED */
1233 }
1234
1235 /*
1236 * Write a superblock and associated information back to disk.
1237 */
1238 int
1239 ffs_sbupdate(mp, waitfor)
1240 struct ufsmount *mp;
1241 int waitfor;
1242 {
1243 struct fs *fs = mp->um_fs;
1244 struct buf *bp;
1245 int i, error = 0;
1246 int32_t saved_nrpos = fs->fs_nrpos;
1247 int64_t saved_qbmask = fs->fs_qbmask;
1248 int64_t saved_qfmask = fs->fs_qfmask;
1249 u_int64_t saved_maxfilesize = fs->fs_maxfilesize;
1250 u_int8_t saveflag;
1251
1252 /* Restore compatibility to old file systems. XXX */
1253 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
1254 fs->fs_nrpos = -1; /* XXX */
1255 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
1256 int32_t *lp, tmp; /* XXX */
1257 /* XXX */
1258 lp = (int32_t *)&fs->fs_qbmask; /* XXX nuke qfmask too */
1259 tmp = lp[4]; /* XXX */
1260 for (i = 4; i > 0; i--) /* XXX */
1261 lp[i] = lp[i-1]; /* XXX */
1262 lp[0] = tmp; /* XXX */
1263 } /* XXX */
1264 fs->fs_maxfilesize = mp->um_savedmaxfilesize; /* XXX */
1265
1266 bp = getblk(mp->um_devvp, SBOFF >> (fs->fs_fshift - fs->fs_fsbtodb),
1267 (int)fs->fs_sbsize, 0, 0);
1268 saveflag = fs->fs_flags & FS_INTERNAL;
1269 fs->fs_flags &= ~FS_INTERNAL;
1270 memcpy(bp->b_data, fs, fs->fs_sbsize);
1271 #ifdef FFS_EI
1272 if (mp->um_flags & UFS_NEEDSWAP)
1273 ffs_sb_swap(fs, (struct fs*)bp->b_data);
1274 #endif
1275
1276 fs->fs_flags |= saveflag;
1277 fs->fs_nrpos = saved_nrpos; /* XXX */
1278 fs->fs_qbmask = saved_qbmask; /* XXX */
1279 fs->fs_qfmask = saved_qfmask; /* XXX */
1280 fs->fs_maxfilesize = saved_maxfilesize; /* XXX */
1281
1282 if (waitfor == MNT_WAIT)
1283 error = bwrite(bp);
1284 else
1285 bawrite(bp);
1286 return (error);
1287 }
1288
1289 int
1290 ffs_cgupdate(mp, waitfor)
1291 struct ufsmount *mp;
1292 int waitfor;
1293 {
1294 struct fs *fs = mp->um_fs;
1295 struct buf *bp;
1296 int blks;
1297 caddr_t space;
1298 int i, size, error = 0, allerror = 0;
1299
1300 allerror = ffs_sbupdate(mp, waitfor);
1301 blks = howmany(fs->fs_cssize, fs->fs_fsize);
1302 space = (caddr_t)fs->fs_csp[0];
1303 for (i = 0; i < blks; i += fs->fs_frag) {
1304 size = fs->fs_bsize;
1305 if (i + fs->fs_frag > blks)
1306 size = (blks - i) * fs->fs_fsize;
1307 bp = getblk(mp->um_devvp, fsbtodb(fs, fs->fs_csaddr + i),
1308 size, 0, 0);
1309 #ifdef FFS_EI
1310 if (mp->um_flags & UFS_NEEDSWAP)
1311 ffs_csum_swap((struct csum*)space,
1312 (struct csum*)bp->b_data, size);
1313 else
1314 #endif
1315 memcpy(bp->b_data, space, (u_int)size);
1316 space += size;
1317 if (waitfor == MNT_WAIT)
1318 error = bwrite(bp);
1319 else
1320 bawrite(bp);
1321 }
1322 if (!allerror && error)
1323 allerror = error;
1324 return (allerror);
1325 }
1326