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