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