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